PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.7.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.7.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 / settings / class-update-robots-txt.php

class-update-robots-txt.php in ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO 2.7.0, at includes/abilities/settings/class-update-robots-txt.php

120 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update robots.txt ability.
4 *
5 * @package ThinkRank\Abilities\Settings
6 */
7
8 declare(strict_types=1);
9
10 namespace ThinkRank\Abilities\Settings;
11
12 use ThinkRank\Abilities\Ability_Base;
13 use ThinkRank\SEO\Site_Identity_Manager;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Updates the custom robots.txt content managed by ThinkRank.
21 */
22 class Update_Robots_Txt extends Ability_Base {
23 /**
24 * Constructor.
25 */
26 public function __construct() {
27 $this->id = 'thinkrank/update-robots-txt';
28 $this->label = __( 'Update ThinkRank Robots.txt', 'thinkrank' );
29 $this->description = __( 'Replace the robots.txt served to crawlers with custom content. This overrides ThinkRank\'s auto-generated rules entirely, so fetch get-robots-txt first and edit its "rendered" output to avoid dropping the default wp-admin/sitemap directives. Pass an empty string to revert to the auto-generated defaults.', 'thinkrank' );
30 }
31
32 /**
33 * {@inheritDoc}
34 *
35 * @return array<string, bool|float|string>
36 */
37 public function get_annotations() {
38 return [
39 'readonly' => false,
40 // Settings writes are recoverable: the matching get-* ability reads
41 // the previous value, so nothing is lost that cannot be put back.
42 // `destructive` is reserved for calls that lose data or reach
43 // outside the site, and marking routine configuration with it made
44 // MCP clients demand a human approval for every save — which users
45 // reported as a permission bug, because the client's refusal reads
46 // as "No approval received" (#675).
47 'destructive' => false,
48 'idempotent' => true,
49 'priority' => 2.0,
50 'openWorldHint' => false,
51 ];
52 }
53
54 /**
55 * {@inheritDoc}
56 *
57 * @return array<string, mixed>
58 */
59 public function get_input_schema() {
60 return [
61 'type' => 'object',
62 'additionalProperties' => false,
63 'properties' => [
64 'content' => [
65 'type' => 'string',
66 'description' => __( 'The full robots.txt content to serve. Replaces the generated defaults entirely; seed it from get-robots-txt\'s "rendered" field so default directives are preserved. Pass an empty string to restore auto-generation.', 'thinkrank' ),
67 ],
68 ],
69 'required' => [ 'content' ],
70 ];
71 }
72
73 /**
74 * {@inheritDoc}
75 *
76 * @return array<string, mixed>
77 */
78 public function get_output_schema() {
79 return [
80 'type' => 'object',
81 'properties' => [
82 'success' => [ 'type' => 'boolean' ],
83 'content' => [ 'type' => 'string' ],
84 'rendered' => [
85 'type' => 'string',
86 'description' => __( 'The effective robots.txt now served to crawlers after this update.', 'thinkrank' ),
87 ],
88 ],
89 ];
90 }
91
92 /**
93 * Execute ability.
94 *
95 * @param array<string, mixed> $input Ability input payload.
96 * @return array<string, mixed>
97 */
98 public function execute( $input ) {
99 $content = isset( $input['content'] ) ? sanitize_textarea_field( (string) $input['content'] ) : '';
100
101 $mgr = new Site_Identity_Manager();
102 $updated = $mgr->get_settings( 'site', null );
103
104 $updated['robots_txt_content'] = $content;
105 $updated['robots_txt_enabled'] = true;
106
107 $result = $mgr->save_settings( 'site', null, $updated );
108
109 // Keep the physical robots.txt in step with the saved override so the
110 // change is served immediately, matching a Save from the admin screen.
111 $mgr->sync_robots_txt_file();
112
113 return [
114 'success' => (bool) $result,
115 'content' => $content,
116 'rendered' => (string) $mgr->get_effective_robots_txt()['content'],
117 ];
118 }
119 }
120