generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from alleyinteractive/feature/rest-endpoint
Feature/rest endpoint
- Loading branch information
Showing
10 changed files
with
190 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
|
||
/** | ||
|
@@ -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', '[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'] ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters