PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.25.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.25.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 / abilities / analysis / class-get-post-seo-checks.php

class-get-post-seo-checks.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 1.25.0, at includes/abilities/analysis/class-get-post-seo-checks.php

140 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Get post SEO checks ability.
4 *
5 * @package ThinkRank\Abilities\Analysis
6 */
7
8 declare(strict_types=1);
9
10 namespace ThinkRank\Abilities\Analysis;
11
12 use ThinkRank\Abilities\Ability_Base;
13 use ThinkRank\Admin\Metabox_Manager;
14 use ThinkRank\AI\SEOScoreCalculator;
15 use ThinkRank\Core\Database;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // Exit if accessed directly.
19 }
20
21 /**
22 * Runs ThinkRank's SEO score analysis for one or more posts.
23 */
24 class Get_Post_Seo_Checks extends Ability_Base {
25 /**
26 * Constructor.
27 */
28 public function __construct() {
29 $this->id = 'thinkrank/get-post-seo-checks';
30 $this->label = __( 'Get ThinkRank Post SEO Checks', 'thinkrank' );
31 $this->description = __( 'Run ThinkRank SEO score analysis for one or more posts, pages, or custom post type items.', 'thinkrank' );
32 }
33
34 /**
35 * {@inheritDoc}
36 *
37 * @return array<string, bool|float|string>
38 */
39 public function get_annotations() {
40 return [
41 'readonly' => true,
42 'destructive' => false,
43 'idempotent' => true,
44 'priority' => 1.0,
45 'openWorldHint' => false,
46 ];
47 }
48
49 /**
50 * {@inheritDoc}
51 *
52 * @return array<string, mixed>
53 */
54 public function get_input_schema() {
55 return [
56 'type' => 'object',
57 'additionalProperties' => false,
58 'properties' => [
59 'post_ids' => [
60 'type' => 'array',
61 'description' => __( 'One or more post IDs to inspect.', 'thinkrank' ),
62 'items' => [
63 'type' => 'integer',
64 ],
65 ],
66 ],
67 'required' => [ 'post_ids' ],
68 ];
69 }
70
71 /**
72 * {@inheritDoc}
73 *
74 * @return array<string, mixed>
75 */
76 public function get_output_schema() {
77 return [
78 'type' => 'object',
79 'properties' => [
80 'status' => [ 'type' => 'string' ],
81 'message' => [ 'type' => 'string' ],
82 'data' => [ 'type' => 'array' ],
83 ],
84 ];
85 }
86
87 /**
88 * Execute ability.
89 *
90 * @param array<string, mixed> $input Ability input payload.
91 * @return array<string, mixed>|\WP_Error
92 */
93 public function execute( $input ) {
94 $post_ids = isset( $input['post_ids'] ) && is_array( $input['post_ids'] ) ? array_map( 'absint', $input['post_ids'] ) : [];
95 $post_ids = array_values(
96 array_filter(
97 $post_ids,
98 static function ( $post_id ) {
99 return $post_id > 0 && get_post( $post_id ) instanceof \WP_Post;
100 }
101 )
102 );
103
104 if ( empty( $post_ids ) ) {
105 return new \WP_Error(
106 'thinkrank_missing_post_ids',
107 __( 'At least one valid post ID is required.', 'thinkrank' ),
108 [ 'status' => 400 ]
109 );
110 }
111
112 $calculator = new SEOScoreCalculator( new Database() );
113 $metabox = new Metabox_Manager();
114 $data = [];
115
116 foreach ( $post_ids as $post_id ) {
117 try {
118 $content = $calculator->analyze_post_content( $post_id );
119 $score = $calculator->calculate_score( $content, $metabox->get_post_metadata( $post_id ) );
120
121 $data[] = [
122 'post_id' => $post_id,
123 'score' => $score,
124 ];
125 } catch ( \Throwable $e ) {
126 $data[] = [
127 'post_id' => $post_id,
128 'error' => $e->getMessage(),
129 ];
130 }
131 }
132
133 return [
134 'status' => 'success',
135 'message' => __( 'SEO checks generated.', 'thinkrank' ),
136 'data' => $data,
137 ];
138 }
139 }
140