| 1 |
<?php |
| 2 |
/** |
| 3 |
* Core class for Plugin Groups. |
| 4 |
* |
| 5 |
* @package plugin_groups |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Plugin_Groups; |
| 9 |
|
| 10 |
/** |
| 11 |
* Plugin_Groups Class. |
| 12 |
*/ |
| 13 |
class Plugin_Groups { |
| 14 |
|
| 15 |
/** |
| 16 |
* The single instance of the class. |
| 17 |
* |
| 18 |
* @var Plugin_Groups |
| 19 |
*/ |
| 20 |
protected static $instance = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the version of the plugin. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $version; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the plugin name. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected $plugin_name; |
| 35 |
|
| 36 |
/** |
| 37 |
* Holds the plugin config. |
| 38 |
* |
| 39 |
* @var array |
| 40 |
*/ |
| 41 |
protected $config = array(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Holds the Groups. |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
protected $groups = array(); |
| 49 |
|
| 50 |
/** |
| 51 |
* Holds the menu slug. |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
public static $slug; |
| 56 |
|
| 57 |
/** |
| 58 |
* Holds the current group. |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
protected $current_group; |
| 63 |
|
| 64 |
/** |
| 65 |
* Holds the current status. |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
protected $current_status; |
| 70 |
|
| 71 |
/** |
| 72 |
* Holds the current group and status path. |
| 73 |
* |
| 74 |
* @var string |
| 75 |
*/ |
| 76 |
protected $current_nav_path; |
| 77 |
|
| 78 |
/** |
| 79 |
* Hold the record of the plugins current version for upgrade. |
| 80 |
* |
| 81 |
* @var string |
| 82 |
*/ |
| 83 |
const VERSION_KEY = '_plugin_groups_version'; |
| 84 |
|
| 85 |
/** |
| 86 |
* Hold the config storage key. |
| 87 |
* |
| 88 |
* @var string |
| 89 |
*/ |
| 90 |
const CONFIG_KEY = '_plugin_groups_config'; |
| 91 |
|
| 92 |
/** |
| 93 |
* Initiate the plugin_groups object. |
| 94 |
*/ |
| 95 |
public function __construct() { |
| 96 |
|
| 97 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 98 |
$plugin = get_file_data( PLGGRP_CORE, array( 'Plugin Name', 'Version', 'Text Domain' ), 'plugin' ); |
| 99 |
$this->plugin_name = array_shift( $plugin ); |
| 100 |
$this->version = array_shift( $plugin ); |
| 101 |
self::$slug = array_shift( $plugin ); |
| 102 |
spl_autoload_register( array( $this, 'autoload_class' ), true, false ); |
| 103 |
|
| 104 |
// Start hooks. |
| 105 |
$this->setup_hooks(); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Setup and register WordPress hooks. |
| 110 |
*/ |
| 111 |
protected function setup_hooks() { |
| 112 |
|
| 113 |
// Load plugin text domain |
| 114 |
add_action( 'init', array( $this, 'load_text_domain' ) ); |
| 115 |
add_action( 'init', array( $this, 'plugin_groups_init' ), PHP_INT_MAX ); // Always the last thing to init. |
| 116 |
add_action( 'admin_init', array( $this, 'admin_init' ) ); |
| 117 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 118 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 119 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 120 |
add_filter( 'views_plugins', array( $this, 'add_groups' ), PHP_INT_MAX ); |
| 121 |
add_filter( 'views_plugins-network', array( $this, 'add_groups' ) ); |
| 122 |
add_filter( 'all_plugins', array( $this, 'catch_selected_group' ) ); |
| 123 |
add_filter( 'show_advanced_plugins', array( $this, 'filter_shown_status' ), 10, 2 ); |
| 124 |
add_filter( 'site_transient_update_plugins', array( $this, 'alter_update_plugins' ) ); |
| 125 |
add_action( 'pre_current_active_plugins', array( $this, 'render_group_navigation' ) ); |
| 126 |
add_filter( 'bulk_actions-plugins', array( $this, 'bulk_actions' ) ); |
| 127 |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_item' ), 100 ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Load the plugin text domain for translation. |
| 132 |
*/ |
| 133 |
public function load_text_domain() { |
| 134 |
|
| 135 |
load_plugin_textdomain( self::$slug, false, basename( PLGGRP_PATH ) . '/languages' ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Remove plugins from the update available. |
| 140 |
* |
| 141 |
* @param object $data The data object. |
| 142 |
* |
| 143 |
* @return object |
| 144 |
*/ |
| 145 |
public function alter_update_plugins( $data ) { |
| 146 |
|
| 147 |
if ( $this->current_group ) { |
| 148 |
$keys = array_keys( $this->groups[ $this->current_group ] ); |
| 149 |
foreach ( $data->response as $key => $plugin ) { |
| 150 |
if ( ! in_array( $key, $keys, true ) ) { |
| 151 |
unset( $data->response[ $key ] ); |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return $data; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Remove status that are not relevant. |
| 161 |
* |
| 162 |
* @param bool $show Flag to show. |
| 163 |
* @param string $status The current status |
| 164 |
* |
| 165 |
* @return bool |
| 166 |
*/ |
| 167 |
public function filter_shown_status( $show, $status ) { |
| 168 |
|
| 169 |
global $plugins; |
| 170 |
|
| 171 |
static $removes = array( |
| 172 |
'mustuse', |
| 173 |
'dropins', |
| 174 |
); |
| 175 |
if ( $this->current_group && in_array( $status, $removes, true ) ) { |
| 176 |
$show = false; |
| 177 |
} |
| 178 |
if ( true === $this->config['params']['legacyGrouping'] ) { |
| 179 |
$plugins += $this->groups; |
| 180 |
} |
| 181 |
|
| 182 |
return $show; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Render the Group navigation. |
| 187 |
*/ |
| 188 |
public function render_group_navigation() { |
| 189 |
|
| 190 |
// Use new group system. |
| 191 |
if ( true !== $this->config['params']['legacyGrouping'] && 'groups-dropdown' !== $this->config['params']['navStyle'] ) { |
| 192 |
$parts = array(); |
| 193 |
$parts[] = $this->make_all_tag(); |
| 194 |
foreach ( $this->groups as $key => $plugins ) { |
| 195 |
$parts[] = $this->make_group_tag( $key ); |
| 196 |
} |
| 197 |
|
| 198 |
$groups = implode( "", $parts ); |
| 199 |
$group_set = Utils::build_tag( 'ul', array( 'class' => array( $this->config['params']['navStyle'] ) ), $groups ); |
| 200 |
$html = Utils::build_tag( 'div', array( 'class' => self::$slug ), $group_set ); |
| 201 |
if ( 1 < count( $parts ) ) { |
| 202 |
echo wp_kses( $html, wp_kses_allowed_html( 'post' ) ); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
public function bulk_actions( $actions ) { |
| 208 |
|
| 209 |
if ( true !== $this->config['params']['legacyGrouping'] && 'groups-dropdown' === $this->config['params']['navStyle'] ) { |
| 210 |
$this->dropdown_navigation(); |
| 211 |
} |
| 212 |
|
| 213 |
return $actions; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Render a dropdown navigation. |
| 218 |
* |
| 219 |
* @param bool $echo Optional flag to echo the field or return the string. |
| 220 |
* |
| 221 |
* @return void|string |
| 222 |
*/ |
| 223 |
public function dropdown_navigation( $echo = true ) { |
| 224 |
|
| 225 |
$html = array(); |
| 226 |
$html[] = '<label for="bulk-action-selector-groups" class="screen-reader-text">' . __( 'All groups', self::$slug ) . '</label>'; |
| 227 |
$html[] = '<select id="bulk-action-selector-groups" onChange="window.location = this.value">'; |
| 228 |
$html[] = '<option value="' . esc_url( $this->get_nav_url() ) . '" ' . ( ! empty( $this->current_group ) ? 'selected="selected"' : '' ) . '>' . __( 'All groups', self::$slug ) . '</option>'; |
| 229 |
|
| 230 |
foreach ( $this->groups as $key => $plugins ) { |
| 231 |
$group = $this->config['groups'][ $key ]; |
| 232 |
$selected = ''; |
| 233 |
if ( $this->current_group === $key ) { |
| 234 |
$selected = ' selected="selected"'; |
| 235 |
} |
| 236 |
|
| 237 |
$html[] = "\t" . '<option value="' . esc_url( $this->get_nav_url( $key ) ) . '"' . $selected . '>' . $group['name'] . ' (' . count( $plugins ) . ')</option>' . "\n"; |
| 238 |
} |
| 239 |
|
| 240 |
$html[] = "</select>\n"; |
| 241 |
$html = implode( $html ); |
| 242 |
if ( false === $echo ) { |
| 243 |
return $html; |
| 244 |
} |
| 245 |
echo $html; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Catch the selected group. |
| 250 |
* |
| 251 |
* @param array $plugins List of plugins. |
| 252 |
* |
| 253 |
* @return array |
| 254 |
*/ |
| 255 |
public function catch_selected_group( $plugins ) { |
| 256 |
|
| 257 |
global $status; |
| 258 |
|
| 259 |
$this->current_status = $status; |
| 260 |
$selected_group = filter_input( INPUT_GET, self::$slug, FILTER_SANITIZE_STRING ); |
| 261 |
if ( $selected_group && isset( $this->groups[ $selected_group ] ) ) { |
| 262 |
$this->current_group = $selected_group; |
| 263 |
$plugins = $this->groups[ $selected_group ]; |
| 264 |
|
| 265 |
// Add selection path. |
| 266 |
$this->current_nav_path .= ' | ' . $this->config['groups'][ $selected_group ]['name']; |
| 267 |
} |
| 268 |
|
| 269 |
// Add our styles if we have groups; |
| 270 |
if ( ! empty( $this->groups ) ) { |
| 271 |
wp_enqueue_style( self::$slug . '-navbar' ); |
| 272 |
} |
| 273 |
|
| 274 |
return $plugins; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Get a url link for group navigation. |
| 279 |
* |
| 280 |
* @param null|string $id The group ID or null for all. |
| 281 |
* |
| 282 |
* @return string |
| 283 |
*/ |
| 284 |
protected function get_nav_url( $id = null ) { |
| 285 |
|
| 286 |
$url = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL ); |
| 287 |
if ( ! $url || false === strpos( 'plugins.php', $url ) ) { |
| 288 |
// Just in case. |
| 289 |
$url = self_admin_url( 'plugins.php' ); |
| 290 |
} |
| 291 |
if ( null === $id ) { |
| 292 |
// Null id is for the 'All groups'. |
| 293 |
return remove_query_arg( self::$slug, $url ); |
| 294 |
} |
| 295 |
|
| 296 |
// If no plugins get a link back to admin. |
| 297 |
if ( empty( $this->groups[ $id ] ) ) { |
| 298 |
return add_query_arg( 'page', self::$slug, $url ); |
| 299 |
} |
| 300 |
|
| 301 |
return add_query_arg( |
| 302 |
array( |
| 303 |
'plugin_status' => $this->current_status, |
| 304 |
self::$slug => $id, |
| 305 |
|
| 306 |
), |
| 307 |
$url |
| 308 |
); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Make a group tag. |
| 313 |
* |
| 314 |
* @param string $id The Group ID. |
| 315 |
* |
| 316 |
* @return string |
| 317 |
*/ |
| 318 |
protected function make_group_tag( $id ) { |
| 319 |
|
| 320 |
$group = $this->config['groups'][ $id ] ? $this->config['groups'][ $id ] : $this->make_preset( $id ); |
| 321 |
$total = count( $this->groups[ $id ] ); |
| 322 |
$counter = ''; |
| 323 |
|
| 324 |
if ( ! empty( $total ) ) { |
| 325 |
|
| 326 |
$counter = Utils::build_tag( 'span', array( 'class' => 'count' ), " ({$total})" ); |
| 327 |
} |
| 328 |
$li_atts = array( |
| 329 |
'class' => array( |
| 330 |
'group-link', |
| 331 |
$group['id'], |
| 332 |
), |
| 333 |
); |
| 334 |
$link_atts = array( |
| 335 |
'href' => $this->get_nav_url( $id ), |
| 336 |
); |
| 337 |
if ( $group['id'] === $this->current_group ) { |
| 338 |
$li_atts['class'][] = 'current'; |
| 339 |
$link_atts['class'] = 'current'; |
| 340 |
$link_atts['aria-current'] = 'page'; |
| 341 |
} |
| 342 |
|
| 343 |
$link = Utils::build_tag( 'a', $link_atts, $group['name'] . $counter ); |
| 344 |
|
| 345 |
return Utils::build_tag( 'li', $li_atts, $link ); |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* |
| 350 |
* @param string $id The group ID to make a preset for. |
| 351 |
*/ |
| 352 |
public function make_preset( $id ) { |
| 353 |
|
| 354 |
var_dump( $id ); |
| 355 |
$group = array( |
| 356 |
'id' => $id, |
| 357 |
'name' => $this->config['preset'], |
| 358 |
); |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Load presets into groups for built-in and filtered presets. |
| 363 |
* |
| 364 |
* @return array |
| 365 |
*/ |
| 366 |
protected function load_presets() { |
| 367 |
|
| 368 |
$groups = array( |
| 369 |
'WooCommerce' => array( 'WooCommerce' ), |
| 370 |
'Easy Digital Downloads' => array( 'Easy Digital Downloads' ), |
| 371 |
'Ninja Forms' => array( 'Ninja Forms' ), |
| 372 |
'Gravity Forms' => array( 'Gravity Forms' ), |
| 373 |
'WPForms' => array( 'WPForms' ), |
| 374 |
'E-Commerce' => array( |
| 375 |
'WooCommerce', |
| 376 |
'Easy Digital Downloads', |
| 377 |
), |
| 378 |
'Forms' => array( |
| 379 |
'Ninja Forms', |
| 380 |
'Gravity Forms', |
| 381 |
'WPForms', |
| 382 |
'Formidable Forms', |
| 383 |
), |
| 384 |
'SEO' => array( |
| 385 |
'All in One SEO', |
| 386 |
'Yoast SEO', |
| 387 |
), |
| 388 |
// @todo: Add more categories for presets. |
| 389 |
); |
| 390 |
|
| 391 |
/** |
| 392 |
* Filter the presets (legacy) |
| 393 |
* |
| 394 |
* @deprecated 2.0.0 |
| 395 |
*/ |
| 396 |
$groups = apply_filters( 'plugin-groups-get-presets', $groups ); |
| 397 |
|
| 398 |
/** |
| 399 |
* Filter preset plugin groups. |
| 400 |
* |
| 401 |
* @hook get_preset_plugin_groups |
| 402 |
* @since 2.0.0 |
| 403 |
* |
| 404 |
* @param $groups {array} The preset groups. |
| 405 |
* |
| 406 |
* @return array |
| 407 |
*/ |
| 408 |
$presets = apply_filters( 'get_preset_plugin_groups', $groups ); |
| 409 |
$this->config['preset_groups'] = array(); |
| 410 |
foreach ( $presets as $name => $keywords ) { |
| 411 |
$id = sanitize_title( $name ); |
| 412 |
$this->config['preset_groups'][ $id ] = array( |
| 413 |
'id' => $id, |
| 414 |
'name' => $name, |
| 415 |
'plugins' => array(), |
| 416 |
'keywords' => $keywords, |
| 417 |
); |
| 418 |
} |
| 419 |
|
| 420 |
return array_keys( $groups ); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Make all link tag. |
| 425 |
* |
| 426 |
* @return string |
| 427 |
*/ |
| 428 |
protected function make_all_tag() { |
| 429 |
|
| 430 |
$link_atts = array( |
| 431 |
'href' => $this->get_nav_url(), |
| 432 |
); |
| 433 |
if ( empty( $this->current_group ) ) { |
| 434 |
$link_atts['class'] = 'current'; |
| 435 |
$link_atts['aria-current'] = 'page'; |
| 436 |
} |
| 437 |
$link = Utils::build_tag( 'a', $link_atts, __( 'All Groups', self::$slug ) ); |
| 438 |
|
| 439 |
return Utils::build_tag( 'li', array( 'class' => array( 'group-link', '__allgroups' ) ), $link ); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Add new filter view |
| 444 |
* |
| 445 |
* @param array $views The list of nav tags for plugin statuses. |
| 446 |
* |
| 447 |
* @return array |
| 448 |
*/ |
| 449 |
public function add_groups( $views ) { |
| 450 |
|
| 451 |
foreach ( $views as &$tag ) { |
| 452 |
if ( preg_match( '/<a([^>]*?)>{1}/', $tag, $found ) ) { |
| 453 |
$atts = Utils::get_tag_attributes( $found[1] ); |
| 454 |
$url = add_query_arg( self::$slug, $this->current_group, $atts['href'] ); |
| 455 |
$tag = str_replace( $atts['href'], $url, $tag ); |
| 456 |
wp_parse_str( wp_parse_url( $atts['href'], PHP_URL_QUERY ), $query ); |
| 457 |
if ( isset( $query['plugin_status'] ) && $query['plugin_status'] === $this->current_status ) { |
| 458 |
$names = explode( ' ', wp_kses( $tag, array() ) ); |
| 459 |
array_pop( $names ); |
| 460 |
$this->current_nav_path .= ' | ' . implode( ' ', $names ); |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
// Legacy. |
| 466 |
if ( true === $this->config['params']['legacyGrouping'] ) { |
| 467 |
foreach ( $this->groups as $key => $plugins ) { |
| 468 |
if ( isset( $views[ $key ] ) ) { |
| 469 |
$views[ $key ] = $this->make_group_tag( $key ); |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
// @todo: Make method. |
| 475 |
if ( ! empty( $this->current_nav_path ) ) { |
| 476 |
$append = array( |
| 477 |
'name' => $this->current_nav_path, |
| 478 |
); |
| 479 |
wp_add_inline_script( |
| 480 |
'wp-util', |
| 481 |
'var appendHead = ' . wp_json_encode( |
| 482 |
$append |
| 483 |
) . '; var pluginsHead = document.getElementsByClassName(\'wp-heading-inline\' );if( pluginsHead.length ) {pluginsHead[0].innerText += appendHead.name}' |
| 484 |
); |
| 485 |
} |
| 486 |
|
| 487 |
return $views; |
| 488 |
} |
| 489 |
|
| 490 |
/** |
| 491 |
* Register REST Endpoint for saving config. |
| 492 |
*/ |
| 493 |
public function register_routes() { |
| 494 |
|
| 495 |
register_rest_route( |
| 496 |
self::$slug, |
| 497 |
'save', |
| 498 |
array( |
| 499 |
'methods' => \WP_REST_Server::CREATABLE, |
| 500 |
'args' => array(), |
| 501 |
'callback' => array( $this, 'save_config' ), |
| 502 |
'permission_callback' => function() { |
| 503 |
|
| 504 |
return current_user_can( 'manage_options' ); |
| 505 |
}, |
| 506 |
) |
| 507 |
); |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Save endpoint. |
| 512 |
* |
| 513 |
* @param \WP_REST_Request $request The request. |
| 514 |
* |
| 515 |
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response |
| 516 |
*/ |
| 517 |
public function save_config( \WP_REST_Request $request ) { |
| 518 |
|
| 519 |
$data = $request->get_json_params(); |
| 520 |
$this->config['groups'] = $data['groups']; |
| 521 |
$this->config['selectedPresets'] = $data['selectedPresets']; |
| 522 |
$this->config['params'] = $data['params']; |
| 523 |
$success = update_option( self::CONFIG_KEY, $this->config ); |
| 524 |
|
| 525 |
return rest_ensure_response( array( 'success' => $success ) ); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Autoloader by Locating and finding classes via folder structure. |
| 530 |
* |
| 531 |
* @param string $class class name to be checked and autoloaded. |
| 532 |
*/ |
| 533 |
|
| 534 |
function autoload_class( $class ) { |
| 535 |
|
| 536 |
$class_location = self::locate_class_file( $class ); |
| 537 |
if ( $class_location ) { |
| 538 |
include_once $class_location; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Locates the path to a requested class name. |
| 544 |
* |
| 545 |
* @param string $class The class name to locate. |
| 546 |
* |
| 547 |
* @return string|null |
| 548 |
*/ |
| 549 |
static public function locate_class_file( $class ) { |
| 550 |
|
| 551 |
$return = null; |
| 552 |
$parts = explode( '\\', strtolower( str_replace( '_', '-', $class ) ) ); |
| 553 |
$core = array_shift( $parts ); |
| 554 |
$self = strtolower( str_replace( '_', '-', __CLASS__ ) ); |
| 555 |
if ( $core === self::$slug ) { |
| 556 |
$name = 'class-' . strtolower( array_pop( $parts ) ) . '.php'; |
| 557 |
$parts[] = $name; |
| 558 |
$path = PLGGRP_PATH . 'classes/' . implode( '/', $parts ); |
| 559 |
if ( file_exists( $path ) ) { |
| 560 |
$return = $path; |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
return $return; |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Get the plugin version |
| 569 |
*/ |
| 570 |
public function version() { |
| 571 |
|
| 572 |
return $this->version; |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Check plugin_groups version to allow 3rd party implementations to update or upgrade. |
| 577 |
*/ |
| 578 |
protected function check_version() { |
| 579 |
|
| 580 |
$previous_version = get_option( self::VERSION_KEY, 0.0 ); |
| 581 |
$new_version = $this->version(); |
| 582 |
if ( version_compare( $previous_version, $new_version, '<' ) ) { |
| 583 |
if ( version_compare( $previous_version, '2.0.0', '<' ) ) { |
| 584 |
$data = get_option( 'plugin_groups_plugin_groups', array() ); |
| 585 |
if ( ! empty( $data ) ) { |
| 586 |
$new_config = $this->convert_legacy_groups( $data ); |
| 587 |
update_option( self::CONFIG_KEY, $new_config ); |
| 588 |
} |
| 589 |
} |
| 590 |
// Allow for updating. |
| 591 |
do_action( "_plugin_groups_version_upgrade", $previous_version, $new_version ); |
| 592 |
// Update version. |
| 593 |
update_option( self::VERSION_KEY, $new_version, true ); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Convert legacy configs. |
| 599 |
* |
| 600 |
* @param array $data Array of previous version config. |
| 601 |
* |
| 602 |
* @return array |
| 603 |
*/ |
| 604 |
protected function convert_legacy_groups( $data ) { |
| 605 |
|
| 606 |
$groups = array(); |
| 607 |
foreach ( $data['group'] as $group ) { |
| 608 |
$keywords = explode( "\n", $group['config']['keywords'] ); |
| 609 |
$new_group = array( |
| 610 |
'id' => $group['_id'], |
| 611 |
'name' => $group['config']['group_name'], |
| 612 |
'plugins' => isset( $group['config']['plugins'] ) ? $group['config']['plugins'] : array(), |
| 613 |
'keywords' => array_filter( $keywords ), |
| 614 |
); |
| 615 |
$groups[ $group['_id'] ] = $new_group; |
| 616 |
} |
| 617 |
|
| 618 |
// Set up the new config. |
| 619 |
$config = $this->get_default_config(); |
| 620 |
$config['groups'] = $groups; |
| 621 |
$config['selectedPresets'] = $data['presets']; |
| 622 |
$config['params']['legacyGrouping'] = true; |
| 623 |
$config['params']['navStyle'] = 'subsubsub'; |
| 624 |
|
| 625 |
return $config; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Initialise plugin_groups. |
| 630 |
*/ |
| 631 |
public function plugin_groups_init() { |
| 632 |
|
| 633 |
// Check version. |
| 634 |
$this->check_version(); |
| 635 |
|
| 636 |
// Load config. |
| 637 |
$this->load_config(); |
| 638 |
|
| 639 |
/** |
| 640 |
* Init the settings system |
| 641 |
* |
| 642 |
* @param Plugin_Groups ${slug} The core object. |
| 643 |
*/ |
| 644 |
do_action( 'plugin_groups_init' ); |
| 645 |
} |
| 646 |
|
| 647 |
/** |
| 648 |
* Hook into admin_init. |
| 649 |
*/ |
| 650 |
public function admin_init() { |
| 651 |
|
| 652 |
if ( ! empty( $_POST['plugin-group-config'] ) ) { |
| 653 |
$data = stripslashes( $_POST['plugin-group-config'] ); |
| 654 |
$data = json_decode( $data, true ); |
| 655 |
update_option( Plugin_Groups::CONFIG_KEY, $data ); |
| 656 |
wp_safe_redirect( admin_url( 'plugins.php?page=plugin-groups' ) ); |
| 657 |
} |
| 658 |
$asset = include PLGGRP_PATH . 'js/' . self::$slug . '.asset.php'; |
| 659 |
wp_register_script( self::$slug, PLGGRP_URL . 'js/' . self::$slug . '.js', $asset['dependencies'], $asset['version'], true ); |
| 660 |
wp_register_style( self::$slug, PLGGRP_URL . 'css/' . self::$slug . '.css', array(), $asset['version'] ); |
| 661 |
wp_register_style( self::$slug . '-navbar', PLGGRP_URL . 'css/' . self::$slug . '-navbar.css', array(), $asset['version'] ); |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Hook into the admin_menu. |
| 666 |
*/ |
| 667 |
public function admin_menu() { |
| 668 |
|
| 669 |
add_submenu_page( 'plugins.php', __( 'Plugin Groups', self::$slug ), __( 'Plugin Groups', self::$slug ), 'manage_options', 'plugin-groups', array( $this, 'render_admin' ), 50 ); |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* Add Groups to the admin bar. |
| 674 |
* |
| 675 |
* @param WP_Admin_Bar $admin_bar |
| 676 |
*/ |
| 677 |
public function admin_bar_item( $admin_bar ) { |
| 678 |
|
| 679 |
if ( ! $this->config['params']['menuGroups'] || ! current_user_can( 'activate_plugins' ) ) { |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
$groups = array( |
| 684 |
'parent' => array( |
| 685 |
'id' => self::$slug, |
| 686 |
'title' => $this->plugin_name, |
| 687 |
'href' => $this->get_nav_url( 'dashboard' ), |
| 688 |
'meta' => array( |
| 689 |
'title' => $this->plugin_name, |
| 690 |
), |
| 691 |
), |
| 692 |
); |
| 693 |
|
| 694 |
foreach ( $this->groups as $key => $plugins ) { |
| 695 |
$group = $this->config['groups'][ $key ]; |
| 696 |
$groups[ $key ] = array( |
| 697 |
'id' => $key, |
| 698 |
'parent' => self::$slug, |
| 699 |
'title' => $group['name'] . ' (' . count( $plugins ) . ')', |
| 700 |
'href' => $this->get_nav_url( $key ), |
| 701 |
); |
| 702 |
} |
| 703 |
|
| 704 |
foreach ( $groups as $option ) { |
| 705 |
$admin_bar->add_menu( $option ); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
* Enqueue assets where needed. |
| 711 |
*/ |
| 712 |
public function enqueue_assets() { |
| 713 |
|
| 714 |
$page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ); |
| 715 |
if ( $page && self::$slug === $page ) { |
| 716 |
wp_enqueue_script( self::$slug ); |
| 717 |
wp_enqueue_style( self::$slug ); |
| 718 |
|
| 719 |
$this->prep_config(); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* Prepare the config data for output to the admin UI. |
| 725 |
*/ |
| 726 |
protected function prep_config() { |
| 727 |
|
| 728 |
// Prep config data. |
| 729 |
$data = $this->config; |
| 730 |
$data['saveURL'] = rest_url( self::$slug . '/save' ); |
| 731 |
$data['legacyURL'] = add_query_arg( 'reactivate-legacy', true, $this->get_nav_url( 'dashboard' ) ); |
| 732 |
$data['restNonce'] = wp_create_nonce( 'wp_rest' ); |
| 733 |
|
| 734 |
// Add plugins. |
| 735 |
$data['plugins'] = get_plugins(); |
| 736 |
|
| 737 |
// Remove presets from groups for admin. |
| 738 |
$data['groups'] = array_diff_key( $data['groups'], $this->config['preset_groups'] ); |
| 739 |
|
| 740 |
// Remove empty groups to allow JS to init them. |
| 741 |
if ( empty( $data['groups'] ) ) { |
| 742 |
unset( $data['groups'] ); |
| 743 |
} |
| 744 |
|
| 745 |
// Add config data. |
| 746 |
wp_add_inline_script( self::$slug, 'var plgData = ' . wp_json_encode( $data ), 'before' ); |
| 747 |
} |
| 748 |
|
| 749 |
/** |
| 750 |
* Get the default config. |
| 751 |
* |
| 752 |
* @return array |
| 753 |
*/ |
| 754 |
protected function get_default_config() { |
| 755 |
|
| 756 |
return array( |
| 757 |
'groups' => array(), |
| 758 |
'selectedPresets' => array(), |
| 759 |
'params' => array( |
| 760 |
'legacyGrouping' => false, |
| 761 |
'navStyle' => 'subsubsub', |
| 762 |
'menuGroups' => false, |
| 763 |
), |
| 764 |
); |
| 765 |
} |
| 766 |
|
| 767 |
/** |
| 768 |
* Load the UI config. |
| 769 |
*/ |
| 770 |
protected function load_config() { |
| 771 |
|
| 772 |
// Load the config. |
| 773 |
$this->config = get_option( self::CONFIG_KEY, $this->get_default_config() ); |
| 774 |
$this->config['pluginName'] = $this->plugin_name; |
| 775 |
$this->config['version'] = $this->version; |
| 776 |
$this->config['slug'] = self::$slug; |
| 777 |
|
| 778 |
// Load the presets. |
| 779 |
$this->config['presets'] = $this->load_presets(); |
| 780 |
|
| 781 |
// Populate groups with plugins. |
| 782 |
array_map( array( $this, 'populate_plugins' ), $this->config['groups'] ); |
| 783 |
// register selected presets. |
| 784 |
if ( ! empty( $this->config['selectedPresets'] ) ) { |
| 785 |
array_map( array( $this, 'register_preset' ), $this->config['selectedPresets'] ); |
| 786 |
} |
| 787 |
|
| 788 |
// Populate groups with keywords. |
| 789 |
array_map( array( $this, 'populate_keywords' ), array_keys( $this->config['groups'] ) ); |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* Register a preset. |
| 794 |
* |
| 795 |
* @param string $preset The preset name to register. |
| 796 |
*/ |
| 797 |
protected function register_preset( $preset ) { |
| 798 |
|
| 799 |
$key = sanitize_title( $preset ); |
| 800 |
if ( isset( $this->config['preset_groups'][ $key ] ) ) { |
| 801 |
$this->config['groups'][ $key ] = $this->config['preset_groups'][ $key ]; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* Populate a group with selected plugins. |
| 807 |
* |
| 808 |
* @param array $group The group array. |
| 809 |
*/ |
| 810 |
protected function populate_plugins( $group ) { |
| 811 |
|
| 812 |
static $plugins; |
| 813 |
if ( ! $plugins ) { |
| 814 |
$plugins = get_plugins(); |
| 815 |
} |
| 816 |
foreach ( $group['plugins'] as $plugin ) { |
| 817 |
if ( isset( $plugins[ $plugin ] ) ) { |
| 818 |
$this->groups[ $group['id'] ][ $plugin ] = $plugins[ $plugin ]; |
| 819 |
} |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
/** |
| 824 |
* Populate a group from keywords. |
| 825 |
* |
| 826 |
* @param string $id The group ID. |
| 827 |
*/ |
| 828 |
protected function populate_keywords( $id ) { |
| 829 |
|
| 830 |
if ( ! isset( $this->config['groups'][ $id ] ) || empty( $this->config['groups'][ $id ]['keywords'] ) ) { |
| 831 |
return; |
| 832 |
} |
| 833 |
$keywords = array_map( 'strtolower', $this->config['groups'][ $id ]['keywords'] ); |
| 834 |
$plugins = get_plugins(); |
| 835 |
foreach ( $plugins as $plugin_key => $plugin_data ) { |
| 836 |
if ( isset( $this->groups[ $id ][ $plugin_key ] ) ) { |
| 837 |
continue; |
| 838 |
} |
| 839 |
$plugin_string = strtolower( implode( ' ', $plugin_data ) ); |
| 840 |
$matched = array_filter( |
| 841 |
$keywords, |
| 842 |
function( $keyword ) use ( $plugin_string ) { |
| 843 |
|
| 844 |
return false !== strpos( $plugin_string, $keyword ); |
| 845 |
} |
| 846 |
); |
| 847 |
if ( ! empty( $matched ) ) { |
| 848 |
$this->groups[ $id ][ $plugin_key ] = $plugin_data; |
| 849 |
} |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
/** |
| 854 |
* Render the admin page. |
| 855 |
*/ |
| 856 |
public function render_admin() { |
| 857 |
|
| 858 |
include PLGGRP_PATH . 'includes/main.php'; |
| 859 |
} |
| 860 |
|
| 861 |
/** |
| 862 |
* Get the instance of the class. |
| 863 |
* |
| 864 |
* @return Plugin_Groups |
| 865 |
*/ |
| 866 |
public static function get_instance() { |
| 867 |
|
| 868 |
if ( is_null( self::$instance ) ) { |
| 869 |
self::$instance = new self(); |
| 870 |
} |
| 871 |
|
| 872 |
return self::$instance; |
| 873 |
} |
| 874 |
} |
| 875 |
|