| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base |
| 4 |
* |
| 5 |
* @package CTC |
| 6 |
* @since 5.1.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace CTC; |
| 10 |
|
| 11 |
use CTC\Dashboard; |
| 12 |
use CTC\Global_Injector; |
| 13 |
use CTC\Global_Injector\Main_Rule_List; |
| 14 |
use CTC\Analytics; |
| 15 |
|
| 16 |
/** |
| 17 |
* Base |
| 18 |
* |
| 19 |
* @since 5.1.0 |
| 20 |
*/ |
| 21 |
class Base { |
| 22 |
|
| 23 |
/** |
| 24 |
* Instance |
| 25 |
* |
| 26 |
* @since 5.1.0 |
| 27 |
* |
| 28 |
* @access private |
| 29 |
* @var object Class object. |
| 30 |
*/ |
| 31 |
private static $instance; |
| 32 |
|
| 33 |
/** |
| 34 |
* Initiator |
| 35 |
* |
| 36 |
* @since 5.1.0 |
| 37 |
* |
| 38 |
* @return object initialized object of class. |
| 39 |
*/ |
| 40 |
public static function get() { |
| 41 |
if ( null === self::$instance ) { |
| 42 |
self::$instance = new self(); |
| 43 |
} |
| 44 |
return self::$instance; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor |
| 49 |
* |
| 50 |
* @since 5.1.0 |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
add_action( 'admin_menu', [ $this, 'register_menus' ] ); |
| 54 |
add_action( 'load-settings_page_ctc', [ $this, 'load_dashboard' ] ); |
| 55 |
add_action( 'admin_enqueue_scripts', [ $this, 'load_rules_list_assets' ], 10 ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Load dashboard assets and body class before admin header (required for page=ctc). |
| 60 |
* |
| 61 |
* @since 5.1.0 |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function load_dashboard() { |
| 65 |
Dashboard::get()->bootstrap(); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Register menus (all under Settings). |
| 70 |
* |
| 71 |
* @since 5.1.0 |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function register_menus() { |
| 75 |
add_submenu_page( |
| 76 |
'options-general.php', |
| 77 |
__( 'Copy to Clipboard', 'ctc' ), |
| 78 |
__( 'Copy to Clipboard', 'ctc' ), |
| 79 |
'manage_options', |
| 80 |
'ctc', |
| 81 |
[ $this, 'render_dashboard' ] |
| 82 |
); |
| 83 |
|
| 84 |
add_submenu_page( |
| 85 |
'options-general.php', |
| 86 |
__( 'Global Injector', 'ctc' ), |
| 87 |
'↳ ' . __( 'Global Injector', 'ctc' ), |
| 88 |
'manage_options', |
| 89 |
'ctc-rules', |
| 90 |
[ $this, 'render_rules_list' ] |
| 91 |
); |
| 92 |
|
| 93 |
add_submenu_page( |
| 94 |
'options-general.php', |
| 95 |
__( 'Add New Rule', 'ctc' ), |
| 96 |
'↳ ' . __( 'Add new', 'ctc' ), |
| 97 |
'manage_options', |
| 98 |
'ctc-global-injector', |
| 99 |
[ Global_Injector::get(), 'render' ] |
| 100 |
); |
| 101 |
|
| 102 |
add_submenu_page( |
| 103 |
'options-general.php', |
| 104 |
__( 'Analytics', 'ctc' ), |
| 105 |
'↳ ' . __( 'Analytics', 'ctc' ), |
| 106 |
'manage_options', |
| 107 |
'ctc-analytics', |
| 108 |
[ Analytics::get(), 'render' ] |
| 109 |
); |
| 110 |
|
| 111 |
/* |
| 112 |
add_submenu_page( |
| 113 |
'options-general.php', |
| 114 |
__( 'Copy to Clipboard Settings', 'ctc' ), |
| 115 |
'↳ ' . __( 'Settings', 'ctc' ), |
| 116 |
'manage_options', |
| 117 |
'ctc-settings', |
| 118 |
[ $this, 'render_settings_placeholder' ] |
| 119 |
); |
| 120 |
*/ |
| 121 |
|
| 122 |
/* |
| 123 |
add_submenu_page( |
| 124 |
'options-general.php', |
| 125 |
__( 'Help & Docs', 'ctc' ), |
| 126 |
'↳ ' . __( 'Help & Docs', 'ctc' ), |
| 127 |
'manage_options', |
| 128 |
'ctc-help', |
| 129 |
[ $this, 'render_help_placeholder' ] |
| 130 |
); |
| 131 |
*/ |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Render dashboard (React app mounts here). |
| 136 |
* |
| 137 |
* @since 5.1.0 |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function render_dashboard() { |
| 141 |
?> |
| 142 |
<div class="wrap ctc-admin-root ctc-dashboard-page" id="ctc-dashboard-root"></div> |
| 143 |
<?php |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Load Main Rule List assets on the rules list page (runs before Pro enqueues ctc-main-rule-list-pro). |
| 148 |
* |
| 149 |
* @since 5.1.0 |
| 150 |
* @param string $hook Current admin page hook. |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function load_rules_list_assets( $hook ) { |
| 154 |
if ( 'settings_page_ctc-rules' !== $hook ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
Main_Rule_List::get()->bootstrap(); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Render rules list page (Main Rule List UI mounts here). |
| 162 |
* |
| 163 |
* @since 5.1.0 |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public function render_rules_list() { |
| 167 |
?> |
| 168 |
<div class="wrap ctc-admin-root ctc-main-rule-list-page" id="ctc-main-rule-list-root"></div> |
| 169 |
<?php |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Placeholder for future Settings page. |
| 174 |
* |
| 175 |
* @since 5.1.0 |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function render_settings_placeholder() { |
| 179 |
?> |
| 180 |
<div class="wrap"> |
| 181 |
<h1><?php esc_html_e( 'Copy to Clipboard Settings', 'ctc' ); ?></h1> |
| 182 |
<p><?php esc_html_e( 'Settings will be available in a future release.', 'ctc' ); ?></p> |
| 183 |
</div> |
| 184 |
<?php |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Placeholder for future Help & Docs page. |
| 189 |
* |
| 190 |
* @since 5.1.0 |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public function render_help_placeholder() { |
| 194 |
?> |
| 195 |
<div class="wrap"> |
| 196 |
<h1><?php esc_html_e( 'Help & Docs', 'ctc' ); ?></h1> |
| 197 |
<p><?php esc_html_e( 'Documentation and help will be available here in a future release.', 'ctc' ); ?></p> |
| 198 |
</div> |
| 199 |
<?php |
| 200 |
} |
| 201 |
} |
| 202 |
|