Last active
August 29, 2015 14:15
-
-
Save michaeluno/bb1b9a71d3d992981972 to your computer and use it in GitHub Desktop.
An Admin Page Framework custom field type with image and multiple drop-down lists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Plugin Name: Admin Page Framework Test - Image and Drop-down Field Type */ | |
// Include the framework. Set your library path here. | |
$_bIncluded = true; | |
if ( ! class_exists( 'AdminPageFramework' ) ) { | |
$_bIncluded = include( dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php' ); | |
} | |
if ( ! $_bIncluded ) { | |
return; | |
} | |
class APF_ImageAndDropDownFieldType extends AdminPageFramework { | |
public function setUp() { | |
$this->setRootMenuPage( 'Image and Drop-down' ); // create a root page | |
$this->addSubMenuItem( | |
array( | |
'title' => __( 'Image and Drop-down' ), | |
'page_slug' => 'image_and_drop_down', | |
) | |
); | |
} | |
/** | |
* The pre-defined callback method that is triggered when the page loads. | |
*/ | |
public function load_image_and_drop_down( $oAdminPage ) { // load_{page slug} | |
if ( include( dirname( __FILE__ ) . '/ImageAndDropDownsFieldType.php' ) ) { | |
new ImageAndDropDownsFieldType( get_class( $this ) ); | |
} | |
$this->addSettingFields( | |
array( | |
'field_id' => 'testing', | |
'title' => __( 'Image and Drop-down' ), | |
'type' => 'image_and_drop_down', | |
'repeatable' => true, | |
'sortable' => true, | |
'select_fields' => array( | |
array( | |
'label' => array( | |
'one' => 'One', | |
'two' => 'Two', | |
'three' => 'Three', | |
), | |
), | |
array( | |
'label' => array( | |
'i' => 'i', | |
'ii' => 'ii', | |
'iii' => 'iii', | |
), | |
), | |
array( | |
'label' => array( | |
'apple' => 'apple', | |
'banana' => 'banana', | |
'cherry' => 'cherry', | |
), | |
), | |
array( | |
'label' => array( | |
'foo' => 'Foo', | |
'bar' => 'Bar', | |
), | |
), | |
), | |
), | |
array( | |
'field_id' => 'submit', | |
'type' => 'submit', | |
) | |
); | |
} | |
} | |
new APF_ImageAndDropDownFieldType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* An Admin Page Framework custom field type that has one image input and 5 drop-down lists. | |
* | |
* @version 0.0.2 | |
*/ | |
if ( class_exists( 'AdminPageFramework_FieldType_image' ) && ! class_exists( 'ImageAndDropDownsFieldType' ) ) : | |
class ImageAndDropDownsFieldType extends AdminPageFramework_FieldType_image { | |
/** | |
* Defines the field type slugs used for this field type. | |
*/ | |
public $aFieldTypeSlugs = array( 'image_and_drop_down', ); | |
/** | |
* Defines the default key-values of this field type. | |
* | |
* @remark $_aDefaultKeys holds shared default key-values defined in the base class. | |
*/ | |
protected $aDefaultKeys = array( | |
'attributes_to_store' => array(), // ( array ) This is for the image and media field type. The attributes to save besides URL. e.g. ( for the image field type ) array( 'title', 'alt', 'width', 'height', 'caption', 'id', 'align', 'link' ). | |
'show_preview' => true, // ( boolean ) Indicates whether the image preview should be displayed or not. | |
'allow_external_source' => true, // ( boolean ) Indicates whether the media library box has the From URL tab. | |
'attributes' => array( | |
'input' => array( | |
'size' => 40, | |
'maxlength' => 400, | |
), | |
'button' => array( | |
), | |
'remove_button' => array( | |
), | |
'preview' => array( | |
'style' => 'max-width: 200px;', | |
), | |
), | |
'select_fields' => array( | |
// e.g. | |
// array( | |
// 'label' => array( | |
// 'a' => 'A', | |
// 'b' => 'B', | |
// 'c' => 'C', | |
// ), | |
// ), | |
// array( | |
// 'label' => array( | |
// 'x' => 'X', | |
// 'y' => 'Y', | |
// 'z' => 'Z', | |
// ), | |
// ), | |
), | |
); | |
/** | |
* Returns the output of the form field. | |
*/ | |
protected function getField( $aField ) { | |
return $this->_getImageFieldOutput( $aField ) | |
. $this->_getSelectFieldsOutput( $aField ); | |
} | |
/** | |
* Returns the image field output. | |
*/ | |
private function _getImageFieldOutput( $aField ) { | |
return parent::getField( | |
$this->uniteArrays( | |
array( | |
'type' => 'image', | |
'input_id' => $aField['attributes']['id'] . '_image', | |
'attributes' => array( | |
'id' => $aField['attributes']['id'] . '_image', | |
'name' => $aField['attributes']['name'] . '[image]', | |
'value' => isset( $aField['value']['image'] ) | |
? $aField['value']['image'] | |
: '', | |
), | |
), | |
$aField | |
) | |
); | |
} | |
/** | |
* Returns the select fields output. | |
*/ | |
private function _getSelectFieldsOutput( $aField ) { | |
$_aOutput = array(); | |
foreach( $aField['select_fields'] as $_isIndex => $_aSelectField ) { | |
$_aSelectField = $this->_formatSelectFieldDefitnitionArray( $_aSelectField, $aField ); | |
$_aAttributes = $this->_formatSelectFieldAttributes( $_aSelectField, $_isIndex ); | |
$_aOutput[] = $this->_getSelectFieldOutput( $_aAttributes, $_aSelectField ); | |
} | |
return implode( '', $_aOutput ); | |
} | |
/** | |
* Formats an attributes array of the select field type. | |
* @return array The formatted attributes array of the select field type. | |
*/ | |
private function _formatSelectFieldAttributes( array $_aSelectField, $_isIndex ) { | |
return array( | |
'type' => null, | |
'id' => $_aSelectField['attributes']['id'] . "_select_{$_isIndex}", | |
'name' => $_aSelectField['attributes']['name'] . "[select_{$_isIndex}]", | |
'value' => isset( $_aSelectField['value'][ "select_{$_isIndex}" ] ) | |
? $_aSelectField['value'][ "select_{$_isIndex}" ] | |
: null, | |
) + $_aSelectField['attributes']; | |
} | |
/** | |
* Formats a field definition array of the select field type. | |
* @return array The formatted select field definition array. | |
*/ | |
private function _formatSelectFieldDefitnitionArray( array $_aSelectField, array $aField ) { | |
return $this->uniteArrays( | |
$_aSelectField + $aField, | |
array( | |
'label' => array(), | |
'is_multiple' => false, | |
'label_min_width' => 120, | |
'attributes' => array( | |
'select' => array( | |
'size' => 1, | |
'autofocusNew' => null, | |
'multiple' => null, | |
'required' => null, | |
), | |
'optgroup' => array(), | |
'option' => array(), | |
) | |
) | |
); | |
} | |
/** | |
* Returns the select field output. | |
*/ | |
private function _getSelectFieldOutput( array $aAttributes, array $aSelectField ) { | |
$_oSelectInput = new AdminPageFramework_Input_select( $aAttributes ); | |
if ( $aSelectField['is_multiple'] ) { | |
$_oSelectInput->setAttribute( array( 'select', 'multiple' ), 'multiple' ); | |
} | |
return "<div class='admin-page-framework-input-label-container admin-page-framework-select-label' style='min-width: " . $this->sanitizeLength( $aSelectField['label_min_width'] ) . ";'>" | |
. "<label for='{$aAttributes['id']}'>" | |
. $_oSelectInput->get( $aSelectField['label'] ) | |
. "</label>" | |
. "</div>"; | |
} | |
} | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment