[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> plugin-install.php (source)

   1  <?php
   2  /**
   3   * WordPress Plugin Install Administration API
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Retrieves plugin installer pages from the WordPress.org Plugins API.
  11   *
  12   * It is possible for a plugin to override the Plugin API result with three
  13   * filters. Assume this is for plugins, which can extend on the Plugin Info to
  14   * offer more choices. This is very powerful and must be used with care when
  15   * overriding the filters.
  16   *
  17   * The first filter, {@see 'plugins_api_args'}, is for the args and gives the action
  18   * as the second parameter. The hook for {@see 'plugins_api_args'} must ensure that
  19   * an object is returned.
  20   *
  21   * The second filter, {@see 'plugins_api'}, allows a plugin to override the WordPress.org
  22   * Plugin Installation API entirely. If `$action` is 'query_plugins' or 'plugin_information',
  23   * an object MUST be passed. If `$action` is 'hot_tags', an array MUST be passed.
  24   *
  25   * Finally, the third filter, {@see 'plugins_api_result'}, makes it possible to filter the
  26   * response object or array, depending on the `$action` type.
  27   *
  28   * Supported arguments per action:
  29   *
  30   * | Argument Name        | query_plugins | plugin_information | hot_tags |
  31   * | -------------------- | :-----------: | :----------------: | :------: |
  32   * | `$slug`              | No            |  Yes               | No       |
  33   * | `$per_page`          | Yes           |  No                | No       |
  34   * | `$page`              | Yes           |  No                | No       |
  35   * | `$number`            | No            |  No                | Yes      |
  36   * | `$search`            | Yes           |  No                | No       |
  37   * | `$tag`               | Yes           |  No                | No       |
  38   * | `$author`            | Yes           |  No                | No       |
  39   * | `$user`              | Yes           |  No                | No       |
  40   * | `$browse`            | Yes           |  No                | No       |
  41   * | `$locale`            | Yes           |  Yes               | No       |
  42   * | `$installed_plugins` | Yes           |  No                | No       |
  43   * | `$is_ssl`            | Yes           |  Yes               | No       |
  44   * | `$fields`            | Yes           |  Yes               | No       |
  45   *
  46   * @since 2.7.0
  47   *
  48   * @param string       $action API action to perform: 'query_plugins', 'plugin_information',
  49   *                             or 'hot_tags'.
  50   * @param array|object $args   {
  51   *     Optional. Array or object of arguments to serialize for the Plugin Info API.
  52   *
  53   *     @type string  $slug              The plugin slug. Default empty.
  54   *     @type int     $per_page          Number of plugins per page. Default 24.
  55   *     @type int     $page              Number of current page. Default 1.
  56   *     @type int     $number            Number of tags or categories to be queried.
  57   *     @type string  $search            A search term. Default empty.
  58   *     @type string  $tag               Tag to filter plugins. Default empty.
  59   *     @type string  $author            Username of an plugin author to filter plugins. Default empty.
  60   *     @type string  $user              Username to query for their favorites. Default empty.
  61   *     @type string  $browse            Browse view: 'popular', 'new', 'beta', 'recommended'.
  62   *     @type string  $locale            Locale to provide context-sensitive results. Default is the value
  63   *                                      of get_locale().
  64   *     @type string  $installed_plugins Installed plugins to provide context-sensitive results.
  65   *     @type bool    $is_ssl            Whether links should be returned with https or not. Default false.
  66   *     @type array   $fields            {
  67   *         Array of fields which should or should not be returned.
  68   *
  69   *         @type bool $short_description Whether to return the plugin short description. Default true.
  70   *         @type bool $description       Whether to return the plugin full description. Default false.
  71   *         @type bool $sections          Whether to return the plugin readme sections: description, installation,
  72   *                                       FAQ, screenshots, other notes, and changelog. Default false.
  73   *         @type bool $tested            Whether to return the 'Compatible up to' value. Default true.
  74   *         @type bool $requires          Whether to return the required WordPress version. Default true.
  75   *         @type bool $requires_php      Whether to return the required PHP version. Default true.
  76   *         @type bool $rating            Whether to return the rating in percent and total number of ratings.
  77   *                                       Default true.
  78   *         @type bool $ratings           Whether to return the number of rating for each star (1-5). Default true.
  79   *         @type bool $downloaded        Whether to return the download count. Default true.
  80   *         @type bool $downloadlink      Whether to return the download link for the package. Default true.
  81   *         @type bool $last_updated      Whether to return the date of the last update. Default true.
  82   *         @type bool $added             Whether to return the date when the plugin was added to the wordpress.org
  83   *                                       repository. Default true.
  84   *         @type bool $tags              Whether to return the assigned tags. Default true.
  85   *         @type bool $compatibility     Whether to return the WordPress compatibility list. Default true.
  86   *         @type bool $homepage          Whether to return the plugin homepage link. Default true.
  87   *         @type bool $versions          Whether to return the list of all available versions. Default false.
  88   *         @type bool $donate_link       Whether to return the donation link. Default true.
  89   *         @type bool $reviews           Whether to return the plugin reviews. Default false.
  90   *         @type bool $banners           Whether to return the banner images links. Default false.
  91   *         @type bool $icons             Whether to return the icon links. Default false.
  92   *         @type bool $active_installs   Whether to return the number of active installations. Default false.
  93   *         @type bool $contributors      Whether to return the list of contributors. Default false.
  94   *     }
  95   * }
  96   * @return object|array|WP_Error Response object or array on success, WP_Error on failure. See the
  97   *         {@link https://developer.wordpress.org/reference/functions/plugins_api/ function reference article}
  98   *         for more information on the make-up of possible return values depending on the value of `$action`.
  99   */
 100  function plugins_api( $action, $args = array() ) {
 101      if ( is_array( $args ) ) {
 102          $args = (object) $args;
 103      }
 104  
 105      if ( 'query_plugins' === $action ) {
 106          if ( ! isset( $args->per_page ) ) {
 107              $args->per_page = 24;
 108          }
 109      }
 110  
 111      if ( ! isset( $args->locale ) ) {
 112          $args->locale = get_user_locale();
 113      }
 114  
 115      if ( ! isset( $args->wp_version ) ) {
 116          $args->wp_version = substr( wp_get_wp_version(), 0, 3 ); // x.y
 117      }
 118  
 119      /**
 120       * Filters the WordPress.org Plugin Installation API arguments.
 121       *
 122       * Important: An object MUST be returned to this filter.
 123       *
 124       * @since 2.7.0
 125       *
 126       * @param object $args   Plugin API arguments.
 127       * @param string $action The type of information being requested from the Plugin Installation API.
 128       */
 129      $args = apply_filters( 'plugins_api_args', $args, $action );
 130  
 131      /**
 132       * Filters the response for the current WordPress.org Plugin Installation API request.
 133       *
 134       * Returning a non-false value will effectively short-circuit the WordPress.org API request.
 135       *
 136       * If `$action` is 'query_plugins' or 'plugin_information', an object MUST be passed.
 137       * If `$action` is 'hot_tags', an array should be passed.
 138       *
 139       * @since 2.7.0
 140       *
 141       * @param false|object|array $result The result object or array. Default false.
 142       * @param string             $action The type of information being requested from the Plugin Installation API.
 143       * @param object             $args   Plugin API arguments.
 144       */
 145      $res = apply_filters( 'plugins_api', false, $action, $args );
 146  
 147      if ( false === $res ) {
 148  
 149          $url = 'http://api.wordpress.org/plugins/info/1.2/';
 150          $url = add_query_arg(
 151              array(
 152                  'action'  => $action,
 153                  'request' => $args,
 154              ),
 155              $url
 156          );
 157  
 158          $http_url = $url;
 159          $ssl      = wp_http_supports( array( 'ssl' ) );
 160          if ( $ssl ) {
 161              $url = set_url_scheme( $url, 'https' );
 162          }
 163  
 164          $http_args = array(
 165              'timeout'    => 15,
 166              'user-agent' => 'WordPress/' . wp_get_wp_version() . '; ' . home_url( '/' ),
 167          );
 168          $request   = wp_remote_get( $url, $http_args );
 169  
 170          if ( $ssl && is_wp_error( $request ) ) {
 171              if ( ! wp_is_json_request() ) {
 172                  wp_trigger_error(
 173                      __FUNCTION__,
 174                      sprintf(
 175                          /* translators: %s: Support forums URL. */
 176                          __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 177                          __( 'https://wordpress.org/support/forums/' )
 178                      ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
 179                      headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
 180                  );
 181              }
 182  
 183              $request = wp_remote_get( $http_url, $http_args );
 184          }
 185  
 186          if ( is_wp_error( $request ) ) {
 187              $res = new WP_Error(
 188                  'plugins_api_failed',
 189                  sprintf(
 190                      /* translators: %s: Support forums URL. */
 191                      __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 192                      __( 'https://wordpress.org/support/forums/' )
 193                  ),
 194                  $request->get_error_message()
 195              );
 196          } else {
 197              $res = json_decode( wp_remote_retrieve_body( $request ), true );
 198              if ( is_array( $res ) ) {
 199                  // Object casting is required in order to match the info/1.0 format.
 200                  $res = (object) $res;
 201              } elseif ( null === $res ) {
 202                  $res = new WP_Error(
 203                      'plugins_api_failed',
 204                      sprintf(
 205                          /* translators: %s: Support forums URL. */
 206                          __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
 207                          __( 'https://wordpress.org/support/forums/' )
 208                      ),
 209                      wp_remote_retrieve_body( $request )
 210                  );
 211              }
 212  
 213              if ( isset( $res->error ) ) {
 214                  $res = new WP_Error( 'plugins_api_failed', $res->error );
 215              }
 216          }
 217      } elseif ( ! is_wp_error( $res ) ) {
 218          $res->external = true;
 219      }
 220  
 221      /**
 222       * Filters the Plugin Installation API response results.
 223       *
 224       * @since 2.7.0
 225       *
 226       * @param object|WP_Error $res    Response object or WP_Error.
 227       * @param string          $action The type of information being requested from the Plugin Installation API.
 228       * @param object          $args   Plugin API arguments.
 229       */
 230      return apply_filters( 'plugins_api_result', $res, $action, $args );
 231  }
 232  
 233  /**
 234   * Retrieves popular WordPress plugin tags.
 235   *
 236   * @since 2.7.0
 237   *
 238   * @param array $args
 239   * @return array|WP_Error
 240   */
 241  function install_popular_tags( $args = array() ) {
 242      $key  = md5( serialize( $args ) );
 243      $tags = get_site_transient( 'poptags_' . $key );
 244      if ( false !== $tags ) {
 245          return $tags;
 246      }
 247  
 248      $tags = plugins_api( 'hot_tags', $args );
 249  
 250      if ( is_wp_error( $tags ) ) {
 251          return $tags;
 252      }
 253  
 254      set_site_transient( 'poptags_' . $key, $tags, 3 * HOUR_IN_SECONDS );
 255  
 256      return $tags;
 257  }
 258  
 259  /**
 260   * Displays the Featured tab of Add Plugins screen.
 261   *
 262   * @since 2.7.0
 263   */
 264  function install_dashboard() {
 265      display_plugins_table();
 266      ?>
 267  
 268      <div class="plugins-popular-tags-wrapper">
 269      <h2><?php _e( 'Popular tags' ); ?></h2>
 270      <p><?php _e( 'You may also browse based on the most popular tags in the Plugin Directory:' ); ?></p>
 271      <?php
 272  
 273      $api_tags = install_popular_tags();
 274  
 275      echo '<p class="popular-tags">';
 276      if ( is_wp_error( $api_tags ) ) {
 277          echo $api_tags->get_error_message();
 278      } else {
 279          // Set up the tags in a way which can be interpreted by wp_generate_tag_cloud().
 280          $tags = array();
 281          foreach ( (array) $api_tags as $tag ) {
 282              $url                  = self_admin_url( 'plugin-install.php?tab=search&type=tag&s=' . urlencode( $tag['name'] ) );
 283              $data                 = array(
 284                  'link'  => esc_url( $url ),
 285                  'name'  => $tag['name'],
 286                  'slug'  => $tag['slug'],
 287                  'id'    => sanitize_title_with_dashes( $tag['name'] ),
 288                  'count' => $tag['count'],
 289              );
 290              $tags[ $tag['name'] ] = (object) $data;
 291          }
 292          echo wp_generate_tag_cloud(
 293              $tags,
 294              array(
 295                  /* translators: %s: Number of plugins. */
 296                  'single_text'   => __( '%s plugin' ),
 297                  /* translators: %s: Number of plugins. */
 298                  'multiple_text' => __( '%s plugins' ),
 299              )
 300          );
 301      }
 302      echo '</p><br class="clear" /></div>';
 303  }
 304  
 305  /**
 306   * Displays a search form for searching plugins.
 307   *
 308   * @since 2.7.0
 309   * @since 4.6.0 The `$type_selector` parameter was deprecated.
 310   *
 311   * @param bool $deprecated Not used.
 312   */
 313  function install_search_form( $deprecated = true ) {
 314      $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
 315      $term = isset( $_REQUEST['s'] ) ? urldecode( wp_unslash( $_REQUEST['s'] ) ) : '';
 316      ?>
 317      <form class="search-form search-plugins" method="get">
 318          <input type="hidden" name="tab" value="search" />
 319          <label for="search-plugins"><?php _e( 'Search Plugins' ); ?></label>
 320          <input type="search" name="s" id="search-plugins" value="<?php echo esc_attr( $term ); ?>" class="wp-filter-search" />
 321          <label class="screen-reader-text" for="typeselector">
 322              <?php
 323              /* translators: Hidden accessibility text. */
 324              _e( 'Search plugins by:' );
 325              ?>
 326          </label>
 327          <select name="type" id="typeselector">
 328              <option value="term"<?php selected( 'term', $type ); ?>><?php _e( 'Keyword' ); ?></option>
 329              <option value="author"<?php selected( 'author', $type ); ?>><?php _e( 'Author' ); ?></option>
 330              <option value="tag"<?php selected( 'tag', $type ); ?>><?php _ex( 'Tag', 'Plugin Installer' ); ?></option>
 331          </select>
 332          <?php submit_button( __( 'Search Plugins' ), 'hide-if-js', false, false, array( 'id' => 'search-submit' ) ); ?>
 333      </form>
 334      <?php
 335  }
 336  
 337  /**
 338   * Displays a form to upload plugins from zip files.
 339   *
 340   * @since 2.8.0
 341   */
 342  function install_plugins_upload() {
 343      ?>
 344  <div class="upload-plugin">
 345      <p class="install-help"><?php _e( 'If you have a plugin in a .zip format, you may install or update it by uploading it here.' ); ?></p>
 346      <form method="post" enctype="multipart/form-data" class="wp-upload-form" action="<?php echo esc_url( self_admin_url( 'update.php?action=upload-plugin' ) ); ?>">
 347          <?php wp_nonce_field( 'plugin-upload' ); ?>
 348          <label class="screen-reader-text" for="pluginzip">
 349              <?php
 350              /* translators: Hidden accessibility text. */
 351              _e( 'Plugin zip file' );
 352              ?>
 353          </label>
 354          <input type="file" id="pluginzip" name="pluginzip" accept=".zip" />
 355          <?php submit_button( _x( 'Install Now', 'plugin' ), '', 'install-plugin-submit', false ); ?>
 356      </form>
 357  </div>
 358      <?php
 359  }
 360  
 361  /**
 362   * Shows a username form for the favorites page.
 363   *
 364   * @since 3.5.0
 365   */
 366  function install_plugins_favorites_form() {
 367      $user   = get_user_option( 'wporg_favorites' );
 368      $action = 'save_wporg_username_' . get_current_user_id();
 369      ?>
 370      <p><?php _e( 'If you have marked plugins as favorites on WordPress.org, you can browse them here.' ); ?></p>
 371      <form method="get">
 372          <input type="hidden" name="tab" value="favorites" />
 373          <p>
 374              <label for="user"><?php _e( 'Your WordPress.org username:' ); ?></label>
 375              <input type="search" id="user" name="user" value="<?php echo esc_attr( $user ); ?>" />
 376              <input type="submit" class="button" value="<?php esc_attr_e( 'Get Favorites' ); ?>" />
 377              <input type="hidden" id="wporg-username-nonce" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( $action ) ); ?>" />
 378          </p>
 379      </form>
 380      <?php
 381  }
 382  
 383  /**
 384   * Displays plugin content based on plugin list.
 385   *
 386   * @since 2.7.0
 387   *
 388   * @global WP_List_Table $wp_list_table
 389   */
 390  function display_plugins_table() {
 391      global $wp_list_table;
 392  
 393      switch ( current_filter() ) {
 394          case 'install_plugins_beta':
 395              printf(
 396                  /* translators: %s: URL to "Features as Plugins" page. */
 397                  '<p>' . __( 'You are using a development version of WordPress. These feature plugins are also under development. <a href="%s">Learn more</a>.' ) . '</p>',
 398                  'https://make.wordpress.org/core/handbook/about/release-cycle/features-as-plugins/'
 399              );
 400              break;
 401          case 'install_plugins_featured':
 402              break;
 403          case 'install_plugins_recommended':
 404              echo '<p>' . __( 'These suggestions are based on the plugins you and other users have installed.' ) . '</p>';
 405              break;
 406          case 'install_plugins_favorites':
 407              if ( empty( $_GET['user'] ) && ! get_user_option( 'wporg_favorites' ) ) {
 408                  return;
 409              }
 410              break;
 411      }
 412      if ( isset( $_GET['tab'] ) && 'featured' === $_GET['tab'] ) {
 413          echo '<br />';
 414      }
 415      ?>
 416      <form id="plugin-filter" method="post">
 417          <?php $wp_list_table->display(); ?>
 418      </form>
 419      <?php
 420  }
 421  
 422  /**
 423   * Determines the status we can perform on a plugin.
 424   *
 425   * @since 3.0.0
 426   *
 427   * @param array|object $api  Data about the plugin retrieved from the API.
 428   * @param bool         $loop Optional. Disable further loops. Default false.
 429   * @return array {
 430   *     Plugin installation status data.
 431   *
 432   *     @type string $status  Status of a plugin. Could be one of 'install', 'update_available', 'latest_installed' or 'newer_installed'.
 433   *     @type string $url     Plugin installation URL.
 434   *     @type string $version The most recent version of the plugin.
 435   *     @type string $file    Plugin filename relative to the plugins directory.
 436   * }
 437   */
 438  function install_plugin_install_status( $api, $loop = false ) {
 439      // This function is called recursively, $loop prevents further loops.
 440      if ( is_array( $api ) ) {
 441          $api = (object) $api;
 442      }
 443  
 444      // Default to a "new" plugin.
 445      $status      = 'install';
 446      $url         = false;
 447      $update_file = false;
 448      $version     = '';
 449  
 450      /*
 451       * Check to see if this plugin is known to be installed,
 452       * and has an update awaiting it.
 453       */
 454      $update_plugins = get_site_transient( 'update_plugins' );
 455      if ( isset( $update_plugins->response ) ) {
 456          foreach ( (array) $update_plugins->response as $file => $plugin ) {
 457              if ( $plugin->slug === $api->slug ) {
 458                  $status      = 'update_available';
 459                  $update_file = $file;
 460                  $version     = $plugin->new_version;
 461                  if ( current_user_can( 'update_plugins' ) ) {
 462                      $url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' . $update_file ), 'upgrade-plugin_' . $update_file );
 463                  }
 464                  break;
 465              }
 466          }
 467      }
 468  
 469      if ( 'install' === $status ) {
 470          if ( is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
 471              $installed_plugin = get_plugins( '/' . $api->slug );
 472              if ( empty( $installed_plugin ) ) {
 473                  if ( current_user_can( 'install_plugins' ) ) {
 474                      $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $api->slug ), 'install-plugin_' . $api->slug );
 475                  }
 476              } else {
 477                  $key = array_keys( $installed_plugin );
 478                  /*
 479                   * Use the first plugin regardless of the name.
 480                   * Could have issues for multiple plugins in one directory if they share different version numbers.
 481                   */
 482                  $key = reset( $key );
 483  
 484                  $update_file = $api->slug . '/' . $key;
 485                  if ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '=' ) ) {
 486                      $status = 'latest_installed';
 487                  } elseif ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '<' ) ) {
 488                      $status  = 'newer_installed';
 489                      $version = $installed_plugin[ $key ]['Version'];
 490                  } else {
 491                      // If the above update check failed, then that probably means that the update checker has out-of-date information, force a refresh.
 492                      if ( ! $loop ) {
 493                          delete_site_transient( 'update_plugins' );
 494                          wp_update_plugins();
 495                          return install_plugin_install_status( $api, true );
 496                      }
 497                  }
 498              }
 499          } else {
 500              // "install" & no directory with that slug.
 501              if ( current_user_can( 'install_plugins' ) ) {
 502                  $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $api->slug ), 'install-plugin_' . $api->slug );
 503              }
 504          }
 505      }
 506      if ( isset( $_GET['from'] ) ) {
 507          $url .= '&amp;from=' . urlencode( wp_unslash( $_GET['from'] ) );
 508      }
 509  
 510      $file = $update_file;
 511      return compact( 'status', 'url', 'version', 'file' );
 512  }
 513  
 514  /**
 515   * Displays plugin information in dialog box form.
 516   *
 517   * @since 2.7.0
 518   *
 519   * @global string $tab
 520   */
 521  function install_plugin_information() {
 522      global $tab;
 523  
 524      if ( empty( $_REQUEST['plugin'] ) ) {
 525          return;
 526      }
 527  
 528      $api = plugins_api(
 529          'plugin_information',
 530          array(
 531              'slug' => wp_unslash( $_REQUEST['plugin'] ),
 532          )
 533      );
 534  
 535      if ( is_wp_error( $api ) ) {
 536          wp_die( $api );
 537      }
 538  
 539      $plugins_allowedtags = array(
 540          'a'          => array(
 541              'href'   => array(),
 542              'title'  => array(),
 543              'target' => array(),
 544          ),
 545          'abbr'       => array( 'title' => array() ),
 546          'acronym'    => array( 'title' => array() ),
 547          'code'       => array(),
 548          'pre'        => array(),
 549          'em'         => array(),
 550          'strong'     => array(),
 551          'div'        => array( 'class' => array() ),
 552          'span'       => array( 'class' => array() ),
 553          'p'          => array(),
 554          'br'         => array(),
 555          'ul'         => array(),
 556          'ol'         => array(),
 557          'li'         => array(),
 558          'h1'         => array(),
 559          'h2'         => array(),
 560          'h3'         => array(),
 561          'h4'         => array(),
 562          'h5'         => array(),
 563          'h6'         => array(),
 564          'img'        => array(
 565              'src'   => array(),
 566              'class' => array(),
 567              'alt'   => array(),
 568          ),
 569          'blockquote' => array( 'cite' => true ),
 570      );
 571  
 572      $plugins_section_titles = array(
 573          'description'  => _x( 'Description', 'Plugin installer section title' ),
 574          'installation' => _x( 'Installation', 'Plugin installer section title' ),
 575          'faq'          => _x( 'FAQ', 'Plugin installer section title' ),
 576          'screenshots'  => _x( 'Screenshots', 'Plugin installer section title' ),
 577          'changelog'    => _x( 'Changelog', 'Plugin installer section title' ),
 578          'reviews'      => _x( 'Reviews', 'Plugin installer section title' ),
 579          'other_notes'  => _x( 'Other Notes', 'Plugin installer section title' ),
 580      );
 581  
 582      // Sanitize HTML.
 583      foreach ( (array) $api->sections as $section_name => $content ) {
 584          $api->sections[ $section_name ] = wp_kses( $content, $plugins_allowedtags );
 585      }
 586  
 587      foreach ( array( 'version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug' ) as $key ) {
 588          if ( isset( $api->$key ) ) {
 589              $api->$key = wp_kses( $api->$key, $plugins_allowedtags );
 590          }
 591      }
 592  
 593      $_tab = esc_attr( $tab );
 594  
 595      // Default to the Description tab, Do not translate, API returns English.
 596      $section = isset( $_REQUEST['section'] ) ? wp_unslash( $_REQUEST['section'] ) : 'description';
 597      if ( empty( $section ) || ! isset( $api->sections[ $section ] ) ) {
 598          $section_titles = array_keys( (array) $api->sections );
 599          $section        = reset( $section_titles );
 600      }
 601  
 602      iframe_header( __( 'Plugin Installation' ) );
 603  
 604      $_with_banner = '';
 605  
 606      if ( ! empty( $api->banners ) && ( ! empty( $api->banners['low'] ) || ! empty( $api->banners['high'] ) ) ) {
 607          $_with_banner = 'with-banner';
 608          $low          = empty( $api->banners['low'] ) ? $api->banners['high'] : $api->banners['low'];
 609          $high         = empty( $api->banners['high'] ) ? $api->banners['low'] : $api->banners['high'];
 610          ?>
 611          <style type="text/css">
 612              #plugin-information-title.with-banner {
 613                  background-image: url( <?php echo esc_url( $low ); ?> );
 614              }
 615              @media only screen and ( -webkit-min-device-pixel-ratio: 1.5 ) {
 616                  #plugin-information-title.with-banner {
 617                      background-image: url( <?php echo esc_url( $high ); ?> );
 618                  }
 619              }
 620          </style>
 621          <?php
 622      }
 623  
 624      echo '<div id="plugin-information-scrollable">';
 625      echo "<div id='{$_tab}-title' class='{$_with_banner}'><div class='vignette'></div><h2>{$api->name}</h2></div>";
 626      echo "<div id='{$_tab}-tabs' class='{$_with_banner}'>\n";
 627  
 628      foreach ( (array) $api->sections as $section_name => $content ) {
 629          if ( 'reviews' === $section_name && ( empty( $api->ratings ) || 0 === array_sum( (array) $api->ratings ) ) ) {
 630              continue;
 631          }
 632  
 633          if ( isset( $plugins_section_titles[ $section_name ] ) ) {
 634              $title = $plugins_section_titles[ $section_name ];
 635          } else {
 636              $title = ucwords( str_replace( '_', ' ', $section_name ) );
 637          }
 638  
 639          $class       = ( $section_name === $section ) ? ' class="current"' : '';
 640          $href        = add_query_arg(
 641              array(
 642                  'tab'     => $tab,
 643                  'section' => $section_name,
 644              )
 645          );
 646          $href        = esc_url( $href );
 647          $san_section = esc_attr( $section_name );
 648          echo "\t<a name='$san_section' href='$href' $class>$title</a>\n";
 649      }
 650  
 651      echo "</div>\n";
 652  
 653      ?>
 654  <div id="<?php echo $_tab; ?>-content" class='<?php echo $_with_banner; ?>'>
 655      <div class="fyi">
 656          <ul>
 657              <?php if ( ! empty( $api->version ) ) { ?>
 658                  <li><strong><?php _e( 'Version:' ); ?></strong> <?php echo $api->version; ?></li>
 659              <?php } if ( ! empty( $api->author ) ) { ?>
 660                  <li><strong><?php _e( 'Author:' ); ?></strong> <?php echo links_add_target( $api->author, '_blank' ); ?></li>
 661              <?php } if ( ! empty( $api->last_updated ) ) { ?>
 662                  <li><strong><?php _e( 'Last Updated:' ); ?></strong>
 663                      <?php
 664                      /* translators: %s: Human-readable time difference. */
 665                      printf( __( '%s ago' ), human_time_diff( strtotime( $api->last_updated ) ) );
 666                      ?>
 667                  </li>
 668              <?php } if ( ! empty( $api->requires ) ) { ?>
 669                  <li>
 670                      <strong><?php _e( 'Requires WordPress Version:' ); ?></strong>
 671                      <?php
 672                      /* translators: %s: Version number. */
 673                      printf( __( '%s or higher' ), $api->requires );
 674                      ?>
 675                  </li>
 676              <?php } if ( ! empty( $api->tested ) ) { ?>
 677                  <li><strong><?php _e( 'Compatible up to:' ); ?></strong> <?php echo $api->tested; ?></li>
 678              <?php } if ( ! empty( $api->requires_php ) ) { ?>
 679                  <li>
 680                      <strong><?php _e( 'Requires PHP Version:' ); ?></strong>
 681                      <?php
 682                      /* translators: %s: Version number. */
 683                      printf( __( '%s or higher' ), $api->requires_php );
 684                      ?>
 685                  </li>
 686              <?php } if ( isset( $api->active_installs ) ) { ?>
 687                  <li><strong><?php _e( 'Active Installations:' ); ?></strong>
 688                  <?php
 689                  if ( $api->active_installs >= 1000000 ) {
 690                      $active_installs_millions = floor( $api->active_installs / 1000000 );
 691                      printf(
 692                          /* translators: %s: Number of millions. */
 693                          _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ),
 694                          number_format_i18n( $active_installs_millions )
 695                      );
 696                  } elseif ( $api->active_installs < 10 ) {
 697                      _ex( 'Less Than 10', 'Active plugin installations' );
 698                  } else {
 699                      echo number_format_i18n( $api->active_installs ) . '+';
 700                  }
 701                  ?>
 702                  </li>
 703              <?php } if ( ! empty( $api->slug ) && empty( $api->external ) ) { ?>
 704                  <li><a target="_blank" href="<?php echo esc_url( __( 'https://wordpress.org/plugins/' ) . $api->slug ); ?>/"><?php _e( 'WordPress.org Plugin Page &#187;' ); ?></a></li>
 705              <?php } if ( ! empty( $api->homepage ) ) { ?>
 706                  <li><a target="_blank" href="<?php echo esc_url( $api->homepage ); ?>"><?php _e( 'Plugin Homepage &#187;' ); ?></a></li>
 707              <?php } if ( ! empty( $api->donate_link ) && empty( $api->contributors ) ) { ?>
 708                  <li><a target="_blank" href="<?php echo esc_url( $api->donate_link ); ?>"><?php _e( 'Donate to this plugin &#187;' ); ?></a></li>
 709              <?php } ?>
 710          </ul>
 711          <?php if ( ! empty( $api->rating ) ) { ?>
 712              <h3><?php _e( 'Average Rating' ); ?></h3>
 713              <?php
 714              wp_star_rating(
 715                  array(
 716                      'rating' => $api->rating,
 717                      'type'   => 'percent',
 718                      'number' => $api->num_ratings,
 719                  )
 720              );
 721              ?>
 722              <p aria-hidden="true" class="fyi-description">
 723                  <?php
 724                  printf(
 725                      /* translators: %s: Number of ratings. */
 726                      _n( '(based on %s rating)', '(based on %s ratings)', $api->num_ratings ),
 727                      number_format_i18n( $api->num_ratings )
 728                  );
 729                  ?>
 730              </p>
 731              <?php
 732          }
 733  
 734          if ( ! empty( $api->ratings ) && array_sum( (array) $api->ratings ) > 0 ) {
 735              ?>
 736              <h3><?php _e( 'Reviews' ); ?></h3>
 737              <p class="fyi-description"><?php _e( 'Read all reviews on WordPress.org or write your own!' ); ?></p>
 738              <?php
 739              foreach ( $api->ratings as $key => $ratecount ) {
 740                  // Avoid div-by-zero.
 741                  $_rating    = $api->num_ratings ? ( $ratecount / $api->num_ratings ) : 0;
 742                  $aria_label = esc_attr(
 743                      sprintf(
 744                          /* translators: 1: Number of stars (used to determine singular/plural), 2: Number of reviews. */
 745                          _n(
 746                              'Reviews with %1$d star: %2$s. Opens in a new tab.',
 747                              'Reviews with %1$d stars: %2$s. Opens in a new tab.',
 748                              $key
 749                          ),
 750                          $key,
 751                          number_format_i18n( $ratecount )
 752                      )
 753                  );
 754                  ?>
 755                  <div class="counter-container">
 756                          <span class="counter-label">
 757                              <?php
 758                              printf(
 759                                  '<a href="%s" target="_blank" aria-label="%s">%s</a>',
 760                                  "https://wordpress.org/support/plugin/{$api->slug}/reviews/?filter={$key}",
 761                                  $aria_label,
 762                                  /* translators: %s: Number of stars. */
 763                                  sprintf( _n( '%d star', '%d stars', $key ), $key )
 764                              );
 765                              ?>
 766                          </span>
 767                          <span class="counter-back">
 768                              <span class="counter-bar" style="width: <?php echo 92 * $_rating; ?>px;"></span>
 769                          </span>
 770                      <span class="counter-count" aria-hidden="true"><?php echo number_format_i18n( $ratecount ); ?></span>
 771                  </div>
 772                  <?php
 773              }
 774          }
 775          if ( ! empty( $api->contributors ) ) {
 776              ?>
 777              <h3><?php _e( 'Contributors' ); ?></h3>
 778              <ul class="contributors">
 779                  <?php
 780                  foreach ( (array) $api->contributors as $contrib_username => $contrib_details ) {
 781                      $contrib_name = $contrib_details['display_name'];
 782                      if ( ! $contrib_name ) {
 783                          $contrib_name = $contrib_username;
 784                      }
 785                      $contrib_name = esc_html( $contrib_name );
 786  
 787                      $contrib_profile = esc_url( $contrib_details['profile'] );
 788                      $contrib_avatar  = esc_url( add_query_arg( 's', '36', $contrib_details['avatar'] ) );
 789  
 790                      echo "<li><a href='{$contrib_profile}' target='_blank'><img src='{$contrib_avatar}' width='18' height='18' alt='' />{$contrib_name}</a></li>";
 791                  }
 792                  ?>
 793              </ul>
 794                      <?php if ( ! empty( $api->donate_link ) ) { ?>
 795                  <a target="_blank" href="<?php echo esc_url( $api->donate_link ); ?>"><?php _e( 'Donate to this plugin &#187;' ); ?></a>
 796              <?php } ?>
 797                  <?php } ?>
 798      </div>
 799      <div id="section-holder">
 800      <?php
 801      $requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
 802      $requires_wp  = isset( $api->requires ) ? $api->requires : null;
 803  
 804      $compatible_php = is_php_version_compatible( $requires_php );
 805      $compatible_wp  = is_wp_version_compatible( $requires_wp );
 806      $tested_wp      = ( empty( $api->tested ) || version_compare( get_bloginfo( 'version' ), $api->tested, '<=' ) );
 807  
 808      if ( ! $compatible_php ) {
 809          $compatible_php_notice_message  = '<p>';
 810          $compatible_php_notice_message .= __( '<strong>Error:</strong> This plugin <strong>requires a newer version of PHP</strong>.' );
 811  
 812          if ( current_user_can( 'update_php' ) ) {
 813              $compatible_php_notice_message .= sprintf(
 814                  /* translators: %s: URL to Update PHP page. */
 815                  ' ' . __( '<a href="%s" target="_blank">Click here to learn more about updating PHP</a>.' ),
 816                  esc_url( wp_get_update_php_url() )
 817              ) . wp_update_php_annotation( '</p><p><em>', '</em>', false );
 818          } else {
 819              $compatible_php_notice_message .= '</p>';
 820          }
 821  
 822          wp_admin_notice(
 823              $compatible_php_notice_message,
 824              array(
 825                  'type'               => 'error',
 826                  'additional_classes' => array( 'notice-alt' ),
 827                  'paragraph_wrap'     => false,
 828              )
 829          );
 830      }
 831  
 832      if ( ! $tested_wp ) {
 833          wp_admin_notice(
 834              __( '<strong>Warning:</strong> This plugin <strong>has not been tested</strong> with your current version of WordPress.' ),
 835              array(
 836                  'type'               => 'warning',
 837                  'additional_classes' => array( 'notice-alt' ),
 838              )
 839          );
 840      } elseif ( ! $compatible_wp ) {
 841          $compatible_wp_notice_message = __( '<strong>Error:</strong> This plugin <strong>requires a newer version of WordPress</strong>.' );
 842          if ( current_user_can( 'update_core' ) ) {
 843              $compatible_wp_notice_message .= sprintf(
 844                  /* translators: %s: URL to WordPress Updates screen. */
 845                  ' ' . __( '<a href="%s" target="_parent">Click here to update WordPress</a>.' ),
 846                  esc_url( self_admin_url( 'update-core.php' ) )
 847              );
 848          }
 849  
 850          wp_admin_notice(
 851              $compatible_wp_notice_message,
 852              array(
 853                  'type'               => 'error',
 854                  'additional_classes' => array( 'notice-alt' ),
 855              )
 856          );
 857      }
 858  
 859      foreach ( (array) $api->sections as $section_name => $content ) {
 860          $content = links_add_base_url( $content, 'https://wordpress.org/plugins/' . $api->slug . '/' );
 861          $content = links_add_target( $content, '_blank' );
 862  
 863          $san_section = esc_attr( $section_name );
 864  
 865          $display = ( $section_name === $section ) ? 'block' : 'none';
 866  
 867          echo "\t<div id='section-{$san_section}' class='section' style='display: {$display};'>\n";
 868          echo $content;
 869          echo "\t</div>\n";
 870      }
 871      echo "</div>\n";
 872      echo "</div>\n";
 873      echo "</div>\n"; // #plugin-information-scrollable
 874      echo "<div id='$tab-footer'>\n";
 875      if ( ! empty( $api->download_link ) && ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) ) {
 876          $button = wp_get_plugin_action_button( $api->name, $api, $compatible_php, $compatible_wp );
 877          $button = str_replace( 'class="', 'class="right ', $button );
 878  
 879          if ( ! str_contains( $button, _x( 'Activate', 'plugin' ) ) ) {
 880              $button = str_replace( 'class="', 'id="plugin_install_from_iframe" class="', $button );
 881          }
 882  
 883          echo wp_kses_post( $button );
 884      }
 885      echo "</div>\n";
 886  
 887      wp_print_request_filesystem_credentials_modal();
 888      wp_print_admin_notice_templates();
 889  
 890      iframe_footer();
 891      exit;
 892  }
 893  
 894  /**
 895   * Gets the markup for the plugin install action button.
 896   *
 897   * @since 6.5.0
 898   *
 899   * @param string       $name           Plugin name.
 900   * @param array|object $data           {
 901   *     An array or object of plugin data. Can be retrieved from the API.
 902   *
 903   *     @type string   $slug             The plugin slug.
 904   *     @type string[] $requires_plugins An array of plugin dependency slugs.
 905   *     @type string   $version          The plugin's version string. Used when getting the install status.
 906   * }
 907   * @param bool         $compatible_php   The result of a PHP compatibility check.
 908   * @param bool         $compatible_wp    The result of a WP compatibility check.
 909   * @return string The markup for the dependency row button. An empty string if the user does not have capabilities.
 910   */
 911  function wp_get_plugin_action_button( $name, $data, $compatible_php, $compatible_wp ) {
 912      $button           = '';
 913      $data             = (object) $data;
 914      $status           = install_plugin_install_status( $data );
 915      $requires_plugins = $data->requires_plugins ?? array();
 916  
 917      // Determine the status of plugin dependencies.
 918      $installed_plugins                   = get_plugins();
 919      $active_plugins                      = get_option( 'active_plugins', array() );
 920      $plugin_dependencies_count           = count( $requires_plugins );
 921      $installed_plugin_dependencies_count = 0;
 922      $active_plugin_dependencies_count    = 0;
 923      foreach ( $requires_plugins as $dependency ) {
 924          foreach ( array_keys( $installed_plugins ) as $installed_plugin_file ) {
 925              if ( str_contains( $installed_plugin_file, '/' ) && explode( '/', $installed_plugin_file )[0] === $dependency ) {
 926                  ++$installed_plugin_dependencies_count;
 927              }
 928          }
 929  
 930          foreach ( $active_plugins as $active_plugin_file ) {
 931              if ( str_contains( $active_plugin_file, '/' ) && explode( '/', $active_plugin_file )[0] === $dependency ) {
 932                  ++$active_plugin_dependencies_count;
 933              }
 934          }
 935      }
 936      $all_plugin_dependencies_installed = $installed_plugin_dependencies_count === $plugin_dependencies_count;
 937      $all_plugin_dependencies_active    = $active_plugin_dependencies_count === $plugin_dependencies_count;
 938  
 939      if ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) {
 940          switch ( $status['status'] ) {
 941              case 'install':
 942                  if ( $status['url'] ) {
 943                      if ( $compatible_php && $compatible_wp && $all_plugin_dependencies_installed && ! empty( $data->download_link ) ) {
 944                          $button = sprintf(
 945                              '<a class="install-now button" data-slug="%s" href="%s" aria-label="%s" data-name="%s" role="button">%s</a>',
 946                              esc_attr( $data->slug ),
 947                              esc_url( $status['url'] ),
 948                              /* translators: %s: Plugin name and version. */
 949                              esc_attr( sprintf( _x( 'Install %s now', 'plugin' ), $name ) ),
 950                              esc_attr( $name ),
 951                              _x( 'Install Now', 'plugin' )
 952                          );
 953                      } else {
 954                          $button = sprintf(
 955                              '<button type="button" class="install-now button button-disabled" disabled="disabled">%s</button>',
 956                              _x( 'Install Now', 'plugin' )
 957                          );
 958                      }
 959                  }
 960                  break;
 961  
 962              case 'update_available':
 963                  if ( $status['url'] ) {
 964                      if ( $compatible_php && $compatible_wp ) {
 965                          $button = sprintf(
 966                              '<a class="update-now button aria-button-if-js" data-plugin="%s" data-slug="%s" href="%s" aria-label="%s" data-name="%s" role="button">%s</a>',
 967                              esc_attr( $status['file'] ),
 968                              esc_attr( $data->slug ),
 969                              esc_url( $status['url'] ),
 970                              /* translators: %s: Plugin name and version. */
 971                              esc_attr( sprintf( _x( 'Update %s now', 'plugin' ), $name ) ),
 972                              esc_attr( $name ),
 973                              _x( 'Update Now', 'plugin' )
 974                          );
 975                      } else {
 976                          $button = sprintf(
 977                              '<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
 978                              _x( 'Update Now', 'plugin' )
 979                          );
 980                      }
 981                  }
 982                  break;
 983  
 984              case 'latest_installed':
 985              case 'newer_installed':
 986                  if ( is_plugin_active( $status['file'] ) ) {
 987                      $button = sprintf(
 988                          '<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
 989                          _x( 'Active', 'plugin' )
 990                      );
 991                  } elseif ( current_user_can( 'activate_plugin', $status['file'] ) ) {
 992                      if ( $compatible_php && $compatible_wp && $all_plugin_dependencies_active ) {
 993                          $button_text = _x( 'Activate', 'plugin' );
 994                          /* translators: %s: Plugin name. */
 995                          $button_label = _x( 'Activate %s', 'plugin' );
 996                          $activate_url = add_query_arg(
 997                              array(
 998                                  '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $status['file'] ),
 999                                  'action'   => 'activate',
1000                                  'plugin'   => $status['file'],
1001                              ),
1002                              network_admin_url( 'plugins.php' )
1003                          );
1004  
1005                          if ( is_network_admin() ) {
1006                              $button_text = _x( 'Network Activate', 'plugin' );
1007                              /* translators: %s: Plugin name. */
1008                              $button_label = _x( 'Network Activate %s', 'plugin' );
1009                              $activate_url = add_query_arg( array( 'networkwide' => 1 ), $activate_url );
1010                          }
1011  
1012                          $button = sprintf(
1013                              '<a href="%1$s" data-name="%2$s" data-slug="%3$s" data-plugin="%4$s" class="button button-primary activate-now" aria-label="%5$s" role="button">%6$s</a>',
1014                              esc_url( $activate_url ),
1015                              esc_attr( $name ),
1016                              esc_attr( $data->slug ),
1017                              esc_attr( $status['file'] ),
1018                              esc_attr( sprintf( $button_label, $name ) ),
1019                              $button_text
1020                          );
1021                      } else {
1022                          $button = sprintf(
1023                              '<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
1024                              is_network_admin() ? _x( 'Network Activate', 'plugin' ) : _x( 'Activate', 'plugin' )
1025                          );
1026                      }
1027                  } else {
1028                      $button = sprintf(
1029                          '<button type="button" class="button button-disabled" disabled="disabled">%s</button>',
1030                          _x( 'Installed', 'plugin' )
1031                      );
1032                  }
1033                  break;
1034          }
1035      }
1036  
1037      return $button;
1038  }


Generated : Tue Aug 19 08:20:01 2025 Cross-referenced by PHPXref