[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> file.php (source)

   1  <?php
   2  /**
   3   * Filesystem API: Top-level functionality
   4   *
   5   * Functions for reading, writing, modifying, and deleting files on the file system.
   6   * Includes functionality for theme-specific files as well as operations for uploading,
   7   * archiving, and rendering output when necessary.
   8   *
   9   * @package WordPress
  10   * @subpackage Filesystem
  11   * @since 2.3.0
  12   */
  13  
  14  /** The descriptions for theme files. */
  15  $wp_file_descriptions = array(
  16      'functions.php'         => __( 'Theme Functions' ),
  17      'header.php'            => __( 'Theme Header' ),
  18      'footer.php'            => __( 'Theme Footer' ),
  19      'sidebar.php'           => __( 'Sidebar' ),
  20      'comments.php'          => __( 'Comments' ),
  21      'searchform.php'        => __( 'Search Form' ),
  22      '404.php'               => __( '404 Template' ),
  23      'link.php'              => __( 'Links Template' ),
  24      // Archives.
  25      'index.php'             => __( 'Main Index Template' ),
  26      'archive.php'           => __( 'Archives' ),
  27      'author.php'            => __( 'Author Template' ),
  28      'taxonomy.php'          => __( 'Taxonomy Template' ),
  29      'category.php'          => __( 'Category Template' ),
  30      'tag.php'               => __( 'Tag Template' ),
  31      'home.php'              => __( 'Posts Page' ),
  32      'search.php'            => __( 'Search Results' ),
  33      'date.php'              => __( 'Date Template' ),
  34      // Content.
  35      'singular.php'          => __( 'Singular Template' ),
  36      'single.php'            => __( 'Single Post' ),
  37      'page.php'              => __( 'Single Page' ),
  38      'front-page.php'        => __( 'Homepage' ),
  39      'privacy-policy.php'    => __( 'Privacy Policy Page' ),
  40      // Attachments.
  41      'attachment.php'        => __( 'Attachment Template' ),
  42      'image.php'             => __( 'Image Attachment Template' ),
  43      'video.php'             => __( 'Video Attachment Template' ),
  44      'audio.php'             => __( 'Audio Attachment Template' ),
  45      'application.php'       => __( 'Application Attachment Template' ),
  46      // Embeds.
  47      'embed.php'             => __( 'Embed Template' ),
  48      'embed-404.php'         => __( 'Embed 404 Template' ),
  49      'embed-content.php'     => __( 'Embed Content Template' ),
  50      'header-embed.php'      => __( 'Embed Header Template' ),
  51      'footer-embed.php'      => __( 'Embed Footer Template' ),
  52      // Stylesheets.
  53      'style.css'             => __( 'Stylesheet' ),
  54      'editor-style.css'      => __( 'Visual Editor Stylesheet' ),
  55      'editor-style-rtl.css'  => __( 'Visual Editor RTL Stylesheet' ),
  56      'rtl.css'               => __( 'RTL Stylesheet' ),
  57      // Other.
  58      'my-hacks.php'          => __( 'my-hacks.php (legacy hacks support)' ),
  59      '.htaccess'             => __( '.htaccess (for rewrite rules )' ),
  60      // Deprecated files.
  61      'wp-layout.css'         => __( 'Stylesheet' ),
  62      'wp-comments.php'       => __( 'Comments Template' ),
  63      'wp-comments-popup.php' => __( 'Popup Comments Template' ),
  64      'comments-popup.php'    => __( 'Popup Comments' ),
  65  );
  66  
  67  /**
  68   * Get the description for standard WordPress theme files and other various standard
  69   * WordPress files
  70   *
  71   * @since 1.5.0
  72   *
  73   * @global array $wp_file_descriptions Theme file descriptions.
  74   * @global array $allowed_files        List of allowed files.
  75   * @param string $file Filesystem path or filename
  76   * @return string Description of file from $wp_file_descriptions or basename of $file if description doesn't exist.
  77   *                Appends 'Page Template' to basename of $file if the file is a page template
  78   */
  79  function get_file_description( $file ) {
  80      global $wp_file_descriptions, $allowed_files;
  81  
  82      $dirname = pathinfo( $file, PATHINFO_DIRNAME );
  83  
  84      $file_path = $allowed_files[ $file ];
  85      if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $dirname ) {
  86          return $wp_file_descriptions[ basename( $file ) ];
  87      } elseif ( file_exists( $file_path ) && is_file( $file_path ) ) {
  88          $template_data = implode( '', file( $file_path ) );
  89          if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) ) {
  90              /* translators: %s: Template name. */
  91              return sprintf( __( '%s Page Template' ), _cleanup_header_comment( $name[1] ) );
  92          }
  93      }
  94  
  95      return trim( basename( $file ) );
  96  }
  97  
  98  /**
  99   * Get the absolute filesystem path to the root of the WordPress installation
 100   *
 101   * @since 1.5.0
 102   *
 103   * @return string Full filesystem path to the root of the WordPress installation
 104   */
 105  function get_home_path() {
 106      $home    = set_url_scheme( get_option( 'home' ), 'http' );
 107      $siteurl = set_url_scheme( get_option( 'siteurl' ), 'http' );
 108      if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
 109          $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
 110          $pos                 = strripos( str_replace( '\\', '/', $_SERVER['SCRIPT_FILENAME'] ), trailingslashit( $wp_path_rel_to_home ) );
 111          $home_path           = substr( $_SERVER['SCRIPT_FILENAME'], 0, $pos );
 112          $home_path           = trailingslashit( $home_path );
 113      } else {
 114          $home_path = ABSPATH;
 115      }
 116  
 117      return str_replace( '\\', '/', $home_path );
 118  }
 119  
 120  /**
 121   * Returns a listing of all files in the specified folder and all subdirectories up to 100 levels deep.
 122   * The depth of the recursiveness can be controlled by the $levels param.
 123   *
 124   * @since 2.6.0
 125   * @since 4.9.0 Added the `$exclusions` parameter.
 126   *
 127   * @param string   $folder     Optional. Full path to folder. Default empty.
 128   * @param int      $levels     Optional. Levels of folders to follow, Default 100 (PHP Loop limit).
 129   * @param string[] $exclusions Optional. List of folders and files to skip.
 130   * @return bool|string[] False on failure, else array of files.
 131   */
 132  function list_files( $folder = '', $levels = 100, $exclusions = array() ) {
 133      if ( empty( $folder ) ) {
 134          return false;
 135      }
 136  
 137      $folder = trailingslashit( $folder );
 138  
 139      if ( ! $levels ) {
 140          return false;
 141      }
 142  
 143      $files = array();
 144  
 145      $dir = @opendir( $folder );
 146      if ( $dir ) {
 147          while ( ( $file = readdir( $dir ) ) !== false ) {
 148              // Skip current and parent folder links.
 149              if ( in_array( $file, array( '.', '..' ), true ) ) {
 150                  continue;
 151              }
 152  
 153              // Skip hidden and excluded files.
 154              if ( '.' === $file[0] || in_array( $file, $exclusions, true ) ) {
 155                  continue;
 156              }
 157  
 158              if ( is_dir( $folder . $file ) ) {
 159                  $files2 = list_files( $folder . $file, $levels - 1 );
 160                  if ( $files2 ) {
 161                      $files = array_merge( $files, $files2 );
 162                  } else {
 163                      $files[] = $folder . $file . '/';
 164                  }
 165              } else {
 166                  $files[] = $folder . $file;
 167              }
 168          }
 169  
 170          closedir( $dir );
 171      }
 172  
 173      return $files;
 174  }
 175  
 176  /**
 177   * Get list of file extensions that are editable in plugins.
 178   *
 179   * @since 4.9.0
 180   *
 181   * @param string $plugin Path to the plugin file relative to the plugins directory.
 182   * @return string[] Array of editable file extensions.
 183   */
 184  function wp_get_plugin_file_editable_extensions( $plugin ) {
 185  
 186      $editable_extensions = array(
 187          'bash',
 188          'conf',
 189          'css',
 190          'diff',
 191          'htm',
 192          'html',
 193          'http',
 194          'inc',
 195          'include',
 196          'js',
 197          'json',
 198          'jsx',
 199          'less',
 200          'md',
 201          'patch',
 202          'php',
 203          'php3',
 204          'php4',
 205          'php5',
 206          'php7',
 207          'phps',
 208          'phtml',
 209          'sass',
 210          'scss',
 211          'sh',
 212          'sql',
 213          'svg',
 214          'text',
 215          'txt',
 216          'xml',
 217          'yaml',
 218          'yml',
 219      );
 220  
 221      /**
 222       * Filters file type extensions editable in the plugin editor.
 223       *
 224       * @since 2.8.0
 225       * @since 4.9.0 Added the `$plugin` parameter.
 226       *
 227       * @param string[] $editable_extensions An array of editable plugin file extensions.
 228       * @param string   $plugin              Path to the plugin file relative to the plugins directory.
 229       */
 230      $editable_extensions = (array) apply_filters( 'editable_extensions', $editable_extensions, $plugin );
 231  
 232      return $editable_extensions;
 233  }
 234  
 235  /**
 236   * Get list of file extensions that are editable for a given theme.
 237   *
 238   * @param WP_Theme $theme Theme object.
 239   * @return string[] Array of editable file extensions.
 240   */
 241  function wp_get_theme_file_editable_extensions( $theme ) {
 242  
 243      $default_types = array(
 244          'bash',
 245          'conf',
 246          'css',
 247          'diff',
 248          'htm',
 249          'html',
 250          'http',
 251          'inc',
 252          'include',
 253          'js',
 254          'json',
 255          'jsx',
 256          'less',
 257          'md',
 258          'patch',
 259          'php',
 260          'php3',
 261          'php4',
 262          'php5',
 263          'php7',
 264          'phps',
 265          'phtml',
 266          'sass',
 267          'scss',
 268          'sh',
 269          'sql',
 270          'svg',
 271          'text',
 272          'txt',
 273          'xml',
 274          'yaml',
 275          'yml',
 276      );
 277  
 278      /**
 279       * Filters the list of file types allowed for editing in the Theme editor.
 280       *
 281       * @since 4.4.0
 282       *
 283       * @param string[] $default_types List of allowed file types.
 284       * @param WP_Theme $theme         The current Theme object.
 285       */
 286      $file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
 287  
 288      // Ensure that default types are still there.
 289      return array_unique( array_merge( $file_types, $default_types ) );
 290  }
 291  
 292  /**
 293   * Print file editor templates (for plugins and themes).
 294   *
 295   * @since 4.9.0
 296   */
 297  function wp_print_file_editor_templates() {
 298      ?>
 299      <script type="text/html" id="tmpl-wp-file-editor-notice">
 300          <div class="notice inline notice-{{ data.type || 'info' }} {{ data.alt ? 'notice-alt' : '' }} {{ data.dismissible ? 'is-dismissible' : '' }} {{ data.classes || '' }}">
 301              <# if ( 'php_error' === data.code ) { #>
 302                  <p>
 303                      <?php
 304                      printf(
 305                          /* translators: 1: Line number, 2: File path. */
 306                          __( 'Your PHP code changes were rolled back due to an error on line %1$s of file %2$s. Please fix and try saving again.' ),
 307                          '{{ data.line }}',
 308                          '{{ data.file }}'
 309                      );
 310                      ?>
 311                  </p>
 312                  <pre>{{ data.message }}</pre>
 313              <# } else if ( 'file_not_writable' === data.code ) { #>
 314                  <p>
 315                      <?php
 316                      printf(
 317                          /* translators: %s: Documentation URL. */
 318                          __( 'You need to make this file writable before you can save your changes. See <a href="%s">Changing File Permissions</a> for more information.' ),
 319                          __( 'https://wordpress.org/support/article/changing-file-permissions/' )
 320                      );
 321                      ?>
 322                  </p>
 323              <# } else { #>
 324                  <p>{{ data.message || data.code }}</p>
 325  
 326                  <# if ( 'lint_errors' === data.code ) { #>
 327                      <p>
 328                          <# var elementId = 'el-' + String( Math.random() ); #>
 329                          <input id="{{ elementId }}"  type="checkbox">
 330                          <label for="{{ elementId }}"><?php _e( 'Update anyway, even though it might break your site?' ); ?></label>
 331                      </p>
 332                  <# } #>
 333              <# } #>
 334              <# if ( data.dismissible ) { #>
 335                  <button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e( 'Dismiss' ); ?></span></button>
 336              <# } #>
 337          </div>
 338      </script>
 339      <?php
 340  }
 341  
 342  /**
 343   * Attempt to edit a file for a theme or plugin.
 344   *
 345   * When editing a PHP file, loopback requests will be made to the admin and the homepage
 346   * to attempt to see if there is a fatal error introduced. If so, the PHP change will be
 347   * reverted.
 348   *
 349   * @since 4.9.0
 350   *
 351   * @param string[] $args {
 352   *     Args. Note that all of the arg values are already unslashed. They are, however,
 353   *     coming straight from `$_POST` and are not validated or sanitized in any way.
 354   *
 355   *     @type string $file       Relative path to file.
 356   *     @type string $plugin     Path to the plugin file relative to the plugins directory.
 357   *     @type string $theme      Theme being edited.
 358   *     @type string $newcontent New content for the file.
 359   *     @type string $nonce      Nonce.
 360   * }
 361   * @return true|WP_Error True on success or `WP_Error` on failure.
 362   */
 363  function wp_edit_theme_plugin_file( $args ) {
 364      if ( empty( $args['file'] ) ) {
 365          return new WP_Error( 'missing_file' );
 366      }
 367      $file = $args['file'];
 368      if ( 0 !== validate_file( $file ) ) {
 369          return new WP_Error( 'bad_file' );
 370      }
 371  
 372      if ( ! isset( $args['newcontent'] ) ) {
 373          return new WP_Error( 'missing_content' );
 374      }
 375      $content = $args['newcontent'];
 376  
 377      if ( ! isset( $args['nonce'] ) ) {
 378          return new WP_Error( 'missing_nonce' );
 379      }
 380  
 381      $plugin    = null;
 382      $theme     = null;
 383      $real_file = null;
 384      if ( ! empty( $args['plugin'] ) ) {
 385          $plugin = $args['plugin'];
 386  
 387          if ( ! current_user_can( 'edit_plugins' ) ) {
 388              return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit plugins for this site.' ) );
 389          }
 390  
 391          if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) {
 392              return new WP_Error( 'nonce_failure' );
 393          }
 394  
 395          if ( ! array_key_exists( $plugin, get_plugins() ) ) {
 396              return new WP_Error( 'invalid_plugin' );
 397          }
 398  
 399          if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) {
 400              return new WP_Error( 'bad_plugin_file_path', __( 'Sorry, that file cannot be edited.' ) );
 401          }
 402  
 403          $editable_extensions = wp_get_plugin_file_editable_extensions( $plugin );
 404  
 405          $real_file = WP_PLUGIN_DIR . '/' . $file;
 406  
 407          $is_active = in_array(
 408              $plugin,
 409              (array) get_option( 'active_plugins', array() ),
 410              true
 411          );
 412  
 413      } elseif ( ! empty( $args['theme'] ) ) {
 414          $stylesheet = $args['theme'];
 415          if ( 0 !== validate_file( $stylesheet ) ) {
 416              return new WP_Error( 'bad_theme_path' );
 417          }
 418  
 419          if ( ! current_user_can( 'edit_themes' ) ) {
 420              return new WP_Error( 'unauthorized', __( 'Sorry, you are not allowed to edit templates for this site.' ) );
 421          }
 422  
 423          $theme = wp_get_theme( $stylesheet );
 424          if ( ! $theme->exists() ) {
 425              return new WP_Error( 'non_existent_theme', __( 'The requested theme does not exist.' ) );
 426          }
 427  
 428          if ( ! wp_verify_nonce( $args['nonce'], 'edit-theme_' . $stylesheet . '_' . $file ) ) {
 429              return new WP_Error( 'nonce_failure' );
 430          }
 431  
 432          if ( $theme->errors() && 'theme_no_stylesheet' === $theme->errors()->get_error_code() ) {
 433              return new WP_Error(
 434                  'theme_no_stylesheet',
 435                  __( 'The requested theme does not exist.' ) . ' ' . $theme->errors()->get_error_message()
 436              );
 437          }
 438  
 439          $editable_extensions = wp_get_theme_file_editable_extensions( $theme );
 440  
 441          $allowed_files = array();
 442          foreach ( $editable_extensions as $type ) {
 443              switch ( $type ) {
 444                  case 'php':
 445                      $allowed_files = array_merge( $allowed_files, $theme->get_files( 'php', -1 ) );
 446                      break;
 447                  case 'css':
 448                      $style_files                = $theme->get_files( 'css', -1 );
 449                      $allowed_files['style.css'] = $style_files['style.css'];
 450                      $allowed_files              = array_merge( $allowed_files, $style_files );
 451                      break;
 452                  default:
 453                      $allowed_files = array_merge( $allowed_files, $theme->get_files( $type, -1 ) );
 454                      break;
 455              }
 456          }
 457  
 458          // Compare based on relative paths.
 459          if ( 0 !== validate_file( $file, array_keys( $allowed_files ) ) ) {
 460              return new WP_Error( 'disallowed_theme_file', __( 'Sorry, that file cannot be edited.' ) );
 461          }
 462  
 463          $real_file = $theme->get_stylesheet_directory() . '/' . $file;
 464  
 465          $is_active = ( get_stylesheet() === $stylesheet || get_template() === $stylesheet );
 466  
 467      } else {
 468          return new WP_Error( 'missing_theme_or_plugin' );
 469      }
 470  
 471      // Ensure file is real.
 472      if ( ! is_file( $real_file ) ) {
 473          return new WP_Error( 'file_does_not_exist', __( 'File does not exist! Please double check the name and try again.' ) );
 474      }
 475  
 476      // Ensure file extension is allowed.
 477      $extension = null;
 478      if ( preg_match( '/\.([^.]+)$/', $real_file, $matches ) ) {
 479          $extension = strtolower( $matches[1] );
 480          if ( ! in_array( $extension, $editable_extensions, true ) ) {
 481              return new WP_Error( 'illegal_file_type', __( 'Files of this type are not editable.' ) );
 482          }
 483      }
 484  
 485      $previous_content = file_get_contents( $real_file );
 486  
 487      if ( ! is_writeable( $real_file ) ) {
 488          return new WP_Error( 'file_not_writable' );
 489      }
 490  
 491      $f = fopen( $real_file, 'w+' );
 492      if ( false === $f ) {
 493          return new WP_Error( 'file_not_writable' );
 494      }
 495  
 496      $written = fwrite( $f, $content );
 497      fclose( $f );
 498      if ( false === $written ) {
 499          return new WP_Error( 'unable_to_write', __( 'Unable to write to file.' ) );
 500      }
 501      if ( 'php' === $extension && function_exists( 'opcache_invalidate' ) ) {
 502          opcache_invalidate( $real_file, true );
 503      }
 504  
 505      if ( $is_active && 'php' === $extension ) {
 506  
 507          $scrape_key   = md5( rand() );
 508          $transient    = 'scrape_key_' . $scrape_key;
 509          $scrape_nonce = strval( rand() );
 510          // It shouldn't take more than 60 seconds to make the two loopback requests.
 511          set_transient( $transient, $scrape_nonce, 60 );
 512  
 513          $cookies       = wp_unslash( $_COOKIE );
 514          $scrape_params = array(
 515              'wp_scrape_key'   => $scrape_key,
 516              'wp_scrape_nonce' => $scrape_nonce,
 517          );
 518          $headers       = array(
 519              'Cache-Control' => 'no-cache',
 520          );
 521  
 522          /** This filter is documented in wp-includes/class-wp-http-streams.php */
 523          $sslverify = apply_filters( 'https_local_ssl_verify', false );
 524  
 525          // Include Basic auth in loopback requests.
 526          if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
 527              $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
 528          }
 529  
 530          // Make sure PHP process doesn't die before loopback requests complete.
 531          set_time_limit( 300 );
 532  
 533          // Time to wait for loopback requests to finish.
 534          $timeout = 100;
 535  
 536          $needle_start = "###### wp_scraping_result_start:$scrape_key ######";
 537          $needle_end   = "###### wp_scraping_result_end:$scrape_key ######";
 538  
 539          // Attempt loopback request to editor to see if user just whitescreened themselves.
 540          if ( $plugin ) {
 541              $url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) );
 542          } elseif ( isset( $stylesheet ) ) {
 543              $url = add_query_arg(
 544                  array(
 545                      'theme' => $stylesheet,
 546                      'file'  => $file,
 547                  ),
 548                  admin_url( 'theme-editor.php' )
 549              );
 550          } else {
 551              $url = admin_url();
 552          }
 553  
 554          if ( PHP_SESSION_ACTIVE === session_status() ) {
 555              // Close any active session to prevent HTTP requests from timing out
 556              // when attempting to connect back to the site.
 557              session_write_close();
 558          }
 559  
 560          $url                    = add_query_arg( $scrape_params, $url );
 561          $r                      = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
 562          $body                   = wp_remote_retrieve_body( $r );
 563          $scrape_result_position = strpos( $body, $needle_start );
 564  
 565          $loopback_request_failure = array(
 566              'code'    => 'loopback_request_failed',
 567              'message' => __( 'Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.' ),
 568          );
 569          $json_parse_failure       = array(
 570              'code' => 'json_parse_error',
 571          );
 572  
 573          $result = null;
 574          if ( false === $scrape_result_position ) {
 575              $result = $loopback_request_failure;
 576          } else {
 577              $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
 578              $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
 579              $result       = json_decode( trim( $error_output ), true );
 580              if ( empty( $result ) ) {
 581                  $result = $json_parse_failure;
 582              }
 583          }
 584  
 585          // Try making request to homepage as well to see if visitors have been whitescreened.
 586          if ( true === $result ) {
 587              $url                    = home_url( '/' );
 588              $url                    = add_query_arg( $scrape_params, $url );
 589              $r                      = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout' ) );
 590              $body                   = wp_remote_retrieve_body( $r );
 591              $scrape_result_position = strpos( $body, $needle_start );
 592  
 593              if ( false === $scrape_result_position ) {
 594                  $result = $loopback_request_failure;
 595              } else {
 596                  $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
 597                  $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
 598                  $result       = json_decode( trim( $error_output ), true );
 599                  if ( empty( $result ) ) {
 600                      $result = $json_parse_failure;
 601                  }
 602              }
 603          }
 604  
 605          delete_transient( $transient );
 606  
 607          if ( true !== $result ) {
 608  
 609              // Roll-back file change.
 610              file_put_contents( $real_file, $previous_content );
 611              if ( function_exists( 'opcache_invalidate' ) ) {
 612                  opcache_invalidate( $real_file, true );
 613              }
 614  
 615              if ( ! isset( $result['message'] ) ) {
 616                  $message = __( 'Something went wrong.' );
 617              } else {
 618                  $message = $result['message'];
 619                  unset( $result['message'] );
 620              }
 621              return new WP_Error( 'php_error', $message, $result );
 622          }
 623      }
 624  
 625      if ( $theme instanceof WP_Theme ) {
 626          $theme->cache_delete();
 627      }
 628  
 629      return true;
 630  }
 631  
 632  
 633  /**
 634   * Returns a filename of a Temporary unique file.
 635   * Please note that the calling function must unlink() this itself.
 636   *
 637   * The filename is based off the passed parameter or defaults to the current unix timestamp,
 638   * while the directory can either be passed as well, or by leaving it blank, default to a writable temporary directory.
 639   *
 640   * @since 2.6.0
 641   *
 642   * @param string $filename Optional. Filename to base the Unique file off. Default empty.
 643   * @param string $dir      Optional. Directory to store the file in. Default empty.
 644   * @return string a writable filename
 645   */
 646  function wp_tempnam( $filename = '', $dir = '' ) {
 647      if ( empty( $dir ) ) {
 648          $dir = get_temp_dir();
 649      }
 650  
 651      if ( empty( $filename ) || in_array( $filename, array( '.', '/', '\\' ), true ) ) {
 652          $filename = uniqid();
 653      }
 654  
 655      // Use the basename of the given file without the extension as the name for the temporary directory.
 656      $temp_filename = basename( $filename );
 657      $temp_filename = preg_replace( '|\.[^.]*$|', '', $temp_filename );
 658  
 659      // If the folder is falsey, use its parent directory name instead.
 660      if ( ! $temp_filename ) {
 661          return wp_tempnam( dirname( $filename ), $dir );
 662      }
 663  
 664      // Suffix some random data to avoid filename conflicts.
 665      $temp_filename .= '-' . wp_generate_password( 6, false );
 666      $temp_filename .= '.tmp';
 667      $temp_filename  = $dir . wp_unique_filename( $dir, $temp_filename );
 668  
 669      $fp = @fopen( $temp_filename, 'x' );
 670      if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
 671          return wp_tempnam( $filename, $dir );
 672      }
 673      if ( $fp ) {
 674          fclose( $fp );
 675      }
 676  
 677      return $temp_filename;
 678  }
 679  
 680  /**
 681   * Makes sure that the file that was requested to be edited is allowed to be edited.
 682   *
 683   * Function will die if you are not allowed to edit the file.
 684   *
 685   * @since 1.5.0
 686   *
 687   * @param string   $file          File the user is attempting to edit.
 688   * @param string[] $allowed_files Optional. Array of allowed files to edit. `$file` must match an entry exactly.
 689   * @return string|void Returns the file name on success, dies on failure.
 690   */
 691  function validate_file_to_edit( $file, $allowed_files = array() ) {
 692      $code = validate_file( $file, $allowed_files );
 693  
 694      if ( ! $code ) {
 695          return $file;
 696      }
 697  
 698      switch ( $code ) {
 699          case 1:
 700              wp_die( __( 'Sorry, that file cannot be edited.' ) );
 701  
 702              // case 2 :
 703              // wp_die( __('Sorry, can&#8217;t call files with their real path.' ));
 704  
 705          case 3:
 706              wp_die( __( 'Sorry, that file cannot be edited.' ) );
 707      }
 708  }
 709  
 710  /**
 711   * Handle PHP uploads in WordPress, sanitizing file names, checking extensions for mime type,
 712   * and moving the file to the appropriate directory within the uploads directory.
 713   *
 714   * @access private
 715   * @since 4.0.0
 716   *
 717   * @see wp_handle_upload_error
 718   *
 719   * @param string[]       $file      Reference to a single element of `$_FILES`. Call the function once for each uploaded file.
 720   * @param string[]|false $overrides An associative array of names => values to override default variables. Default false.
 721   * @param string         $time      Time formatted in 'yyyy/mm'.
 722   * @param string         $action    Expected value for `$_POST['action']`.
 723   * @return string[] On success, returns an associative array of file attributes. On failure, returns
 724   *                  `$overrides['upload_error_handler']( &$file, $message )` or `array( 'error' => $message )`.
 725   */
 726  function _wp_handle_upload( &$file, $overrides, $time, $action ) {
 727      // The default error handler.
 728      if ( ! function_exists( 'wp_handle_upload_error' ) ) {
 729  		function wp_handle_upload_error( &$file, $message ) {
 730              return array( 'error' => $message );
 731          }
 732      }
 733  
 734      /**
 735       * Filters the data for a file before it is uploaded to WordPress.
 736       *
 737       * The dynamic portion of the hook name, `$action`, refers to the post action.
 738       *
 739       * @since 2.9.0 as 'wp_handle_upload_prefilter'.
 740       * @since 4.0.0 Converted to a dynamic hook with `$action`.
 741       *
 742       * @param string[] $file An array of data for a single file.
 743       */
 744      $file = apply_filters( "{$action}_prefilter", $file );
 745  
 746      // You may define your own function and pass the name in $overrides['upload_error_handler'].
 747      $upload_error_handler = 'wp_handle_upload_error';
 748      if ( isset( $overrides['upload_error_handler'] ) ) {
 749          $upload_error_handler = $overrides['upload_error_handler'];
 750      }
 751  
 752      // You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
 753      if ( isset( $file['error'] ) && ! is_numeric( $file['error'] ) && $file['error'] ) {
 754          return call_user_func_array( $upload_error_handler, array( &$file, $file['error'] ) );
 755      }
 756  
 757      // Install user overrides. Did we mention that this voids your warranty?
 758  
 759      // You may define your own function and pass the name in $overrides['unique_filename_callback'].
 760      $unique_filename_callback = null;
 761      if ( isset( $overrides['unique_filename_callback'] ) ) {
 762          $unique_filename_callback = $overrides['unique_filename_callback'];
 763      }
 764  
 765      /*
 766       * This may not have originally been intended to be overridable,
 767       * but historically has been.
 768       */
 769      if ( isset( $overrides['upload_error_strings'] ) ) {
 770          $upload_error_strings = $overrides['upload_error_strings'];
 771      } else {
 772          // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
 773          $upload_error_strings = array(
 774              false,
 775              sprintf(
 776                  /* translators: 1: upload_max_filesize, 2: php.ini */
 777                  __( 'The uploaded file exceeds the %1$s directive in %2$s.' ),
 778                  'upload_max_filesize',
 779                  'php.ini'
 780              ),
 781              sprintf(
 782                  /* translators: %s: MAX_FILE_SIZE */
 783                  __( 'The uploaded file exceeds the %s directive that was specified in the HTML form.' ),
 784                  'MAX_FILE_SIZE'
 785              ),
 786              __( 'The uploaded file was only partially uploaded.' ),
 787              __( 'No file was uploaded.' ),
 788              '',
 789              __( 'Missing a temporary folder.' ),
 790              __( 'Failed to write file to disk.' ),
 791              __( 'File upload stopped by extension.' ),
 792          );
 793      }
 794  
 795      // All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
 796      $test_form = isset( $overrides['test_form'] ) ? $overrides['test_form'] : true;
 797      $test_size = isset( $overrides['test_size'] ) ? $overrides['test_size'] : true;
 798  
 799      // If you override this, you must provide $ext and $type!!
 800      $test_type = isset( $overrides['test_type'] ) ? $overrides['test_type'] : true;
 801      $mimes     = isset( $overrides['mimes'] ) ? $overrides['mimes'] : false;
 802  
 803      // A correct form post will pass this test.
 804      if ( $test_form && ( ! isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) ) {
 805          return call_user_func_array( $upload_error_handler, array( &$file, __( 'Invalid form submission.' ) ) );
 806      }
 807      // A successful upload will pass this test. It makes no sense to override this one.
 808      if ( isset( $file['error'] ) && $file['error'] > 0 ) {
 809          return call_user_func_array( $upload_error_handler, array( &$file, $upload_error_strings[ $file['error'] ] ) );
 810      }
 811  
 812      // A properly uploaded file will pass this test. There should be no reason to override this one.
 813      $test_uploaded_file = 'wp_handle_upload' === $action ? is_uploaded_file( $file['tmp_name'] ) : @is_readable( $file['tmp_name'] );
 814      if ( ! $test_uploaded_file ) {
 815          return call_user_func_array( $upload_error_handler, array( &$file, __( 'Specified file failed upload test.' ) ) );
 816      }
 817  
 818      $test_file_size = 'wp_handle_upload' === $action ? $file['size'] : filesize( $file['tmp_name'] );
 819      // A non-empty file will pass this test.
 820      if ( $test_size && ! ( $test_file_size > 0 ) ) {
 821          if ( is_multisite() ) {
 822              $error_msg = __( 'File is empty. Please upload something more substantial.' );
 823          } else {
 824              $error_msg = sprintf(
 825                  /* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */
 826                  __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your %1$s file or by %2$s being defined as smaller than %3$s in %1$s.' ),
 827                  'php.ini',
 828                  'post_max_size',
 829                  'upload_max_filesize'
 830              );
 831          }
 832          return call_user_func_array( $upload_error_handler, array( &$file, $error_msg ) );
 833      }
 834  
 835      // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
 836      if ( $test_type ) {
 837          $wp_filetype     = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
 838          $ext             = empty( $wp_filetype['ext'] ) ? '' : $wp_filetype['ext'];
 839          $type            = empty( $wp_filetype['type'] ) ? '' : $wp_filetype['type'];
 840          $proper_filename = empty( $wp_filetype['proper_filename'] ) ? '' : $wp_filetype['proper_filename'];
 841  
 842          // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect.
 843          if ( $proper_filename ) {
 844              $file['name'] = $proper_filename;
 845          }
 846          if ( ( ! $type || ! $ext ) && ! current_user_can( 'unfiltered_upload' ) ) {
 847              return call_user_func_array( $upload_error_handler, array( &$file, __( 'Sorry, this file type is not permitted for security reasons.' ) ) );
 848          }
 849          if ( ! $type ) {
 850              $type = $file['type'];
 851          }
 852      } else {
 853          $type = '';
 854      }
 855  
 856      /*
 857       * A writable uploads dir will pass this test. Again, there's no point
 858       * overriding this one.
 859       */
 860      $uploads = wp_upload_dir( $time );
 861      if ( ! ( $uploads && false === $uploads['error'] ) ) {
 862          return call_user_func_array( $upload_error_handler, array( &$file, $uploads['error'] ) );
 863      }
 864  
 865      $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
 866  
 867      // Move the file to the uploads dir.
 868      $new_file = $uploads['path'] . "/$filename";
 869  
 870      /**
 871       * Filters whether to short-circuit moving the uploaded file after passing all checks.
 872       *
 873       * If a non-null value is passed to the filter, moving the file and any related error
 874       * reporting will be completely skipped.
 875       *
 876       * @since 4.9.0
 877       *
 878       * @param mixed    $move_new_file If null (default) move the file after the upload.
 879       * @param string[] $file          An array of data for a single file.
 880       * @param string   $new_file      Filename of the newly-uploaded file.
 881       * @param string   $type          File type.
 882       */
 883      $move_new_file = apply_filters( 'pre_move_uploaded_file', null, $file, $new_file, $type );
 884  
 885      if ( null === $move_new_file ) {
 886          if ( 'wp_handle_upload' === $action ) {
 887              $move_new_file = @move_uploaded_file( $file['tmp_name'], $new_file );
 888          } else {
 889              // Use copy and unlink because rename breaks streams.
 890              // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
 891              $move_new_file = @copy( $file['tmp_name'], $new_file );
 892              unlink( $file['tmp_name'] );
 893          }
 894  
 895          if ( false === $move_new_file ) {
 896              if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
 897                  $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
 898              } else {
 899                  $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
 900              }
 901              return $upload_error_handler(
 902                  $file,
 903                  sprintf(
 904                      /* translators: %s: Destination file path. */
 905                      __( 'The uploaded file could not be moved to %s.' ),
 906                      $error_path
 907                  )
 908              );
 909          }
 910      }
 911  
 912      // Set correct file permissions.
 913      $stat  = stat( dirname( $new_file ) );
 914      $perms = $stat['mode'] & 0000666;
 915      chmod( $new_file, $perms );
 916  
 917      // Compute the URL.
 918      $url = $uploads['url'] . "/$filename";
 919  
 920      if ( is_multisite() ) {
 921          delete_transient( 'dirsize_cache' );
 922      }
 923  
 924      /**
 925       * Filters the data array for the uploaded file.
 926       *
 927       * @since 2.1.0
 928       *
 929       * @param array  $upload {
 930       *     Array of upload data.
 931       *
 932       *     @type string $file Filename of the newly-uploaded file.
 933       *     @type string $url  URL of the uploaded file.
 934       *     @type string $type File type.
 935       * }
 936       * @param string $context The type of upload action. Values include 'upload' or 'sideload'.
 937       */
 938      return apply_filters(
 939          'wp_handle_upload',
 940          array(
 941              'file' => $new_file,
 942              'url'  => $url,
 943              'type' => $type,
 944          ),
 945          'wp_handle_sideload' === $action ? 'sideload' : 'upload'
 946      );
 947  }
 948  
 949  /**
 950   * Wrapper for _wp_handle_upload().
 951   *
 952   * Passes the {@see 'wp_handle_upload'} action.
 953   *
 954   * @since 2.0.0
 955   *
 956   * @see _wp_handle_upload()
 957   *
 958   * @param array      $file      Reference to a single element of `$_FILES`. Call the function once for
 959   *                              each uploaded file.
 960   * @param array|bool $overrides Optional. An associative array of names=>values to override default
 961   *                              variables. Default false.
 962   * @param string     $time      Optional. Time formatted in 'yyyy/mm'. Default null.
 963   * @return array On success, returns an associative array of file attributes. On failure, returns
 964   *               $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
 965   */
 966  function wp_handle_upload( &$file, $overrides = false, $time = null ) {
 967      /*
 968       *  $_POST['action'] must be set and its value must equal $overrides['action']
 969       *  or this:
 970       */
 971      $action = 'wp_handle_upload';
 972      if ( isset( $overrides['action'] ) ) {
 973          $action = $overrides['action'];
 974      }
 975  
 976      return _wp_handle_upload( $file, $overrides, $time, $action );
 977  }
 978  
 979  /**
 980   * Wrapper for _wp_handle_upload().
 981   *
 982   * Passes the {@see 'wp_handle_sideload'} action.
 983   *
 984   * @since 2.6.0
 985   *
 986   * @see _wp_handle_upload()
 987   *
 988   * @param array      $file      An array similar to that of a PHP `$_FILES` POST array
 989   * @param array|bool $overrides Optional. An associative array of names=>values to override default
 990   *                              variables. Default false.
 991   * @param string     $time      Optional. Time formatted in 'yyyy/mm'. Default null.
 992   * @return array On success, returns an associative array of file attributes. On failure, returns
 993   *               $overrides['upload_error_handler'](&$file, $message ) or array( 'error'=>$message ).
 994   */
 995  function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
 996      /*
 997       *  $_POST['action'] must be set and its value must equal $overrides['action']
 998       *  or this:
 999       */
1000      $action = 'wp_handle_sideload';
1001      if ( isset( $overrides['action'] ) ) {
1002          $action = $overrides['action'];
1003      }
1004      return _wp_handle_upload( $file, $overrides, $time, $action );
1005  }
1006  
1007  /**
1008   * Downloads a URL to a local temporary file using the WordPress HTTP API.
1009   *
1010   * Please note that the calling function must unlink() the file.
1011   *
1012   * @since 2.5.0
1013   * @since 5.2.0 Signature Verification with SoftFail was added.
1014   *
1015   * @param string $url                    The URL of the file to download.
1016   * @param int    $timeout                The timeout for the request to download the file. Default 300 seconds.
1017   * @param bool   $signature_verification Whether to perform Signature Verification. Default false.
1018   * @return string|WP_Error Filename on success, WP_Error on failure.
1019   */
1020  function download_url( $url, $timeout = 300, $signature_verification = false ) {
1021      // WARNING: The file is not automatically deleted, the script must unlink() the file.
1022      if ( ! $url ) {
1023          return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) );
1024      }
1025  
1026      $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
1027  
1028      $tmpfname = wp_tempnam( $url_filename );
1029      if ( ! $tmpfname ) {
1030          return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) );
1031      }
1032  
1033      $response = wp_safe_remote_get(
1034          $url,
1035          array(
1036              'timeout'  => $timeout,
1037              'stream'   => true,
1038              'filename' => $tmpfname,
1039          )
1040      );
1041  
1042      if ( is_wp_error( $response ) ) {
1043          unlink( $tmpfname );
1044          return $response;
1045      }
1046  
1047      $response_code = wp_remote_retrieve_response_code( $response );
1048  
1049      if ( 200 != $response_code ) {
1050          $data = array(
1051              'code' => $response_code,
1052          );
1053  
1054          // Retrieve a sample of the response body for debugging purposes.
1055          $tmpf = fopen( $tmpfname, 'rb' );
1056          if ( $tmpf ) {
1057              /**
1058               * Filters the maximum error response body size in `download_url()`.
1059               *
1060               * @since 5.1.0
1061               *
1062               * @see download_url()
1063               *
1064               * @param int $size The maximum error response body size. Default 1 KB.
1065               */
1066              $response_size = apply_filters( 'download_url_error_max_body_size', KB_IN_BYTES );
1067              $data['body']  = fread( $tmpf, $response_size );
1068              fclose( $tmpf );
1069          }
1070  
1071          unlink( $tmpfname );
1072          return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ), $data );
1073      }
1074  
1075      $content_md5 = wp_remote_retrieve_header( $response, 'content-md5' );
1076      if ( $content_md5 ) {
1077          $md5_check = verify_file_md5( $tmpfname, $content_md5 );
1078          if ( is_wp_error( $md5_check ) ) {
1079              unlink( $tmpfname );
1080              return $md5_check;
1081          }
1082      }
1083  
1084      // If the caller expects signature verification to occur, check to see if this URL supports it.
1085      if ( $signature_verification ) {
1086          /**
1087           * Filters the list of hosts which should have Signature Verification attempted on.
1088           *
1089           * @since 5.2.0
1090           *
1091           * @param string[] $hostnames List of hostnames.
1092           */
1093          $signed_hostnames       = apply_filters( 'wp_signature_hosts', array( 'wordpress.org', 'downloads.wordpress.org', 's.w.org' ) );
1094          $signature_verification = in_array( parse_url( $url, PHP_URL_HOST ), $signed_hostnames, true );
1095      }
1096  
1097      // Perform signature valiation if supported.
1098      if ( $signature_verification ) {
1099          $signature = wp_remote_retrieve_header( $response, 'x-content-signature' );
1100          if ( ! $signature ) {
1101              // Retrieve signatures from a file if the header wasn't included.
1102              // WordPress.org stores signatures at $package_url.sig.
1103  
1104              $signature_url = false;
1105              $url_path      = parse_url( $url, PHP_URL_PATH );
1106  
1107              if ( '.zip' === substr( $url_path, -4 ) || '.tar.gz' === substr( $url_path, -7 ) ) {
1108                  $signature_url = str_replace( $url_path, $url_path . '.sig', $url );
1109              }
1110  
1111              /**
1112               * Filter the URL where the signature for a file is located.
1113               *
1114               * @since 5.2.0
1115               *
1116               * @param false|string $signature_url The URL where signatures can be found for a file, or false if none are known.
1117               * @param string $url                 The URL being verified.
1118               */
1119              $signature_url = apply_filters( 'wp_signature_url', $signature_url, $url );
1120  
1121              if ( $signature_url ) {
1122                  $signature_request = wp_safe_remote_get(
1123                      $signature_url,
1124                      array(
1125                          'limit_response_size' => 10 * KB_IN_BYTES, // 10KB should be large enough for quite a few signatures.
1126                      )
1127                  );
1128  
1129                  if ( ! is_wp_error( $signature_request ) && 200 === wp_remote_retrieve_response_code( $signature_request ) ) {
1130                      $signature = explode( "\n", wp_remote_retrieve_body( $signature_request ) );
1131                  }
1132              }
1133          }
1134  
1135          // Perform the checks.
1136          $signature_verification = verify_file_signature( $tmpfname, $signature, basename( parse_url( $url, PHP_URL_PATH ) ) );
1137      }
1138  
1139      if ( is_wp_error( $signature_verification ) ) {
1140          if (
1141              /**
1142               * Filters whether Signature Verification failures should be allowed to soft fail.
1143               *
1144               * WARNING: This may be removed from a future release.
1145               *
1146               * @since 5.2.0
1147               *
1148               * @param bool   $signature_softfail If a softfail is allowed.
1149               * @param string $url                The url being accessed.
1150               */
1151              apply_filters( 'wp_signature_softfail', true, $url )
1152          ) {
1153              $signature_verification->add_data( $tmpfname, 'softfail-filename' );
1154          } else {
1155              // Hard-fail.
1156              unlink( $tmpfname );
1157          }
1158  
1159          return $signature_verification;
1160      }
1161  
1162      return $tmpfname;
1163  }
1164  
1165  /**
1166   * Calculates and compares the MD5 of a file to its expected value.
1167   *
1168   * @since 3.7.0
1169   *
1170   * @param string $filename     The filename to check the MD5 of.
1171   * @param string $expected_md5 The expected MD5 of the file, either a base64-encoded raw md5,
1172   *                             or a hex-encoded md5.
1173   * @return bool|WP_Error True on success, false when the MD5 format is unknown/unexpected,
1174   *                       WP_Error on failure.
1175   */
1176  function verify_file_md5( $filename, $expected_md5 ) {
1177      if ( 32 == strlen( $expected_md5 ) ) {
1178          $expected_raw_md5 = pack( 'H*', $expected_md5 );
1179      } elseif ( 24 == strlen( $expected_md5 ) ) {
1180          $expected_raw_md5 = base64_decode( $expected_md5 );
1181      } else {
1182          return false; // Unknown format.
1183      }
1184  
1185      $file_md5 = md5_file( $filename, true );
1186  
1187      if ( $file_md5 === $expected_raw_md5 ) {
1188          return true;
1189      }
1190  
1191      return new WP_Error(
1192          'md5_mismatch',
1193          sprintf(
1194              /* translators: 1: File checksum, 2: Expected checksum value. */
1195              __( 'The checksum of the file (%1$s) does not match the expected checksum value (%2$s).' ),
1196              bin2hex( $file_md5 ),
1197              bin2hex( $expected_raw_md5 )
1198          )
1199      );
1200  }
1201  
1202  /**
1203   * Verifies the contents of a file against its ED25519 signature.
1204   *
1205   * @since 5.2.0
1206   *
1207   * @param string       $filename            The file to validate.
1208   * @param string|array $signatures          A Signature provided for the file.
1209   * @param string       $filename_for_errors A friendly filename for errors. Optional.
1210   * @return bool|WP_Error True on success, false if verification not attempted,
1211   *                       or WP_Error describing an error condition.
1212   */
1213  function verify_file_signature( $filename, $signatures, $filename_for_errors = false ) {
1214      if ( ! $filename_for_errors ) {
1215          $filename_for_errors = wp_basename( $filename );
1216      }
1217  
1218      // Check we can process signatures.
1219      if ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) || ! in_array( 'sha384', array_map( 'strtolower', hash_algos() ), true ) ) {
1220          return new WP_Error(
1221              'signature_verification_unsupported',
1222              sprintf(
1223                  /* translators: %s: The filename of the package. */
1224                  __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
1225                  '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
1226              ),
1227              ( ! function_exists( 'sodium_crypto_sign_verify_detached' ) ? 'sodium_crypto_sign_verify_detached' : 'sha384' )
1228          );
1229      }
1230  
1231      // Check for a edge-case affecting PHP Maths abilities.
1232      if (
1233          ! extension_loaded( 'sodium' ) &&
1234          in_array( PHP_VERSION_ID, array( 70200, 70201, 70202 ), true ) &&
1235          extension_loaded( 'opcache' )
1236      ) {
1237          // Sodium_Compat isn't compatible with PHP 7.2.0~7.2.2 due to a bug in the PHP Opcache extension, bail early as it'll fail.
1238          // https://bugs.php.net/bug.php?id=75938
1239  
1240          return new WP_Error(
1241              'signature_verification_unsupported',
1242              sprintf(
1243                  /* translators: %s: The filename of the package. */
1244                  __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
1245                  '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
1246              ),
1247              array(
1248                  'php'    => phpversion(),
1249                  // phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
1250                  'sodium' => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
1251              )
1252          );
1253  
1254      }
1255  
1256      // Verify runtime speed of Sodium_Compat is acceptable.
1257      if ( ! extension_loaded( 'sodium' ) && ! ParagonIE_Sodium_Compat::polyfill_is_fast() ) {
1258          $sodium_compat_is_fast = false;
1259  
1260          // Allow for an old version of Sodium_Compat being loaded before the bundled WordPress one.
1261          if ( method_exists( 'ParagonIE_Sodium_Compat', 'runtime_speed_test' ) ) {
1262              // Run `ParagonIE_Sodium_Compat::runtime_speed_test()` in optimized integer mode, as that's what WordPress utilises during signing verifications.
1263              // phpcs:disable WordPress.NamingConventions.ValidVariableName
1264              $old_fastMult                      = ParagonIE_Sodium_Compat::$fastMult;
1265              ParagonIE_Sodium_Compat::$fastMult = true;
1266              $sodium_compat_is_fast             = ParagonIE_Sodium_Compat::runtime_speed_test( 100, 10 );
1267              ParagonIE_Sodium_Compat::$fastMult = $old_fastMult;
1268              // phpcs:enable
1269          }
1270  
1271          // This cannot be performed in a reasonable amount of time.
1272          // https://github.com/paragonie/sodium_compat#help-sodium_compat-is-slow-how-can-i-make-it-fast
1273          if ( ! $sodium_compat_is_fast ) {
1274              return new WP_Error(
1275                  'signature_verification_unsupported',
1276                  sprintf(
1277                      /* translators: %s: The filename of the package. */
1278                      __( 'The authenticity of %s could not be verified as signature verification is unavailable on this system.' ),
1279                      '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
1280                  ),
1281                  array(
1282                      'php'                => phpversion(),
1283                      // phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
1284                      'sodium'             => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
1285                      'polyfill_is_fast'   => false,
1286                      'max_execution_time' => ini_get( 'max_execution_time' ),
1287                  )
1288              );
1289          }
1290      }
1291  
1292      if ( ! $signatures ) {
1293          return new WP_Error(
1294              'signature_verification_no_signature',
1295              sprintf(
1296                  /* translators: %s: The filename of the package. */
1297                  __( 'The authenticity of %s could not be verified as no signature was found.' ),
1298                  '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
1299              ),
1300              array(
1301                  'filename' => $filename_for_errors,
1302              )
1303          );
1304      }
1305  
1306      $trusted_keys = wp_trusted_keys();
1307      $file_hash    = hash_file( 'sha384', $filename, true );
1308  
1309      mbstring_binary_safe_encoding();
1310  
1311      $skipped_key       = 0;
1312      $skipped_signature = 0;
1313  
1314      foreach ( (array) $signatures as $signature ) {
1315          $signature_raw = base64_decode( $signature );
1316  
1317          // Ensure only valid-length signatures are considered.
1318          if ( SODIUM_CRYPTO_SIGN_BYTES !== strlen( $signature_raw ) ) {
1319              $skipped_signature++;
1320              continue;
1321          }
1322  
1323          foreach ( (array) $trusted_keys as $key ) {
1324              $key_raw = base64_decode( $key );
1325  
1326              // Only pass valid public keys through.
1327              if ( SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES !== strlen( $key_raw ) ) {
1328                  $skipped_key++;
1329                  continue;
1330              }
1331  
1332              if ( sodium_crypto_sign_verify_detached( $signature_raw, $file_hash, $key_raw ) ) {
1333                  reset_mbstring_encoding();
1334                  return true;
1335              }
1336          }
1337      }
1338  
1339      reset_mbstring_encoding();
1340  
1341      return new WP_Error(
1342          'signature_verification_failed',
1343          sprintf(
1344              /* translators: %s: The filename of the package. */
1345              __( 'The authenticity of %s could not be verified.' ),
1346              '<span class="code">' . esc_html( $filename_for_errors ) . '</span>'
1347          ),
1348          // Error data helpful for debugging:
1349          array(
1350              'filename'    => $filename_for_errors,
1351              'keys'        => $trusted_keys,
1352              'signatures'  => $signatures,
1353              'hash'        => bin2hex( $file_hash ),
1354              'skipped_key' => $skipped_key,
1355              'skipped_sig' => $skipped_signature,
1356              'php'         => phpversion(),
1357              // phpcs:ignore PHPCompatibility.Constants.NewConstants.sodium_library_versionFound
1358              'sodium'      => defined( 'SODIUM_LIBRARY_VERSION' ) ? SODIUM_LIBRARY_VERSION : ( defined( 'ParagonIE_Sodium_Compat::VERSION_STRING' ) ? ParagonIE_Sodium_Compat::VERSION_STRING : false ),
1359          )
1360      );
1361  }
1362  
1363  /**
1364   * Retrieve the list of signing keys trusted by WordPress.
1365   *
1366   * @since 5.2.0
1367   *
1368   * @return string[] Array of base64-encoded signing keys.
1369   */
1370  function wp_trusted_keys() {
1371      $trusted_keys = array();
1372  
1373      if ( time() < 1617235200 ) {
1374          // WordPress.org Key #1 - This key is only valid before April 1st, 2021.
1375          $trusted_keys[] = 'fRPyrxb/MvVLbdsYi+OOEv4xc+Eqpsj+kkAS6gNOkI0=';
1376      }
1377  
1378      // TODO: Add key #2 with longer expiration.
1379  
1380      /**
1381       * Filter the valid signing keys used to verify the contents of files.
1382       *
1383       * @since 5.2.0
1384       *
1385       * @param string[] $trusted_keys The trusted keys that may sign packages.
1386       */
1387      return apply_filters( 'wp_trusted_keys', $trusted_keys );
1388  }
1389  
1390  /**
1391   * Unzips a specified ZIP file to a location on the filesystem via the WordPress
1392   * Filesystem Abstraction.
1393   *
1394   * Assumes that WP_Filesystem() has already been called and set up. Does not extract
1395   * a root-level __MACOSX directory, if present.
1396   *
1397   * Attempts to increase the PHP memory limit to 256M before uncompressing. However,
1398   * the most memory required shouldn't be much larger than the archive itself.
1399   *
1400   * @since 2.5.0
1401   *
1402   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
1403   *
1404   * @param string $file Full path and filename of ZIP archive.
1405   * @param string $to   Full path on the filesystem to extract archive to.
1406   * @return true|WP_Error True on success, WP_Error on failure.
1407   */
1408  function unzip_file( $file, $to ) {
1409      global $wp_filesystem;
1410  
1411      if ( ! $wp_filesystem || ! is_object( $wp_filesystem ) ) {
1412          return new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
1413      }
1414  
1415      // Unzip can use a lot of memory, but not this much hopefully.
1416      wp_raise_memory_limit( 'admin' );
1417  
1418      $needed_dirs = array();
1419      $to          = trailingslashit( $to );
1420  
1421      // Determine any parent directories needed (of the upgrade directory).
1422      if ( ! $wp_filesystem->is_dir( $to ) ) { // Only do parents if no children exist.
1423          $path = preg_split( '![/\\\]!', untrailingslashit( $to ) );
1424          for ( $i = count( $path ); $i >= 0; $i-- ) {
1425              if ( empty( $path[ $i ] ) ) {
1426                  continue;
1427              }
1428  
1429              $dir = implode( '/', array_slice( $path, 0, $i + 1 ) );
1430              if ( preg_match( '!^[a-z]:$!i', $dir ) ) { // Skip it if it looks like a Windows Drive letter.
1431                  continue;
1432              }
1433  
1434              if ( ! $wp_filesystem->is_dir( $dir ) ) {
1435                  $needed_dirs[] = $dir;
1436              } else {
1437                  break; // A folder exists, therefore we don't need to check the levels below this.
1438              }
1439          }
1440      }
1441  
1442      /**
1443       * Filters whether to use ZipArchive to unzip archives.
1444       *
1445       * @since 3.0.0
1446       *
1447       * @param bool $ziparchive Whether to use ZipArchive. Default true.
1448       */
1449      if ( class_exists( 'ZipArchive', false ) && apply_filters( 'unzip_file_use_ziparchive', true ) ) {
1450          $result = _unzip_file_ziparchive( $file, $to, $needed_dirs );
1451          if ( true === $result ) {
1452              return $result;
1453          } elseif ( is_wp_error( $result ) ) {
1454              if ( 'incompatible_archive' !== $result->get_error_code() ) {
1455                  return $result;
1456              }
1457          }
1458      }
1459      // Fall through to PclZip if ZipArchive is not available, or encountered an error opening the file.
1460      return _unzip_file_pclzip( $file, $to, $needed_dirs );
1461  }
1462  
1463  /**
1464   * Attempts to unzip an archive using the ZipArchive class.
1465   *
1466   * This function should not be called directly, use `unzip_file()` instead.
1467   *
1468   * Assumes that WP_Filesystem() has already been called and set up.
1469   *
1470   * @since 3.0.0
1471   * @see unzip_file()
1472   * @access private
1473   *
1474   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
1475   *
1476   * @param string   $file        Full path and filename of ZIP archive.
1477   * @param string   $to          Full path on the filesystem to extract archive to.
1478   * @param string[] $needed_dirs A partial list of required folders needed to be created.
1479   * @return true|WP_Error True on success, WP_Error on failure.
1480   */
1481  function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
1482      global $wp_filesystem;
1483  
1484      $z = new ZipArchive();
1485  
1486      $zopen = $z->open( $file, ZIPARCHIVE::CHECKCONS );
1487      if ( true !== $zopen ) {
1488          return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), array( 'ziparchive_error' => $zopen ) );
1489      }
1490  
1491      $uncompressed_size = 0;
1492  
1493      for ( $i = 0; $i < $z->numFiles; $i++ ) {
1494          $info = $z->statIndex( $i );
1495          if ( ! $info ) {
1496              return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
1497          }
1498  
1499          if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
1500              continue;
1501          }
1502  
1503          // Don't extract invalid files:
1504          if ( 0 !== validate_file( $info['name'] ) ) {
1505              continue;
1506          }
1507  
1508          $uncompressed_size += $info['size'];
1509  
1510          $dirname = dirname( $info['name'] );
1511  
1512          if ( '/' === substr( $info['name'], -1 ) ) {
1513              // Directory.
1514              $needed_dirs[] = $to . untrailingslashit( $info['name'] );
1515          } elseif ( '.' !== $dirname ) {
1516              // Path to a file.
1517              $needed_dirs[] = $to . untrailingslashit( $dirname );
1518          }
1519      }
1520  
1521      /*
1522       * disk_free_space() could return false. Assume that any falsey value is an error.
1523       * A disk that has zero free bytes has bigger problems.
1524       * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
1525       */
1526      if ( wp_doing_cron() ) {
1527          $available_space = @disk_free_space( WP_CONTENT_DIR );
1528          if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
1529              return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );
1530          }
1531      }
1532  
1533      $needed_dirs = array_unique( $needed_dirs );
1534      foreach ( $needed_dirs as $dir ) {
1535          // Check the parent folders of the folders all exist within the creation array.
1536          if ( untrailingslashit( $to ) == $dir ) { // Skip over the working directory, we know this exists (or will exist).
1537              continue;
1538          }
1539          if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
1540              continue;
1541          }
1542  
1543          $parent_folder = dirname( $dir );
1544          while ( ! empty( $parent_folder ) && untrailingslashit( $to ) != $parent_folder && ! in_array( $parent_folder, $needed_dirs, true ) ) {
1545              $needed_dirs[] = $parent_folder;
1546              $parent_folder = dirname( $parent_folder );
1547          }
1548      }
1549      asort( $needed_dirs );
1550  
1551      // Create those directories if need be:
1552      foreach ( $needed_dirs as $_dir ) {
1553          // Only check to see if the Dir exists upon creation failure. Less I/O this way.
1554          if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
1555              return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
1556          }
1557      }
1558      unset( $needed_dirs );
1559  
1560      for ( $i = 0; $i < $z->numFiles; $i++ ) {
1561          $info = $z->statIndex( $i );
1562          if ( ! $info ) {
1563              return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
1564          }
1565  
1566          if ( '/' === substr( $info['name'], -1 ) ) { // Directory.
1567              continue;
1568          }
1569  
1570          if ( '__MACOSX/' === substr( $info['name'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
1571              continue;
1572          }
1573  
1574          // Don't extract invalid files:
1575          if ( 0 !== validate_file( $info['name'] ) ) {
1576              continue;
1577          }
1578  
1579          $contents = $z->getFromIndex( $i );
1580          if ( false === $contents ) {
1581              return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] );
1582          }
1583  
1584          if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE ) ) {
1585              return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] );
1586          }
1587      }
1588  
1589      $z->close();
1590  
1591      return true;
1592  }
1593  
1594  /**
1595   * Attempts to unzip an archive using the PclZip library.
1596   *
1597   * This function should not be called directly, use `unzip_file()` instead.
1598   *
1599   * Assumes that WP_Filesystem() has already been called and set up.
1600   *
1601   * @since 3.0.0
1602   * @see unzip_file()
1603   * @access private
1604   *
1605   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
1606   *
1607   * @param string   $file        Full path and filename of ZIP archive.
1608   * @param string   $to          Full path on the filesystem to extract archive to.
1609   * @param string[] $needed_dirs A partial list of required folders needed to be created.
1610   * @return true|WP_Error True on success, WP_Error on failure.
1611   */
1612  function _unzip_file_pclzip( $file, $to, $needed_dirs = array() ) {
1613      global $wp_filesystem;
1614  
1615      mbstring_binary_safe_encoding();
1616  
1617      require_once  ABSPATH . 'wp-admin/includes/class-pclzip.php';
1618  
1619      $archive = new PclZip( $file );
1620  
1621      $archive_files = $archive->extract( PCLZIP_OPT_EXTRACT_AS_STRING );
1622  
1623      reset_mbstring_encoding();
1624  
1625      // Is the archive valid?
1626      if ( ! is_array( $archive_files ) ) {
1627          return new WP_Error( 'incompatible_archive', __( 'Incompatible Archive.' ), $archive->errorInfo( true ) );
1628      }
1629  
1630      if ( 0 === count( $archive_files ) ) {
1631          return new WP_Error( 'empty_archive_pclzip', __( 'Empty archive.' ) );
1632      }
1633  
1634      $uncompressed_size = 0;
1635  
1636      // Determine any children directories needed (From within the archive).
1637      foreach ( $archive_files as $file ) {
1638          if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Skip the OS X-created __MACOSX directory.
1639              continue;
1640          }
1641  
1642          $uncompressed_size += $file['size'];
1643  
1644          $needed_dirs[] = $to . untrailingslashit( $file['folder'] ? $file['filename'] : dirname( $file['filename'] ) );
1645      }
1646  
1647      /*
1648       * disk_free_space() could return false. Assume that any falsey value is an error.
1649       * A disk that has zero free bytes has bigger problems.
1650       * Require we have enough space to unzip the file and copy its contents, with a 10% buffer.
1651       */
1652      if ( wp_doing_cron() ) {
1653          $available_space = @disk_free_space( WP_CONTENT_DIR );
1654          if ( $available_space && ( $uncompressed_size * 2.1 ) > $available_space ) {
1655              return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), compact( 'uncompressed_size', 'available_space' ) );
1656          }
1657      }
1658  
1659      $needed_dirs = array_unique( $needed_dirs );
1660      foreach ( $needed_dirs as $dir ) {
1661          // Check the parent folders of the folders all exist within the creation array.
1662          if ( untrailingslashit( $to ) == $dir ) { // Skip over the working directory, we know this exists (or will exist).
1663              continue;
1664          }
1665          if ( strpos( $dir, $to ) === false ) { // If the directory is not within the working directory, skip it.
1666              continue;
1667          }
1668  
1669          $parent_folder = dirname( $dir );
1670          while ( ! empty( $parent_folder ) && untrailingslashit( $to ) != $parent_folder && ! in_array( $parent_folder, $needed_dirs, true ) ) {
1671              $needed_dirs[] = $parent_folder;
1672              $parent_folder = dirname( $parent_folder );
1673          }
1674      }
1675      asort( $needed_dirs );
1676  
1677      // Create those directories if need be:
1678      foreach ( $needed_dirs as $_dir ) {
1679          // Only check to see if the dir exists upon creation failure. Less I/O this way.
1680          if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
1681              return new WP_Error( 'mkdir_failed_pclzip', __( 'Could not create directory.' ), substr( $_dir, strlen( $to ) ) );
1682          }
1683      }
1684      unset( $needed_dirs );
1685  
1686      // Extract the files from the zip.
1687      foreach ( $archive_files as $file ) {
1688          if ( $file['folder'] ) {
1689              continue;
1690          }
1691  
1692          if ( '__MACOSX/' === substr( $file['filename'], 0, 9 ) ) { // Don't extract the OS X-created __MACOSX directory files.
1693              continue;
1694          }
1695  
1696          // Don't extract invalid files:
1697          if ( 0 !== validate_file( $file['filename'] ) ) {
1698              continue;
1699          }
1700  
1701          if ( ! $wp_filesystem->put_contents( $to . $file['filename'], $file['content'], FS_CHMOD_FILE ) ) {
1702              return new WP_Error( 'copy_failed_pclzip', __( 'Could not copy file.' ), $file['filename'] );
1703          }
1704      }
1705      return true;
1706  }
1707  
1708  /**
1709   * Copies a directory from one location to another via the WordPress Filesystem
1710   * Abstraction.
1711   *
1712   * Assumes that WP_Filesystem() has already been called and setup.
1713   *
1714   * @since 2.5.0
1715   *
1716   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
1717   *
1718   * @param string   $from      Source directory.
1719   * @param string   $to        Destination directory.
1720   * @param string[] $skip_list An array of files/folders to skip copying.
1721   * @return true|WP_Error True on success, WP_Error on failure.
1722   */
1723  function copy_dir( $from, $to, $skip_list = array() ) {
1724      global $wp_filesystem;
1725  
1726      $dirlist = $wp_filesystem->dirlist( $from );
1727  
1728      $from = trailingslashit( $from );
1729      $to   = trailingslashit( $to );
1730  
1731      foreach ( (array) $dirlist as $filename => $fileinfo ) {
1732          if ( in_array( $filename, $skip_list, true ) ) {
1733              continue;
1734          }
1735  
1736          if ( 'f' === $fileinfo['type'] ) {
1737              if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
1738                  // If copy failed, chmod file to 0644 and try again.
1739                  $wp_filesystem->chmod( $to . $filename, FS_CHMOD_FILE );
1740                  if ( ! $wp_filesystem->copy( $from . $filename, $to . $filename, true, FS_CHMOD_FILE ) ) {
1741                      return new WP_Error( 'copy_failed_copy_dir', __( 'Could not copy file.' ), $to . $filename );
1742                  }
1743              }
1744          } elseif ( 'd' === $fileinfo['type'] ) {
1745              if ( ! $wp_filesystem->is_dir( $to . $filename ) ) {
1746                  if ( ! $wp_filesystem->mkdir( $to . $filename, FS_CHMOD_DIR ) ) {
1747                      return new WP_Error( 'mkdir_failed_copy_dir', __( 'Could not create directory.' ), $to . $filename );
1748                  }
1749              }
1750  
1751              // Generate the $sub_skip_list for the subdirectory as a sub-set of the existing $skip_list.
1752              $sub_skip_list = array();
1753              foreach ( $skip_list as $skip_item ) {
1754                  if ( 0 === strpos( $skip_item, $filename . '/' ) ) {
1755                      $sub_skip_list[] = preg_replace( '!^' . preg_quote( $filename, '!' ) . '/!i', '', $skip_item );
1756                  }
1757              }
1758  
1759              $result = copy_dir( $from . $filename, $to . $filename, $sub_skip_list );
1760              if ( is_wp_error( $result ) ) {
1761                  return $result;
1762              }
1763          }
1764      }
1765  
1766      return true;
1767  }
1768  
1769  /**
1770   * Initializes and connects the WordPress Filesystem Abstraction classes.
1771   *
1772   * This function will include the chosen transport and attempt connecting.
1773   *
1774   * Plugins may add extra transports, And force WordPress to use them by returning
1775   * the filename via the {@see 'filesystem_method_file'} filter.
1776   *
1777   * @since 2.5.0
1778   *
1779   * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
1780   *
1781   * @param array|false  $args                         Optional. Connection args, These are passed directly to
1782   *                                                   the `WP_Filesystem_*()` classes. Default false.
1783   * @param string|false $context                      Optional. Context for get_filesystem_method(). Default false.
1784   * @param bool         $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
1785   * @return bool|null True on success, false on failure, null if the filesystem method class file does not exist.
1786   */
1787  function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
1788      global $wp_filesystem;
1789  
1790      require_once  ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';
1791  
1792      $method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );
1793  
1794      if ( ! $method ) {
1795          return false;
1796      }
1797  
1798      if ( ! class_exists( "WP_Filesystem_$method" ) ) {
1799  
1800          /**
1801           * Filters the path for a specific filesystem method class file.
1802           *
1803           * @since 2.6.0
1804           *
1805           * @see get_filesystem_method()
1806           *
1807           * @param string $path   Path to the specific filesystem method class file.
1808           * @param string $method The filesystem method to use.
1809           */
1810          $abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );
1811  
1812          if ( ! file_exists( $abstraction_file ) ) {
1813              return;
1814          }
1815  
1816          require_once $abstraction_file;
1817      }
1818      $method = "WP_Filesystem_$method";
1819  
1820      $wp_filesystem = new $method( $args );
1821  
1822      /*
1823       * Define the timeouts for the connections. Only available after the constructor is called
1824       * to allow for per-transport overriding of the default.
1825       */
1826      if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
1827          define( 'FS_CONNECT_TIMEOUT', 30 );
1828      }
1829      if ( ! defined( 'FS_TIMEOUT' ) ) {
1830          define( 'FS_TIMEOUT', 30 );
1831      }
1832  
1833      if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
1834          return false;
1835      }
1836  
1837      if ( ! $wp_filesystem->connect() ) {
1838          return false; // There was an error connecting to the server.
1839      }
1840  
1841      // Set the permission constants if not already set.
1842      if ( ! defined( 'FS_CHMOD_DIR' ) ) {
1843          define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
1844      }
1845      if ( ! defined( 'FS_CHMOD_FILE' ) ) {
1846          define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
1847      }
1848  
1849      return true;
1850  }
1851  
1852  /**
1853   * Determines which method to use for reading, writing, modifying, or deleting
1854   * files on the filesystem.
1855   *
1856   * The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets
1857   * (Via Sockets class, or `fsockopen()`). Valid values for these are: 'direct', 'ssh2',
1858   * 'ftpext' or 'ftpsockets'.
1859   *
1860   * The return value can be overridden by defining the `FS_METHOD` constant in `wp-config.php`,
1861   * or filtering via {@see 'filesystem_method'}.
1862   *
1863   * @link https://wordpress.org/support/article/editing-wp-config-php/#wordpress-upgrade-constants
1864   *
1865   * Plugins may define a custom transport handler, See WP_Filesystem().
1866   *
1867   * @since 2.5.0
1868   *
1869   * @global callable $_wp_filesystem_direct_method
1870   *
1871   * @param array  $args                         Optional. Connection details. Default empty array.
1872   * @param string $context                      Optional. Full path to the directory that is tested
1873   *                                             for being writable. Default empty.
1874   * @param bool   $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable.
1875   *                                             Default false.
1876   * @return string The transport to use, see description for valid return values.
1877   */
1878  function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) {
1879      // Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'.
1880      $method = defined( 'FS_METHOD' ) ? FS_METHOD : false;
1881  
1882      if ( ! $context ) {
1883          $context = WP_CONTENT_DIR;
1884      }
1885  
1886      // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it.
1887      if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) {
1888          $context = dirname( $context );
1889      }
1890  
1891      $context = trailingslashit( $context );
1892  
1893      if ( ! $method ) {
1894  
1895          $temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) );
1896          $temp_handle    = @fopen( $temp_file_name, 'w' );
1897          if ( $temp_handle ) {
1898  
1899              // Attempt to determine the file owner of the WordPress files, and that of newly created files.
1900              $wp_file_owner   = false;
1901              $temp_file_owner = false;
1902              if ( function_exists( 'fileowner' ) ) {
1903                  $wp_file_owner   = @fileowner( __FILE__ );
1904                  $temp_file_owner = @fileowner( $temp_file_name );
1905              }
1906  
1907              if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) {
1908                  /*
1909                   * WordPress is creating files as the same owner as the WordPress files,
1910                   * this means it's safe to modify & create new files via PHP.
1911                   */
1912                  $method                                  = 'direct';
1913                  $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner';
1914              } elseif ( $allow_relaxed_file_ownership ) {
1915                  /*
1916                   * The $context directory is writable, and $allow_relaxed_file_ownership is set,
1917                   * this means we can modify files safely in this directory.
1918                   * This mode doesn't create new files, only alter existing ones.
1919                   */
1920                  $method                                  = 'direct';
1921                  $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership';
1922              }
1923  
1924              fclose( $temp_handle );
1925              @unlink( $temp_file_name );
1926          }
1927      }
1928  
1929      if ( ! $method && isset( $args['connection_type'] ) && 'ssh' === $args['connection_type'] && extension_loaded( 'ssh2' ) ) {
1930          $method = 'ssh2';
1931      }
1932      if ( ! $method && extension_loaded( 'ftp' ) ) {
1933          $method = 'ftpext';
1934      }
1935      if ( ! $method && ( extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) ) {
1936          $method = 'ftpsockets'; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread.
1937      }
1938  
1939      /**
1940       * Filters the filesystem method to use.
1941       *
1942       * @since 2.6.0
1943       *
1944       * @param string $method  Filesystem method to return.
1945       * @param array  $args    An array of connection details for the method.
1946       * @param string $context Full path to the directory that is tested for being writable.
1947       * @param bool   $allow_relaxed_file_ownership Whether to allow Group/World writable.
1948       */
1949      return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );
1950  }
1951  
1952  /**
1953   * Displays a form to the user to request for their FTP/SSH details in order
1954   * to connect to the filesystem.
1955   *
1956   * All chosen/entered details are saved, excluding the password.
1957   *
1958   * Hostnames may be in the form of hostname:portnumber (eg: wordpress.org:2467)
1959   * to specify an alternate FTP/SSH port.
1960   *
1961   * Plugins may override this form by returning true|false via the {@see 'request_filesystem_credentials'} filter.
1962   *
1963   * @since 2.5.0
1964   * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
1965   *
1966   * @global string $pagenow
1967   *
1968   * @param string        $form_post                    The URL to post the form to.
1969   * @param string        $type                         Optional. Chosen type of filesystem. Default empty.
1970   * @param bool|WP_Error $error                        Optional. Whether the current request has failed to connect,
1971   *                                                    or an error object. Default false.
1972   * @param string        $context                      Optional. Full path to the directory that is tested for being
1973   *                                                    writable. Default empty.
1974   * @param array         $extra_fields                 Optional. Extra `POST` fields to be checked for inclusion in
1975   *                                                    the post. Default null.
1976   * @param bool          $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
1977   *
1978   * @return bool|array True if no filesystem credentials are required, false if they are required but have not been
1979   *                    provided, array of credentials if they are required and have been provided.
1980   */
1981  function request_filesystem_credentials( $form_post, $type = '', $error = false, $context = '', $extra_fields = null, $allow_relaxed_file_ownership = false ) {
1982      global $pagenow;
1983  
1984      /**
1985       * Filters the filesystem credentials.
1986       *
1987       * Returning anything other than an empty string will effectively short-circuit
1988       * output of the filesystem credentials form, returning that value instead.
1989       *
1990       * A filter should return true if no filesystem credentials are required, false if they are required but have not been
1991       * provided, or an array of credentials if they are required and have been provided.
1992       *
1993       * @since 2.5.0
1994       * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
1995       *
1996       * @param mixed         $credentials                  Credentials to return instead. Default empty string.
1997       * @param string        $form_post                    The URL to post the form to.
1998       * @param string        $type                         Chosen type of filesystem.
1999       * @param bool|WP_Error $error                        Whether the current request has failed to connect,
2000       *                                                    or an error object.
2001       * @param string        $context                      Full path to the directory that is tested for
2002       *                                                    being writable.
2003       * @param array         $extra_fields                 Extra POST fields.
2004       * @param bool          $allow_relaxed_file_ownership Whether to allow Group/World writable.
2005       */
2006      $req_cred = apply_filters( 'request_filesystem_credentials', '', $form_post, $type, $error, $context, $extra_fields, $allow_relaxed_file_ownership );
2007      if ( '' !== $req_cred ) {
2008          return $req_cred;
2009      }
2010  
2011      if ( empty( $type ) ) {
2012          $type = get_filesystem_method( array(), $context, $allow_relaxed_file_ownership );
2013      }
2014  
2015      if ( 'direct' === $type ) {
2016          return true;
2017      }
2018  
2019      if ( is_null( $extra_fields ) ) {
2020          $extra_fields = array( 'version', 'locale' );
2021      }
2022  
2023      $credentials = get_option(
2024          'ftp_credentials',
2025          array(
2026              'hostname' => '',
2027              'username' => '',
2028          )
2029      );
2030  
2031      $submitted_form = wp_unslash( $_POST );
2032  
2033      // Verify nonce, or unset submitted form field values on failure.
2034      if ( ! isset( $_POST['_fs_nonce'] ) || ! wp_verify_nonce( $_POST['_fs_nonce'], 'filesystem-credentials' ) ) {
2035          unset(
2036              $submitted_form['hostname'],
2037              $submitted_form['username'],
2038              $submitted_form['password'],
2039              $submitted_form['public_key'],
2040              $submitted_form['private_key'],
2041              $submitted_form['connection_type']
2042          );
2043      }
2044  
2045      // If defined, set it to that. Else, if POST'd, set it to that. If not, set it to whatever it previously was (saved details in option).
2046      $credentials['hostname'] = defined( 'FTP_HOST' ) ? FTP_HOST : ( ! empty( $submitted_form['hostname'] ) ? $submitted_form['hostname'] : $credentials['hostname'] );
2047      $credentials['username'] = defined( 'FTP_USER' ) ? FTP_USER : ( ! empty( $submitted_form['username'] ) ? $submitted_form['username'] : $credentials['username'] );
2048      $credentials['password'] = defined( 'FTP_PASS' ) ? FTP_PASS : ( ! empty( $submitted_form['password'] ) ? $submitted_form['password'] : '' );
2049  
2050      // Check to see if we are setting the public/private keys for ssh.
2051      $credentials['public_key']  = defined( 'FTP_PUBKEY' ) ? FTP_PUBKEY : ( ! empty( $submitted_form['public_key'] ) ? $submitted_form['public_key'] : '' );
2052      $credentials['private_key'] = defined( 'FTP_PRIKEY' ) ? FTP_PRIKEY : ( ! empty( $submitted_form['private_key'] ) ? $submitted_form['private_key'] : '' );
2053  
2054      // Sanitize the hostname, some people might pass in odd data.
2055      $credentials['hostname'] = preg_replace( '|\w+://|', '', $credentials['hostname'] ); // Strip any schemes off.
2056  
2057      if ( strpos( $credentials['hostname'], ':' ) ) {
2058          list( $credentials['hostname'], $credentials['port'] ) = explode( ':', $credentials['hostname'], 2 );
2059          if ( ! is_numeric( $credentials['port'] ) ) {
2060              unset( $credentials['port'] );
2061          }
2062      } else {
2063          unset( $credentials['port'] );
2064      }
2065  
2066      if ( ( defined( 'FTP_SSH' ) && FTP_SSH ) || ( defined( 'FS_METHOD' ) && 'ssh2' === FS_METHOD ) ) {
2067          $credentials['connection_type'] = 'ssh';
2068      } elseif ( ( defined( 'FTP_SSL' ) && FTP_SSL ) && 'ftpext' === $type ) { // Only the FTP Extension understands SSL.
2069          $credentials['connection_type'] = 'ftps';
2070      } elseif ( ! empty( $submitted_form['connection_type'] ) ) {
2071          $credentials['connection_type'] = $submitted_form['connection_type'];
2072      } elseif ( ! isset( $credentials['connection_type'] ) ) { // All else fails (and it's not defaulted to something else saved), default to FTP.
2073          $credentials['connection_type'] = 'ftp';
2074      }
2075      if ( ! $error
2076          && ( ( ! empty( $credentials['password'] ) && ! empty( $credentials['username'] ) && ! empty( $credentials['hostname'] ) )
2077              || ( 'ssh' === $credentials['connection_type'] && ! empty( $credentials['public_key'] ) && ! empty( $credentials['private_key'] ) )
2078          )
2079      ) {
2080          $stored_credentials = $credentials;
2081  
2082          if ( ! empty( $stored_credentials['port'] ) ) { // Save port as part of hostname to simplify above code.
2083              $stored_credentials['hostname'] .= ':' . $stored_credentials['port'];
2084          }
2085  
2086          unset( $stored_credentials['password'], $stored_credentials['port'], $stored_credentials['private_key'], $stored_credentials['public_key'] );
2087  
2088          if ( ! wp_installing() ) {
2089              update_option( 'ftp_credentials', $stored_credentials );
2090          }
2091  
2092          return $credentials;
2093      }
2094      $hostname        = isset( $credentials['hostname'] ) ? $credentials['hostname'] : '';
2095      $username        = isset( $credentials['username'] ) ? $credentials['username'] : '';
2096      $public_key      = isset( $credentials['public_key'] ) ? $credentials['public_key'] : '';
2097      $private_key     = isset( $credentials['private_key'] ) ? $credentials['private_key'] : '';
2098      $port            = isset( $credentials['port'] ) ? $credentials['port'] : '';
2099      $connection_type = isset( $credentials['connection_type'] ) ? $credentials['connection_type'] : '';
2100  
2101      if ( $error ) {
2102          $error_string = __( '<strong>Error</strong>: There was an error connecting to the server. Please verify the settings are correct.' );
2103          if ( is_wp_error( $error ) ) {
2104              $error_string = esc_html( $error->get_error_message() );
2105          }
2106          echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
2107      }
2108  
2109      $types = array();
2110      if ( extension_loaded( 'ftp' ) || extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) {
2111          $types['ftp'] = __( 'FTP' );
2112      }
2113      if ( extension_loaded( 'ftp' ) ) { // Only this supports FTPS.
2114          $types['ftps'] = __( 'FTPS (SSL)' );
2115      }
2116      if ( extension_loaded( 'ssh2' ) ) {
2117          $types['ssh'] = __( 'SSH2' );
2118      }
2119  
2120      /**
2121       * Filters the connection types to output to the filesystem credentials form.
2122       *
2123       * @since 2.9.0
2124       * @since 4.6.0 The `$context` parameter default changed from `false` to an empty string.
2125       *
2126       * @param string[]      $types       Types of connections.
2127       * @param array         $credentials Credentials to connect with.
2128       * @param string        $type        Chosen filesystem method.
2129       * @param bool|WP_Error $error       Whether the current request has failed to connect,
2130       *                                   or an error object.
2131       * @param string        $context     Full path to the directory that is tested for being writable.
2132       */
2133      $types = apply_filters( 'fs_ftp_connection_types', $types, $credentials, $type, $error, $context );
2134  
2135      ?>
2136  <form action="<?php echo esc_url( $form_post ); ?>" method="post">
2137  <div id="request-filesystem-credentials-form" class="request-filesystem-credentials-form">
2138      <?php
2139      // Print a H1 heading in the FTP credentials modal dialog, default is a H2.
2140      $heading_tag = 'h2';
2141      if ( 'plugins.php' === $pagenow || 'plugin-install.php' === $pagenow ) {
2142          $heading_tag = 'h1';
2143      }
2144      echo "<$heading_tag id='request-filesystem-credentials-title'>" . __( 'Connection Information' ) . "</$heading_tag>";
2145      ?>
2146  <p id="request-filesystem-credentials-desc">
2147      <?php
2148      $label_user = __( 'Username' );
2149      $label_pass = __( 'Password' );
2150      _e( 'To perform the requested action, WordPress needs to access your web server.' );
2151      echo ' ';
2152      if ( ( isset( $types['ftp'] ) || isset( $types['ftps'] ) ) ) {
2153          if ( isset( $types['ssh'] ) ) {
2154              _e( 'Please enter your FTP or SSH credentials to proceed.' );
2155              $label_user = __( 'FTP/SSH Username' );
2156              $label_pass = __( 'FTP/SSH Password' );
2157          } else {
2158              _e( 'Please enter your FTP credentials to proceed.' );
2159              $label_user = __( 'FTP Username' );
2160              $label_pass = __( 'FTP Password' );
2161          }
2162          echo ' ';
2163      }
2164      _e( 'If you do not remember your credentials, you should contact your web host.' );
2165  
2166      $hostname_value = esc_attr( $hostname );
2167      if ( ! empty( $port ) ) {
2168          $hostname_value .= ":$port";
2169      }
2170  
2171      $password_value = '';
2172      if ( defined( 'FTP_PASS' ) ) {
2173          $password_value = '*****';
2174      }
2175      ?>
2176  </p>
2177  <label for="hostname">
2178      <span class="field-title"><?php _e( 'Hostname' ); ?></span>
2179      <input name="hostname" type="text" id="hostname" aria-describedby="request-filesystem-credentials-desc" class="code" placeholder="<?php esc_attr_e( 'example: www.wordpress.org' ); ?>" value="<?php echo $hostname_value; ?>"<?php disabled( defined( 'FTP_HOST' ) ); ?> />
2180  </label>
2181  <div class="ftp-username">
2182      <label for="username">
2183          <span class="field-title"><?php echo $label_user; ?></span>
2184          <input name="username" type="text" id="username" value="<?php echo esc_attr( $username ); ?>"<?php disabled( defined( 'FTP_USER' ) ); ?> />
2185      </label>
2186  </div>
2187  <div class="ftp-password">
2188      <label for="password">
2189          <span class="field-title"><?php echo $label_pass; ?></span>
2190          <input name="password" type="password" id="password" value="<?php echo $password_value; ?>"<?php disabled( defined( 'FTP_PASS' ) ); ?> />
2191          <em>
2192          <?php
2193          if ( ! defined( 'FTP_PASS' ) ) {
2194              _e( 'This password will not be stored on the server.' );}
2195          ?>
2196  </em>
2197      </label>
2198  </div>
2199  <fieldset>
2200  <legend><?php _e( 'Connection Type' ); ?></legend>
2201      <?php
2202      $disabled = disabled( ( defined( 'FTP_SSL' ) && FTP_SSL ) || ( defined( 'FTP_SSH' ) && FTP_SSH ), true, false );
2203      foreach ( $types as $name => $text ) :
2204          ?>
2205      <label for="<?php echo esc_attr( $name ); ?>">
2206          <input type="radio" name="connection_type" id="<?php echo esc_attr( $name ); ?>" value="<?php echo esc_attr( $name ); ?>" <?php checked( $name, $connection_type ); ?> <?php echo $disabled; ?> />
2207          <?php echo $text; ?>
2208      </label>
2209          <?php
2210      endforeach;
2211      ?>
2212  </fieldset>
2213      <?php
2214      if ( isset( $types['ssh'] ) ) {
2215          $hidden_class = '';
2216          if ( 'ssh' !== $connection_type || empty( $connection_type ) ) {
2217              $hidden_class = ' class="hidden"';
2218          }
2219          ?>
2220  <fieldset id="ssh-keys"<?php echo $hidden_class; ?>>
2221  <legend><?php _e( 'Authentication Keys' ); ?></legend>
2222  <label for="public_key">
2223      <span class="field-title"><?php _e( 'Public Key:' ); ?></span>
2224      <input name="public_key" type="text" id="public_key" aria-describedby="auth-keys-desc" value="<?php echo esc_attr( $public_key ); ?>"<?php disabled( defined( 'FTP_PUBKEY' ) ); ?> />
2225  </label>
2226  <label for="private_key">
2227      <span class="field-title"><?php _e( 'Private Key:' ); ?></span>
2228      <input name="private_key" type="text" id="private_key" value="<?php echo esc_attr( $private_key ); ?>"<?php disabled( defined( 'FTP_PRIKEY' ) ); ?> />
2229  </label>
2230  <p id="auth-keys-desc"><?php _e( 'Enter the location on the server where the public and private keys are located. If a passphrase is needed, enter that in the password field above.' ); ?></p>
2231  </fieldset>
2232          <?php
2233      }
2234  
2235      foreach ( (array) $extra_fields as $field ) {
2236          if ( isset( $submitted_form[ $field ] ) ) {
2237              echo '<input type="hidden" name="' . esc_attr( $field ) . '" value="' . esc_attr( $submitted_form[ $field ] ) . '" />';
2238          }
2239      }
2240      ?>
2241      <p class="request-filesystem-credentials-action-buttons">
2242          <?php wp_nonce_field( 'filesystem-credentials', '_fs_nonce', false, true ); ?>
2243          <button class="button cancel-button" data-js-action="close" type="button"><?php _e( 'Cancel' ); ?></button>
2244          <?php submit_button( __( 'Proceed' ), '', 'upgrade', false ); ?>
2245      </p>
2246  </div>
2247  </form>
2248      <?php
2249      return false;
2250  }
2251  
2252  /**
2253   * Print the filesystem credentials modal when needed.
2254   *
2255   * @since 4.2.0
2256   */
2257  function wp_print_request_filesystem_credentials_modal() {
2258      $filesystem_method = get_filesystem_method();
2259  
2260      ob_start();
2261      $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
2262      ob_end_clean();
2263  
2264      $request_filesystem_credentials = ( 'direct' !== $filesystem_method && ! $filesystem_credentials_are_stored );
2265      if ( ! $request_filesystem_credentials ) {
2266          return;
2267      }
2268      ?>
2269      <div id="request-filesystem-credentials-dialog" class="notification-dialog-wrap request-filesystem-credentials-dialog">
2270          <div class="notification-dialog-background"></div>
2271          <div class="notification-dialog" role="dialog" aria-labelledby="request-filesystem-credentials-title" tabindex="0">
2272              <div class="request-filesystem-credentials-dialog-content">
2273                  <?php request_filesystem_credentials( site_url() ); ?>
2274              </div>
2275          </div>
2276      </div>
2277      <?php
2278  }


Generated : Thu May 28 08:20:02 2020 Cross-referenced by PHPXref