Make WordPress Core


Ignore:
Timestamp:
02/06/2024 11:44:09 PM (5 months ago)
Author:
costdev
Message:

Upgrade/Install: Introduce Plugin Dependencies.

Introduces a new "Requires Plugins" plugin header so that plugin developers can list the slugs of the plugins theirs depends on.

This will inform users of the requirements, and provide links to the WordPress.org Plugins Repository that they can click to install and activate the dependencies first.

Plugins whose requirements are not met cannot be installed or activated, and they will be deactivated automatically if their requirements become unmet.
Plugins that others rely on cannot be deactivated or deleted until their dependent plugins are deactivated or deleted.

In memory of Alex Mills and Alex King.
WordPress Remembers.

Props ahoereth, afragen, alanfuller, alexkingorg, amykamala, anonymized_10690803, apeatling, ashfame, atimmer, audrasjb, aristath, azaozz, batmoo, beaulebens, blobaugh, bobbingwide, boonebgorges, brianhenryie, chanthaboune, chrisdavidmiles, coolmann, costdev, courane01, danielbachhuber, davidperez, dd32, Denis-de-Bernardy, dingo_d, DJPaul, dougal, DrewAPicture, ethitter, filosofo, georgestephanis, giuseppemazzapica-1, goldenapples, griffinjt, hellofromTonya, husobj, ideag, jarednova, jbobich, jbrinley, jltallon, joedolson, johnciacia, johnjamesjacoby, joppuyo, jsmoriss, karmatosed, kebbet, knutsp, kraftbj, kraftner, kurtpayne, lkraav, logikal16, luisherranz, man4toman, markjaquith, matt, mbijon, megphillips91, mikeschinkel, mordauk, morehawes, mrwweb, mte90, mukesh27, mzaweb, nacin, norcross, nvwd, nwjames, obliviousharmony, ocean90, oglekler, paaljoachim, pauldewouters, pbaylies, pbiron, peterwilsoncc, Philipp15b, poena, pogidude, retlehs, rmccue, ryan, sabreuse, sc0ttkclark, scribu, sereedmedia, SergeyBiryukov, ShaneF, shidouhikari, soean, spacedmonkey, stephenh1988, swissspidy, taylorde, tazotodua, threadi, TimothyBlynJacobs, TJNowell, tollmanz, toscho, tropicalista, Viper007Bond, westi, whiteshadow, williamsba1, wpsmith, ZaneMatthew.
Fixes #22316.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-settings.php

    r57539 r57545  
    413413$GLOBALS['wp_plugin_paths'] = array();
    414414
     415// Load and initialize WP_Plugin_Dependencies.
     416require_once ABSPATH . WPINC . '/class-wp-plugin-dependencies.php';
     417if ( ! defined( 'WP_RUN_CORE_TESTS' ) ) {
     418    WP_Plugin_Dependencies::initialize();
     419}
     420
    415421// Load must-use plugins.
    416422foreach ( wp_get_mu_plugins() as $mu_plugin ) {
     
    487493
    488494// Load active plugins.
     495$all_plugin_data    = get_option( 'plugin_data', array() );
     496$failed_plugins     = array();
     497$update_plugin_data = false;
    489498foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
     499    $plugin_file = str_replace( trailingslashit( WP_PLUGIN_DIR ), '', $plugin );
     500    if ( ! isset( $all_plugin_data[ $plugin_file ] ) ) {
     501        require_once ABSPATH . 'wp-admin/includes/plugin.php';
     502        $all_plugin_data[ $plugin_file ] = get_plugin_data( WP_PLUGIN_DIR . "/$plugin_file" );
     503
     504        $update_plugin_data = true;
     505    }
     506
     507    $plugin_headers   = $all_plugin_data[ $plugin_file ];
     508    $errors           = array();
     509    $requirements     = array(
     510        'requires'         => ! empty( $plugin_headers['RequiresWP'] ) ? $plugin_headers['RequiresWP'] : '',
     511        'requires_php'     => ! empty( $plugin_headers['RequiresPHP'] ) ? $plugin_headers['RequiresPHP'] : '',
     512        'requires_plugins' => ! empty( $plugin_headers['RequiresPlugins'] ) ? $plugin_headers['RequiresPlugins'] : '',
     513    );
     514    $compatible_wp    = is_wp_version_compatible( $requirements['requires'] );
     515    $compatible_php   = is_php_version_compatible( $requirements['requires_php'] );
     516    $dependencies_met = ! WP_Plugin_Dependencies::has_unmet_dependencies( $plugin_file );
     517
     518    $php_update_message = '</p><p>' . sprintf(
     519        /* translators: %s: URL to Update PHP page. */
     520        __( '<a href="%s">Learn more about updating PHP</a>.' ),
     521        esc_url( wp_get_update_php_url() )
     522    );
     523
     524    $annotation = wp_get_update_php_annotation();
     525
     526    if ( $annotation ) {
     527        $php_update_message .= '</p><p><em>' . $annotation . '</em>';
     528    }
     529
     530    if ( ! $dependencies_met ) {
     531        $errors[] = sprintf(
     532            /* translators: %s: The plugin's name. */
     533            _x( '%s has unmet dependencies.', 'plugin' ),
     534            $plugin_headers['Name']
     535        );
     536    }
     537
     538    if ( ! $compatible_wp && ! $compatible_php ) {
     539        $errors[] = sprintf(
     540            /* translators: 1: Current WordPress version, 2: Current PHP version, 3: Plugin name, 4: Required WordPress version, 5: Required PHP version. */
     541            _x( '<strong>Error:</strong> Current versions of WordPress (%1$s) and PHP (%2$s) do not meet minimum requirements for %3$s. The plugin requires WordPress %4$s and PHP %5$s.', 'plugin' ),
     542            get_bloginfo( 'version' ),
     543            PHP_VERSION,
     544            $plugin_headers['Name'],
     545            $requirements['requires'],
     546            $requirements['requires_php']
     547        ) . $php_update_message;
     548    } elseif ( ! $compatible_php ) {
     549        $errors[] = sprintf(
     550            /* translators: 1: Current PHP version, 2: Plugin name, 3: Required PHP version. */
     551            _x( '<strong>Error:</strong> Current PHP version (%1$s) does not meet minimum requirements for %2$s. The plugin requires PHP %3$s.', 'plugin' ),
     552            PHP_VERSION,
     553            $plugin_headers['Name'],
     554            $requirements['requires_php']
     555        ) . $php_update_message;
     556    } elseif ( ! $compatible_wp ) {
     557        $errors[] = sprintf(
     558            /* translators: 1: Current WordPress version, 2: Plugin name, 3: Required WordPress version. */
     559            _x( '<strong>Error:</strong> Current WordPress version (%1$s) does not meet minimum requirements for %2$s. The plugin requires WordPress %3$s.', 'plugin' ),
     560            get_bloginfo( 'version' ),
     561            $plugin_headers['Name'],
     562            $requirements['requires']
     563        );
     564    }
     565
     566    if ( ! empty( $errors ) ) {
     567        $failed_plugins[ $plugin_file ] = '';
     568        foreach ( $errors as $error ) {
     569            $failed_plugins[ $plugin_file ] .= wp_get_admin_notice(
     570                $error,
     571                array(
     572                    'type'        => 'error',
     573                    'dismissible' => true,
     574                )
     575            );
     576        }
     577        continue;
     578    }
     579
    490580    wp_register_plugin_realpath( $plugin );
    491581
     
    505595unset( $plugin, $_wp_plugin_file );
    506596
     597if ( $update_plugin_data ) {
     598    update_option( 'plugin_data', $all_plugin_data );
     599}
     600
     601if ( ! empty( $failed_plugins ) ) {
     602    add_action(
     603        'admin_notices',
     604        function () use ( $failed_plugins ) {
     605            global $pagenow;
     606
     607            if ( 'index.php' === $pagenow || 'plugins.php' === $pagenow ) {
     608                echo implode( '', $failed_plugins );
     609            }
     610        }
     611    );
     612}
     613unset( $failed_plugins );
     614
    507615// Load pluggable functions.
    508616require ABSPATH . WPINC . '/pluggable.php';
Note: See TracChangeset for help on using the changeset viewer.