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