API
3 years ago
Composer
4 years ago
DateTimeProvider
4 years ago
Features
3 years ago
Marketing
3 years ago
Notes
3 years ago
Overrides
4 years ago
PluginsProvider
4 years ago
RemoteInboxNotifications
3 years ago
Schedulers
4 years ago
DataSourcePoller.php
3 years ago
DeprecatedClassFacade.php
4 years ago
FeaturePlugin.php
4 years ago
Loader.php
4 years ago
PageController.php
4 years ago
PluginsHelper.php
4 years ago
PluginsInstaller.php
4 years ago
ReportCSVEmail.php
4 years ago
ReportCSVExporter.php
4 years ago
ReportExporter.php
4 years ago
ReportsSync.php
4 years ago
WCAdminHelper.php
4 years ago
DeprecatedClassFacade.php
94 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A facade to allow deprecating an entire class. Calling instance or static |
| 4 | * functions on the facade triggers a deprecation notice before calling the |
| 5 | * underlying function. |
| 6 | * |
| 7 | * Use it by extending DeprecatedClassFacade in your facade class, setting the |
| 8 | * static $facade_over_classname string to the name of the class to build |
| 9 | * a facade over, and setting the static $deprecated_in_version to the version |
| 10 | * that the class was deprecated in. Eg.: |
| 11 | * |
| 12 | * class DeprecatedGoose extends DeprecatedClassFacade { |
| 13 | * static $facade_over_classname = 'Goose'; |
| 14 | * static $deprecated_in_version = '1.7.0'; |
| 15 | * } |
| 16 | */ |
| 17 | |
| 18 | namespace Automattic\WooCommerce\Admin; |
| 19 | |
| 20 | defined( 'ABSPATH' ) || exit; |
| 21 | |
| 22 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 23 | |
| 24 | /** |
| 25 | * A facade to allow deprecating an entire class. |
| 26 | */ |
| 27 | class DeprecatedClassFacade { |
| 28 | /** |
| 29 | * The instance that this facade covers over. |
| 30 | * |
| 31 | * @var object |
| 32 | */ |
| 33 | protected $instance; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | $this->instance = new static::$facade_over_classname(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Log a deprecation to the error log. |
| 44 | * |
| 45 | * @param string $function The name of the deprecated function being called. |
| 46 | */ |
| 47 | private static function log_deprecation( $function ) { |
| 48 | error_log( // phpcs:ignore |
| 49 | sprintf( |
| 50 | '%1$s is deprecated since version %2$s! Use %3$s instead.', |
| 51 | static::class . '::' . $function, |
| 52 | static::$deprecated_in_version, |
| 53 | static::$facade_over_classname . '::' . $function |
| 54 | ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Executes when calling any function on an instance of this class. |
| 60 | * |
| 61 | * @param string $name The name of the function being called. |
| 62 | * @param array $arguments An array of the arguments to the function call. |
| 63 | */ |
| 64 | public function __call( $name, $arguments ) { |
| 65 | self::log_deprecation( $name ); |
| 66 | |
| 67 | return call_user_func_array( |
| 68 | array( |
| 69 | $this->instance, |
| 70 | $name, |
| 71 | ), |
| 72 | $arguments |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Executes when calling any static function on this class. |
| 78 | * |
| 79 | * @param string $name The name of the function being called. |
| 80 | * @param array $arguments An array of the arguments to the function call. |
| 81 | */ |
| 82 | public static function __callStatic( $name, $arguments ) { |
| 83 | self::log_deprecation( $name ); |
| 84 | |
| 85 | return call_user_func_array( |
| 86 | array( |
| 87 | static::$facade_over_classname, |
| 88 | $name, |
| 89 | ), |
| 90 | $arguments |
| 91 | ); |
| 92 | } |
| 93 | } |
| 94 |