[ Index ]

PHP Cross Reference of WordPress

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'] ) ? 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"><?php _e( 'Search plugins by:' ); ?></label>
 324          <select name="type" id="typeselector">
 325              <option value="term"<?php selected( 'term', $type ); ?>><?php _e( 'Keyword' ); ?></option>
 326              <option value="author"<?php selected( 'author', $type ); ?>><?php _e( 'Author' ); ?></option>
 327              <option value="tag"<?php selected( 'tag', $type ); ?>><?php _ex( 'Tag', 'Plugin Installer' ); ?></option>
 328          </select>
 329          <label class="screen-reader-text" for="search-plugins"><?php _e( 'Search Plugins' ); ?></label>
 330          <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...' ); ?>" />
 331          <?php submit_button( __( 'Search Plugins' ), 'hide-if-js', false, false, array( 'id' => 'search-submit' ) ); ?>
 332      </form>
 333      <?php
 334  }
 335  
 336  /**
 337   * Displays a form to upload plugins from zip files.
 338   *
 339   * @since 2.8.0
 340   */
 341  function install_plugins_upload() {
 342      ?>
 343  <div class="upload-plugin">
 344      <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>
 345      <form method="post" enctype="multipart/form-data" class="wp-upload-form" action="<?php echo self_admin_url( 'update.php?action=upload-plugin' ); ?>">
 346          <?php wp_nonce_field( 'plugin-upload' ); ?>
 347          <label class="screen-reader-text" for="pluginzip"><?php _e( 'Plugin zip file' ); ?></label>
 348          <input type="file" id="pluginzip" name="pluginzip" accept=".zip" />
 349          <?php submit_button( __( 'Install Now' ), '', 'install-plugin-submit', false ); ?>
 350      </form>
 351  </div>
 352      <?php
 353  }
 354  
 355  /**
 356   * Shows a username form for the favorites page.
 357   *
 358   * @since 3.5.0
 359   */
 360  function install_plugins_favorites_form() {
 361      $user   = get_user_option( 'wporg_favorites' );
 362      $action = 'save_wporg_username_' . get_current_user_id();
 363      ?>
 364      <p><?php _e( 'If you have marked plugins as favorites on WordPress.org, you can browse them here.' ); ?></p>
 365      <form method="get">
 366          <input type="hidden" name="tab" value="favorites" />
 367          <p>
 368              <label for="user"><?php _e( 'Your WordPress.org username:' ); ?></label>
 369              <input type="search" id="user" name="user" value="<?php echo esc_attr( $user ); ?>" />
 370              <input type="submit" class="button" value="<?php esc_attr_e( 'Get Favorites' ); ?>" />
 371              <input type="hidden" id="wporg-username-nonce" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( $action ) ); ?>" />
 372          </p>
 373      </form>
 374      <?php
 375  }
 376  
 377  /**
 378   * Displays plugin content based on plugin list.
 379   *
 380   * @since 2.7.0
 381   *
 382   * @global WP_List_Table $wp_list_table
 383   */
 384  function display_plugins_table() {
 385      global $wp_list_table;
 386  
 387      switch ( current_filter() ) {
 388          case 'install_plugins_beta':
 389              printf(
 390                  /* translators: %s: URL to "Features as Plugins" page. */
 391                  '<p>' . __( 'You are using a development version of WordPress. These feature plugins are also under development. <a href="%s">Learn more</a>.' ) . '</p>',
 392                  'https://make.wordpress.org/core/handbook/about/release-cycle/features-as-plugins/'
 393              );
 394              break;
 395          case 'install_plugins_featured':
 396              printf(
 397                  /* translators: %s: https://wordpress.org/plugins/ */
 398                  '<p>' . __( 'Plugins extend and expand the functionality of WordPress. You may automatically install plugins from the <a href="%s">WordPress Plugin Directory</a> or upload a plugin in .zip format by clicking the button at the top of this page.' ) . '</p>',
 399                  __( 'https://wordpress.org/plugins/' )
 400              );
 401              break;
 402          case 'install_plugins_recommended':
 403              echo '<p>' . __( 'These suggestions are based on the plugins you and other users have installed.' ) . '</p>';
 404              break;
 405          case 'install_plugins_favorites':
 406              if ( empty( $_GET['user'] ) && ! get_user_option( 'wporg_favorites' ) ) {
 407                  return;
 408              }
 409              break;
 410      }
 411      ?>
 412      <form id="plugin-filter" method="post">
 413          <?php $wp_list_table->display(); ?>
 414      </form>
 415      <?php
 416  }
 417  
 418  /**
 419   * Determines the status we can perform on a plugin.
 420   *
 421   * @since 3.0.0
 422   *
 423   * @param array|object $api  Data about the plugin retrieved from the API.
 424   * @param bool         $loop Optional. Disable further loops. Default false.
 425   * @return array {
 426   *     Plugin installation status data.
 427   *
 428   *     @type string $status  Status of a plugin. Could be one of 'install', 'update_available', 'latest_installed' or 'newer_installed'.
 429   *     @type string $url     Plugin installation URL.
 430   *     @type string $version The most recent version of the plugin.
 431   *     @type string $file    Plugin filename relative to the plugins directory.
 432   * }
 433   */
 434  function install_plugin_install_status( $api, $loop = false ) {
 435      // This function is called recursively, $loop prevents further loops.
 436      if ( is_array( $api ) ) {
 437          $api = (object) $api;
 438      }
 439  
 440      // Default to a "new" plugin.
 441      $status      = 'install';
 442      $url         = false;
 443      $update_file = false;
 444      $version     = '';
 445  
 446      /*
 447       * Check to see if this plugin is known to be installed,
 448       * and has an update awaiting it.
 449       */
 450      $update_plugins = get_site_transient( 'update_plugins' );
 451      if ( isset( $update_plugins->response ) ) {
 452          foreach ( (array) $update_plugins->response as $file => $plugin ) {
 453              if ( $plugin->slug === $api->slug ) {
 454                  $status      = 'update_available';
 455                  $update_file = $file;
 456                  $version     = $plugin->new_version;
 457                  if ( current_user_can( 'update_plugins' ) ) {
 458                      $url = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' . $update_file ), 'upgrade-plugin_' . $update_file );
 459                  }
 460                  break;
 461              }
 462          }
 463      }
 464  
 465      if ( 'install' === $status ) {
 466          if ( is_dir( WP_PLUGIN_DIR . '/' . $api->slug ) ) {
 467              $installed_plugin = get_plugins( '/' . $api->slug );
 468              if ( empty( $installed_plugin ) ) {
 469                  if ( current_user_can( 'install_plugins' ) ) {
 470                      $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $api->slug ), 'install-plugin_' . $api->slug );
 471                  }
 472              } else {
 473                  $key = array_keys( $installed_plugin );
 474                  // Use the first plugin regardless of the name.
 475                  // Could have issues for multiple plugins in one directory if they share different version numbers.
 476                  $key = reset( $key );
 477  
 478                  $update_file = $api->slug . '/' . $key;
 479                  if ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '=' ) ) {
 480                      $status = 'latest_installed';
 481                  } elseif ( version_compare( $api->version, $installed_plugin[ $key ]['Version'], '<' ) ) {
 482                      $status  = 'newer_installed';
 483                      $version = $installed_plugin[ $key ]['Version'];
 484                  } else {
 485                      // If the above update check failed, then that probably means that the update checker has out-of-date information, force a refresh.
 486                      if ( ! $loop ) {
 487                          delete_site_transient( 'update_plugins' );
 488                          wp_update_plugins();
 489                          return install_plugin_install_status( $api, true );
 490                      }
 491                  }
 492              }
 493          } else {
 494              // "install" & no directory with that slug.
 495              if ( current_user_can( 'install_plugins' ) ) {
 496                  $url = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . $api->slug ), 'install-plugin_' . $api->slug );
 497              }
 498          }
 499      }
 500      if ( isset( $_GET['from'] ) ) {
 501          $url .= '&amp;from=' . urlencode( wp_unslash( $_GET['from'] ) );
 502      }
 503  
 504      $file = $update_file;
 505      return compact( 'status', 'url', 'version', 'file' );
 506  }
 507  
 508  /**
 509   * Displays plugin information in dialog box form.
 510   *
 511   * @since 2.7.0
 512   *
 513   * @global string $tab
 514   */
 515  function install_plugin_information() {
 516      global $tab;
 517  
 518      if ( empty( $_REQUEST['plugin'] ) ) {
 519          return;
 520      }
 521  
 522      $api = plugins_api(
 523          'plugin_information',
 524          array(
 525              'slug' => wp_unslash( $_REQUEST['plugin'] ),
 526          )
 527      );
 528  
 529      if ( is_wp_error( $api ) ) {
 530          wp_die( $api );
 531      }
 532  
 533      $plugins_allowedtags = array(
 534          'a'          => array(
 535              'href'   => array(),
 536              'title'  => array(),
 537              'target' => array(),
 538          ),
 539          'abbr'       => array( 'title' => array() ),
 540          'acronym'    => array( 'title' => array() ),
 541          'code'       => array(),
 542          'pre'        => array(),
 543          'em'         => array(),
 544          'strong'     => array(),
 545          'div'        => array( 'class' => array() ),
 546          'span'       => array( 'class' => array() ),
 547          'p'          => array(),
 548          'br'         => array(),
 549          'ul'         => array(),
 550          'ol'         => array(),
 551          'li'         => array(),
 552          'h1'         => array(),
 553          'h2'         => array(),
 554          'h3'         => array(),
 555          'h4'         => array(),
 556          'h5'         => array(),
 557          'h6'         => array(),
 558          'img'        => array(
 559              'src'   => array(),
 560              'class' => array(),
 561              'alt'   => array(),
 562          ),
 563          'blockquote' => array( 'cite' => true ),
 564      );
 565  
 566      $plugins_section_titles = array(
 567          'description'  => _x( 'Description', 'Plugin installer section title' ),
 568          'installation' => _x( 'Installation', 'Plugin installer section title' ),
 569          'faq'          => _x( 'FAQ', 'Plugin installer section title' ),
 570          'screenshots'  => _x( 'Screenshots', 'Plugin installer section title' ),
 571          'changelog'    => _x( 'Changelog', 'Plugin installer section title' ),
 572          'reviews'      => _x( 'Reviews', 'Plugin installer section title' ),
 573          'other_notes'  => _x( 'Other Notes', 'Plugin installer section title' ),
 574      );
 575  
 576      // Sanitize HTML.
 577      foreach ( (array) $api->sections as $section_name => $content ) {
 578          $api->sections[ $section_name ] = wp_kses( $content, $plugins_allowedtags );
 579      }
 580  
 581      foreach ( array( 'version', 'author', 'requires', 'tested', 'homepage', 'downloaded', 'slug' ) as $key ) {
 582          if ( isset( $api->$key ) ) {
 583              $api->$key = wp_kses( $api->$key, $plugins_allowedtags );
 584          }
 585      }
 586  
 587      $_tab = esc_attr( $tab );
 588  
 589      // Default to the Description tab, Do not translate, API returns English.
 590      $section = isset( $_REQUEST['section'] ) ? wp_unslash( $_REQUEST['section'] ) : 'description';
 591      if ( empty( $section ) || ! isset( $api->sections[ $section ] ) ) {
 592          $section_titles = array_keys( (array) $api->sections );
 593          $section        = reset( $section_titles );
 594      }
 595  
 596      iframe_header( __( 'Plugin Installation' ) );
 597  
 598      $_with_banner = '';
 599  
 600      if ( ! empty( $api->banners ) && ( ! empty( $api->banners['low'] ) || ! empty( $api->banners['high'] ) ) ) {
 601          $_with_banner = 'with-banner';
 602          $low          = empty( $api->banners['low'] ) ? $api->banners['high'] : $api->banners['low'];
 603          $high         = empty( $api->banners['high'] ) ? $api->banners['low'] : $api->banners['high'];
 604          ?>
 605          <style type="text/css">
 606              #plugin-information-title.with-banner {
 607                  background-image: url( <?php echo esc_url( $low ); ?> );
 608              }
 609              @media only screen and ( -webkit-min-device-pixel-ratio: 1.5 ) {
 610                  #plugin-information-title.with-banner {
 611                      background-image: url( <?php echo esc_url( $high ); ?> );
 612                  }
 613              }
 614          </style>
 615          <?php
 616      }
 617  
 618      echo '<div id="plugin-information-scrollable">';
 619      echo "<div id='{$_tab}-title' class='{$_with_banner}'><div class='vignette'></div><h2>{$api->name}</h2></div>";
 620      echo "<div id='{$_tab}-tabs' class='{$_with_banner}'>\n";
 621  
 622      foreach ( (array) $api->sections as $section_name => $content ) {
 623          if ( 'reviews' === $section_name && ( empty( $api->ratings ) || 0 === array_sum( (array) $api->ratings ) ) ) {
 624              continue;
 625          }
 626  
 627          if ( isset( $plugins_section_titles[ $section_name ] ) ) {
 628              $title = $plugins_section_titles[ $section_name ];
 629          } else {
 630              $title = ucwords( str_replace( '_', ' ', $section_name ) );
 631          }
 632  
 633          $class       = ( $section_name === $section ) ? ' class="current"' : '';
 634          $href        = add_query_arg(
 635              array(
 636                  'tab'     => $tab,
 637                  'section' => $section_name,
 638              )
 639          );
 640          $href        = esc_url( $href );
 641          $san_section = esc_attr( $section_name );
 642          echo "\t<a name='$san_section' href='$href' $class>$title</a>\n";
 643      }
 644  
 645      echo "</div>\n";
 646  
 647      ?>
 648  <div id="<?php echo $_tab; ?>-content" class='<?php echo $_with_banner; ?>'>
 649      <div class="fyi">
 650          <ul>
 651              <?php if ( ! empty( $api->version ) ) { ?>
 652                  <li><strong><?php _e( 'Version:' ); ?></strong> <?php echo $api->version; ?></li>
 653              <?php } if ( ! empty( $api->author ) ) { ?>
 654                  <li><strong><?php _e( 'Author:' ); ?></strong> <?php echo links_add_target( $api->author, '_blank' ); ?></li>
 655              <?php } if ( ! empty( $api->last_updated ) ) { ?>
 656                  <li><strong><?php _e( 'Last Updated:' ); ?></strong>
 657                      <?php
 658                      /* translators: %s: Human-readable time difference. */
 659                      printf( __( '%s ago' ), human_time_diff( strtotime( $api->last_updated ) ) );
 660                      ?>
 661                  </li>
 662              <?php } if ( ! empty( $api->requires ) ) { ?>
 663                  <li>
 664                      <strong><?php _e( 'Requires WordPress Version:' ); ?></strong>
 665                      <?php
 666                      /* translators: %s: Version number. */
 667                      printf( __( '%s or higher' ), $api->requires );
 668                      ?>
 669                  </li>
 670              <?php } if ( ! empty( $api->tested ) ) { ?>
 671                  <li><strong><?php _e( 'Compatible up to:' ); ?></strong> <?php echo $api->tested; ?></li>
 672              <?php } if ( ! empty( $api->requires_php ) ) { ?>
 673                  <li>
 674                      <strong><?php _e( 'Requires PHP Version:' ); ?></strong>
 675                      <?php
 676                      /* translators: %s: Version number. */
 677                      printf( __( '%s or higher' ), $api->requires_php );
 678                      ?>
 679                  </li>
 680              <?php } if ( isset( $api->active_installs ) ) { ?>
 681                  <li><strong><?php _e( 'Active Installations:' ); ?></strong>
 682                  <?php
 683                  if ( $api->active_installs >= 1000000 ) {
 684                      $active_installs_millions = floor( $api->active_installs / 1000000 );
 685                      printf(
 686                          /* translators: %s: Number of millions. */
 687                          _nx( '%s+ Million', '%s+ Million', $active_installs_millions, 'Active plugin installations' ),
 688                          number_format_i18n( $active_installs_millions )
 689                      );
 690                  } elseif ( $api->active_installs < 10 ) {
 691                      _ex( 'Less Than 10', 'Active plugin installations' );
 692                  } else {
 693                      echo number_format_i18n( $api->active_installs ) . '+';
 694                  }
 695                  ?>
 696                  </li>
 697              <?php } if ( ! empty( $api->slug ) && empty( $api->external ) ) { ?>
 698                  <li><a target="_blank" href="<?php echo esc_url( __( 'https://wordpress.org/plugins/' ) . $api->slug ); ?>/"><?php _e( 'WordPress.org Plugin Page &#187;' ); ?></a></li>
 699              <?php } if ( ! empty( $api->homepage ) ) { ?>
 700                  <li><a target="_blank" href="<?php echo esc_url( $api->homepage ); ?>"><?php _e( 'Plugin Homepage &#187;' ); ?></a></li>
 701              <?php } if ( ! empty( $api->donate_link ) && empty( $api->contributors ) ) { ?>
 702                  <li><a target="_blank" href="<?php echo esc_url( $api->donate_link ); ?>"><?php _e( 'Donate to this plugin &#187;' ); ?></a></li>
 703              <?php } ?>
 704          </ul>
 705          <?php if ( ! empty( $api->rating ) ) { ?>
 706              <h3><?php _e( 'Average Rating' ); ?></h3>
 707              <?php
 708              wp_star_rating(
 709                  array(
 710                      'rating' => $api->rating,
 711                      'type'   => 'percent',
 712                      'number' => $api->num_ratings,
 713                  )
 714              );
 715              ?>
 716              <p aria-hidden="true" class="fyi-description">
 717                  <?php
 718                  printf(
 719                      /* translators: %s: Number of ratings. */
 720                      _n( '(based on %s rating)', '(based on %s ratings)', $api->num_ratings ),
 721                      number_format_i18n( $api->num_ratings )
 722                  );
 723                  ?>
 724              </p>
 725              <?php
 726          }
 727  
 728          if ( ! empty( $api->ratings ) && array_sum( (array) $api->ratings ) > 0 ) {
 729              ?>
 730              <h3><?php _e( 'Reviews' ); ?></h3>
 731              <p class="fyi-description"><?php _e( 'Read all reviews on WordPress.org or write your own!' ); ?></p>
 732              <?php
 733              foreach ( $api->ratings as $key => $ratecount ) {
 734                  // Avoid div-by-zero.
 735                  $_rating    = $api->num_ratings ? ( $ratecount / $api->num_ratings ) : 0;
 736                  $aria_label = esc_attr(
 737                      sprintf(
 738                          /* translators: 1: Number of stars (used to determine singular/plural), 2: Number of reviews. */
 739                          _n(
 740                              'Reviews with %1$d star: %2$s. Opens in a new tab.',
 741                              'Reviews with %1$d stars: %2$s. Opens in a new tab.',
 742                              $key
 743                          ),
 744                          $key,
 745                          number_format_i18n( $ratecount )
 746                      )
 747                  );
 748                  ?>
 749                  <div class="counter-container">
 750                          <span class="counter-label">
 751                              <?php
 752                              printf(
 753                                  '<a href="%s" target="_blank" aria-label="%s">%s</a>',
 754                                  "https://wordpress.org/support/plugin/{$api->slug}/reviews/?filter={$key}",
 755                                  $aria_label,
 756                                  /* translators: %s: Number of stars. */
 757                                  sprintf( _n( '%d star', '%d stars', $key ), $key )
 758                              );
 759                              ?>
 760                          </span>
 761                          <span class="counter-back">
 762                              <span class="counter-bar" style="width: <?php echo 92 * $_rating; ?>px;"></span>
 763                          </span>
 764                      <span class="counter-count" aria-hidden="true"><?php echo number_format_i18n( $ratecount ); ?></span>
 765                  </div>
 766                  <?php
 767              }
 768          }
 769          if ( ! empty( $api->contributors ) ) {
 770              ?>
 771              <h3><?php _e( 'Contributors' ); ?></h3>
 772              <ul class="contributors">
 773                  <?php
 774                  foreach ( (array) $api->contributors as $contrib_username => $contrib_details ) {
 775                      $contrib_name = $contrib_details['display_name'];
 776                      if ( ! $contrib_name ) {
 777                          $contrib_name = $contrib_username;
 778                      }
 779                      $contrib_name = esc_html( $contrib_name );
 780  
 781                      $contrib_profile = esc_url( $contrib_details['profile'] );
 782                      $contrib_avatar  = esc_url( add_query_arg( 's', '36', $contrib_details['avatar'] ) );
 783  
 784                      echo "<li><a href='{$contrib_profile}' target='_blank'><img src='{$contrib_avatar}' width='18' height='18' alt='' />{$contrib_name}</a></li>";
 785                  }
 786                  ?>
 787              </ul>
 788                      <?php if ( ! empty( $api->donate_link ) ) { ?>
 789                  <a target="_blank" href="<?php echo esc_url( $api->donate_link ); ?>"><?php _e( 'Donate to this plugin &#187;' ); ?></a>
 790              <?php } ?>
 791                  <?php } ?>
 792      </div>
 793      <div id="section-holder">
 794      <?php
 795      $requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
 796      $requires_wp  = isset( $api->requires ) ? $api->requires : null;
 797  
 798      $compatible_php = is_php_version_compatible( $requires_php );
 799      $compatible_wp  = is_wp_version_compatible( $requires_wp );
 800      $tested_wp      = ( empty( $api->tested ) || version_compare( get_bloginfo( 'version' ), $api->tested, '<=' ) );
 801  
 802      if ( ! $compatible_php ) {
 803          echo '<div class="notice notice-error notice-alt"><p>';
 804          _e( '<strong>Error:</strong> This plugin <strong>requires a newer version of PHP</strong>.' );
 805          if ( current_user_can( 'update_php' ) ) {
 806              printf(
 807                  /* translators: %s: URL to Update PHP page. */
 808                  ' ' . __( '<a href="%s" target="_blank">Click here to learn more about updating PHP</a>.' ),
 809                  esc_url( wp_get_update_php_url() )
 810              );
 811  
 812              wp_update_php_annotation( '</p><p><em>', '</em>' );
 813          } else {
 814              echo '</p>';
 815          }
 816          echo '</div>';
 817      }
 818  
 819      if ( ! $tested_wp ) {
 820          echo '<div class="notice notice-warning notice-alt"><p>';
 821          _e( '<strong>Warning:</strong> This plugin <strong>has not been tested</strong> with your current version of WordPress.' );
 822          echo '</p></div>';
 823      } elseif ( ! $compatible_wp ) {
 824          echo '<div class="notice notice-error notice-alt"><p>';
 825          _e( '<strong>Error:</strong> This plugin <strong>requires a newer version of WordPress</strong>.' );
 826          if ( current_user_can( 'update_core' ) ) {
 827              printf(
 828                  /* translators: %s: URL to WordPress Updates screen. */
 829                  ' ' . __( '<a href="%s" target="_parent">Click here to update WordPress</a>.' ),
 830                  self_admin_url( 'update-core.php' )
 831              );
 832          }
 833          echo '</p></div>';
 834      }
 835  
 836      foreach ( (array) $api->sections as $section_name => $content ) {
 837          $content = links_add_base_url( $content, 'https://wordpress.org/plugins/' . $api->slug . '/' );
 838          $content = links_add_target( $content, '_blank' );
 839  
 840          $san_section = esc_attr( $section_name );
 841  
 842          $display = ( $section_name === $section ) ? 'block' : 'none';
 843  
 844          echo "\t<div id='section-{$san_section}' class='section' style='display: {$display};'>\n";
 845          echo $content;
 846          echo "\t</div>\n";
 847      }
 848      echo "</div>\n";
 849      echo "</div>\n";
 850      echo "</div>\n"; // #plugin-information-scrollable
 851      echo "<div id='$tab-footer'>\n";
 852      if ( ! empty( $api->download_link ) && ( current_user_can( 'install_plugins' ) || current_user_can( 'update_plugins' ) ) ) {
 853          $status = install_plugin_install_status( $api );
 854          switch ( $status['status'] ) {
 855              case 'install':
 856                  if ( $status['url'] ) {
 857                      if ( $compatible_php && $compatible_wp ) {
 858                          echo '<a data-slug="' . esc_attr( $api->slug ) . '" id="plugin_install_from_iframe" class="button button-primary right" href="' . $status['url'] . '" target="_parent">' . __( 'Install Now' ) . '</a>';
 859                      } else {
 860                          printf(
 861                              '<button type="button" class="button button-primary button-disabled right" disabled="disabled">%s</button>',
 862                              _x( 'Cannot Install', 'plugin' )
 863                          );
 864                      }
 865                  }
 866                  break;
 867              case 'update_available':
 868                  if ( $status['url'] ) {
 869                      if ( $compatible_php ) {
 870                          echo '<a data-slug="' . esc_attr( $api->slug ) . '" data-plugin="' . esc_attr( $status['file'] ) . '" id="plugin_update_from_iframe" class="button button-primary right" href="' . $status['url'] . '" target="_parent">' . __( 'Install Update Now' ) . '</a>';
 871                      } else {
 872                          printf(
 873                              '<button type="button" class="button button-primary button-disabled right" disabled="disabled">%s</button>',
 874                              _x( 'Cannot Update', 'plugin' )
 875                          );
 876                      }
 877                  }
 878                  break;
 879              case 'newer_installed':
 880                  /* translators: %s: Plugin version. */
 881                  echo '<a class="button button-primary right disabled">' . sprintf( __( 'Newer Version (%s) Installed' ), esc_html( $status['version'] ) ) . '</a>';
 882                  break;
 883              case 'latest_installed':
 884                  echo '<a class="button button-primary right disabled">' . __( 'Latest Version Installed' ) . '</a>';
 885                  break;
 886          }
 887      }
 888      echo "</div>\n";
 889  
 890      iframe_footer();
 891      exit;
 892  }


Generated: Tue Dec 24 01:00:02 2024 Cross-referenced by PHPXref 0.7.1