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


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref