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 / tracking / user-interface / action-tracking-route.php

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

149 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 namespace Yoast\WP\SEO\Tracking\User_Interface;
5
6 use Exception;
7 use WP_Error;
8 use WP_REST_Request;
9 use WP_REST_Response;
10 use Yoast\WP\SEO\Conditionals\No_Conditionals;
11 use Yoast\WP\SEO\Helpers\Capability_Helper;
12 use Yoast\WP\SEO\Helpers\Options_Helper;
13 use Yoast\WP\SEO\Main;
14 use Yoast\WP\SEO\Routes\Route_Interface;
15 use Yoast\WP\SEO\Tracking\Application\Action_Tracker;
16 use Yoast\WP\SEO\Tracking\Domain\Exceptions\Invalid_Tracked_Action_Exception;
17
18 /**
19 * Registers a route to track user actions.
20 */
21 class Action_Tracking_Route implements Route_Interface {
22
23 use No_Conditionals;
24
25 /**
26 * The namespace for this route.
27 *
28 * @var string
29 */
30 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
31
32 /**
33 * The prefix for this route.
34 *
35 * @var string
36 */
37 public const ROUTE_PREFIX = '/action_tracking';
38
39 /**
40 * Holds the action tracker instance.
41 *
42 * @var Action_Tracker
43 */
44 private $action_tracker;
45
46 /**
47 * Holds the capability helper instance.
48 *
49 * @var Capability_Helper
50 */
51 private $capability_helper;
52
53 /**
54 * Holds the options helper instance.
55 *
56 * @var Options_Helper
57 */
58 private $options_helper;
59
60 /**
61 * Constructs the class.
62 *
63 * @param Action_Tracker $action_tracker The action tracker.
64 * @param Capability_Helper $capability_helper The capability helper.
65 * @param Options_Helper $options_helper The options helper.
66 */
67 public function __construct(
68 Action_Tracker $action_tracker,
69 Capability_Helper $capability_helper,
70 Options_Helper $options_helper
71 ) {
72 $this->action_tracker = $action_tracker;
73 $this->capability_helper = $capability_helper;
74 $this->options_helper = $options_helper;
75 }
76
77 /**
78 * Registers routes with WordPress.
79 *
80 * @return void
81 */
82 public function register_routes() {
83 \register_rest_route(
84 self::ROUTE_NAMESPACE,
85 self::ROUTE_PREFIX,
86 [
87 [
88 'methods' => 'POST',
89 'callback' => [ $this, 'track_action' ],
90 'permission_callback' => [ $this, 'check_capabilities' ],
91 'args' => [
92 'action' => [
93 'required' => true,
94 'type' => 'string',
95 'sanitize_callback' => 'sanitize_text_field',
96 ],
97 ],
98 ],
99 ],
100 );
101 }
102
103 /**
104 * Tracks an action.
105 *
106 * @param WP_REST_Request $request The request object.
107 *
108 * @return WP_REST_Response|WP_Error The success or failure response.
109 *
110 * @throws Invalid_Tracked_Action_Exception When the given action is invalid.
111 */
112 public function track_action( WP_REST_Request $request ): WP_REST_Response {
113 $action_to_track = $request->get_param( 'action' );
114
115 try {
116 if ( ! \in_array( $action_to_track, $this->options_helper->get_tracking_only_options(), true ) ) {
117 throw new Invalid_Tracked_Action_Exception();
118 }
119
120 $this->action_tracker->track_version_for_performed_action( $action_to_track );
121 } catch ( Exception $exception ) {
122 return new WP_REST_Response(
123 [
124 'success' => false,
125 'error' => $exception->getMessage(),
126 ],
127 $exception->getCode(),
128 );
129 }
130
131 return new WP_REST_Response(
132 [
133 'success' => true,
134 'action' => $action_to_track,
135 ],
136 200,
137 );
138 }
139
140 /**
141 * Checks if the current user has the required capabilities.
142 *
143 * @return bool
144 */
145 public function check_capabilities() {
146 return $this->capability_helper->current_user_can( 'wpseo_manage_options' );
147 }
148 }
149