• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / Theme_Upgrader::check_package() – Checks that the package source contains a valid theme.

You appear to be a bot. Output may be restricted

Description

Checks that the package source contains a valid theme.

Hooked to the upgrader_source_selection filter by Theme_Upgrader::install().

Usage

$string|WP_Error = Theme_Upgrader::check_package( $source );

Parameters

$source
( string ) required – The path to the downloaded package source.

Returns

string|WP_Error The source as passed, or a WP_Error object on failure.

Source

File name: wordpress/wp-admin/includes/class-theme-upgrader.php


Lines:

1 to 95 of 95
  public function check_package( $source ) {
    global $wp_filesystem, $wp_version;

    $this->new_theme_data = array();

    if ( is_wp_error( $source ) ) {
      return $source;
    }

    // Check that the folder contains a valid theme.
    $working_directory = str_replace( $wp_filesystem->wp_content_dir(), trailingslashit( WP_CONTENT_DIR ), $source );
    if ( ! is_dir( $working_directory ) ) { // Sanity check, if the above fails, let's not prevent installation.
      return $source;
    }

    // A proper archive should have a style.css file in the single subdirectory.
    if ( ! file_exists( $working_directory . 'style.css' ) ) {
      return new WP_Error(
        'incompatible_archive_theme_no_style',
        $this->strings['incompatible_archive'],
        sprintf(
          /* translators: %s: style.css */
          __( 'The theme is missing the %s stylesheet.' ),
          '<code>style.css</code>'
        )
      );
    }

    // All these headers are needed on Theme_Installer_Skin::do_overwrite().
    $info = get_file_data(
      $working_directory . 'style.css',
      array(
        'Name'        => 'Theme Name',
        'Version'     => 'Version',
        'Author'      => 'Author',
        'Template'    => 'Template',
        'RequiresWP'  => 'Requires at least',
        'RequiresPHP' => 'Requires PHP',
      )
    );

    if ( empty( $info['Name'] ) ) {
      return new WP_Error(
        'incompatible_archive_theme_no_name',
        $this->strings['incompatible_archive'],
        sprintf(
          /* translators: %s: style.css */
          __( 'The %s stylesheet doesn&#8217;t contain a valid theme header.' ),
          '<code>style.css</code>'
        )
      );
    }

    // If it's not a child theme, it must have at least an index.php to be legit.
    if ( empty( $info['Template'] ) && ! file_exists( $working_directory . 'index.php' ) ) {
      return new WP_Error(
        'incompatible_archive_theme_no_index',
        $this->strings['incompatible_archive'],
        sprintf(
          /* translators: %s: index.php */
          __( 'The theme is missing the %s file.' ),
          '<code>index.php</code>'
        )
      );
    }

    $requires_php = isset( $info['RequiresPHP'] ) ? $info['RequiresPHP'] : null;
    $requires_wp  = isset( $info['RequiresWP'] ) ? $info['RequiresWP'] : null;

    if ( ! is_php_version_compatible( $requires_php ) ) {
      $error = sprintf(
        /* translators: 1: Current PHP version, 2: Version required by the uploaded theme. */
        __( 'The PHP version on your server is %1$s, however the uploaded theme requires %2$s.' ),
        phpversion(),
        $requires_php
      );

      return new WP_Error( 'incompatible_php_required_version', $this->strings['incompatible_archive'], $error );
    }
    if ( ! is_wp_version_compatible( $requires_wp ) ) {
      $error = sprintf(
        /* translators: 1: Current WordPress version, 2: Version required by the uploaded theme. */
        __( 'Your WordPress version is %1$s, however the uploaded theme requires %2$s.' ),
        $wp_version,
        $requires_wp
      );

      return new WP_Error( 'incompatible_wp_required_version', $this->strings['incompatible_archive'], $error );
    }

    $this->new_theme_data = $info;

    return $source;
  }
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 9th December 2020

Primary Sidebar

Information

Function name: Theme_Upgrader::check_package
Class ref: Theme_Upgrader
Plugin ref: WordPress
Version: 5.6
Sourcefile: wp-admin/includes/class-theme-upgrader.php
File ref: wp-admin/includes/class-theme-upgrader.php
Deprecated?: No
API Letters: C,P,T,U

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites