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