From 6812712f16b4e1476b77fcd5130d510eabd23163 Mon Sep 17 00:00:00 2001 From: JuanMa Date: Mon, 22 Dec 2025 17:32:33 +0100 Subject: [PATCH] Refactor experiment registration to initialization Updated method names from `register()` to `init()` across various classes and interfaces to better reflect their purpose. This change enhances clarity in the codebase regarding the initialization of experiments and their hooks. --- docs/DEVELOPER_GUIDE.md | 6 ++--- includes/Abstracts/Abstract_Experiment.php | 4 +-- includes/Contracts/Experiment.php | 4 +-- includes/Experiment_Loader.php | 6 ++--- .../Example_Experiment/Example_Experiment.php | 2 +- .../Excerpt_Generation/Excerpt_Generation.php | 2 +- .../Image_Generation/Image_Generation.php | 2 +- .../Title_Generation/Title_Generation.php | 2 +- .../Abilities/Excerpt_GenerationTest.php | 4 +-- .../Abilities/Image_GenerationTest.php | 4 +-- .../Includes/Abilities/Image_ImportTest.php | 4 +-- .../Abilities/Title_GenerationTest.php | 4 +-- .../Abstracts/Abstract_AbilityTest.php | 4 +-- .../Includes/Experiment_LoaderTest.php | 26 +++++++++---------- .../Includes/Experiment_RegistryTest.php | 4 +-- 15 files changed, 39 insertions(+), 39 deletions(-) diff --git a/docs/DEVELOPER_GUIDE.md b/docs/DEVELOPER_GUIDE.md index 09bc752b..41c29699 100644 --- a/docs/DEVELOPER_GUIDE.md +++ b/docs/DEVELOPER_GUIDE.md @@ -144,12 +144,12 @@ class My_Experiment extends Abstract_Experiment { } /** - * Registers the experiment's hooks and functionality. + * Initializes the experiment's hooks and functionality. * * @since 0.1.0 */ - public function register(): void { - // Register your hooks here + public function init(): void { + // Initialize your hooks here add_action( 'init', array( $this, 'initialize' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_filter( 'the_content', array( $this, 'filter_content' ) ); diff --git a/includes/Abstracts/Abstract_Experiment.php b/includes/Abstracts/Abstract_Experiment.php index 8bf0b5e3..ec6420de 100644 --- a/includes/Abstracts/Abstract_Experiment.php +++ b/includes/Abstracts/Abstract_Experiment.php @@ -223,11 +223,11 @@ final protected function get_field_option_name( string $option_name ): string { } /** - * Registers the experiment. + * Initializes the experiment. * * Must be implemented by child classes to set up hooks and functionality. * * @since 0.1.0 */ - abstract public function register(): void; + abstract public function init(): void; } diff --git a/includes/Contracts/Experiment.php b/includes/Contracts/Experiment.php index b1ecb691..d90d1eb3 100644 --- a/includes/Contracts/Experiment.php +++ b/includes/Contracts/Experiment.php @@ -51,14 +51,14 @@ public function get_label(): string; public function get_description(): string; /** - * Registers the experiment's hooks and functionality. + * Initializes the experiment's hooks and functionality. * * This method is called when the experiment is initialized. * Use this to add actions, filters, and set up the experiment. * * @since 0.1.0 */ - public function register(): void; + public function init(): void; /** * Checks if the experiment is currently enabled. diff --git a/includes/Experiment_Loader.php b/includes/Experiment_Loader.php index c7223b27..e4d78ef6 100644 --- a/includes/Experiment_Loader.php +++ b/includes/Experiment_Loader.php @@ -173,7 +173,7 @@ private function get_default_experiments(): array { /** * Initializes all enabled experiments. * - * Loops through all registered experiments and calls their register() method + * Loops through all registered experiments and calls their init() method * if they are enabled. * * @since 0.1.0 @@ -203,8 +203,8 @@ public function initialize_experiments(): void { continue; } - // Register the experiment. - $experiment->register(); + // Initialize the experiment. + $experiment->init(); } /** diff --git a/includes/Experiments/Example_Experiment/Example_Experiment.php b/includes/Experiments/Example_Experiment/Example_Experiment.php index 578b22fc..2be77e0e 100644 --- a/includes/Experiments/Example_Experiment/Example_Experiment.php +++ b/includes/Experiments/Example_Experiment/Example_Experiment.php @@ -35,7 +35,7 @@ protected function load_experiment_metadata(): array { * * @since 0.1.0 */ - public function register(): void { + public function init(): void { add_action( 'wp_footer', array( $this, 'add_footer_content' ), 20 ); add_filter( 'document_title_parts', array( $this, 'modify_title' ), 10, 1 ); add_action( 'rest_api_init', array( $this, 'register_rest_route' ) ); diff --git a/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php b/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php index e0d7f62a..fb681c0d 100644 --- a/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php +++ b/includes/Experiments/Excerpt_Generation/Excerpt_Generation.php @@ -37,7 +37,7 @@ protected function load_experiment_metadata(): array { * * @since x.x.x */ - public function register(): void { + public function init(): void { add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); } diff --git a/includes/Experiments/Image_Generation/Image_Generation.php b/includes/Experiments/Image_Generation/Image_Generation.php index 7a073b8a..27811d26 100644 --- a/includes/Experiments/Image_Generation/Image_Generation.php +++ b/includes/Experiments/Image_Generation/Image_Generation.php @@ -40,7 +40,7 @@ protected function load_experiment_metadata(): array { * * @since x.x.x */ - public function register(): void { + public function init(): void { add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); } diff --git a/includes/Experiments/Title_Generation/Title_Generation.php b/includes/Experiments/Title_Generation/Title_Generation.php index 3a7b63d9..46e4d928 100644 --- a/includes/Experiments/Title_Generation/Title_Generation.php +++ b/includes/Experiments/Title_Generation/Title_Generation.php @@ -40,7 +40,7 @@ protected function load_experiment_metadata(): array { * * @since 0.1.0 */ - public function register(): void { + public function init(): void { add_action( 'wp_abilities_api_init', array( $this, 'register_abilities' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); } diff --git a/tests/Integration/Includes/Abilities/Excerpt_GenerationTest.php b/tests/Integration/Includes/Abilities/Excerpt_GenerationTest.php index 512aed76..ff7b3494 100644 --- a/tests/Integration/Includes/Abilities/Excerpt_GenerationTest.php +++ b/tests/Integration/Includes/Abilities/Excerpt_GenerationTest.php @@ -34,11 +34,11 @@ protected function load_experiment_metadata(): array { } /** - * Registers the experiment. + * Initializes the experiment. * * @since 0.1.0 */ - public function register(): void { + public function init(): void { // No-op for testing. } } diff --git a/tests/Integration/Includes/Abilities/Image_GenerationTest.php b/tests/Integration/Includes/Abilities/Image_GenerationTest.php index 80895990..7403b573 100644 --- a/tests/Integration/Includes/Abilities/Image_GenerationTest.php +++ b/tests/Integration/Includes/Abilities/Image_GenerationTest.php @@ -34,11 +34,11 @@ protected function load_experiment_metadata(): array { } /** - * Registers the experiment. + * Initializes the experiment. * * @since x.x.x */ - public function register(): void { + public function init(): void { // No-op for testing. } diff --git a/tests/Integration/Includes/Abilities/Image_ImportTest.php b/tests/Integration/Includes/Abilities/Image_ImportTest.php index b7219ac7..dc6ee344 100644 --- a/tests/Integration/Includes/Abilities/Image_ImportTest.php +++ b/tests/Integration/Includes/Abilities/Image_ImportTest.php @@ -34,11 +34,11 @@ protected function load_experiment_metadata(): array { } /** - * Registers the experiment. + * Initializes the experiment. * * @since x.x.x */ - public function register(): void { + public function init(): void { // No-op for testing. } diff --git a/tests/Integration/Includes/Abilities/Title_GenerationTest.php b/tests/Integration/Includes/Abilities/Title_GenerationTest.php index a3cb9454..1ef7e14c 100644 --- a/tests/Integration/Includes/Abilities/Title_GenerationTest.php +++ b/tests/Integration/Includes/Abilities/Title_GenerationTest.php @@ -34,11 +34,11 @@ protected function load_experiment_metadata(): array { } /** - * Registers the experiment. + * Initializes the experiment. * * @since 0.1.0 */ - public function register(): void { + public function init(): void { // No-op for testing. } } diff --git a/tests/Integration/Includes/Abstracts/Abstract_AbilityTest.php b/tests/Integration/Includes/Abstracts/Abstract_AbilityTest.php index 14abae4d..9d8fc0e5 100644 --- a/tests/Integration/Includes/Abstracts/Abstract_AbilityTest.php +++ b/tests/Integration/Includes/Abstracts/Abstract_AbilityTest.php @@ -122,11 +122,11 @@ protected function load_experiment_metadata(): array { } /** - * Registers the experiment. + * Initializes the experiment. * * @since 0.1.0 */ - public function register(): void { + public function init(): void { // No-op for testing. } } diff --git a/tests/Integration/Includes/Experiment_LoaderTest.php b/tests/Integration/Includes/Experiment_LoaderTest.php index c6663c11..cc4b072b 100644 --- a/tests/Integration/Includes/Experiment_LoaderTest.php +++ b/tests/Integration/Includes/Experiment_LoaderTest.php @@ -19,11 +19,11 @@ */ class Mock_Experiment extends Abstract_Experiment { /** - * Tracks if register was called. + * Tracks if init was called. * * @var bool */ - public $register_called = false; + public $init_called = false; /** * Loads experiment metadata. @@ -41,12 +41,12 @@ protected function load_experiment_metadata(): array { } /** - * Registers the experiment. + * Initializes the experiment. * * @since 0.1.0 */ - public function register(): void { - $this->register_called = true; + public function init(): void { + $this->init_called = true; } } @@ -183,7 +183,7 @@ function ( $registry ) { } /** - * Test initialize_experiments calls register on enabled experiments. + * Test initialize_experiments calls init on enabled experiments. * * @since 0.1.0 */ @@ -198,8 +198,8 @@ public function test_initialize_experiments_calls_register() { $this->loader->initialize_experiments(); $this->assertTrue( - $experiment->register_called, - 'Experiment register() should be called' + $experiment->init_called, + 'Experiment init() should be called' ); // Cleanup. @@ -220,14 +220,14 @@ public function test_initialize_experiments_prevents_double_initialization() { $this->assertTrue( $this->loader->is_initialized(), 'Should be initialized' ); // Reset the flag to track second call. - $experiment->register_called = false; + $experiment->init_called = false; // Try to initialize again. $this->loader->initialize_experiments(); $this->assertFalse( - $experiment->register_called, - 'Experiment register() should not be called twice' + $experiment->init_called, + 'Experiment init() should not be called twice' ); } @@ -296,8 +296,8 @@ public function test_disabled_experiments_are_skipped() { $this->loader->initialize_experiments(); $this->assertFalse( - $experiment->register_called, - 'Disabled experiment register() should not be called' + $experiment->init_called, + 'Disabled experiment init() should not be called' ); } } diff --git a/tests/Integration/Includes/Experiment_RegistryTest.php b/tests/Integration/Includes/Experiment_RegistryTest.php index 7a9c679f..53663ec8 100644 --- a/tests/Integration/Includes/Experiment_RegistryTest.php +++ b/tests/Integration/Includes/Experiment_RegistryTest.php @@ -34,11 +34,11 @@ protected function load_experiment_metadata(): array { } /** - * Registers the experiment. + * Initializes the experiment. * * @since 0.1.0 */ - public function register(): void { + public function init(): void { // No-op for testing. } }