[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-plugin-installer-skin.php (source)

   1  <?php
   2  /**
   3   * Upgrader API: Plugin_Installer_Skin class
   4   *
   5   * @package WordPress
   6   * @subpackage Upgrader
   7   * @since 4.6.0
   8   */
   9  
  10  /**
  11   * Plugin Installer Skin for WordPress Plugin Installer.
  12   *
  13   * @since 2.8.0
  14   * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
  15   *
  16   * @see WP_Upgrader_Skin
  17   */
  18  class Plugin_Installer_Skin extends WP_Upgrader_Skin {
  19      /**
  20       * The upgrader instance.
  21       *
  22       * @since 2.8.0
  23       * @var Plugin_Upgrader
  24       */
  25      public $upgrader;
  26  
  27      public $api;
  28      public $type;
  29      public $url;
  30      public $overwrite;
  31  
  32      private $is_downgrading = false;
  33  
  34      /**
  35       * Constructor.
  36       *
  37       * Sets up the plugin installer skin.
  38       *
  39       * @since 2.8.0
  40       *
  41       * @param array $args
  42       */
  43  	public function __construct( $args = array() ) {
  44          $defaults = array(
  45              'type'      => 'web',
  46              'url'       => '',
  47              'plugin'    => '',
  48              'nonce'     => '',
  49              'title'     => '',
  50              'overwrite' => '',
  51          );
  52          $args     = wp_parse_args( $args, $defaults );
  53  
  54          $this->type      = $args['type'];
  55          $this->url       = $args['url'];
  56          $this->api       = $args['api'] ?? array();
  57          $this->overwrite = $args['overwrite'];
  58  
  59          parent::__construct( $args );
  60      }
  61  
  62      /**
  63       * Performs an action before installing a plugin.
  64       *
  65       * @since 2.8.0
  66       */
  67  	public function before() {
  68          if ( ! empty( $this->api ) ) {
  69              $this->upgrader->strings['process_success'] = sprintf(
  70                  $this->upgrader->strings['process_success_specific'],
  71                  $this->api->name,
  72                  $this->api->version
  73              );
  74          }
  75      }
  76  
  77      /**
  78       * Hides the `process_failed` error when updating a plugin by uploading a zip file.
  79       *
  80       * @since 5.5.0
  81       *
  82       * @param WP_Error $wp_error WP_Error object.
  83       * @return bool True if the error should be hidden, false otherwise.
  84       */
  85  	public function hide_process_failed( $wp_error ) {
  86          if (
  87              'upload' === $this->type &&
  88              '' === $this->overwrite &&
  89              $wp_error->get_error_code() === 'folder_exists'
  90          ) {
  91              return true;
  92          }
  93  
  94          return false;
  95      }
  96  
  97      /**
  98       * Performs an action following a plugin install.
  99       *
 100       * @since 2.8.0
 101       */
 102  	public function after() {
 103          // Check if the plugin can be overwritten and output the HTML.
 104          if ( $this->do_overwrite() ) {
 105              return;
 106          }
 107  
 108          $plugin_file = $this->upgrader->plugin_info();
 109  
 110          $install_actions = array();
 111  
 112          $from = isset( $_GET['from'] ) ? wp_unslash( $_GET['from'] ) : 'plugins';
 113  
 114          if ( 'import' === $from ) {
 115              $install_actions['activate_plugin'] = sprintf(
 116                  '<a class="button button-primary" href="%s" target="_parent">%s</a>',
 117                  wp_nonce_url( 'plugins.php?action=activate&amp;from=import&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
 118                  __( 'Activate Plugin &amp; Run Importer' )
 119              );
 120          } elseif ( 'press-this' === $from ) {
 121              $install_actions['activate_plugin'] = sprintf(
 122                  '<a class="button button-primary" href="%s" target="_parent">%s</a>',
 123                  wp_nonce_url( 'plugins.php?action=activate&amp;from=press-this&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
 124                  __( 'Activate Plugin &amp; Go to Press This' )
 125              );
 126          } else {
 127              $install_actions['activate_plugin'] = sprintf(
 128                  '<a class="button button-primary" href="%s" target="_parent">%s</a>',
 129                  wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
 130                  __( 'Activate Plugin' )
 131              );
 132          }
 133  
 134          if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
 135              $install_actions['network_activate'] = sprintf(
 136                  '<a class="button button-primary" href="%s" target="_parent">%s</a>',
 137                  wp_nonce_url( 'plugins.php?action=activate&amp;networkwide=1&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
 138                  _x( 'Network Activate', 'plugin' )
 139              );
 140              unset( $install_actions['activate_plugin'] );
 141          }
 142  
 143          if ( 'import' === $from ) {
 144              $install_actions['importers_page'] = sprintf(
 145                  '<a href="%s" target="_parent">%s</a>',
 146                  admin_url( 'import.php' ),
 147                  __( 'Go to Importers' )
 148              );
 149          } elseif ( 'web' === $this->type ) {
 150              $install_actions['plugins_page'] = sprintf(
 151                  '<a href="%s" target="_parent">%s</a>',
 152                  self_admin_url( 'plugin-install.php' ),
 153                  __( 'Go to Plugin Installer' )
 154              );
 155          } elseif ( 'upload' === $this->type && 'plugins' === $from ) {
 156              $install_actions['plugins_page'] = sprintf(
 157                  '<a href="%s">%s</a>',
 158                  self_admin_url( 'plugin-install.php' ),
 159                  __( 'Go to Plugin Installer' )
 160              );
 161          } else {
 162              $install_actions['plugins_page'] = sprintf(
 163                  '<a href="%s" target="_parent">%s</a>',
 164                  self_admin_url( 'plugins.php' ),
 165                  __( 'Go to Plugins page' )
 166              );
 167          }
 168  
 169          if ( ! $this->result || is_wp_error( $this->result ) ) {
 170              unset( $install_actions['activate_plugin'], $install_actions['network_activate'] );
 171          } elseif ( ! current_user_can( 'activate_plugin', $plugin_file ) || is_plugin_active( $plugin_file ) ) {
 172              unset( $install_actions['activate_plugin'] );
 173          }
 174  
 175          /**
 176           * Filters the list of action links available following a single plugin installation.
 177           *
 178           * @since 2.7.0
 179           *
 180           * @param string[] $install_actions Array of plugin action links.
 181           * @param object   $api             Object containing WordPress.org API plugin data. Empty
 182           *                                  for non-API installs, such as when a plugin is installed
 183           *                                  via upload.
 184           * @param string   $plugin_file     Path to the plugin file relative to the plugins directory.
 185           */
 186          $install_actions = apply_filters( 'install_plugin_complete_actions', $install_actions, $this->api, $plugin_file );
 187  
 188          if ( ! empty( $install_actions ) ) {
 189              $this->feedback( implode( ' ', (array) $install_actions ) );
 190          }
 191      }
 192  
 193      /**
 194       * Checks if the plugin can be overwritten and outputs the HTML for overwriting a plugin on upload.
 195       *
 196       * @since 5.5.0
 197       *
 198       * @return bool Whether the plugin can be overwritten and HTML was outputted.
 199       */
 200  	private function do_overwrite() {
 201          if ( 'upload' !== $this->type || ! is_wp_error( $this->result ) || 'folder_exists' !== $this->result->get_error_code() ) {
 202              return false;
 203          }
 204  
 205          $folder = $this->result->get_error_data( 'folder_exists' );
 206          $folder = ltrim( substr( $folder, strlen( WP_PLUGIN_DIR ) ), '/' );
 207  
 208          $current_plugin_data = false;
 209          $all_plugins         = get_plugins();
 210  
 211          foreach ( $all_plugins as $plugin => $plugin_data ) {
 212              if ( strrpos( $plugin, $folder ) !== 0 ) {
 213                  continue;
 214              }
 215  
 216              $current_plugin_data = $plugin_data;
 217          }
 218  
 219          $new_plugin_data = $this->upgrader->new_plugin_data;
 220  
 221          if ( ! $current_plugin_data || ! $new_plugin_data ) {
 222              return false;
 223          }
 224  
 225          echo '<h2 class="update-from-upload-heading">' . esc_html__( 'This plugin is already installed.' ) . '</h2>';
 226  
 227          $this->is_downgrading = version_compare( $current_plugin_data['Version'], $new_plugin_data['Version'], '>' );
 228  
 229          $rows = array(
 230              'Name'        => __( 'Plugin name' ),
 231              'Version'     => __( 'Version' ),
 232              'Author'      => __( 'Author' ),
 233              'RequiresWP'  => __( 'Required WordPress version' ),
 234              'RequiresPHP' => __( 'Required PHP version' ),
 235          );
 236  
 237          $table  = '<table class="update-from-upload-comparison"><tbody>';
 238          $table .= '<tr><th></th><th>' . esc_html_x( 'Current', 'plugin' ) . '</th>';
 239          $table .= '<th>' . esc_html_x( 'Uploaded', 'plugin' ) . '</th></tr>';
 240  
 241          $is_same_plugin = true; // Let's consider only these rows.
 242  
 243          foreach ( $rows as $field => $label ) {
 244              $old_value = ! empty( $current_plugin_data[ $field ] ) ? (string) $current_plugin_data[ $field ] : '-';
 245              $new_value = ! empty( $new_plugin_data[ $field ] ) ? (string) $new_plugin_data[ $field ] : '-';
 246  
 247              $is_same_plugin = $is_same_plugin && ( $old_value === $new_value );
 248  
 249              $diff_field   = ( 'Version' !== $field && $new_value !== $old_value );
 250              $diff_version = ( 'Version' === $field && $this->is_downgrading );
 251  
 252              $table .= '<tr><td class="name-label">' . $label . '</td><td>' . wp_strip_all_tags( $old_value ) . '</td>';
 253              $table .= ( $diff_field || $diff_version ) ? '<td class="warning">' : '<td>';
 254              $table .= wp_strip_all_tags( $new_value ) . '</td></tr>';
 255          }
 256  
 257          $table .= '</tbody></table>';
 258  
 259          /**
 260           * Filters the compare table output for overwriting a plugin package on upload.
 261           *
 262           * @since 5.5.0
 263           *
 264           * @param string $table               The output table with Name, Version, Author, RequiresWP, and RequiresPHP info.
 265           * @param array  $current_plugin_data Array with current plugin data.
 266           * @param array  $new_plugin_data     Array with uploaded plugin data.
 267           */
 268          echo apply_filters( 'install_plugin_overwrite_comparison', $table, $current_plugin_data, $new_plugin_data );
 269  
 270          $install_actions = array();
 271          $can_update      = true;
 272  
 273          $blocked_message  = '<p>' . esc_html__( 'The plugin cannot be updated due to the following:' ) . '</p>';
 274          $blocked_message .= '<ul class="ul-disc">';
 275  
 276          $requires_php = $new_plugin_data['RequiresPHP'] ?? null;
 277          $requires_wp  = $new_plugin_data['RequiresWP'] ?? null;
 278  
 279          if ( ! is_php_version_compatible( $requires_php ) ) {
 280              $error = sprintf(
 281                  /* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
 282                  __( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
 283                  PHP_VERSION,
 284                  $requires_php
 285              );
 286  
 287              $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
 288              $can_update       = false;
 289          }
 290  
 291          if ( ! is_wp_version_compatible( $requires_wp ) ) {
 292              $error = sprintf(
 293                  /* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
 294                  __( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
 295                  esc_html( wp_get_wp_version() ),
 296                  $requires_wp
 297              );
 298  
 299              $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
 300              $can_update       = false;
 301          }
 302  
 303          $blocked_message .= '</ul>';
 304  
 305          if ( $can_update ) {
 306              if ( $this->is_downgrading ) {
 307                  $warning = sprintf(
 308                      /* translators: %s: Documentation URL. */
 309                      __( 'You are uploading an older version of a current plugin. You can continue to install the older version, but be sure to <a href="%s">back up your database and files</a> first.' ),
 310                      __( 'https://developer.wordpress.org/advanced-administration/security/backup/' )
 311                  );
 312              } else {
 313                  $warning = sprintf(
 314                      /* translators: %s: Documentation URL. */
 315                      __( 'You are updating a plugin. Be sure to <a href="%s">back up your database and files</a> first.' ),
 316                      __( 'https://developer.wordpress.org/advanced-administration/security/backup/' )
 317                  );
 318              }
 319  
 320              echo '<p class="update-from-upload-notice">' . $warning . '</p>';
 321  
 322              $overwrite = $this->is_downgrading ? 'downgrade-plugin' : 'update-plugin';
 323  
 324              $install_actions['overwrite_plugin'] = sprintf(
 325                  '<a class="button button-primary update-from-upload-overwrite" href="%s" target="_parent">%s</a>',
 326                  wp_nonce_url( add_query_arg( 'overwrite', $overwrite, $this->url ), 'plugin-upload' ),
 327                  _x( 'Replace current with uploaded', 'plugin' )
 328              );
 329          } else {
 330              echo $blocked_message;
 331          }
 332  
 333          $cancel_url = add_query_arg( 'action', 'upload-plugin-cancel-overwrite', $this->url );
 334  
 335          $install_actions['plugins_page'] = sprintf(
 336              '<a class="button" href="%s">%s</a>',
 337              wp_nonce_url( $cancel_url, 'plugin-upload-cancel-overwrite' ),
 338              __( 'Cancel and go back' )
 339          );
 340  
 341          /**
 342           * Filters the list of action links available following a single plugin installation failure
 343           * when overwriting is allowed.
 344           *
 345           * @since 5.5.0
 346           *
 347           * @param string[] $install_actions Array of plugin action links.
 348           * @param object   $api             Object containing WordPress.org API plugin data.
 349           * @param array    $new_plugin_data Array with uploaded plugin data.
 350           */
 351          $install_actions = apply_filters( 'install_plugin_overwrite_actions', $install_actions, $this->api, $new_plugin_data );
 352  
 353          if ( ! empty( $install_actions ) ) {
 354              printf(
 355                  '<p class="update-from-upload-expired hidden">%s</p>',
 356                  __( 'The uploaded file has expired. Please go back and upload it again.' )
 357              );
 358              echo '<p class="update-from-upload-actions">' . implode( ' ', (array) $install_actions ) . '</p>';
 359          }
 360  
 361          return true;
 362      }
 363  }


Generated : Sat Sep 5 08:20:28 2026 Cross-referenced by PHPXref