PluginProbe ʕ •ᴥ•ʔ
Code Manager / 1.0.12
Code Manager v1.0.12
1.0.47 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.3 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9
code-manager / admin / class-code-manager-admin.php
code-manager / admin Last commit date
class-code-manager-admin.php 4 years ago
class-code-manager-admin.php
403 lines
1 <?php
2
3 use Code_Manager\Code_Manager_Dashboard;
4
5 /**
6 * Class Code_Manager_Admin
7 *
8 * Defines admin specific functionality for the Code Manager.
9 *
10 * @author Peter Schulz
11 * @since 1.0.0
12 */
13 class Code_Manager_Admin {
14
15 /**
16 * Current page (menu slug)
17 *
18 * @var null|string
19 */
20 protected $page = null;
21
22 /**
23 * Tab mode:
24 * on > Code Manager works in tab mode (edit multiple codes simultaneously)
25 * off > Code Manager works in list mode (edit single codes + enable/disable code)
26 *
27 * @var string
28 */
29 protected $tabmode = 'off';
30
31 /**
32 * Handle to list view
33 *
34 * @var Code_Manager\Code_Manager_List_View
35 */
36 protected $cm_list_view = null;
37
38 /**
39 * Code_Manager_Admin constructor.
40 *
41 * Checks if WP Data Access is available to add additional features.
42 *
43 * @since 1.0.0
44 */
45 public function __construct() {
46 if ( isset( $_REQUEST['page'] ) ) {
47 $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay.
48 }
49
50 if ( isset( $_REQUEST['tabmode'] ) ) {
51 $this->tabmode = sanitize_text_field( wp_unslash( $_REQUEST['tabmode'] ) ); // input var okay.
52 }
53 }
54
55 /**
56 * Add stylesheets to back-end
57 *
58 * Only added when Code Manager is visible. Adds minimal CSS depending on actual page (menu slug).
59 *
60 * @since 1.0.0
61 */
62 public function enqueue_styles() {
63 if ( CODE_MANAGER_MENU_SLUG === $this->page ) {
64 wp_enqueue_style( 'wp-codemirror' );
65
66 if ( isset( $_REQUEST['action'] ) &&
67 ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] )
68 ) {
69 // Code Manager data entry form
70 wp_enqueue_style(
71 'code_manager',
72 plugins_url( '../assets/css/code_manager.css', __FILE__ ),
73 [],
74 CODE_MANAGER_VERSION
75 );
76 } elseif ( 'on' === $this->tabmode ) {
77 // Code Manager tab mode
78 wp_enqueue_style(
79 'code_manager_tabmode',
80 plugins_url( '../assets/css/code_manager_tabmode.css', __FILE__ ),
81 [],
82 CODE_MANAGER_VERSION
83 );
84 }
85
86 $this->load_global_css();
87 } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) {
88 // Code Manager list mode
89 wp_enqueue_style(
90 'code_manager_settings',
91 plugins_url( '../assets/css/code_manager_settings.css', __FILE__ ),
92 [],
93 CODE_MANAGER_VERSION
94 );
95
96 $this->load_global_css();
97 }
98 }
99
100 /**
101 * Adds CSS needed by all Code Manager pages
102 *
103 * @since 1.0.0
104 */
105 protected function load_global_css() {
106 // Dashboard CSS
107 wp_enqueue_style(
108 'code_manager_dashboard',
109 plugins_url( '../assets/css/code_manager_dashboard.css', __FILE__ ),
110 [],
111 CODE_MANAGER_VERSION
112 );
113
114 // Material icons are used in page headers
115 wp_enqueue_style(
116 'code_manager_material_icons',
117 plugins_url( '../assets/icons/material-icons.css', __FILE__ ),
118 [],
119 CODE_MANAGER_VERSION
120 );
121
122 // Global Code Manager styling
123 wp_enqueue_style(
124 'code_manager_global',
125 plugins_url( '../assets/css/code_manager_global.css', __FILE__ ),
126 [],
127 CODE_MANAGER_VERSION
128 );
129
130 // Add tooltips to add Code Manager pages
131 wp_enqueue_style(
132 'code_manager_tooltip_css',
133 plugins_url( '../assets/css/jquery-ui.min.css', __FILE__ ),
134 [],
135 CODE_MANAGER_VERSION
136 );
137
138 // Load fontawesome icons
139 wp_enqueue_style(
140 'cm_fontawesome_icons',
141 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/fontawesome.min.css',
142 []
143 );
144 wp_enqueue_style(
145 'cm_fontawesome_icons_solid',
146 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/solid.min.css',
147 []
148 );
149 }
150
151 /**
152 * Add scripts to back-end
153 *
154 * Only added when Code Manager is visible. Adds relevant JS only depending on action and tabmode.
155 *
156 * @since 1.0.0
157 */
158 public function enqueue_scripts() {
159 if ( CODE_MANAGER_MENU_SLUG === $this->page ) {
160 $this->load_global_js();
161
162 // Register codeEditor (PHP = default editor, can be overridden on user request)
163 $cm_settings['codeEditor'] =
164 wp_enqueue_code_editor(
165 [
166 'type' => 'application/x-httpd-php',
167 'codemirror' => [
168 'lineNumbers' => true,
169 'autoRefresh' => true,
170 'mode' => 'php',
171 'lineWrapping' => true,
172 ],
173 ]
174 );
175 wp_enqueue_script( 'wp-theme-plugin-editor' );
176 wp_localize_script( 'wp-theme-plugin-editor', 'cm_settings', $cm_settings );
177
178 if (
179 isset( $_REQUEST['action'] ) &&
180 ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] )
181 ) {
182 // Code Manager data entry form
183 wp_enqueue_script(
184 'code_manager',
185 plugins_url( '../assets/js/code_manager.js', __FILE__ ),
186 [],
187 CODE_MANAGER_VERSION
188 );
189 } elseif ( 'on' === $this->tabmode ) {
190 // Code Manager tab mode
191 wp_enqueue_script(
192 'code_manager_tabmode',
193 plugins_url( '../assets/js/code_manager_tabmode.js', __FILE__ ),
194 [],
195 CODE_MANAGER_VERSION
196 );
197 } else {
198 // Code Manager list mode
199 wp_enqueue_script(
200 'code_manager_listmode',
201 plugins_url( '../assets/js/code_manager_listmode.js', __FILE__ ),
202 [],
203 CODE_MANAGER_VERSION
204 );
205 }
206
207 // Enqueue clipboard
208 wp_enqueue_script( 'clipboard' );
209
210 // Register external library notify.js
211 wp_enqueue_script(
212 'code_manager_notify',
213 plugins_url( '../assets/js/notify.min.js', __FILE__ ),
214 [ 'jquery' ],
215 CODE_MANAGER_VERSION
216 );
217 } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) {
218 $this->load_global_js();
219 }
220 }
221
222 protected function load_global_js() {
223 wp_enqueue_script( 'jquery' );
224 wp_enqueue_script( 'jquery-ui-core' );
225 wp_enqueue_script( 'jquery-ui-dialog' );
226 wp_enqueue_script( 'jquery-ui-tooltip' );
227 wp_enqueue_script( 'jquery-ui-sortable' );
228
229 // Dashboard JS
230 wp_enqueue_script(
231 'code_manager_dashboard',
232 plugins_url( '../assets/js/code_manager_dashboard.js', __FILE__ ),
233 [],
234 CODE_MANAGER_VERSION
235 );
236 }
237
238 /**
239 * Add plugin menus (only accessible to admin users)
240 *
241 * @since 1.0.0
242 */
243 public function add_menu_items() {
244 if ( current_user_can( 'manage_options' ) ) {
245 // Check if code manager table is available
246 $code_manager_model_class = CODE_MANAGER_MODEL_CLASS;
247 $code_manager_model = new $code_manager_model_class();
248 $code_manager_table_found = $code_manager_model::table_exists();
249
250 if ( $code_manager_table_found ) {
251 // Determine view mode
252 $code_manager_page = 'on' === $this->tabmode ? 'code_manager_tab_mode' : 'code_manager_page';
253 } else {
254 // Show error page if plugin table is not found
255 $code_manager_page = 'code_manager_page_not_found';
256 }
257
258 // Add top level menu
259 add_menu_page(
260 CODE_MANAGER_MENU_SLUG,
261 CODE_MANAGER_MENU_TITLE,
262 'manage_options',
263 CODE_MANAGER_MENU_SLUG,
264 null,
265 'dashicons-editor-code',
266 999999999
267 );
268
269 // Add Code Manager submenu
270 $cm_list_menu = add_submenu_page(
271 CODE_MANAGER_MENU_SLUG,
272 CODE_MANAGER_PAGE_TITLE,
273 CODE_MANAGER_MENU_TITLE,
274 'manage_options',
275 CODE_MANAGER_MENU_SLUG,
276 [ $this, $code_manager_page ]
277 );
278
279 // Create list view to handle screen options
280 if ( $this->page === CODE_MANAGER_MENU_SLUG && $code_manager_table_found && 'on' !== $this->tabmode ) {
281 header( "X-XSS-Protection: 0" );
282 $code_manager_main_class = CODE_MANAGER_MAIN_CLASS;
283 $this->cm_list_view = new $code_manager_main_class(
284 [
285 'page_hook_suffix' => $cm_list_menu,
286 ]
287 );
288 }
289 }
290 }
291
292 public function user_admin_notices() {
293 if ( ( 'code_manager' === $this->page || 'code_manager_settings' === $this->page ) ) {
294 $code_manager_plugin_hide_foreign_notices = get_option( 'code_manager_plugin_hide_foreign_notices' );
295 if (
296 false === $code_manager_plugin_hide_foreign_notices ||
297 'on' === $code_manager_plugin_hide_foreign_notices
298 ) {
299 remove_all_actions('admin_notices');
300 remove_all_actions('all_admin_notices');
301 }
302 }
303 }
304
305 public function submenu_filter( $submenu_file ) {
306 if ( ! Code_Manager_Dashboard::menu_enabled() ) {
307 $hidden_submenus = [
308 'code_manager-account',
309 'code_manager-wp-support-forum',
310 'code_manager-pricing',
311 'code_manager-contact',
312 'code_manager',
313 ];
314 } else {
315 $hidden_submenus = [];
316 }
317
318 foreach ( $hidden_submenus as $submenu ) {
319 remove_submenu_page( 'code_manager', $submenu );
320 }
321
322 return $submenu_file;
323 }
324
325 /**
326 * Register settings page
327 *
328 * @since 1.0.0
329 */
330 public function code_manager_register_settings_page() {
331 add_options_page(
332 CODE_MANAGER_SETTINGS_PAGE_TITLE,
333 CODE_MANAGER_SETTINGS_MENU_TITLE,
334 'manage_options',
335 CODE_MANAGER_SETTINGS_MENU_SLUG,
336 [
337 $this,
338 'code_manager_settings_page'
339 ]
340 );
341 }
342
343 /**
344 * Show settings page
345 *
346 * @since 1.0.0
347 */
348 public function code_manager_settings_page() {
349 Code_Manager_Dashboard::add_dashboard();
350
351 $code_manager_settings_class = CODE_MANAGER_SETTINGS_CLASS;
352 $cm_settings = new $code_manager_settings_class();
353 $cm_settings->show();
354 }
355
356 /**
357 * Show Code Manager in list mode
358 *
359 * @since 1.0.0
360 */
361 public function code_manager_page() {
362 Code_Manager_Dashboard::add_dashboard();
363
364 $this->cm_list_view->show();
365 }
366
367 /**
368 * Show Code Manager in tab mode
369 *
370 * @since 1.0.0
371 */
372 public function code_manager_tab_mode() {
373 Code_Manager_Dashboard::add_dashboard();
374
375 $code_manager_tab_class = CODE_MANAGER_TAB_CLASS;
376 $tabmode = new $code_manager_tab_class();
377 $tabmode->show();
378 }
379
380 /**
381 * Plugin table not found > show error page
382 *
383 * @since 1.0.0
384 */
385 public function code_manager_page_not_found() {
386 Code_Manager_Dashboard::add_dashboard();
387 ?>
388 <div class="wrap">
389 <h1 class="wp-heading-inline">
390 <span><?php echo CODE_MANAGER_H1_TITLE; ?></span>
391 <a href="<?php echo CODE_MANAGER_HELP_URL; ?>" target="_blank" title="Plugin Help - open a new tab or window">
392 <span class="dashicons dashicons-editor-help"
393 style="text-decoration:none;vertical-align:top;font-size:36px;">
394 </span></a>
395 </h1>
396 <p>
397 <?php echo __( 'ERROR: Repository table not found!', 'code-manager' ); ?>
398 </p>
399 </div>
400 <?php
401 }
402 }
403