| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Introductions\User_Interface; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use WP_Error; |
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use Yoast\WP\SEO\Conditionals\No_Conditionals; |
| 10 |
use Yoast\WP\SEO\Helpers\User_Helper; |
| 11 |
use Yoast\WP\SEO\Introductions\Infrastructure\Wistia_Embed_Permission_Repository; |
| 12 |
use Yoast\WP\SEO\Main; |
| 13 |
use Yoast\WP\SEO\Routes\Route_Interface; |
| 14 |
|
| 15 |
/** |
| 16 |
* Registers a route to offer get/set of the wistia embed permission for a user. |
| 17 |
*/ |
| 18 |
class Wistia_Embed_Permission_Route implements Route_Interface { |
| 19 |
|
| 20 |
use No_Conditionals; |
| 21 |
|
| 22 |
/** |
| 23 |
* Represents the prefix. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public const ROUTE_PREFIX = '/wistia_embed_permission'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the repository. |
| 31 |
* |
| 32 |
* @var Wistia_Embed_Permission_Repository |
| 33 |
*/ |
| 34 |
private $wistia_embed_permission_repository; |
| 35 |
|
| 36 |
/** |
| 37 |
* Holds the user helper. |
| 38 |
* |
| 39 |
* @var User_Helper |
| 40 |
*/ |
| 41 |
private $user_helper; |
| 42 |
|
| 43 |
/** |
| 44 |
* Constructs the class. |
| 45 |
* |
| 46 |
* @param Wistia_Embed_Permission_Repository $wistia_embed_permission_repository The repository. |
| 47 |
* @param User_Helper $user_helper The user helper. |
| 48 |
*/ |
| 49 |
public function __construct( |
| 50 |
Wistia_Embed_Permission_Repository $wistia_embed_permission_repository, |
| 51 |
User_Helper $user_helper |
| 52 |
) { |
| 53 |
$this->wistia_embed_permission_repository = $wistia_embed_permission_repository; |
| 54 |
$this->user_helper = $user_helper; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Registers routes with WordPress. |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function register_routes() { |
| 63 |
\register_rest_route( |
| 64 |
Main::API_V1_NAMESPACE, |
| 65 |
self::ROUTE_PREFIX, |
| 66 |
[ |
| 67 |
[ |
| 68 |
'methods' => 'GET', |
| 69 |
'callback' => [ $this, 'get_wistia_embed_permission' ], |
| 70 |
'permission_callback' => [ $this, 'permission_edit_posts' ], |
| 71 |
], |
| 72 |
[ |
| 73 |
'methods' => 'POST', |
| 74 |
'callback' => [ $this, 'set_wistia_embed_permission' ], |
| 75 |
'permission_callback' => [ $this, 'permission_edit_posts' ], |
| 76 |
'args' => [ |
| 77 |
'value' => [ |
| 78 |
'required' => false, |
| 79 |
'type' => 'bool', |
| 80 |
'default' => true, |
| 81 |
], |
| 82 |
], |
| 83 |
], |
| 84 |
], |
| 85 |
); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets the value of the wistia embed permission. |
| 90 |
* |
| 91 |
* @return WP_REST_Response|WP_Error The response, or an error. |
| 92 |
*/ |
| 93 |
public function get_wistia_embed_permission() { |
| 94 |
try { |
| 95 |
$user_id = $this->user_helper->get_current_user_id(); |
| 96 |
$value = $this->wistia_embed_permission_repository->get_value_for_user( $user_id ); |
| 97 |
} catch ( Exception $exception ) { |
| 98 |
return new WP_Error( |
| 99 |
'wpseo_wistia_embed_permission_error', |
| 100 |
$exception->getMessage(), |
| 101 |
(object) [], |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
return new WP_REST_Response( |
| 106 |
[ |
| 107 |
'json' => (object) [ |
| 108 |
'value' => $value, |
| 109 |
], |
| 110 |
], |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Sets the value of the wistia embed permission. |
| 116 |
* |
| 117 |
* @param WP_REST_Request $request The request object. |
| 118 |
* |
| 119 |
* @return WP_REST_Response|WP_Error The success or failure response. |
| 120 |
*/ |
| 121 |
public function set_wistia_embed_permission( WP_REST_Request $request ) { |
| 122 |
$params = $request->get_json_params(); |
| 123 |
$value = (bool) $params['value']; |
| 124 |
|
| 125 |
try { |
| 126 |
$user_id = $this->user_helper->get_current_user_id(); |
| 127 |
$result = $this->wistia_embed_permission_repository->set_value_for_user( $user_id, $value ); |
| 128 |
} catch ( Exception $exception ) { |
| 129 |
return new WP_Error( |
| 130 |
'wpseo_wistia_embed_permission_error', |
| 131 |
$exception->getMessage(), |
| 132 |
(object) [], |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
return new WP_REST_Response( |
| 137 |
[ |
| 138 |
'json' => (object) [ |
| 139 |
'success' => $result, |
| 140 |
], |
| 141 |
], |
| 142 |
( $result ) ? 200 : 400, |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Permission callback. |
| 148 |
* |
| 149 |
* @return bool True when user has 'edit_posts' permission. |
| 150 |
*/ |
| 151 |
public function permission_edit_posts() { |
| 152 |
return \current_user_can( 'edit_posts' ); |
| 153 |
} |
| 154 |
} |
| 155 |
|