michaeluno/admin-page-framework

Repeatable options cannot be saved properly when namespace declared.

Merasmiz opened this issue · 2 comments

Hello,

I tried to create a repeatable section and isolate them with namespaces to avoid conflicts when multiple plugins were installed.

But the newly created option always overwrite the old options, only the last option can be saved.

If I don't use namespace, then it works perfectly.

I followed this tutorial:
http://admin-page-framework.michaeluno.jp/tutorials/06-creating-repeatable-section-tabs/

Here is my code in setting page.

<?php

namespace PostStatusBroadcaster\admin\ConfigPages;

class Test extends \PostStatusBroadcaster_AdminPageFramework
{
	// Define the setup() method to set how many pages, page titles and icons etc.
	public function setUp()
	{
		// Set the root menu
		$this->setRootMenuPage('Settings');        // specifies to which parent menu to add.
		// Add the sub menus and the pages
		$this->addSubMenuItems(
			array(
				'title'        => '6. Advanced Sections',        // the page and menu title
				'page_slug'    => 'advanced_sections'        // the page slug
			)
		);
		// Add form sections
		$this->addSettingSections(
			'advanced_sections',    // target page slug
			array(
				'section_id'        => 'repeatable_tabbed_section',
				'section_tab_slug'  => 'repeatable_sections',
				'repeatable'        => true,
				'title'             => 'Repeatable Tabbed Section',
				'description'       => 'This section is a repeatable tabbed section.',
			)
		);
		// Add form fields
		$this->addSettingFields(
			'repeatable_tabbed_section',    // target page slug
			array(
				'field_id'   => 'my_section_title',
				'type'       => 'section_title',
			),
			array(
				'field_id'   => 'my_color',
				'type'       => 'color',
				'title'      => 'Color',
				'repeatable' => true,
				'sortable'   => true,
			),
			array(
				'field_id'   => 'my_image',
				'type'       => 'image',
				'title'      => 'Image',
				'repeatable' => true,
				'sortable'   => true,
				'attributes' => array(
					'style' => 'max-width: 300px;',
				)
			)
		);
	}
	/**
	 * @callback        action      do_ + page slug
	 */
	public function do_advanced_sections()
	{
		submit_button();
		/**
		 *Show the saved option value.
		 * The extended class name is used as the option key. This can be changed by passing a custom string to the constructor.
		 */
		echo '<h3>Options as an array</h4>';
		echo $this->oDebug->get(get_option('APF_AdvancedSections'));
		echo '<h3>Retrieve individual field values</h4>';
		echo '<pre>APF_AdvancedSections[repeatable_tabbed_section][0][my_section_title]: ' . \PostStatusBroadcaster_AdminPageFramework::getOption('APF_AdvancedSections', array('repeatable_tabbed_section', '0', 'my_section_title'), 'default title value') . '</pre>';
		echo '<pre>APF_AdvancedSections[repeatable_tabbed_section][1][my_color]: ' . \PostStatusBroadcaster_AdminPageFramework::getOption('APF_AdvancedSections', array('repeatable_tabbed_section', '0', 'color'), 'default color value') . '</pre>';
	}
}
// Instantiate the class object.
new \PostStatusBroadcaster\admin\ConfigPages\Test;
// new Test;

Try setting the option key with the class constructor and then re-save the form.

new \PostStatusBroadcaster\admin\ConfigPages\Test( 'APF_AdvancedSections' );

@michaeluno Problem solved, it works like a charm! Thanks.