PluginProbe
Plugin Groups / 1.2.2
Plugin Groups v1.2.2
trunk 1.0.3 1.1.0 1.2.1 1.2.2 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.9 3.0.0
plugin-groups / classes / class-settings.php

class-settings.php in Plugin Groups 1.2.2, at classes/class-settings.php

612 lines 16.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Groups Setting.
4 *
5 * @package Plugin_Groups
6 * @author David Cramer <david@digilab.co.za>
7 * @license GPL-2.0+
8 * @link
9 * @copyright 2015 David Cramer <david@digilab.co.za>
10 */
11
12 /**
13 * Plugin class.
14 *
15 * @package Plugin_Groups
16 * @author David Cramer <david@digilab.co.za>
17 */
18 class Plugin_Groups_Settings extends Plugin_Groups {
19
20
21 /**
22 * Constructor for class
23 *
24 * @since 0.0.1
25 */
26 public function __construct() {
27
28 if ( is_multisite() ) {
29 $this->multisite = 'network_';
30 }
31
32 // add admin page
33 add_action( "{$this->multisite}admin_menu", array(
34 $this,
35 'add_settings_pages',
36 ), 25 );
37 // save config
38 add_action( 'wp_ajax_plorg_save_config', array(
39 $this,
40 'save_config',
41 ) );
42 // get plugins filters
43 add_filter( 'all_plugins', array( $this, 'prepare_filter_addons' ) );
44 add_filter( 'views_plugins', array(
45 $this,
46 'filter_addons_filter_addons',
47 ) );
48 add_filter( 'views_plugins-network', array(
49 $this,
50 'filter_addons_filter_addons',
51 ) );
52 add_filter( 'show_advanced_plugins', array(
53 $this,
54 'filter_addons_do_filter_addons',
55 ) );
56 add_action( 'after_plugin_row', array(
57 $this,
58 'filter_addons_prepare_filter_addons_referer',
59 ), 10, 2 );
60 add_action( 'check_admin_referer', array(
61 $this,
62 'filter_addons_prepare_filter_addons_referer',
63 ), 10, 2 );
64
65 // presets
66 add_filter( 'plugin-groups-get-presets', array(
67 $this,
68 'get_preset_groups',
69 ) );
70
71 // exporter
72 add_action( 'init', array( $this, 'check_exporter' ) );
73
74 // add to bulk actions.
75 add_filter( 'bulk_actions-plugins', array( $this, 'bulk_actions' ) );
76 // action handler.
77 add_filter( 'handle_bulk_actions-plugins', array(
78 $this,
79 'bulk_action_handler',
80 ), 10, 3 );
81
82 // add options.
83 add_filter( 'disabled-screen_options_show_submit', array(
84 $this,
85 'screen_options',
86 ), 10, 2 );
87
88 }
89
90 /**
91 * Checks if is a group status.
92 *
93 * @since 0.0.1
94 */
95 public function screen_options( $opt, $screen ) {
96 if ( 'plugins' === $screen->id ) {
97 echo '<fieldset class="metabox-prefs plugin-groups">';
98 echo '<legend>' . __( 'Preset Groups' ) . '</legend>';
99 // Presets / Intergrations
100 $presetGroups = apply_filters( 'plugin-groups-get-presets', array() );
101 foreach ( $presetGroups as $group => $group_keys ) {
102 echo '<label for="list-preset-group">';
103 echo '<input id="list-view-mode" type="checkbox" name="group_preset[]" value="' . esc_attr__( $group ) . '" />';
104 echo esc_html( $group );
105 echo '</label>';
106 }
107 echo '</fieldset>';
108 }
109
110 return false;
111 }
112
113 /**
114 * Checks if is a group status.
115 *
116 * @since 0.0.1
117 */
118 public function is_group( $status ) {
119 $plugin_groups = Plugin_Groups_Options::get_single( 'plugin_groups' );
120 $return = false;
121 if ( ! empty( $plugin_groups['presets'] ) ) {
122 foreach ( $plugin_groups['presets'] as $preset_key => $preset ) {
123 $key = '_' . sanitize_key( $preset );
124 if ( $status === $key ) {
125 $return = true;
126 }
127 }
128 }
129 if ( ! empty( $plugin_groups['group'] ) ) {
130 foreach ( $plugin_groups['group'] as $group_key => $group ) {
131 $key = '_' . sanitize_key( $group['config']['group_name'] );
132 if ( $status === $key ) {
133 $return = true;
134 }
135 }
136 }
137
138 return $return;
139 }
140
141 /**
142 * Add to bulk actions.
143 *
144 * @since 0.0.1
145 */
146 public function bulk_actions( $actions ) {
147
148 $plugin_groups = Plugin_Groups_Options::get_single( 'plugin_groups' );
149 $current_status = filter_input( INPUT_GET, 'plugin_status', FILTER_SANITIZE_STRING );
150 if ( $this->is_group( $current_status ) ) {
151 $actions['_remove_group'] = __( 'Remove Group' );
152 }
153 $actions['_add_to_new_group'] = __( 'New Group' );
154 if ( ! empty( $plugin_groups['group'] ) ) {
155 foreach ( $plugin_groups['group'] as $key => $group ) {
156 $actions[ $key ] = __( 'Add to ' ) . $group['config']['group_name'];
157 }
158 }
159 return $actions;
160 }
161
162 /**
163 * Handle bulk action.
164 *
165 * @since 0.0.1
166 */
167 public function bulk_action_handler( $sendback, $action, $plugins ) {
168
169
170 $plugin_groups = Plugin_Groups_Options::get_single( 'plugin_groups' );
171 if ( ! empty( $plugin_groups['group'][ $action ] ) ) {
172
173 foreach ( $plugins as $plugin ) {
174 if ( ! in_array( $plugin, $plugin_groups['group'][ $action ]['config']['plugins'], true ) ) {
175 $plugin_groups['group'][ $action ]['config']['plugins'][] = $plugin;
176 }
177 }
178 Plugin_Groups_Options::update( $plugin_groups );
179 $key = '_' . sanitize_key( $plugin_groups['group'][ $action ]['config']['group_name'] );
180 $sendback = admin_url( 'plugins.php?plugin_status=' . $key );
181
182 }
183 if ( '_add_to_new_group' === $action ) {
184
185 $group_id = uniqid( 'nd' );
186 $new_name = filter_input( INPUT_POST, 'new_group', FILTER_SANITIZE_STRING );
187 $plugin_groups['group'][ $group_id ] = array(
188 '_id' => $group_id,
189 '_node_point' => 'group.' . $group_id,
190 'config' => array(
191 'group_name' => $new_name,
192 'plugins' => $plugins,
193 'keywords' => '',
194 ),
195 );
196 $key = '_' . sanitize_key( $new_name );
197 $sendback = admin_url( 'plugins.php?plugin_status=' . $key );
198 Plugin_Groups_Options::update( $plugin_groups );
199 }
200 if ( '_remove_group' === $action ) {
201 $to_remove = filter_input( INPUT_POST, 'plugin_status', FILTER_SANITIZE_STRING );
202 // presets.
203 if ( ! empty( $plugin_groups['presets'] ) ) {
204 foreach ( $plugin_groups['presets'] as $preset_key => $preset ) {
205 $key = '_' . sanitize_key( $preset );
206 if ( $to_remove === $key ) {
207 unset( $plugin_groups['presets'][ $preset_key ] );
208 }
209 }
210 }
211
212 foreach ( $plugin_groups['group'] as $group_key => $group ) {
213 $key = '_' . sanitize_key( $group['config']['group_name'] );
214 if ( $to_remove === $key ) {
215 unset( $plugin_groups['group'][ $group_key ] );
216 }
217 }
218 $sendback = admin_url( 'plugins.php' );
219 Plugin_Groups_Options::update( $plugin_groups );
220 }
221
222 return $sendback;
223 }
224
225 /**
226 * builds an export
227 *
228 * @uses "wp_ajax_plorg_check_exporter" hook
229 * @since 0.0.1
230 */
231 public function check_exporter() {
232
233 if ( current_user_can( 'manage_options' ) ) {
234
235 if ( ! empty( $_REQUEST['download'] ) && ! empty( $_REQUEST['plugin-groups-export'] ) && wp_verify_nonce( $_REQUEST['plugin-groups-export'], 'plugin-groups' ) ) {
236
237 $data = Plugin_Groups_Options::get_single( $_REQUEST['download'] );
238
239 header( 'Content-Type: application/json' );
240 header( 'Content-Disposition: attachment; filename="plugin-groups-export.json"' );
241 echo wp_json_encode( $data );
242 exit;
243
244 }
245
246 }
247 }
248
249 /**
250 * Sets the status back to the group on action ( activate etc.)
251 *
252 * @since 0.0.1
253 */
254 public function filter_addons_prepare_filter_addons_referer( $a, $b ) {
255 global $status;
256 if ( ! function_exists( 'get_current_screen' ) ) {
257 return;
258 }
259
260 // work on plugins list
261 $plugin_groups = Plugin_Groups_Options::get_single( 'plugin_groups' );
262 if ( ! empty( $plugin_groups['presets'] ) ) {
263 $presets = $this->apply_preset_groups( $plugin_groups['presets'] );
264 }
265 if ( ! empty( $presets ) ) {
266 if ( empty( $plugin_groups['group'] ) ) {
267 $plugin_groups['group'] = array();
268 }
269 $plugin_groups['group'] = array_merge( $plugin_groups['group'], $presets );
270 }
271 $screen = get_current_screen();
272 if ( is_object( $screen ) && $screen->base === 'plugins' && isset( $_REQUEST['plugin_status'] ) && ! empty( $plugin_groups['group'] ) ) {
273 foreach ( $plugin_groups['group'] as $group ) {
274 $key = '_' . sanitize_key( $group['config']['group_name'] );
275 if ( $_REQUEST['plugin_status'] === $key ) {
276 $status = $key;
277 break;
278 }
279 }
280 }
281 }
282
283 /**
284 * Add new filter group
285 *
286 * @since 0.0.1
287 * @return Bool
288 */
289 public function filter_addons_do_filter_addons( $a ) {
290 global $plugins, $status;
291
292 // work on plugins list
293 $plugin_groups = Plugin_Groups_Options::get_single( 'plugin_groups' );
294 if ( ! empty( $plugin_groups['presets'] ) ) {
295 $presets = $this->apply_preset_groups( $plugin_groups['presets'] );
296 }
297 if ( ! empty( $presets ) ) {
298 if ( empty( $plugin_groups['group'] ) ) {
299 $plugin_groups['group'] = array();
300 }
301 $plugin_groups['group'] = array_merge( $plugin_groups['group'], $presets );
302 }
303 if ( ! empty( $plugin_groups['group'] ) && is_array( $plugins ) ) {
304 foreach ( $plugins['all'] as $plugin_slug => $plugin_data ) {
305 foreach ( $plugin_groups['group'] as $group ) {
306 $key = '_' . sanitize_key( $group['config']['group_name'] );
307 if ( ! isset( $plugins[ $key ] ) ) {
308 $plugins[ $key ] = array();
309 }
310 if ( ! empty( $group['config']['plugins'] ) && in_array( $plugin_slug, $group['config']['plugins'], true ) ) {
311 $plugins[ $key ][ $plugin_slug ] = $plugin_data;
312 $plugins[ $key ][ $plugin_slug ]['plugin'] = $plugin_slug;
313 // is a remove group?
314 if ( ! empty( $group['config']['remove_base'] ) ) {
315 foreach ( $group['config']['plugins'] as $plugin ) {
316 unset( $plugins['all'][ $plugin ] );
317 }
318 }
319 // replicate teh next step
320 if ( current_user_can( 'update_plugins' ) ) {
321 $current = get_site_transient( 'update_plugins' );
322 if ( isset( $current->response[ $plugin_slug ] ) ) {
323 $plugins[ $key ][ $plugin_slug ]['update'] = true;
324 }
325 }
326 }
327 // do keyword.
328 if ( ! empty( $group['config']['auto_keyword'] ) && ! empty( $group['config']['keywords'] ) ) {
329 $keywords = explode( "\n", $group['config']['keywords'] );
330 foreach ( $keywords as $keyword ) {
331 $keyword = strtolower( trim( $keyword ) );
332 if ( false !== strpos( strtolower( $plugin_data['Name'] ), $keyword ) || false !== strpos( strtolower( $plugin_data['Description'] ), $keyword ) ) {
333 $plugins[ $key ][ $plugin_slug ] = $plugin_data;
334 $plugins[ $key ][ $plugin_slug ]['plugin'] = $plugin_slug;
335 }
336 }
337 }
338 }
339 }
340 }
341
342 return $a;
343 }
344
345 /**
346 * Add new filter view
347 *
348 * @since 0.0.1
349 * @return array|Views with added groups
350 */
351 public function filter_addons_filter_addons( $views ) {
352 global $status, $plugins;
353
354 // work on plugins list
355 $plugin_groups = Plugin_Groups_Options::get_single( 'plugin_groups' );
356 if ( ! empty( $plugin_groups['presets'] ) ) {
357 $presets = $this->apply_preset_groups( $plugin_groups['presets'] );
358 }
359 if ( ! empty( $presets ) ) {
360 if ( empty( $plugin_groups['group'] ) ) {
361 $plugin_groups['group'] = array();
362 }
363 $plugin_groups['group'] = array_merge( $plugin_groups['group'], $presets );
364 }
365 if ( ! empty( $plugin_groups['group'] ) ) {
366 foreach ( $plugin_groups['group'] as $group ) {
367
368 $key = '_' . sanitize_key( $group['config']['group_name'] );
369 if ( empty( $plugins[ $key ] ) ) {
370 $views[ $key ] = $group['config']['group_name'] . ' <span class="count">(0)</span>';
371 continue;
372 }
373 $count = 0;
374 foreach ( $plugins[ $key ] as $plugin_data ) {
375
376 if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_data['plugin'] ) ) {
377 $count ++;
378 }
379
380 }
381
382 $class = "";
383 if ( $status == $key ) {
384 $class = 'current';
385 }
386 $views[ $key ] = '<a class="' . $class . '" href="plugins.php?plugin_status=' . $key . '">' . $group['config']['group_name'] . ' <span class="count">(' . $count . ')</span></a>';
387
388 }
389 }
390
391 return $views;
392 }
393
394 /**
395 * alter and set the current status.
396 *
397 * @since 0.0.1
398 * @return array|plugins - no change
399 */
400 public function prepare_filter_addons( $plugins ) {
401 global $wp_list_table, $status;
402
403 // work on plugins list
404 $plugin_groups = Plugin_Groups_Options::get_single( 'plugin_groups' );
405 if ( ! empty( $plugin_groups['presets'] ) ) {
406 $presets = $this->apply_preset_groups( $plugin_groups['presets'] );
407 }
408 if ( ! empty( $presets ) ) {
409 if ( empty( $plugin_groups['group'] ) ) {
410 $plugin_groups['group'] = array();
411 }
412 $plugin_groups['group'] = array_merge( $plugin_groups['group'], $presets );
413 }
414 if ( isset( $_REQUEST['plugin_status'] ) && ! empty( $plugin_groups['group'] ) ) {
415 foreach ( $plugin_groups['group'] as $group ) {
416 $key = '_' . sanitize_key( $group['config']['group_name'] );
417 if ( $_REQUEST['plugin_status'] === $key ) {
418 $status = $key;
419 break;
420 }
421 }
422 }
423
424 return $plugins;
425 }
426
427 /**
428 * built in presets keywords
429 *
430 * @since 0.0.1
431 * @return array with preset groups
432 */
433 public function get_preset_groups( $groups ) {
434
435 $baseGroups = array(
436 'WooCommerce' => array( 'WooCommerce' ),
437 'Easy Digital Downloads' => array( 'Easy Digital Downloads' ),
438 'Ninja Forms' => array( 'Ninja Forms' ),
439 'Gravity Forms' => array( 'Gravity Forms' ),
440 'CalderaWP' => array( 'Caldera' ),
441 );
442
443 $baseGroups = array_merge( $groups, $baseGroups );
444
445 return $baseGroups;
446 }
447
448 /**
449 * get presets keywords
450 *
451 * @since 0.0.1
452 * @return array with preset groups
453 */
454 public function apply_preset_groups( $presets ) {
455
456 $presetGroups = apply_filters( 'plugin-groups-get-presets', array() );
457 $groups = array();
458 foreach ( $presets as $preset_key => $preset ) {
459 if ( empty( $presetGroups[ $preset ] ) ) {
460 continue;
461 }
462 $group_id = sanitize_key( $preset );
463 $groups[ $group_id ] = array(
464 'config' => array(
465 'group_name' => $preset,
466 'auto_keyword' => true,
467 'keywords' => implode( "/n", (array) $presetGroups[ $preset ] ),
468 ),
469 );
470 }
471
472 return $groups;
473 }
474
475 /**
476 * Saves a config
477 *
478 * @uses "wp_ajax_plorg_save_config" hook
479 * @since 0.0.1
480 */
481 public function save_config() {
482
483 if ( empty( $_POST['plugin-groups-setup'] ) || ! wp_verify_nonce( $_POST['plugin-groups-setup'], 'plugin-groups' ) ) {
484 if ( empty( $_POST['config'] ) ) {
485 return;
486 }
487 }
488
489 if ( ! empty( $_POST['plugin-groups-setup'] ) && empty( $_POST['config'] ) ) {
490 $config = stripslashes_deep( $_POST['config'] );
491
492 Plugin_Groups_Options::update( $config );
493
494
495 wp_redirect( '?page=plugin_groups&updated=true' );
496 exit;
497 }
498
499 if ( ! empty( $_POST['config'] ) ) {
500
501 $config = json_decode( stripslashes_deep( $_POST['config'] ), true );
502
503 if ( wp_verify_nonce( $config['plugin-groups-setup'], 'plugin-groups' ) ) {
504 Plugin_Groups_Options::update( $config );
505 wp_send_json_success( $config );
506 }
507
508 }
509
510 // nope
511 wp_send_json_error( $config );
512
513 }
514
515 /**
516 * Array of "internal" fields not to mess with
517 *
518 * @since 0.0.1
519 * @return array
520 */
521 public function internal_config_fields() {
522 return array( '_wp_http_referer', 'id', '_current_tab' );
523 }
524
525
526 /**
527 * Deletes an item
528 *
529 * @uses 'wp_ajax_plorg_create_plugin_groups' action
530 * @since 0.0.1
531 */
532 public function delete_plugin_groups() {
533
534 $deleted = Plugin_Groups_Options::delete( strip_tags( $_POST['block'] ) );
535
536 if ( $deleted ) {
537 wp_send_json_success( $_POST );
538 } else {
539 wp_send_json_error( $_POST );
540 }
541
542
543 }
544
545 /**
546 * Create a new item
547 *
548 * @uses "wp_ajax_plorg_create_plugin_groups" action
549 * @since 0.0.1
550 */
551 public function create_new_plugin_groups() {
552 $new = Plugin_Groups_Options::create( $_POST['name'], $_POST['slug'] );
553
554 if ( is_array( $new ) ) {
555 wp_send_json_success( $new );
556 } else {
557 wp_send_json_error( $_POST );
558 }
559
560 }
561
562
563 /**
564 * Add options page
565 *
566 * @since 0.0.1
567 * @uses "admin_menu" hook
568 */
569 public function add_settings_pages() {
570 // This page will be under "Settings"
571
572 $this->plugin_screen_hook_suffix['plugin_groups'] = add_submenu_page( 'plugins.php', __( 'Plugin Groups', $this->plugin_slug ), __( 'Groups', $this->plugin_slug ), $this->capability, 'plugin_groups', array(
573 $this,
574 'create_admin_page',
575 ) );
576 add_action( 'admin_print_styles-' . $this->plugin_screen_hook_suffix['plugin_groups'], array(
577 $this,
578 'enqueue_admin_stylescripts',
579 ) );
580
581 }
582
583 /**
584 * Options page callback
585 *
586 * @since 0.0.1
587 */
588 public function create_admin_page() {
589 // Set class property
590 $screen = get_current_screen();
591 $base = array_search( $screen->id, $this->plugin_screen_hook_suffix );
592
593 // include main template
594 include PLORG_PATH . 'includes/edit.php';
595
596 // php based script include
597 if ( file_exists( PLORG_PATH . 'assets/js/inline-scripts.php' ) ) {
598 echo "<script type=\"text/javascript\">\r\n";
599 include PLORG_PATH . 'assets/js/inline-scripts.php';
600 echo "</script>\r\n";
601 }
602
603 }
604
605
606 }
607
608 if ( is_admin() ) {
609 global $settings_plugin_groups;
610 $settings_plugin_groups = new Plugin_Groups_Settings();
611 }
612