Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeluno/8849b9c84768c817ce22 to your computer and use it in GitHub Desktop.
Save michaeluno/8849b9c84768c817ce22 to your computer and use it in GitHub Desktop.
Demonstrates a simple Wizard form with Admin Page Framework.
<?php
/*
Plugin Name: Admin Page Framework Example - Wizard Type Form
Plugin URI: http://en.michaeluno.jp/admin-page-framework
Description: Demonstrates a simple Wizard form.
Author: Michael Uno
Author URI: http://michaeluno.jp
Version: 1.0.0
*/
// Include the library file. Set your file path here.
$_sLibraryPath = defined( 'WP_DEBUG' ) && WP_DEBUG
? dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php'
: dirname( dirname( __FILE__ ) ) . '/admin-page-framework/library/admin-page-framework/admin-page-framework.php';
if ( ! class_exists( 'AdminPageFramework' ) && ! include( $_sLibraryPath ) ) {
return;
}
class APF_Example_WizardTypeForm_MainPage extends AdminPageFramework {
public function setUp() {
$this->setRootMenuPage(
__( 'Wizard Example', 'admin-page-framework-example' ) // menu name
);
$this->addSubMenuItems(
array(
'title' => __( 'Saved Data', 'admin-page-framework-example' ), // page title
'page_slug' => 'apf_example_wizard_type_form_saved_data',
)
);
}
public function content( $sHTML ) {
return $sHTML
. $this->oDebug->get( get_option( 'APF_Example_WizardTypeForm_MainPage', array() ) );
}
}
new APF_Example_WizardTypeForm_MainPage;
class APF_Example_WizardTypeForm extends AdminPageFramework {
public function setUp() {
$this->setRootMenuPageBySlug(
'APF_Example_WizardTypeForm_MainPage' // the class name instantiated above.
);
$this->addSubMenuItems(
array(
'title' => __( 'Wizard', 'admin-page-framework-example' ), // page title
'page_slug' => 'wizard_example',
)
);
$this->addInPageTabs(
'wizard_example', // target page slug
array(
'tab_slug' => 'initial_tab',
'title' => __( 'IinitiaL Form', 'admin-page-framework-example' ),
'show_in_page_tab' => false, // hide it
),
array(
'tab_slug' => 'second_a',
'title' => __( 'Second Form A', 'admin-page-framework-example' ),
'show_in_page_tab' => false, // hide it
),
array(
'tab_slug' => 'second_b',
'title' => __( 'Second Form B', 'admin-page-framework-example' ),
'show_in_page_tab' => false, // hide it
)
);
}
/**
*
* @callback action load_{page slug}_{tab slug}
*/
public function load_wizard_example_initial_tab( $oAdminPage ) {
$this->addSettingSections(
'wizard_example', // target page slug
array(
'section_id' => 'initial',
'tab_slug' => 'initial_tab',
'title' => 'Start',
)
);
$this->addSettingFields(
'initial', // target section id
array (
'field_id' => 'select',
'type' => 'select',
'title' => __( 'Select', 'admin-page-framework-example' ),
'label' => array(
'none' => __( 'Select', 'admin-page-framework-example' ),
'a' => 'A',
'b' => 'B',
),
'default' => 'none',
),
array(
'field_id' => 'submit',
'type' => 'submit',
)
);
}
/**
*
* @callback action load_{page slug}_{tab slug}
*/
public function load_wizard_example_second_a( $oAdminPage ) {
$this->addSettingSections(
'wizard_example', // target page slug
array(
'section_id' => 'second_a',
'tab_slug' => 'second_a',
'title' => 'Form - Second A',
'description' => 'You have selected A',
)
);
$this->addSettingFields(
'second_a', // target section id
array (
'field_id' => 'text',
'type' => 'text',
'title' => __( 'Text', 'admin-page-framework-example' ),
),
array(
'field_id' => 'submit',
'type' => 'submit',
)
);
}
/**
*
* @callback action load_{page slug}_{tab slug}
*/
public function load_wizard_example_second_b( $oAdminPage ) {
$this->addSettingSections(
'wizard_example', // target page slug
array(
'section_id' => 'second_b',
'tab_slug' => 'second_b',
'title' => 'Form - Second B',
'description' => 'You have selected B',
)
);
$this->addSettingFields(
'second_b', // target section id
array (
'field_id' => 'textarea',
'type' => 'textarea',
'title' => __( 'Text Area', 'admin-page-framework-example' ),
),
array(
'field_id' => 'submit',
'type' => 'submit',
)
);
}
/**
*
* @callback filter validation_{page slug}_{tab slug}
*/
public function validation_wizard_example_initial_tab( $aInput, $aOldInput, $oAdminPage, $aSubmitInfo ) {
$_sSelect = $aInput[ 'initial' ][ 'select' ];
if ( 'none' === $_sSelect ) {
$this->setSettingNotice( 'Please select an item.' );
}
exit(
wp_safe_redirect(
$this->_getNextPageURL( $_sSelect )
)
);
}
/**
*
* @return string The url to load in the next page, calculated with the given selected option value.
*/
private function _getNextPageURL( $sSelected ) {
return add_query_arg(
in_array( $sSelected, array( 'a', 'b' ) )
? array(
'tab' => "second_" . $sSelected
) + $_GET
: $_GET,
admin_url( $GLOBALS['pagenow'] )
);
}
/**
*
* @callback filter validation_{page slug}
*/
public function validation_wizard_example( $aInput, $aOldInput, $oAdminPage, $aSubmitInfo ) {
update_option( 'APF_Example_WizardTypeForm_MainPage', $aInput );
exit(
wp_safe_redirect(
add_query_arg(
array(
'tab' => '',
'page' => 'apf_example_wizard_type_form_saved_data',
)
+ $_GET,
admin_url( $GLOBALS['pagenow'] )
)
)
);
}
}
new APF_Example_WizardTypeForm(
'' // set empty to disable saving options.
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment