PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.1.1
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.1.1
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-site-identity-settings.php

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

147 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update site identity settings 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 ThinkRank site identity settings.
21 *
22 * Only the non-robots identity keys are writable here. The robots.txt keys
23 * (robots_txt_enabled, allow_search_engines, robots_txt_content) are never
24 * touched so this ability cannot clobber the robots.txt configuration.
25 */
26 class Update_Site_Identity_Settings extends Ability_Base {
27 /**
28 * Constructor.
29 */
30 public function __construct() {
31 $this->id = 'thinkrank/update-site-identity-settings';
32 $this->label = __( 'Update ThinkRank Site Identity Settings', 'thinkrank' );
33 $this->description = __( 'Update ThinkRank site identity settings: the homepage/category/tag/author/search/archive title templates, site name and description, breadcrumb configuration, brand imagery, homepage hero, and the business details behind LocalBusiness schema. Robots.txt contents and schema toggles are never modified by this ability. Read the current values with get-site-identity-settings first; only the keys you pass are changed.', 'thinkrank' );
34 }
35
36 /**
37 * {@inheritDoc}
38 *
39 * @return array<string, bool|float|string>
40 */
41 public function get_annotations() {
42 return [
43 'readonly' => false,
44 'destructive' => true,
45 'idempotent' => true,
46 'priority' => 2.0,
47 'openWorldHint' => false,
48 ];
49 }
50
51 /**
52 * Build the JSON schema properties for site identity settings.
53 *
54 * @return array<string, mixed>
55 */
56 private static function schema_properties() {
57 return Settings_Key_Map::site_identity();
58 }
59
60 /**
61 * {@inheritDoc}
62 *
63 * @return array<string, mixed>
64 */
65 public function get_input_schema() {
66 return [
67 'type' => 'object',
68 'additionalProperties' => false,
69 'properties' => [
70 'settings' => [
71 'type' => 'object',
72 'description' => __( 'Site identity settings to update.', 'thinkrank' ),
73 'properties' => self::schema_properties(),
74 ],
75 ],
76 'required' => [ 'settings' ],
77 ];
78 }
79
80 /**
81 * {@inheritDoc}
82 *
83 * @return array<string, mixed>
84 */
85 public function get_output_schema() {
86 return [
87 'type' => 'object',
88 'properties' => [
89 'success' => [ 'type' => 'boolean' ],
90 'message' => [ 'type' => 'string' ],
91 ],
92 ];
93 }
94
95 /**
96 * Execute ability.
97 *
98 * @param array<string, mixed> $input Ability input payload.
99 * @return array<string, mixed>|\WP_Error
100 */
101 public function execute( $input ) {
102 $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : [];
103
104 if ( empty( $settings ) ) {
105 return new \WP_Error(
106 'thinkrank_missing_site_identity_settings_payload',
107 __( 'A site identity settings payload is required.', 'thinkrank' ),
108 [ 'status' => 400 ]
109 );
110 }
111
112 $mgr = new Site_Identity_Manager();
113 $merged = $mgr->get_settings( 'site', null );
114 $patch = Settings_Key_Map::coerce( Settings_Key_Map::site_identity(), $settings );
115 $merged = array_merge( $merged, $patch );
116
117 if ( empty( $patch ) ) {
118 return new \WP_Error(
119 'thinkrank_no_valid_site_identity_setting_keys',
120 __( 'No valid site identity setting keys were provided.', 'thinkrank' ),
121 [ 'status' => 400 ]
122 );
123 }
124
125 $result = $mgr->save_settings( 'site', null, $merged );
126
127 if ( $result ) {
128 return [
129 'success' => true,
130 'message' => __( 'Site identity settings updated.', 'thinkrank' ),
131 ];
132 }
133
134 // The manager recorded why the save failed; pass it on rather than
135 // leaving the caller with a fixed string it cannot act on.
136 $reason = $mgr->get_last_save_error();
137
138 return [
139 'success' => false,
140 'message' => '' !== $reason
141 /* translators: %s: reason the save failed. */
142 ? sprintf( __( 'Failed to update site identity settings: %s', 'thinkrank' ), $reason )
143 : __( 'Failed to update site identity settings.', 'thinkrank' ),
144 ];
145 }
146 }
147