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-pillar-content-endpoint.php

class-pillar-content-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-pillar-content-endpoint.php

174 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Pillar Content API Endpoint Class
5 *
6 * REST API endpoints for fetching pillar content suggestions.
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 WP_REST_Controller;
18 use WP_REST_Request;
19 use WP_REST_Response;
20 use WP_Query;
21 use WP_Error;
22
23 /**
24 * Pillar Content API Endpoint Class
25 *
26 * @since 1.0.0
27 */
28 class Pillar_Content_Endpoint extends WP_REST_Controller {
29
30 /**
31 * API namespace
32 *
33 * @var string
34 */
35 protected $namespace = 'thinkrank/v1';
36
37 /**
38 * API resource base
39 *
40 * @var string
41 */
42 protected $rest_base = 'pillar-content';
43
44 /**
45 * Register API routes
46 *
47 * @return void
48 */
49 public function register_routes(): void {
50 register_rest_route(
51 $this->namespace,
52 '/' . $this->rest_base . '/suggestions',
53 [
54 [
55 'methods' => 'GET',
56 'callback' => [$this, 'get_suggestions'],
57 'permission_callback' => [$this, 'check_read_permissions'],
58 'args' => [
59 'post_id' => [
60 'required' => true,
61 'type' => 'integer',
62 'validate_callback' => function ($param, $request, $key) {
63 return is_numeric($param);
64 },
65 'sanitize_callback' => 'absint',
66 ],
67 ],
68 ]
69 ]
70 );
71 }
72
73 /**
74 * Get pillar content suggestions
75 *
76 * @param WP_REST_Request $request Request object
77 * @return WP_REST_Response|WP_Error Response object
78 */
79 public function get_suggestions(WP_REST_Request $request) {
80 $post_id = $request->get_param('post_id');
81
82 if (!$post_id) {
83 return new WP_Error(
84 'missing_post_id',
85 __('Post ID is required', 'thinkrank'),
86 ['status' => 400]
87 );
88 }
89
90 $post_type = get_post_type($post_id);
91 if (!$post_type) {
92 return new WP_Error(
93 'invalid_post_id',
94 __('Invalid Post ID', 'thinkrank'),
95 ['status' => 400]
96 );
97 }
98
99 // Check global settings for this post type
100 $all_settings = get_option('thinkrank_global_seo_settings', []);
101 $link_suggestions_enabled = true; // Default to true
102
103 if (isset($all_settings[$post_type]['link_suggestions'])) {
104 $link_suggestions_enabled = (bool) $all_settings[$post_type]['link_suggestions'];
105 }
106
107 if (!$link_suggestions_enabled) {
108 return new WP_REST_Response([], 200);
109 }
110
111 $taxonomies = get_object_taxonomies($post_type);
112 $tax_query = [];
113
114 if (!empty($taxonomies)) {
115 $tax_query['relation'] = 'OR';
116
117 foreach ($taxonomies as $taxonomy) {
118 $terms = get_the_terms($post_id, $taxonomy);
119 if ($terms && !is_wp_error($terms)) {
120 $term_ids = wp_list_pluck($terms, 'term_id');
121 if (!empty($term_ids)) {
122 $tax_query[] = [
123 'taxonomy' => $taxonomy,
124 'field' => 'term_id',
125 'terms' => $term_ids,
126 ];
127 $has_terms = true;
128 }
129 }
130 }
131 }
132
133 $args = [
134 'post_type' => $post_type,
135 'posts_per_page' => 5,
136 'post__not_in' => [$post_id],
137 'meta_query' => [
138 [
139 'key' => '_thinkrank_pillar_content',
140 'value' => '1',
141 'compare' => '=',
142 ]
143 ],
144 'tax_query' => $tax_query,
145 'fields' => 'ids'
146 ];
147
148 $query = new WP_Query($args);
149 $suggestions = [];
150
151 if ($query->have_posts()) {
152 foreach ($query->posts as $p_id) {
153 $suggestions[] = [
154 'id' => $p_id,
155 'title' => get_the_title($p_id),
156 'permalink' => get_permalink($p_id),
157 'type' => get_post_type($p_id)
158 ];
159 }
160 }
161
162 return new WP_REST_Response($suggestions, 200);
163 }
164
165 /**
166 * Check if user can read posts
167 *
168 * @return bool
169 */
170 public function check_read_permissions() {
171 return current_user_can('edit_posts');
172 }
173 }
174