refactor: move Plugin settings under general settings menu

This commit is contained in:
Ben Goldsworthy 2021-04-16 03:10:03 +01:00
parent 6b42f12716
commit f010c175ed
2 changed files with 18 additions and 76 deletions

View file

@ -20,7 +20,7 @@ class MCI_Footnotes_Layout_Init {
* @since 1.5.0 * @since 1.5.0
* @var string * @var string
*/ */
const C_STR_MAIN_MENU_SLUG = 'mfmmf'; const C_STR_MAIN_MENU_SLUG = 'footnotes';
/** /**
* Plugin main menu name. * Plugin main menu name.
@ -31,12 +31,12 @@ class MCI_Footnotes_Layout_Init {
const C_STR_MAIN_MENU_TITLE = 'ManFisher'; const C_STR_MAIN_MENU_TITLE = 'ManFisher';
/** /**
* Contains layout engine sub classes. * Contains the settings layoutEngine
* *
* @since 1.5.0 * @since 1.5.0
* @var array * @var array
*/ */
private $a_arr_sub_page_classes = array(); private $settings_page;
/** /**
* Class Constructor. Initializes all WordPress hooks for the Plugin Settings. * Class Constructor. Initializes all WordPress hooks for the Plugin Settings.
@ -44,100 +44,42 @@ class MCI_Footnotes_Layout_Init {
* @since 1.5.0 * @since 1.5.0
*/ */
public function __construct() { public function __construct() {
foreach ( get_declared_classes() as $l_str_class_name ) { $this->settings_page = new MCI_Footnotes_Layout_Settings();
if ( is_subclass_of( $l_str_class_name, 'MCI_Footnotes_Layout_Engine' ) ) {
$l_obj_class = new $l_str_class_name();
// Append new instance of the layout engine sub class.
$this->a_arr_sub_page_classes[ $l_obj_class->get_priority() ] = $l_obj_class;
}
}
ksort( $this->a_arr_sub_page_classes );
// Register hooks/actions. // Register hooks/actions.
add_action( 'admin_menu', array( $this, 'register_options_submenu' ) );
add_action( 'admin_init', array( $this, 'initialize_settings' ) ); add_action( 'admin_init', array( $this, 'initialize_settings' ) );
add_action( 'admin_menu', array( $this, 'register_main_menu' ) );
// Register AJAX callbacks for Plugin information. // Register AJAX callbacks for Plugin information.
add_action( 'wp_ajax_nopriv_footnotes_get_plugin_info', array( $this, 'get_plugin_meta_information' ) ); add_action( 'wp_ajax_nopriv_footnotes_get_plugin_info', array( $this, 'get_plugin_meta_information' ) );
add_action( 'wp_ajax_footnotes_get_plugin_info', array( $this, 'get_plugin_meta_information' ) ); add_action( 'wp_ajax_footnotes_get_plugin_info', array( $this, 'get_plugin_meta_information' ) );
} }
/** /**
* Initializes all sub pages and registers the settings. * Registers the settings and initialises the settings page.
* *
* @since 1.5.0 * @since 1.5.0
*/ */
public function initialize_settings() { public function initialize_settings() {
MCI_Footnotes_Settings::instance()->register_settings(); MCI_Footnotes_Settings::instance()->register_settings();
// Iterate though each sub class of the layout engine and register their sections. $this->settings_page->register_sections();
foreach ( $this->a_arr_sub_page_classes as $l_obj_layout_engine_sub_class ) {
$l_obj_layout_engine_sub_class->register_sections();
}
} }
/** /**
* Registers the new main menu for the WordPress dashboard. * Registers the footnotes submenu page.
* Registers all sub menu pages for the new main menu.
* *
* @since 1.5.0 * @since 1.5.0
* @see http://codex.wordpress.org/Function_Reference/add_menu_page * @see http://codex.wordpress.org/Function_Reference/add_menu_page
*/ */
public function register_main_menu() { public function register_options_submenu() {
global $menu; add_submenu_page(
// Iterate through each main menu. 'options-general.php',
foreach ( $menu as $l_arr_main_menu ) { 'footnotes Settings',
// 3terate through each main menu attribute. self::C_STR_MAIN_MENU_SLUG,
foreach ( $l_arr_main_menu as $l_str_attribute ) { 'manage_options',
// Main menu already added, append sub pages and stop. 'footnotes',
if ( self::C_STR_MAIN_MENU_SLUG === $l_str_attribute ) { array( $this->settings_page, 'display_content' )
$this->register_sub_pages();
return;
}
}
}
// Add a new main menu page to the WordPress dashboard.
add_menu_page(
self::C_STR_MAIN_MENU_TITLE, // Page title.
self::C_STR_MAIN_MENU_TITLE, // Menu title.
'manage_options', // Capability.
self::C_STR_MAIN_MENU_SLUG, // Menu slug.
array( $this, 'display_other_plugins' ), // Function.
plugins_url( 'footnotes/img/main-menu.png' ), // Icon URL.
null // Position.
); );
$this->register_sub_pages(); $this->settings_page->register_sub_page();
}
/**
* Registers all SubPages for this Plugin.
*
* @since 1.5.0
*/
private function register_sub_pages() {
// First registered sub menu page MUST NOT contain a unique slug suffix.
// Iterate though each sub class of the layout engine and register their sub page.
foreach ( $this->a_arr_sub_page_classes as $l_obj_layout_engine_sub_class ) {
$l_obj_layout_engine_sub_class->register_sub_page();
}
}
/**
* Displays other Plugins from the developers.
*
* @since 1.5.0
*/
public function display_other_plugins() {
printf( '<br/><br/>' );
// Load template file.
$l_obj_template = new MCI_Footnotes_Template( MCI_Footnotes_Template::C_STR_DASHBOARD, 'manfisher' );
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
echo $l_obj_template->get_content();
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
printf( '<em>visit <a href="https://cheret.org/footnotes/" target="_blank">Mark Cheret</a></em>' );
printf( '<br/><br/>' );
printf( '</div>' );
} }
// phpcs:disable WordPress.Security.NonceVerification.Missing // phpcs:disable WordPress.Security.NonceVerification.Missing

View file

@ -257,7 +257,7 @@ abstract class MCI_Footnotes_Layout_Engine {
echo sprintf( echo sprintf(
'<a class="nav-tab%s" href="?page=%s&t=%s">%s</a>', '<a class="nav-tab%s" href="?page=%s&t=%s">%s</a>',
( $l_str_id === $l_arr_active_section['id'] ) ? ' nav-tab-active' : '', ( $l_str_id === $l_arr_active_section['id'] ) ? ' nav-tab-active' : '',
MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG . $this->get_sub_page_slug(), MCI_Footnotes_Layout_Init::C_STR_MAIN_MENU_SLUG,
$l_str_id, $l_str_id,
$l_arr_description['title'] $l_arr_description['title']
); );