| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Content Intelligence Feature Trait |
| 4 |
* Provides a trait for Content Intelligence endpoints that require a feature to |
| 5 |
* be enabled, by providing a method to check if the current user has permission |
| 6 |
* to use the feature. |
| 7 |
* |
| 8 |
* @package Parsely |
| 9 |
* @since 3.17.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
declare(strict_types=1); |
| 13 |
|
| 14 |
namespace Parsely\REST_API\Content_Helper; |
| 15 |
|
| 16 |
use Parsely\Permissions; |
| 17 |
use WP_Error; |
| 18 |
use WP_REST_Request; |
| 19 |
|
| 20 |
/** |
| 21 |
* Trait for Content Intelligence endpoints that require a feature to be enabled. |
| 22 |
* |
| 23 |
* @since 3.17.0 |
| 24 |
*/ |
| 25 |
trait Content_Helper_Feature { |
| 26 |
/** |
| 27 |
* Returns the name of the feature associated with the current endpoint. |
| 28 |
* |
| 29 |
* @since 3.17.0 |
| 30 |
* |
| 31 |
* @return string The feature's name. |
| 32 |
*/ |
| 33 |
abstract public function get_pch_feature_name(): string; |
| 34 |
|
| 35 |
/** |
| 36 |
* Checks if the current user has permission to use the feature associated |
| 37 |
* with the current endpoint. |
| 38 |
* |
| 39 |
* @since 3.17.0 |
| 40 |
* @since 3.23.6 Added the `$post_id` parameter. |
| 41 |
* |
| 42 |
* @param int $post_id The post ID, if the check is for a specific post. |
| 43 |
* @return bool True if the user has permission, false otherwise. |
| 44 |
*/ |
| 45 |
protected function is_pch_feature_enabled_for_user( |
| 46 |
int $post_id = 0 |
| 47 |
): bool { |
| 48 |
return Permissions::current_user_can_use_pch_feature( |
| 49 |
$this->get_pch_feature_name(), |
| 50 |
$this->parsely->get_options()['content_helper'], |
| 51 |
$post_id |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the IDs of the posts that the request operates on. |
| 57 |
* |
| 58 |
* @since 3.23.6 |
| 59 |
* |
| 60 |
* @param WP_REST_Request|null $request The request object. |
| 61 |
* @return array<int> The post IDs referenced by the request. |
| 62 |
*/ |
| 63 |
private function get_request_post_ids( ?WP_REST_Request $request ): array { |
| 64 |
if ( null === $request ) { |
| 65 |
return array(); |
| 66 |
} |
| 67 |
|
| 68 |
$post_ids = array(); |
| 69 |
|
| 70 |
foreach ( array( 'post_id', 'source_post_id' ) as $param_name ) { |
| 71 |
$param_value = $request->get_param( $param_name ); |
| 72 |
|
| 73 |
if ( is_numeric( $param_value ) && (int) $param_value > 0 ) { |
| 74 |
$post_ids[] = (int) $param_value; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return array_values( array_unique( $post_ids ) ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Checks if the endpoint is available to the current user. |
| 83 |
* |
| 84 |
* Overrides the method in the Base_Endpoint class to check if the current |
| 85 |
* user has permission to use the feature, on each post the request |
| 86 |
* references. |
| 87 |
* |
| 88 |
* @since 3.17.0 |
| 89 |
* @since 3.23.6 Added per-post permission checks. |
| 90 |
* |
| 91 |
* @param WP_REST_Request|null $request The request object. |
| 92 |
* @return bool|WP_Error True if the endpoint is available. |
| 93 |
*/ |
| 94 |
public function is_available_to_current_user( ?WP_REST_Request $request = null ) { |
| 95 |
$post_ids = $this->get_request_post_ids( $request ); |
| 96 |
|
| 97 |
if ( 0 === count( $post_ids ) ) { |
| 98 |
$post_ids = array( 0 ); |
| 99 |
} |
| 100 |
|
| 101 |
foreach ( $post_ids as $post_id ) { |
| 102 |
if ( ! $this->is_pch_feature_enabled_for_user( $post_id ) ) { |
| 103 |
return new WP_Error( |
| 104 |
'ch_access_to_feature_disabled', |
| 105 |
'', |
| 106 |
array( 'status' => 403 ) |
| 107 |
); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return parent::is_available_to_current_user( $request ); |
| 112 |
} |
| 113 |
} |
| 114 |
|