API
4 weeks ago
BlockTemplates
2 years ago
Composer
2 years ago
DateTimeProvider
4 years ago
Features
4 weeks ago
Marketing
1 year ago
Notes
4 weeks ago
Overrides
2 months ago
PluginsInstallLoggers
1 year ago
PluginsProvider
4 years ago
RemoteInboxNotifications
4 weeks ago
RemoteSpecs
2 months ago
Schedulers
1 year ago
Settings
2 weeks ago
DataSourcePoller.php
2 years ago
DeprecatedClassFacade.php
1 year ago
FeaturePlugin.php
4 years ago
Loader.php
2 years ago
PageController.php
4 weeks ago
PluginsHelper.php
4 weeks ago
PluginsInstaller.php
4 years ago
ReportCSVEmail.php
1 year ago
ReportCSVExporter.php
1 year ago
ReportExporter.php
3 years ago
ReportsSync.php
4 months ago
WCAdminHelper.php
4 weeks ago
DeprecatedClassFacade.php
137 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 | /** |
| 30 | * The instance that this facade covers over. |
| 31 | * |
| 32 | * @var object |
| 33 | */ |
| 34 | protected $instance; |
| 35 | |
| 36 | /** |
| 37 | * The name of the non-deprecated class that this facade covers. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | protected static $facade_over_classname = ''; |
| 42 | |
| 43 | /** |
| 44 | * The version that this class was deprecated in. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected static $deprecated_in_version = ''; |
| 49 | |
| 50 | /** |
| 51 | * Static array of logged messages. |
| 52 | * |
| 53 | * @var array |
| 54 | */ |
| 55 | private static $logged_messages = array(); |
| 56 | |
| 57 | /** |
| 58 | * Constructor. |
| 59 | */ |
| 60 | public function __construct() { |
| 61 | if ( '' !== static::$facade_over_classname ) { |
| 62 | $this->instance = new static::$facade_over_classname(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Log a deprecation to the error log. |
| 68 | * |
| 69 | * @param string $function The name of the deprecated function being called. |
| 70 | */ |
| 71 | private static function log_deprecation( $function ) { |
| 72 | $message = sprintf( |
| 73 | '%1$s is deprecated since version %2$s! Use %3$s instead.', |
| 74 | static::class . '::' . $function, |
| 75 | static::$deprecated_in_version, |
| 76 | static::$facade_over_classname . '::' . $function |
| 77 | ); |
| 78 | |
| 79 | if ( '' !== static::$facade_over_classname ) { |
| 80 | $message = $message . sprintf( |
| 81 | ' Use %s instead.', |
| 82 | static::$facade_over_classname . '::' . $function |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | // Only log when the message has not been logged before. |
| 87 | if ( ! in_array( $message, self::$logged_messages, true ) ) { |
| 88 | error_log( $message ); // phpcs:ignore |
| 89 | self::$logged_messages[] = $message; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Executes when calling any function on an instance of this class. |
| 95 | * |
| 96 | * @param string $name The name of the function being called. |
| 97 | * @param array $arguments An array of the arguments to the function call. |
| 98 | */ |
| 99 | public function __call( $name, $arguments ) { |
| 100 | self::log_deprecation( $name ); |
| 101 | |
| 102 | if ( ! isset( $this->instance ) ) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | return call_user_func_array( |
| 107 | array( |
| 108 | $this->instance, |
| 109 | $name, |
| 110 | ), |
| 111 | $arguments |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Executes when calling any static function on this class. |
| 117 | * |
| 118 | * @param string $name The name of the function being called. |
| 119 | * @param array $arguments An array of the arguments to the function call. |
| 120 | */ |
| 121 | public static function __callStatic( $name, $arguments ) { |
| 122 | self::log_deprecation( $name ); |
| 123 | |
| 124 | if ( '' === static::$facade_over_classname ) { |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | return call_user_func_array( |
| 129 | array( |
| 130 | static::$facade_over_classname, |
| 131 | $name, |
| 132 | ), |
| 133 | $arguments |
| 134 | ); |
| 135 | } |
| 136 | } |
| 137 |