These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Plugin installation and activation for WordPress themes. |
||
| 4 | * |
||
| 5 | * Please note that this is a drop-in library for a theme or plugin. |
||
| 6 | * The authors of this library (Thomas, Gary and Juliette) are NOT responsible |
||
| 7 | * for the support of your plugin or theme. Please contact the plugin |
||
| 8 | * or theme author for support. |
||
| 9 | * |
||
| 10 | * @package TGM-Plugin-Activation |
||
| 11 | * @version 2.6.1 |
||
| 12 | * @link http://tgmpluginactivation.com/ |
||
| 13 | * @author Thomas Griffin, Gary Jones, Juliette Reinders Folmer |
||
| 14 | * @copyright Copyright (c) 2011, Thomas Griffin |
||
| 15 | * @license GPL-2.0+ |
||
| 16 | */ |
||
| 17 | |||
| 18 | /* |
||
| 19 | Copyright 2011 Thomas Griffin (thomasgriffinmedia.com) |
||
| 20 | |||
| 21 | This program is free software; you can redistribute it and/or modify |
||
| 22 | it under the terms of the GNU General Public License, version 2, as |
||
| 23 | published by the Free Software Foundation. |
||
| 24 | |||
| 25 | This program is distributed in the hope that it will be useful, |
||
| 26 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 27 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 28 | GNU General Public License for more details. |
||
| 29 | |||
| 30 | You should have received a copy of the GNU General Public License |
||
| 31 | along with this program; if not, write to the Free Software |
||
| 32 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
||
| 33 | */ |
||
| 34 | |||
| 35 | if ( ! class_exists( 'TGM_Plugin_Activation' ) ) { |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Automatic plugin installation and activation library. |
||
| 39 | * |
||
| 40 | * Creates a way to automatically install and activate plugins from within themes. |
||
| 41 | * The plugins can be either bundled, downloaded from the WordPress |
||
| 42 | * Plugin Repository or downloaded from another external source. |
||
| 43 | * |
||
| 44 | * @since 1.0.0 |
||
| 45 | * |
||
| 46 | * @package TGM-Plugin-Activation |
||
| 47 | * @author Thomas Griffin |
||
| 48 | * @author Gary Jones |
||
| 49 | */ |
||
| 50 | class TGM_Plugin_Activation { |
||
| 51 | /** |
||
| 52 | * TGMPA version number. |
||
| 53 | * |
||
| 54 | * @since 2.5.0 |
||
| 55 | * |
||
| 56 | * @const string Version number. |
||
| 57 | */ |
||
| 58 | const TGMPA_VERSION = '2.6.1'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Regular expression to test if a URL is a WP plugin repo URL. |
||
| 62 | * |
||
| 63 | * @const string Regex. |
||
| 64 | * |
||
| 65 | * @since 2.5.0 |
||
| 66 | */ |
||
| 67 | const WP_REPO_REGEX = '|^http[s]?://wordpress\.org/(?:extend/)?plugins/|'; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Arbitrary regular expression to test if a string starts with a URL. |
||
| 71 | * |
||
| 72 | * @const string Regex. |
||
| 73 | * |
||
| 74 | * @since 2.5.0 |
||
| 75 | */ |
||
| 76 | const IS_URL_REGEX = '|^http[s]?://|'; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Holds a copy of itself, so it can be referenced by the class name. |
||
| 80 | * |
||
| 81 | * @since 1.0.0 |
||
| 82 | * |
||
| 83 | * @var TGM_Plugin_Activation |
||
| 84 | */ |
||
| 85 | public static $instance; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Holds arrays of plugin details. |
||
| 89 | * |
||
| 90 | * @since 1.0.0 |
||
| 91 | * @since 2.5.0 the array has the plugin slug as an associative key. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | public $plugins = array(); |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Holds arrays of plugin names to use to sort the plugins array. |
||
| 99 | * |
||
| 100 | * @since 2.5.0 |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $sort_order = array(); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Whether any plugins have the 'force_activation' setting set to true. |
||
| 108 | * |
||
| 109 | * @since 2.5.0 |
||
| 110 | * |
||
| 111 | * @var bool |
||
| 112 | */ |
||
| 113 | protected $has_forced_activation = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Whether any plugins have the 'force_deactivation' setting set to true. |
||
| 117 | * |
||
| 118 | * @since 2.5.0 |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | protected $has_forced_deactivation = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Name of the unique ID to hash notices. |
||
| 126 | * |
||
| 127 | * @since 2.4.0 |
||
| 128 | * |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | public $id = 'tgmpa'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Name of the query-string argument for the admin page. |
||
| 135 | * |
||
| 136 | * @since 1.0.0 |
||
| 137 | * |
||
| 138 | * @var string |
||
| 139 | */ |
||
| 140 | protected $menu = 'tgmpa-install-plugins'; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Parent menu file slug. |
||
| 144 | * |
||
| 145 | * @since 2.5.0 |
||
| 146 | * |
||
| 147 | * @var string |
||
| 148 | */ |
||
| 149 | public $parent_slug = 'themes.php'; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Capability needed to view the plugin installation menu item. |
||
| 153 | * |
||
| 154 | * @since 2.5.0 |
||
| 155 | * |
||
| 156 | * @var string |
||
| 157 | */ |
||
| 158 | public $capability = 'edit_theme_options'; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Default absolute path to folder containing bundled plugin zip files. |
||
| 162 | * |
||
| 163 | * @since 2.0.0 |
||
| 164 | * |
||
| 165 | * @var string Absolute path prefix to zip file location for bundled plugins. Default is empty string. |
||
| 166 | */ |
||
| 167 | public $default_path = ''; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Flag to show admin notices or not. |
||
| 171 | * |
||
| 172 | * @since 2.1.0 |
||
| 173 | * |
||
| 174 | * @var boolean |
||
| 175 | */ |
||
| 176 | public $has_notices = true; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Flag to determine if the user can dismiss the notice nag. |
||
| 180 | * |
||
| 181 | * @since 2.4.0 |
||
| 182 | * |
||
| 183 | * @var boolean |
||
| 184 | */ |
||
| 185 | public $dismissable = true; |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Message to be output above nag notice if dismissable is false. |
||
| 189 | * |
||
| 190 | * @since 2.4.0 |
||
| 191 | * |
||
| 192 | * @var string |
||
| 193 | */ |
||
| 194 | public $dismiss_msg = ''; |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Flag to set automatic activation of plugins. Off by default. |
||
| 198 | * |
||
| 199 | * @since 2.2.0 |
||
| 200 | * |
||
| 201 | * @var boolean |
||
| 202 | */ |
||
| 203 | public $is_automatic = false; |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Optional message to display before the plugins table. |
||
| 207 | * |
||
| 208 | * @since 2.2.0 |
||
| 209 | * |
||
| 210 | * @var string Message filtered by wp_kses_post(). Default is empty string. |
||
| 211 | */ |
||
| 212 | public $message = ''; |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Holds configurable array of strings. |
||
| 216 | * |
||
| 217 | * Default values are added in the constructor. |
||
| 218 | * |
||
| 219 | * @since 2.0.0 |
||
| 220 | * |
||
| 221 | * @var array |
||
| 222 | */ |
||
| 223 | public $strings = array(); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Holds the version of WordPress. |
||
| 227 | * |
||
| 228 | * @since 2.4.0 |
||
| 229 | * |
||
| 230 | * @var int |
||
| 231 | */ |
||
| 232 | public $wp_version; |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Holds the hook name for the admin page. |
||
| 236 | * |
||
| 237 | * @since 2.5.0 |
||
| 238 | * |
||
| 239 | * @var string |
||
| 240 | */ |
||
| 241 | public $page_hook; |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Adds a reference of this object to $instance, populates default strings, |
||
| 245 | * does the tgmpa_init action hook, and hooks in the interactions to init. |
||
| 246 | * |
||
| 247 | * {@internal This method should be `protected`, but as too many TGMPA implementations |
||
| 248 | * haven't upgraded beyond v2.3.6 yet, this gives backward compatibility issues. |
||
| 249 | * Reverted back to public for the time being.}} |
||
| 250 | * |
||
| 251 | * @since 1.0.0 |
||
| 252 | * |
||
| 253 | * @see TGM_Plugin_Activation::init() |
||
| 254 | */ |
||
| 255 | public function __construct() { |
||
| 256 | // Set the current WordPress version. |
||
| 257 | $this->wp_version = $GLOBALS['wp_version']; |
||
| 258 | |||
| 259 | // Announce that the class is ready, and pass the object (for advanced use). |
||
| 260 | do_action_ref_array( 'tgmpa_init', array( $this ) ); |
||
| 261 | |||
| 262 | /* |
||
| 263 | * Load our text domain and allow for overloading the fall-back file. |
||
| 264 | * |
||
| 265 | * {@internal IMPORTANT! If this code changes, review the regex in the custom TGMPA |
||
| 266 | * generator on the website.}} |
||
| 267 | */ |
||
| 268 | add_action( 'init', array( $this, 'load_textdomain' ), 5 ); |
||
| 269 | add_filter( 'load_textdomain_mofile', array( $this, 'overload_textdomain_mofile' ), 10, 2 ); |
||
| 270 | |||
| 271 | // When the rest of WP has loaded, kick-start the rest of the class. |
||
| 272 | add_action( 'init', array( $this, 'init' ) ); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Magic method to (not) set protected properties from outside of this class. |
||
| 277 | * |
||
| 278 | * {@internal hackedihack... There is a serious bug in v2.3.2 - 2.3.6 where the `menu` property |
||
| 279 | * is being assigned rather than tested in a conditional, effectively rendering it useless. |
||
| 280 | * This 'hack' prevents this from happening.}} |
||
| 281 | * |
||
| 282 | * @see https://github.com/TGMPA/TGM-Plugin-Activation/blob/2.3.6/tgm-plugin-activation/class-tgm-plugin-activation.php#L1593 |
||
| 283 | * |
||
| 284 | * @since 2.5.2 |
||
| 285 | * |
||
| 286 | * @param string $name Name of an inaccessible property. |
||
| 287 | * @param mixed $value Value to assign to the property. |
||
| 288 | * @return void Silently fail to set the property when this is tried from outside of this class context. |
||
| 289 | * (Inside this class context, the __set() method if not used as there is direct access.) |
||
| 290 | */ |
||
| 291 | public function __set( $name, $value ) { |
||
| 292 | return; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Magic method to get the value of a protected property outside of this class context. |
||
| 297 | * |
||
| 298 | * @since 2.5.2 |
||
| 299 | * |
||
| 300 | * @param string $name Name of an inaccessible property. |
||
| 301 | * @return mixed The property value. |
||
| 302 | */ |
||
| 303 | public function __get( $name ) { |
||
| 304 | return $this->{$name}; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Initialise the interactions between this class and WordPress. |
||
| 309 | * |
||
| 310 | * Hooks in three new methods for the class: admin_menu, notices and styles. |
||
| 311 | * |
||
| 312 | * @since 2.0.0 |
||
| 313 | * |
||
| 314 | * @see TGM_Plugin_Activation::admin_menu() |
||
| 315 | * @see TGM_Plugin_Activation::notices() |
||
| 316 | * @see TGM_Plugin_Activation::styles() |
||
| 317 | */ |
||
| 318 | public function init() { |
||
| 319 | /** |
||
| 320 | * By default TGMPA only loads on the WP back-end and not in an Ajax call. Using this filter |
||
| 321 | * you can overrule that behaviour. |
||
| 322 | * |
||
| 323 | * @since 2.5.0 |
||
| 324 | * |
||
| 325 | * @param bool $load Whether or not TGMPA should load. |
||
| 326 | * Defaults to the return of `is_admin() && ! defined( 'DOING_AJAX' )`. |
||
| 327 | */ |
||
| 328 | if ( true !== apply_filters( 'tgmpa_load', ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) ) { |
||
| 329 | return; |
||
| 330 | } |
||
| 331 | |||
| 332 | // Load class strings. |
||
| 333 | $this->strings = array( |
||
| 334 | 'page_title' => __( 'Install Required Plugins', 'tgmpa' ), |
||
| 335 | 'menu_title' => __( 'Install Plugins', 'tgmpa' ), |
||
| 336 | /* translators: %s: plugin name. */ |
||
| 337 | 'installing' => __( 'Installing Plugin: %s', 'tgmpa' ), |
||
| 338 | /* translators: %s: plugin name. */ |
||
| 339 | 'updating' => __( 'Updating Plugin: %s', 'tgmpa' ), |
||
| 340 | 'oops' => __( 'Something went wrong with the plugin API.', 'tgmpa' ), |
||
| 341 | 'notice_can_install_required' => _n_noop( |
||
| 342 | /* translators: 1: plugin name(s). */ |
||
| 343 | 'This theme requires the following plugin: %1$s.', |
||
| 344 | 'This theme requires the following plugins: %1$s.', |
||
| 345 | 'tgmpa' |
||
| 346 | ), |
||
| 347 | 'notice_can_install_recommended' => _n_noop( |
||
| 348 | /* translators: 1: plugin name(s). */ |
||
| 349 | 'This theme recommends the following plugin: %1$s.', |
||
| 350 | 'This theme recommends the following plugins: %1$s.', |
||
| 351 | 'tgmpa' |
||
| 352 | ), |
||
| 353 | 'notice_ask_to_update' => _n_noop( |
||
| 354 | /* translators: 1: plugin name(s). */ |
||
| 355 | 'The following plugin needs to be updated to its latest version to ensure maximum compatibility with this theme: %1$s.', |
||
| 356 | 'The following plugins need to be updated to their latest version to ensure maximum compatibility with this theme: %1$s.', |
||
| 357 | 'tgmpa' |
||
| 358 | ), |
||
| 359 | 'notice_ask_to_update_maybe' => _n_noop( |
||
| 360 | /* translators: 1: plugin name(s). */ |
||
| 361 | 'There is an update available for: %1$s.', |
||
| 362 | 'There are updates available for the following plugins: %1$s.', |
||
| 363 | 'tgmpa' |
||
| 364 | ), |
||
| 365 | 'notice_can_activate_required' => _n_noop( |
||
| 366 | /* translators: 1: plugin name(s). */ |
||
| 367 | 'The following required plugin is currently inactive: %1$s.', |
||
| 368 | 'The following required plugins are currently inactive: %1$s.', |
||
| 369 | 'tgmpa' |
||
| 370 | ), |
||
| 371 | 'notice_can_activate_recommended' => _n_noop( |
||
| 372 | /* translators: 1: plugin name(s). */ |
||
| 373 | 'The following recommended plugin is currently inactive: %1$s.', |
||
| 374 | 'The following recommended plugins are currently inactive: %1$s.', |
||
| 375 | 'tgmpa' |
||
| 376 | ), |
||
| 377 | 'install_link' => _n_noop( |
||
| 378 | 'Begin installing plugin', |
||
| 379 | 'Begin installing plugins', |
||
| 380 | 'tgmpa' |
||
| 381 | ), |
||
| 382 | 'update_link' => _n_noop( |
||
| 383 | 'Begin updating plugin', |
||
| 384 | 'Begin updating plugins', |
||
| 385 | 'tgmpa' |
||
| 386 | ), |
||
| 387 | 'activate_link' => _n_noop( |
||
| 388 | 'Begin activating plugin', |
||
| 389 | 'Begin activating plugins', |
||
| 390 | 'tgmpa' |
||
| 391 | ), |
||
| 392 | 'return' => __( 'Return to Required Plugins Installer', 'tgmpa' ), |
||
| 393 | 'dashboard' => __( 'Return to the Dashboard', 'tgmpa' ), |
||
| 394 | 'plugin_activated' => __( 'Plugin activated successfully.', 'tgmpa' ), |
||
| 395 | 'activated_successfully' => __( 'The following plugin was activated successfully:', 'tgmpa' ), |
||
| 396 | /* translators: 1: plugin name. */ |
||
| 397 | 'plugin_already_active' => __( 'No action taken. Plugin %1$s was already active.', 'tgmpa' ), |
||
| 398 | /* translators: 1: plugin name. */ |
||
| 399 | 'plugin_needs_higher_version' => __( 'Plugin not activated. A higher version of %s is needed for this theme. Please update the plugin.', 'tgmpa' ), |
||
| 400 | /* translators: 1: dashboard link. */ |
||
| 401 | 'complete' => __( 'All plugins installed and activated successfully. %1$s', 'tgmpa' ), |
||
| 402 | 'dismiss' => __( 'Dismiss this notice', 'tgmpa' ), |
||
| 403 | 'notice_cannot_install_activate' => __( 'There are one or more required or recommended plugins to install, update or activate.', 'tgmpa' ), |
||
| 404 | 'contact_admin' => __( 'Please contact the administrator of this site for help.', 'tgmpa' ), |
||
| 405 | ); |
||
| 406 | |||
| 407 | do_action( 'tgmpa_register' ); |
||
| 408 | |||
| 409 | /* After this point, the plugins should be registered and the configuration set. */ |
||
| 410 | |||
| 411 | // Proceed only if we have plugins to handle. |
||
| 412 | if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { |
||
| 413 | return; |
||
| 414 | } |
||
| 415 | |||
| 416 | // Set up the menu and notices if we still have outstanding actions. |
||
| 417 | if ( true !== $this->is_tgmpa_complete() ) { |
||
| 418 | // Sort the plugins. |
||
| 419 | array_multisort( $this->sort_order, SORT_ASC, $this->plugins ); |
||
| 420 | |||
| 421 | add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
||
| 422 | add_action( 'admin_head', array( $this, 'dismiss' ) ); |
||
| 423 | |||
| 424 | // Prevent the normal links from showing underneath a single install/update page. |
||
| 425 | add_filter( 'install_plugin_complete_actions', array( $this, 'actions' ) ); |
||
| 426 | add_filter( 'update_plugin_complete_actions', array( $this, 'actions' ) ); |
||
| 427 | |||
| 428 | if ( $this->has_notices ) { |
||
| 429 | add_action( 'admin_notices', array( $this, 'notices' ) ); |
||
| 430 | add_action( 'admin_init', array( $this, 'admin_init' ), 1 ); |
||
| 431 | add_action( 'admin_enqueue_scripts', array( $this, 'thickbox' ) ); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | // If needed, filter plugin action links. |
||
| 436 | add_action( 'load-plugins.php', array( $this, 'add_plugin_action_link_filters' ), 1 ); |
||
| 437 | |||
| 438 | // Make sure things get reset on switch theme. |
||
| 439 | add_action( 'switch_theme', array( $this, 'flush_plugins_cache' ) ); |
||
| 440 | |||
| 441 | if ( $this->has_notices ) { |
||
| 442 | add_action( 'switch_theme', array( $this, 'update_dismiss' ) ); |
||
| 443 | } |
||
| 444 | |||
| 445 | // Setup the force activation hook. |
||
| 446 | if ( true === $this->has_forced_activation ) { |
||
| 447 | add_action( 'admin_init', array( $this, 'force_activation' ) ); |
||
| 448 | } |
||
| 449 | |||
| 450 | // Setup the force deactivation hook. |
||
| 451 | if ( true === $this->has_forced_deactivation ) { |
||
| 452 | add_action( 'switch_theme', array( $this, 'force_deactivation' ) ); |
||
| 453 | } |
||
| 454 | |||
| 455 | // Add CSS for the TGMPA admin page. |
||
| 456 | add_action( 'admin_head', array( $this, 'admin_css' ) ); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Load translations. |
||
| 461 | * |
||
| 462 | * @since 2.6.0 |
||
| 463 | * |
||
| 464 | * (@internal Uses `load_theme_textdomain()` rather than `load_plugin_textdomain()` to |
||
| 465 | * get round the different ways of handling the path and deprecated notices being thrown |
||
| 466 | * and such. For plugins, the actual file name will be corrected by a filter.}} |
||
| 467 | * |
||
| 468 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
| 469 | * generator on the website.}} |
||
| 470 | */ |
||
| 471 | public function load_textdomain() { |
||
| 472 | if ( is_textdomain_loaded( 'tgmpa' ) ) { |
||
| 473 | return; |
||
| 474 | } |
||
| 475 | |||
| 476 | if ( false !== strpos( __FILE__, WP_PLUGIN_DIR ) || false !== strpos( __FILE__, WPMU_PLUGIN_DIR ) ) { |
||
| 477 | // Plugin, we'll need to adjust the file name. |
||
| 478 | add_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10, 2 ); |
||
| 479 | load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); |
||
| 480 | remove_action( 'load_textdomain_mofile', array( $this, 'correct_plugin_mofile' ), 10 ); |
||
| 481 | } else { |
||
| 482 | load_theme_textdomain( 'tgmpa', dirname( __FILE__ ) . '/languages' ); |
||
| 483 | } |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Correct the .mo file name for (must-use) plugins. |
||
| 488 | * |
||
| 489 | * Themese use `/path/{locale}.mo` while plugins use `/path/{text-domain}-{locale}.mo`. |
||
| 490 | * |
||
| 491 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
| 492 | * generator on the website.}} |
||
| 493 | * |
||
| 494 | * @since 2.6.0 |
||
| 495 | * |
||
| 496 | * @param string $mofile Full path to the target mofile. |
||
| 497 | * @param string $domain The domain for which a language file is being loaded. |
||
| 498 | * @return string $mofile |
||
| 499 | */ |
||
| 500 | public function correct_plugin_mofile( $mofile, $domain ) { |
||
| 501 | // Exit early if not our domain (just in case). |
||
| 502 | if ( 'tgmpa' !== $domain ) { |
||
| 503 | return $mofile; |
||
| 504 | } |
||
| 505 | return preg_replace( '`/([a-z]{2}_[A-Z]{2}.mo)$`', '/tgmpa-$1', $mofile ); |
||
| 506 | } |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Potentially overload the fall-back translation file for the current language. |
||
| 510 | * |
||
| 511 | * WP, by default since WP 3.7, will load a local translation first and if none |
||
| 512 | * can be found, will try and find a translation in the /wp-content/languages/ directory. |
||
| 513 | * As this library is theme/plugin agnostic, translation files for TGMPA can exist both |
||
| 514 | * in the WP_LANG_DIR /plugins/ subdirectory as well as in the /themes/ subdirectory. |
||
| 515 | * |
||
| 516 | * This method makes sure both directories are checked. |
||
| 517 | * |
||
| 518 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
| 519 | * generator on the website.}} |
||
| 520 | * |
||
| 521 | * @since 2.6.0 |
||
| 522 | * |
||
| 523 | * @param string $mofile Full path to the target mofile. |
||
| 524 | * @param string $domain The domain for which a language file is being loaded. |
||
| 525 | * @return string $mofile |
||
| 526 | */ |
||
| 527 | public function overload_textdomain_mofile( $mofile, $domain ) { |
||
| 528 | // Exit early if not our domain, not a WP_LANG_DIR load or if the file exists and is readable. |
||
| 529 | if ( 'tgmpa' !== $domain || false === strpos( $mofile, WP_LANG_DIR ) || @is_readable( $mofile ) ) { |
||
| 530 | return $mofile; |
||
| 531 | } |
||
| 532 | |||
| 533 | // Current fallback file is not valid, let's try the alternative option. |
||
| 534 | if ( false !== strpos( $mofile, '/themes/' ) ) { |
||
| 535 | return str_replace( '/themes/', '/plugins/', $mofile ); |
||
| 536 | } elseif ( false !== strpos( $mofile, '/plugins/' ) ) { |
||
| 537 | return str_replace( '/plugins/', '/themes/', $mofile ); |
||
| 538 | } else { |
||
| 539 | return $mofile; |
||
| 540 | } |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Hook in plugin action link filters for the WP native plugins page. |
||
| 545 | * |
||
| 546 | * - Prevent activation of plugins which don't meet the minimum version requirements. |
||
| 547 | * - Prevent deactivation of force-activated plugins. |
||
| 548 | * - Add update notice if update available. |
||
| 549 | * |
||
| 550 | * @since 2.5.0 |
||
| 551 | */ |
||
| 552 | public function add_plugin_action_link_filters() { |
||
| 553 | foreach ( $this->plugins as $slug => $plugin ) { |
||
| 554 | if ( false === $this->can_plugin_activate( $slug ) ) { |
||
| 555 | add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_activate' ), 20 ); |
||
| 556 | } |
||
| 557 | |||
| 558 | if ( true === $plugin['force_activation'] ) { |
||
| 559 | add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_deactivate' ), 20 ); |
||
| 560 | } |
||
| 561 | |||
| 562 | if ( false !== $this->does_plugin_require_update( $slug ) ) { |
||
| 563 | add_filter( 'plugin_action_links_' . $plugin['file_path'], array( $this, 'filter_plugin_action_links_update' ), 20 ); |
||
| 564 | } |
||
| 565 | } |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Remove the 'Activate' link on the WP native plugins page if the plugin does not meet the |
||
| 570 | * minimum version requirements. |
||
| 571 | * |
||
| 572 | * @since 2.5.0 |
||
| 573 | * |
||
| 574 | * @param array $actions Action links. |
||
| 575 | * @return array |
||
| 576 | */ |
||
| 577 | public function filter_plugin_action_links_activate( $actions ) { |
||
| 578 | unset( $actions['activate'] ); |
||
| 579 | |||
| 580 | return $actions; |
||
| 581 | } |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Remove the 'Deactivate' link on the WP native plugins page if the plugin has been set to force activate. |
||
| 585 | * |
||
| 586 | * @since 2.5.0 |
||
| 587 | * |
||
| 588 | * @param array $actions Action links. |
||
| 589 | * @return array |
||
| 590 | */ |
||
| 591 | public function filter_plugin_action_links_deactivate( $actions ) { |
||
| 592 | unset( $actions['deactivate'] ); |
||
| 593 | |||
| 594 | return $actions; |
||
| 595 | } |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Add a 'Requires update' link on the WP native plugins page if the plugin does not meet the |
||
| 599 | * minimum version requirements. |
||
| 600 | * |
||
| 601 | * @since 2.5.0 |
||
| 602 | * |
||
| 603 | * @param array $actions Action links. |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | public function filter_plugin_action_links_update( $actions ) { |
||
| 607 | $actions['update'] = sprintf( |
||
| 608 | '<a href="%1$s" title="%2$s" class="edit">%3$s</a>', |
||
| 609 | esc_url( $this->get_tgmpa_status_url( 'update' ) ), |
||
| 610 | esc_attr__( 'This plugin needs to be updated to be compatible with your theme.', 'tgmpa' ), |
||
| 611 | esc_html__( 'Update Required', 'tgmpa' ) |
||
| 612 | ); |
||
| 613 | |||
| 614 | return $actions; |
||
| 615 | } |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Handles calls to show plugin information via links in the notices. |
||
| 619 | * |
||
| 620 | * We get the links in the admin notices to point to the TGMPA page, rather |
||
| 621 | * than the typical plugin-install.php file, so we can prepare everything |
||
| 622 | * beforehand. |
||
| 623 | * |
||
| 624 | * WP does not make it easy to show the plugin information in the thickbox - |
||
| 625 | * here we have to require a file that includes a function that does the |
||
| 626 | * main work of displaying it, enqueue some styles, set up some globals and |
||
| 627 | * finally call that function before exiting. |
||
| 628 | * |
||
| 629 | * Down right easy once you know how... |
||
| 630 | * |
||
| 631 | * Returns early if not the TGMPA page. |
||
| 632 | * |
||
| 633 | * @since 2.1.0 |
||
| 634 | * |
||
| 635 | * @global string $tab Used as iframe div class names, helps with styling |
||
| 636 | * @global string $body_id Used as the iframe body ID, helps with styling |
||
| 637 | * |
||
| 638 | * @return null Returns early if not the TGMPA page. |
||
| 639 | */ |
||
| 640 | public function admin_init() { |
||
| 641 | if ( ! $this->is_tgmpa_page() ) { |
||
| 642 | return; |
||
| 643 | } |
||
| 644 | |||
| 645 | if ( isset( $_REQUEST['tab'] ) && 'plugin-information' === $_REQUEST['tab'] ) { |
||
| 646 | // Needed for install_plugin_information(). |
||
| 647 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
||
| 648 | |||
| 649 | wp_enqueue_style( 'plugin-install' ); |
||
| 650 | |||
| 651 | global $tab, $body_id; |
||
| 652 | $body_id = 'plugin-information'; |
||
| 653 | // @codingStandardsIgnoreStart |
||
| 654 | $tab = 'plugin-information'; |
||
| 655 | // @codingStandardsIgnoreEnd |
||
| 656 | |||
| 657 | install_plugin_information(); |
||
| 658 | |||
| 659 | exit; |
||
| 660 | } |
||
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Enqueue thickbox scripts/styles for plugin info. |
||
| 665 | * |
||
| 666 | * Thickbox is not automatically included on all admin pages, so we must |
||
| 667 | * manually enqueue it for those pages. |
||
| 668 | * |
||
| 669 | * Thickbox is only loaded if the user has not dismissed the admin |
||
| 670 | * notice or if there are any plugins left to install and activate. |
||
| 671 | * |
||
| 672 | * @since 2.1.0 |
||
| 673 | */ |
||
| 674 | public function thickbox() { |
||
| 675 | if ( ! get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) ) { |
||
| 676 | add_thickbox(); |
||
| 677 | } |
||
| 678 | } |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Adds submenu page if there are plugin actions to take. |
||
| 682 | * |
||
| 683 | * This method adds the submenu page letting users know that a required |
||
| 684 | * plugin needs to be installed. |
||
| 685 | * |
||
| 686 | * This page disappears once the plugin has been installed and activated. |
||
| 687 | * |
||
| 688 | * @since 1.0.0 |
||
| 689 | * |
||
| 690 | * @see TGM_Plugin_Activation::init() |
||
| 691 | * @see TGM_Plugin_Activation::install_plugins_page() |
||
| 692 | * |
||
| 693 | * @return null Return early if user lacks capability to install a plugin. |
||
| 694 | */ |
||
| 695 | public function admin_menu() { |
||
| 696 | // Make sure privileges are correct to see the page. |
||
| 697 | if ( ! current_user_can( 'install_plugins' ) ) { |
||
| 698 | return; |
||
| 699 | } |
||
| 700 | |||
| 701 | $args = apply_filters( |
||
| 702 | 'tgmpa_admin_menu_args', |
||
| 703 | array( |
||
| 704 | 'parent_slug' => $this->parent_slug, // Parent Menu slug. |
||
| 705 | 'page_title' => $this->strings['page_title'], // Page title. |
||
| 706 | 'menu_title' => $this->strings['menu_title'], // Menu title. |
||
| 707 | 'capability' => $this->capability, // Capability. |
||
| 708 | 'menu_slug' => $this->menu, // Menu slug. |
||
| 709 | 'function' => array( $this, 'install_plugins_page' ), // Callback. |
||
| 710 | ) |
||
| 711 | ); |
||
| 712 | |||
| 713 | $this->add_admin_menu( $args ); |
||
| 714 | } |
||
| 715 | |||
| 716 | /** |
||
| 717 | * Add the menu item. |
||
| 718 | * |
||
| 719 | * {@internal IMPORTANT! If this function changes, review the regex in the custom TGMPA |
||
| 720 | * generator on the website.}} |
||
| 721 | * |
||
| 722 | * @since 2.5.0 |
||
| 723 | * |
||
| 724 | * @param array $args Menu item configuration. |
||
| 725 | */ |
||
| 726 | protected function add_admin_menu( array $args ) { |
||
| 727 | if ( has_filter( 'tgmpa_admin_menu_use_add_theme_page' ) ) { |
||
| 728 | _deprecated_function( 'The "tgmpa_admin_menu_use_add_theme_page" filter', '2.5.0', esc_html__( 'Set the parent_slug config variable instead.', 'tgmpa' ) ); |
||
| 729 | } |
||
| 730 | |||
| 731 | if ( 'themes.php' === $this->parent_slug ) { |
||
| 732 | $this->page_hook = call_user_func( 'add_theme_page', $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); |
||
| 733 | } else { |
||
| 734 | $this->page_hook = call_user_func( 'add_submenu_page', $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] ); |
||
| 735 | } |
||
| 736 | } |
||
| 737 | |||
| 738 | /** |
||
| 739 | * Echoes plugin installation form. |
||
| 740 | * |
||
| 741 | * This method is the callback for the admin_menu method function. |
||
| 742 | * This displays the admin page and form area where the user can select to install and activate the plugin. |
||
| 743 | * Aborts early if we're processing a plugin installation action. |
||
| 744 | * |
||
| 745 | * @since 1.0.0 |
||
| 746 | * |
||
| 747 | * @return null Aborts early if we're processing a plugin installation action. |
||
| 748 | */ |
||
| 749 | public function install_plugins_page() { |
||
| 750 | // Store new instance of plugin table in object. |
||
| 751 | $plugin_table = new TGMPA_List_Table; |
||
| 752 | |||
| 753 | // Return early if processing a plugin installation action. |
||
| 754 | if ( ( ( 'tgmpa-bulk-install' === $plugin_table->current_action() || 'tgmpa-bulk-update' === $plugin_table->current_action() ) && $plugin_table->process_bulk_actions() ) || $this->do_plugin_install() ) { |
||
| 755 | return; |
||
| 756 | } |
||
| 757 | |||
| 758 | // Force refresh of available plugin information so we'll know about manual updates/deletes. |
||
| 759 | wp_clean_plugins_cache( false ); |
||
| 760 | |||
| 761 | ?> |
||
| 762 | <div class="tgmpa wrap"> |
||
| 763 | <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
||
| 764 | <?php $plugin_table->prepare_items(); ?> |
||
| 765 | |||
| 766 | <?php |
||
| 767 | if ( ! empty( $this->message ) && is_string( $this->message ) ) { |
||
| 768 | echo wp_kses_post( $this->message ); |
||
| 769 | } |
||
| 770 | ?> |
||
| 771 | <?php $plugin_table->views(); ?> |
||
| 772 | |||
| 773 | <form id="tgmpa-plugins" action="" method="post"> |
||
| 774 | <input type="hidden" name="tgmpa-page" value="<?php echo esc_attr( $this->menu ); ?>" /> |
||
| 775 | <input type="hidden" name="plugin_status" value="<?php echo esc_attr( $plugin_table->view_context ); ?>" /> |
||
| 776 | <?php $plugin_table->display(); ?> |
||
| 777 | </form> |
||
| 778 | </div> |
||
| 779 | <?php |
||
| 780 | } |
||
| 781 | |||
| 782 | /** |
||
| 783 | * Installs, updates or activates a plugin depending on the action link clicked by the user. |
||
| 784 | * |
||
| 785 | * Checks the $_GET variable to see which actions have been |
||
| 786 | * passed and responds with the appropriate method. |
||
| 787 | * |
||
| 788 | * Uses WP_Filesystem to process and handle the plugin installation |
||
| 789 | * method. |
||
| 790 | * |
||
| 791 | * @since 1.0.0 |
||
| 792 | * |
||
| 793 | * @uses WP_Filesystem |
||
| 794 | * @uses WP_Error |
||
| 795 | * @uses WP_Upgrader |
||
| 796 | * @uses Plugin_Upgrader |
||
| 797 | * @uses Plugin_Installer_Skin |
||
| 798 | * @uses Plugin_Upgrader_Skin |
||
| 799 | * |
||
| 800 | * @return boolean True on success, false on failure. |
||
| 801 | */ |
||
| 802 | protected function do_plugin_install() { |
||
| 803 | if ( empty( $_GET['plugin'] ) ) { |
||
| 804 | return false; |
||
| 805 | } |
||
| 806 | |||
| 807 | // All plugin information will be stored in an array for processing. |
||
| 808 | $slug = $this->sanitize_key( urldecode( $_GET['plugin'] ) ); |
||
| 809 | |||
| 810 | if ( ! isset( $this->plugins[ $slug ] ) ) { |
||
| 811 | return false; |
||
| 812 | } |
||
| 813 | |||
| 814 | // Was an install or upgrade action link clicked? |
||
| 815 | if ( ( isset( $_GET['tgmpa-install'] ) && 'install-plugin' === $_GET['tgmpa-install'] ) || ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) ) { |
||
| 816 | |||
| 817 | $install_type = 'install'; |
||
| 818 | if ( isset( $_GET['tgmpa-update'] ) && 'update-plugin' === $_GET['tgmpa-update'] ) { |
||
| 819 | $install_type = 'update'; |
||
| 820 | } |
||
| 821 | |||
| 822 | check_admin_referer( 'tgmpa-' . $install_type, 'tgmpa-nonce' ); |
||
| 823 | |||
| 824 | // Pass necessary information via URL if WP_Filesystem is needed. |
||
| 825 | $url = wp_nonce_url( |
||
| 826 | add_query_arg( |
||
| 827 | array( |
||
| 828 | 'plugin' => urlencode( $slug ), |
||
| 829 | 'tgmpa-' . $install_type => $install_type . '-plugin', |
||
| 830 | ), |
||
| 831 | $this->get_tgmpa_url() |
||
| 832 | ), |
||
| 833 | 'tgmpa-' . $install_type, |
||
| 834 | 'tgmpa-nonce' |
||
| 835 | ); |
||
| 836 | |||
| 837 | $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. |
||
| 838 | |||
| 839 | if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, array() ) ) ) { |
||
| 840 | return true; |
||
| 841 | } |
||
| 842 | |||
| 843 | if ( ! WP_Filesystem( $creds ) ) { |
||
| 844 | request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, array() ); // Setup WP_Filesystem. |
||
| 845 | return true; |
||
| 846 | } |
||
| 847 | |||
| 848 | /* If we arrive here, we have the filesystem. */ |
||
| 849 | |||
| 850 | // Prep variables for Plugin_Installer_Skin class. |
||
| 851 | $extra = array(); |
||
| 852 | $extra['slug'] = $slug; // Needed for potentially renaming of directory name. |
||
| 853 | $source = $this->get_download_url( $slug ); |
||
| 854 | $api = ( 'repo' === $this->plugins[ $slug ]['source_type'] ) ? $this->get_plugins_api( $slug ) : null; |
||
| 855 | $api = ( false !== $api ) ? $api : null; |
||
| 856 | |||
| 857 | $url = add_query_arg( |
||
| 858 | array( |
||
| 859 | 'action' => $install_type . '-plugin', |
||
| 860 | 'plugin' => urlencode( $slug ), |
||
| 861 | ), |
||
| 862 | 'update.php' |
||
| 863 | ); |
||
| 864 | |||
| 865 | if ( ! class_exists( 'Plugin_Upgrader', false ) ) { |
||
| 866 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 867 | } |
||
| 868 | |||
| 869 | $title = ( 'update' === $install_type ) ? $this->strings['updating'] : $this->strings['installing']; |
||
| 870 | $skin_args = array( |
||
| 871 | 'type' => ( 'bundled' !== $this->plugins[ $slug ]['source_type'] ) ? 'web' : 'upload', |
||
| 872 | 'title' => sprintf( $title, $this->plugins[ $slug ]['name'] ), |
||
| 873 | 'url' => esc_url_raw( $url ), |
||
| 874 | 'nonce' => $install_type . '-plugin_' . $slug, |
||
| 875 | 'plugin' => '', |
||
| 876 | 'api' => $api, |
||
| 877 | 'extra' => $extra, |
||
| 878 | ); |
||
| 879 | unset( $title ); |
||
| 880 | |||
| 881 | if ( 'update' === $install_type ) { |
||
| 882 | $skin_args['plugin'] = $this->plugins[ $slug ]['file_path']; |
||
| 883 | $skin = new Plugin_Upgrader_Skin( $skin_args ); |
||
| 884 | } else { |
||
| 885 | $skin = new Plugin_Installer_Skin( $skin_args ); |
||
| 886 | } |
||
| 887 | |||
| 888 | // Create a new instance of Plugin_Upgrader. |
||
| 889 | $upgrader = new Plugin_Upgrader( $skin ); |
||
| 890 | |||
| 891 | // Perform the action and install the plugin from the $source urldecode(). |
||
| 892 | add_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1, 3 ); |
||
| 893 | |||
| 894 | if ( 'update' === $install_type ) { |
||
| 895 | // Inject our info into the update transient. |
||
| 896 | $to_inject = array( $slug => $this->plugins[ $slug ] ); |
||
| 897 | $to_inject[ $slug ]['source'] = $source; |
||
| 898 | $this->inject_update_info( $to_inject ); |
||
| 899 | |||
| 900 | $upgrader->upgrade( $this->plugins[ $slug ]['file_path'] ); |
||
| 901 | } else { |
||
| 902 | $upgrader->install( $source ); |
||
| 903 | } |
||
| 904 | |||
| 905 | remove_filter( 'upgrader_source_selection', array( $this, 'maybe_adjust_source_dir' ), 1 ); |
||
| 906 | |||
| 907 | // Make sure we have the correct file path now the plugin is installed/updated. |
||
| 908 | $this->populate_file_path( $slug ); |
||
| 909 | |||
| 910 | // Only activate plugins if the config option is set to true and the plugin isn't |
||
| 911 | // already active (upgrade). |
||
| 912 | if ( $this->is_automatic && ! $this->is_plugin_active( $slug ) ) { |
||
| 913 | $plugin_activate = $upgrader->plugin_info(); // Grab the plugin info from the Plugin_Upgrader method. |
||
| 914 | if ( false === $this->activate_single_plugin( $plugin_activate, $slug, true ) ) { |
||
| 915 | return true; // Finish execution of the function early as we encountered an error. |
||
| 916 | } |
||
| 917 | } |
||
| 918 | |||
| 919 | $this->show_tgmpa_version(); |
||
| 920 | |||
| 921 | // Display message based on if all plugins are now active or not. |
||
| 922 | if ( $this->is_tgmpa_complete() ) { |
||
| 923 | echo '<p>', sprintf( esc_html( $this->strings['complete'] ), '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' ), '</p>'; |
||
| 924 | echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; |
||
| 925 | } else { |
||
| 926 | echo '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; |
||
| 927 | } |
||
| 928 | |||
| 929 | return true; |
||
| 930 | } elseif ( isset( $this->plugins[ $slug ]['file_path'], $_GET['tgmpa-activate'] ) && 'activate-plugin' === $_GET['tgmpa-activate'] ) { |
||
| 931 | // Activate action link was clicked. |
||
| 932 | check_admin_referer( 'tgmpa-activate', 'tgmpa-nonce' ); |
||
| 933 | |||
| 934 | if ( false === $this->activate_single_plugin( $this->plugins[ $slug ]['file_path'], $slug ) ) { |
||
| 935 | return true; // Finish execution of the function early as we encountered an error. |
||
| 936 | } |
||
| 937 | } |
||
| 938 | |||
| 939 | return false; |
||
| 940 | } |
||
| 941 | |||
| 942 | /** |
||
| 943 | * Inject information into the 'update_plugins' site transient as WP checks that before running an update. |
||
| 944 | * |
||
| 945 | * @since 2.5.0 |
||
| 946 | * |
||
| 947 | * @param array $plugins The plugin information for the plugins which are to be updated. |
||
| 948 | */ |
||
| 949 | public function inject_update_info( $plugins ) { |
||
| 950 | $repo_updates = get_site_transient( 'update_plugins' ); |
||
| 951 | |||
| 952 | if ( ! is_object( $repo_updates ) ) { |
||
| 953 | $repo_updates = new stdClass; |
||
| 954 | } |
||
| 955 | |||
| 956 | foreach ( $plugins as $slug => $plugin ) { |
||
| 957 | $file_path = $plugin['file_path']; |
||
| 958 | |||
| 959 | if ( empty( $repo_updates->response[ $file_path ] ) ) { |
||
| 960 | $repo_updates->response[ $file_path ] = new stdClass; |
||
| 961 | } |
||
| 962 | |||
| 963 | // We only really need to set package, but let's do all we can in case WP changes something. |
||
| 964 | $repo_updates->response[ $file_path ]->slug = $slug; |
||
| 965 | $repo_updates->response[ $file_path ]->plugin = $file_path; |
||
| 966 | $repo_updates->response[ $file_path ]->new_version = $plugin['version']; |
||
| 967 | $repo_updates->response[ $file_path ]->package = $plugin['source']; |
||
| 968 | if ( empty( $repo_updates->response[ $file_path ]->url ) && ! empty( $plugin['external_url'] ) ) { |
||
| 969 | $repo_updates->response[ $file_path ]->url = $plugin['external_url']; |
||
| 970 | } |
||
| 971 | } |
||
| 972 | |||
| 973 | set_site_transient( 'update_plugins', $repo_updates ); |
||
| 974 | } |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Adjust the plugin directory name if necessary. |
||
| 978 | * |
||
| 979 | * The final destination directory of a plugin is based on the subdirectory name found in the |
||
| 980 | * (un)zipped source. In some cases - most notably GitHub repository plugin downloads -, this |
||
| 981 | * subdirectory name is not the same as the expected slug and the plugin will not be recognized |
||
| 982 | * as installed. This is fixed by adjusting the temporary unzipped source subdirectory name to |
||
| 983 | * the expected plugin slug. |
||
| 984 | * |
||
| 985 | * @since 2.5.0 |
||
| 986 | * |
||
| 987 | * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/. |
||
| 988 | * @param string $remote_source Path to upgrade/zip-file-name.tmp. |
||
| 989 | * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin. |
||
| 990 | * @return string $source |
||
| 991 | */ |
||
| 992 | public function maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { |
||
| 993 | if ( ! $this->is_tgmpa_page() || ! is_object( $GLOBALS['wp_filesystem'] ) ) { |
||
| 994 | return $source; |
||
| 995 | } |
||
| 996 | |||
| 997 | // Check for single file plugins. |
||
| 998 | $source_files = array_keys( $GLOBALS['wp_filesystem']->dirlist( $remote_source ) ); |
||
| 999 | if ( 1 === count( $source_files ) && false === $GLOBALS['wp_filesystem']->is_dir( $source ) ) { |
||
| 1000 | return $source; |
||
| 1001 | } |
||
| 1002 | |||
| 1003 | // Multi-file plugin, let's see if the directory is correctly named. |
||
| 1004 | $desired_slug = ''; |
||
| 1005 | |||
| 1006 | // Figure out what the slug is supposed to be. |
||
| 1007 | if ( false === $upgrader->bulk && ! empty( $upgrader->skin->options['extra']['slug'] ) ) { |
||
| 1008 | $desired_slug = $upgrader->skin->options['extra']['slug']; |
||
| 1009 | } else { |
||
| 1010 | // Bulk installer contains less info, so fall back on the info registered here. |
||
| 1011 | foreach ( $this->plugins as $slug => $plugin ) { |
||
| 1012 | if ( ! empty( $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) && $plugin['name'] === $upgrader->skin->plugin_names[ $upgrader->skin->i ] ) { |
||
| 1013 | $desired_slug = $slug; |
||
| 1014 | break; |
||
| 1015 | } |
||
| 1016 | } |
||
| 1017 | unset( $slug, $plugin ); |
||
| 1018 | } |
||
| 1019 | |||
| 1020 | if ( ! empty( $desired_slug ) ) { |
||
| 1021 | $subdir_name = untrailingslashit( str_replace( trailingslashit( $remote_source ), '', $source ) ); |
||
| 1022 | |||
| 1023 | if ( ! empty( $subdir_name ) && $subdir_name !== $desired_slug ) { |
||
| 1024 | $from_path = untrailingslashit( $source ); |
||
| 1025 | $to_path = trailingslashit( $remote_source ) . $desired_slug; |
||
| 1026 | |||
| 1027 | if ( true === $GLOBALS['wp_filesystem']->move( $from_path, $to_path ) ) { |
||
| 1028 | return trailingslashit( $to_path ); |
||
| 1029 | View Code Duplication | } else { |
|
| 1030 | return new WP_Error( 'rename_failed', esc_html__( 'The remote plugin package does not contain a folder with the desired slug and renaming did not work.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); |
||
| 1031 | } |
||
| 1032 | View Code Duplication | } elseif ( empty( $subdir_name ) ) { |
|
| 1033 | return new WP_Error( 'packaged_wrong', esc_html__( 'The remote plugin package consists of more than one file, but the files are not packaged in a folder.', 'tgmpa' ) . ' ' . esc_html__( 'Please contact the plugin provider and ask them to package their plugin according to the WordPress guidelines.', 'tgmpa' ), array( 'found' => $subdir_name, 'expected' => $desired_slug ) ); |
||
| 1034 | } |
||
| 1035 | } |
||
| 1036 | |||
| 1037 | return $source; |
||
| 1038 | } |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Activate a single plugin and send feedback about the result to the screen. |
||
| 1042 | * |
||
| 1043 | * @since 2.5.0 |
||
| 1044 | * |
||
| 1045 | * @param string $file_path Path within wp-plugins/ to main plugin file. |
||
| 1046 | * @param string $slug Plugin slug. |
||
| 1047 | * @param bool $automatic Whether this is an automatic activation after an install. Defaults to false. |
||
| 1048 | * This determines the styling of the output messages. |
||
| 1049 | * @return bool False if an error was encountered, true otherwise. |
||
| 1050 | */ |
||
| 1051 | protected function activate_single_plugin( $file_path, $slug, $automatic = false ) { |
||
| 1052 | if ( $this->can_plugin_activate( $slug ) ) { |
||
| 1053 | $activate = activate_plugin( $file_path ); |
||
| 1054 | |||
| 1055 | if ( is_wp_error( $activate ) ) { |
||
| 1056 | echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>', |
||
| 1057 | '<p><a href="', esc_url( $this->get_tgmpa_url() ), '" target="_parent">', esc_html( $this->strings['return'] ), '</a></p>'; |
||
| 1058 | |||
| 1059 | return false; // End it here if there is an error with activation. |
||
| 1060 | } else { |
||
| 1061 | if ( ! $automatic ) { |
||
| 1062 | // Make sure message doesn't display again if bulk activation is performed |
||
| 1063 | // immediately after a single activation. |
||
| 1064 | if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK. |
||
| 1065 | echo '<div id="message" class="updated"><p>', esc_html( $this->strings['activated_successfully'] ), ' <strong>', esc_html( $this->plugins[ $slug ]['name'] ), '.</strong></p></div>'; |
||
| 1066 | } |
||
| 1067 | } else { |
||
| 1068 | // Simpler message layout for use on the plugin install page. |
||
| 1069 | echo '<p>', esc_html( $this->strings['plugin_activated'] ), '</p>'; |
||
| 1070 | } |
||
| 1071 | } |
||
| 1072 | View Code Duplication | } elseif ( $this->is_plugin_active( $slug ) ) { |
|
| 1073 | // No simpler message format provided as this message should never be encountered |
||
| 1074 | // on the plugin install page. |
||
| 1075 | echo '<div id="message" class="error"><p>', |
||
| 1076 | sprintf( |
||
| 1077 | esc_html( $this->strings['plugin_already_active'] ), |
||
| 1078 | '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' |
||
| 1079 | ), |
||
| 1080 | '</p></div>'; |
||
| 1081 | } elseif ( $this->does_plugin_require_update( $slug ) ) { |
||
| 1082 | if ( ! $automatic ) { |
||
| 1083 | // Make sure message doesn't display again if bulk activation is performed |
||
| 1084 | // immediately after a single activation. |
||
| 1085 | View Code Duplication | if ( ! isset( $_POST['action'] ) ) { // WPCS: CSRF OK. |
|
| 1086 | echo '<div id="message" class="error"><p>', |
||
| 1087 | sprintf( |
||
| 1088 | esc_html( $this->strings['plugin_needs_higher_version'] ), |
||
| 1089 | '<strong>' . esc_html( $this->plugins[ $slug ]['name'] ) . '</strong>' |
||
| 1090 | ), |
||
| 1091 | '</p></div>'; |
||
| 1092 | } |
||
| 1093 | } else { |
||
| 1094 | // Simpler message layout for use on the plugin install page. |
||
| 1095 | echo '<p>', sprintf( esc_html( $this->strings['plugin_needs_higher_version'] ), esc_html( $this->plugins[ $slug ]['name'] ) ), '</p>'; |
||
| 1096 | } |
||
| 1097 | } |
||
| 1098 | |||
| 1099 | return true; |
||
| 1100 | } |
||
| 1101 | |||
| 1102 | /** |
||
| 1103 | * Echoes required plugin notice. |
||
| 1104 | * |
||
| 1105 | * Outputs a message telling users that a specific plugin is required for |
||
| 1106 | * their theme. If appropriate, it includes a link to the form page where |
||
| 1107 | * users can install and activate the plugin. |
||
| 1108 | * |
||
| 1109 | * Returns early if we're on the Install page. |
||
| 1110 | * |
||
| 1111 | * @since 1.0.0 |
||
| 1112 | * |
||
| 1113 | * @global object $current_screen |
||
| 1114 | * |
||
| 1115 | * @return null Returns early if we're on the Install page. |
||
| 1116 | */ |
||
| 1117 | public function notices() { |
||
| 1118 | // Remove nag on the install page / Return early if the nag message has been dismissed or user < author. |
||
| 1119 | if ( ( $this->is_tgmpa_page() || $this->is_core_update_page() ) || get_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, true ) || ! current_user_can( apply_filters( 'tgmpa_show_admin_notice_capability', 'publish_posts' ) ) ) { |
||
| 1120 | return; |
||
| 1121 | } |
||
| 1122 | |||
| 1123 | // Store for the plugin slugs by message type. |
||
| 1124 | $message = array(); |
||
| 1125 | |||
| 1126 | // Initialize counters used to determine plurality of action link texts. |
||
| 1127 | $install_link_count = 0; |
||
| 1128 | $update_link_count = 0; |
||
| 1129 | $activate_link_count = 0; |
||
| 1130 | $total_required_action_count = 0; |
||
| 1131 | |||
| 1132 | foreach ( $this->plugins as $slug => $plugin ) { |
||
| 1133 | if ( $this->is_plugin_active( $slug ) && false === $this->does_plugin_have_update( $slug ) ) { |
||
| 1134 | continue; |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | if ( ! $this->is_plugin_installed( $slug ) ) { |
||
| 1138 | if ( current_user_can( 'install_plugins' ) ) { |
||
| 1139 | $install_link_count++; |
||
| 1140 | |||
| 1141 | if ( true === $plugin['required'] ) { |
||
| 1142 | $message['notice_can_install_required'][] = $slug; |
||
| 1143 | } else { |
||
| 1144 | $message['notice_can_install_recommended'][] = $slug; |
||
| 1145 | } |
||
| 1146 | } |
||
| 1147 | if ( true === $plugin['required'] ) { |
||
| 1148 | $total_required_action_count++; |
||
| 1149 | } |
||
| 1150 | } else { |
||
| 1151 | if ( ! $this->is_plugin_active( $slug ) && $this->can_plugin_activate( $slug ) ) { |
||
| 1152 | if ( current_user_can( 'activate_plugins' ) ) { |
||
| 1153 | $activate_link_count++; |
||
| 1154 | |||
| 1155 | if ( true === $plugin['required'] ) { |
||
| 1156 | $message['notice_can_activate_required'][] = $slug; |
||
| 1157 | } else { |
||
| 1158 | $message['notice_can_activate_recommended'][] = $slug; |
||
| 1159 | } |
||
| 1160 | } |
||
| 1161 | if ( true === $plugin['required'] ) { |
||
| 1162 | $total_required_action_count++; |
||
| 1163 | } |
||
| 1164 | } |
||
| 1165 | |||
| 1166 | if ( $this->does_plugin_require_update( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { |
||
| 1167 | |||
| 1168 | if ( current_user_can( 'update_plugins' ) ) { |
||
| 1169 | $update_link_count++; |
||
| 1170 | |||
| 1171 | if ( $this->does_plugin_require_update( $slug ) ) { |
||
| 1172 | $message['notice_ask_to_update'][] = $slug; |
||
| 1173 | } elseif ( false !== $this->does_plugin_have_update( $slug ) ) { |
||
| 1174 | $message['notice_ask_to_update_maybe'][] = $slug; |
||
| 1175 | } |
||
| 1176 | } |
||
| 1177 | if ( true === $plugin['required'] ) { |
||
| 1178 | $total_required_action_count++; |
||
| 1179 | } |
||
| 1180 | } |
||
| 1181 | } |
||
| 1182 | } |
||
| 1183 | unset( $slug, $plugin ); |
||
| 1184 | |||
| 1185 | // If we have notices to display, we move forward. |
||
| 1186 | if ( ! empty( $message ) || $total_required_action_count > 0 ) { |
||
| 1187 | krsort( $message ); // Sort messages. |
||
| 1188 | $rendered = ''; |
||
| 1189 | |||
| 1190 | // As add_settings_error() wraps the final message in a <p> and as the final message can't be |
||
| 1191 | // filtered, using <p>'s in our html would render invalid html output. |
||
| 1192 | $line_template = '<span style="display: block; margin: 0.5em 0.5em 0 0; clear: both;">%s</span>' . "\n"; |
||
| 1193 | |||
| 1194 | if ( ! current_user_can( 'activate_plugins' ) && ! current_user_can( 'install_plugins' ) && ! current_user_can( 'update_plugins' ) ) { |
||
| 1195 | $rendered = esc_html( $this->strings['notice_cannot_install_activate'] ) . ' ' . esc_html( $this->strings['contact_admin'] ); |
||
| 1196 | $rendered .= $this->create_user_action_links_for_notice( 0, 0, 0, $line_template ); |
||
| 1197 | } else { |
||
| 1198 | |||
| 1199 | // If dismissable is false and a message is set, output it now. |
||
| 1200 | if ( ! $this->dismissable && ! empty( $this->dismiss_msg ) ) { |
||
| 1201 | $rendered .= sprintf( $line_template, wp_kses_post( $this->dismiss_msg ) ); |
||
| 1202 | } |
||
| 1203 | |||
| 1204 | // Render the individual message lines for the notice. |
||
| 1205 | foreach ( $message as $type => $plugin_group ) { |
||
| 1206 | $linked_plugins = array(); |
||
| 1207 | |||
| 1208 | // Get the external info link for a plugin if one is available. |
||
| 1209 | foreach ( $plugin_group as $plugin_slug ) { |
||
| 1210 | $linked_plugins[] = $this->get_info_link( $plugin_slug ); |
||
| 1211 | } |
||
| 1212 | unset( $plugin_slug ); |
||
| 1213 | |||
| 1214 | $count = count( $plugin_group ); |
||
| 1215 | $linked_plugins = array_map( array( 'TGMPA_Utils', 'wrap_in_em' ), $linked_plugins ); |
||
| 1216 | $last_plugin = array_pop( $linked_plugins ); // Pop off last name to prep for readability. |
||
| 1217 | $imploded = empty( $linked_plugins ) ? $last_plugin : ( implode( ', ', $linked_plugins ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin ); |
||
| 1218 | |||
| 1219 | $rendered .= sprintf( |
||
| 1220 | $line_template, |
||
| 1221 | sprintf( |
||
| 1222 | translate_nooped_plural( $this->strings[ $type ], $count, 'tgmpa' ), |
||
| 1223 | $imploded, |
||
| 1224 | $count |
||
| 1225 | ) |
||
| 1226 | ); |
||
| 1227 | |||
| 1228 | } |
||
| 1229 | unset( $type, $plugin_group, $linked_plugins, $count, $last_plugin, $imploded ); |
||
| 1230 | |||
| 1231 | $rendered .= $this->create_user_action_links_for_notice( $install_link_count, $update_link_count, $activate_link_count, $line_template ); |
||
| 1232 | } |
||
| 1233 | |||
| 1234 | // Register the nag messages and prepare them to be processed. |
||
| 1235 | add_settings_error( 'tgmpa', 'tgmpa', $rendered, $this->get_admin_notice_class() ); |
||
| 1236 | } |
||
| 1237 | |||
| 1238 | // Admin options pages already output settings_errors, so this is to avoid duplication. |
||
| 1239 | if ( 'options-general' !== $GLOBALS['current_screen']->parent_base ) { |
||
| 1240 | $this->display_settings_errors(); |
||
| 1241 | } |
||
| 1242 | } |
||
| 1243 | |||
| 1244 | /** |
||
| 1245 | * Generate the user action links for the admin notice. |
||
| 1246 | * |
||
| 1247 | * @since 2.6.0 |
||
| 1248 | * |
||
| 1249 | * @param int $install_count Number of plugins to install. |
||
| 1250 | * @param int $update_count Number of plugins to update. |
||
| 1251 | * @param int $activate_count Number of plugins to activate. |
||
| 1252 | * @param int $line_template Template for the HTML tag to output a line. |
||
| 1253 | * @return string Action links. |
||
| 1254 | */ |
||
| 1255 | protected function create_user_action_links_for_notice( $install_count, $update_count, $activate_count, $line_template ) { |
||
| 1256 | // Setup action links. |
||
| 1257 | $action_links = array( |
||
| 1258 | 'install' => '', |
||
| 1259 | 'update' => '', |
||
| 1260 | 'activate' => '', |
||
| 1261 | 'dismiss' => $this->dismissable ? '<a href="' . esc_url( wp_nonce_url( add_query_arg( 'tgmpa-dismiss', 'dismiss_admin_notices' ), 'tgmpa-dismiss-' . get_current_user_id() ) ) . '" class="dismiss-notice" target="_parent">' . esc_html( $this->strings['dismiss'] ) . '</a>' : '', |
||
| 1262 | ); |
||
| 1263 | |||
| 1264 | $link_template = '<a href="%2$s">%1$s</a>'; |
||
| 1265 | |||
| 1266 | if ( current_user_can( 'install_plugins' ) ) { |
||
| 1267 | if ( $install_count > 0 ) { |
||
| 1268 | $action_links['install'] = sprintf( |
||
| 1269 | $link_template, |
||
| 1270 | translate_nooped_plural( $this->strings['install_link'], $install_count, 'tgmpa' ), |
||
| 1271 | esc_url( $this->get_tgmpa_status_url( 'install' ) ) |
||
| 1272 | ); |
||
| 1273 | } |
||
| 1274 | if ( $update_count > 0 ) { |
||
| 1275 | $action_links['update'] = sprintf( |
||
| 1276 | $link_template, |
||
| 1277 | translate_nooped_plural( $this->strings['update_link'], $update_count, 'tgmpa' ), |
||
| 1278 | esc_url( $this->get_tgmpa_status_url( 'update' ) ) |
||
| 1279 | ); |
||
| 1280 | } |
||
| 1281 | } |
||
| 1282 | |||
| 1283 | if ( current_user_can( 'activate_plugins' ) && $activate_count > 0 ) { |
||
| 1284 | $action_links['activate'] = sprintf( |
||
| 1285 | $link_template, |
||
| 1286 | translate_nooped_plural( $this->strings['activate_link'], $activate_count, 'tgmpa' ), |
||
| 1287 | esc_url( $this->get_tgmpa_status_url( 'activate' ) ) |
||
| 1288 | ); |
||
| 1289 | } |
||
| 1290 | |||
| 1291 | $action_links = apply_filters( 'tgmpa_notice_action_links', $action_links ); |
||
| 1292 | |||
| 1293 | $action_links = array_filter( (array) $action_links ); // Remove any empty array items. |
||
| 1294 | |||
| 1295 | if ( ! empty( $action_links ) ) { |
||
| 1296 | $action_links = sprintf( $line_template, implode( ' | ', $action_links ) ); |
||
| 1297 | return apply_filters( 'tgmpa_notice_rendered_action_links', $action_links ); |
||
| 1298 | } else { |
||
| 1299 | return ''; |
||
| 1300 | } |
||
| 1301 | } |
||
| 1302 | |||
| 1303 | /** |
||
| 1304 | * Get admin notice class. |
||
| 1305 | * |
||
| 1306 | * Work around all the changes to the various admin notice classes between WP 4.4 and 3.7 |
||
| 1307 | * (lowest supported version by TGMPA). |
||
| 1308 | * |
||
| 1309 | * @since 2.6.0 |
||
| 1310 | * |
||
| 1311 | * @return string |
||
| 1312 | */ |
||
| 1313 | protected function get_admin_notice_class() { |
||
| 1314 | if ( ! empty( $this->strings['nag_type'] ) ) { |
||
| 1315 | return sanitize_html_class( strtolower( $this->strings['nag_type'] ) ); |
||
| 1316 | } else { |
||
| 1317 | if ( version_compare( $this->wp_version, '4.2', '>=' ) ) { |
||
| 1318 | return 'notice-warning'; |
||
| 1319 | } elseif ( version_compare( $this->wp_version, '4.1', '>=' ) ) { |
||
| 1320 | return 'notice'; |
||
| 1321 | } else { |
||
| 1322 | return 'updated'; |
||
| 1323 | } |
||
| 1324 | } |
||
| 1325 | } |
||
| 1326 | |||
| 1327 | /** |
||
| 1328 | * Display settings errors and remove those which have been displayed to avoid duplicate messages showing |
||
| 1329 | * |
||
| 1330 | * @since 2.5.0 |
||
| 1331 | */ |
||
| 1332 | protected function display_settings_errors() { |
||
| 1333 | global $wp_settings_errors; |
||
| 1334 | |||
| 1335 | settings_errors( 'tgmpa' ); |
||
| 1336 | |||
| 1337 | foreach ( (array) $wp_settings_errors as $key => $details ) { |
||
| 1338 | if ( 'tgmpa' === $details['setting'] ) { |
||
| 1339 | unset( $wp_settings_errors[ $key ] ); |
||
| 1340 | break; |
||
| 1341 | } |
||
| 1342 | } |
||
| 1343 | } |
||
| 1344 | |||
| 1345 | /** |
||
| 1346 | * Register dismissal of admin notices. |
||
| 1347 | * |
||
| 1348 | * Acts on the dismiss link in the admin nag messages. |
||
| 1349 | * If clicked, the admin notice disappears and will no longer be visible to this user. |
||
| 1350 | * |
||
| 1351 | * @since 2.1.0 |
||
| 1352 | */ |
||
| 1353 | public function dismiss() { |
||
| 1354 | if ( isset( $_GET['tgmpa-dismiss'] ) && check_admin_referer( 'tgmpa-dismiss-' . get_current_user_id() ) ) { |
||
| 1355 | update_user_meta( get_current_user_id(), 'tgmpa_dismissed_notice_' . $this->id, 1 ); |
||
| 1356 | } |
||
| 1357 | } |
||
| 1358 | |||
| 1359 | /** |
||
| 1360 | * Add individual plugin to our collection of plugins. |
||
| 1361 | * |
||
| 1362 | * If the required keys are not set or the plugin has already |
||
| 1363 | * been registered, the plugin is not added. |
||
| 1364 | * |
||
| 1365 | * @since 2.0.0 |
||
| 1366 | * |
||
| 1367 | * @param array|null $plugin Array of plugin arguments or null if invalid argument. |
||
| 1368 | * @return null Return early if incorrect argument. |
||
| 1369 | */ |
||
| 1370 | public function register( $plugin ) { |
||
| 1371 | if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) { |
||
| 1372 | return; |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | if ( empty( $plugin['slug'] ) || ! is_string( $plugin['slug'] ) || isset( $this->plugins[ $plugin['slug'] ] ) ) { |
||
| 1376 | return; |
||
| 1377 | } |
||
| 1378 | |||
| 1379 | $defaults = array( |
||
| 1380 | 'name' => '', // String |
||
| 1381 | 'slug' => '', // String |
||
| 1382 | 'source' => 'repo', // String |
||
| 1383 | 'required' => false, // Boolean |
||
| 1384 | 'version' => '', // String |
||
| 1385 | 'force_activation' => false, // Boolean |
||
| 1386 | 'force_deactivation' => false, // Boolean |
||
| 1387 | 'external_url' => '', // String |
||
| 1388 | 'is_callable' => '', // String|Array. |
||
| 1389 | ); |
||
| 1390 | |||
| 1391 | // Prepare the received data. |
||
| 1392 | $plugin = wp_parse_args( $plugin, $defaults ); |
||
| 1393 | |||
| 1394 | // Standardize the received slug. |
||
| 1395 | $plugin['slug'] = $this->sanitize_key( $plugin['slug'] ); |
||
| 1396 | |||
| 1397 | // Forgive users for using string versions of booleans or floats for version number. |
||
| 1398 | $plugin['version'] = (string) $plugin['version']; |
||
| 1399 | $plugin['source'] = empty( $plugin['source'] ) ? 'repo' : $plugin['source']; |
||
| 1400 | $plugin['required'] = TGMPA_Utils::validate_bool( $plugin['required'] ); |
||
| 1401 | $plugin['force_activation'] = TGMPA_Utils::validate_bool( $plugin['force_activation'] ); |
||
| 1402 | $plugin['force_deactivation'] = TGMPA_Utils::validate_bool( $plugin['force_deactivation'] ); |
||
| 1403 | |||
| 1404 | // Enrich the received data. |
||
| 1405 | $plugin['file_path'] = $this->_get_plugin_basename_from_slug( $plugin['slug'] ); |
||
| 1406 | $plugin['source_type'] = $this->get_plugin_source_type( $plugin['source'] ); |
||
| 1407 | |||
| 1408 | // Set the class properties. |
||
| 1409 | $this->plugins[ $plugin['slug'] ] = $plugin; |
||
| 1410 | $this->sort_order[ $plugin['slug'] ] = $plugin['name']; |
||
| 1411 | |||
| 1412 | // Should we add the force activation hook ? |
||
| 1413 | if ( true === $plugin['force_activation'] ) { |
||
| 1414 | $this->has_forced_activation = true; |
||
| 1415 | } |
||
| 1416 | |||
| 1417 | // Should we add the force deactivation hook ? |
||
| 1418 | if ( true === $plugin['force_deactivation'] ) { |
||
| 1419 | $this->has_forced_deactivation = true; |
||
| 1420 | } |
||
| 1421 | } |
||
| 1422 | |||
| 1423 | /** |
||
| 1424 | * Determine what type of source the plugin comes from. |
||
| 1425 | * |
||
| 1426 | * @since 2.5.0 |
||
| 1427 | * |
||
| 1428 | * @param string $source The source of the plugin as provided, either empty (= WP repo), a file path |
||
| 1429 | * (= bundled) or an external URL. |
||
| 1430 | * @return string 'repo', 'external', or 'bundled' |
||
| 1431 | */ |
||
| 1432 | protected function get_plugin_source_type( $source ) { |
||
| 1433 | if ( 'repo' === $source || preg_match( self::WP_REPO_REGEX, $source ) ) { |
||
| 1434 | return 'repo'; |
||
| 1435 | } elseif ( preg_match( self::IS_URL_REGEX, $source ) ) { |
||
| 1436 | return 'external'; |
||
| 1437 | } else { |
||
| 1438 | return 'bundled'; |
||
| 1439 | } |
||
| 1440 | } |
||
| 1441 | |||
| 1442 | /** |
||
| 1443 | * Sanitizes a string key. |
||
| 1444 | * |
||
| 1445 | * Near duplicate of WP Core `sanitize_key()`. The difference is that uppercase characters *are* |
||
| 1446 | * allowed, so as not to break upgrade paths from non-standard bundled plugins using uppercase |
||
| 1447 | * characters in the plugin directory path/slug. Silly them. |
||
| 1448 | * |
||
| 1449 | * @see https://developer.wordpress.org/reference/hooks/sanitize_key/ |
||
| 1450 | * |
||
| 1451 | * @since 2.5.0 |
||
| 1452 | * |
||
| 1453 | * @param string $key String key. |
||
| 1454 | * @return string Sanitized key |
||
| 1455 | */ |
||
| 1456 | public function sanitize_key( $key ) { |
||
| 1457 | $raw_key = $key; |
||
| 1458 | $key = preg_replace( '`[^A-Za-z0-9_-]`', '', $key ); |
||
| 1459 | |||
| 1460 | /** |
||
| 1461 | * Filter a sanitized key string. |
||
| 1462 | * |
||
| 1463 | * @since 2.5.0 |
||
| 1464 | * |
||
| 1465 | * @param string $key Sanitized key. |
||
| 1466 | * @param string $raw_key The key prior to sanitization. |
||
| 1467 | */ |
||
| 1468 | return apply_filters( 'tgmpa_sanitize_key', $key, $raw_key ); |
||
| 1469 | } |
||
| 1470 | |||
| 1471 | /** |
||
| 1472 | * Amend default configuration settings. |
||
| 1473 | * |
||
| 1474 | * @since 2.0.0 |
||
| 1475 | * |
||
| 1476 | * @param array $config Array of config options to pass as class properties. |
||
| 1477 | */ |
||
| 1478 | public function config( $config ) { |
||
| 1479 | $keys = array( |
||
| 1480 | 'id', |
||
| 1481 | 'default_path', |
||
| 1482 | 'has_notices', |
||
| 1483 | 'dismissable', |
||
| 1484 | 'dismiss_msg', |
||
| 1485 | 'menu', |
||
| 1486 | 'parent_slug', |
||
| 1487 | 'capability', |
||
| 1488 | 'is_automatic', |
||
| 1489 | 'message', |
||
| 1490 | 'strings', |
||
| 1491 | ); |
||
| 1492 | |||
| 1493 | foreach ( $keys as $key ) { |
||
| 1494 | if ( isset( $config[ $key ] ) ) { |
||
| 1495 | if ( is_array( $config[ $key ] ) ) { |
||
| 1496 | $this->$key = array_merge( $this->$key, $config[ $key ] ); |
||
| 1497 | } else { |
||
| 1498 | $this->$key = $config[ $key ]; |
||
| 1499 | } |
||
| 1500 | } |
||
| 1501 | } |
||
| 1502 | } |
||
| 1503 | |||
| 1504 | /** |
||
| 1505 | * Amend action link after plugin installation. |
||
| 1506 | * |
||
| 1507 | * @since 2.0.0 |
||
| 1508 | * |
||
| 1509 | * @param array $install_actions Existing array of actions. |
||
| 1510 | * @return false|array Amended array of actions. |
||
| 1511 | */ |
||
| 1512 | public function actions( $install_actions ) { |
||
| 1513 | // Remove action links on the TGMPA install page. |
||
| 1514 | if ( $this->is_tgmpa_page() ) { |
||
| 1515 | return false; |
||
| 1516 | } |
||
| 1517 | |||
| 1518 | return $install_actions; |
||
| 1519 | } |
||
| 1520 | |||
| 1521 | /** |
||
| 1522 | * Flushes the plugins cache on theme switch to prevent stale entries |
||
| 1523 | * from remaining in the plugin table. |
||
| 1524 | * |
||
| 1525 | * @since 2.4.0 |
||
| 1526 | * |
||
| 1527 | * @param bool $clear_update_cache Optional. Whether to clear the Plugin updates cache. |
||
| 1528 | * Parameter added in v2.5.0. |
||
| 1529 | */ |
||
| 1530 | public function flush_plugins_cache( $clear_update_cache = true ) { |
||
| 1531 | wp_clean_plugins_cache( $clear_update_cache ); |
||
| 1532 | } |
||
| 1533 | |||
| 1534 | /** |
||
| 1535 | * Set file_path key for each installed plugin. |
||
| 1536 | * |
||
| 1537 | * @since 2.1.0 |
||
| 1538 | * |
||
| 1539 | * @param string $plugin_slug Optional. If set, only (re-)populates the file path for that specific plugin. |
||
| 1540 | * Parameter added in v2.5.0. |
||
| 1541 | */ |
||
| 1542 | public function populate_file_path( $plugin_slug = '' ) { |
||
| 1543 | if ( ! empty( $plugin_slug ) && is_string( $plugin_slug ) && isset( $this->plugins[ $plugin_slug ] ) ) { |
||
| 1544 | $this->plugins[ $plugin_slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $plugin_slug ); |
||
| 1545 | } else { |
||
| 1546 | // Add file_path key for all plugins. |
||
| 1547 | foreach ( $this->plugins as $slug => $values ) { |
||
| 1548 | $this->plugins[ $slug ]['file_path'] = $this->_get_plugin_basename_from_slug( $slug ); |
||
| 1549 | } |
||
| 1550 | } |
||
| 1551 | } |
||
| 1552 | |||
| 1553 | /** |
||
| 1554 | * Helper function to extract the file path of the plugin file from the |
||
| 1555 | * plugin slug, if the plugin is installed. |
||
| 1556 | * |
||
| 1557 | * @since 2.0.0 |
||
| 1558 | * |
||
| 1559 | * @param string $slug Plugin slug (typically folder name) as provided by the developer. |
||
| 1560 | * @return string Either file path for plugin if installed, or just the plugin slug. |
||
| 1561 | */ |
||
| 1562 | protected function _get_plugin_basename_from_slug( $slug ) { |
||
| 1563 | $keys = array_keys( $this->get_plugins() ); |
||
| 1564 | |||
| 1565 | foreach ( $keys as $key ) { |
||
| 1566 | if ( preg_match( '|^' . $slug . '/|', $key ) ) { |
||
| 1567 | return $key; |
||
| 1568 | } |
||
| 1569 | } |
||
| 1570 | |||
| 1571 | return $slug; |
||
| 1572 | } |
||
| 1573 | |||
| 1574 | /** |
||
| 1575 | * Retrieve plugin data, given the plugin name. |
||
| 1576 | * |
||
| 1577 | * Loops through the registered plugins looking for $name. If it finds it, |
||
| 1578 | * it returns the $data from that plugin. Otherwise, returns false. |
||
| 1579 | * |
||
| 1580 | * @since 2.1.0 |
||
| 1581 | * |
||
| 1582 | * @param string $name Name of the plugin, as it was registered. |
||
| 1583 | * @param string $data Optional. Array key of plugin data to return. Default is slug. |
||
| 1584 | * @return string|boolean Plugin slug if found, false otherwise. |
||
| 1585 | */ |
||
| 1586 | public function _get_plugin_data_from_name( $name, $data = 'slug' ) { |
||
| 1587 | foreach ( $this->plugins as $values ) { |
||
| 1588 | if ( $name === $values['name'] && isset( $values[ $data ] ) ) { |
||
| 1589 | return $values[ $data ]; |
||
| 1590 | } |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | return false; |
||
| 1594 | } |
||
| 1595 | |||
| 1596 | /** |
||
| 1597 | * Retrieve the download URL for a package. |
||
| 1598 | * |
||
| 1599 | * @since 2.5.0 |
||
| 1600 | * |
||
| 1601 | * @param string $slug Plugin slug. |
||
| 1602 | * @return string Plugin download URL or path to local file or empty string if undetermined. |
||
| 1603 | */ |
||
| 1604 | public function get_download_url( $slug ) { |
||
| 1605 | $dl_source = ''; |
||
| 1606 | |||
| 1607 | switch ( $this->plugins[ $slug ]['source_type'] ) { |
||
| 1608 | case 'repo': |
||
| 1609 | return $this->get_wp_repo_download_url( $slug ); |
||
| 1610 | case 'external': |
||
| 1611 | return $this->plugins[ $slug ]['source']; |
||
| 1612 | case 'bundled': |
||
| 1613 | return $this->default_path . $this->plugins[ $slug ]['source']; |
||
| 1614 | } |
||
| 1615 | |||
| 1616 | return $dl_source; // Should never happen. |
||
| 1617 | } |
||
| 1618 | |||
| 1619 | /** |
||
| 1620 | * Retrieve the download URL for a WP repo package. |
||
| 1621 | * |
||
| 1622 | * @since 2.5.0 |
||
| 1623 | * |
||
| 1624 | * @param string $slug Plugin slug. |
||
| 1625 | * @return string Plugin download URL. |
||
| 1626 | */ |
||
| 1627 | protected function get_wp_repo_download_url( $slug ) { |
||
| 1628 | $source = ''; |
||
| 1629 | $api = $this->get_plugins_api( $slug ); |
||
| 1630 | |||
| 1631 | if ( false !== $api && isset( $api->download_link ) ) { |
||
| 1632 | $source = $api->download_link; |
||
| 1633 | } |
||
| 1634 | |||
| 1635 | return $source; |
||
| 1636 | } |
||
| 1637 | |||
| 1638 | /** |
||
| 1639 | * Try to grab information from WordPress API. |
||
| 1640 | * |
||
| 1641 | * @since 2.5.0 |
||
| 1642 | * |
||
| 1643 | * @param string $slug Plugin slug. |
||
| 1644 | * @return object Plugins_api response object on success, WP_Error on failure. |
||
| 1645 | */ |
||
| 1646 | protected function get_plugins_api( $slug ) { |
||
| 1647 | static $api = array(); // Cache received responses. |
||
| 1648 | |||
| 1649 | if ( ! isset( $api[ $slug ] ) ) { |
||
| 1650 | if ( ! function_exists( 'plugins_api' ) ) { |
||
| 1651 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
||
| 1652 | } |
||
| 1653 | |||
| 1654 | $response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) ); |
||
| 1655 | |||
| 1656 | $api[ $slug ] = false; |
||
| 1657 | |||
| 1658 | if ( is_wp_error( $response ) ) { |
||
| 1659 | wp_die( esc_html( $this->strings['oops'] ) ); |
||
| 1660 | } else { |
||
| 1661 | $api[ $slug ] = $response; |
||
| 1662 | } |
||
| 1663 | } |
||
| 1664 | |||
| 1665 | return $api[ $slug ]; |
||
| 1666 | } |
||
| 1667 | |||
| 1668 | /** |
||
| 1669 | * Retrieve a link to a plugin information page. |
||
| 1670 | * |
||
| 1671 | * @since 2.5.0 |
||
| 1672 | * |
||
| 1673 | * @param string $slug Plugin slug. |
||
| 1674 | * @return string Fully formed html link to a plugin information page if available |
||
| 1675 | * or the plugin name if not. |
||
| 1676 | */ |
||
| 1677 | public function get_info_link( $slug ) { |
||
| 1678 | if ( ! empty( $this->plugins[ $slug ]['external_url'] ) && preg_match( self::IS_URL_REGEX, $this->plugins[ $slug ]['external_url'] ) ) { |
||
| 1679 | $link = sprintf( |
||
| 1680 | '<a href="%1$s" target="_blank">%2$s</a>', |
||
| 1681 | esc_url( $this->plugins[ $slug ]['external_url'] ), |
||
| 1682 | esc_html( $this->plugins[ $slug ]['name'] ) |
||
| 1683 | ); |
||
| 1684 | } elseif ( 'repo' === $this->plugins[ $slug ]['source_type'] ) { |
||
| 1685 | $url = add_query_arg( |
||
| 1686 | array( |
||
| 1687 | 'tab' => 'plugin-information', |
||
| 1688 | 'plugin' => urlencode( $slug ), |
||
| 1689 | 'TB_iframe' => 'true', |
||
| 1690 | 'width' => '640', |
||
| 1691 | 'height' => '500', |
||
| 1692 | ), |
||
| 1693 | self_admin_url( 'plugin-install.php' ) |
||
| 1694 | ); |
||
| 1695 | |||
| 1696 | $link = sprintf( |
||
| 1697 | '<a href="%1$s" class="thickbox">%2$s</a>', |
||
| 1698 | esc_url( $url ), |
||
| 1699 | esc_html( $this->plugins[ $slug ]['name'] ) |
||
| 1700 | ); |
||
| 1701 | } else { |
||
| 1702 | $link = esc_html( $this->plugins[ $slug ]['name'] ); // No hyperlink. |
||
| 1703 | } |
||
| 1704 | |||
| 1705 | return $link; |
||
| 1706 | } |
||
| 1707 | |||
| 1708 | /** |
||
| 1709 | * Determine if we're on the TGMPA Install page. |
||
| 1710 | * |
||
| 1711 | * @since 2.1.0 |
||
| 1712 | * |
||
| 1713 | * @return boolean True when on the TGMPA page, false otherwise. |
||
| 1714 | */ |
||
| 1715 | protected function is_tgmpa_page() { |
||
| 1716 | return isset( $_GET['page'] ) && $this->menu === $_GET['page']; |
||
| 1717 | } |
||
| 1718 | |||
| 1719 | /** |
||
| 1720 | * Determine if we're on a WP Core installation/upgrade page. |
||
| 1721 | * |
||
| 1722 | * @since 2.6.0 |
||
| 1723 | * |
||
| 1724 | * @return boolean True when on a WP Core installation/upgrade page, false otherwise. |
||
| 1725 | */ |
||
| 1726 | protected function is_core_update_page() { |
||
| 1727 | // Current screen is not always available, most notably on the customizer screen. |
||
| 1728 | if ( ! function_exists( 'get_current_screen' ) ) { |
||
| 1729 | return false; |
||
| 1730 | } |
||
| 1731 | |||
| 1732 | $screen = get_current_screen(); |
||
| 1733 | |||
| 1734 | if ( 'update-core' === $screen->base ) { |
||
| 1735 | // Core update screen. |
||
| 1736 | return true; |
||
| 1737 | } elseif ( 'plugins' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok. |
||
| 1738 | // Plugins bulk update screen. |
||
| 1739 | return true; |
||
| 1740 | } elseif ( 'update' === $screen->base && ! empty( $_POST['action'] ) ) { // WPCS: CSRF ok. |
||
| 1741 | // Individual updates (ajax call). |
||
| 1742 | return true; |
||
| 1743 | } |
||
| 1744 | |||
| 1745 | return false; |
||
| 1746 | } |
||
| 1747 | |||
| 1748 | /** |
||
| 1749 | * Retrieve the URL to the TGMPA Install page. |
||
| 1750 | * |
||
| 1751 | * I.e. depending on the config settings passed something along the lines of: |
||
| 1752 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins |
||
| 1753 | * |
||
| 1754 | * @since 2.5.0 |
||
| 1755 | * |
||
| 1756 | * @return string Properly encoded URL (not escaped). |
||
| 1757 | */ |
||
| 1758 | public function get_tgmpa_url() { |
||
| 1759 | static $url; |
||
| 1760 | |||
| 1761 | if ( ! isset( $url ) ) { |
||
| 1762 | $parent = $this->parent_slug; |
||
| 1763 | if ( false === strpos( $parent, '.php' ) ) { |
||
| 1764 | $parent = 'admin.php'; |
||
| 1765 | } |
||
| 1766 | $url = add_query_arg( |
||
| 1767 | array( |
||
| 1768 | 'page' => urlencode( $this->menu ), |
||
| 1769 | ), |
||
| 1770 | self_admin_url( $parent ) |
||
| 1771 | ); |
||
| 1772 | } |
||
| 1773 | |||
| 1774 | return $url; |
||
| 1775 | } |
||
| 1776 | |||
| 1777 | /** |
||
| 1778 | * Retrieve the URL to the TGMPA Install page for a specific plugin status (view). |
||
| 1779 | * |
||
| 1780 | * I.e. depending on the config settings passed something along the lines of: |
||
| 1781 | * http://example.com/wp-admin/themes.php?page=tgmpa-install-plugins&plugin_status=install |
||
| 1782 | * |
||
| 1783 | * @since 2.5.0 |
||
| 1784 | * |
||
| 1785 | * @param string $status Plugin status - either 'install', 'update' or 'activate'. |
||
| 1786 | * @return string Properly encoded URL (not escaped). |
||
| 1787 | */ |
||
| 1788 | public function get_tgmpa_status_url( $status ) { |
||
| 1789 | return add_query_arg( |
||
| 1790 | array( |
||
| 1791 | 'plugin_status' => urlencode( $status ), |
||
| 1792 | ), |
||
| 1793 | $this->get_tgmpa_url() |
||
| 1794 | ); |
||
| 1795 | } |
||
| 1796 | |||
| 1797 | /** |
||
| 1798 | * Determine whether there are open actions for plugins registered with TGMPA. |
||
| 1799 | * |
||
| 1800 | * @since 2.5.0 |
||
| 1801 | * |
||
| 1802 | * @return bool True if complete, i.e. no outstanding actions. False otherwise. |
||
| 1803 | */ |
||
| 1804 | public function is_tgmpa_complete() { |
||
| 1805 | $complete = true; |
||
| 1806 | foreach ( $this->plugins as $slug => $plugin ) { |
||
| 1807 | if ( ! $this->is_plugin_active( $slug ) || false !== $this->does_plugin_have_update( $slug ) ) { |
||
| 1808 | $complete = false; |
||
| 1809 | break; |
||
| 1810 | } |
||
| 1811 | } |
||
| 1812 | |||
| 1813 | return $complete; |
||
| 1814 | } |
||
| 1815 | |||
| 1816 | /** |
||
| 1817 | * Check if a plugin is installed. Does not take must-use plugins into account. |
||
| 1818 | * |
||
| 1819 | * @since 2.5.0 |
||
| 1820 | * |
||
| 1821 | * @param string $slug Plugin slug. |
||
| 1822 | * @return bool True if installed, false otherwise. |
||
| 1823 | */ |
||
| 1824 | public function is_plugin_installed( $slug ) { |
||
| 1825 | $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). |
||
| 1826 | |||
| 1827 | return ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ] ) ); |
||
| 1828 | } |
||
| 1829 | |||
| 1830 | /** |
||
| 1831 | * Check if a plugin is active. |
||
| 1832 | * |
||
| 1833 | * @since 2.5.0 |
||
| 1834 | * |
||
| 1835 | * @param string $slug Plugin slug. |
||
| 1836 | * @return bool True if active, false otherwise. |
||
| 1837 | */ |
||
| 1838 | public function is_plugin_active( $slug ) { |
||
| 1839 | return ( ( ! empty( $this->plugins[ $slug ]['is_callable'] ) && is_callable( $this->plugins[ $slug ]['is_callable'] ) ) || is_plugin_active( $this->plugins[ $slug ]['file_path'] ) ); |
||
| 1840 | } |
||
| 1841 | |||
| 1842 | /** |
||
| 1843 | * Check if a plugin can be updated, i.e. if we have information on the minimum WP version required |
||
| 1844 | * available, check whether the current install meets them. |
||
| 1845 | * |
||
| 1846 | * @since 2.5.0 |
||
| 1847 | * |
||
| 1848 | * @param string $slug Plugin slug. |
||
| 1849 | * @return bool True if OK to update, false otherwise. |
||
| 1850 | */ |
||
| 1851 | public function can_plugin_update( $slug ) { |
||
| 1852 | // We currently can't get reliable info on non-WP-repo plugins - issue #380. |
||
| 1853 | if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { |
||
| 1854 | return true; |
||
| 1855 | } |
||
| 1856 | |||
| 1857 | $api = $this->get_plugins_api( $slug ); |
||
| 1858 | |||
| 1859 | if ( false !== $api && isset( $api->requires ) ) { |
||
| 1860 | return version_compare( $this->wp_version, $api->requires, '>=' ); |
||
| 1861 | } |
||
| 1862 | |||
| 1863 | // No usable info received from the plugins API, presume we can update. |
||
| 1864 | return true; |
||
| 1865 | } |
||
| 1866 | |||
| 1867 | /** |
||
| 1868 | * Check to see if the plugin is 'updatetable', i.e. installed, with an update available |
||
| 1869 | * and no WP version requirements blocking it. |
||
| 1870 | * |
||
| 1871 | * @since 2.6.0 |
||
| 1872 | * |
||
| 1873 | * @param string $slug Plugin slug. |
||
| 1874 | * @return bool True if OK to proceed with update, false otherwise. |
||
| 1875 | */ |
||
| 1876 | public function is_plugin_updatetable( $slug ) { |
||
| 1877 | if ( ! $this->is_plugin_installed( $slug ) ) { |
||
| 1878 | return false; |
||
| 1879 | } else { |
||
| 1880 | return ( false !== $this->does_plugin_have_update( $slug ) && $this->can_plugin_update( $slug ) ); |
||
| 1881 | } |
||
| 1882 | } |
||
| 1883 | |||
| 1884 | /** |
||
| 1885 | * Check if a plugin can be activated, i.e. is not currently active and meets the minimum |
||
| 1886 | * plugin version requirements set in TGMPA (if any). |
||
| 1887 | * |
||
| 1888 | * @since 2.5.0 |
||
| 1889 | * |
||
| 1890 | * @param string $slug Plugin slug. |
||
| 1891 | * @return bool True if OK to activate, false otherwise. |
||
| 1892 | */ |
||
| 1893 | public function can_plugin_activate( $slug ) { |
||
| 1894 | return ( ! $this->is_plugin_active( $slug ) && ! $this->does_plugin_require_update( $slug ) ); |
||
| 1895 | } |
||
| 1896 | |||
| 1897 | /** |
||
| 1898 | * Retrieve the version number of an installed plugin. |
||
| 1899 | * |
||
| 1900 | * @since 2.5.0 |
||
| 1901 | * |
||
| 1902 | * @param string $slug Plugin slug. |
||
| 1903 | * @return string Version number as string or an empty string if the plugin is not installed |
||
| 1904 | * or version unknown (plugins which don't comply with the plugin header standard). |
||
| 1905 | */ |
||
| 1906 | public function get_installed_version( $slug ) { |
||
| 1907 | $installed_plugins = $this->get_plugins(); // Retrieve a list of all installed plugins (WP cached). |
||
| 1908 | |||
| 1909 | if ( ! empty( $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version'] ) ) { |
||
| 1910 | return $installed_plugins[ $this->plugins[ $slug ]['file_path'] ]['Version']; |
||
| 1911 | } |
||
| 1912 | |||
| 1913 | return ''; |
||
| 1914 | } |
||
| 1915 | |||
| 1916 | /** |
||
| 1917 | * Check whether a plugin complies with the minimum version requirements. |
||
| 1918 | * |
||
| 1919 | * @since 2.5.0 |
||
| 1920 | * |
||
| 1921 | * @param string $slug Plugin slug. |
||
| 1922 | * @return bool True when a plugin needs to be updated, otherwise false. |
||
| 1923 | */ |
||
| 1924 | public function does_plugin_require_update( $slug ) { |
||
| 1925 | $installed_version = $this->get_installed_version( $slug ); |
||
| 1926 | $minimum_version = $this->plugins[ $slug ]['version']; |
||
| 1927 | |||
| 1928 | return version_compare( $minimum_version, $installed_version, '>' ); |
||
| 1929 | } |
||
| 1930 | |||
| 1931 | /** |
||
| 1932 | * Check whether there is an update available for a plugin. |
||
| 1933 | * |
||
| 1934 | * @since 2.5.0 |
||
| 1935 | * |
||
| 1936 | * @param string $slug Plugin slug. |
||
| 1937 | * @return false|string Version number string of the available update or false if no update available. |
||
| 1938 | */ |
||
| 1939 | public function does_plugin_have_update( $slug ) { |
||
| 1940 | // Presume bundled and external plugins will point to a package which meets the minimum required version. |
||
| 1941 | if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { |
||
| 1942 | if ( $this->does_plugin_require_update( $slug ) ) { |
||
| 1943 | return $this->plugins[ $slug ]['version']; |
||
| 1944 | } |
||
| 1945 | |||
| 1946 | return false; |
||
| 1947 | } |
||
| 1948 | |||
| 1949 | $repo_updates = get_site_transient( 'update_plugins' ); |
||
| 1950 | |||
| 1951 | View Code Duplication | if ( isset( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version ) ) { |
|
| 1952 | return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->new_version; |
||
| 1953 | } |
||
| 1954 | |||
| 1955 | return false; |
||
| 1956 | } |
||
| 1957 | |||
| 1958 | /** |
||
| 1959 | * Retrieve potential upgrade notice for a plugin. |
||
| 1960 | * |
||
| 1961 | * @since 2.5.0 |
||
| 1962 | * |
||
| 1963 | * @param string $slug Plugin slug. |
||
| 1964 | * @return string The upgrade notice or an empty string if no message was available or provided. |
||
| 1965 | */ |
||
| 1966 | public function get_upgrade_notice( $slug ) { |
||
| 1967 | // We currently can't get reliable info on non-WP-repo plugins - issue #380. |
||
| 1968 | if ( 'repo' !== $this->plugins[ $slug ]['source_type'] ) { |
||
| 1969 | return ''; |
||
| 1970 | } |
||
| 1971 | |||
| 1972 | $repo_updates = get_site_transient( 'update_plugins' ); |
||
| 1973 | |||
| 1974 | View Code Duplication | if ( ! empty( $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice ) ) { |
|
| 1975 | return $repo_updates->response[ $this->plugins[ $slug ]['file_path'] ]->upgrade_notice; |
||
| 1976 | } |
||
| 1977 | |||
| 1978 | return ''; |
||
| 1979 | } |
||
| 1980 | |||
| 1981 | /** |
||
| 1982 | * Wrapper around the core WP get_plugins function, making sure it's actually available. |
||
| 1983 | * |
||
| 1984 | * @since 2.5.0 |
||
| 1985 | * |
||
| 1986 | * @param string $plugin_folder Optional. Relative path to single plugin folder. |
||
| 1987 | * @return array Array of installed plugins with plugin information. |
||
| 1988 | */ |
||
| 1989 | public function get_plugins( $plugin_folder = '' ) { |
||
| 1990 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 1991 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
| 1992 | } |
||
| 1993 | |||
| 1994 | return get_plugins( $plugin_folder ); |
||
| 1995 | } |
||
| 1996 | |||
| 1997 | /** |
||
| 1998 | * Delete dismissable nag option when theme is switched. |
||
| 1999 | * |
||
| 2000 | * This ensures that the user(s) is/are again reminded via nag of required |
||
| 2001 | * and/or recommended plugins if they re-activate the theme. |
||
| 2002 | * |
||
| 2003 | * @since 2.1.1 |
||
| 2004 | */ |
||
| 2005 | public function update_dismiss() { |
||
| 2006 | delete_metadata( 'user', null, 'tgmpa_dismissed_notice_' . $this->id, null, true ); |
||
| 2007 | } |
||
| 2008 | |||
| 2009 | /** |
||
| 2010 | * Forces plugin activation if the parameter 'force_activation' is |
||
| 2011 | * set to true. |
||
| 2012 | * |
||
| 2013 | * This allows theme authors to specify certain plugins that must be |
||
| 2014 | * active at all times while using the current theme. |
||
| 2015 | * |
||
| 2016 | * Please take special care when using this parameter as it has the |
||
| 2017 | * potential to be harmful if not used correctly. Setting this parameter |
||
| 2018 | * to true will not allow the specified plugin to be deactivated unless |
||
| 2019 | * the user switches themes. |
||
| 2020 | * |
||
| 2021 | * @since 2.2.0 |
||
| 2022 | */ |
||
| 2023 | public function force_activation() { |
||
| 2024 | foreach ( $this->plugins as $slug => $plugin ) { |
||
| 2025 | if ( true === $plugin['force_activation'] ) { |
||
| 2026 | if ( ! $this->is_plugin_installed( $slug ) ) { |
||
| 2027 | // Oops, plugin isn't there so iterate to next condition. |
||
| 2028 | continue; |
||
| 2029 | } elseif ( $this->can_plugin_activate( $slug ) ) { |
||
| 2030 | // There we go, activate the plugin. |
||
| 2031 | activate_plugin( $plugin['file_path'] ); |
||
| 2032 | } |
||
| 2033 | } |
||
| 2034 | } |
||
| 2035 | } |
||
| 2036 | |||
| 2037 | /** |
||
| 2038 | * Forces plugin deactivation if the parameter 'force_deactivation' |
||
| 2039 | * is set to true and adds the plugin to the 'recently active' plugins list. |
||
| 2040 | * |
||
| 2041 | * This allows theme authors to specify certain plugins that must be |
||
| 2042 | * deactivated upon switching from the current theme to another. |
||
| 2043 | * |
||
| 2044 | * Please take special care when using this parameter as it has the |
||
| 2045 | * potential to be harmful if not used correctly. |
||
| 2046 | * |
||
| 2047 | * @since 2.2.0 |
||
| 2048 | */ |
||
| 2049 | public function force_deactivation() { |
||
| 2050 | $deactivated = array(); |
||
| 2051 | |||
| 2052 | foreach ( $this->plugins as $slug => $plugin ) { |
||
| 2053 | /* |
||
| 2054 | * Only proceed forward if the parameter is set to true and plugin is active |
||
| 2055 | * as a 'normal' (not must-use) plugin. |
||
| 2056 | */ |
||
| 2057 | if ( true === $plugin['force_deactivation'] && is_plugin_active( $plugin['file_path'] ) ) { |
||
| 2058 | deactivate_plugins( $plugin['file_path'] ); |
||
| 2059 | $deactivated[ $plugin['file_path'] ] = time(); |
||
| 2060 | } |
||
| 2061 | } |
||
| 2062 | |||
| 2063 | if ( ! empty( $deactivated ) ) { |
||
| 2064 | update_option( 'recently_activated', $deactivated + (array) get_option( 'recently_activated' ) ); |
||
| 2065 | } |
||
| 2066 | } |
||
| 2067 | |||
| 2068 | /** |
||
| 2069 | * Echo the current TGMPA version number to the page. |
||
| 2070 | * |
||
| 2071 | * @since 2.5.0 |
||
| 2072 | */ |
||
| 2073 | public function show_tgmpa_version() { |
||
| 2074 | echo '<p style="float: right; padding: 0em 1.5em 0.5em 0;"><strong><small>', |
||
| 2075 | esc_html( |
||
| 2076 | sprintf( |
||
| 2077 | /* translators: %s: version number */ |
||
| 2078 | __( 'TGMPA v%s', 'tgmpa' ), |
||
| 2079 | self::TGMPA_VERSION |
||
| 2080 | ) |
||
| 2081 | ), |
||
| 2082 | '</small></strong></p>'; |
||
| 2083 | } |
||
| 2084 | |||
| 2085 | /** |
||
| 2086 | * Adds CSS to admin head. |
||
| 2087 | * |
||
| 2088 | * @since 2.6.2 |
||
| 2089 | */ |
||
| 2090 | function admin_css() { |
||
|
0 ignored issues
–
show
|
|||
| 2091 | if ( ! $this->is_tgmpa_page() ) { |
||
| 2092 | return; |
||
| 2093 | } |
||
| 2094 | |||
| 2095 | echo ' |
||
| 2096 | <style> |
||
| 2097 | #tgmpa-plugins .tgmpa-type-required > th { |
||
| 2098 | border-left: 3px solid #dc3232; |
||
| 2099 | } |
||
| 2100 | </style>'; |
||
| 2101 | } |
||
| 2102 | |||
| 2103 | /** |
||
| 2104 | * Returns the singleton instance of the class. |
||
| 2105 | * |
||
| 2106 | * @since 2.4.0 |
||
| 2107 | * |
||
| 2108 | * @return \TGM_Plugin_Activation The TGM_Plugin_Activation object. |
||
| 2109 | */ |
||
| 2110 | public static function get_instance() { |
||
| 2111 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof self ) ) { |
||
| 2112 | self::$instance = new self(); |
||
| 2113 | } |
||
| 2114 | |||
| 2115 | return self::$instance; |
||
| 2116 | } |
||
| 2117 | } |
||
| 2118 | |||
| 2119 | if ( ! function_exists( 'load_tgm_plugin_activation' ) ) { |
||
| 2120 | /** |
||
| 2121 | * Ensure only one instance of the class is ever invoked. |
||
| 2122 | * |
||
| 2123 | * @since 2.5.0 |
||
| 2124 | */ |
||
| 2125 | function load_tgm_plugin_activation() { |
||
| 2126 | $GLOBALS['tgmpa'] = TGM_Plugin_Activation::get_instance(); |
||
| 2127 | } |
||
| 2128 | } |
||
| 2129 | |||
| 2130 | if ( did_action( 'plugins_loaded' ) ) { |
||
| 2131 | load_tgm_plugin_activation(); |
||
| 2132 | } else { |
||
| 2133 | add_action( 'plugins_loaded', 'load_tgm_plugin_activation' ); |
||
| 2134 | } |
||
| 2135 | } |
||
| 2136 | |||
| 2137 | if ( ! function_exists( 'tgmpa' ) ) { |
||
| 2138 | /** |
||
| 2139 | * Helper function to register a collection of required plugins. |
||
| 2140 | * |
||
| 2141 | * @since 2.0.0 |
||
| 2142 | * @api |
||
| 2143 | * |
||
| 2144 | * @param array $plugins An array of plugin arrays. |
||
| 2145 | * @param array $config Optional. An array of configuration values. |
||
| 2146 | */ |
||
| 2147 | function tgmpa( $plugins, $config = array() ) { |
||
| 2148 | $instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); |
||
| 2149 | |||
| 2150 | foreach ( $plugins as $plugin ) { |
||
| 2151 | call_user_func( array( $instance, 'register' ), $plugin ); |
||
| 2152 | } |
||
| 2153 | |||
| 2154 | if ( ! empty( $config ) && is_array( $config ) ) { |
||
| 2155 | // Send out notices for deprecated arguments passed. |
||
| 2156 | if ( isset( $config['notices'] ) ) { |
||
| 2157 | _deprecated_argument( __FUNCTION__, '2.2.0', 'The `notices` config parameter was renamed to `has_notices` in TGMPA 2.2.0. Please adjust your configuration.' ); |
||
| 2158 | if ( ! isset( $config['has_notices'] ) ) { |
||
| 2159 | $config['has_notices'] = $config['notices']; |
||
| 2160 | } |
||
| 2161 | } |
||
| 2162 | |||
| 2163 | if ( isset( $config['parent_menu_slug'] ) ) { |
||
| 2164 | _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_menu_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' ); |
||
| 2165 | } |
||
| 2166 | if ( isset( $config['parent_url_slug'] ) ) { |
||
| 2167 | _deprecated_argument( __FUNCTION__, '2.4.0', 'The `parent_url_slug` config parameter was removed in TGMPA 2.4.0. In TGMPA 2.5.0 an alternative was (re-)introduced. Please adjust your configuration. For more information visit the website: http://tgmpluginactivation.com/configuration/#h-configuration-options.' ); |
||
| 2168 | } |
||
| 2169 | |||
| 2170 | call_user_func( array( $instance, 'config' ), $config ); |
||
| 2171 | } |
||
| 2172 | } |
||
| 2173 | } |
||
| 2174 | |||
| 2175 | /** |
||
| 2176 | * WP_List_Table isn't always available. If it isn't available, |
||
| 2177 | * we load it here. |
||
| 2178 | * |
||
| 2179 | * @since 2.2.0 |
||
| 2180 | */ |
||
| 2181 | if ( ! class_exists( 'WP_List_Table' ) ) { |
||
| 2182 | require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php'; |
||
| 2183 | } |
||
| 2184 | |||
| 2185 | if ( ! class_exists( 'TGMPA_List_Table' ) ) { |
||
| 2186 | |||
| 2187 | /** |
||
| 2188 | * List table class for handling plugins. |
||
| 2189 | * |
||
| 2190 | * Extends the WP_List_Table class to provide a future-compatible |
||
| 2191 | * way of listing out all required/recommended plugins. |
||
| 2192 | * |
||
| 2193 | * Gives users an interface similar to the Plugin Administration |
||
| 2194 | * area with similar (albeit stripped down) capabilities. |
||
| 2195 | * |
||
| 2196 | * This class also allows for the bulk install of plugins. |
||
| 2197 | * |
||
| 2198 | * @since 2.2.0 |
||
| 2199 | * |
||
| 2200 | * @package TGM-Plugin-Activation |
||
| 2201 | * @author Thomas Griffin |
||
| 2202 | * @author Gary Jones |
||
| 2203 | */ |
||
| 2204 | class TGMPA_List_Table extends WP_List_Table { |
||
| 2205 | /** |
||
| 2206 | * TGMPA instance. |
||
| 2207 | * |
||
| 2208 | * @since 2.5.0 |
||
| 2209 | * |
||
| 2210 | * @var object |
||
| 2211 | */ |
||
| 2212 | protected $tgmpa; |
||
| 2213 | |||
| 2214 | /** |
||
| 2215 | * The currently chosen view. |
||
| 2216 | * |
||
| 2217 | * @since 2.5.0 |
||
| 2218 | * |
||
| 2219 | * @var string One of: 'all', 'install', 'update', 'activate' |
||
| 2220 | */ |
||
| 2221 | public $view_context = 'all'; |
||
| 2222 | |||
| 2223 | /** |
||
| 2224 | * The plugin counts for the various views. |
||
| 2225 | * |
||
| 2226 | * @since 2.5.0 |
||
| 2227 | * |
||
| 2228 | * @var array |
||
| 2229 | */ |
||
| 2230 | protected $view_totals = array( |
||
| 2231 | 'all' => 0, |
||
| 2232 | 'install' => 0, |
||
| 2233 | 'update' => 0, |
||
| 2234 | 'activate' => 0, |
||
| 2235 | ); |
||
| 2236 | |||
| 2237 | /** |
||
| 2238 | * References parent constructor and sets defaults for class. |
||
| 2239 | * |
||
| 2240 | * @since 2.2.0 |
||
| 2241 | */ |
||
| 2242 | public function __construct() { |
||
| 2243 | $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); |
||
| 2244 | |||
| 2245 | parent::__construct( |
||
| 2246 | array( |
||
| 2247 | 'singular' => 'plugin', |
||
| 2248 | 'plural' => 'plugins', |
||
| 2249 | 'ajax' => false, |
||
| 2250 | ) |
||
| 2251 | ); |
||
| 2252 | |||
| 2253 | if ( isset( $_REQUEST['plugin_status'] ) && in_array( $_REQUEST['plugin_status'], array( 'install', 'update', 'activate' ), true ) ) { |
||
| 2254 | $this->view_context = sanitize_key( $_REQUEST['plugin_status'] ); |
||
| 2255 | } |
||
| 2256 | |||
| 2257 | add_filter( 'tgmpa_table_data_items', array( $this, 'sort_table_items' ) ); |
||
| 2258 | } |
||
| 2259 | |||
| 2260 | /** |
||
| 2261 | * Get a list of CSS classes for the <table> tag. |
||
| 2262 | * |
||
| 2263 | * Overruled to prevent the 'plural' argument from being added. |
||
| 2264 | * |
||
| 2265 | * @since 2.5.0 |
||
| 2266 | * |
||
| 2267 | * @return array CSS classnames. |
||
| 2268 | */ |
||
| 2269 | public function get_table_classes() { |
||
| 2270 | return array( 'widefat', 'fixed' ); |
||
| 2271 | } |
||
| 2272 | |||
| 2273 | /** |
||
| 2274 | * Gathers and renames all of our plugin information to be used by WP_List_Table to create our table. |
||
| 2275 | * |
||
| 2276 | * @since 2.2.0 |
||
| 2277 | * |
||
| 2278 | * @return array $table_data Information for use in table. |
||
| 2279 | */ |
||
| 2280 | protected function _gather_plugin_data() { |
||
| 2281 | // Load thickbox for plugin links. |
||
| 2282 | $this->tgmpa->admin_init(); |
||
| 2283 | $this->tgmpa->thickbox(); |
||
| 2284 | |||
| 2285 | // Categorize the plugins which have open actions. |
||
| 2286 | $plugins = $this->categorize_plugins_to_views(); |
||
| 2287 | |||
| 2288 | // Set the counts for the view links. |
||
| 2289 | $this->set_view_totals( $plugins ); |
||
| 2290 | |||
| 2291 | // Prep variables for use and grab list of all installed plugins. |
||
| 2292 | $table_data = array(); |
||
| 2293 | $i = 0; |
||
| 2294 | |||
| 2295 | // Redirect to the 'all' view if no plugins were found for the selected view context. |
||
| 2296 | if ( empty( $plugins[ $this->view_context ] ) ) { |
||
| 2297 | $this->view_context = 'all'; |
||
| 2298 | } |
||
| 2299 | |||
| 2300 | foreach ( $plugins[ $this->view_context ] as $slug => $plugin ) { |
||
| 2301 | $table_data[ $i ]['sanitized_plugin'] = $plugin['name']; |
||
| 2302 | $table_data[ $i ]['slug'] = $slug; |
||
| 2303 | $table_data[ $i ]['plugin'] = '<strong>' . $this->tgmpa->get_info_link( $slug ) . '</strong>'; |
||
| 2304 | $table_data[ $i ]['source'] = $this->get_plugin_source_type_text( $plugin['source_type'] ); |
||
| 2305 | $table_data[ $i ]['type'] = $this->get_plugin_advise_type_text( $plugin['required'] ); |
||
| 2306 | $table_data[ $i ]['status'] = $this->get_plugin_status_text( $slug ); |
||
| 2307 | $table_data[ $i ]['installed_version'] = $this->tgmpa->get_installed_version( $slug ); |
||
| 2308 | $table_data[ $i ]['minimum_version'] = $plugin['version']; |
||
| 2309 | $table_data[ $i ]['available_version'] = $this->tgmpa->does_plugin_have_update( $slug ); |
||
| 2310 | |||
| 2311 | // Prep the upgrade notice info. |
||
| 2312 | $upgrade_notice = $this->tgmpa->get_upgrade_notice( $slug ); |
||
| 2313 | if ( ! empty( $upgrade_notice ) ) { |
||
| 2314 | $table_data[ $i ]['upgrade_notice'] = $upgrade_notice; |
||
| 2315 | |||
| 2316 | add_action( "tgmpa_after_plugin_row_{$slug}", array( $this, 'wp_plugin_update_row' ), 10, 2 ); |
||
| 2317 | } |
||
| 2318 | |||
| 2319 | $table_data[ $i ] = apply_filters( 'tgmpa_table_data_item', $table_data[ $i ], $plugin ); |
||
| 2320 | |||
| 2321 | $i++; |
||
| 2322 | } |
||
| 2323 | |||
| 2324 | return $table_data; |
||
| 2325 | } |
||
| 2326 | |||
| 2327 | /** |
||
| 2328 | * Categorize the plugins which have open actions into views for the TGMPA page. |
||
| 2329 | * |
||
| 2330 | * @since 2.5.0 |
||
| 2331 | */ |
||
| 2332 | protected function categorize_plugins_to_views() { |
||
| 2333 | $plugins = array( |
||
| 2334 | 'all' => array(), // Meaning: all plugins which still have open actions. |
||
| 2335 | 'install' => array(), |
||
| 2336 | 'update' => array(), |
||
| 2337 | 'activate' => array(), |
||
| 2338 | ); |
||
| 2339 | |||
| 2340 | foreach ( $this->tgmpa->plugins as $slug => $plugin ) { |
||
| 2341 | if ( $this->tgmpa->is_plugin_active( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) { |
||
| 2342 | // No need to display plugins if they are installed, up-to-date and active. |
||
| 2343 | continue; |
||
| 2344 | } else { |
||
| 2345 | $plugins['all'][ $slug ] = $plugin; |
||
| 2346 | |||
| 2347 | if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) { |
||
| 2348 | $plugins['install'][ $slug ] = $plugin; |
||
| 2349 | } else { |
||
| 2350 | if ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) { |
||
| 2351 | $plugins['update'][ $slug ] = $plugin; |
||
| 2352 | } |
||
| 2353 | |||
| 2354 | if ( $this->tgmpa->can_plugin_activate( $slug ) ) { |
||
| 2355 | $plugins['activate'][ $slug ] = $plugin; |
||
| 2356 | } |
||
| 2357 | } |
||
| 2358 | } |
||
| 2359 | } |
||
| 2360 | |||
| 2361 | return $plugins; |
||
| 2362 | } |
||
| 2363 | |||
| 2364 | /** |
||
| 2365 | * Set the counts for the view links. |
||
| 2366 | * |
||
| 2367 | * @since 2.5.0 |
||
| 2368 | * |
||
| 2369 | * @param array $plugins Plugins order by view. |
||
| 2370 | */ |
||
| 2371 | protected function set_view_totals( $plugins ) { |
||
| 2372 | foreach ( $plugins as $type => $list ) { |
||
| 2373 | $this->view_totals[ $type ] = count( $list ); |
||
| 2374 | } |
||
| 2375 | } |
||
| 2376 | |||
| 2377 | /** |
||
| 2378 | * Get the plugin required/recommended text string. |
||
| 2379 | * |
||
| 2380 | * @since 2.5.0 |
||
| 2381 | * |
||
| 2382 | * @param string $required Plugin required setting. |
||
| 2383 | * @return string |
||
| 2384 | */ |
||
| 2385 | protected function get_plugin_advise_type_text( $required ) { |
||
| 2386 | if ( true === $required ) { |
||
| 2387 | return __( 'Required', 'tgmpa' ); |
||
| 2388 | } |
||
| 2389 | |||
| 2390 | return __( 'Recommended', 'tgmpa' ); |
||
| 2391 | } |
||
| 2392 | |||
| 2393 | /** |
||
| 2394 | * Get the plugin source type text string. |
||
| 2395 | * |
||
| 2396 | * @since 2.5.0 |
||
| 2397 | * |
||
| 2398 | * @param string $type Plugin type. |
||
| 2399 | * @return string |
||
| 2400 | */ |
||
| 2401 | protected function get_plugin_source_type_text( $type ) { |
||
| 2402 | $string = ''; |
||
| 2403 | |||
| 2404 | switch ( $type ) { |
||
| 2405 | case 'repo': |
||
| 2406 | $string = __( 'WordPress Repository', 'tgmpa' ); |
||
| 2407 | break; |
||
| 2408 | case 'external': |
||
| 2409 | $string = __( 'External Source', 'tgmpa' ); |
||
| 2410 | break; |
||
| 2411 | case 'bundled': |
||
| 2412 | $string = __( 'Pre-Packaged', 'tgmpa' ); |
||
| 2413 | break; |
||
| 2414 | } |
||
| 2415 | |||
| 2416 | return $string; |
||
| 2417 | } |
||
| 2418 | |||
| 2419 | /** |
||
| 2420 | * Determine the plugin status message. |
||
| 2421 | * |
||
| 2422 | * @since 2.5.0 |
||
| 2423 | * |
||
| 2424 | * @param string $slug Plugin slug. |
||
| 2425 | * @return string |
||
| 2426 | */ |
||
| 2427 | protected function get_plugin_status_text( $slug ) { |
||
| 2428 | if ( ! $this->tgmpa->is_plugin_installed( $slug ) ) { |
||
| 2429 | return __( 'Not Installed', 'tgmpa' ); |
||
| 2430 | } |
||
| 2431 | |||
| 2432 | if ( ! $this->tgmpa->is_plugin_active( $slug ) ) { |
||
| 2433 | $install_status = __( 'Installed But Not Activated', 'tgmpa' ); |
||
| 2434 | } else { |
||
| 2435 | $install_status = __( 'Active', 'tgmpa' ); |
||
| 2436 | } |
||
| 2437 | |||
| 2438 | $update_status = ''; |
||
| 2439 | |||
| 2440 | if ( $this->tgmpa->does_plugin_require_update( $slug ) && false === $this->tgmpa->does_plugin_have_update( $slug ) ) { |
||
| 2441 | $update_status = __( 'Required Update not Available', 'tgmpa' ); |
||
| 2442 | |||
| 2443 | } elseif ( $this->tgmpa->does_plugin_require_update( $slug ) ) { |
||
| 2444 | $update_status = __( 'Requires Update', 'tgmpa' ); |
||
| 2445 | |||
| 2446 | } elseif ( false !== $this->tgmpa->does_plugin_have_update( $slug ) ) { |
||
| 2447 | $update_status = __( 'Update recommended', 'tgmpa' ); |
||
| 2448 | } |
||
| 2449 | |||
| 2450 | if ( '' === $update_status ) { |
||
| 2451 | return $install_status; |
||
| 2452 | } |
||
| 2453 | |||
| 2454 | return sprintf( |
||
| 2455 | /* translators: 1: install status, 2: update status */ |
||
| 2456 | _x( '%1$s, %2$s', 'Install/Update Status', 'tgmpa' ), |
||
| 2457 | $install_status, |
||
| 2458 | $update_status |
||
| 2459 | ); |
||
| 2460 | } |
||
| 2461 | |||
| 2462 | /** |
||
| 2463 | * Sort plugins by Required/Recommended type and by alphabetical plugin name within each type. |
||
| 2464 | * |
||
| 2465 | * @since 2.5.0 |
||
| 2466 | * |
||
| 2467 | * @param array $items Prepared table items. |
||
| 2468 | * @return array Sorted table items. |
||
| 2469 | */ |
||
| 2470 | public function sort_table_items( $items ) { |
||
| 2471 | $type = array(); |
||
| 2472 | $name = array(); |
||
| 2473 | |||
| 2474 | foreach ( $items as $i => $plugin ) { |
||
| 2475 | $type[ $i ] = $plugin['type']; // Required / recommended. |
||
| 2476 | $name[ $i ] = $plugin['sanitized_plugin']; |
||
| 2477 | } |
||
| 2478 | |||
| 2479 | array_multisort( $type, SORT_DESC, $name, SORT_ASC, $items ); |
||
| 2480 | |||
| 2481 | return $items; |
||
| 2482 | } |
||
| 2483 | |||
| 2484 | /** |
||
| 2485 | * Get an associative array ( id => link ) of the views available on this table. |
||
| 2486 | * |
||
| 2487 | * @since 2.5.0 |
||
| 2488 | * |
||
| 2489 | * @return array |
||
| 2490 | */ |
||
| 2491 | public function get_views() { |
||
| 2492 | $status_links = array(); |
||
| 2493 | |||
| 2494 | foreach ( $this->view_totals as $type => $count ) { |
||
| 2495 | if ( $count < 1 ) { |
||
| 2496 | continue; |
||
| 2497 | } |
||
| 2498 | |||
| 2499 | switch ( $type ) { |
||
| 2500 | case 'all': |
||
| 2501 | /* translators: 1: number of plugins. */ |
||
| 2502 | $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'plugins', 'tgmpa' ); |
||
| 2503 | break; |
||
| 2504 | case 'install': |
||
| 2505 | /* translators: 1: number of plugins. */ |
||
| 2506 | $text = _n( 'To Install <span class="count">(%s)</span>', 'To Install <span class="count">(%s)</span>', $count, 'tgmpa' ); |
||
| 2507 | break; |
||
| 2508 | case 'update': |
||
| 2509 | /* translators: 1: number of plugins. */ |
||
| 2510 | $text = _n( 'Update Available <span class="count">(%s)</span>', 'Update Available <span class="count">(%s)</span>', $count, 'tgmpa' ); |
||
| 2511 | break; |
||
| 2512 | case 'activate': |
||
| 2513 | /* translators: 1: number of plugins. */ |
||
| 2514 | $text = _n( 'To Activate <span class="count">(%s)</span>', 'To Activate <span class="count">(%s)</span>', $count, 'tgmpa' ); |
||
| 2515 | break; |
||
| 2516 | default: |
||
| 2517 | $text = ''; |
||
| 2518 | break; |
||
| 2519 | } |
||
| 2520 | |||
| 2521 | if ( ! empty( $text ) ) { |
||
| 2522 | |||
| 2523 | $status_links[ $type ] = sprintf( |
||
| 2524 | '<a href="%s"%s>%s</a>', |
||
| 2525 | esc_url( $this->tgmpa->get_tgmpa_status_url( $type ) ), |
||
| 2526 | ( $type === $this->view_context ) ? ' class="current"' : '', |
||
| 2527 | sprintf( $text, number_format_i18n( $count ) ) |
||
| 2528 | ); |
||
| 2529 | } |
||
| 2530 | } |
||
| 2531 | |||
| 2532 | return $status_links; |
||
| 2533 | } |
||
| 2534 | |||
| 2535 | /** |
||
| 2536 | * Create default columns to display important plugin information |
||
| 2537 | * like type, action and status. |
||
| 2538 | * |
||
| 2539 | * @since 2.2.0 |
||
| 2540 | * |
||
| 2541 | * @param array $item Array of item data. |
||
| 2542 | * @param string $column_name The name of the column. |
||
| 2543 | * @return string |
||
| 2544 | */ |
||
| 2545 | public function column_default( $item, $column_name ) { |
||
| 2546 | return $item[ $column_name ]; |
||
| 2547 | } |
||
| 2548 | |||
| 2549 | /** |
||
| 2550 | * Required for bulk installing. |
||
| 2551 | * |
||
| 2552 | * Adds a checkbox for each plugin. |
||
| 2553 | * |
||
| 2554 | * @since 2.2.0 |
||
| 2555 | * |
||
| 2556 | * @param array $item Array of item data. |
||
| 2557 | * @return string The input checkbox with all necessary info. |
||
| 2558 | */ |
||
| 2559 | public function column_cb( $item ) { |
||
| 2560 | return sprintf( |
||
| 2561 | '<input type="checkbox" name="%1$s[]" value="%2$s" id="%3$s" />', |
||
| 2562 | esc_attr( $this->_args['singular'] ), |
||
| 2563 | esc_attr( $item['slug'] ), |
||
| 2564 | esc_attr( $item['sanitized_plugin'] ) |
||
| 2565 | ); |
||
| 2566 | } |
||
| 2567 | |||
| 2568 | /** |
||
| 2569 | * Create default title column along with the action links. |
||
| 2570 | * |
||
| 2571 | * @since 2.2.0 |
||
| 2572 | * |
||
| 2573 | * @param array $item Array of item data. |
||
| 2574 | * @return string The plugin name and action links. |
||
| 2575 | */ |
||
| 2576 | public function column_plugin( $item ) { |
||
| 2577 | return sprintf( |
||
| 2578 | '%1$s %2$s', |
||
| 2579 | $item['plugin'], |
||
| 2580 | $this->row_actions( $this->get_row_actions( $item ), true ) |
||
| 2581 | ); |
||
| 2582 | } |
||
| 2583 | |||
| 2584 | /** |
||
| 2585 | * Create version information column. |
||
| 2586 | * |
||
| 2587 | * @since 2.5.0 |
||
| 2588 | * |
||
| 2589 | * @param array $item Array of item data. |
||
| 2590 | * @return string HTML-formatted version information. |
||
| 2591 | */ |
||
| 2592 | public function column_version( $item ) { |
||
| 2593 | $output = array(); |
||
| 2594 | |||
| 2595 | if ( $this->tgmpa->is_plugin_installed( $item['slug'] ) ) { |
||
| 2596 | $installed = ! empty( $item['installed_version'] ) ? $item['installed_version'] : _x( 'unknown', 'as in: "version nr unknown"', 'tgmpa' ); |
||
| 2597 | |||
| 2598 | $color = ''; |
||
| 2599 | if ( ! empty( $item['minimum_version'] ) && $this->tgmpa->does_plugin_require_update( $item['slug'] ) ) { |
||
| 2600 | $color = ' color: #ff0000; font-weight: bold;'; |
||
| 2601 | } |
||
| 2602 | |||
| 2603 | $output[] = sprintf( |
||
| 2604 | '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Installed version:', 'tgmpa' ) . '</p>', |
||
| 2605 | $color, |
||
| 2606 | $installed |
||
| 2607 | ); |
||
| 2608 | } |
||
| 2609 | |||
| 2610 | if ( ! empty( $item['minimum_version'] ) ) { |
||
| 2611 | $output[] = sprintf( |
||
| 2612 | '<p><span style="min-width: 32px; text-align: right; float: right;">%1$s</span>' . __( 'Minimum required version:', 'tgmpa' ) . '</p>', |
||
| 2613 | $item['minimum_version'] |
||
| 2614 | ); |
||
| 2615 | } |
||
| 2616 | |||
| 2617 | if ( ! empty( $item['available_version'] ) ) { |
||
| 2618 | $color = ''; |
||
| 2619 | if ( ! empty( $item['minimum_version'] ) && version_compare( $item['available_version'], $item['minimum_version'], '>=' ) ) { |
||
| 2620 | $color = ' color: #71C671; font-weight: bold;'; |
||
| 2621 | } |
||
| 2622 | |||
| 2623 | $output[] = sprintf( |
||
| 2624 | '<p><span style="min-width: 32px; text-align: right; float: right;%1$s">%2$s</span>' . __( 'Available version:', 'tgmpa' ) . '</p>', |
||
| 2625 | $color, |
||
| 2626 | $item['available_version'] |
||
| 2627 | ); |
||
| 2628 | } |
||
| 2629 | |||
| 2630 | if ( empty( $output ) ) { |
||
| 2631 | return ' '; // Let's not break the table layout. |
||
| 2632 | } else { |
||
| 2633 | return implode( "\n", $output ); |
||
| 2634 | } |
||
| 2635 | } |
||
| 2636 | |||
| 2637 | /** |
||
| 2638 | * Sets default message within the plugins table if no plugins |
||
| 2639 | * are left for interaction. |
||
| 2640 | * |
||
| 2641 | * Hides the menu item to prevent the user from clicking and |
||
| 2642 | * getting a permissions error. |
||
| 2643 | * |
||
| 2644 | * @since 2.2.0 |
||
| 2645 | */ |
||
| 2646 | public function no_items() { |
||
| 2647 | echo esc_html__( 'No plugins to install, update or activate.', 'tgmpa' ) . ' <a href="' . esc_url( self_admin_url() ) . '"> ' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>'; |
||
| 2648 | echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; |
||
| 2649 | } |
||
| 2650 | |||
| 2651 | /** |
||
| 2652 | * Output all the column information within the table. |
||
| 2653 | * |
||
| 2654 | * @since 2.2.0 |
||
| 2655 | * |
||
| 2656 | * @return array $columns The column names. |
||
| 2657 | */ |
||
| 2658 | public function get_columns() { |
||
| 2659 | $columns = array( |
||
| 2660 | 'cb' => '<input type="checkbox" />', |
||
| 2661 | 'plugin' => __( 'Plugin', 'tgmpa' ), |
||
| 2662 | 'source' => __( 'Source', 'tgmpa' ), |
||
| 2663 | 'type' => __( 'Type', 'tgmpa' ), |
||
| 2664 | ); |
||
| 2665 | |||
| 2666 | if ( 'all' === $this->view_context || 'update' === $this->view_context ) { |
||
| 2667 | $columns['version'] = __( 'Version', 'tgmpa' ); |
||
| 2668 | $columns['status'] = __( 'Status', 'tgmpa' ); |
||
| 2669 | } |
||
| 2670 | |||
| 2671 | return apply_filters( 'tgmpa_table_columns', $columns ); |
||
| 2672 | } |
||
| 2673 | |||
| 2674 | /** |
||
| 2675 | * Get name of default primary column |
||
| 2676 | * |
||
| 2677 | * @since 2.5.0 / WP 4.3+ compatibility |
||
| 2678 | * @access protected |
||
| 2679 | * |
||
| 2680 | * @return string |
||
| 2681 | */ |
||
| 2682 | protected function get_default_primary_column_name() { |
||
| 2683 | return 'plugin'; |
||
| 2684 | } |
||
| 2685 | |||
| 2686 | /** |
||
| 2687 | * Get the name of the primary column. |
||
| 2688 | * |
||
| 2689 | * @since 2.5.0 / WP 4.3+ compatibility |
||
| 2690 | * @access protected |
||
| 2691 | * |
||
| 2692 | * @return string The name of the primary column. |
||
| 2693 | */ |
||
| 2694 | protected function get_primary_column_name() { |
||
| 2695 | if ( method_exists( 'WP_List_Table', 'get_primary_column_name' ) ) { |
||
| 2696 | return parent::get_primary_column_name(); |
||
| 2697 | } else { |
||
| 2698 | return $this->get_default_primary_column_name(); |
||
| 2699 | } |
||
| 2700 | } |
||
| 2701 | |||
| 2702 | /** |
||
| 2703 | * Get the actions which are relevant for a specific plugin row. |
||
| 2704 | * |
||
| 2705 | * @since 2.5.0 |
||
| 2706 | * |
||
| 2707 | * @param array $item Array of item data. |
||
| 2708 | * @return array Array with relevant action links. |
||
| 2709 | */ |
||
| 2710 | protected function get_row_actions( $item ) { |
||
| 2711 | $actions = array(); |
||
| 2712 | $action_links = array(); |
||
| 2713 | |||
| 2714 | // Display the 'Install' action link if the plugin is not yet available. |
||
| 2715 | if ( ! $this->tgmpa->is_plugin_installed( $item['slug'] ) ) { |
||
| 2716 | /* translators: %2$s: plugin name in screen reader markup */ |
||
| 2717 | $actions['install'] = __( 'Install %2$s', 'tgmpa' ); |
||
| 2718 | } else { |
||
| 2719 | // Display the 'Update' action link if an update is available and WP complies with plugin minimum. |
||
| 2720 | if ( false !== $this->tgmpa->does_plugin_have_update( $item['slug'] ) && $this->tgmpa->can_plugin_update( $item['slug'] ) ) { |
||
| 2721 | /* translators: %2$s: plugin name in screen reader markup */ |
||
| 2722 | $actions['update'] = __( 'Update %2$s', 'tgmpa' ); |
||
| 2723 | } |
||
| 2724 | |||
| 2725 | // Display the 'Activate' action link, but only if the plugin meets the minimum version. |
||
| 2726 | if ( $this->tgmpa->can_plugin_activate( $item['slug'] ) ) { |
||
| 2727 | /* translators: %2$s: plugin name in screen reader markup */ |
||
| 2728 | $actions['activate'] = __( 'Activate %2$s', 'tgmpa' ); |
||
| 2729 | } |
||
| 2730 | } |
||
| 2731 | |||
| 2732 | // Create the actual links. |
||
| 2733 | foreach ( $actions as $action => $text ) { |
||
| 2734 | $nonce_url = wp_nonce_url( |
||
| 2735 | add_query_arg( |
||
| 2736 | array( |
||
| 2737 | 'plugin' => urlencode( $item['slug'] ), |
||
| 2738 | 'tgmpa-' . $action => $action . '-plugin', |
||
| 2739 | ), |
||
| 2740 | $this->tgmpa->get_tgmpa_url() |
||
| 2741 | ), |
||
| 2742 | 'tgmpa-' . $action, |
||
| 2743 | 'tgmpa-nonce' |
||
| 2744 | ); |
||
| 2745 | |||
| 2746 | $action_links[ $action ] = sprintf( |
||
| 2747 | '<a href="%1$s">' . esc_html( $text ) . '</a>', // $text contains the second placeholder. |
||
| 2748 | esc_url( $nonce_url ), |
||
| 2749 | '<span class="screen-reader-text">' . esc_html( $item['sanitized_plugin'] ) . '</span>' |
||
| 2750 | ); |
||
| 2751 | } |
||
| 2752 | |||
| 2753 | $prefix = ( defined( 'WP_NETWORK_ADMIN' ) && WP_NETWORK_ADMIN ) ? 'network_admin_' : ''; |
||
| 2754 | return apply_filters( "tgmpa_{$prefix}plugin_action_links", array_filter( $action_links ), $item['slug'], $item, $this->view_context ); |
||
| 2755 | } |
||
| 2756 | |||
| 2757 | /** |
||
| 2758 | * Generates content for a single row of the table. |
||
| 2759 | * |
||
| 2760 | * @since 2.5.0 |
||
| 2761 | * |
||
| 2762 | * @param object $item The current item. |
||
| 2763 | */ |
||
| 2764 | public function single_row( $item ) { |
||
| 2765 | echo '<tr class="' . esc_attr( 'tgmpa-type-' . strtolower( $item['type'] ) ) . '">'; |
||
| 2766 | $this->single_row_columns( $item ); |
||
| 2767 | echo '</tr>'; |
||
| 2768 | |||
| 2769 | /** |
||
| 2770 | * Fires after each specific row in the TGMPA Plugins list table. |
||
| 2771 | * |
||
| 2772 | * The dynamic portion of the hook name, `$item['slug']`, refers to the slug |
||
| 2773 | * for the plugin. |
||
| 2774 | * |
||
| 2775 | * @since 2.5.0 |
||
| 2776 | */ |
||
| 2777 | do_action( "tgmpa_after_plugin_row_{$item['slug']}", $item['slug'], $item, $this->view_context ); |
||
| 2778 | } |
||
| 2779 | |||
| 2780 | /** |
||
| 2781 | * Show the upgrade notice below a plugin row if there is one. |
||
| 2782 | * |
||
| 2783 | * @since 2.5.0 |
||
| 2784 | * |
||
| 2785 | * @see /wp-admin/includes/update.php |
||
| 2786 | * |
||
| 2787 | * @param string $slug Plugin slug. |
||
| 2788 | * @param array $item The information available in this table row. |
||
| 2789 | * @return null Return early if upgrade notice is empty. |
||
| 2790 | */ |
||
| 2791 | public function wp_plugin_update_row( $slug, $item ) { |
||
| 2792 | if ( empty( $item['upgrade_notice'] ) ) { |
||
| 2793 | return; |
||
| 2794 | } |
||
| 2795 | |||
| 2796 | echo ' |
||
| 2797 | <tr class="plugin-update-tr"> |
||
| 2798 | <td colspan="', absint( $this->get_column_count() ), '" class="plugin-update colspanchange"> |
||
| 2799 | <div class="update-message">', |
||
| 2800 | esc_html__( 'Upgrade message from the plugin author:', 'tgmpa' ), |
||
| 2801 | ' <strong>', wp_kses_data( $item['upgrade_notice'] ), '</strong> |
||
| 2802 | </div> |
||
| 2803 | </td> |
||
| 2804 | </tr>'; |
||
| 2805 | } |
||
| 2806 | |||
| 2807 | /** |
||
| 2808 | * Extra controls to be displayed between bulk actions and pagination. |
||
| 2809 | * |
||
| 2810 | * @since 2.5.0 |
||
| 2811 | * |
||
| 2812 | * @param string $which 'top' or 'bottom' table navigation. |
||
| 2813 | */ |
||
| 2814 | public function extra_tablenav( $which ) { |
||
| 2815 | if ( 'bottom' === $which ) { |
||
| 2816 | $this->tgmpa->show_tgmpa_version(); |
||
| 2817 | } |
||
| 2818 | } |
||
| 2819 | |||
| 2820 | /** |
||
| 2821 | * Defines the bulk actions for handling registered plugins. |
||
| 2822 | * |
||
| 2823 | * @since 2.2.0 |
||
| 2824 | * |
||
| 2825 | * @return array $actions The bulk actions for the plugin install table. |
||
| 2826 | */ |
||
| 2827 | public function get_bulk_actions() { |
||
| 2828 | |||
| 2829 | $actions = array(); |
||
| 2830 | |||
| 2831 | if ( 'update' !== $this->view_context && 'activate' !== $this->view_context ) { |
||
| 2832 | if ( current_user_can( 'install_plugins' ) ) { |
||
| 2833 | $actions['tgmpa-bulk-install'] = __( 'Install', 'tgmpa' ); |
||
| 2834 | } |
||
| 2835 | } |
||
| 2836 | |||
| 2837 | if ( 'install' !== $this->view_context ) { |
||
| 2838 | if ( current_user_can( 'update_plugins' ) ) { |
||
| 2839 | $actions['tgmpa-bulk-update'] = __( 'Update', 'tgmpa' ); |
||
| 2840 | } |
||
| 2841 | if ( current_user_can( 'activate_plugins' ) ) { |
||
| 2842 | $actions['tgmpa-bulk-activate'] = __( 'Activate', 'tgmpa' ); |
||
| 2843 | } |
||
| 2844 | } |
||
| 2845 | |||
| 2846 | return $actions; |
||
| 2847 | } |
||
| 2848 | |||
| 2849 | /** |
||
| 2850 | * Processes bulk installation and activation actions. |
||
| 2851 | * |
||
| 2852 | * The bulk installation process looks for the $_POST information and passes that |
||
| 2853 | * through if a user has to use WP_Filesystem to enter their credentials. |
||
| 2854 | * |
||
| 2855 | * @since 2.2.0 |
||
| 2856 | */ |
||
| 2857 | public function process_bulk_actions() { |
||
| 2858 | // Bulk installation process. |
||
| 2859 | if ( 'tgmpa-bulk-install' === $this->current_action() || 'tgmpa-bulk-update' === $this->current_action() ) { |
||
| 2860 | |||
| 2861 | check_admin_referer( 'bulk-' . $this->_args['plural'] ); |
||
| 2862 | |||
| 2863 | $install_type = 'install'; |
||
| 2864 | if ( 'tgmpa-bulk-update' === $this->current_action() ) { |
||
| 2865 | $install_type = 'update'; |
||
| 2866 | } |
||
| 2867 | |||
| 2868 | $plugins_to_install = array(); |
||
| 2869 | |||
| 2870 | // Did user actually select any plugins to install/update ? |
||
| 2871 | View Code Duplication | if ( empty( $_POST['plugin'] ) ) { |
|
| 2872 | if ( 'install' === $install_type ) { |
||
| 2873 | $message = __( 'No plugins were selected to be installed. No action taken.', 'tgmpa' ); |
||
| 2874 | } else { |
||
| 2875 | $message = __( 'No plugins were selected to be updated. No action taken.', 'tgmpa' ); |
||
| 2876 | } |
||
| 2877 | |||
| 2878 | echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>'; |
||
| 2879 | |||
| 2880 | return false; |
||
| 2881 | } |
||
| 2882 | |||
| 2883 | if ( is_array( $_POST['plugin'] ) ) { |
||
| 2884 | $plugins_to_install = (array) $_POST['plugin']; |
||
| 2885 | } elseif ( is_string( $_POST['plugin'] ) ) { |
||
| 2886 | // Received via Filesystem page - un-flatten array (WP bug #19643). |
||
| 2887 | $plugins_to_install = explode( ',', $_POST['plugin'] ); |
||
| 2888 | } |
||
| 2889 | |||
| 2890 | // Sanitize the received input. |
||
| 2891 | $plugins_to_install = array_map( 'urldecode', $plugins_to_install ); |
||
| 2892 | $plugins_to_install = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins_to_install ); |
||
| 2893 | |||
| 2894 | // Validate the received input. |
||
| 2895 | foreach ( $plugins_to_install as $key => $slug ) { |
||
| 2896 | // Check if the plugin was registered with TGMPA and remove if not. |
||
| 2897 | if ( ! isset( $this->tgmpa->plugins[ $slug ] ) ) { |
||
| 2898 | unset( $plugins_to_install[ $key ] ); |
||
| 2899 | continue; |
||
| 2900 | } |
||
| 2901 | |||
| 2902 | // For install: make sure this is a plugin we *can* install and not one already installed. |
||
| 2903 | if ( 'install' === $install_type && true === $this->tgmpa->is_plugin_installed( $slug ) ) { |
||
| 2904 | unset( $plugins_to_install[ $key ] ); |
||
| 2905 | } |
||
| 2906 | |||
| 2907 | // For updates: make sure this is a plugin we *can* update (update available and WP version ok). |
||
| 2908 | if ( 'update' === $install_type && false === $this->tgmpa->is_plugin_updatetable( $slug ) ) { |
||
| 2909 | unset( $plugins_to_install[ $key ] ); |
||
| 2910 | } |
||
| 2911 | } |
||
| 2912 | |||
| 2913 | // No need to proceed further if we have no plugins to handle. |
||
| 2914 | View Code Duplication | if ( empty( $plugins_to_install ) ) { |
|
| 2915 | if ( 'install' === $install_type ) { |
||
| 2916 | $message = __( 'No plugins are available to be installed at this time.', 'tgmpa' ); |
||
| 2917 | } else { |
||
| 2918 | $message = __( 'No plugins are available to be updated at this time.', 'tgmpa' ); |
||
| 2919 | } |
||
| 2920 | |||
| 2921 | echo '<div id="message" class="error"><p>', esc_html( $message ), '</p></div>'; |
||
| 2922 | |||
| 2923 | return false; |
||
| 2924 | } |
||
| 2925 | |||
| 2926 | // Pass all necessary information if WP_Filesystem is needed. |
||
| 2927 | $url = wp_nonce_url( |
||
| 2928 | $this->tgmpa->get_tgmpa_url(), |
||
| 2929 | 'bulk-' . $this->_args['plural'] |
||
| 2930 | ); |
||
| 2931 | |||
| 2932 | // Give validated data back to $_POST which is the only place the filesystem looks for extra fields. |
||
| 2933 | $_POST['plugin'] = implode( ',', $plugins_to_install ); // Work around for WP bug #19643. |
||
| 2934 | |||
| 2935 | $method = ''; // Leave blank so WP_Filesystem can populate it as necessary. |
||
| 2936 | $fields = array_keys( $_POST ); // Extra fields to pass to WP_Filesystem. |
||
| 2937 | |||
| 2938 | if ( false === ( $creds = request_filesystem_credentials( esc_url_raw( $url ), $method, false, false, $fields ) ) ) { |
||
| 2939 | return true; // Stop the normal page form from displaying, credential request form will be shown. |
||
| 2940 | } |
||
| 2941 | |||
| 2942 | // Now we have some credentials, setup WP_Filesystem. |
||
| 2943 | if ( ! WP_Filesystem( $creds ) ) { |
||
| 2944 | // Our credentials were no good, ask the user for them again. |
||
| 2945 | request_filesystem_credentials( esc_url_raw( $url ), $method, true, false, $fields ); |
||
| 2946 | |||
| 2947 | return true; |
||
| 2948 | } |
||
| 2949 | |||
| 2950 | /* If we arrive here, we have the filesystem */ |
||
| 2951 | |||
| 2952 | // Store all information in arrays since we are processing a bulk installation. |
||
| 2953 | $names = array(); |
||
| 2954 | $sources = array(); // Needed for installs. |
||
| 2955 | $file_paths = array(); // Needed for upgrades. |
||
| 2956 | $to_inject = array(); // Information to inject into the update_plugins transient. |
||
| 2957 | |||
| 2958 | // Prepare the data for validated plugins for the install/upgrade. |
||
| 2959 | foreach ( $plugins_to_install as $slug ) { |
||
| 2960 | $name = $this->tgmpa->plugins[ $slug ]['name']; |
||
| 2961 | $source = $this->tgmpa->get_download_url( $slug ); |
||
| 2962 | |||
| 2963 | if ( ! empty( $name ) && ! empty( $source ) ) { |
||
| 2964 | $names[] = $name; |
||
| 2965 | |||
| 2966 | switch ( $install_type ) { |
||
| 2967 | |||
| 2968 | case 'install': |
||
| 2969 | $sources[] = $source; |
||
| 2970 | break; |
||
| 2971 | |||
| 2972 | case 'update': |
||
| 2973 | $file_paths[] = $this->tgmpa->plugins[ $slug ]['file_path']; |
||
| 2974 | $to_inject[ $slug ] = $this->tgmpa->plugins[ $slug ]; |
||
| 2975 | $to_inject[ $slug ]['source'] = $source; |
||
| 2976 | break; |
||
| 2977 | } |
||
| 2978 | } |
||
| 2979 | } |
||
| 2980 | unset( $slug, $name, $source ); |
||
| 2981 | |||
| 2982 | // Create a new instance of TGMPA_Bulk_Installer. |
||
| 2983 | $installer = new TGMPA_Bulk_Installer( |
||
| 2984 | new TGMPA_Bulk_Installer_Skin( |
||
| 2985 | array( |
||
| 2986 | 'url' => esc_url_raw( $this->tgmpa->get_tgmpa_url() ), |
||
| 2987 | 'nonce' => 'bulk-' . $this->_args['plural'], |
||
| 2988 | 'names' => $names, |
||
| 2989 | 'install_type' => $install_type, |
||
| 2990 | ) |
||
| 2991 | ) |
||
| 2992 | ); |
||
| 2993 | |||
| 2994 | // Wrap the install process with the appropriate HTML. |
||
| 2995 | echo '<div class="tgmpa">', |
||
| 2996 | '<h2 style="font-size: 23px; font-weight: 400; line-height: 29px; margin: 0; padding: 9px 15px 4px 0;">', esc_html( get_admin_page_title() ), '</h2> |
||
| 2997 | <div class="update-php" style="width: 100%; height: 98%; min-height: 850px; padding-top: 1px;">'; |
||
| 2998 | |||
| 2999 | // Process the bulk installation submissions. |
||
| 3000 | add_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1, 3 ); |
||
| 3001 | |||
| 3002 | if ( 'tgmpa-bulk-update' === $this->current_action() ) { |
||
| 3003 | // Inject our info into the update transient. |
||
| 3004 | $this->tgmpa->inject_update_info( $to_inject ); |
||
| 3005 | |||
| 3006 | $installer->bulk_upgrade( $file_paths ); |
||
| 3007 | } else { |
||
| 3008 | $installer->bulk_install( $sources ); |
||
| 3009 | } |
||
| 3010 | |||
| 3011 | remove_filter( 'upgrader_source_selection', array( $this->tgmpa, 'maybe_adjust_source_dir' ), 1 ); |
||
| 3012 | |||
| 3013 | echo '</div></div>'; |
||
| 3014 | |||
| 3015 | return true; |
||
| 3016 | } |
||
| 3017 | |||
| 3018 | // Bulk activation process. |
||
| 3019 | if ( 'tgmpa-bulk-activate' === $this->current_action() ) { |
||
| 3020 | check_admin_referer( 'bulk-' . $this->_args['plural'] ); |
||
| 3021 | |||
| 3022 | // Did user actually select any plugins to activate ? |
||
| 3023 | if ( empty( $_POST['plugin'] ) ) { |
||
| 3024 | echo '<div id="message" class="error"><p>', esc_html__( 'No plugins were selected to be activated. No action taken.', 'tgmpa' ), '</p></div>'; |
||
| 3025 | |||
| 3026 | return false; |
||
| 3027 | } |
||
| 3028 | |||
| 3029 | // Grab plugin data from $_POST. |
||
| 3030 | $plugins = array(); |
||
| 3031 | if ( isset( $_POST['plugin'] ) ) { |
||
| 3032 | $plugins = array_map( 'urldecode', (array) $_POST['plugin'] ); |
||
| 3033 | $plugins = array_map( array( $this->tgmpa, 'sanitize_key' ), $plugins ); |
||
| 3034 | } |
||
| 3035 | |||
| 3036 | $plugins_to_activate = array(); |
||
| 3037 | $plugin_names = array(); |
||
| 3038 | |||
| 3039 | // Grab the file paths for the selected & inactive plugins from the registration array. |
||
| 3040 | foreach ( $plugins as $slug ) { |
||
| 3041 | if ( $this->tgmpa->can_plugin_activate( $slug ) ) { |
||
| 3042 | $plugins_to_activate[] = $this->tgmpa->plugins[ $slug ]['file_path']; |
||
| 3043 | $plugin_names[] = $this->tgmpa->plugins[ $slug ]['name']; |
||
| 3044 | } |
||
| 3045 | } |
||
| 3046 | unset( $slug ); |
||
| 3047 | |||
| 3048 | // Return early if there are no plugins to activate. |
||
| 3049 | if ( empty( $plugins_to_activate ) ) { |
||
| 3050 | echo '<div id="message" class="error"><p>', esc_html__( 'No plugins are available to be activated at this time.', 'tgmpa' ), '</p></div>'; |
||
| 3051 | |||
| 3052 | return false; |
||
| 3053 | } |
||
| 3054 | |||
| 3055 | // Now we are good to go - let's start activating plugins. |
||
| 3056 | $activate = activate_plugins( $plugins_to_activate ); |
||
| 3057 | |||
| 3058 | if ( is_wp_error( $activate ) ) { |
||
| 3059 | echo '<div id="message" class="error"><p>', wp_kses_post( $activate->get_error_message() ), '</p></div>'; |
||
| 3060 | } else { |
||
| 3061 | $count = count( $plugin_names ); // Count so we can use _n function. |
||
| 3062 | $plugin_names = array_map( array( 'TGMPA_Utils', 'wrap_in_strong' ), $plugin_names ); |
||
| 3063 | $last_plugin = array_pop( $plugin_names ); // Pop off last name to prep for readability. |
||
| 3064 | $imploded = empty( $plugin_names ) ? $last_plugin : ( implode( ', ', $plugin_names ) . ' ' . esc_html_x( 'and', 'plugin A *and* plugin B', 'tgmpa' ) . ' ' . $last_plugin ); |
||
| 3065 | |||
| 3066 | printf( // WPCS: xss ok. |
||
| 3067 | '<div id="message" class="updated"><p>%1$s %2$s.</p></div>', |
||
| 3068 | esc_html( _n( 'The following plugin was activated successfully:', 'The following plugins were activated successfully:', $count, 'tgmpa' ) ), |
||
| 3069 | $imploded |
||
| 3070 | ); |
||
| 3071 | |||
| 3072 | // Update recently activated plugins option. |
||
| 3073 | $recent = (array) get_option( 'recently_activated' ); |
||
| 3074 | foreach ( $plugins_to_activate as $plugin => $time ) { |
||
| 3075 | if ( isset( $recent[ $plugin ] ) ) { |
||
| 3076 | unset( $recent[ $plugin ] ); |
||
| 3077 | } |
||
| 3078 | } |
||
| 3079 | update_option( 'recently_activated', $recent ); |
||
| 3080 | } |
||
| 3081 | |||
| 3082 | unset( $_POST ); // Reset the $_POST variable in case user wants to perform one action after another. |
||
| 3083 | |||
| 3084 | return true; |
||
| 3085 | } |
||
| 3086 | |||
| 3087 | return false; |
||
| 3088 | } |
||
| 3089 | |||
| 3090 | /** |
||
| 3091 | * Prepares all of our information to be outputted into a usable table. |
||
| 3092 | * |
||
| 3093 | * @since 2.2.0 |
||
| 3094 | */ |
||
| 3095 | public function prepare_items() { |
||
| 3096 | $columns = $this->get_columns(); // Get all necessary column information. |
||
| 3097 | $hidden = array(); // No columns to hide, but we must set as an array. |
||
| 3098 | $sortable = array(); // No reason to make sortable columns. |
||
| 3099 | $primary = $this->get_primary_column_name(); // Column which has the row actions. |
||
| 3100 | $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); // Get all necessary column headers. |
||
| 3101 | |||
| 3102 | // Process our bulk activations here. |
||
| 3103 | if ( 'tgmpa-bulk-activate' === $this->current_action() ) { |
||
| 3104 | $this->process_bulk_actions(); |
||
| 3105 | } |
||
| 3106 | |||
| 3107 | // Store all of our plugin data into $items array so WP_List_Table can use it. |
||
| 3108 | $this->items = apply_filters( 'tgmpa_table_data_items', $this->_gather_plugin_data() ); |
||
| 3109 | } |
||
| 3110 | |||
| 3111 | /* *********** DEPRECATED METHODS *********** */ |
||
| 3112 | |||
| 3113 | /** |
||
| 3114 | * Retrieve plugin data, given the plugin name. |
||
| 3115 | * |
||
| 3116 | * @since 2.2.0 |
||
| 3117 | * @deprecated 2.5.0 use {@see TGM_Plugin_Activation::_get_plugin_data_from_name()} instead. |
||
| 3118 | * @see TGM_Plugin_Activation::_get_plugin_data_from_name() |
||
| 3119 | * |
||
| 3120 | * @param string $name Name of the plugin, as it was registered. |
||
| 3121 | * @param string $data Optional. Array key of plugin data to return. Default is slug. |
||
| 3122 | * @return string|boolean Plugin slug if found, false otherwise. |
||
| 3123 | */ |
||
| 3124 | protected function _get_plugin_data_from_name( $name, $data = 'slug' ) { |
||
| 3125 | _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'TGM_Plugin_Activation::_get_plugin_data_from_name()' ); |
||
| 3126 | |||
| 3127 | return $this->tgmpa->_get_plugin_data_from_name( $name, $data ); |
||
| 3128 | } |
||
| 3129 | } |
||
| 3130 | } |
||
| 3131 | |||
| 3132 | |||
| 3133 | if ( ! class_exists( 'TGM_Bulk_Installer' ) ) { |
||
| 3134 | |||
| 3135 | /** |
||
| 3136 | * Hack: Prevent TGMPA v2.4.1- bulk installer class from being loaded if 2.4.1- is loaded after 2.5+. |
||
| 3137 | * |
||
| 3138 | * @since 2.5.2 |
||
| 3139 | * |
||
| 3140 | * {@internal The TGMPA_Bulk_Installer class was originally called TGM_Bulk_Installer. |
||
| 3141 | * For more information, see that class.}} |
||
| 3142 | */ |
||
| 3143 | class TGM_Bulk_Installer { |
||
| 3144 | } |
||
| 3145 | } |
||
| 3146 | if ( ! class_exists( 'TGM_Bulk_Installer_Skin' ) ) { |
||
| 3147 | |||
| 3148 | /** |
||
| 3149 | * Hack: Prevent TGMPA v2.4.1- bulk installer skin class from being loaded if 2.4.1- is loaded after 2.5+. |
||
| 3150 | * |
||
| 3151 | * @since 2.5.2 |
||
| 3152 | * |
||
| 3153 | * {@internal The TGMPA_Bulk_Installer_Skin class was originally called TGM_Bulk_Installer_Skin. |
||
| 3154 | * For more information, see that class.}} |
||
| 3155 | */ |
||
| 3156 | class TGM_Bulk_Installer_Skin { |
||
| 3157 | } |
||
| 3158 | } |
||
| 3159 | |||
| 3160 | /** |
||
| 3161 | * The WP_Upgrader file isn't always available. If it isn't available, |
||
| 3162 | * we load it here. |
||
| 3163 | * |
||
| 3164 | * We check to make sure no action or activation keys are set so that WordPress |
||
| 3165 | * does not try to re-include the class when processing upgrades or installs outside |
||
| 3166 | * of the class. |
||
| 3167 | * |
||
| 3168 | * @since 2.2.0 |
||
| 3169 | */ |
||
| 3170 | add_action( 'admin_init', 'tgmpa_load_bulk_installer' ); |
||
| 3171 | if ( ! function_exists( 'tgmpa_load_bulk_installer' ) ) { |
||
| 3172 | /** |
||
| 3173 | * Load bulk installer |
||
| 3174 | */ |
||
| 3175 | function tgmpa_load_bulk_installer() { |
||
| 3176 | // Silently fail if 2.5+ is loaded *after* an older version. |
||
| 3177 | if ( ! isset( $GLOBALS['tgmpa'] ) ) { |
||
| 3178 | return; |
||
| 3179 | } |
||
| 3180 | |||
| 3181 | // Get TGMPA class instance. |
||
| 3182 | $tgmpa_instance = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); |
||
| 3183 | |||
| 3184 | if ( isset( $_GET['page'] ) && $tgmpa_instance->menu === $_GET['page'] ) { |
||
| 3185 | if ( ! class_exists( 'Plugin_Upgrader', false ) ) { |
||
| 3186 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 3187 | } |
||
| 3188 | |||
| 3189 | if ( ! class_exists( 'TGMPA_Bulk_Installer' ) ) { |
||
| 3190 | |||
| 3191 | /** |
||
| 3192 | * Installer class to handle bulk plugin installations. |
||
| 3193 | * |
||
| 3194 | * Extends WP_Upgrader and customizes to suit the installation of multiple |
||
| 3195 | * plugins. |
||
| 3196 | * |
||
| 3197 | * @since 2.2.0 |
||
| 3198 | * |
||
| 3199 | * {@internal Since 2.5.0 the class is an extension of Plugin_Upgrader rather than WP_Upgrader.}} |
||
| 3200 | * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer to TGMPA_Bulk_Installer. |
||
| 3201 | * This was done to prevent backward compatibility issues with v2.3.6.}} |
||
| 3202 | * |
||
| 3203 | * @package TGM-Plugin-Activation |
||
| 3204 | * @author Thomas Griffin |
||
| 3205 | * @author Gary Jones |
||
| 3206 | */ |
||
| 3207 | class TGMPA_Bulk_Installer extends Plugin_Upgrader { |
||
| 3208 | /** |
||
| 3209 | * Holds result of bulk plugin installation. |
||
| 3210 | * |
||
| 3211 | * @since 2.2.0 |
||
| 3212 | * |
||
| 3213 | * @var string |
||
| 3214 | */ |
||
| 3215 | public $result; |
||
| 3216 | |||
| 3217 | /** |
||
| 3218 | * Flag to check if bulk installation is occurring or not. |
||
| 3219 | * |
||
| 3220 | * @since 2.2.0 |
||
| 3221 | * |
||
| 3222 | * @var boolean |
||
| 3223 | */ |
||
| 3224 | public $bulk = false; |
||
| 3225 | |||
| 3226 | /** |
||
| 3227 | * TGMPA instance |
||
| 3228 | * |
||
| 3229 | * @since 2.5.0 |
||
| 3230 | * |
||
| 3231 | * @var object |
||
| 3232 | */ |
||
| 3233 | protected $tgmpa; |
||
| 3234 | |||
| 3235 | /** |
||
| 3236 | * Whether or not the destination directory needs to be cleared ( = on update). |
||
| 3237 | * |
||
| 3238 | * @since 2.5.0 |
||
| 3239 | * |
||
| 3240 | * @var bool |
||
| 3241 | */ |
||
| 3242 | protected $clear_destination = false; |
||
| 3243 | |||
| 3244 | /** |
||
| 3245 | * References parent constructor and sets defaults for class. |
||
| 3246 | * |
||
| 3247 | * @since 2.2.0 |
||
| 3248 | * |
||
| 3249 | * @param \Bulk_Upgrader_Skin|null $skin Installer skin. |
||
| 3250 | */ |
||
| 3251 | public function __construct( $skin = null ) { |
||
| 3252 | // Get TGMPA class instance. |
||
| 3253 | $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); |
||
| 3254 | |||
| 3255 | parent::__construct( $skin ); |
||
| 3256 | |||
| 3257 | if ( isset( $this->skin->options['install_type'] ) && 'update' === $this->skin->options['install_type'] ) { |
||
| 3258 | $this->clear_destination = true; |
||
| 3259 | } |
||
| 3260 | |||
| 3261 | if ( $this->tgmpa->is_automatic ) { |
||
| 3262 | $this->activate_strings(); |
||
| 3263 | } |
||
| 3264 | |||
| 3265 | add_action( 'upgrader_process_complete', array( $this->tgmpa, 'populate_file_path' ) ); |
||
| 3266 | } |
||
| 3267 | |||
| 3268 | /** |
||
| 3269 | * Sets the correct activation strings for the installer skin to use. |
||
| 3270 | * |
||
| 3271 | * @since 2.2.0 |
||
| 3272 | */ |
||
| 3273 | public function activate_strings() { |
||
| 3274 | $this->strings['activation_failed'] = __( 'Plugin activation failed.', 'tgmpa' ); |
||
| 3275 | $this->strings['activation_success'] = __( 'Plugin activated successfully.', 'tgmpa' ); |
||
| 3276 | } |
||
| 3277 | |||
| 3278 | /** |
||
| 3279 | * Performs the actual installation of each plugin. |
||
| 3280 | * |
||
| 3281 | * @since 2.2.0 |
||
| 3282 | * |
||
| 3283 | * @see WP_Upgrader::run() |
||
| 3284 | * |
||
| 3285 | * @param array $options The installation config options. |
||
| 3286 | * @return null|array Return early if error, array of installation data on success. |
||
| 3287 | */ |
||
| 3288 | public function run( $options ) { |
||
| 3289 | $result = parent::run( $options ); |
||
| 3290 | |||
| 3291 | // Reset the strings in case we changed one during automatic activation. |
||
| 3292 | if ( $this->tgmpa->is_automatic ) { |
||
| 3293 | if ( 'update' === $this->skin->options['install_type'] ) { |
||
| 3294 | $this->upgrade_strings(); |
||
| 3295 | } else { |
||
| 3296 | $this->install_strings(); |
||
| 3297 | } |
||
| 3298 | } |
||
| 3299 | |||
| 3300 | return $result; |
||
| 3301 | } |
||
| 3302 | |||
| 3303 | /** |
||
| 3304 | * Processes the bulk installation of plugins. |
||
| 3305 | * |
||
| 3306 | * @since 2.2.0 |
||
| 3307 | * |
||
| 3308 | * {@internal This is basically a near identical copy of the WP Core |
||
| 3309 | * Plugin_Upgrader::bulk_upgrade() method, with minor adjustments to deal with |
||
| 3310 | * new installs instead of upgrades. |
||
| 3311 | * For ease of future synchronizations, the adjustments are clearly commented, but no other |
||
| 3312 | * comments are added. Code style has been made to comply.}} |
||
| 3313 | * |
||
| 3314 | * @see Plugin_Upgrader::bulk_upgrade() |
||
| 3315 | * @see https://core.trac.wordpress.org/browser/tags/4.2.1/src/wp-admin/includes/class-wp-upgrader.php#L838 |
||
| 3316 | * (@internal Last synced: Dec 31st 2015 against https://core.trac.wordpress.org/browser/trunk?rev=36134}} |
||
| 3317 | * |
||
| 3318 | * @param array $plugins The plugin sources needed for installation. |
||
| 3319 | * @param array $args Arbitrary passed extra arguments. |
||
| 3320 | * @return array|false Install confirmation messages on success, false on failure. |
||
| 3321 | */ |
||
| 3322 | public function bulk_install( $plugins, $args = array() ) { |
||
| 3323 | // [TGMPA + ] Hook auto-activation in. |
||
| 3324 | add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); |
||
| 3325 | |||
| 3326 | $defaults = array( |
||
| 3327 | 'clear_update_cache' => true, |
||
| 3328 | ); |
||
| 3329 | $parsed_args = wp_parse_args( $args, $defaults ); |
||
| 3330 | |||
| 3331 | $this->init(); |
||
| 3332 | $this->bulk = true; |
||
| 3333 | |||
| 3334 | $this->install_strings(); // [TGMPA + ] adjusted. |
||
| 3335 | |||
| 3336 | /* [TGMPA - ] $current = get_site_transient( 'update_plugins' ); */ |
||
| 3337 | |||
| 3338 | /* [TGMPA - ] add_filter('upgrader_clear_destination', array($this, 'delete_old_plugin'), 10, 4); */ |
||
| 3339 | |||
| 3340 | $this->skin->header(); |
||
| 3341 | |||
| 3342 | // Connect to the Filesystem first. |
||
| 3343 | $res = $this->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) ); |
||
| 3344 | if ( ! $res ) { |
||
| 3345 | $this->skin->footer(); |
||
| 3346 | return false; |
||
| 3347 | } |
||
| 3348 | |||
| 3349 | $this->skin->bulk_header(); |
||
| 3350 | |||
| 3351 | /* |
||
| 3352 | * Only start maintenance mode if: |
||
| 3353 | * - running Multisite and there are one or more plugins specified, OR |
||
| 3354 | * - a plugin with an update available is currently active. |
||
| 3355 | * @TODO: For multisite, maintenance mode should only kick in for individual sites if at all possible. |
||
| 3356 | */ |
||
| 3357 | $maintenance = ( is_multisite() && ! empty( $plugins ) ); |
||
| 3358 | |||
| 3359 | /* |
||
| 3360 | [TGMPA - ] |
||
| 3361 | foreach ( $plugins as $plugin ) |
||
| 3362 | $maintenance = $maintenance || ( is_plugin_active( $plugin ) && isset( $current->response[ $plugin] ) ); |
||
| 3363 | */ |
||
| 3364 | if ( $maintenance ) { |
||
| 3365 | $this->maintenance_mode( true ); |
||
| 3366 | } |
||
| 3367 | |||
| 3368 | $results = array(); |
||
| 3369 | |||
| 3370 | $this->update_count = count( $plugins ); |
||
| 3371 | $this->update_current = 0; |
||
| 3372 | foreach ( $plugins as $plugin ) { |
||
| 3373 | $this->update_current++; |
||
| 3374 | |||
| 3375 | /* |
||
| 3376 | [TGMPA - ] |
||
| 3377 | $this->skin->plugin_info = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin, false, true); |
||
| 3378 | |||
| 3379 | if ( !isset( $current->response[ $plugin ] ) ) { |
||
| 3380 | $this->skin->set_result('up_to_date'); |
||
| 3381 | $this->skin->before(); |
||
| 3382 | $this->skin->feedback('up_to_date'); |
||
| 3383 | $this->skin->after(); |
||
| 3384 | $results[$plugin] = true; |
||
| 3385 | continue; |
||
| 3386 | } |
||
| 3387 | |||
| 3388 | // Get the URL to the zip file. |
||
| 3389 | $r = $current->response[ $plugin ]; |
||
| 3390 | |||
| 3391 | $this->skin->plugin_active = is_plugin_active($plugin); |
||
| 3392 | */ |
||
| 3393 | |||
| 3394 | $result = $this->run( |
||
| 3395 | array( |
||
| 3396 | 'package' => $plugin, // [TGMPA + ] adjusted. |
||
| 3397 | 'destination' => WP_PLUGIN_DIR, |
||
| 3398 | 'clear_destination' => false, // [TGMPA + ] adjusted. |
||
| 3399 | 'clear_working' => true, |
||
| 3400 | 'is_multi' => true, |
||
| 3401 | 'hook_extra' => array( |
||
| 3402 | 'plugin' => $plugin, |
||
| 3403 | ), |
||
| 3404 | ) |
||
| 3405 | ); |
||
| 3406 | |||
| 3407 | $results[ $plugin ] = $this->result; |
||
| 3408 | |||
| 3409 | // Prevent credentials auth screen from displaying multiple times. |
||
| 3410 | if ( false === $result ) { |
||
| 3411 | break; |
||
| 3412 | } |
||
| 3413 | } //end foreach $plugins |
||
| 3414 | |||
| 3415 | $this->maintenance_mode( false ); |
||
| 3416 | |||
| 3417 | /** |
||
| 3418 | * Fires when the bulk upgrader process is complete. |
||
| 3419 | * |
||
| 3420 | * @since WP 3.6.0 / TGMPA 2.5.0 |
||
| 3421 | * |
||
| 3422 | * @param Plugin_Upgrader $this Plugin_Upgrader instance. In other contexts, $this, might |
||
| 3423 | * be a Theme_Upgrader or Core_Upgrade instance. |
||
| 3424 | * @param array $data { |
||
| 3425 | * Array of bulk item update data. |
||
| 3426 | * |
||
| 3427 | * @type string $action Type of action. Default 'update'. |
||
| 3428 | * @type string $type Type of update process. Accepts 'plugin', 'theme', or 'core'. |
||
| 3429 | * @type bool $bulk Whether the update process is a bulk update. Default true. |
||
| 3430 | * @type array $packages Array of plugin, theme, or core packages to update. |
||
| 3431 | * } |
||
| 3432 | */ |
||
| 3433 | do_action( 'upgrader_process_complete', $this, array( |
||
| 3434 | 'action' => 'install', // [TGMPA + ] adjusted. |
||
| 3435 | 'type' => 'plugin', |
||
| 3436 | 'bulk' => true, |
||
| 3437 | 'plugins' => $plugins, |
||
| 3438 | ) ); |
||
| 3439 | |||
| 3440 | $this->skin->bulk_footer(); |
||
| 3441 | |||
| 3442 | $this->skin->footer(); |
||
| 3443 | |||
| 3444 | // Cleanup our hooks, in case something else does a upgrade on this connection. |
||
| 3445 | /* [TGMPA - ] remove_filter('upgrader_clear_destination', array($this, 'delete_old_plugin')); */ |
||
| 3446 | |||
| 3447 | // [TGMPA + ] Remove our auto-activation hook. |
||
| 3448 | remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); |
||
| 3449 | |||
| 3450 | // Force refresh of plugin update information. |
||
| 3451 | wp_clean_plugins_cache( $parsed_args['clear_update_cache'] ); |
||
| 3452 | |||
| 3453 | return $results; |
||
| 3454 | } |
||
| 3455 | |||
| 3456 | /** |
||
| 3457 | * Handle a bulk upgrade request. |
||
| 3458 | * |
||
| 3459 | * @since 2.5.0 |
||
| 3460 | * |
||
| 3461 | * @see Plugin_Upgrader::bulk_upgrade() |
||
| 3462 | * |
||
| 3463 | * @param array $plugins The local WP file_path's of the plugins which should be upgraded. |
||
| 3464 | * @param array $args Arbitrary passed extra arguments. |
||
| 3465 | * @return string|bool Install confirmation messages on success, false on failure. |
||
| 3466 | */ |
||
| 3467 | public function bulk_upgrade( $plugins, $args = array() ) { |
||
| 3468 | |||
| 3469 | add_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); |
||
| 3470 | |||
| 3471 | $result = parent::bulk_upgrade( $plugins, $args ); |
||
| 3472 | |||
| 3473 | remove_filter( 'upgrader_post_install', array( $this, 'auto_activate' ), 10 ); |
||
| 3474 | |||
| 3475 | return $result; |
||
| 3476 | } |
||
| 3477 | |||
| 3478 | /** |
||
| 3479 | * Abuse a filter to auto-activate plugins after installation. |
||
| 3480 | * |
||
| 3481 | * Hooked into the 'upgrader_post_install' filter hook. |
||
| 3482 | * |
||
| 3483 | * @since 2.5.0 |
||
| 3484 | * |
||
| 3485 | * @param bool $bool The value we need to give back (true). |
||
| 3486 | * @return bool |
||
| 3487 | */ |
||
| 3488 | public function auto_activate( $bool ) { |
||
| 3489 | // Only process the activation of installed plugins if the automatic flag is set to true. |
||
| 3490 | if ( $this->tgmpa->is_automatic ) { |
||
| 3491 | // Flush plugins cache so the headers of the newly installed plugins will be read correctly. |
||
| 3492 | wp_clean_plugins_cache(); |
||
| 3493 | |||
| 3494 | // Get the installed plugin file. |
||
| 3495 | $plugin_info = $this->plugin_info(); |
||
| 3496 | |||
| 3497 | // Don't try to activate on upgrade of active plugin as WP will do this already. |
||
| 3498 | if ( ! is_plugin_active( $plugin_info ) ) { |
||
| 3499 | $activate = activate_plugin( $plugin_info ); |
||
| 3500 | |||
| 3501 | // Adjust the success string based on the activation result. |
||
| 3502 | $this->strings['process_success'] = $this->strings['process_success'] . "<br />\n"; |
||
| 3503 | |||
| 3504 | if ( is_wp_error( $activate ) ) { |
||
| 3505 | $this->skin->error( $activate ); |
||
| 3506 | $this->strings['process_success'] .= $this->strings['activation_failed']; |
||
| 3507 | } else { |
||
| 3508 | $this->strings['process_success'] .= $this->strings['activation_success']; |
||
| 3509 | } |
||
| 3510 | } |
||
| 3511 | } |
||
| 3512 | |||
| 3513 | return $bool; |
||
| 3514 | } |
||
| 3515 | } |
||
| 3516 | } |
||
| 3517 | |||
| 3518 | if ( ! class_exists( 'TGMPA_Bulk_Installer_Skin' ) ) { |
||
| 3519 | |||
| 3520 | /** |
||
| 3521 | * Installer skin to set strings for the bulk plugin installations.. |
||
| 3522 | * |
||
| 3523 | * Extends Bulk_Upgrader_Skin and customizes to suit the installation of multiple |
||
| 3524 | * plugins. |
||
| 3525 | * |
||
| 3526 | * @since 2.2.0 |
||
| 3527 | * |
||
| 3528 | * {@internal Since 2.5.2 the class has been renamed from TGM_Bulk_Installer_Skin to |
||
| 3529 | * TGMPA_Bulk_Installer_Skin. |
||
| 3530 | * This was done to prevent backward compatibility issues with v2.3.6.}} |
||
| 3531 | * |
||
| 3532 | * @see https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/class-wp-upgrader-skins.php |
||
| 3533 | * |
||
| 3534 | * @package TGM-Plugin-Activation |
||
| 3535 | * @author Thomas Griffin |
||
| 3536 | * @author Gary Jones |
||
| 3537 | */ |
||
| 3538 | class TGMPA_Bulk_Installer_Skin extends Bulk_Upgrader_Skin { |
||
| 3539 | /** |
||
| 3540 | * Holds plugin info for each individual plugin installation. |
||
| 3541 | * |
||
| 3542 | * @since 2.2.0 |
||
| 3543 | * |
||
| 3544 | * @var array |
||
| 3545 | */ |
||
| 3546 | public $plugin_info = array(); |
||
| 3547 | |||
| 3548 | /** |
||
| 3549 | * Holds names of plugins that are undergoing bulk installations. |
||
| 3550 | * |
||
| 3551 | * @since 2.2.0 |
||
| 3552 | * |
||
| 3553 | * @var array |
||
| 3554 | */ |
||
| 3555 | public $plugin_names = array(); |
||
| 3556 | |||
| 3557 | /** |
||
| 3558 | * Integer to use for iteration through each plugin installation. |
||
| 3559 | * |
||
| 3560 | * @since 2.2.0 |
||
| 3561 | * |
||
| 3562 | * @var integer |
||
| 3563 | */ |
||
| 3564 | public $i = 0; |
||
| 3565 | |||
| 3566 | /** |
||
| 3567 | * TGMPA instance |
||
| 3568 | * |
||
| 3569 | * @since 2.5.0 |
||
| 3570 | * |
||
| 3571 | * @var object |
||
| 3572 | */ |
||
| 3573 | protected $tgmpa; |
||
| 3574 | |||
| 3575 | /** |
||
| 3576 | * Constructor. Parses default args with new ones and extracts them for use. |
||
| 3577 | * |
||
| 3578 | * @since 2.2.0 |
||
| 3579 | * |
||
| 3580 | * @param array $args Arguments to pass for use within the class. |
||
| 3581 | */ |
||
| 3582 | public function __construct( $args = array() ) { |
||
| 3583 | // Get TGMPA class instance. |
||
| 3584 | $this->tgmpa = call_user_func( array( get_class( $GLOBALS['tgmpa'] ), 'get_instance' ) ); |
||
| 3585 | |||
| 3586 | // Parse default and new args. |
||
| 3587 | $defaults = array( |
||
| 3588 | 'url' => '', |
||
| 3589 | 'nonce' => '', |
||
| 3590 | 'names' => array(), |
||
| 3591 | 'install_type' => 'install', |
||
| 3592 | ); |
||
| 3593 | $args = wp_parse_args( $args, $defaults ); |
||
| 3594 | |||
| 3595 | // Set plugin names to $this->plugin_names property. |
||
| 3596 | $this->plugin_names = $args['names']; |
||
| 3597 | |||
| 3598 | // Extract the new args. |
||
| 3599 | parent::__construct( $args ); |
||
| 3600 | } |
||
| 3601 | |||
| 3602 | /** |
||
| 3603 | * Sets install skin strings for each individual plugin. |
||
| 3604 | * |
||
| 3605 | * Checks to see if the automatic activation flag is set and uses the |
||
| 3606 | * the proper strings accordingly. |
||
| 3607 | * |
||
| 3608 | * @since 2.2.0 |
||
| 3609 | */ |
||
| 3610 | public function add_strings() { |
||
| 3611 | if ( 'update' === $this->options['install_type'] ) { |
||
| 3612 | parent::add_strings(); |
||
| 3613 | /* translators: 1: plugin name, 2: action number 3: total number of actions. */ |
||
| 3614 | $this->upgrader->strings['skin_before_update_header'] = __( 'Updating Plugin %1$s (%2$d/%3$d)', 'tgmpa' ); |
||
| 3615 | } else { |
||
| 3616 | /* translators: 1: plugin name, 2: error message. */ |
||
| 3617 | $this->upgrader->strings['skin_update_failed_error'] = __( 'An error occurred while installing %1$s: <strong>%2$s</strong>.', 'tgmpa' ); |
||
| 3618 | /* translators: 1: plugin name. */ |
||
| 3619 | $this->upgrader->strings['skin_update_failed'] = __( 'The installation of %1$s failed.', 'tgmpa' ); |
||
| 3620 | |||
| 3621 | if ( $this->tgmpa->is_automatic ) { |
||
| 3622 | // Automatic activation strings. |
||
| 3623 | $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation and activation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' ); |
||
| 3624 | /* translators: 1: plugin name. */ |
||
| 3625 | $this->upgrader->strings['skin_update_successful'] = __( '%1$s installed and activated successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>'; |
||
| 3626 | $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations and activations have been completed.', 'tgmpa' ); |
||
| 3627 | /* translators: 1: plugin name, 2: action number 3: total number of actions. */ |
||
| 3628 | $this->upgrader->strings['skin_before_update_header'] = __( 'Installing and Activating Plugin %1$s (%2$d/%3$d)', 'tgmpa' ); |
||
| 3629 | } else { |
||
| 3630 | // Default installation strings. |
||
| 3631 | $this->upgrader->strings['skin_upgrade_start'] = __( 'The installation process is starting. This process may take a while on some hosts, so please be patient.', 'tgmpa' ); |
||
| 3632 | /* translators: 1: plugin name. */ |
||
| 3633 | $this->upgrader->strings['skin_update_successful'] = esc_html__( '%1$s installed successfully.', 'tgmpa' ) . ' <a href="#" class="hide-if-no-js" onclick="%2$s"><span>' . esc_html__( 'Show Details', 'tgmpa' ) . '</span><span class="hidden">' . esc_html__( 'Hide Details', 'tgmpa' ) . '</span>.</a>'; |
||
| 3634 | $this->upgrader->strings['skin_upgrade_end'] = __( 'All installations have been completed.', 'tgmpa' ); |
||
| 3635 | /* translators: 1: plugin name, 2: action number 3: total number of actions. */ |
||
| 3636 | $this->upgrader->strings['skin_before_update_header'] = __( 'Installing Plugin %1$s (%2$d/%3$d)', 'tgmpa' ); |
||
| 3637 | } |
||
| 3638 | } |
||
| 3639 | } |
||
| 3640 | |||
| 3641 | /** |
||
| 3642 | * Outputs the header strings and necessary JS before each plugin installation. |
||
| 3643 | * |
||
| 3644 | * @since 2.2.0 |
||
| 3645 | * |
||
| 3646 | * @param string $title Unused in this implementation. |
||
| 3647 | */ |
||
| 3648 | public function before( $title = '' ) { |
||
| 3649 | if ( empty( $title ) ) { |
||
| 3650 | $title = esc_html( $this->plugin_names[ $this->i ] ); |
||
| 3651 | } |
||
| 3652 | parent::before( $title ); |
||
| 3653 | } |
||
| 3654 | |||
| 3655 | /** |
||
| 3656 | * Outputs the footer strings and necessary JS after each plugin installation. |
||
| 3657 | * |
||
| 3658 | * Checks for any errors and outputs them if they exist, else output |
||
| 3659 | * success strings. |
||
| 3660 | * |
||
| 3661 | * @since 2.2.0 |
||
| 3662 | * |
||
| 3663 | * @param string $title Unused in this implementation. |
||
| 3664 | */ |
||
| 3665 | public function after( $title = '' ) { |
||
| 3666 | if ( empty( $title ) ) { |
||
| 3667 | $title = esc_html( $this->plugin_names[ $this->i ] ); |
||
| 3668 | } |
||
| 3669 | parent::after( $title ); |
||
| 3670 | |||
| 3671 | $this->i++; |
||
| 3672 | } |
||
| 3673 | |||
| 3674 | /** |
||
| 3675 | * Outputs links after bulk plugin installation is complete. |
||
| 3676 | * |
||
| 3677 | * @since 2.2.0 |
||
| 3678 | */ |
||
| 3679 | public function bulk_footer() { |
||
| 3680 | // Serve up the string to say installations (and possibly activations) are complete. |
||
| 3681 | parent::bulk_footer(); |
||
| 3682 | |||
| 3683 | // Flush plugins cache so we can make sure that the installed plugins list is always up to date. |
||
| 3684 | wp_clean_plugins_cache(); |
||
| 3685 | |||
| 3686 | $this->tgmpa->show_tgmpa_version(); |
||
| 3687 | |||
| 3688 | // Display message based on if all plugins are now active or not. |
||
| 3689 | $update_actions = array(); |
||
| 3690 | |||
| 3691 | if ( $this->tgmpa->is_tgmpa_complete() ) { |
||
| 3692 | // All plugins are active, so we display the complete string and hide the menu to protect users. |
||
| 3693 | echo '<style type="text/css">#adminmenu .wp-submenu li.current { display: none !important; }</style>'; |
||
| 3694 | $update_actions['dashboard'] = sprintf( |
||
| 3695 | esc_html( $this->tgmpa->strings['complete'] ), |
||
| 3696 | '<a href="' . esc_url( self_admin_url() ) . '">' . esc_html__( 'Return to the Dashboard', 'tgmpa' ) . '</a>' |
||
| 3697 | ); |
||
| 3698 | } else { |
||
| 3699 | $update_actions['tgmpa_page'] = '<a href="' . esc_url( $this->tgmpa->get_tgmpa_url() ) . '" target="_parent">' . esc_html( $this->tgmpa->strings['return'] ) . '</a>'; |
||
| 3700 | } |
||
| 3701 | |||
| 3702 | /** |
||
| 3703 | * Filter the list of action links available following bulk plugin installs/updates. |
||
| 3704 | * |
||
| 3705 | * @since 2.5.0 |
||
| 3706 | * |
||
| 3707 | * @param array $update_actions Array of plugin action links. |
||
| 3708 | * @param array $plugin_info Array of information for the last-handled plugin. |
||
| 3709 | */ |
||
| 3710 | $update_actions = apply_filters( 'tgmpa_update_bulk_plugins_complete_actions', $update_actions, $this->plugin_info ); |
||
| 3711 | |||
| 3712 | if ( ! empty( $update_actions ) ) { |
||
| 3713 | $this->feedback( implode( ' | ', (array) $update_actions ) ); |
||
| 3714 | } |
||
| 3715 | } |
||
| 3716 | |||
| 3717 | /* *********** DEPRECATED METHODS *********** */ |
||
| 3718 | |||
| 3719 | /** |
||
| 3720 | * Flush header output buffer. |
||
| 3721 | * |
||
| 3722 | * @since 2.2.0 |
||
| 3723 | * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead |
||
| 3724 | * @see Bulk_Upgrader_Skin::flush_output() |
||
| 3725 | */ |
||
| 3726 | public function before_flush_output() { |
||
| 3727 | _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' ); |
||
| 3728 | $this->flush_output(); |
||
| 3729 | } |
||
| 3730 | |||
| 3731 | /** |
||
| 3732 | * Flush footer output buffer and iterate $this->i to make sure the |
||
| 3733 | * installation strings reference the correct plugin. |
||
| 3734 | * |
||
| 3735 | * @since 2.2.0 |
||
| 3736 | * @deprecated 2.5.0 use {@see Bulk_Upgrader_Skin::flush_output()} instead |
||
| 3737 | * @see Bulk_Upgrader_Skin::flush_output() |
||
| 3738 | */ |
||
| 3739 | public function after_flush_output() { |
||
| 3740 | _deprecated_function( __FUNCTION__, 'TGMPA 2.5.0', 'Bulk_Upgrader_Skin::flush_output()' ); |
||
| 3741 | $this->flush_output(); |
||
| 3742 | $this->i++; |
||
| 3743 | } |
||
| 3744 | } |
||
| 3745 | } |
||
| 3746 | } |
||
| 3747 | } |
||
| 3748 | } |
||
| 3749 | |||
| 3750 | if ( ! class_exists( 'TGMPA_Utils' ) ) { |
||
| 3751 | |||
| 3752 | /** |
||
| 3753 | * Generic utilities for TGMPA. |
||
| 3754 | * |
||
| 3755 | * All methods are static, poor-dev name-spacing class wrapper. |
||
| 3756 | * |
||
| 3757 | * Class was called TGM_Utils in 2.5.0 but renamed TGMPA_Utils in 2.5.1 as this was conflicting with Soliloquy. |
||
| 3758 | * |
||
| 3759 | * @since 2.5.0 |
||
| 3760 | * |
||
| 3761 | * @package TGM-Plugin-Activation |
||
| 3762 | * @author Juliette Reinders Folmer |
||
| 3763 | */ |
||
| 3764 | class TGMPA_Utils { |
||
| 3765 | /** |
||
| 3766 | * Whether the PHP filter extension is enabled. |
||
| 3767 | * |
||
| 3768 | * @see http://php.net/book.filter |
||
| 3769 | * |
||
| 3770 | * @since 2.5.0 |
||
| 3771 | * |
||
| 3772 | * @static |
||
| 3773 | * |
||
| 3774 | * @var bool $has_filters True is the extension is enabled. |
||
| 3775 | */ |
||
| 3776 | public static $has_filters; |
||
| 3777 | |||
| 3778 | /** |
||
| 3779 | * Wrap an arbitrary string in <em> tags. Meant to be used in combination with array_map(). |
||
| 3780 | * |
||
| 3781 | * @since 2.5.0 |
||
| 3782 | * |
||
| 3783 | * @static |
||
| 3784 | * |
||
| 3785 | * @param string $string Text to be wrapped. |
||
| 3786 | * @return string |
||
| 3787 | */ |
||
| 3788 | public static function wrap_in_em( $string ) { |
||
| 3789 | return '<em>' . wp_kses_post( $string ) . '</em>'; |
||
| 3790 | } |
||
| 3791 | |||
| 3792 | /** |
||
| 3793 | * Wrap an arbitrary string in <strong> tags. Meant to be used in combination with array_map(). |
||
| 3794 | * |
||
| 3795 | * @since 2.5.0 |
||
| 3796 | * |
||
| 3797 | * @static |
||
| 3798 | * |
||
| 3799 | * @param string $string Text to be wrapped. |
||
| 3800 | * @return string |
||
| 3801 | */ |
||
| 3802 | public static function wrap_in_strong( $string ) { |
||
| 3803 | return '<strong>' . wp_kses_post( $string ) . '</strong>'; |
||
| 3804 | } |
||
| 3805 | |||
| 3806 | /** |
||
| 3807 | * Helper function: Validate a value as boolean |
||
| 3808 | * |
||
| 3809 | * @since 2.5.0 |
||
| 3810 | * |
||
| 3811 | * @static |
||
| 3812 | * |
||
| 3813 | * @param mixed $value Arbitrary value. |
||
| 3814 | * @return bool |
||
| 3815 | */ |
||
| 3816 | public static function validate_bool( $value ) { |
||
| 3817 | if ( ! isset( self::$has_filters ) ) { |
||
| 3818 | self::$has_filters = extension_loaded( 'filter' ); |
||
| 3819 | } |
||
| 3820 | |||
| 3821 | if ( self::$has_filters ) { |
||
| 3822 | return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
||
| 3823 | } else { |
||
| 3824 | return self::emulate_filter_bool( $value ); |
||
| 3825 | } |
||
| 3826 | } |
||
| 3827 | |||
| 3828 | /** |
||
| 3829 | * Helper function: Cast a value to bool |
||
| 3830 | * |
||
| 3831 | * @since 2.5.0 |
||
| 3832 | * |
||
| 3833 | * @static |
||
| 3834 | * |
||
| 3835 | * @param mixed $value Value to cast. |
||
| 3836 | * @return bool |
||
| 3837 | */ |
||
| 3838 | protected static function emulate_filter_bool( $value ) { |
||
| 3839 | // @codingStandardsIgnoreStart |
||
| 3840 | static $true = array( |
||
| 3841 | '1', |
||
| 3842 | 'true', 'True', 'TRUE', |
||
| 3843 | 'y', 'Y', |
||
| 3844 | 'yes', 'Yes', 'YES', |
||
| 3845 | 'on', 'On', 'ON', |
||
| 3846 | ); |
||
| 3847 | static $false = array( |
||
| 3848 | '0', |
||
| 3849 | 'false', 'False', 'FALSE', |
||
| 3850 | 'n', 'N', |
||
| 3851 | 'no', 'No', 'NO', |
||
| 3852 | 'off', 'Off', 'OFF', |
||
| 3853 | ); |
||
| 3854 | // @codingStandardsIgnoreEnd |
||
| 3855 | |||
| 3856 | if ( is_bool( $value ) ) { |
||
| 3857 | return $value; |
||
| 3858 | } elseif ( is_int( $value ) && ( 0 === $value || 1 === $value ) ) { |
||
| 3859 | return (bool) $value; |
||
| 3860 | } elseif ( ( is_float( $value ) && ! is_nan( $value ) ) && ( (float) 0 === $value || (float) 1 === $value ) ) { |
||
| 3861 | return (bool) $value; |
||
| 3862 | } elseif ( is_string( $value ) ) { |
||
| 3863 | $value = trim( $value ); |
||
| 3864 | if ( in_array( $value, $true, true ) ) { |
||
| 3865 | return true; |
||
| 3866 | } elseif ( in_array( $value, $false, true ) ) { |
||
| 3867 | return false; |
||
| 3868 | } else { |
||
| 3869 | return false; |
||
| 3870 | } |
||
| 3871 | } |
||
| 3872 | |||
| 3873 | return false; |
||
| 3874 | } |
||
| 3875 | } // End of class TGMPA_Utils |
||
| 3876 | } // End of class_exists wrapper |
||
| 3877 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.