AccessiblePrivateMethods.php
5 months ago
OrderAttributionMeta.php
1 year ago
RestApiCache.php
4 months ago
ScriptDebug.php
2 years ago
AccessiblePrivateMethods.php
92 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\Traits; |
| 6 | |
| 7 | /** |
| 8 | * DON'T USE THIS TRAIT. It's DEPRECATED and will be REMOVED in a future version of WooCommerce. |
| 9 | * |
| 10 | * If you have class methods that are public solely because they are the target of WordPress hooks, |
| 11 | * make the methods public and mark them with an @internal annotation. |
| 12 | * |
| 13 | * @deprecated 9.6.0 Make the hook target methods public and mark them with an @internal annotation. This trait will be REMOVED in a future version of WooCommerce. |
| 14 | */ |
| 15 | trait AccessiblePrivateMethods { |
| 16 | // phpcs:disable |
| 17 | |
| 18 | private $_accessible_private_methods = array(); |
| 19 | |
| 20 | private static $_accessible_static_private_methods = array(); |
| 21 | |
| 22 | protected static function add_action( string $hook_name, $callback, int $priority = 10, int $accepted_args = 1 ): void { |
| 23 | self::process_callback_before_hooking( $callback ); |
| 24 | add_action( $hook_name, $callback, $priority, $accepted_args ); |
| 25 | } |
| 26 | |
| 27 | protected static function add_filter( string $hook_name, $callback, int $priority = 10, int $accepted_args = 1 ): void { |
| 28 | self::process_callback_before_hooking( $callback ); |
| 29 | add_filter( $hook_name, $callback, $priority, $accepted_args ); |
| 30 | } |
| 31 | |
| 32 | protected static function process_callback_before_hooking( $callback ): void { |
| 33 | if ( ! is_array( $callback ) || count( $callback ) < 2 ) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | $first_item = $callback[0]; |
| 38 | if ( __CLASS__ === $first_item ) { |
| 39 | static::mark_static_method_as_accessible( $callback[1] ); |
| 40 | } elseif ( is_object( $first_item ) && get_class( $first_item ) === __CLASS__ ) { |
| 41 | $first_item->mark_method_as_accessible( $callback[1] ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | protected function mark_method_as_accessible( string $method_name ): bool { |
| 46 | if ( method_exists( $this, $method_name ) ) { |
| 47 | $this->_accessible_private_methods[ $method_name ] = $method_name; |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | protected static function mark_static_method_as_accessible( string $method_name ): bool { |
| 55 | if ( method_exists( __CLASS__, $method_name ) ) { |
| 56 | static::$_accessible_static_private_methods[ $method_name ] = $method_name; |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | public function __call( $name, $arguments ) { |
| 64 | if ( isset( $this->_accessible_private_methods[ $name ] ) ) { |
| 65 | return call_user_func_array( array( $this, $name ), $arguments ); |
| 66 | } elseif ( is_callable( array( 'parent', '__call' ) ) ) { |
| 67 | return parent::__call( $name, $arguments ); |
| 68 | } elseif ( method_exists( $this, $name ) ) { |
| 69 | throw new \Error( 'Call to private method ' . get_class( $this ) . '::' . $name ); |
| 70 | } else { |
| 71 | throw new \Error( 'Call to undefined method ' . get_class( $this ) . '::' . $name ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public static function __callStatic( $name, $arguments ) { |
| 76 | if ( isset( static::$_accessible_static_private_methods[ $name ] ) ) { |
| 77 | return call_user_func_array( array( __CLASS__, $name ), $arguments ); |
| 78 | } elseif ( is_callable( array( 'parent', '__callStatic' ) ) ) { |
| 79 | return parent::__callStatic( $name, $arguments ); |
| 80 | } elseif ( 'add_action' === $name || 'add_filter' === $name ) { |
| 81 | $proper_method_name = 'add_static_' . substr( $name, 4 ); |
| 82 | throw new \Error( __CLASS__ . '::' . $name . " can't be called statically, did you mean '$proper_method_name'?" ); |
| 83 | } elseif ( method_exists( __CLASS__, $name ) ) { |
| 84 | throw new \Error( 'Call to private method ' . __CLASS__ . '::' . $name ); |
| 85 | } else { |
| 86 | throw new \Error( 'Call to undefined method ' . __CLASS__ . '::' . $name ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // phpcs:enable |
| 91 | } |
| 92 |