1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
<?php /** * A facade to allow deprecating an entire class. Calling instance or static * functions on the facade triggers a deprecation notice before calling the * underlying function. * * Use it by extending DeprecatedClassFacade in your facade class, setting the * static $facade_over_classname string to the name of the class to build * a facade over, and setting the static $deprecated_in_version to the version * that the class was deprecated in. Eg.: * * class DeprecatedGoose extends DeprecatedClassFacade { * static $facade_over_classname = 'Goose'; * static $deprecated_in_version = '1.7.0'; * } */
namespace Automattic\WooCommerce\Admin;
defined( 'ABSPATH' ) || exit;
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
/** * A facade to allow deprecating an entire class. */ class DeprecatedClassFacade {
/** * The instance that this facade covers over. * * @var object */ protected $instance;
/** * The name of the non-deprecated class that this facade covers. * * @var string */ protected static $facade_over_classname = '';
/** * The version that this class was deprecated in. * * @var string */ protected static $deprecated_in_version = '';
/** * Static array of logged messages. * * @var array */ private static $logged_messages = array();
/** * Constructor. */ public function __construct() { if ( '' !== static::$facade_over_classname ) { $this->instance = new static::$facade_over_classname(); } }
/** * Log a deprecation to the error log. * * @param string $function The name of the deprecated function being called. */ private static function log_deprecation( $function ) { $message = sprintf( '%1$s is deprecated since version %2$s! Use %3$s instead.', static::class . '::' . $function, static::$deprecated_in_version, static::$facade_over_classname . '::' . $function );
if ( '' !== static::$facade_over_classname ) { $message = $message . sprintf( ' Use %s instead.', static::$facade_over_classname . '::' . $function ); }
// Only log when the message has not been logged before. if ( ! in_array( $message, self::$logged_messages, true ) ) { error_log( $message ); // phpcs:ignore self::$logged_messages[] = $message; } }
/** * Executes when calling any function on an instance of this class. * * @param string $name The name of the function being called. * @param array $arguments An array of the arguments to the function call. */ public function __call( $name, $arguments ) { self::log_deprecation( $name );
if ( ! isset( $this->instance ) ) { return; }
return call_user_func_array( array( $this->instance, $name, ), $arguments ); }
/** * Executes when calling any static function on this class. * * @param string $name The name of the function being called. * @param array $arguments An array of the arguments to the function call. */ public static function __callStatic( $name, $arguments ) { self::log_deprecation( $name );
if ( '' === static::$facade_over_classname ) { return; }
return call_user_func_array( array( static::$facade_over_classname, $name, ), $arguments ); } }
|