| 1 |
<?php |
| 2 |
namespace BTNB\Admin; |
| 3 |
|
| 4 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 5 |
|
| 6 |
/** |
| 7 |
* AdminMenu class |
| 8 |
* Registers the settings field to hide the CPT menu and adds the dashboard submenu page. |
| 9 |
* |
| 10 |
* @package BTN\Admin |
| 11 |
*/ |
| 12 |
class AdminMenu { |
| 13 |
public $post_type = 'button-block'; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor. |
| 17 |
* Registers hooks for admin settings, scripts, and menu. |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'admin_init', array($this, 'adminInit') ); |
| 21 |
add_action( 'admin_enqueue_scripts', [$this, 'adminEnqueueScripts'] ); |
| 22 |
add_action( 'admin_menu', [ $this, 'adminMenu' ] ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Registers the 'button_block_option' setting and adds a toggle field to Settings > General. |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
function adminInit(){ |
| 31 |
register_setting( 'general', 'button_block_option', 'sanitize_text_field' ); |
| 32 |
|
| 33 |
add_settings_field( |
| 34 |
'button_block_option_field', |
| 35 |
'Hide Button Block from admin Menu', |
| 36 |
[ $this , 'optionCallback' ], |
| 37 |
'general' |
| 38 |
); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Renders the checkbox toggle for the 'Hide Button Block from admin menu' setting. |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
function optionCallback() { |
| 47 |
$value = get_option( 'button_block_option', '' ); ?> |
| 48 |
|
| 49 |
<label class='btnAdminHideSwitch'> |
| 50 |
<input type='checkbox' id='button_block_option' name='button_block_option' value='true' <?php checked( $value, 'true' ); ?>> |
| 51 |
<span class='slider round'></span> |
| 52 |
</label> |
| 53 |
<p class='description'>Turn this setting on or off.</p> |
| 54 |
<?php } |
| 55 |
|
| 56 |
/** |
| 57 |
* Enqueues admin styles for the General Settings page. |
| 58 |
* |
| 59 |
* @param string $hook The current admin page hook suffix. |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
function adminEnqueueScripts( $hook ){ |
| 63 |
if( 'options-general.php' === $hook ){ |
| 64 |
wp_enqueue_style( 'btn-admin-general', BTNB_DIR_URL . 'build/admin/general.css', [], BTNB_VERSION ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Adds the 'Help & Demos' submenu page under the button-block CPT menu. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
function adminMenu() { |
| 74 |
add_submenu_page( |
| 75 |
'edit.php?post_type=' . $this->post_type, |
| 76 |
__( 'Help & Demos - Button Block', 'button-block' ), |
| 77 |
__( 'Help & Demos', 'button-block' ), |
| 78 |
'manage_options', |
| 79 |
'button-block', |
| 80 |
[ \BTNBPlugin::class, 'renderDashboard' ] |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
new AdminMenu(); |