Skip to content

Commit

Permalink
Merge pull request #73 from alleyinteractive/feature/rest-endpoint
Browse files Browse the repository at this point in the history
Feature/rest endpoint
  • Loading branch information
rpasillas authored Apr 23, 2024
2 parents 6e5bc91 + 5d43f05 commit 5092be0
Show file tree
Hide file tree
Showing 10 changed files with 190 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
The GNU General Public License (GPL) license. Please see [License File](LICENSE) for more information.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* WP_Conditional_Blocks class file
* Conditions class file
*
* @package wp-conditional-blocks
*/
Expand All @@ -17,9 +17,9 @@
use Alley\WP\WP_Conditional_Blocks\traits\Singleton;

/**
* WP_Conditional_Blocks
* Conditions
*/
class WP_Conditional_Blocks {
class Conditions {
/**
* Use the singleton.
*
Expand Down
80 changes: 80 additions & 0 deletions src/class-endpoint-get-conditions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Endpoint class file that gets all the conditions.
*
* @package wp-conditional-blocks
*/

namespace Alley\WP\WP_Conditional_Blocks;

/**
* Class Endpoint_Get_Conditions
*
* 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' ] );
}

/**
* Registers the route.
*
* @return void
*/
public function register_route(): void {
register_rest_route(
'conditional-blocks/v1',
'/get-conditions/',
[
'methods' => '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
);
}
}
26 changes: 26 additions & 0 deletions src/class-endpoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* This file registers REST API Endpoints.
*
* @package wp-conditional-blocks
*/

namespace Alley\WP\WP_Conditional_Blocks;

use Alley\WP\Types\Feature;

/**
* Class Endpoints
*
* This class implements the Feature interface and registers REST API Endpoints.
*/
class Endpoints implements Feature {
/**
* Boot the feature.
*
* @return void
*/
public function boot(): void {
new Endpoint_Get_Conditions();
}
}
20 changes: 20 additions & 0 deletions src/endpoints.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* This file registers REST endpoints.
*
* @package wp-conditional-blocks
*/

namespace Alley\WP\WP_Conditional_Blocks;

/**
* Boots the endpoints.
*
* @return void
*/
function boot_endpoints(): void {
$endpoints = new Endpoints();
$endpoints->boot();
}

boot_endpoints();
14 changes: 4 additions & 10 deletions src/traits/trait-singleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@

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;

/**
* WP Conditional Blocks unit test suite.
*
* @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.
*
* @return void
*/
public static function setUpBeforeClass(): void {
self::$instance = WP_Conditional_Blocks::get_instance();
self::$instance = Conditions::get_instance();
}

/**
Expand All @@ -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();
}
Expand Down Expand Up @@ -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', '[email protected]' );
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'] );
}
}
6 changes: 4 additions & 2 deletions wp-conditional-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 5092be0

Please sign in to comment.