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

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

174 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Update author archives 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\Core\Settings;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Updates ThinkRank author archives settings.
21 *
22 * Controls whether author archives are enabled and indexable, whether empty
23 * archives are shown, and the SEO title/meta description templates.
24 */
25 class Update_Author_Archives_Settings extends Ability_Base {
26 /**
27 * Boolean settings: API alias => storage_key.
28 */
29 private const BOOL_MAP = [
30 'enabled' => 'author_archives_enabled',
31 'show_in_search_results' => 'author_archives_index',
32 'show_empty_archives' => 'author_archives_show_empty',
33 ];
34
35 /**
36 * String settings: API alias => storage_key.
37 */
38 private const STRING_MAP = [
39 'title' => 'author_archives_title',
40 'meta_description' => 'author_archives_meta_desc',
41 ];
42
43 /**
44 * Constructor.
45 */
46 public function __construct() {
47 $this->id = 'thinkrank/update-author-archives-settings';
48 $this->label = __( 'Update ThinkRank Author Archives Settings', 'thinkrank' );
49 $this->description = __( 'Update ThinkRank author archives settings: enable/index toggles, empty-archive visibility, and the SEO title/meta description templates.', 'thinkrank' );
50 }
51
52 /**
53 * {@inheritDoc}
54 *
55 * @return array<string, bool|float|string>
56 */
57 public function get_annotations() {
58 return [
59 'readonly' => false,
60 'destructive' => true,
61 'idempotent' => true,
62 'priority' => 2.0,
63 'openWorldHint' => false,
64 ];
65 }
66
67 /**
68 * {@inheritDoc}
69 *
70 * @return array<string, mixed>
71 */
72 public function get_input_schema() {
73 return [
74 'type' => 'object',
75 'additionalProperties' => false,
76 'properties' => [
77 'settings' => [
78 'type' => 'object',
79 'description' => __( 'Author archives settings to update.', 'thinkrank' ),
80 'properties' => [
81 'enabled' => [ 'type' => 'boolean' ],
82 'show_in_search_results' => [ 'type' => 'boolean' ],
83 'show_empty_archives' => [ 'type' => 'boolean' ],
84 'title' => [ 'type' => 'string' ],
85 'meta_description' => [ 'type' => 'string' ],
86 ],
87 ],
88 ],
89 'required' => [ 'settings' ],
90 ];
91 }
92
93 /**
94 * {@inheritDoc}
95 *
96 * @return array<string, mixed>
97 */
98 public function get_output_schema() {
99 return [
100 'type' => 'object',
101 'properties' => [
102 'success' => [ 'type' => 'boolean' ],
103 'message' => [ 'type' => 'string' ],
104 ],
105 ];
106 }
107
108 /**
109 * Execute ability.
110 *
111 * @param array<string, mixed> $input Ability input payload.
112 * @return array<string, mixed>|\WP_Error
113 */
114 public function execute( $input ) {
115 $settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : [];
116
117 if ( empty( $settings ) ) {
118 return new \WP_Error(
119 'thinkrank_missing_author_archives_settings_payload',
120 __( 'An author archives settings payload is required.', 'thinkrank' ),
121 [ 'status' => 400 ]
122 );
123 }
124
125 $store = Settings::instance();
126 $found = false;
127 $failed = [];
128
129 foreach ( self::BOOL_MAP as $alias => $storage_key ) {
130 if ( array_key_exists( $alias, $settings ) ) {
131 $found = true;
132 if ( ! $store->set( $storage_key, (bool) $settings[ $alias ] ) ) {
133 $failed[] = $alias;
134 }
135 }
136 }
137 foreach ( self::STRING_MAP as $alias => $storage_key ) {
138 if ( array_key_exists( $alias, $settings ) ) {
139 $found = true;
140 if ( ! $store->set( $storage_key, sanitize_text_field( (string) $settings[ $alias ] ) ) ) {
141 $failed[] = $alias;
142 }
143 }
144 }
145
146 if ( ! $found ) {
147 return new \WP_Error(
148 'thinkrank_no_valid_author_archives_setting_keys',
149 __( 'No valid author archives setting keys were provided.', 'thinkrank' ),
150 [ 'status' => 400 ]
151 );
152 }
153
154 // A discarded set() return meant a failed write still answered
155 // "settings updated"; surface it instead.
156 if ( ! empty( $failed ) ) {
157 return new \WP_Error(
158 'thinkrank_author_archives_settings_not_saved',
159 sprintf(
160 /* translators: %s is a comma-separated list of setting names that could not be saved. */
161 __( 'Some author archives settings could not be saved: %s', 'thinkrank' ),
162 implode( ', ', $failed )
163 ),
164 [ 'status' => 500 ]
165 );
166 }
167
168 return [
169 'success' => true,
170 'message' => __( 'Author archives settings updated.', 'thinkrank' ),
171 ];
172 }
173 }
174