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