diff --git a/woocommerce/class-sv-wc-hook-deprecator.php b/woocommerce/class-sv-wc-hook-deprecator.php index 680ed8ff0..dbbeb2723 100644 --- a/woocommerce/class-sv-wc-hook-deprecator.php +++ b/woocommerce/class-sv-wc-hook-deprecator.php @@ -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 */ @@ -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); } @@ -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'] @@ -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'; + } + } + } diff --git a/woocommerce/class-sv-wc-plugin.php b/woocommerce/class-sv-wc-plugin.php index a160aa29c..9f98974c5 100644 --- a/woocommerce/class-sv-wc-plugin.php +++ b/woocommerce/class-sv-wc-plugin.php @@ -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() ) ); }