PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 1.27.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v1.27.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-image-seo-settings.php

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

164 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 image SEO 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\Image_SEO_Manager;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Updates ThinkRank image SEO settings.
21 *
22 * Controls automatic alt/title injection and the token-based formats used when
23 * generating those attributes, persisted through the Image_SEO_Manager.
24 */
25 class Update_Image_Seo_Settings extends Ability_Base {
26 /**
27 * Boolean-typed image SEO keys.
28 */
29 private const BOOL_KEYS = [
30 'add_missing_alt',
31 'add_missing_title',
32 'save_alt_to_media',
33 'auto_fill_on_upload',
34 'media_alt_overwrite',
35 ];
36
37 /**
38 * String-typed image SEO keys.
39 */
40 private const STRING_KEYS = [
41 'alt_format',
42 'title_format',
43 ];
44
45 /**
46 * Constructor.
47 */
48 public function __construct() {
49 $this->id = 'thinkrank/update-image-seo-settings';
50 $this->label = __( 'Update ThinkRank Image SEO Settings', 'thinkrank' );
51 $this->description = __( 'Update ThinkRank image SEO settings, which control automatic alt/title injection, the token-based formats used to generate them, and whether generated alt text is persisted to the Media Library.', 'thinkrank' );
52 }
53
54 /**
55 * {@inheritDoc}
56 *
57 * @return array<string, bool|float|string>
58 */
59 public function get_annotations() {
60 return [
61 'readonly' => false,
62 'destructive' => true,
63 'idempotent' => true,
64 'priority' => 2.0,
65 'openWorldHint' => false,
66 ];
67 }
68
69 /**
70 * {@inheritDoc}
71 *
72 * @return array<string, mixed>
73 */
74 public function get_input_schema() {
75 return [
76 'type' => 'object',
77 'additionalProperties' => false,
78 'properties' => [
79 'settings' => [
80 'type' => 'object',
81 'description' => __( 'Image SEO settings to update.', 'thinkrank' ),
82 'properties' => [
83 'add_missing_alt' => [ 'type' => 'boolean' ],
84 'alt_format' => [ 'type' => 'string' ],
85 'add_missing_title' => [ 'type' => 'boolean' ],
86 'title_format' => [ 'type' => 'string' ],
87 'save_alt_to_media' => [ 'type' => 'boolean' ],
88 'auto_fill_on_upload' => [ 'type' => 'boolean' ],
89 'media_alt_overwrite' => [ 'type' => 'boolean' ],
90 ],
91 ],
92 ],
93 'required' => [ 'settings' ],
94 ];
95 }
96
97 /**
98 * {@inheritDoc}
99 *
100 * @return array<string, mixed>
101 */
102 public function get_output_schema() {
103 return [
104 'type' => 'object',
105 'properties' => [
106 'success' => [ 'type' => 'boolean' ],
107 'message' => [ 'type' => 'string' ],
108 ],
109 ];
110 }
111
112 /**
113 * Execute ability.
114 *
115 * @param array<string, mixed> $input Ability input payload.
116 * @return array<string, mixed>|\WP_Error
117 */
118 public function execute( $input ) {
119 $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : [];
120
121 if ( empty( $settings ) ) {
122 return new \WP_Error(
123 'thinkrank_missing_image_seo_settings_payload',
124 __( 'An image SEO settings payload is required.', 'thinkrank' ),
125 [ 'status' => 400 ]
126 );
127 }
128
129 $mgr = new Image_SEO_Manager();
130 $merged = $mgr->get_settings( 'site', null );
131 $found = false;
132
133 foreach ( self::BOOL_KEYS as $key ) {
134 if ( array_key_exists( $key, $settings ) ) {
135 $merged[ $key ] = (bool) $settings[ $key ];
136 $found = true;
137 }
138 }
139 foreach ( self::STRING_KEYS as $key ) {
140 if ( array_key_exists( $key, $settings ) ) {
141 $merged[ $key ] = sanitize_text_field( (string) $settings[ $key ] );
142 $found = true;
143 }
144 }
145
146 if ( ! $found ) {
147 return new \WP_Error(
148 'thinkrank_no_valid_image_seo_setting_keys',
149 __( 'No valid image SEO setting keys were provided.', 'thinkrank' ),
150 [ 'status' => 400 ]
151 );
152 }
153
154 $result = $mgr->save_settings( 'site', null, $merged );
155
156 return [
157 'success' => (bool) $result,
158 'message' => (bool) $result
159 ? __( 'Image SEO settings updated.', 'thinkrank' )
160 : __( 'Failed to update image SEO settings.', 'thinkrank' ),
161 ];
162 }
163 }
164