PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.10.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.10.0
2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.10.0 All 48 releases
thinkrank / includes / api / class-social-platforms-endpoint.php

class-social-platforms-endpoint.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.10.0, at includes/api/class-social-platforms-endpoint.php

325 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Social Platforms API Endpoints Class
4 *
5 * Provides REST API endpoints for social platform verification codes and IDs
6 * with selective encryption for sensitive verification codes.
7 *
8 * @package ThinkRank\API
9 * @since 1.0.0
10 */
11
12 declare(strict_types=1);
13
14 namespace ThinkRank\API;
15
16 use WP_REST_Controller;
17 use WP_REST_Request;
18 use WP_REST_Response;
19 use WP_Error;
20 use ThinkRank\Core\Settings;
21
22 // Prevent direct access
23 if (!defined('ABSPATH')) {
24 exit;
25 }
26
27 /**
28 * Social Platforms API Endpoints Class
29 *
30 * Handles social platform verification codes and IDs with selective encryption.
31 * Encrypts sensitive verification codes while keeping public IDs visible.
32 *
33 * @since 1.0.0
34 */
35 class Social_Platforms_Endpoint extends WP_REST_Controller {
36
37 /**
38 * API namespace
39 *
40 * @since 1.0.0
41 * @var string
42 */
43 protected $namespace = 'thinkrank/v1';
44
45 /**
46 * REST base
47 *
48 * @since 1.0.0
49 * @var string
50 */
51 protected $rest_base = 'social-platforms';
52
53 /**
54 * Settings instance
55 *
56 * @since 1.0.0
57 * @var Settings
58 */
59 private Settings $settings;
60
61 /**
62 * Sensitive keys that should be encrypted
63 *
64 * @since 1.0.0
65 * @var array
66 */
67 private array $sensitive_keys = [
68 'pinterest_site_verification',
69 'instagram_verification',
70 'tiktok_verification'
71 ];
72
73 /**
74 * Public keys that remain visible
75 *
76 * @since 1.0.0
77 * @var array
78 */
79 private array $public_keys = [
80 'facebook_app_id',
81 'facebook_admins',
82 'youtube_channel_id',
83 'whatsapp_business_id'
84 ];
85
86 /**
87 * Constructor
88 *
89 * @since 1.0.0
90 */
91 public function __construct() {
92 $this->settings = new Settings();
93 }
94
95 /**
96 * Register API routes
97 *
98 * @since 1.0.0
99 */
100 public function register_routes(): void {
101 // Social platform settings management
102 register_rest_route(
103 $this->namespace,
104 '/' . $this->rest_base . '/settings',
105 [
106 [
107 'methods' => 'GET',
108 'callback' => [$this, 'get_settings'],
109 'permission_callback' => [$this, 'check_read_permissions']
110 ],
111 [
112 'methods' => 'POST',
113 'callback' => [$this, 'update_settings'],
114 'permission_callback' => [$this, 'check_manage_permissions'],
115 'args' => $this->get_settings_args()
116 ]
117 ]
118 );
119 }
120
121 /**
122 * Get social platform settings
123 *
124 * @since 1.0.0
125 *
126 * @param WP_REST_Request $request Request object
127 * @return WP_REST_Response Response object
128 */
129 public function get_settings(WP_REST_Request $request): WP_REST_Response {
130 try {
131 $settings = $this->get_social_platform_settings();
132
133 return new WP_REST_Response([
134 'success' => true,
135 'data' => [
136 'settings' => $settings
137 ],
138 'message' => 'Social platform settings retrieved successfully'
139 ], 200);
140
141 } catch (\Exception $e) {
142 return new WP_REST_Response([
143 'success' => false,
144 'message' => 'Failed to retrieve social platform settings: ' . $e->getMessage()
145 ], 500);
146 }
147 }
148
149 /**
150 * Update social platform settings
151 *
152 * @since 1.0.0
153 *
154 * @param WP_REST_Request $request Request object
155 * @return WP_REST_Response Response object
156 */
157 public function update_settings(WP_REST_Request $request): WP_REST_Response {
158 try {
159 $settings = $request->get_param('settings');
160
161 if (empty($settings) || !is_array($settings)) {
162 return new WP_REST_Response([
163 'success' => false,
164 'message' => 'Invalid settings data provided'
165 ], 400);
166 }
167
168 // Sanitize and save settings
169 $sanitized_settings = $this->sanitize_settings($settings);
170 $success = $this->save_social_platform_settings($sanitized_settings);
171
172 if ($success) {
173 return new WP_REST_Response([
174 'success' => true,
175 'data' => [
176 'settings' => $this->get_social_platform_settings()
177 ],
178 'message' => 'Social platform settings saved successfully'
179 ], 200);
180 } else {
181 return new WP_REST_Response([
182 'success' => false,
183 'message' => 'Failed to save social platform settings'
184 ], 500);
185 }
186
187 } catch (\Exception $e) {
188 return new WP_REST_Response([
189 'success' => false,
190 'message' => 'Failed to update social platform settings: ' . $e->getMessage()
191 ], 500);
192 }
193 }
194
195 /**
196 * Get social platform settings from Settings class
197 *
198 * @since 1.0.0
199 * @return array Settings array
200 */
201 private function get_social_platform_settings(): array {
202 $settings = [];
203
204 // Get public IDs (visible)
205 foreach ($this->public_keys as $key) {
206 $settings[$key] = $this->settings->get($key, '');
207 }
208
209 // Get sensitive verification codes (encrypted, masked for display)
210 foreach ($this->sensitive_keys as $key) {
211 $value = $this->settings->get($key, '');
212 $settings[$key] = $this->mask_verification_code($value);
213 }
214
215 return $settings;
216 }
217
218 /**
219 * Save social platform settings using Settings class
220 *
221 * @since 1.0.0
222 * @param array $settings Settings to save
223 * @return bool Success status
224 */
225 private function save_social_platform_settings(array $settings): bool {
226 $success = true;
227
228 // Save each setting individually using the Settings class
229 // This ensures proper encryption for sensitive verification codes
230 foreach ($settings as $key => $value) {
231 // Only save if the key is in our allowed lists and has a value
232 if (in_array($key, array_merge($this->public_keys, $this->sensitive_keys)) && !empty($value)) {
233 if (!$this->settings->set($key, $value)) {
234 $success = false;
235 }
236 }
237 }
238
239 return $success;
240 }
241
242 /**
243 * Sanitize settings data
244 *
245 * @since 1.0.0
246 * @param array $settings Raw settings
247 * @return array Sanitized settings
248 */
249 private function sanitize_settings(array $settings): array {
250 $sanitized = [];
251
252 // Sanitize public IDs (only if not empty)
253 foreach ($this->public_keys as $key) {
254 if (!empty($settings[$key])) {
255 $sanitized[$key] = sanitize_text_field($settings[$key]);
256 }
257 }
258
259 // Sanitize sensitive verification codes (only if not empty)
260 foreach ($this->sensitive_keys as $key) {
261 if (!empty($settings[$key])) {
262 $sanitized[$key] = sanitize_text_field($settings[$key]);
263 }
264 }
265
266 return $sanitized;
267 }
268
269 /**
270 * Mask verification code for security display (XXX pattern)
271 *
272 * @since 1.0.0
273 * @param string $code Verification code to mask
274 * @return string Masked code or empty string
275 */
276 private function mask_verification_code(string $code): string {
277 if (empty($code)) {
278 return '';
279 }
280
281 // Show first 4 characters + XXXX suffix (like placeholders)
282 if (strlen($code) > 8) {
283 return substr($code, 0, 4) . 'XXXX';
284 }
285
286 return 'XXXX';
287 }
288
289 /**
290 * Get settings arguments for REST API
291 *
292 * @since 1.0.0
293 * @return array Settings arguments
294 */
295 private function get_settings_args(): array {
296 return [
297 'settings' => [
298 'required' => true,
299 'type' => 'object',
300 'description' => 'Social platform settings object'
301 ]
302 ];
303 }
304
305 /**
306 * Check read permissions
307 *
308 * @since 1.0.0
309 * @return bool Permission status
310 */
311 public function check_read_permissions(): bool {
312 return current_user_can('manage_options');
313 }
314
315 /**
316 * Check manage permissions
317 *
318 * @since 1.0.0
319 * @return bool Permission status
320 */
321 public function check_manage_permissions(): bool {
322 return current_user_can('manage_options');
323 }
324 }
325