PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / introductions / user-interface / introductions-seen-route.php

introductions-seen-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/introductions/user-interface/introductions-seen-route.php

147 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Application\Introductions_Collector;
12 use Yoast\WP\SEO\Introductions\Infrastructure\Introductions_Seen_Repository;
13 use Yoast\WP\SEO\Main;
14 use Yoast\WP\SEO\Routes\Route_Interface;
15
16 /**
17 * Registers a route to set whether the user has seen an introduction.
18 *
19 * @makePublic
20 */
21 class Introductions_Seen_Route implements Route_Interface {
22
23 use No_Conditionals;
24
25 /**
26 * Represents the prefix.
27 *
28 * @var string
29 */
30 public const ROUTE_PREFIX = '/introductions/(?P<introduction_id>[\w-]+)/seen';
31
32 /**
33 * Holds the introductions collector instance.
34 *
35 * @var Introductions_Collector
36 */
37 private $introductions_collector;
38
39 /**
40 * Holds the repository.
41 *
42 * @var Introductions_Seen_Repository
43 */
44 private $introductions_seen_repository;
45
46 /**
47 * Holds the user helper.
48 *
49 * @var User_Helper
50 */
51 private $user_helper;
52
53 /**
54 * Constructs the class.
55 *
56 * @param Introductions_Seen_Repository $introductions_seen_repository The repository.
57 * @param User_Helper $user_helper The user helper.
58 * @param Introductions_Collector $introductions_collector The introduction collector.
59 */
60 public function __construct(
61 Introductions_Seen_Repository $introductions_seen_repository,
62 User_Helper $user_helper,
63 Introductions_Collector $introductions_collector
64 ) {
65 $this->introductions_seen_repository = $introductions_seen_repository;
66 $this->user_helper = $user_helper;
67 $this->introductions_collector = $introductions_collector;
68 }
69
70 /**
71 * Registers routes with WordPress.
72 *
73 * @return void
74 */
75 public function register_routes() {
76 \register_rest_route(
77 Main::API_V1_NAMESPACE,
78 self::ROUTE_PREFIX,
79 [
80 [
81 'methods' => 'POST',
82 'callback' => [ $this, 'set_introduction_seen' ],
83 'permission_callback' => [ $this, 'permission_edit_posts' ],
84 'args' => [
85 'introduction_id' => [
86 'required' => true,
87 'type' => 'string',
88 'sanitize_callback' => 'sanitize_text_field',
89 ],
90 'is_seen' => [
91 'required' => false,
92 'type' => 'bool',
93 'default' => true,
94 'sanitize_callback' => 'rest_sanitize_boolean',
95 ],
96 ],
97 ],
98 ],
99 );
100 }
101
102 /**
103 * Sets whether the introduction is seen.
104 *
105 * @param WP_REST_Request $request The request object.
106 *
107 * @return WP_REST_Response|WP_Error The success or failure response.
108 */
109 public function set_introduction_seen( WP_REST_Request $request ) {
110 $params = $request->get_params();
111 $introduction_id = $params['introduction_id'];
112 $is_seen = $params['is_seen'];
113
114 if ( $this->introductions_collector->is_available_introduction( $introduction_id ) ) {
115 try {
116 $user_id = $this->user_helper->get_current_user_id();
117 $result = $this->introductions_seen_repository->set_introduction( $user_id, $introduction_id, $is_seen );
118 } catch ( Exception $exception ) {
119 return new WP_Error(
120 'wpseo_introductions_seen_error',
121 $exception->getMessage(),
122 (object) [],
123 );
124 }
125
126 return new WP_REST_Response(
127 [
128 'json' => (object) [
129 'success' => $result,
130 ],
131 ],
132 ( $result ) ? 200 : 400,
133 );
134 }
135 return new WP_REST_Response( [], 400 );
136 }
137
138 /**
139 * Permission callback.
140 *
141 * @return bool True when user has 'edit_posts' permission.
142 */
143 public function permission_edit_posts() {
144 return \current_user_can( 'edit_posts' );
145 }
146 }
147