Skip to:
Content

BuddyPress.org

Changeset 13304


Ignore:
Timestamp:
07/26/2022 01:31:07 PM (20 months ago)
Author:
imath
Message:

Start using a new strategy about deprecated functions management

The main goal of this commit is to change the way we load the files containing deprecated functions. This files are located into the src/bp-core/deprecated/ directory and there's one file for each version of BuddyPress we created to store the deprecate functions.

We noticed that these files were not necessarily loaded although they should have. This case happens to all users who firstly installed BuddyPress when its version was upper than 2.7.

From now on, here's how deprecated code will eventually load into your BuddyPress installation:

  • First installs won't load deprecated code.
  • Updated installs will load the 2 latest BuddyPress versions deprecated code.
  • Defining the BP_IGNORE_DEPRECATED constant to true will change the 2 above points behavior ignoring any BuddyPress versions deprecated code.
  • Defining the BP_IGNORE_DEPRECATED constant to false will change the 2 first points behavior loading all BuddyPress versions deprecated code.

Props djpaul, dcavins.

Fixes #8687

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-functions.php

    r13273 r13304  
    6969        $bp = buddypress();
    7070        return !empty( $bp->db_version_raw ) ? $bp->db_version_raw : 0;
     71    }
     72
     73/**
     74 * Output a BuddyPress major version.
     75 *
     76 * @since 11.0.0
     77 *
     78 * @param string $version BuddyPress version.
     79 */
     80function bp_major_version( $version = '' ) {
     81    echo bp_get_major_version( $version );
     82}
     83
     84    /**
     85     * Return a BuddyPress major version.
     86     *
     87     * @since 11.0.0
     88     *
     89     * @param string $version BuddyPress version.
     90     * @return string The corresponding BuddyPress major version.
     91     */
     92    function bp_get_major_version( $version = '' ) {
     93        if ( ! $version ) {
     94            $version = bp_get_version();
     95        }
     96
     97        $last_wp_like_major_versions = '2.9';
     98        $float_version               = (float) $version;
     99
     100        if ( 1 !== version_compare( $version, $last_wp_like_major_versions ) ) {
     101            $major_version = (string) $float_version;
     102        } else {
     103            $major_version = (int) $float_version . '.0';
     104        }
     105
     106        return $major_version;
     107    }
     108
     109/**
     110 * Output the BuddyPress version used for its first install.
     111 *
     112 * @since 11.0.0
     113 */
     114function bp_initial_version() {
     115    echo bp_get_initial_version();
     116}
     117
     118    /**
     119     * Return the BuddyPress version used for its first install.
     120     *
     121     * @since 11.0.0
     122     *
     123     * @return string The BuddyPress version used for its first install.
     124     */
     125    function bp_get_initial_version() {
     126        return bp_get_option( '_bp_initial_major_version', '0' );
    71127    }
    72128
     
    46754731    return $optout_class::delete_by_id( $id );
    46764732}
     4733
     4734/**
     4735 * Get the list of versions needing their deprecated functions to be loaded.
     4736 *
     4737 * @since 11.0.0
     4738 *
     4739 * @return array The list of versions needing their deprecated functions to be loaded.
     4740 */
     4741function bp_get_deprecated_functions_versions() {
     4742    $ignore_deprecated = null;
     4743    if ( defined( 'BP_IGNORE_DEPRECATED' ) ) {
     4744        $ignore_deprecated = (bool) BP_IGNORE_DEPRECATED;
     4745    }
     4746
     4747    /*
     4748     * Respect the site owner's choice to ignore deprecated functions.
     4749     * Return an empty array to inform no deprecated version files should be loaded.
     4750     */
     4751    if ( true === $ignore_deprecated ) {
     4752        return array();
     4753    }
     4754
     4755    // List of versions containing deprecated functions.
     4756    $deprecated_functions_versions = array(
     4757        '1.2',
     4758        '1.5',
     4759        '1.6',
     4760        '1.7',
     4761        '1.9',
     4762        '2.0',
     4763        '2.1',
     4764        '2.2',
     4765        '2.3',
     4766        '2.4',
     4767        '2.5',
     4768        '2.6',
     4769        '2.7',
     4770        '2.8',
     4771        '2.9',
     4772        '3.0',
     4773        '4.0',
     4774        '6.0',
     4775        '7.0',
     4776        '8.0',
     4777        '9.0',
     4778        '10.0',
     4779        '11.0',
     4780    );
     4781
     4782    /*
     4783     * Respect the site owner's choice to load all deprecated functions.
     4784     * Return an empty array to inform no deprecated version files should be loaded.
     4785     */
     4786    if ( false === $ignore_deprecated ) {
     4787        return $deprecated_functions_versions;
     4788    }
     4789
     4790    /*
     4791     * Unless the `BP_IGNORE_DEPRECATED` constant is used & set to false, the development
     4792     * version of BuddyPress do not load deprecated functions.
     4793     */
     4794    if ( defined( 'BP_SOURCE_SUBDIRECTORY' ) && BP_SOURCE_SUBDIRECTORY === 'src' ) {
     4795        return array();
     4796    }
     4797
     4798    /*
     4799     * If the constant is not defined, put our logic in place so that only the
     4800     * 2 last versions deprecated functions will be loaded for upgraded installs.
     4801     */
     4802    $initial_version        = (float) bp_get_initial_version();
     4803    $current_version        = (float) bp_get_version();
     4804    $load_latest_deprecated = $initial_version < $current_version;
     4805
     4806    // We don't load deprecated functions for new installs.
     4807    if ( ! $load_latest_deprecated ) {
     4808        // Run some additional checks if PHPUnit is running.
     4809        if ( defined( 'BP_TESTS_DIR' ) ) {
     4810            $deprecated_files = array_filter(
     4811                array_map(
     4812                    function( $file ) {
     4813                        if ( false !== strpos( $file, '.php' ) ) {
     4814                            return str_replace( '.php', '', $file );
     4815                        };
     4816                    },
     4817                    scandir( buddypress()->plugin_dir . 'bp-core/deprecated' )
     4818                )
     4819            );
     4820
     4821            if ( array_diff( $deprecated_files, $deprecated_functions_versions ) ) {
     4822                return false;
     4823            }
     4824        }
     4825        return array();
     4826    }
     4827
     4828    // Try to get the first major version that was in used when BuddyPress was fist installed.
     4829    $first_major = '';
     4830    if ( $initial_version ) {
     4831        $first_major = bp_get_major_version( $initial_version );
     4832    }
     4833
     4834    $index_first_major = array_search( $first_major, $deprecated_functions_versions, true );
     4835    if ( false === $index_first_major ) {
     4836        return array_splice( $deprecated_functions_versions, -2 );
     4837    }
     4838
     4839    $latest_deprecated_functions_versions = array_splice( $deprecated_functions_versions, $index_first_major );
     4840
     4841    if ( 2 <= count( $latest_deprecated_functions_versions ) ) {
     4842        $latest_deprecated_functions_versions = array_splice( $latest_deprecated_functions_versions, -2 );
     4843    }
     4844
     4845    $index_initial_version = array_search( $first_major, $latest_deprecated_functions_versions, true );
     4846    if ( false !== $index_initial_version ) {
     4847        unset( $latest_deprecated_functions_versions[ $index_initial_version ] );
     4848    }
     4849
     4850    return $latest_deprecated_functions_versions;
     4851}
  • trunk/src/bp-core/bp-core-update.php

    r13273 r13304  
    206206    // Install BP schema and activate only Activity and XProfile.
    207207    if ( bp_is_install() ) {
     208        // Set the first BP major version the plugin was installed.
     209        bp_update_option( '_bp_initial_major_version', bp_get_major_version() );
    208210
    209211        // Apply schema and set Activity and XProfile components as active.
     
    762764 */
    763765function bp_update_to_11_0() {
     766    bp_delete_option( '_bp_ignore_deprecated_code' );
    764767
    765768    add_filter( 'bp_email_get_schema', 'bp_core_get_11_0_upgrade_email_schema' );
  • trunk/src/class-buddypress.php

    r13303 r13304  
    464464         */
    465465        $this->email_taxonomy_type = apply_filters( 'bp_email_tax_type', 'bp-email-type' );
     466
     467        /**
     468         * Are PHPUnit tests running?
     469         *
     470         * @since 11.0.0
     471         *
     472         * @param bool $value True if PHPUnit tests are running, false otherwise.
     473         */
     474        $this->is_phpunit_running = function_exists( 'tests_add_filter' );
    466475    }
    467476
     
    484493        if ( ! defined( 'BP_DB_VERSION' ) ) {
    485494            define( 'BP_DB_VERSION', $this->db_version );
    486         }
    487 
    488         // Define if deprecated functions should be ignored.
    489         if ( ! defined( 'BP_IGNORE_DEPRECATED' ) ) {
    490             define( 'BP_IGNORE_DEPRECATED', true );
    491495        }
    492496    }
     
    536540        require $this->plugin_dir . 'bp-core/bp-core-blocks.php';
    537541
    538         // Maybe load deprecated functionality (this double negative is proof positive!).
    539         if ( ! bp_get_option( '_bp_ignore_deprecated_code', ! $this->load_deprecated ) ) {
    540             require $this->plugin_dir . 'bp-core/deprecated/1.2.php';
    541             require $this->plugin_dir . 'bp-core/deprecated/1.5.php';
    542             require $this->plugin_dir . 'bp-core/deprecated/1.6.php';
    543             require $this->plugin_dir . 'bp-core/deprecated/1.7.php';
    544             require $this->plugin_dir . 'bp-core/deprecated/1.9.php';
    545             require $this->plugin_dir . 'bp-core/deprecated/2.0.php';
    546             require $this->plugin_dir . 'bp-core/deprecated/2.1.php';
    547             require $this->plugin_dir . 'bp-core/deprecated/2.2.php';
    548             require $this->plugin_dir . 'bp-core/deprecated/2.3.php';
    549             require $this->plugin_dir . 'bp-core/deprecated/2.4.php';
    550             require $this->plugin_dir . 'bp-core/deprecated/2.5.php';
    551             require $this->plugin_dir . 'bp-core/deprecated/2.6.php';
    552             require $this->plugin_dir . 'bp-core/deprecated/2.7.php';
    553             require $this->plugin_dir . 'bp-core/deprecated/2.8.php';
    554             require $this->plugin_dir . 'bp-core/deprecated/2.9.php';
    555             require $this->plugin_dir . 'bp-core/deprecated/3.0.php';
    556             require $this->plugin_dir . 'bp-core/deprecated/4.0.php';
    557             require $this->plugin_dir . 'bp-core/deprecated/6.0.php';
    558             require $this->plugin_dir . 'bp-core/deprecated/7.0.php';
    559             require $this->plugin_dir . 'bp-core/deprecated/8.0.php';
    560             require $this->plugin_dir . 'bp-core/deprecated/9.0.php';
    561             require $this->plugin_dir . 'bp-core/deprecated/10.0.php';
    562             require $this->plugin_dir . 'bp-core/deprecated/11.0.php';
    563         }
     542        // Get the list of versions needing their deprecated functions to be loaded.
     543        $deprecated_functions_versions = bp_get_deprecated_functions_versions();
     544
     545        // Maybe load deprecated functionality.
     546        if ( $deprecated_functions_versions && ! $this->is_phpunit_running ) {
     547            $this->load_deprecated = true;
     548
     549            foreach ( $deprecated_functions_versions as $deprecated_functions_version ) {
     550                // Load all or last 2 deprecated versions.
     551                require $this->plugin_dir . sprintf( 'bp-core/deprecated/%s.php', $deprecated_functions_version );
     552            }
     553        }
    564554
    565555        // Load wp-cli module if PHP 5.6+.
     
    721711            ! in_array( $component, array( 'core', 'members' ), true ) &&
    722712            ! bp_is_active( $component ) &&
    723             ! function_exists( 'tests_add_filter' )
     713            ! $this->is_phpunit_running
    724714        ) {
    725715            return;
  • trunk/tests/phpunit/testcases/core/functions.php

    r12680 r13304  
    66
    77class BP_Tests_Core_Functions extends BP_UnitTestCase {
     8    protected $bp_initial_version;
     9
    810    /**
    911     * @group bp_esc_sql_order
     
    876878        return array_merge( $page_default_titles, array( 'newcomponent' => 'NewComponent' ) );
    877879    }
     880
     881    public function override_initial_version() {
     882        return $this->bp_initial_version;
     883    }
     884
     885    /**
     886     * @ticket BP8687
     887     */
     888    public function test_bp_get_deprecated_functions_versions() {
     889        $current_version = (float) bp_get_version();
     890        $versions        = bp_get_deprecated_functions_versions();
     891
     892        // When current version is the initial version, we shouldn't load deprecated functions files.
     893        $this->assertTrue( is_array( $versions ) && ! $versions, 'Please check the list of `$deprecated_functions_versions` in `bp_get_deprecated_functions_versions()`. There should be one for each file of the `/src/bp-core/deprecated` directory.' );
     894
     895        // We should load the 2 lasts deprecated functions files.
     896        $this->bp_initial_version = '8.0';
     897
     898        add_filter( 'pre_option__bp_initial_major_version', array( $this, 'override_initial_version' ), 10, 0 );
     899
     900        $versions = bp_get_deprecated_functions_versions();
     901
     902        remove_filter( 'pre_option__bp_initial_major_version', array( $this, 'override_initial_version' ), 10, 0 );
     903
     904        $this->assertTrue( 2 === count( $versions ) );
     905
     906        // Even if this version does not exist in deprecated functions files, we should load the 2 lasts.
     907        $this->bp_initial_version = '1.0';
     908
     909        add_filter( 'pre_option__bp_initial_major_version', array( $this, 'override_initial_version' ), 10, 0 );
     910
     911        $versions = bp_get_deprecated_functions_versions();
     912
     913        remove_filter( 'pre_option__bp_initial_major_version', array( $this, 'override_initial_version' ), 10, 0 );
     914
     915        $this->assertTrue( 2 === count( $versions ) );
     916    }
    878917}
Note: See TracChangeset for help on using the changeset viewer.