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/9cc296a0b70114f26586 to your computer and use it in GitHub Desktop.
Save michaeluno/9cc296a0b70114f26586 to your computer and use it in GitHub Desktop.
Tests tabs in post listing pages with Admin Page Framework.
<?php
/*
Plugin Name: Admin Page Framework Test - Tabs in Post Listing Page.
Plugin URI: http://en.michaeluno.jp/admin-page-framework
Description: Tests tabs in post listing pages.
Author: Michael Uno
Author URI: http://michaeluno.jp
Version: 1.0.0
*/
// Include the library file. Set your file path here.
$_sDevPath = dirname( dirname( __FILE__ ) ) . '/admin-page-framework/development/admin-page-framework.php';
$_sLibraryPath = defined( 'WP_DEBUG' ) && WP_DEBUG && file_exists( $_sDevPath )
? $_sDevPath
: dirname( dirname( __FILE__ ) ) . '/admin-page-framework/library/admin-page-framework/admin-page-framework.php';
if ( ! class_exists( 'AdminPageFramework' ) && ! include( $_sLibraryPath ) ) {
return;
}
class APF_Test_TabsInPostListingPages extends AdminPageFramework {
public function setUp() {
$this->setRootMenuPage(
'Post Listing Pages' // menu name
);
$this->addSubMenuItems(
array(
'title' => 'Settings', // page title
'page_slug' => 'test_tab_post_types', // page slug
)
);
add_action( 'admin_menu', array( $this, 'modifyMenu' ) );
}
/**
* @callback action load_{page slug}
*/
public function load_test_tab_post_types( $oFactory ) {
add_action( 'all_admin_notices', 'printAPFTestTabsInPostListingPages' );
}
/**
* @callback filter content_{page slug}
*/
public function content_test_tab_post_types( $sContent ) {
return "<h3>Post Type Tab Test</h3>"
. "<p>This is a test.</p>"
. $sContent;
}
/**
*
* @callback action admin_menu
*/
public function modifyMenu() {
// Remove the default post type menu item.
$_sPageSlug = $this->oProp->aRootMenu[ 'sPageSlug' ];
if ( ! isset( $GLOBALS['submenu'][ $_sPageSlug ] ) ) {
// logged-in users of an insufficient access level don't have the menu to be registered.
return;
}
foreach ( $GLOBALS['submenu'][ $_sPageSlug ] as $_iIndex => $_aSubMenu ) {
if ( ! isset( $_aSubMenu[ 2 ] ) ) {
continue;
}
if ( ! in_array( $_aSubMenu[ 0 ], array( 'Tab A', 'Tab B', 'Tab C' ) ) ) {
continue;
}
// Remove post type menu items
$_sCurrentPostType = isset( $_GET[ 'post_type' ] )
? $_GET[ 'post_type' ]
: '';
if ( isset( $_GET[ 'page' ] ) || ! $_sCurrentPostType ) {
unset( $GLOBALS['submenu'][ $_sPageSlug ][ $_iIndex ] );
continue;
}
if ( false === strpos( $_aSubMenu[ 2 ], 'post_type=' . $_sCurrentPostType ) ) {
unset( $GLOBALS['submenu'][ $_sPageSlug ][ $_iIndex ] );
}
}
}
}
new APF_Test_TabsInPostListingPages;
class APF_Test_TabsInPostListingPages_PostTypeA extends AdminPageFramework_PostType {
public function setUp() {
$this->setArguments(
array(
'labels' => array(
'name' => 'Tab A',
'all_items' => 'Tab A',
'singular_name' => 'Tab A',
'plugin_listing_table_title_cell_link' => '', // framework specific key. [3.0.6+]
),
'show_ui' => true,
'show_in_menu' => 'APF_Test_TabsInPostListingPages',
)
);
if ( $this->_isInThePage() ) {
add_action( 'all_admin_notices', 'printAPFTestTabsInPostListingPages' );
}
}
}
new APF_Test_TabsInPostListingPages_PostTypeA(
'test_tab_a', // the post type slug
array(), // the argument array - empty because it is defined inside the class.
__FILE__ // the caller script path.
);
class APF_Test_TabsInPostListingPages_PostTypeB extends AdminPageFramework_PostType {
public function setUp() {
$this->setArguments(
array(
'labels' => array(
'name' => 'Tab B',
'all_items' => 'Tab B',
'singular_name' => 'Tab B',
'plugin_listing_table_title_cell_link' => '', // framework specific key. [3.0.6+]
),
'show_ui' => true,
'show_in_menu' => 'APF_Test_TabsInPostListingPages',
)
);
if ( $this->_isInThePage() ) {
add_action( 'all_admin_notices', 'printAPFTestTabsInPostListingPages' );
}
}
}
new APF_Test_TabsInPostListingPages_PostTypeB(
'test_tab_b', // the post type slug
array(), // the argument array - empty because it is defined inside the class.
__FILE__ // the caller script path.
);
class APF_Test_TabsInPostListingPages_PostTypeC extends AdminPageFramework_PostType {
public function setUp() {
$this->setArguments(
array(
'labels' => array(
'name' => 'Tab C',
'all_items' => 'Tab C',
'singular_name' => 'Tab C',
'plugin_listing_table_title_cell_link' => '', // framework specific key. [3.0.6+]
),
'show_ui' => true,
'show_in_menu' => 'APF_Test_TabsInPostListingPages',
)
);
if ( $this->_isInThePage() ) {
add_action( 'all_admin_notices', 'printAPFTestTabsInPostListingPages' );
}
}
}
new APF_Test_TabsInPostListingPages_PostTypeC(
'test_tab_c', // the post type slug
array(), // the argument array - empty because it is defined inside the class.
__FILE__ // the caller script path.
);
function printAPFTestTabsInPostListingPages() {
$_aTabs = array(
array(
'slug' => 'test_tab_post_types',
'title' => 'Settings',
'href' => add_query_arg(
array(
'page' => 'test_tab_post_types',
),
admin_url( 'admin.php' )
),
),
array(
'slug' => 'test_tab_a',
'title' => 'Tab A',
'href' => add_query_arg(
array(
'post_type' => 'test_tab_a',
),
admin_url( 'edit.php' )
),
),
array(
'slug' => 'test_tab_b',
'title' => 'Tab B',
'href' => add_query_arg(
array(
'post_type' => 'test_tab_b',
),
admin_url( 'edit.php' )
),
),
array(
'slug' => 'test_tab_c',
'title' => 'Tab C',
'href' => add_query_arg(
array(
'post_type' => 'test_tab_c',
),
admin_url( 'edit.php' )
),
),
);
$_oTabs = new AdminPageFramework_TabNavigationBar(
$_aTabs, // tabs
isset( $_GET[ 'post_type' ] )
? $_GET[ 'post_type' ]
: $_GET[ 'page' ], // active tab slug
'h2' // used tag
);
echo $_oTabs->get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment