Skip to content
Permalink
Browse files

Fixed a bug that the `save` argument did not take effect for User Met…

…a and Page Meta Box factories. Fixes #275.
  • Loading branch information...
michaeluno committed Jul 8, 2018
1 parent 4e9be0e commit 86468e5adfd80500edcdf2aeafd051130bfd0625
@@ -9,7 +9,9 @@
/**
* Provides methods to build forms for the user meta and post meta structure type.
*
*
* This is used by taxonomy meta, user meta, and post meta form classes.
*
* @package AdminPageFramework/Common/Form
* @since 3.7.0
* @extends AdminPageFramework_Form
@@ -31,7 +33,7 @@ class AdminPageFramework_Form_Meta extends AdminPageFramework_Form {
* @return void
*/
public function updateMetaDataByType( $iObjectID, array $aInput, array $aSavedMeta, $sStructureType='post_meta_box' ) {
if ( ! $iObjectID ) {
return;
}
@@ -47,11 +49,11 @@ public function updateMetaDataByType( $iObjectID, array $aInput, array $aSavedMe
$_sFunctionName = $this->getElement( $_aFunctionNameMapByFieldsType, $sStructureType );
// 3.6.0+ Unset field elements that the 'save' argument is false.
$aInput = $this->_getInputByUnset( $aInput );
$aInput = $this->getInputsUnset( $aInput, $this->sStructureType );
// Loop through sections/fields and save the data.
foreach ( $aInput as $_sSectionOrFieldID => $_vValue ) {
$this->_updateMetaDatumByFuncitonName(
$this->_updateMetaDatumByFunctionName(
$iObjectID,
$_vValue,
$aSavedMeta,
@@ -60,42 +62,7 @@ public function updateMetaDataByType( $iObjectID, array $aInput, array $aSavedMe
);
}
}
/**
* Removes elements whose 'save' argument is false.
* @since 3.6.0
* @since 3.7.0 Moved from `AdminPageFramework_FormDefinition_Meta`.
* @return array
*/
private function _getInputByUnset( array $aInput ) {
$_sUnsetKey = '__unset_' . $this->sStructureType;
if ( ! isset( $_POST[ $_sUnsetKey ] ) ) {
return $aInput;
}
$_aUnsetElements = array_unique( $_POST[ $_sUnsetKey ] );
foreach( $_aUnsetElements as $_sFlatInputName ) {
$_aDimensionalKeys = explode( '|', $_sFlatInputName );
if ( ! isset( $_aDimensionalKeys[ 0 ] ) ) {
continue;
}
// The first element is the option key; the section or field dimensional keys follow.
if ( '__dummy_option_key' === $_aDimensionalKeys[ 0 ] ) {
array_shift( $_aDimensionalKeys );
}
$this->unsetDimensionalArrayElement(
$aInput,
$_aDimensionalKeys
);
}
return $aInput;
}
}
/**
* Saves an individual meta datum with the given section or field ID with the given function name.
@@ -105,7 +72,7 @@ private function _getInputByUnset( array $aInput ) {
* @since 3.7.0 Moved from `AdminPageFramework_FormDefinition_Meta`.
* @return void
*/
private function _updateMetaDatumByFuncitonName( $iObjectID, $_vValue, array $aSavedMeta, $_sSectionOrFieldID, $_sFunctionName ) {
private function _updateMetaDatumByFunctionName( $iObjectID, $_vValue, array $aSavedMeta, $_sSectionOrFieldID, $_sFunctionName ) {
if ( is_null( $_vValue ) ) {
return;
@@ -16,7 +16,67 @@
* @internal
*/
abstract class AdminPageFramework_Form_Utility extends AdminPageFramework_FrameworkUtility {
/**
* Removes form input elements whose 'save' argument is false.
*
* The inputs array structure looks like the following.
* ```
* [example_section] => Array
* (
* [example_text] => (string, length: 5) hello
* [__submit] => (string, length: 6) Submit
* )
* ```
* The unset keys stored in $_POST looks like the following. The pipe (|) character is used to delimit dimensional elements.
* ```
* [example_section|example_text] => (string, length: 28) example_section|example_text
* [example_section|__submit] => (string, length: 24) example_section|__submit
* ```
*
* @return array
* @since 3.8.17
* @param array $aInputs The inputs array to parse.
* @param string $sFieldsType The subject fields type (factory structure type).
* @param integer $iSkipDepth The dimensional depth to skip.
* Depending on the fields type (structure type), dimensional keys are prepended.
* For the `admin_page` fields type, an option key is prepended.
* For `page_meta_box`, no prepended element and it starts with the section or field ID.
*/
static public function getInputsUnset( array $aInputs, $sFieldsType, $iSkipDepth=0 ) {
$_sUnsetKey = '__unset_' . $sFieldsType;
if ( ! isset( $_POST[ $_sUnsetKey ] ) ) {
return $aInputs;
}
$_aUnsetElements = array_unique( $_POST[ $_sUnsetKey ] );
foreach( $_aUnsetElements as $_sFlatInputName ) {
$_aDimensionalKeys = explode( '|', $_sFlatInputName );
// For form sections, a dummy key is set
if ( '__dummy_option_key' === $_aDimensionalKeys[ 0 ] ) {
array_shift( $_aDimensionalKeys );
}
// The first element is the option key for the `admin_page` field type and section or field dimensional keys follow.
for ( $_i = 0; $_i < $iSkipDepth; $_i++) {
unset( $_aDimensionalKeys[ $_i ] );
}
self::unsetDimensionalArrayElement(
$aInputs,
$_aDimensionalKeys
);
}
return $aInputs;
}
/**
* @since 3.7.0
* @return array
@@ -13,11 +13,12 @@
* @package AdminPageFramework/Factory/AdminPage/Model
* @extends AdminPageFramework_WPUtility
* @since 3.6.3
* @since 3.8.17 Made it extend `AdminPageFramework_Form_Utility` from `AdminPageFramework_FrameworkUtility`.
* @internal
* @extends AdminPageFramework_FrameworkUtility
* @extends AdminPageFramework_Form_Utility
*/
abstract class AdminPageFramework_Model__FormSubmission_Base extends AdminPageFramework_FrameworkUtility {
abstract class AdminPageFramework_Model__FormSubmission_Base extends AdminPageFramework_Form_Utility {
/**
* Retrieves the target key's value associated with the given data to a custom submit button.
*
@@ -111,7 +111,7 @@ private function _getFiltered( $aInputs, $aRawInputs, $aStoredData, $aSubmitInfo
$_aInput = $this->getAsArray( $_aInput );
// Drop input elements with the `save` argument of false.
$_aInput = $this->_getInputsUnset( $_aInput, $this->oFactory->oProp->sStructureType );
$_aInput = $this->getInputsUnset( $_aInput, $this->oFactory->oProp->sStructureType, 1 );
// If everything fine, return the filtered input data.
$this->_bHasFieldErrors = $this->oFactory->hasFieldError();
@@ -145,37 +145,6 @@ public function _replyToSetStatus( $aStatus ) {
) + $aStatus;
}
/**
* Removes elements whose 'save' argument is false.
* @return array
* @since 3.6.0
* @deprecated 3.8.17 Use `_getInputsUnset()`
*/
private function _getInputByUnset( array $aInputs ) {
AdminPageFramework_Debug::log( $aInputs );
$_sUnsetKey = '__unset_' . $this->oFactory->oProp->sStructureType;
if ( ! isset( $_POST[ $_sUnsetKey ] ) ) {
AdminPageFramework_Debug::log( '$_POST ' . $_sUnsetKey . ' is not set' );
return $aInputs;
}
$_aUnsetElements = array_unique( $_POST[ $_sUnsetKey ] );
foreach( $_aUnsetElements as $_sFlatInputName ) {
$_aDimensionalKeys = explode( '|', $_sFlatInputName );
// The first element is the option key; the section or field dimensional keys follow.
unset( $_aDimensionalKeys[ 0 ] );
$this->unsetDimensionalArrayElement(
$aInputs,
$_aDimensionalKeys
);
}
AdminPageFramework_Debug::log( $aInputs );
return $aInputs;
}
/**
* Validates each field or section.
*
@@ -16,21 +16,31 @@
class APF_Demo_MyUserMeta extends AdminPageFramework_UserMeta {
public function setUp() {
$_sSectionID = 'apf_user_meta_demo';
$this->addSettingSections(
array(
'section_id' => $_sSectionID, // avoid hyphen(dash), dots, and white spaces
'title' => __( 'Admin Page Framework', 'admin-page-framework-loader' ),
)
);
$this->addSettingFields(
$_sSectionID,
array(
'field_id' => 'text_field',
'type' => 'text',
'title' => __( 'Text', 'admin-page-framework-loader' ),
'repeatable' => true,
'sortable' => true,
'description' => 'Type something here.',
'description' => __( 'Type something here.', 'admin-page-framework-loader' ),
),
array(
'field_id' => 'text_area',
'type' => 'textarea',
'title' => __( 'Text Area', 'admin-page-framework-loader' ),
'default' => 'Hi there!',
'default' => __( 'Hi there!', 'admin-page-framework-loader' ),
),
array(
'field_id' => 'image',
@@ -59,7 +69,7 @@ public function setUp() {
'default' => 'b',
)
);
}
/**
@@ -72,4 +82,4 @@ public function validate( $aInputs, $aOldInputs, $oFactory ) {
}
new APF_Demo_MyUserMeta;
new APF_Demo_MyUserMeta;
@@ -3,7 +3,7 @@ Contributors: Michael Uno, miunosoft, pcraig3
Donate link: http://michaeluno.jp/en/donate
Tags: admin, admin page, administration, developers, options, settings, API, framework, library, meta box, custom post type, custom post types, utility, fields, custom field, custom fields, tool, tools, widget, widgets, form, forms, plugin, plugins, plugin framework, theme, theme framework
Requires at least: 3.4
Tested up to: 4.9.6
Tested up to: 4.9.7
Stable tag: 3.8.16
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -506,6 +506,7 @@ See examples, https://gist.github.com/michaeluno/c30713fcfe0d9d45d89f, https://g
== Changelog ==

= 3.8.17 =
- Fixed a bug that the `save` argument did not take effect for User Meta and Page Meta Box factories.
- Fixed a compatibility issue with some third-parties which attempt to instantiate the framework widget class without given any parameters.

= 3.8.16 - 2018/07/06 =

0 comments on commit 86468e5

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.