| 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 = 'plugin-groups'; |
| 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_plugin_data( PLGGRP_CORE ); |
| 99 |
$this->plugin_name = $plugin['Name']; |
| 100 |
$this->version = $plugin['Version']; |
| 101 |
|
| 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( 'network_admin_menu', array( $this, 'admin_menu' ) ); |
| 119 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 120 |
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 121 |
add_filter( 'views_plugins', array( $this, 'add_groups' ), PHP_INT_MAX ); |
| 122 |
add_filter( 'views_plugins-network', array( $this, 'add_groups' ), PHP_INT_MAX ); |
| 123 |
add_filter( 'all_plugins', array( $this, 'catch_selected_group' ) ); |
| 124 |
add_filter( 'show_advanced_plugins', array( $this, 'filter_shown_status' ), 10, 2 ); |
| 125 |
add_filter( 'site_transient_update_plugins', array( $this, 'alter_update_plugins' ) ); |
| 126 |
add_action( 'pre_current_active_plugins', array( $this, 'render_group_navigation' ) ); |
| 127 |
add_filter( 'bulk_actions-plugins', array( $this, 'bulk_actions' ) ); |
| 128 |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_item' ), 100 ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Load the plugin text domain for translation. |
| 133 |
*/ |
| 134 |
public function load_text_domain() { |
| 135 |
|
| 136 |
load_plugin_textdomain( self::$slug, false, basename( PLGGRP_PATH ) . '/languages' ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Remove plugins from the update available. |
| 141 |
* |
| 142 |
* @param object $data The data object. |
| 143 |
* |
| 144 |
* @return object |
| 145 |
*/ |
| 146 |
public function alter_update_plugins( $data ) { |
| 147 |
|
| 148 |
if ( $this->current_group ) { |
| 149 |
$keys = array_keys( $this->groups[ $this->current_group ] ); |
| 150 |
foreach ( $data->response as $key => $plugin ) { |
| 151 |
if ( ! in_array( $key, $keys, true ) ) { |
| 152 |
unset( $data->response[ $key ] ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $data; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Remove status that are not relevant. |
| 162 |
* |
| 163 |
* @param bool $show Flag to show. |
| 164 |
* @param string $status The current status |
| 165 |
* |
| 166 |
* @return bool |
| 167 |
*/ |
| 168 |
public function filter_shown_status( $show, $status ) { |
| 169 |
|
| 170 |
global $plugins; |
| 171 |
|
| 172 |
static $removes = array( |
| 173 |
'mustuse', |
| 174 |
'dropins', |
| 175 |
); |
| 176 |
if ( $this->current_group && in_array( $status, $removes, true ) ) { |
| 177 |
$show = false; |
| 178 |
} |
| 179 |
if ( true === $this->config['params']['legacyGrouping'] ) { |
| 180 |
$plugins += $this->groups; |
| 181 |
} |
| 182 |
|
| 183 |
return $show; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Render the Group navigation. |
| 188 |
*/ |
| 189 |
public function render_group_navigation() { |
| 190 |
|
| 191 |
// Use new group system. |
| 192 |
if ( true !== $this->config['params']['legacyGrouping'] && 'groups-dropdown' !== $this->config['params']['navStyle'] ) { |
| 193 |
$parts = array(); |
| 194 |
$parts[] = $this->make_all_tag(); |
| 195 |
foreach ( $this->groups as $key => $plugins ) { |
| 196 |
$parts[] = $this->make_group_tag( $key ); |
| 197 |
} |
| 198 |
|
| 199 |
$groups = implode( "", $parts ); |
| 200 |
$group_set = Utils::build_tag( 'ul', array( 'class' => array( $this->config['params']['navStyle'] ) ), $groups ); |
| 201 |
$html = Utils::build_tag( 'div', array( 'class' => self::$slug ), $group_set ); |
| 202 |
if ( 1 < count( $parts ) ) { |
| 203 |
echo wp_kses( $html, wp_kses_allowed_html( 'post' ) ); |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Add dropdown navigation on bulk actions. |
| 210 |
* |
| 211 |
* @param array $actions Unchanged actions. |
| 212 |
* |
| 213 |
* @return array |
| 214 |
*/ |
| 215 |
public function bulk_actions( $actions ) { |
| 216 |
|
| 217 |
if ( true !== $this->config['params']['legacyGrouping'] && 'groups-dropdown' === $this->config['params']['navStyle'] ) { |
| 218 |
$this->dropdown_navigation(); |
| 219 |
} |
| 220 |
|
| 221 |
return $actions; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Render a dropdown navigation. |
| 226 |
* |
| 227 |
* @param bool $echo Optional flag to echo the field or return the string. |
| 228 |
* |
| 229 |
* @return void|string |
| 230 |
*/ |
| 231 |
public function dropdown_navigation( $echo = true ) { |
| 232 |
|
| 233 |
$html = array(); |
| 234 |
$html[] = '<label for="bulk-action-selector-groups" class="screen-reader-text">' . __( 'All groups', self::$slug ) . '</label>'; |
| 235 |
$html[] = '<select id="bulk-action-selector-groups" onChange="window.location = this.value">'; |
| 236 |
$html[] = '<option value="' . esc_url( $this->get_nav_url() ) . '" ' . ( ! empty( $this->current_group ) ? 'selected="selected"' : '' ) . '>' . __( 'All groups', self::$slug ) . '</option>'; |
| 237 |
|
| 238 |
foreach ( $this->groups as $key => $plugins ) { |
| 239 |
$group = $this->config['groups'][ $key ]; |
| 240 |
$selected = ''; |
| 241 |
if ( $this->current_group === $key ) { |
| 242 |
$selected = ' selected="selected"'; |
| 243 |
} |
| 244 |
|
| 245 |
$html[] = "\t" . '<option value="' . esc_url( $this->get_nav_url( $key ) ) . '"' . $selected . '>' . $group['name'] . ' (' . count( $plugins ) . ')</option>' . "\n"; |
| 246 |
} |
| 247 |
|
| 248 |
$html[] = "</select>\n"; |
| 249 |
$html = implode( $html ); |
| 250 |
if ( false === $echo ) { |
| 251 |
return $html; |
| 252 |
} |
| 253 |
echo $html; |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Catch the selected group. |
| 258 |
* |
| 259 |
* @param array $plugins List of plugins. |
| 260 |
* |
| 261 |
* @return array |
| 262 |
*/ |
| 263 |
public function catch_selected_group( $plugins ) { |
| 264 |
|
| 265 |
global $status; |
| 266 |
|
| 267 |
$this->current_status = $status; |
| 268 |
$selected_group = filter_input( INPUT_GET, self::$slug, FILTER_SANITIZE_STRING ); |
| 269 |
if ( $selected_group && isset( $this->groups[ $selected_group ] ) ) { |
| 270 |
$this->current_group = $selected_group; |
| 271 |
$plugins = $this->groups[ $selected_group ]; |
| 272 |
|
| 273 |
// Add selection path. |
| 274 |
$this->current_nav_path .= ' | ' . $this->config['groups'][ $selected_group ]['name']; |
| 275 |
} |
| 276 |
|
| 277 |
// Add our styles if we have groups; |
| 278 |
if ( ! empty( $this->groups ) ) { |
| 279 |
wp_enqueue_style( self::$slug . '-navbar' ); |
| 280 |
} |
| 281 |
|
| 282 |
return $plugins; |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Get a url link for group navigation. |
| 287 |
* |
| 288 |
* @param null|string $id The group ID or null for all. |
| 289 |
* |
| 290 |
* @return string |
| 291 |
*/ |
| 292 |
protected function get_nav_url( $id = null ) { |
| 293 |
|
| 294 |
$url = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL ); |
| 295 |
if ( ! $url || false === strpos( 'plugins.php', $url ) ) { |
| 296 |
// Just in case. |
| 297 |
$url = self_admin_url( 'plugins.php' ); |
| 298 |
} |
| 299 |
if ( null === $id ) { |
| 300 |
// Null id is for the 'All groups'. |
| 301 |
return remove_query_arg( self::$slug, $url ); |
| 302 |
} |
| 303 |
|
| 304 |
// If no plugins get a link back to admin. |
| 305 |
if ( empty( $this->groups[ $id ] ) ) { |
| 306 |
return add_query_arg( 'page', self::$slug, $url ); |
| 307 |
} |
| 308 |
|
| 309 |
return add_query_arg( |
| 310 |
array( |
| 311 |
'plugin_status' => $this->current_status, |
| 312 |
self::$slug => $id, |
| 313 |
|
| 314 |
), |
| 315 |
$url |
| 316 |
); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Make a group tag. |
| 321 |
* |
| 322 |
* @param string $id The Group ID. |
| 323 |
* |
| 324 |
* @return string |
| 325 |
*/ |
| 326 |
protected function make_group_tag( $id ) { |
| 327 |
|
| 328 |
$group = $this->config['groups'][ $id ] ? $this->config['groups'][ $id ] : $this->make_preset( $id ); |
| 329 |
$total = count( $this->groups[ $id ] ); |
| 330 |
$counter = ''; |
| 331 |
|
| 332 |
if ( ! empty( $total ) ) { |
| 333 |
|
| 334 |
$counter = Utils::build_tag( 'span', array( 'class' => 'count' ), " ({$total})" ); |
| 335 |
} |
| 336 |
$li_atts = array( |
| 337 |
'class' => array( |
| 338 |
'group-link', |
| 339 |
$group['id'], |
| 340 |
), |
| 341 |
); |
| 342 |
$link_atts = array( |
| 343 |
'href' => $this->get_nav_url( $id ), |
| 344 |
); |
| 345 |
if ( $group['id'] === $this->current_group ) { |
| 346 |
$li_atts['class'][] = 'current'; |
| 347 |
$link_atts['class'] = 'current'; |
| 348 |
$link_atts['aria-current'] = 'page'; |
| 349 |
} |
| 350 |
|
| 351 |
$link = Utils::build_tag( 'a', $link_atts, $group['name'] . $counter ); |
| 352 |
|
| 353 |
return Utils::build_tag( 'li', $li_atts, $link ); |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* |
| 358 |
* @param string $id The group ID to make a preset for. |
| 359 |
*/ |
| 360 |
public function make_preset( $id ) { |
| 361 |
|
| 362 |
var_dump( $id ); |
| 363 |
$group = array( |
| 364 |
'id' => $id, |
| 365 |
'name' => $this->config['preset'], |
| 366 |
); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Load presets into groups for built-in and filtered presets. |
| 371 |
* |
| 372 |
* @return array |
| 373 |
*/ |
| 374 |
protected function load_presets() { |
| 375 |
|
| 376 |
$groups = array( |
| 377 |
'WooCommerce' => array( 'WooCommerce' ), |
| 378 |
'Easy Digital Downloads' => array( 'Easy Digital Downloads' ), |
| 379 |
'Ninja Forms' => array( 'Ninja Forms' ), |
| 380 |
'Gravity Forms' => array( 'Gravity Forms' ), |
| 381 |
'WPForms' => array( 'WPForms' ), |
| 382 |
'E-Commerce' => array( |
| 383 |
'WooCommerce', |
| 384 |
'Easy Digital Downloads', |
| 385 |
), |
| 386 |
'Forms' => array( |
| 387 |
'Ninja Forms', |
| 388 |
'Gravity Forms', |
| 389 |
'WPForms', |
| 390 |
'Formidable Forms', |
| 391 |
), |
| 392 |
'SEO' => array( |
| 393 |
'All in One SEO', |
| 394 |
'Yoast SEO', |
| 395 |
), |
| 396 |
// @todo: Add more categories for presets. |
| 397 |
); |
| 398 |
|
| 399 |
/** |
| 400 |
* Filter the presets (legacy) |
| 401 |
* |
| 402 |
* @deprecated 2.0.0 |
| 403 |
*/ |
| 404 |
$groups = apply_filters( 'plugin-groups-get-presets', $groups ); |
| 405 |
|
| 406 |
/** |
| 407 |
* Filter preset plugin groups. |
| 408 |
* |
| 409 |
* @hook get_preset_plugin_groups |
| 410 |
* @since 2.0.0 |
| 411 |
* |
| 412 |
* @param $groups {array} The preset groups. |
| 413 |
* |
| 414 |
* @return array |
| 415 |
*/ |
| 416 |
$presets = apply_filters( 'get_preset_plugin_groups', $groups ); |
| 417 |
$preset_groups = array(); |
| 418 |
foreach ( $presets as $name => $keywords ) { |
| 419 |
$id = sanitize_title( $name ); |
| 420 |
$preset_groups[ $id ] = array( |
| 421 |
'id' => $id, |
| 422 |
'name' => $name, |
| 423 |
'plugins' => array(), |
| 424 |
'keywords' => $keywords, |
| 425 |
); |
| 426 |
} |
| 427 |
|
| 428 |
return array( |
| 429 |
'preset_groups' => $preset_groups, |
| 430 |
'presets' => array_keys( $groups ), |
| 431 |
); |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Make all link tag. |
| 436 |
* |
| 437 |
* @return string |
| 438 |
*/ |
| 439 |
protected function make_all_tag() { |
| 440 |
|
| 441 |
$link_atts = array( |
| 442 |
'href' => $this->get_nav_url(), |
| 443 |
); |
| 444 |
if ( empty( $this->current_group ) ) { |
| 445 |
$link_atts['class'] = 'current'; |
| 446 |
$link_atts['aria-current'] = 'page'; |
| 447 |
} |
| 448 |
$link = Utils::build_tag( 'a', $link_atts, __( 'All Groups', self::$slug ) ); |
| 449 |
|
| 450 |
return Utils::build_tag( 'li', array( 'class' => array( 'group-link', '__allgroups' ) ), $link ); |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Add new filter view |
| 455 |
* |
| 456 |
* @param array $views The list of nav tags for plugin statuses. |
| 457 |
* |
| 458 |
* @return array |
| 459 |
*/ |
| 460 |
public function add_groups( $views ) { |
| 461 |
|
| 462 |
foreach ( $views as &$tag ) { |
| 463 |
if ( preg_match( '/<a([^>]*?)>{1}/', $tag, $found ) ) { |
| 464 |
$atts = Utils::get_tag_attributes( $found[1] ); |
| 465 |
$url = add_query_arg( self::$slug, $this->current_group, $atts['href'] ); |
| 466 |
$tag = str_replace( $atts['href'], $url, $tag ); |
| 467 |
wp_parse_str( wp_parse_url( $atts['href'], PHP_URL_QUERY ), $query ); |
| 468 |
if ( isset( $query['plugin_status'] ) && $query['plugin_status'] === $this->current_status ) { |
| 469 |
$names = explode( ' ', wp_kses( $tag, array() ) ); |
| 470 |
array_pop( $names ); |
| 471 |
$this->current_nav_path .= ' | ' . implode( ' ', $names ); |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
// Legacy. |
| 477 |
if ( true === $this->config['params']['legacyGrouping'] ) { |
| 478 |
foreach ( $this->groups as $key => $plugins ) { |
| 479 |
$views[ $key ] = $this->make_group_tag( $key ); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
// @todo: Make method. |
| 484 |
if ( ! empty( $this->current_nav_path ) ) { |
| 485 |
$append = array( |
| 486 |
'name' => $this->current_nav_path, |
| 487 |
); |
| 488 |
wp_add_inline_script( |
| 489 |
'wp-util', |
| 490 |
'var appendHead = ' . wp_json_encode( |
| 491 |
$append |
| 492 |
) . '; var pluginsHead = document.getElementsByClassName(\'wp-heading-inline\' );if( pluginsHead.length ) {pluginsHead[0].innerText += appendHead.name}' |
| 493 |
); |
| 494 |
} |
| 495 |
|
| 496 |
return $views; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Register REST Endpoint for saving config. |
| 501 |
*/ |
| 502 |
public function register_routes() { |
| 503 |
|
| 504 |
register_rest_route( |
| 505 |
self::$slug, |
| 506 |
'save', |
| 507 |
array( |
| 508 |
'methods' => \WP_REST_Server::CREATABLE, |
| 509 |
'args' => array(), |
| 510 |
'callback' => array( $this, 'rest_save_config' ), |
| 511 |
'permission_callback' => function( \WP_REST_Request $request ) { |
| 512 |
|
| 513 |
if ( is_multisite() ) { |
| 514 |
$data = $request->get_json_params(); |
| 515 |
$can = current_user_can_for_blog( $data['siteID'], 'manage_options' ); |
| 516 |
} else { |
| 517 |
$can = current_user_can( 'manage_options' ); |
| 518 |
} |
| 519 |
|
| 520 |
return $can; |
| 521 |
}, |
| 522 |
) |
| 523 |
); |
| 524 |
|
| 525 |
register_rest_route( |
| 526 |
self::$slug, |
| 527 |
'load', |
| 528 |
array( |
| 529 |
'methods' => \WP_REST_Server::READABLE, |
| 530 |
'args' => array(), |
| 531 |
'callback' => array( $this, 'rest_load_config' ), |
| 532 |
'permission_callback' => function( \WP_REST_Request $request ) { |
| 533 |
|
| 534 |
$id = $request->get_param( 'siteID' ); |
| 535 |
|
| 536 |
return current_user_can_for_blog( $id, 'manage_options' ); |
| 537 |
}, |
| 538 |
) |
| 539 |
); |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Load a config for a specific site. |
| 544 |
* |
| 545 |
* @param \WP_REST_Request $request |
| 546 |
* |
| 547 |
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response |
| 548 |
*/ |
| 549 |
public function rest_load_config( \WP_REST_Request $request ) { |
| 550 |
|
| 551 |
$id = $request->get_param( 'siteID' ); |
| 552 |
$json = $this->build_config_object( $id ); |
| 553 |
|
| 554 |
return rest_ensure_response( json_decode( $json ) ); |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Save endpoint. |
| 559 |
* |
| 560 |
* @param \WP_REST_Request $request The request. |
| 561 |
* |
| 562 |
* @return \WP_Error|\WP_HTTP_Response|\WP_REST_Response |
| 563 |
*/ |
| 564 |
public function rest_save_config( \WP_REST_Request $request ) { |
| 565 |
|
| 566 |
$data = $request->get_json_params(); |
| 567 |
$this->config = wp_parse_args( $data, $this->config ); |
| 568 |
if ( is_multisite() ) { |
| 569 |
$site_id = get_current_blog_id(); |
| 570 |
if ( ! empty( $data['siteID'] ) ) { |
| 571 |
$site_id = $data['siteID']; |
| 572 |
unset( $data['siteID'] ); |
| 573 |
} |
| 574 |
if ( ! empty( $data['sitesEnabled'] ) ) { |
| 575 |
// Ensure we have the same types. |
| 576 |
$data['sitesEnabled'] = array_map( 'intval', $data['sitesEnabled'] ); |
| 577 |
} |
| 578 |
$success = update_network_option( $site_id, self::CONFIG_KEY, $this->config ); |
| 579 |
} else { |
| 580 |
$success = update_option( self::CONFIG_KEY, $this->config ); |
| 581 |
} |
| 582 |
|
| 583 |
return rest_ensure_response( array( 'success' => $success ) ); |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
* Autoloader by Locating and finding classes via folder structure. |
| 588 |
* |
| 589 |
* @param string $class class name to be checked and autoloaded. |
| 590 |
*/ |
| 591 |
|
| 592 |
function autoload_class( $class ) { |
| 593 |
|
| 594 |
$class_location = self::locate_class_file( $class ); |
| 595 |
if ( $class_location ) { |
| 596 |
include_once $class_location; |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
/** |
| 601 |
* Locates the path to a requested class name. |
| 602 |
* |
| 603 |
* @param string $class The class name to locate. |
| 604 |
* |
| 605 |
* @return string|null |
| 606 |
*/ |
| 607 |
static public function locate_class_file( $class ) { |
| 608 |
|
| 609 |
$return = null; |
| 610 |
$parts = explode( '\\', strtolower( str_replace( '_', '-', $class ) ) ); |
| 611 |
$core = array_shift( $parts ); |
| 612 |
$self = strtolower( str_replace( '_', '-', __CLASS__ ) ); |
| 613 |
if ( $core === self::$slug ) { |
| 614 |
$name = 'class-' . strtolower( array_pop( $parts ) ) . '.php'; |
| 615 |
$parts[] = $name; |
| 616 |
$path = PLGGRP_PATH . 'classes/' . implode( '/', $parts ); |
| 617 |
if ( file_exists( $path ) ) { |
| 618 |
$return = $path; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
return $return; |
| 623 |
} |
| 624 |
|
| 625 |
/** |
| 626 |
* Get the plugin version |
| 627 |
*/ |
| 628 |
public function version() { |
| 629 |
|
| 630 |
return $this->version; |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* Check plugin_groups version to allow 3rd party implementations to update or upgrade. |
| 635 |
*/ |
| 636 |
protected function check_version() { |
| 637 |
|
| 638 |
$previous_version = get_option( self::VERSION_KEY, 0.0 ); |
| 639 |
$new_version = $this->version(); |
| 640 |
if ( version_compare( $previous_version, $new_version, '<' ) ) { |
| 641 |
if ( version_compare( $previous_version, '2.0.0', '<' ) ) { |
| 642 |
$data = get_option( 'plugin_groups_plugin_groups', array() ); |
| 643 |
if ( ! empty( $data ) ) { |
| 644 |
$new_config = $this->convert_legacy_groups( $data ); |
| 645 |
update_option( self::CONFIG_KEY, $new_config ); |
| 646 |
} |
| 647 |
} |
| 648 |
// Allow for updating. |
| 649 |
do_action( "_plugin_groups_version_upgrade", $previous_version, $new_version ); |
| 650 |
// Update version. |
| 651 |
update_option( self::VERSION_KEY, $new_version, true ); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Convert legacy configs. |
| 657 |
* |
| 658 |
* @param array $data Array of previous version config. |
| 659 |
* |
| 660 |
* @return array |
| 661 |
*/ |
| 662 |
protected function convert_legacy_groups( $data ) { |
| 663 |
|
| 664 |
$groups = array(); |
| 665 |
foreach ( $data['group'] as $group ) { |
| 666 |
$keywords = explode( "\n", $group['config']['keywords'] ); |
| 667 |
$new_group = array( |
| 668 |
'id' => $group['_id'], |
| 669 |
'name' => $group['config']['group_name'], |
| 670 |
'plugins' => isset( $group['config']['plugins'] ) ? $group['config']['plugins'] : array(), |
| 671 |
'keywords' => array_filter( $keywords ), |
| 672 |
); |
| 673 |
$groups[ $group['_id'] ] = $new_group; |
| 674 |
} |
| 675 |
|
| 676 |
// Set up the new config. |
| 677 |
$config = $this->get_default_config(); |
| 678 |
$config['groups'] = $groups; |
| 679 |
$config['selectedPresets'] = isset( $data['presets'] ) ? $data['presets'] : array(); |
| 680 |
$config['params']['legacyGrouping'] = true; |
| 681 |
$config['params']['navStyle'] = 'subsubsub'; |
| 682 |
|
| 683 |
return $config; |
| 684 |
} |
| 685 |
|
| 686 |
/** |
| 687 |
* Initialise plugin_groups. |
| 688 |
*/ |
| 689 |
public function plugin_groups_init() { |
| 690 |
|
| 691 |
// Check version. |
| 692 |
$this->check_version(); |
| 693 |
|
| 694 |
// Load config. |
| 695 |
$this->set_config(); |
| 696 |
|
| 697 |
/** |
| 698 |
* Init the settings system |
| 699 |
* |
| 700 |
* @param Plugin_Groups ${slug} The core object. |
| 701 |
*/ |
| 702 |
do_action( 'plugin_groups_init' ); |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* Hook into admin_init. |
| 707 |
*/ |
| 708 |
public function admin_init() { |
| 709 |
|
| 710 |
if ( ! empty( $_POST['plugin-group-config'] ) ) { |
| 711 |
$data = stripslashes( $_POST['plugin-group-config'] ); |
| 712 |
$data = json_decode( $data, true ); |
| 713 |
update_option( Plugin_Groups::CONFIG_KEY, $data ); |
| 714 |
wp_safe_redirect( admin_url( 'plugins.php?page=plugin-groups' ) ); |
| 715 |
} |
| 716 |
$asset = include PLGGRP_PATH . 'js/' . self::$slug . '.asset.php'; |
| 717 |
wp_register_script( self::$slug, PLGGRP_URL . 'js/' . self::$slug . '.js', $asset['dependencies'], $asset['version'], true ); |
| 718 |
wp_register_style( self::$slug, PLGGRP_URL . 'css/' . self::$slug . '.css', array(), $asset['version'] ); |
| 719 |
wp_register_style( self::$slug . '-navbar', PLGGRP_URL . 'css/' . self::$slug . '-navbar.css', array(), $asset['version'] ); |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Hook into the admin_menu. |
| 724 |
*/ |
| 725 |
public function admin_menu() { |
| 726 |
|
| 727 |
if ( ! $this->network_active() || $this->site_enabled() || is_main_site() ) { |
| 728 |
add_submenu_page( 'plugins.php', __( 'Plugin Groups', self::$slug ), __( 'Plugin Groups', self::$slug ), 'manage_options', 'plugin-groups', array( $this, 'render_admin' ), 50 ); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Check if the plugin is network activated. |
| 734 |
* |
| 735 |
* @return bool |
| 736 |
*/ |
| 737 |
protected function network_active() { |
| 738 |
|
| 739 |
return is_plugin_active_for_network( PLGGRP_SLUG ); |
| 740 |
} |
| 741 |
|
| 742 |
/** |
| 743 |
* Check to see if the site is allowed to use this. |
| 744 |
* |
| 745 |
* @return bool |
| 746 |
*/ |
| 747 |
protected function site_enabled() { |
| 748 |
|
| 749 |
$site_id = get_current_blog_id(); |
| 750 |
$main_config = get_network_option( get_main_site_id(), self::CONFIG_KEY, $this->get_default_config() ); |
| 751 |
|
| 752 |
return in_array( $site_id, $main_config['sitesEnabled'], true ); |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
* Add Groups to the admin bar. |
| 757 |
* |
| 758 |
* @param WP_Admin_Bar $admin_bar |
| 759 |
*/ |
| 760 |
public function admin_bar_item( $admin_bar ) { |
| 761 |
|
| 762 |
if ( ! $this->config['params']['menuGroups'] || ! current_user_can( 'activate_plugins' ) ) { |
| 763 |
return; |
| 764 |
} |
| 765 |
|
| 766 |
$groups = array( |
| 767 |
'parent' => array( |
| 768 |
'id' => self::$slug, |
| 769 |
'title' => $this->plugin_name, |
| 770 |
'href' => $this->get_nav_url( 'dashboard' ), |
| 771 |
'meta' => array( |
| 772 |
'title' => $this->plugin_name, |
| 773 |
), |
| 774 |
), |
| 775 |
); |
| 776 |
|
| 777 |
foreach ( $this->groups as $key => $plugins ) { |
| 778 |
$group = $this->config['groups'][ $key ]; |
| 779 |
$groups[ $key ] = array( |
| 780 |
'id' => $key, |
| 781 |
'parent' => self::$slug, |
| 782 |
'title' => $group['name'] . ' (' . count( $plugins ) . ')', |
| 783 |
'href' => $this->get_nav_url( $key ), |
| 784 |
); |
| 785 |
} |
| 786 |
|
| 787 |
foreach ( $groups as $option ) { |
| 788 |
$admin_bar->add_menu( $option ); |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
/** |
| 793 |
* Enqueue assets where needed. |
| 794 |
*/ |
| 795 |
public function enqueue_assets() { |
| 796 |
|
| 797 |
$page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING ); |
| 798 |
if ( $page && self::$slug === $page ) { |
| 799 |
wp_enqueue_script( self::$slug ); |
| 800 |
wp_enqueue_style( self::$slug ); |
| 801 |
|
| 802 |
$this->prep_config(); |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
/** |
| 807 |
* Prepare the config data for output to the admin UI. |
| 808 |
*/ |
| 809 |
protected function prep_config() { |
| 810 |
|
| 811 |
$data = $this->build_config_object(); |
| 812 |
|
| 813 |
// Add config data. |
| 814 |
wp_add_inline_script( self::$slug, 'var plgData = ' . $data, 'before' ); |
| 815 |
} |
| 816 |
|
| 817 |
/** |
| 818 |
* Build the json config object. |
| 819 |
* |
| 820 |
* @param int|null $site_id The site to get config for, ir null for current. |
| 821 |
* |
| 822 |
* @return string |
| 823 |
*/ |
| 824 |
protected function build_config_object( $site_id = null ) { |
| 825 |
|
| 826 |
if ( null === $site_id ) { |
| 827 |
$data = $this->config; |
| 828 |
} else { |
| 829 |
$data = $this->load_config( $site_id ); |
| 830 |
} |
| 831 |
// Prep config data. |
| 832 |
$data['saveURL'] = rest_url( self::$slug . '/save' ); |
| 833 |
$data['legacyURL'] = add_query_arg( 'reactivate-legacy', true, $this->get_nav_url( 'dashboard' ) ); |
| 834 |
$data['restNonce'] = wp_create_nonce( 'wp_rest' ); |
| 835 |
// Multisite. |
| 836 |
if ( $this->network_active() && ( is_network_admin() || defined( 'REST_REQUEST' ) && true === REST_REQUEST ) ) { |
| 837 |
$data['loadURL'] = rest_url( self::$slug . '/load' ); |
| 838 |
$data['sites'] = get_sites(); |
| 839 |
$data['mainSite'] = get_main_site_id(); |
| 840 |
if ( ! in_array( $data['mainSite'], $data['sitesEnabled'], true ) ) { |
| 841 |
$data['sitesEnabled'][] = get_main_site_id(); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
// Add plugins. |
| 846 |
$data['plugins'] = get_plugins(); |
| 847 |
|
| 848 |
// Remove presets from groups for admin. |
| 849 |
$data['groups'] = array_diff_key( $data['groups'], $data['preset_groups'] ); |
| 850 |
|
| 851 |
return wp_json_encode( $data ); |
| 852 |
} |
| 853 |
|
| 854 |
/** |
| 855 |
* Get the default config. |
| 856 |
* |
| 857 |
* @return array |
| 858 |
*/ |
| 859 |
protected function get_default_config() { |
| 860 |
|
| 861 |
return array( |
| 862 |
'groups' => array(), |
| 863 |
'selectedPresets' => array(), |
| 864 |
'params' => array( |
| 865 |
'legacyGrouping' => false, |
| 866 |
'navStyle' => 'subsubsub', |
| 867 |
'menuGroups' => false, |
| 868 |
), |
| 869 |
|
| 870 |
'sitesEnabled' => array(), // Used for multisite. |
| 871 |
); |
| 872 |
} |
| 873 |
|
| 874 |
/** |
| 875 |
* Load the UI config. |
| 876 |
* |
| 877 |
* @param int|null $site_id The site ID to load. Null for current site. |
| 878 |
* |
| 879 |
* @return array; |
| 880 |
*/ |
| 881 |
protected function load_config( $site_id = null ) { |
| 882 |
|
| 883 |
// Load the config. |
| 884 |
if ( is_multisite() ) { |
| 885 |
if ( ! $site_id ) { |
| 886 |
$site_id = get_current_blog_id(); |
| 887 |
} |
| 888 |
$config = get_network_option( $site_id, self::CONFIG_KEY, $this->get_default_config() ); |
| 889 |
$config['siteID'] = (int) $site_id; |
| 890 |
} else { |
| 891 |
$config = get_option( self::CONFIG_KEY, $this->get_default_config() ); |
| 892 |
} |
| 893 |
$config['pluginName'] = $this->plugin_name; |
| 894 |
$config['version'] = $this->version; |
| 895 |
$config['slug'] = self::$slug; |
| 896 |
|
| 897 |
// Load the presets. |
| 898 |
$config += $this->load_presets(); |
| 899 |
|
| 900 |
return $config; |
| 901 |
} |
| 902 |
|
| 903 |
/** |
| 904 |
* Set the config. |
| 905 |
* |
| 906 |
* @param int|null $site_id The site ID to load. Null for current site. |
| 907 |
*/ |
| 908 |
protected function set_config( $site_id = null ) { |
| 909 |
|
| 910 |
$this->config = $this->load_config( $site_id ); |
| 911 |
// Populate groups with plugins. |
| 912 |
array_map( array( $this, 'populate_plugins' ), $this->config['groups'] ); |
| 913 |
// register selected presets. |
| 914 |
|
| 915 |
if ( ! empty( $this->config['selectedPresets'] ) ) { |
| 916 |
array_map( array( $this, 'register_preset' ), $this->config['selectedPresets'] ); |
| 917 |
} |
| 918 |
|
| 919 |
// Populate groups with keywords. |
| 920 |
array_map( array( $this, 'populate_keywords' ), array_keys( $this->config['groups'] ) ); |
| 921 |
} |
| 922 |
|
| 923 |
/** |
| 924 |
* Register a preset. |
| 925 |
* |
| 926 |
* @param string $preset The preset name to register. |
| 927 |
*/ |
| 928 |
protected function register_preset( $preset ) { |
| 929 |
|
| 930 |
$key = sanitize_title( $preset ); |
| 931 |
if ( isset( $this->config['preset_groups'][ $key ] ) ) { |
| 932 |
$this->config['groups'][ $key ] = $this->config['preset_groups'][ $key ]; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
/** |
| 937 |
* Populate a group with selected plugins. |
| 938 |
* |
| 939 |
* @param array $group The group array. |
| 940 |
*/ |
| 941 |
protected function populate_plugins( $group ) { |
| 942 |
|
| 943 |
static $plugins; |
| 944 |
if ( ! $plugins ) { |
| 945 |
$plugins = get_plugins(); |
| 946 |
} |
| 947 |
foreach ( $group['plugins'] as $plugin ) { |
| 948 |
if ( isset( $plugins[ $plugin ] ) ) { |
| 949 |
$this->groups[ $group['id'] ][ $plugin ] = $plugins[ $plugin ]; |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
/** |
| 955 |
* Populate a group from keywords. |
| 956 |
* |
| 957 |
* @param string $id The group ID. |
| 958 |
*/ |
| 959 |
protected function populate_keywords( $id ) { |
| 960 |
|
| 961 |
if ( ! isset( $this->config['groups'][ $id ] ) || empty( $this->config['groups'][ $id ]['keywords'] ) ) { |
| 962 |
return; |
| 963 |
} |
| 964 |
$keywords = array_map( 'strtolower', $this->config['groups'][ $id ]['keywords'] ); |
| 965 |
$plugins = get_plugins(); |
| 966 |
foreach ( $plugins as $plugin_key => $plugin_data ) { |
| 967 |
if ( isset( $this->groups[ $id ][ $plugin_key ] ) ) { |
| 968 |
continue; |
| 969 |
} |
| 970 |
$plugin_string = strtolower( implode( ' ', $plugin_data ) ); |
| 971 |
$matched = array_filter( |
| 972 |
$keywords, |
| 973 |
function( $keyword ) use ( $plugin_string ) { |
| 974 |
|
| 975 |
return false !== strpos( $plugin_string, $keyword ); |
| 976 |
} |
| 977 |
); |
| 978 |
if ( ! empty( $matched ) ) { |
| 979 |
$this->groups[ $id ][ $plugin_key ] = $plugin_data; |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
/** |
| 985 |
* Render the admin page. |
| 986 |
*/ |
| 987 |
public function render_admin() { |
| 988 |
|
| 989 |
include PLGGRP_PATH . 'includes/main.php'; |
| 990 |
} |
| 991 |
|
| 992 |
/** |
| 993 |
* Get the instance of the class. |
| 994 |
* |
| 995 |
* @return Plugin_Groups |
| 996 |
*/ |
| 997 |
public static function get_instance() { |
| 998 |
|
| 999 |
if ( is_null( self::$instance ) ) { |
| 1000 |
self::$instance = new self(); |
| 1001 |
} |
| 1002 |
|
| 1003 |
return self::$instance; |
| 1004 |
} |
| 1005 |
} |
| 1006 |
|