PluginProbe ʕ •ᴥ•ʔ
Code Manager / 1.0.2
Code Manager v1.0.2
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 5 years ago
class-code-manager-admin.php
329 lines
1 <?php
2
3 /**
4 * Class Code_Manager_Admin
5 *
6 * Defines admin specific functionality for the Code Manager.
7 *
8 * @author Peter Schulz
9 * @since 1.0.0
10 */
11 class Code_Manager_Admin {
12
13 /**
14 * Current page (menu slug)
15 *
16 * @var null|string
17 */
18 protected $page = null;
19
20 /**
21 * Tab mode:
22 * on > Code Manager works in tab mode (edit multiple codes simultaneously)
23 * off > Code Manager works in list mode (edit single codes + enable/disable code)
24 *
25 * @var string
26 */
27 protected $tabmode = 'off';
28
29 /**
30 * Handle to list view
31 *
32 * @var Code_Manager\Code_Manager_List_View
33 */
34 protected $cm_list_view = null;
35
36 /**
37 * Code_Manager_Admin constructor.
38 *
39 * Checks if WP Data Access is available to add additional features.
40 *
41 * @since 1.0.0
42 */
43 public function __construct() {
44 if ( isset( $_REQUEST['page'] ) ) {
45 $this->page = sanitize_text_field( wp_unslash( $_REQUEST['page'] ) ); // input var okay.
46 }
47
48 if ( isset( $_REQUEST['tabmode'] ) ) {
49 $this->tabmode = sanitize_text_field( wp_unslash( $_REQUEST['tabmode'] ) ); // input var okay.
50 }
51 }
52
53 /**
54 * Add stylesheets to back-end
55 *
56 * Only added when Code Manager is visible. Adds minimal CSS depending on actual page (menu slug).
57 *
58 * @since 1.0.0
59 */
60 public function enqueue_styles() {
61 if ( CODE_MANAGER_MENU_SLUG === $this->page ) {
62 wp_enqueue_style( 'wp-codemirror' );
63
64 if ( isset( $_REQUEST['action'] ) &&
65 ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] )
66 ) {
67 // Code Manager data entry form
68 wp_enqueue_style(
69 'code_manager',
70 plugins_url( '../assets/css/code_manager.css', __FILE__ ),
71 [],
72 CODE_MANAGER_VERSION
73 );
74 } elseif ( 'on' === $this->tabmode ) {
75 // Code Manager tab mode
76 wp_enqueue_style(
77 'code_manager_tabmode',
78 plugins_url( '../assets/css/code_manager_tabmode.css', __FILE__ ),
79 [],
80 CODE_MANAGER_VERSION
81 );
82 }
83
84 $this->load_global_css();
85 } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) {
86 // Code Manager list mode
87 wp_enqueue_style(
88 'code_manager_settings',
89 plugins_url( '../assets/css/code_manager_settings.css', __FILE__ ),
90 [],
91 CODE_MANAGER_VERSION
92 );
93
94 $this->load_global_css();
95 }
96 }
97
98 /**
99 * Adds CSS needed by all Code Manager pages
100 *
101 * @since 1.0.0
102 */
103 protected function load_global_css() {
104 // Material icons are used in page headers
105 wp_enqueue_style(
106 'code_manager_material_icons',
107 plugins_url( '../assets/icons/material-icons.css', __FILE__ ),
108 [],
109 CODE_MANAGER_VERSION
110 );
111
112 // Global Code Manager styling
113 wp_enqueue_style(
114 'code_manager_global',
115 plugins_url( '../assets/css/code_manager_global.css', __FILE__ ),
116 [],
117 CODE_MANAGER_VERSION
118 );
119
120 // Add tooltips to add Code Manager pages
121 wp_enqueue_style(
122 'code_manager_tooltip_css',
123 plugins_url( '../assets/css/jquery-ui-darkness.css', __FILE__ ),
124 [],
125 CODE_MANAGER_VERSION
126 );
127 }
128
129 /**
130 * Add scripts to back-end
131 *
132 * Only added when Code Manager is visible. Adds relevant JS only depending on action and tabmode.
133 *
134 * @since 1.0.0
135 */
136 public function enqueue_scripts() {
137 if ( CODE_MANAGER_MENU_SLUG === $this->page ) {
138 wp_enqueue_script( 'jquery' );
139 wp_enqueue_script( 'jquery-ui-tooltip' );
140 wp_enqueue_script( 'jquery-ui-dialog' );
141
142 // Register codeEditor (PHP = default editor, can be overridden on user request)
143 $cm_settings['codeEditor'] =
144 wp_enqueue_code_editor(
145 [
146 'type' => 'application/x-httpd-php',
147 'codemirror' => [
148 'lineNumbers' => true,
149 'autoRefresh' => true,
150 'mode' => 'php',
151 'lineWrapping' => true,
152 ],
153 ]
154 );
155 wp_enqueue_script( 'wp-theme-plugin-editor' );
156 wp_localize_script( 'wp-theme-plugin-editor', 'cm_settings', $cm_settings );
157
158 if (
159 isset( $_REQUEST['action'] ) &&
160 ( 'new' === $_REQUEST['action'] || 'edit' === $_REQUEST['action'] )
161 ) {
162 // Code Manager data entry form
163 wp_enqueue_script(
164 'code_manager',
165 plugins_url( '../assets/js/code_manager.js', __FILE__ ),
166 [],
167 CODE_MANAGER_VERSION
168 );
169 } elseif ( 'on' === $this->tabmode ) {
170 // Code Manager tab mode
171 wp_enqueue_script(
172 'code_manager_tabmode',
173 plugins_url( '../assets/js/code_manager_tabmode.js', __FILE__ ),
174 [],
175 CODE_MANAGER_VERSION
176 );
177 } else {
178 // Code Manager list mode
179 wp_enqueue_script(
180 'code_manager_listmode',
181 plugins_url( '../assets/js/code_manager_listmode.js', __FILE__ ),
182 [],
183 CODE_MANAGER_VERSION
184 );
185
186 // Enqueue clipboard
187 wp_enqueue_script( 'clipboard' );
188 }
189
190 // Register external library notify.js
191 wp_enqueue_script(
192 'code_manager_notify',
193 plugins_url( '../assets/js/notify.min.js', __FILE__ ),
194 [ 'jquery' ],
195 CODE_MANAGER_VERSION
196 );
197 } elseif ( CODE_MANAGER_SETTINGS_MENU_SLUG === $this->page ) {
198 wp_enqueue_script( 'jquery' );
199 wp_enqueue_script( 'jquery-ui-tooltip' );
200 wp_enqueue_script( 'jquery-ui-dialog' );
201 }
202 }
203
204 /**
205 * Add plugin menus (only accessible to admin users)
206 *
207 * @since 1.0.0
208 */
209 public function add_menu_items() {
210 if ( current_user_can( 'manage_options' ) ) {
211 // Check if code manager table is available
212 $code_manager_model_class = CODE_MANAGER_MODEL_CLASS;
213 $code_manager_model = new $code_manager_model_class();
214 $code_manager_table_found = $code_manager_model::table_exists();
215
216 if ( $code_manager_table_found ) {
217 // Determine view mode
218 $code_manager_page = 'on' === $this->tabmode ? 'code_manager_tab_mode' : 'code_manager_page';
219 } else {
220 // Show error page if plugin table is not found
221 $code_manager_page = 'code_manager_page_not_found';
222 }
223
224 // Add top level menu
225 add_menu_page(
226 CODE_MANAGER_MENU_SLUG,
227 CODE_MANAGER_MENU_TITLE,
228 'manage_options',
229 CODE_MANAGER_MENU_SLUG,
230 null,
231 'dashicons-editor-code',
232 999999999
233 );
234
235 // Add Code Manager submenu
236 $cm_list_menu = add_submenu_page(
237 CODE_MANAGER_MENU_SLUG,
238 CODE_MANAGER_PAGE_TITLE,
239 CODE_MANAGER_MENU_TITLE,
240 'manage_options',
241 CODE_MANAGER_MENU_SLUG,
242 [ $this, $code_manager_page ]
243 );
244
245 // Create list view to handle screen options
246 if ( $this->page === CODE_MANAGER_MENU_SLUG && $code_manager_table_found && 'on' !== $this->tabmode ) {
247 header( "X-XSS-Protection: 0" );
248 $code_manager_main_class = CODE_MANAGER_MAIN_CLASS;
249 $this->cm_list_view = new $code_manager_main_class(
250 [
251 'page_hook_suffix' => $cm_list_menu,
252 ]
253 );
254 }
255 }
256 }
257
258 /**
259 * Register settings page
260 *
261 * @since 1.0.0
262 */
263 public function code_manager_register_settings_page() {
264 add_options_page(
265 CODE_MANAGER_SETTINGS_PAGE_TITLE,
266 CODE_MANAGER_SETTINGS_MENU_TITLE,
267 'manage_options',
268 CODE_MANAGER_SETTINGS_MENU_SLUG,
269 [
270 $this,
271 'code_manager_settings_page'
272 ]
273 );
274 }
275
276 /**
277 * Show settings page
278 *
279 * @since 1.0.0
280 */
281 public function code_manager_settings_page() {
282 $code_manager_settings_class = CODE_MANAGER_SETTINGS_CLASS;
283 $cm_settings = new $code_manager_settings_class();
284 $cm_settings->show();
285 }
286
287 /**
288 * Show Code Manager in list mode
289 *
290 * @since 1.0.0
291 */
292 public function code_manager_page() {
293 $this->cm_list_view->show();
294 }
295
296 /**
297 * Show Code Manager in tab mode
298 *
299 * @since 1.0.0
300 */
301 public function code_manager_tab_mode() {
302 $code_manager_tab_class = CODE_MANAGER_TAB_CLASS;
303 $tabmode = new $code_manager_tab_class();
304 $tabmode->show();
305 }
306
307 /**
308 * Plugin table not found > show error page
309 *
310 * @since 1.0.0
311 */
312 public function code_manager_page_not_found() {
313 ?>
314 <div class="wrap">
315 <h1 class="wp-heading-inline">
316 <span><?php echo CODE_MANAGER_H1_TITLE; ?></span>
317 <a href="<?php echo CODE_MANAGER_HELP_URL; ?>" target="_blank" title="Plugin Help - open a new tab or window">
318 <span class="dashicons dashicons-editor-help"
319 style="text-decoration:none;vertical-align:top;font-size:36px;">
320 </span></a>
321 </h1>
322 <p>
323 <?php echo __( 'ERROR: Repository table not found!', 'code-manager' ); ?>
324 </p>
325 </div>
326 <?php
327 }
328 }
329