label.
*
* @var array
*/
private $tabs = array();
/**
* Get the single instance.
*
* @return BHTK_Admin
*/
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Hook into WordPress.
*/
private function __construct() {
add_action( 'admin_menu', array( $this, 'register_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
add_action( 'admin_notices', array( $this, 'legacy_notice' ) );
add_action( 'admin_post_bhtk_save_modules', array( $this, 'handle_save_modules' ) );
add_action( 'admin_post_bhtk_save_settings', array( $this, 'handle_save_settings' ) );
add_action( 'admin_post_bhtk_toggle_module', array( $this, 'handle_toggle_module' ) );
add_action( 'admin_post_bhtk_save_module', array( $this, 'handle_save_module' ) );
add_filter( 'plugin_action_links_' . plugin_basename( BHTK_FILE ), array( $this, 'action_links' ) );
}
/**
* Add the toolkit menu and the pages of the enabled modules.
*
* @return void
*/
public function register_menu() {
$cap = BHTK_Plugin::capability();
$icon = include BHTK_PATH . 'includes/menu-icon.php';
add_menu_page( 'Bani Honar Toolkit', 'Bani Honar Toolkit', $cap, self::SLUG, array( $this, 'render_dashboard' ), $icon, 58 );
$this->tabs[ self::SLUG ] = __( 'Dashboard', 'bani-honar-toolkit' );
add_submenu_page( self::SLUG, __( 'Dashboard', 'bani-honar-toolkit' ), __( 'Dashboard', 'bani-honar-toolkit' ), $cap, self::SLUG, array( $this, 'render_dashboard' ) );
$plugin = BHTK_Plugin::instance();
foreach ( $plugin->get_modules() as $id => $module ) {
// A module that is off keeps its tab only when the tab carries its on/off switch.
if ( ! $plugin->is_enabled( $id ) && ! $module->always_show_pages() ) {
continue;
}
foreach ( $module->admin_pages() as $page ) {
if ( empty( $page['slug'] ) || empty( $page['callback'] ) || ! is_callable( $page['callback'] ) ) {
continue;
}
$title = isset( $page['title'] ) ? $page['title'] : $module->get_name();
$this->tabs[ $page['slug'] ] = $title;
add_submenu_page( self::SLUG, $title, $title, $cap, $page['slug'], $page['callback'] );
}
}
$this->tabs[ self::SETTINGS_SLUG ] = __( 'Settings', 'bani-honar-toolkit' );
add_submenu_page( self::SLUG, __( 'Settings', 'bani-honar-toolkit' ), __( 'Settings', 'bani-honar-toolkit' ), $cap, self::SETTINGS_SLUG, array( $this, 'render_settings' ) );
$this->tabs[ BHTK_Contact::SLUG ] = __( 'Contact us', 'bani-honar-toolkit' );
add_submenu_page( self::SLUG, __( 'Contact us', 'bani-honar-toolkit' ), __( 'Contact us', 'bani-honar-toolkit' ), $cap, BHTK_Contact::SLUG, array( 'BHTK_Contact', 'render' ) );
}
/**
* Load the toolkit CSS and JS on its own screens and where a module asks for it.
*
* @param string $hook_suffix Current admin page hook suffix.
* @return void
*/
public function assets( $hook_suffix ) {
$needed = $this->is_toolkit_screen();
if ( ! $needed ) {
foreach ( BHTK_Plugin::instance()->get_enabled_modules() as $module ) {
if ( $module->needs_assets( $hook_suffix ) ) {
$needed = true;
break;
}
}
}
if ( ! $needed ) {
return;
}
wp_enqueue_style( 'bhtk-admin', BHTK_URL . 'assets/css/admin.css', array(), BHTK_VERSION );
wp_enqueue_script( 'bhtk-admin', BHTK_URL . 'assets/js/admin.js', array(), BHTK_VERSION, true );
wp_localize_script( 'bhtk-admin', 'bhtkAdmin', BHTK_I18n::js_strings() );
}
/**
* Whether the current screen is one of ours.
*
* @return bool
*/
private function is_toolkit_screen() {
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return self::SLUG === $page || 0 === strpos( $page, 'bhtk-' );
}
/**
* Quick link on the plugins list.
*
* @param array $links Existing links.
* @return array
*/
public function action_links( $links ) {
$mine = array(
'' . esc_html__( 'Settings', 'bani-honar-toolkit' ) . '',
);
return array_merge( $mine, $links );
}
/**
* Warn when the old BHAC plugin is still active next to this one.
*
* @return void
*/
public function legacy_notice() {
if ( ! $this->is_toolkit_screen() || ! class_exists( 'BHAC_Links' ) ) {
return;
}
echo '
';
esc_html_e( 'The older "BHAC" plugin is still active. Your links and settings were copied into Bani Honar Toolkit, so please deactivate the old plugin to avoid conflicts.', 'bani-honar-toolkit' );
echo '
';
}
/**
* Print the opening of a toolkit screen.
*
* @param string $active Slug of the current tab.
* @return void
*/
public function render_header( $active ) {
?>