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