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