Skip to content

Instantly share code, notes, and snippets.

@michaeluno
Created July 21, 2017 15:24
Show Gist options
  • Save michaeluno/3c1d4d1ca39305787780f882fd7bb080 to your computer and use it in GitHub Desktop.
Save michaeluno/3c1d4d1ca39305787780f882fd7bb080 to your computer and use it in GitHub Desktop.
Demonstrates sortable set of checkboxes with different labels in Admin Page Framework.
<?php
/**
* Plugin Name: APF - Sortable Checkboxes
*/
class APFTest_SortableCheckboxesBootstrap {
static public $sFilePath = __FILE__;
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'loadPlugin' ) );
}
public function loadPlugin() {
include( dirname( __FILE__ ) . '/APFTest_SortableCheckboxes.php' );
}
}
new APFTest_SortableCheckboxesBootstrap;
<?php
class APFTest_SortableCheckboxes extends AdminPageFramework {
public function setUp() {
// To reset the options uncomment the next line.
// delete_option( get_class( $this ) );
$this->setRootMenuPage( __( 'Sortable Checkboxes', 'apf-test-sortable-checkboxes' ) );
$this->addSubMenuItems(
array(
'title' => __( 'Settings', 'apf-test-sortable-checkboxes' ),
'page_slug' => 'apftsc_settings'
)
);
}
public function do_apftsc_settings( $oFactory ) {
echo "<h3>Saved Data</h3>";
AdminPageFramework_Debug::dump( $this->oProp->aOptions );
}
public function load_apftsc_settings( $oFactory ) {
// Section
$oFactory->addSettingSections(
'apftsc_settings', // the target page slug
array(
'section_id' => 'sortable_checkboxes_section',
'title' => __( 'Sortable Checkboxes', 'apf-test-sortable-checkboxes' ),
)
);
$oFactory->addSettingFields(
'sortable_checkboxes_section', // the target section ID
array(
'field_id' => 'checkbox_number_select',
'type' => 'inline_mixed',
'title' => __( 'Checkbox, Number & Select', 'admin-page-framework-loader' ),
'sortable' => true,
'content' => $this->___getFirstRowCheckboxes(),
array( // Second field
'content' => $this->___getSecondRowCheckboxes(),
),
array( // Third field
'content' => $this->___getThirdRowCheckboxes(),
),
),
array(
'field_id' => '_submit',
'type' => 'submit',
'save' => false,
'show_title_column' => false,
)
);
}
private function ___getFirstRowCheckboxes() {
$_aSavedCheckboxes = $this->oUtil->getElement(
$this->oProp->aOptions,
array( 'sortable_checkboxes_section', 'checkbox_number_select', 0 ),
array(
'a' => 0,
'b' => 1, // checked
'c' => 0,
)
);
return $this->___getCheckboxLabels( $_aSavedCheckboxes );
}
private function ___getSecondRowCheckboxes() {
$_aSavedCheckboxes = $this->oUtil->getElement(
$this->oProp->aOptions,
array( 'sortable_checkboxes_section', 'checkbox_number_select', 1 ),
array(
'x' => 0,
'y' => 0,
'z' => 1,
)
);
return $this->___getCheckboxLabels( $_aSavedCheckboxes );
}
private function ___getThirdRowCheckboxes() {
$_aSavedCheckboxes = $this->oUtil->getElement(
$this->oProp->aOptions,
array( 'sortable_checkboxes_section', 'checkbox_number_select', 2 ),
array(
'one' => 1,
'two' => 1,
'three' => 0,
)
);
return $this->___getCheckboxLabels( $_aSavedCheckboxes );
}
private function ___getCheckboxLabels( array $aSavedCheckboxes ) {
$_aCheckboxes = array();
foreach( $aSavedCheckboxes as $_sKey => $_iValue ) {
$_aCheckboxes[] = array(
'field_id' => $_sKey,
'type' => 'checkbox',
'label' => $this->___getCheckboxLabelByKey( $_sKey ),
'value' => $_iValue,
);
}
return $_aCheckboxes;
}
private function ___getCheckboxLabelByKey( $sKey ) {
$_aLabels = array(
'a' => 'A',
'b' => 'B',
'c' => 'C',
'x' => 'X',
'y' => 'Y',
'z' => 'Z',
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
);
return isset( $_aLabels[ $sKey ] )
? $_aLabels[ $sKey ]
: '';
}
}
new APFTest_SortableCheckboxes( null, APFTest_SortableCheckboxesBootstrap::$sFilePath );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment