From 3f2a355e806cad87067d1d14007614e2fbdeed1b Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Thu, 4 Apr 2024 15:56:27 -0700 Subject: [PATCH 01/14] Issue 14: Refactors conditions class name. --- ...-wp-conditional-blocks.php => class-conditions.php} | 6 +++--- tests/unit/class-wp-conditional-blocks-unit-tests.php | 10 +++++----- wp-conditional-blocks.php | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) rename src/{class-wp-conditional-blocks.php => class-conditions.php} (97%) diff --git a/src/class-wp-conditional-blocks.php b/src/class-conditions.php similarity index 97% rename from src/class-wp-conditional-blocks.php rename to src/class-conditions.php index abd4f79..678e1ea 100644 --- a/src/class-wp-conditional-blocks.php +++ b/src/class-conditions.php @@ -1,6 +1,6 @@ Date: Mon, 15 Apr 2024 16:34:41 -0700 Subject: [PATCH 02/14] #14: Adds the endpoints in a new class file. --- src/class-endpoint-get-conditions.php | 44 +++++++++++++++++++++++++++ src/endpoints.php | 18 +++++++++++ wp-conditional-blocks.php | 1 + 3 files changed, 63 insertions(+) create mode 100644 src/class-endpoint-get-conditions.php create mode 100644 src/endpoints.php diff --git a/src/class-endpoint-get-conditions.php b/src/class-endpoint-get-conditions.php new file mode 100644 index 0000000..be1a61b --- /dev/null +++ b/src/class-endpoint-get-conditions.php @@ -0,0 +1,44 @@ + 'GET', + 'callback' => [ $this, 'get_response' ], + ] ); + } + + /** + * Retrieves the response for the get-conditions endpoint. + * + * @return \WP_REST_Response The REST response object. + */ + public function get_response(): \WP_REST_Response { + $conditions = Conditions::get_instance(); + + return new \WP_REST_Response( [ + 'message' => !empty( $conditions->get_conditions() ) ? $conditions->get_conditions() : 'No conditions found.' + ], 200 ); + } +} diff --git a/src/endpoints.php b/src/endpoints.php new file mode 100644 index 0000000..cc84b2c --- /dev/null +++ b/src/endpoints.php @@ -0,0 +1,18 @@ + Date: Mon, 15 Apr 2024 17:01:33 -0700 Subject: [PATCH 03/14] #14: Formats the returned conditions. --- src/class-endpoint-get-conditions.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/class-endpoint-get-conditions.php b/src/class-endpoint-get-conditions.php index be1a61b..ee24385 100644 --- a/src/class-endpoint-get-conditions.php +++ b/src/class-endpoint-get-conditions.php @@ -38,7 +38,22 @@ public function get_response(): \WP_REST_Response { $conditions = Conditions::get_instance(); return new \WP_REST_Response( [ - 'message' => !empty( $conditions->get_conditions() ) ? $conditions->get_conditions() : 'No conditions found.' + 'message' => !empty( $conditions->get_conditions() ) ? $this->format_conditions( $conditions->get_conditions() ) : 'No conditions found.' ], 200 ); } + + /** + * Formats the conditions. + * + * @param array{int, array{name:string, slug:string, callable:callable}} $conditions The conditions to be formatted. + * + * + * @return array{array{name:string, slug:string}} formatted conditions. + */ + private function format_conditions( array $conditions ): array { + return array_map( function( $item ) { + unset( $item['callable'] ); + return $item; + }, $conditions ); + } } From a35300104583eb93a5315be2f7367d0e0b165e61 Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Tue, 16 Apr 2024 15:35:17 -0700 Subject: [PATCH 04/14] #14: adds a unit test. --- src/class-endpoint-get-conditions.php | 1 + ...class-wp-conditional-blocks-unit-tests.php | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/class-endpoint-get-conditions.php b/src/class-endpoint-get-conditions.php index ee24385..58b8ac7 100644 --- a/src/class-endpoint-get-conditions.php +++ b/src/class-endpoint-get-conditions.php @@ -26,6 +26,7 @@ public function register_route(): void { register_rest_route('conditional-blocks/v1', '/get-conditions/', [ 'methods' => 'GET', 'callback' => [ $this, 'get_response' ], + 'permission_callback' => '__return_true', ] ); } diff --git a/tests/unit/class-wp-conditional-blocks-unit-tests.php b/tests/unit/class-wp-conditional-blocks-unit-tests.php index 90d1bc8..153d55d 100644 --- a/tests/unit/class-wp-conditional-blocks-unit-tests.php +++ b/tests/unit/class-wp-conditional-blocks-unit-tests.php @@ -8,6 +8,7 @@ namespace Alley\WP\WP_Conditional_Blocks\Tests\Unit; use Alley\WP\WP_Conditional_Blocks\Conditions; +use Alley\WP\WP_Conditional_Blocks\Endpoint_Get_Conditions; use PHPUnit\Framework\TestCase; /** @@ -117,4 +118,32 @@ private function add_test_conditions( array $conditions ): void { ); } } + + public function test_get_conditions_endpoint(): void { + $this->add_test_conditions( + [ + [ + 'name' => 'Condition 1', + 'slug' => 'condition-1', + 'callable' => fn() => true, + ], + [ + 'name' => 'Condition 2', + 'slug' => 'condition-2', + 'callable' => fn() => true, + ], + ] + ); + + do_action( 'rest_api_init' ); + + $request = new \WP_REST_Request( 'GET', '/conditional-blocks/v1/get-conditions/' ); + $server = rest_get_server(); + $response = $server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertIsArray( $data ); + var_dump( $data ); + $this->assertCount( 2, $data ); + } } From b9fc70eabe9f709ae1710763976282baa1f29404 Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Tue, 16 Apr 2024 16:16:53 -0700 Subject: [PATCH 05/14] #14: Fixes the failing assertions. Cleanup. --- tests/unit/class-wp-conditional-blocks-unit-tests.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/unit/class-wp-conditional-blocks-unit-tests.php b/tests/unit/class-wp-conditional-blocks-unit-tests.php index 153d55d..783f2e6 100644 --- a/tests/unit/class-wp-conditional-blocks-unit-tests.php +++ b/tests/unit/class-wp-conditional-blocks-unit-tests.php @@ -135,15 +135,12 @@ public function test_get_conditions_endpoint(): void { ] ); - do_action( 'rest_api_init' ); - - $request = new \WP_REST_Request( 'GET', '/conditional-blocks/v1/get-conditions/' ); + $request = new \WP_REST_Request( 'GET', '/conditional-blocks/v1/get-conditions' ); $server = rest_get_server(); $response = $server->dispatch( $request ); $data = $response->get_data(); $this->assertIsArray( $data ); - var_dump( $data ); - $this->assertCount( 2, $data ); + $this->assertCount( 2, $data['message'] ); } } From 22c4d7ea6da56b6b7a9da4c748926ea6cbece750 Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Wed, 17 Apr 2024 09:30:13 -0700 Subject: [PATCH 06/14] #14: Adds docblock. --- tests/unit/class-wp-conditional-blocks-unit-tests.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/unit/class-wp-conditional-blocks-unit-tests.php b/tests/unit/class-wp-conditional-blocks-unit-tests.php index 783f2e6..9d73bad 100644 --- a/tests/unit/class-wp-conditional-blocks-unit-tests.php +++ b/tests/unit/class-wp-conditional-blocks-unit-tests.php @@ -119,6 +119,11 @@ private function add_test_conditions( array $conditions ): void { } } + /** + * Test the get_conditions endpoint. + * + * @return void + */ public function test_get_conditions_endpoint(): void { $this->add_test_conditions( [ From 2cf84155a351d3c34acad875932cda417588da1a Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Wed, 17 Apr 2024 10:50:59 -0700 Subject: [PATCH 07/14] #14 PHPCS Fixes and text domain translation. --- src/class-endpoint-get-conditions.php | 38 ++++++++++++------- ...> class-conditional-blocks-unit-tests.php} | 0 2 files changed, 25 insertions(+), 13 deletions(-) rename tests/unit/{class-wp-conditional-blocks-unit-tests.php => class-conditional-blocks-unit-tests.php} (100%) diff --git a/src/class-endpoint-get-conditions.php b/src/class-endpoint-get-conditions.php index 58b8ac7..2be6f19 100644 --- a/src/class-endpoint-get-conditions.php +++ b/src/class-endpoint-get-conditions.php @@ -13,6 +13,9 @@ * This class is responsible for registering a REST API endpoint and defining its functionality. */ class Endpoint_Get_Conditions { + /** + * Constructor + */ public function __construct() { add_action( 'rest_api_init', [ $this, 'register_route' ] ); } @@ -23,11 +26,15 @@ public function __construct() { * @return void */ public function register_route(): void { - register_rest_route('conditional-blocks/v1', '/get-conditions/', [ - 'methods' => 'GET', - 'callback' => [ $this, 'get_response' ], - 'permission_callback' => '__return_true', - ] ); + register_rest_route( + 'conditional-blocks/v1', + '/get-conditions/', + [ + 'methods' => 'GET', + 'callback' => [ $this, 'get_response' ], + 'permission_callback' => '__return_true', + ] + ); } /** @@ -38,9 +45,12 @@ public function register_route(): void { public function get_response(): \WP_REST_Response { $conditions = Conditions::get_instance(); - return new \WP_REST_Response( [ - 'message' => !empty( $conditions->get_conditions() ) ? $this->format_conditions( $conditions->get_conditions() ) : 'No conditions found.' - ], 200 ); + return new \WP_REST_Response( + [ + 'message' => ! empty( $conditions->get_conditions() ) ? $this->format_conditions( $conditions->get_conditions() ) : __( 'No conditions found.', 'wp-conditional-blocks' ), + ], + 200 + ); } /** @@ -48,13 +58,15 @@ public function get_response(): \WP_REST_Response { * * @param array{int, array{name:string, slug:string, callable:callable}} $conditions The conditions to be formatted. * - * * @return array{array{name:string, slug:string}} formatted conditions. */ private function format_conditions( array $conditions ): array { - return array_map( function( $item ) { - unset( $item['callable'] ); - return $item; - }, $conditions ); + return array_map( + function ( $item ) { + unset( $item['callable'] ); + return $item; + }, + $conditions + ); } } diff --git a/tests/unit/class-wp-conditional-blocks-unit-tests.php b/tests/unit/class-conditional-blocks-unit-tests.php similarity index 100% rename from tests/unit/class-wp-conditional-blocks-unit-tests.php rename to tests/unit/class-conditional-blocks-unit-tests.php From 40d27e29069c7a9c3c8ad98e08e91e7ddbab8850 Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Wed, 17 Apr 2024 10:53:03 -0700 Subject: [PATCH 08/14] #14: Removes the init method from the singleton trait and invokes it conditionally via the `get_instance()` --- src/traits/trait-singleton.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/traits/trait-singleton.php b/src/traits/trait-singleton.php index af95f46..6400776 100644 --- a/src/traits/trait-singleton.php +++ b/src/traits/trait-singleton.php @@ -30,6 +30,10 @@ trait Singleton { public static function get_instance(): self { if ( null === self::$instance ) { self::$instance = new static(); + + if ( method_exists( self::$instance, 'init' ) ) { + self::$instance->init(); + } } return self::$instance; @@ -40,14 +44,4 @@ public static function get_instance(): self { */ final private function __construct() { } - - /** - * Initializes the class. - * - * Use this function instead of the constructor for any required initialization. - * - * @return void - */ - protected function init() { - } } From ab11d943fee87d9e4f7830933c0c9b9cc1d954d4 Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Wed, 17 Apr 2024 10:53:25 -0700 Subject: [PATCH 09/14] #14: Remove unused use statement. Change the class name. --- tests/unit/class-conditional-blocks-unit-tests.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/class-conditional-blocks-unit-tests.php b/tests/unit/class-conditional-blocks-unit-tests.php index 9d73bad..1dd158f 100644 --- a/tests/unit/class-conditional-blocks-unit-tests.php +++ b/tests/unit/class-conditional-blocks-unit-tests.php @@ -8,7 +8,6 @@ namespace Alley\WP\WP_Conditional_Blocks\Tests\Unit; use Alley\WP\WP_Conditional_Blocks\Conditions; -use Alley\WP\WP_Conditional_Blocks\Endpoint_Get_Conditions; use PHPUnit\Framework\TestCase; /** @@ -16,7 +15,7 @@ * * @link https://mantle.alley.com/testing/test-framework.html */ -class Conditions_Unit_Tests extends TestCase { +class Conditional_Blocks_Unit_Tests extends TestCase { /** * Contains a static instance of the class. * From 36c4afa0d9226fee4d08b26a6d647f61311c460b Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Wed, 17 Apr 2024 14:02:30 -0700 Subject: [PATCH 10/14] #14: Adds a comment. --- src/class-endpoint-get-conditions.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/class-endpoint-get-conditions.php b/src/class-endpoint-get-conditions.php index 2be6f19..e4788b5 100644 --- a/src/class-endpoint-get-conditions.php +++ b/src/class-endpoint-get-conditions.php @@ -62,6 +62,10 @@ public function get_response(): \WP_REST_Response { */ private function format_conditions( array $conditions ): array { return array_map( + /** + * Removes the `callable`. + * see https://github.com/alleyinteractive/wp-conditional-blocks/issues/14 + */ function ( $item ) { unset( $item['callable'] ); return $item; From 52856dcc4da33766440ee3b0b7c50e82269f834d Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Wed, 17 Apr 2024 14:50:34 -0700 Subject: [PATCH 11/14] #14: Adds the Type Extensions package and refactors the endpoints registration to use the Features interface. --- composer.json | 3 ++- src/class-endpoints.php | 26 ++++++++++++++++++++++++++ src/endpoints.php | 12 +++++++----- wp-conditional-blocks.php | 1 + 4 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/class-endpoints.php diff --git a/composer.json b/composer.json index c9609b2..43b6436 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "require": { "php": "^8.0", "alleyinteractive/composer-wordpress-autoloader": "^1.0", - "alleyinteractive/wp-match-blocks": "^3.0" + "alleyinteractive/wp-match-blocks": "^3.0", + "alleyinteractive/wp-type-extensions": "^2.1" }, "require-dev": { "alleyinteractive/alley-coding-standards": "^2.0", diff --git a/src/class-endpoints.php b/src/class-endpoints.php new file mode 100644 index 0000000..ffc361d --- /dev/null +++ b/src/class-endpoints.php @@ -0,0 +1,26 @@ +boot(); } -register_endpoints(); + +boot_endpoints(); diff --git a/wp-conditional-blocks.php b/wp-conditional-blocks.php index 8035611..7d4ed53 100644 --- a/wp-conditional-blocks.php +++ b/wp-conditional-blocks.php @@ -54,6 +54,7 @@ function () { // Load the plugin's main files. require_once __DIR__ . '/src/assets.php'; require_once __DIR__ . '/src/class-conditions.php'; +require_once __DIR__ . '/src/class-endpoints.php'; require_once __DIR__ . '/src/meta.php'; require_once __DIR__ . '/src/endpoints.php'; From 8ebf21292de23c748dcbaf46b6f7f471cc1a77af Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Thu, 18 Apr 2024 14:36:29 -0700 Subject: [PATCH 12/14] #14: Refactor to return an empty array if there are no conditions. --- src/class-endpoint-get-conditions.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/class-endpoint-get-conditions.php b/src/class-endpoint-get-conditions.php index e4788b5..b295b51 100644 --- a/src/class-endpoint-get-conditions.php +++ b/src/class-endpoint-get-conditions.php @@ -47,7 +47,7 @@ public function get_response(): \WP_REST_Response { return new \WP_REST_Response( [ - 'message' => ! empty( $conditions->get_conditions() ) ? $this->format_conditions( $conditions->get_conditions() ) : __( 'No conditions found.', 'wp-conditional-blocks' ), + 'message' => $this->format_conditions( $conditions->get_conditions() ), ], 200 ); @@ -56,11 +56,15 @@ public function get_response(): \WP_REST_Response { /** * Formats the conditions. * - * @param array{int, array{name:string, slug:string, callable:callable}} $conditions The conditions to be formatted. + * @param array{int, array{name:string, slug:string, callable:callable}}|array{} $conditions The conditions to be formatted. * - * @return array{array{name:string, slug:string}} formatted conditions. + * @return array{array{name:string, slug:string}}|array{} formatted conditions. */ private function format_conditions( array $conditions ): array { + if ( empty( $conditions ) ) { + return $conditions; + } + return array_map( /** * Removes the `callable`. From f55ad2d3c4cf837114c88274faf7ab42dde4c4f8 Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Thu, 18 Apr 2024 15:19:34 -0700 Subject: [PATCH 13/14] #14 Adds `is_user_logged_in` to the endpoint permission callback. Update unit test. --- src/class-endpoint-get-conditions.php | 2 +- tests/unit/class-conditional-blocks-unit-tests.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/class-endpoint-get-conditions.php b/src/class-endpoint-get-conditions.php index b295b51..4c185c6 100644 --- a/src/class-endpoint-get-conditions.php +++ b/src/class-endpoint-get-conditions.php @@ -32,7 +32,7 @@ public function register_route(): void { [ 'methods' => 'GET', 'callback' => [ $this, 'get_response' ], - 'permission_callback' => '__return_true', + 'permission_callback' => 'is_user_logged_in', ] ); } diff --git a/tests/unit/class-conditional-blocks-unit-tests.php b/tests/unit/class-conditional-blocks-unit-tests.php index 1dd158f..869692a 100644 --- a/tests/unit/class-conditional-blocks-unit-tests.php +++ b/tests/unit/class-conditional-blocks-unit-tests.php @@ -38,6 +38,8 @@ public static function setUpBeforeClass(): void { * @return void */ public function setUp(): void { + parent::setUp(); + // Reset the conditions before each test. self::$instance->reset_conditions_for_testing(); } @@ -139,12 +141,22 @@ public function test_get_conditions_endpoint(): void { ] ); + // Create a user and log them in. + $user_id = wp_create_user( 'test_user', 'password', 'unit@test.com' ); + wp_set_current_user( $user_id ); + + // Perform the REST request. $request = new \WP_REST_Request( 'GET', '/conditional-blocks/v1/get-conditions' ); $server = rest_get_server(); $response = $server->dispatch( $request ); $data = $response->get_data(); + // Log out the user. + wp_set_current_user( null ); + $this->assertIsArray( $data ); + + // We should have two items in the message, ie, both conditions. $this->assertCount( 2, $data['message'] ); } } From 5d43f05b78e72477d17e4d8403310748c316554b Mon Sep 17 00:00:00 2001 From: Ron Pasillas Date: Mon, 22 Apr 2024 15:04:11 -0700 Subject: [PATCH 14/14] #14: Updates PHP 8.0 references to 8.1 --- .github/workflows/unit-test.yml | 2 +- README.md | 4 ++-- composer.json | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 213fbb4..12b7eb1 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -13,7 +13,7 @@ jobs: php-tests: strategy: matrix: - php: [8.0, 8.1, 8.2] + php: [8.1, 8.2] wordpress: ["latest"] uses: alleyinteractive/.github/.github/workflows/php-tests.yml@main with: diff --git a/README.md b/README.md index 9f55b32..54715d5 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Requires at least: 5.9 Tested up to: 6.1 -Requires PHP: 8.0 +Requires PHP: 8.1 License: GPL v2 or later @@ -132,4 +132,4 @@ with us](https://alley.co/careers/). ## License -The GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information. \ No newline at end of file +The GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information. diff --git a/composer.json b/composer.json index 43b6436..51a5643 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "alleyinteractive/composer-wordpress-autoloader": "^1.0", "alleyinteractive/wp-match-blocks": "^3.0", "alleyinteractive/wp-type-extensions": "^2.1"