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-author-archives-endpoint.php

class-author-archives-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-author-archives-endpoint.php

190 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Author Archives API Endpoint Class
5 *
6 * REST API endpoints for author archives management
7 *
8 * @package ThinkRank
9 * @subpackage API
10 * @since 1.0.0
11 */
12
13 declare(strict_types=1);
14
15 namespace ThinkRank\API;
16
17 use ThinkRank\API\Traits\CSRF_Protection;
18 use ThinkRank\Core\Settings;
19 use WP_REST_Controller;
20 use WP_REST_Request;
21 use WP_REST_Response;
22 use WP_Error;
23
24 // Load CSRF Protection trait
25 require_once THINKRANK_PLUGIN_DIR . 'includes/api/traits/trait-csrf-protection.php';
26
27 /**
28 * Author Archives API Endpoint Class
29 *
30 * @since 1.0.0
31 */
32 class Author_Archives_Endpoint extends WP_REST_Controller {
33 use CSRF_Protection;
34
35 /**
36 * API namespace
37 *
38 * @var string
39 */
40 protected $namespace = 'thinkrank/v1';
41
42 /**
43 * API resource base
44 *
45 * @var string
46 */
47 protected $rest_base = 'author-archives';
48
49 /**
50 * Register API routes
51 *
52 * @return void
53 */
54 public function register_routes(): void {
55 register_rest_route(
56 $this->namespace,
57 '/' . $this->rest_base . '/settings',
58 [
59 [
60 'methods' => 'GET',
61 'callback' => [$this, 'get_settings'],
62 'permission_callback' => [$this, 'check_permissions'],
63 ],
64 [
65 'methods' => 'POST',
66 'callback' => [$this, 'update_settings'],
67 'permission_callback' => [$this, 'check_permissions'],
68 'args' => [
69 'settings' => [
70 'required' => true,
71 'type' => 'object',
72 ],
73 ],
74 ],
75 ]
76 );
77 }
78
79 /**
80 * Get author archives settings
81 *
82 * @param WP_REST_Request $request Request object
83 * @return WP_REST_Response|WP_Error Response object
84 */
85 /**
86 * Get author archives settings
87 *
88 * @param WP_REST_Request $request Request object
89 * @return WP_REST_Response|WP_Error Response object
90 */
91 public function get_settings(WP_REST_Request $request) {
92 try {
93 $settings = new Settings();
94
95 return new WP_REST_Response([
96 'success' => true,
97 'data' => [
98 'enabled' => $settings->get('author_archives_enabled', true),
99 'show_in_search_results' => $settings->get('author_archives_index', true),
100 'show_empty_archives' => $settings->get('author_archives_show_empty', false),
101 'title' => $settings->get('author_archives_title', '%author_name% – %site_title% %page%'),
102 'meta_description' => $settings->get('author_archives_meta_desc', 'Articles written by %author_name% on %site_title%')
103 ],
104 ], 200);
105 } catch (\Exception $e) {
106 return new WP_Error(
107 'retrieval_failed',
108 'Failed to retrieve settings: ' . $e->getMessage(),
109 ['status' => 500]
110 );
111 }
112 }
113
114 /**
115 * Update author archives settings
116 *
117 * @param WP_REST_Request $request Request object
118 * @return WP_REST_Response|WP_Error Response object
119 */
120 public function update_settings(WP_REST_Request $request) {
121 try {
122 $params = $request->get_param('settings');
123
124 if (!is_array($params)) {
125 return new WP_Error(
126 'invalid_params',
127 'Invalid settings format',
128 ['status' => 400]
129 );
130 }
131
132 $settings = new Settings();
133 $success = true;
134
135 // Map and save settings
136 if (isset($params['enabled'])) {
137 $success = $settings->set('author_archives_enabled', (bool) $params['enabled']);
138 }
139 if (isset($params['show_in_search_results'])) {
140 $success = $settings->set('author_archives_index', (bool) $params['show_in_search_results']);
141 }
142 if (isset($params['show_empty_archives'])) {
143 $success = $settings->set('author_archives_show_empty', (bool) $params['show_empty_archives']);
144 }
145 if (isset($params['title'])) {
146 $success = $settings->set('author_archives_title', sanitize_text_field($params['title']));
147 }
148 if (isset($params['meta_description'])) {
149 $success = $settings->set('author_archives_meta_desc', sanitize_text_field($params['meta_description']));
150 }
151
152 if (!$success) {
153 return new WP_Error(
154 'update_failed',
155 'Failed to update settings',
156 ['status' => 500]
157 );
158 }
159
160 return new WP_REST_Response([
161 'success' => true,
162 'message' => 'Settings saved successfully',
163 'data' => $params // Return back what was sent for UI consistency
164 ], 200);
165 } catch (\Exception $e) {
166 return new WP_Error(
167 'update_failed',
168 'Failed to update settings: ' . $e->getMessage(),
169 ['status' => 500]
170 );
171 }
172 }
173
174 /**
175 * Check permissions
176 *
177 * @return bool|WP_Error
178 */
179 public function check_permissions() {
180 if (!current_user_can('manage_options')) {
181 return new WP_Error(
182 'rest_forbidden',
183 'You do not have permission to access this endpoint.',
184 ['status' => 403]
185 );
186 }
187 return true;
188 }
189 }
190