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-social-meta-settings.php

class-update-social-meta-settings.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-social-meta-settings.php

227 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update social meta 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\Social_Meta_Manager;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Updates ThinkRank social meta settings.
21 *
22 * Covers Open Graph, Twitter Cards, and per-network verification/handle
23 * configuration persisted through the Social_Meta_Manager SEO manager.
24 */
25 class Update_Social_Meta_Settings extends Ability_Base {
26 /**
27 * Boolean-typed social meta keys.
28 */
29 private const BOOL_KEYS = [
30 'enable_open_graph',
31 'enable_twitter_cards',
32 'enable_linkedin',
33 'enable_pinterest',
34 'enable_instagram',
35 'enable_tiktok',
36 'enable_youtube',
37 'enable_whatsapp',
38 'og_multiple_images',
39 'oembed_use_seo_title',
40 'oembed_use_social_image',
41 'oembed_remove_author',
42 'auto_generate_descriptions',
43 'fallback_to_excerpt',
44 'strip_html_tags',
45 'image_optimization',
46 'auto_generate',
47 ];
48
49 /**
50 * Integer-typed social meta keys.
51 */
52 private const INT_KEYS = [
53 'og_image_width',
54 'og_image_height',
55 'max_description_length',
56 ];
57
58 /**
59 * String-typed social meta keys.
60 */
61 private const STRING_KEYS = [
62 'og_site_name',
63 'og_description',
64 'og_type',
65 'og_locale',
66 'default_og_image',
67 'twitter_username',
68 'twitter_creator',
69 'twitter_card_type',
70 'default_twitter_image',
71 'twitter_site',
72 'default_image',
73 'facebook_app_id',
74 'facebook_admins',
75 'pinterest_site_verification',
76 'instagram_verification',
77 'tiktok_verification',
78 'youtube_channel_id',
79 'whatsapp_business_id',
80 ];
81
82 /**
83 * Constructor.
84 */
85 public function __construct() {
86 $this->id = 'thinkrank/update-social-meta-settings';
87 $this->label = __( 'Update ThinkRank Social Meta Settings', 'thinkrank' );
88 $this->description = __( 'Update ThinkRank social meta settings, covering Open Graph, Twitter Cards, and per-network verification and handle configuration. Read the current values with get-social-meta-settings first; only the keys you pass are changed.', 'thinkrank' );
89 }
90
91 /**
92 * {@inheritDoc}
93 *
94 * @return array<string, bool|float|string>
95 */
96 public function get_annotations() {
97 return [
98 'readonly' => false,
99 // Settings writes are recoverable: the matching get-* ability reads
100 // the previous value, so nothing is lost that cannot be put back.
101 // `destructive` is reserved for calls that lose data or reach
102 // outside the site, and marking routine configuration with it made
103 // MCP clients demand a human approval for every save — which users
104 // reported as a permission bug, because the client's refusal reads
105 // as "No approval received" (#675).
106 'destructive' => false,
107 'idempotent' => true,
108 'priority' => 2.0,
109 'openWorldHint' => false,
110 ];
111 }
112
113 /**
114 * Build the JSON schema properties for social meta settings.
115 *
116 * @return array<string, mixed>
117 */
118 private static function schema_properties() {
119 $props = [];
120
121 foreach ( self::BOOL_KEYS as $key ) {
122 $props[ $key ] = [ 'type' => 'boolean' ];
123 }
124 foreach ( self::INT_KEYS as $key ) {
125 $props[ $key ] = [ 'type' => 'integer' ];
126 }
127 foreach ( self::STRING_KEYS as $key ) {
128 $props[ $key ] = [ 'type' => 'string' ];
129 }
130
131 return $props;
132 }
133
134 /**
135 * {@inheritDoc}
136 *
137 * @return array<string, mixed>
138 */
139 public function get_input_schema() {
140 return [
141 'type' => 'object',
142 'additionalProperties' => false,
143 'properties' => [
144 'settings' => [
145 'type' => 'object',
146 'description' => __( 'Social meta settings to update.', 'thinkrank' ),
147 'properties' => self::schema_properties(),
148 ],
149 ],
150 'required' => [ 'settings' ],
151 ];
152 }
153
154 /**
155 * {@inheritDoc}
156 *
157 * @return array<string, mixed>
158 */
159 public function get_output_schema() {
160 return [
161 'type' => 'object',
162 'properties' => [
163 'success' => [ 'type' => 'boolean' ],
164 'message' => [ 'type' => 'string' ],
165 ],
166 ];
167 }
168
169 /**
170 * Execute ability.
171 *
172 * @param array<string, mixed> $input Ability input payload.
173 * @return array<string, mixed>|\WP_Error
174 */
175 public function execute( $input ) {
176 $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : [];
177
178 if ( empty( $settings ) ) {
179 return new \WP_Error(
180 'thinkrank_missing_social_meta_settings_payload',
181 __( 'A social meta settings payload is required.', 'thinkrank' ),
182 [ 'status' => 400 ]
183 );
184 }
185
186 $mgr = new Social_Meta_Manager();
187 $merged = $mgr->get_settings( 'site', null );
188 $found = false;
189
190 foreach ( self::BOOL_KEYS as $key ) {
191 if ( array_key_exists( $key, $settings ) ) {
192 $merged[ $key ] = (bool) $settings[ $key ];
193 $found = true;
194 }
195 }
196 foreach ( self::INT_KEYS as $key ) {
197 if ( array_key_exists( $key, $settings ) ) {
198 $merged[ $key ] = (int) $settings[ $key ];
199 $found = true;
200 }
201 }
202 foreach ( self::STRING_KEYS as $key ) {
203 if ( array_key_exists( $key, $settings ) ) {
204 $merged[ $key ] = sanitize_text_field( (string) $settings[ $key ] );
205 $found = true;
206 }
207 }
208
209 if ( ! $found ) {
210 return new \WP_Error(
211 'thinkrank_no_valid_social_meta_setting_keys',
212 __( 'No valid social meta setting keys were provided.', 'thinkrank' ),
213 [ 'status' => 400 ]
214 );
215 }
216
217 $result = $mgr->save_settings( 'site', null, $merged );
218
219 return [
220 'success' => (bool) $result,
221 'message' => (bool) $result
222 ? __( 'Social meta settings updated.', 'thinkrank' )
223 : __( 'Failed to update social meta settings.', 'thinkrank' ),
224 ];
225 }
226 }
227