PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.27.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.27.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.27.0, at includes/api/class-author-archives-endpoint.php

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