Skip to content

Commit

Permalink
Changed the behaviour of not loading form components in `admin-ajax.p…
Browse files Browse the repository at this point in the history
…hp` so that custom field types can access `admin-ajax.php` using Ajax.
  • Loading branch information
michaeluno committed Jan 12, 2017
1 parent 41069bc commit ec46e48
Show file tree
Hide file tree
Showing 20 changed files with 257 additions and 90 deletions.
2 changes: 1 addition & 1 deletion development/admin-page-framework-include-class-list.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
Admin Page Framework - Inclusion Class Files v3.8.13b04 by Michael Uno
Admin Page Framework - Inclusion Class Files v3.8.14b01 by Michael Uno
Generated by PHP Class Files Inclusion List Creator
<http://en.michaeluno.jp/admin-page-framework>
Copyright (c) 2013-2017, Michael Uno; Licensed under MIT <http://opensource.org/licenses/MIT> */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@
abstract class AdminPageFramework_Factory_Controller extends AdminPageFramework_Factory_View {

/**
* Should be extended
* Should be extended.
*
* @internal
*/
public function start() {}
public function setUp() {}


/**
* Shouold be extended.
*
* @since 3.8.14
* @return void
*/
public function load() {}


/**
* Allows the user to check if the current page belongs to the admin pages of the factory component.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ public function __construct( $oProp ) {
// Required sub-class objects
$this->oProp = $oProp;

if ( $this->oProp->bIsAdmin && ! $this->oProp->bIsAdminAjax ) {
// @deprecated 3.8.14
// if ( $this->oProp->bIsAdmin && ! $this->oProp->bIsAdminAjax ) {
if ( $this->oProp->bIsAdmin ) {
$this->oUtil->registerAction( 'current_screen', array( $this, '_replyToLoadComponents' ) );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ abstract class AdminPageFramework_Property_Base extends AdminPageFramework_Frame
public $_bSetupLoaded;

/**
* Indicates whether the current page is in admin-ajax.php
* Indicates whether the current page is in `admin-ajax.php`
*
* @since 3.1.3
* @internal
Expand Down Expand Up @@ -377,7 +377,15 @@ abstract class AdminPageFramework_Property_Base extends AdminPageFramework_Frame
* @since 3.8.5
*/
public $bShowDebugInfo = true;


/**
* Stores URL query key-value pairs.
* For admin-ajax.php, the referrer's will be stored. For other cases, the `$_GET` variable values will be used.
* @since 3.8.14
* @var array
*/
public $aQuery = array();

/**
* Sets up necessary property values.
*
Expand Down Expand Up @@ -410,6 +418,8 @@ private function ___setProperties( $oCaller, $sCallerPath, $sClassName, $sCapabi
$this->bIsAdminAjax = 'admin-ajax.php' === $this->sPageNow;
$this->bShowDebugInfo = $this->isDebugMode();

$this->aQuery = $this->___getURLQuery();

// Overloading property items - these will be set on demand
unset(
$this->aScriptInfo,
Expand All @@ -419,6 +429,23 @@ private function ___setProperties( $oCaller, $sCallerPath, $sClassName, $sCapabi
);

}
/**
*
* @since 3.8.14
* @return array
* @internal
*/
private function ___getURLQuery() {

if ( ! $this->bIsAdminAjax ) {
return $_GET;
}

$_aParts = parse_url( $_SERVER[ 'HTTP_REFERER' ] );
parse_str( $_aParts[ 'query' ], $_aQuery );
return $_GET + $_aQuery;

}

/**
* Sets up global variables.
Expand Down Expand Up @@ -450,7 +477,9 @@ public function getFormArguments() {
return array(
'caller_id' => $this->sClassName,
'structure_type' => $this->_sPropertyType, // @todo change this to admin_page
'action_hook_form_registration' => $this->_sFormRegistrationHook,
'action_hook_form_registration' => $this->bIsAdminAjax
? 'admin_init' // 3.8.14
: $this->_sFormRegistrationHook,
) + $this->aFormArguments;
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function _replyToRegisterFormItems() {
if ( ! $this->isInThePage() ) {
return;
}

// Load field type definitions.
$this->_setFieldTypeDefinitions( 'admin_page_framework' ); // site-wide
$this->_setFieldTypeDefinitions( $this->aArguments[ 'caller_id' ] ); // per class
Expand Down
12 changes: 8 additions & 4 deletions development/factory/admin_page/AdminPageFramework_Model_Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ abstract class AdminPageFramework_Model_Form extends AdminPageFramework_Router {
public function __construct( $sOptionKey=null, $sCallerPath=null, $sCapability='manage_options', $sTextDomain='admin-page-framework' ) {

parent::__construct( $sOptionKey, $sCallerPath, $sCapability, $sTextDomain );

if ( $this->oProp->bIsAdminAjax ) {
return;
}

// @deprecated 3.8.14
// if ( $this->oProp->bIsAdminAjax ) {
// return;
// }
if ( ! $this->oProp->bIsAdmin ) {
return;
}
Expand Down Expand Up @@ -410,6 +411,9 @@ private function _getSectionTabSlug( $aSectionset ) {
* @return boolean Whether or not the form registration should be allowed in the current screen.
*/
public function _replyToDetermineWhetherToProcessFormRegistration( $bAllowed ) {
if ( $this->oProp->bIsAdminAjax ) {
return true;
}
$_sPageSlug = $this->oProp->getCurrentPageSlug();
return $this->oProp->isPageAdded( $_sPageSlug );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ abstract class AdminPageFramework_Model_Menu extends AdminPageFramework_Controll
public function __construct( $sOptionKey=null, $sCallerPath=null, $sCapability='manage_options', $sTextDomain='admin-page-framework' ) {

parent::__construct( $sOptionKey, $sCallerPath, $sCapability, $sTextDomain );

if ( $this->oProp->bIsAdminAjax ) {
return;
}

// @deprecated 3.8.14
// if ( $this->oProp->bIsAdminAjax ) {
// return;
// }

new AdminPageFramework_Model_Menu__RegisterMenu( $this );

Expand Down
79 changes: 52 additions & 27 deletions development/factory/admin_page/AdminPageFramework_Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,36 @@ public function __construct( $sOptionKey=null, $sCallerPath=null, $sCapability='

parent::__construct( $this->oProp );

if ( $this->oProp->bIsAdminAjax ) {
return;
}
// @deprecated 3.8.14
// if ( $this->oProp->bIsAdminAjax ) {
// return;
// }
if ( ! $this->oProp->bIsAdmin ) {
return;
}

add_action( 'wp_loaded', array( $this, '_replyToDetermineToLoad' ) );

}

add_action( 'wp_loaded', array( $this, '_replyToDetermineToLoad' ) );
add_action( 'set_up_' . $this->oProp->sClassName, array( $this, '_replyToLoadComponentsForAjax' ), 100 );

}

/**
* Loads page components for ajax calls.
*
* @since 3.8.14
* @remark It is assumed that the `setUp()` method is already called.
* @callback add_action wp_loaded
*/
public function _replyToLoadComponentsForAjax() {
if ( ! $this->oProp->bIsAdminAjax ) {
return;
}
new AdminPageFramework_Model_Menu__RegisterMenu( $this, 'pseudo_admin_menu' );
do_action( 'pseudo_admin_menu', '' );
do_action( 'pseudo_current_screen' );

}

/**
* Instantiates a link object based on the type.
*
Expand Down Expand Up @@ -154,11 +173,11 @@ private function _getCallbackName( $sMethodName, $sPageSlug, array $aKnownMethod
* @internal
*/
protected function _doPageLoadCall( $sMethodName, $sPageSlug, $sTabSlug, $oScreen ) {
if ( ! $this->_isPageLoadCall( $sMethodName, $sPageSlug, $oScreen->id ) ) {

if ( ! $this->_isPageLoadCall( $sMethodName, $sPageSlug, $oScreen ) ) {
return;
}

// [3.4.6+] Set the page and tab slugs to the default form section so that added form fields without a section will appear in different pages and tabs.
$this->_setPageAndTabSlugsForForm( $sPageSlug, $sTabSlug );

Expand All @@ -174,13 +193,13 @@ protected function _doPageLoadCall( $sMethodName, $sPageSlug, $sTabSlug, $oScree
),
$this // the admin page object - this lets third-party scripts use the framework methods.
);

// * Note that the in-page tabs handling method `_replyToFinalizeInPageTabs()` is called in the above action hook.

// Re-retrieve the current tab slug as in-page tabs may be added during the above `load_{...}` hooks.
// Note that the if the tab is the first item, and the user arrives the page by clicking on the sidebar menu, the tab slug will be empty unless an in-page tab is added.
// Note that if the tab is the first item and the user arrives the page by clicking on the sidebar menu,
// the tab slug will be empty unless an in-page tab is added.
$sTabSlug = $this->oProp->getCurrentTabSlug( $sPageSlug );

if ( strlen( $sTabSlug ) ) {
$this->_setShowDebugInfoProperty( $sPageSlug, $sTabSlug ); // 3.8.8+
$this->oUtil->addAndDoActions(
Expand Down Expand Up @@ -248,23 +267,23 @@ private function _setPageAndTabSlugsForForm( $sPageSlug, $sTabSlug ) {
* Determines whether the function call is of a page load.
* @since 3.5.3
* @internal
* @return boolean True if it is a page load call; othwrwise, false.
* @param string $sMethodName The undefined method name that is passed to the __call() overload method.
* @param string $sPageSlug The currently loading page slug.
* @param string $sScreenID The screen ID that the WordPress screen object gives.
* @return boolean True if it is a page load call; othwrwise, false.
* @param string $sMethodName The undefined method name that is passed to the __call() overload method.
* @param string $sPageSlug The currently loading page slug.
* @param object|string $osScreenORPageHook The screen ID that the WordPress screen object gives.
*/
private function _isPageLoadCall( $sMethodName, $sPageSlug, $sScreenID ) {
private function _isPageLoadCall( $sMethodName, $sPageSlug, $osScreenORPageHook ) {

if ( substr( $sMethodName, strlen( 'load_pre_' ) ) !== $sPageSlug ) {
return false;
}
if ( ! isset( $this->oProp->aPageHooks[ $sPageSlug ] ) ) {
return false;
}
if ( $sScreenID !== $this->oProp->aPageHooks[ $sPageSlug ] ) {
return false;
}
return true;
$_sPageHook = is_object( $osScreenORPageHook )
? $osScreenORPageHook->id
: $sPageSlug; // for ajax calls
return $_sPageHook === $this->oProp->aPageHooks[ $sPageSlug ];

}

Expand All @@ -278,11 +297,12 @@ private function _isPageLoadCall( $sMethodName, $sPageSlug, $sScreenID ) {
* @internal
*/
protected function _isInstantiatable() {


// @deprecated 3.8.14
// Disable in admin-ajax.php
if ( isset( $GLOBALS[ 'pagenow' ] ) && 'admin-ajax.php' === $GLOBALS[ 'pagenow' ] ) {
return false;
}
// if ( isset( $GLOBALS[ 'pagenow' ] ) && 'admin-ajax.php' === $GLOBALS[ 'pagenow' ] ) {
// return false;
// }

// Nothing to do in the network admin area.
return ! is_network_admin();
Expand All @@ -299,7 +319,12 @@ protected function _isInstantiatable() {
* @internal
*/
protected function _isInThePage() {


// 3.8.14+
if ( $this->oProp->bIsAdminAjax ) {
return true;
}

// If the setUp method is not loaded yet,
if ( ! did_action( 'set_up_' . $this->oProp->sClassName ) ) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,11 +493,11 @@ public function isPageAdded( $sPageSlug='' ) {
* @remark Do not return `null` when not found as some framework methods check the retuened value with `isset()` and if null is given, `isset()` yields `false` while it does `true` for an emtpy string ('').
*/
public function getCurrentPageSlug() {
return $this->getElement(
$_GET, // subject array
'page', // key
'' // default
);
return $this->getElement(
$this->aQuery,
'page',
''
);
}

/**
Expand All @@ -513,9 +513,9 @@ public function getCurrentPageSlug() {
* @remark Do not return `null` when not found as some framework methods check the returned value with `isset()` and if null is given, `isset()` yields `false` while it does `true` for an empty string ('').
*/
public function getCurrentTabSlug( $sCurrentPageSlug='' ) {

// It is possible that the tab slug is not set if it is the default tab.
$_sTabSlug = $this->getElement( $_GET, 'tab' );
$_sTabSlug = $this->getElement( $this->aQuery, 'tab', '' );
if ( $_sTabSlug ) {
return $_sTabSlug;
}
Expand Down Expand Up @@ -544,10 +544,10 @@ public function getCurrentTab( $sCurrentPageSlug='' ) {
* @internal
* @remark Used in the __call() method in the main class.
* @return string The default in-page tab slug if found; otherwise, an empty string.
*/
*/
public function getDefaultInPageTab( $sPageSlug ) {
if ( ! $sPageSlug ) {

if ( ! $sPageSlug ) {
return '';
}
return $this->getElement(
Expand Down
Loading

0 comments on commit ec46e48

Please sign in to comment.