api
2 years ago
backups
2 years ago
external
2 years ago
integrations
2 years ago
media
2 years ago
media-library
2 years ago
modules
2 years ago
photon
2 years ago
png2jpg
2 years ago
resize
2 years ago
s3
2 years ago
smush
2 years ago
stats
2 years ago
webp
2 years ago
class-animated-status-controller.php
2 years ago
class-array-utils.php
2 years ago
class-attachment-id-list.php
2 years ago
class-backup-size.php
2 years ago
class-cli.php
2 years ago
class-configs.php
2 years ago
class-controller.php
2 years ago
class-core.php
2 years ago
class-deprecated-hooks.php
2 years ago
class-error-handler.php
2 years ago
class-file-system.php
2 years ago
class-helper.php
2 years ago
class-installer.php
2 years ago
class-modules.php
2 years ago
class-optimization-controller.php
2 years ago
class-plugin-settings-watcher.php
2 years ago
class-rest.php
2 years ago
class-server-utils.php
2 years ago
class-settings.php
2 years ago
class-smush-file.php
2 years ago
class-stats.php
2 years ago
class-upload-dir.php
2 years ago
class-deprecated-hooks.php
148 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Deprecated hooks. |
| 4 | * It's created based on WC_Deprecated_Hooks. |
| 5 | * |
| 6 | * @since 3.9.6 |
| 7 | * @package Deprecated_Hooks |
| 8 | */ |
| 9 | |
| 10 | namespace Smush\Core; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | /** |
| 15 | * Handles deprecation notices and triggering of legacy action hooks. |
| 16 | */ |
| 17 | class Deprecated_Hooks { |
| 18 | |
| 19 | /** |
| 20 | * Array of deprecated actions hooks we need to handle. Format of 'new' => 'old'. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | private $deprecated_action_hooks = array( |
| 25 | 'wp_smush_before_smush_file' => 'smush_s3_integration_fetch_file', |
| 26 | 'wp_smush_after_remove_file' => 'smush_s3_backup_remove', |
| 27 | ); |
| 28 | |
| 29 | /** |
| 30 | * Array of deprecated filters hooks we need to handle. Format of 'new' => 'old'. |
| 31 | * |
| 32 | * @var array |
| 33 | */ |
| 34 | private $deprecated_filter_hooks = array( |
| 35 | 'wp_smush_backup_exists' => 'smush_backup_exists', |
| 36 | 'wp_smush_file_exists' => 'smush_file_exists', |
| 37 | ); |
| 38 | |
| 39 | /** |
| 40 | * Array of versions on each hook has been deprecated. |
| 41 | * |
| 42 | * @var array |
| 43 | */ |
| 44 | private $deprecated_version = array( |
| 45 | 'smush_backup_exists' => '3.9.6', |
| 46 | 'smush_s3_integration_fetch_file' => '3.9.6', |
| 47 | 'smush_s3_backup_remove' => '3.9.6', |
| 48 | 'smush_file_exists' => '3.9.6', |
| 49 | ); |
| 50 | |
| 51 | /** |
| 52 | * Is action hook. |
| 53 | * |
| 54 | * @var bool |
| 55 | */ |
| 56 | private $is_action; |
| 57 | |
| 58 | /** |
| 59 | * Constructor. |
| 60 | * |
| 61 | * Hook into the new hook so we can handle deprecated hooks once fired. |
| 62 | */ |
| 63 | public function __construct() { |
| 64 | $deprecated_hooks = array_merge( array_keys( $this->deprecated_action_hooks ), array_keys( $this->deprecated_filter_hooks ) ); |
| 65 | if ( $deprecated_hooks ) { |
| 66 | foreach ( $deprecated_hooks as $new_action ) { |
| 67 | add_filter( $new_action, array( $this, 'maybe_handle_deprecated_hook' ), -1000, 8 ); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get old hooks to map to new hook. |
| 74 | * |
| 75 | * @param string $new_hook New hook name. |
| 76 | * @return array |
| 77 | */ |
| 78 | private function get_old_hooks( $new_hook ) { |
| 79 | $old_hooks = array(); |
| 80 | if ( isset( $this->deprecated_action_hooks[ $new_hook ] ) ) { |
| 81 | $old_hooks = $this->deprecated_action_hooks[ $new_hook ]; |
| 82 | |
| 83 | $this->is_action = true; |
| 84 | } elseif ( isset( $this->deprecated_filter_hooks[ $new_hook ] ) ) { |
| 85 | $old_hooks = $this->deprecated_filter_hooks[ $new_hook ]; |
| 86 | // reset hook type. |
| 87 | $this->is_action = null; |
| 88 | } |
| 89 | |
| 90 | return is_array( $old_hooks ) ? $old_hooks : array( $old_hooks ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * If the hook is Deprecated, call the old hooks here. |
| 95 | */ |
| 96 | public function maybe_handle_deprecated_hook() { |
| 97 | $new_hook = current_filter(); |
| 98 | $new_callback_args = func_get_args(); |
| 99 | $return_value = $new_callback_args[0]; |
| 100 | $old_hooks = $this->get_old_hooks( $new_hook ); |
| 101 | if ( $old_hooks ) { |
| 102 | foreach ( $old_hooks as $old_hook ) { |
| 103 | if ( has_filter( $old_hook ) ) { |
| 104 | $this->display_notice( $old_hook, $new_hook ); |
| 105 | $return_value = $this->trigger_hook( $old_hook, $new_callback_args ); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $return_value; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Display a deprecated notice for old hooks. |
| 115 | * |
| 116 | * @param string $old_hook Old hook. |
| 117 | * @param string $new_hook New hook. |
| 118 | */ |
| 119 | protected function display_notice( $old_hook, $new_hook ) { |
| 120 | _deprecated_hook( esc_html( $old_hook ), esc_html( $this->get_deprecated_version( $old_hook ) ), esc_html( $new_hook ) ); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Fire off a legacy hook with it's args. |
| 125 | * |
| 126 | * @param string $old_hook Old hook name. |
| 127 | * @param array $new_callback_args New callback args. |
| 128 | * @return mixed|void |
| 129 | */ |
| 130 | protected function trigger_hook( $old_hook, $new_callback_args ) { |
| 131 | if ( $this->is_action ) { |
| 132 | do_action_ref_array( $old_hook, $new_callback_args ); |
| 133 | } else { |
| 134 | return apply_filters_ref_array( $old_hook, $new_callback_args ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Get deprecated version. |
| 140 | * |
| 141 | * @param string $old_hook Old hook name. |
| 142 | * @return string |
| 143 | */ |
| 144 | protected function get_deprecated_version( $old_hook ) { |
| 145 | return ! empty( $this->deprecated_version[ $old_hook ] ) ? $this->deprecated_version[ $old_hook ] : WP_SMUSH_VERSION; |
| 146 | } |
| 147 | } |
| 148 |