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 c9609b2..51a5643 100644 --- a/composer.json +++ b/composer.json @@ -15,9 +15,10 @@ } ], "require": { - "php": "^8.0", + "php": "^8.1", "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-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 @@ 'GET', + 'callback' => [ $this, 'get_response' ], + 'permission_callback' => 'is_user_logged_in', + ] + ); + } + + /** + * 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' => $this->format_conditions( $conditions->get_conditions() ), + ], + 200 + ); + } + + /** + * Formats the conditions. + * + * @param array{int, array{name:string, slug:string, callable:callable}}|array{} $conditions The conditions to be formatted. + * + * @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`. + * see https://github.com/alleyinteractive/wp-conditional-blocks/issues/14 + */ + function ( $item ) { + unset( $item['callable'] ); + return $item; + }, + $conditions + ); + } +} 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(); +} + +boot_endpoints(); 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() { - } } diff --git a/tests/unit/class-wp-conditional-blocks-unit-tests.php b/tests/unit/class-conditional-blocks-unit-tests.php similarity index 66% rename from tests/unit/class-wp-conditional-blocks-unit-tests.php rename to tests/unit/class-conditional-blocks-unit-tests.php index 2f35cde..869692a 100644 --- a/tests/unit/class-wp-conditional-blocks-unit-tests.php +++ b/tests/unit/class-conditional-blocks-unit-tests.php @@ -7,7 +7,7 @@ namespace Alley\WP\WP_Conditional_Blocks\Tests\Unit; -use Alley\WP\WP_Conditional_Blocks\WP_Conditional_Blocks; +use Alley\WP\WP_Conditional_Blocks\Conditions; use PHPUnit\Framework\TestCase; /** @@ -15,13 +15,13 @@ * * @link https://mantle.alley.com/testing/test-framework.html */ -class WP_Conditional_Blocks_Unit_Tests extends TestCase { +class Conditional_Blocks_Unit_Tests extends TestCase { /** * Contains a static instance of the class. * - * @var WP_Conditional_Blocks + * @var Conditions */ - private static WP_Conditional_Blocks $instance; + private static Conditions $instance; /** * Fixture method to set up the state of the tests. @@ -29,7 +29,7 @@ class WP_Conditional_Blocks_Unit_Tests extends TestCase { * @return void */ public static function setUpBeforeClass(): void { - self::$instance = WP_Conditional_Blocks::get_instance(); + self::$instance = Conditions::get_instance(); } /** @@ -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(); } @@ -117,4 +119,44 @@ 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( + [ + [ + 'name' => 'Condition 1', + 'slug' => 'condition-1', + 'callable' => fn() => true, + ], + [ + 'name' => 'Condition 2', + 'slug' => 'condition-2', + 'callable' => fn() => true, + ], + ] + ); + + // 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'] ); + } } diff --git a/wp-conditional-blocks.php b/wp-conditional-blocks.php index d20ae97..7d4ed53 100644 --- a/wp-conditional-blocks.php +++ b/wp-conditional-blocks.php @@ -53,13 +53,15 @@ function () { // Load the plugin's main files. require_once __DIR__ . '/src/assets.php'; -require_once __DIR__ . '/src/class-wp-conditional-blocks.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'; /** * Instantiate the plugin. */ function main(): void { - WP_Conditional_Blocks::get_instance(); + Conditions::get_instance(); } main();