141 lines
3.8 KiB
PHP
141 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace PluginNamespace\Admin;
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
class AdminSettings {
|
|
|
|
/**
|
|
* Class constructor.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct() {
|
|
add_action( 'admin_menu', array( $this, 'add_admin_menu' ), 11 );
|
|
add_action( 'admin_menu', array( $this, 'add_submenu_items' ), 12 );
|
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
|
|
}
|
|
|
|
/**
|
|
* Setting up the WordPress admin menu for this plugin. Also setting up
|
|
* a submenu item that will take the place of the main settings page.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function add_admin_menu(): void {
|
|
$page_title = __( 'Plugin Name', 'nmsp_plugin_name_settings_title' );
|
|
$menu_title = __( 'Plugin Name', 'nmsp_plugin_name_settings_title' );
|
|
$capability = 'manage_options';
|
|
$menu_slug = 'nmsp-plugin-name-menu-slug';
|
|
$callback = '';
|
|
$icon = 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( NMSP_PLUGIN_BASE_DIR . '/assets/images/menu-icon.svg' ) );
|
|
$position = 100; // after the separator below "Settings"
|
|
|
|
$subpage_title = __( 'Namespace Plugin Name - Settings', 'nmsp_plugin_name_settings_title' );
|
|
$submenu_title = __( 'Settings', 'nmsp_plugin_name_settings_title' );
|
|
$subcallback = array( $this, 'show_main_settings_page' );
|
|
|
|
add_menu_page( $page_title, $menu_title, $capability, $menu_slug, '', $icon, $position );
|
|
add_submenu_page( $menu_slug, $subpage_title, $submenu_title, $capability, $menu_slug, $subcallback );
|
|
|
|
}
|
|
|
|
/**
|
|
* Append to the admin menu entry for this plugin.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function add_submenu_items(): void {
|
|
$parent_menu_slug = 'nmsp-plugin-name-menu-slug';
|
|
$page_title = __( 'Namespace Plugin Name - Other', 'nmsp_plugin_name_settings_title' );
|
|
$menu_title = __( 'Other', 'nmsp_plugin_name_settings_title' );
|
|
$menu_slug = 'nmsp-plugin-name-menu-other-slug';
|
|
$capability = 'manage_options';
|
|
$callback = array( $this, 'show_other_page' );
|
|
|
|
add_submenu_page( $parent_menu_slug, $page_title, $menu_title, $capability, $menu_slug, $callback );
|
|
}
|
|
|
|
/**
|
|
* Show the settings page.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function show_main_settings_page(): void {
|
|
$data = array();
|
|
$this->localize_script();
|
|
|
|
ob_start();
|
|
echo nmsp_plugin_app()->blade->run( 'admin.settings', $data );
|
|
$output = ob_get_clean();
|
|
echo wp_kses_post( $output );
|
|
}
|
|
|
|
/**
|
|
* Show the "other" page.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function show_other_page(): void {
|
|
$data = array();
|
|
$this->localize_script();
|
|
|
|
ob_start();
|
|
echo nmsp_plugin_app()->blade->run( 'admin.other', $data );
|
|
$output = ob_get_clean();
|
|
echo wp_kses_post( $output );
|
|
}
|
|
|
|
/**
|
|
* Register and enqueue stylesheets and javascript files.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register_scripts(): void {
|
|
wp_enqueue_style( 'nmsp-plugin-name-admin-css', plugins_url( '../assets/css/nmsp-plugin-name-admin.css', __FILE__ ), array(), '1.0.0', 'screen' );
|
|
|
|
wp_register_script( 'nmsp-plugin-name-admin-js', plugins_url( '../assets/js/nmsp-plugin-name-admin.js', __FILE__ ), array( 'jquery' ), '1.0.0', false );
|
|
}
|
|
|
|
/**
|
|
* Description
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @param array $additional_data (optional) Additional key-value paired data to pass to JavaScript.
|
|
*
|
|
* @return void
|
|
*/
|
|
private function localize_script( array $additional_data = array() ): void {
|
|
wp_enqueue_script( 'nmsp-plugin-name-admin-js' );
|
|
|
|
$payload = array_merge(
|
|
$additional_data,
|
|
array(
|
|
'wp_ajax_url' => admin_url( 'admin-ajax.php' ),
|
|
'_nonce' => wp_create_nonce( NMSP_PLUGIN_NONCE ),
|
|
)
|
|
);
|
|
wp_localize_script( 'nmsp-plugin-name-admin-js', 'nmspPluginAdminApi', $payload );
|
|
}
|
|
|
|
}
|
|
|
|
new AdminSettings();
|