Make WordPress Core


Ignore:
Timestamp:
08/25/2023 03:24:00 AM (14 months ago)
Author:
DrewAPicture
Message:

Introduce a _deprecated_class() function.

Similar to other function in the _deprecated_* series, _deprecated_class() comes with two new hooks: deprecated_class_run and deprecated_class_trigger_error.

Support has also been added for setting class deprecation expectations in tests.

Props jrf, wvega, ohryan.
See #41125.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r56414 r56467  
    55975597
    55985598/**
     5599 * Marks a class as deprecated and informs when it has been used.
     5600 *
     5601 * There is a {@see 'deprecated_class_run'} hook that will be called that can be used
     5602 * to get the backtrace up to what file and function called the
     5603 * deprecated class.
     5604 *
     5605 * The current behavior is to trigger a user error if `WP_DEBUG` is true.
     5606 *
     5607 * This function is to be used in the class constructor for every deprecated class.
     5608 * See {@see _deprecated_constructor()} for deprecating PHP4 style constructors.
     5609 *
     5610 * @since 6.4.0
     5611 *
     5612 * @param string $class       The class being instantiated
     5613 * @param string $version     The version of WordPress that deprecated the class.
     5614 * @param string $replacement Optional. The class or function that should have been called.
     5615 *                            Default empty string.
     5616 */
     5617function _deprecated_class( $class, $version, $replacement = '' ) {
     5618
     5619    /**
     5620     * Fires when a deprecated class is called.
     5621     *
     5622     * @since 6.4.0
     5623     *
     5624     * @param string $class       The class being instantiated
     5625     * @param string $replacement The class or function that should have been called.
     5626     * @param string $version     The version of WordPress that deprecated the class.
     5627     */
     5628    do_action( 'deprecated_class_run', $class, $replacement, $version );
     5629
     5630    /**
     5631     * Filters whether to trigger an error for a deprecated class.
     5632     *
     5633     * @since 6.4.0
     5634     *
     5635     * @param bool $trigger Whether to trigger an error for a deprecated class. Default true.
     5636     */
     5637    if ( WP_DEBUG && apply_filters( 'deprecated_class_trigger_error', true ) ) {
     5638        if ( function_exists( '__' ) ) {
     5639            if ( ! is_null( $replacement ) ) {
     5640                /* translators: 1: PHP class name, 2: version number, 3: alternative clas or function name */
     5641                trigger_error( sprintf( __('Class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.'), $class, $version, $replacement ), E_USER_DEPRECATED );
     5642            } else {
     5643                /* translators: 1: PHP class name, 2: version number */
     5644                trigger_error( sprintf( __('Class %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.'), $class, $version ), E_USER_DEPRECATED );
     5645            }
     5646        } else {
     5647            if ( ! is_null( $replacement ) ) {
     5648                trigger_error( sprintf( 'Class %1$s is <strong>deprecated</strong> since version %2$s! Use %3$s instead.', $class, $version, $replacement ), E_USER_DEPRECATED );
     5649            } else {
     5650                trigger_error( sprintf( 'Class %1$s is <strong>deprecated</strong> since version %2$s with no alternative available.', $class, $version ), E_USER_DEPRECATED );
     5651            }
     5652        }
     5653    }
     5654}
     5655
     5656/**
    55995657 * Marks a file as deprecated and inform when it has been used.
    56005658 *
Note: See TracChangeset for help on using the changeset viewer.