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