Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions woocommerce/class-sv-wc-hook-deprecator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
*/
#[\AllowDynamicProperties]
class SV_WC_Hook_Deprecator {
protected SV_WC_Plugin $plugin;


/** @var string plugin name */
/** @var string plugin name (deprecated) */
protected $plugin_name;

/** @var array deprecated/removed hooks */
Expand All @@ -50,17 +50,27 @@ class SV_WC_Hook_Deprecator {
/**
* Setup class
*
* @param string $plugin_name
* @param string|SV_WC_Plugin $plugin Plugin instance or string name of plugin (latter is deprecated)
* @param array $hooks
*
* @since 5.15.7 The `$plugin_name` parameter has been renamed to `$plugin` and now expects an `SV_WC_Plugin`
* object. This change is to avoid loading translations too early. Support for `$plugin` as
* plugin name remains for back-compat though it will likely result in `_load_textdomain_just_in_time`
* notices being logged for the current extension.
*/
public function __construct( $plugin_name, $hooks ) {
public function __construct($plugin, $hooks)
{
if ($plugin instanceof SV_WC_Plugin) {
$this->plugin = $plugin;
} elseif (is_string($plugin)) {
$this->plugin_name = $plugin;
}

$this->plugin_name = $plugin_name;
$this->hooks = array_map( array( $this, 'set_hook_defaults' ), $hooks );
$this->hooks = array_map([$this, 'set_hook_defaults'], $hooks);

$this->map_deprecated_hooks();

add_action( 'shutdown', array( $this, 'trigger_deprecated_errors' ), 999 );
add_action('shutdown', [$this, 'trigger_deprecated_errors'], 999);
}


Expand Down Expand Up @@ -180,7 +190,7 @@ protected function trigger_error( $old_hook_name, $hook ) {

// e.g. WooCommerce Memberships: "wc_memberships_some_hook" was deprecated in version 1.2.3.
$message = sprintf( '%1$s: action/filter "%2$s" was %3$s in version %4$s. ',
$this->plugin_name,
$this->getPluginName(),
$old_hook_name,
$hook['removed'] ? 'removed' : 'deprecated',
$hook['version']
Expand All @@ -194,6 +204,22 @@ protected function trigger_error( $old_hook_name, $hook ) {
}


/**
* Gets the plugin name.
*
* @return string
*/
protected function getPluginName() : string
{
if (isset($this->plugin)) {
return $this->plugin->get_plugin_name();
} elseif(isset($this->plugin_name)) {
return $this->plugin_name;
} else {
return 'Plugin';
}
}

}


Expand Down
3 changes: 2 additions & 1 deletion woocommerce/class-sv-wc-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,11 @@ protected function init_admin_notice_handler() {
* Plugins can override this with their own handler.
*
* @since 5.2.0
* @since 5.15.7 The full `SV_WC_Plugin` instance is now used to instantiate `SV_WC_Hook_Deprecator`.
*/
protected function init_hook_deprecator() {

$this->hook_deprecator = new SV_WC_Hook_Deprecator( $this->get_plugin_name(), array_merge( $this->get_framework_deprecated_hooks(), $this->get_deprecated_hooks() ) );
$this->hook_deprecator = new SV_WC_Hook_Deprecator( $this, array_merge( $this->get_framework_deprecated_hooks(), $this->get_deprecated_hooks() ) );
}


Expand Down
Loading