2017-02-14 18:56:09 +00:00
|
|
|
<?php
|
2017-11-22 16:55:12 +00:00
|
|
|
/**
|
|
|
|
* Abstract deprecated hooks
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Abstracts
|
|
|
|
* @since 3.0.0
|
|
|
|
* @version 3.3.0
|
|
|
|
*/
|
|
|
|
|
2017-02-14 18:56:09 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Deprecated_Hooks class maps old actions and filters to new ones. This is the base class for handling those deprecated hooks.
|
|
|
|
*
|
|
|
|
* Based on the WCS_Hook_Deprecator class by Prospress.
|
|
|
|
*/
|
|
|
|
abstract class WC_Deprecated_Hooks {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of deprecated hooks we need to handle.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $deprecated_hooks = array();
|
|
|
|
|
2017-11-22 17:21:32 +00:00
|
|
|
/**
|
|
|
|
* Array of versions on each hook has been deprecated.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $deprecated_version = array();
|
|
|
|
|
2017-02-14 18:56:09 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2017-02-14 19:23:57 +00:00
|
|
|
$new_hooks = array_keys( $this->deprecated_hooks );
|
|
|
|
array_walk( $new_hooks, array( $this, 'hook_in' ) );
|
2017-02-14 18:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook into the new hook so we can handle deprecated hooks once fired.
|
|
|
|
*
|
2017-11-22 16:55:12 +00:00
|
|
|
* @param string $hook_name Hook name.
|
2017-02-14 18:56:09 +00:00
|
|
|
*/
|
2017-11-22 16:55:12 +00:00
|
|
|
abstract public function hook_in( $hook_name );
|
2017-02-14 18:56:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get old hooks to map to new hook.
|
|
|
|
*
|
2017-11-22 17:21:32 +00:00
|
|
|
* @param string $new_hook New hook name.
|
2017-02-14 18:56:09 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_old_hooks( $new_hook ) {
|
|
|
|
$old_hooks = isset( $this->deprecated_hooks[ $new_hook ] ) ? $this->deprecated_hooks[ $new_hook ] : array();
|
|
|
|
$old_hooks = is_array( $old_hooks ) ? $old_hooks : array( $old_hooks );
|
|
|
|
|
|
|
|
return $old_hooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the hook is Deprecated, call the old hooks here.
|
|
|
|
*/
|
|
|
|
public function maybe_handle_deprecated_hook() {
|
|
|
|
$new_hook = current_filter();
|
|
|
|
$old_hooks = $this->get_old_hooks( $new_hook );
|
|
|
|
$new_callback_args = func_get_args();
|
|
|
|
$return_value = $new_callback_args[0];
|
|
|
|
|
|
|
|
foreach ( $old_hooks as $old_hook ) {
|
|
|
|
$return_value = $this->handle_deprecated_hook( $new_hook, $old_hook, $new_callback_args, $return_value );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the old hook is in-use, trigger it.
|
|
|
|
*
|
2017-11-22 17:21:32 +00:00
|
|
|
* @param string $new_hook New hook name.
|
|
|
|
* @param string $old_hook Old hook name.
|
2017-11-22 16:55:12 +00:00
|
|
|
* @param array $new_callback_args New callback args.
|
|
|
|
* @param mixed $return_value Returned value.
|
2017-02-14 18:56:09 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-11-22 16:55:12 +00:00
|
|
|
abstract public function handle_deprecated_hook( $new_hook, $old_hook, $new_callback_args, $return_value );
|
2017-02-14 18:56:09 +00:00
|
|
|
|
2017-11-22 17:21:32 +00:00
|
|
|
/**
|
|
|
|
* Get deprecated version.
|
|
|
|
*
|
|
|
|
* @param string $old_hook Old hook name.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_deprecated_version( $old_hook ) {
|
|
|
|
return ! empty( $this->deprecated_version[ $old_hook ] ) ? $this->deprecated_version[ $old_hook ] : WC_VERSION;
|
|
|
|
}
|
|
|
|
|
2017-02-14 18:56:09 +00:00
|
|
|
/**
|
|
|
|
* Display a deprecated notice for old hooks.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2017-11-22 16:55:12 +00:00
|
|
|
* @param string $old_hook Old hook.
|
|
|
|
* @param string $new_hook New hook.
|
2017-02-14 18:56:09 +00:00
|
|
|
*/
|
2017-02-14 19:31:14 +00:00
|
|
|
protected function display_notice( $old_hook, $new_hook ) {
|
2017-11-22 17:21:32 +00:00
|
|
|
wc_deprecated_hook( esc_html( $old_hook ), esc_html( $this->get_deprecated_version( $old_hook ) ), esc_html( $new_hook ) );
|
2017-02-14 18:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fire off a legacy hook with it's args.
|
|
|
|
*
|
2017-11-22 17:21:32 +00:00
|
|
|
* @param string $old_hook Old hook name.
|
2017-11-22 16:55:12 +00:00
|
|
|
* @param array $new_callback_args New callback args.
|
2017-02-14 18:56:09 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2017-02-14 19:23:57 +00:00
|
|
|
abstract protected function trigger_hook( $old_hook, $new_callback_args );
|
2017-02-14 18:56:09 +00:00
|
|
|
}
|