| 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 |
'auto_generate_descriptions', |
| 39 |
'fallback_to_excerpt', |
| 40 |
'strip_html_tags', |
| 41 |
'image_optimization', |
| 42 |
'auto_generate', |
| 43 |
]; |
| 44 |
|
| 45 |
/** |
| 46 |
* Integer-typed social meta keys. |
| 47 |
*/ |
| 48 |
private const INT_KEYS = [ |
| 49 |
'og_image_width', |
| 50 |
'og_image_height', |
| 51 |
'max_description_length', |
| 52 |
]; |
| 53 |
|
| 54 |
/** |
| 55 |
* String-typed social meta keys. |
| 56 |
*/ |
| 57 |
private const STRING_KEYS = [ |
| 58 |
'og_site_name', |
| 59 |
'og_description', |
| 60 |
'og_type', |
| 61 |
'og_locale', |
| 62 |
'default_og_image', |
| 63 |
'twitter_username', |
| 64 |
'twitter_creator', |
| 65 |
'twitter_card_type', |
| 66 |
'default_twitter_image', |
| 67 |
'twitter_site', |
| 68 |
'default_image', |
| 69 |
'facebook_app_id', |
| 70 |
'facebook_admins', |
| 71 |
'pinterest_site_verification', |
| 72 |
'instagram_verification', |
| 73 |
'tiktok_verification', |
| 74 |
'youtube_channel_id', |
| 75 |
'whatsapp_business_id', |
| 76 |
]; |
| 77 |
|
| 78 |
/** |
| 79 |
* Constructor. |
| 80 |
*/ |
| 81 |
public function __construct() { |
| 82 |
$this->id = 'thinkrank/update-social-meta-settings'; |
| 83 |
$this->label = __( 'Update ThinkRank Social Meta Settings', 'thinkrank' ); |
| 84 |
$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' ); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* {@inheritDoc} |
| 89 |
* |
| 90 |
* @return array<string, bool|float|string> |
| 91 |
*/ |
| 92 |
public function get_annotations() { |
| 93 |
return [ |
| 94 |
'readonly' => false, |
| 95 |
'destructive' => true, |
| 96 |
'idempotent' => true, |
| 97 |
'priority' => 2.0, |
| 98 |
'openWorldHint' => false, |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Build the JSON schema properties for social meta settings. |
| 104 |
* |
| 105 |
* @return array<string, mixed> |
| 106 |
*/ |
| 107 |
private static function schema_properties() { |
| 108 |
$props = []; |
| 109 |
|
| 110 |
foreach ( self::BOOL_KEYS as $key ) { |
| 111 |
$props[ $key ] = [ 'type' => 'boolean' ]; |
| 112 |
} |
| 113 |
foreach ( self::INT_KEYS as $key ) { |
| 114 |
$props[ $key ] = [ 'type' => 'integer' ]; |
| 115 |
} |
| 116 |
foreach ( self::STRING_KEYS as $key ) { |
| 117 |
$props[ $key ] = [ 'type' => 'string' ]; |
| 118 |
} |
| 119 |
|
| 120 |
return $props; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* {@inheritDoc} |
| 125 |
* |
| 126 |
* @return array<string, mixed> |
| 127 |
*/ |
| 128 |
public function get_input_schema() { |
| 129 |
return [ |
| 130 |
'type' => 'object', |
| 131 |
'additionalProperties' => false, |
| 132 |
'properties' => [ |
| 133 |
'settings' => [ |
| 134 |
'type' => 'object', |
| 135 |
'description' => __( 'Social meta settings to update.', 'thinkrank' ), |
| 136 |
'properties' => self::schema_properties(), |
| 137 |
], |
| 138 |
], |
| 139 |
'required' => [ 'settings' ], |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* {@inheritDoc} |
| 145 |
* |
| 146 |
* @return array<string, mixed> |
| 147 |
*/ |
| 148 |
public function get_output_schema() { |
| 149 |
return [ |
| 150 |
'type' => 'object', |
| 151 |
'properties' => [ |
| 152 |
'success' => [ 'type' => 'boolean' ], |
| 153 |
'message' => [ 'type' => 'string' ], |
| 154 |
], |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Execute ability. |
| 160 |
* |
| 161 |
* @param array<string, mixed> $input Ability input payload. |
| 162 |
* @return array<string, mixed>|\WP_Error |
| 163 |
*/ |
| 164 |
public function execute( $input ) { |
| 165 |
$settings = isset( $input['settings'] ) && is_array( $input['settings'] ) ? $input['settings'] : []; |
| 166 |
|
| 167 |
if ( empty( $settings ) ) { |
| 168 |
return new \WP_Error( |
| 169 |
'thinkrank_missing_social_meta_settings_payload', |
| 170 |
__( 'A social meta settings payload is required.', 'thinkrank' ), |
| 171 |
[ 'status' => 400 ] |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
$mgr = new Social_Meta_Manager(); |
| 176 |
$merged = $mgr->get_settings( 'site', null ); |
| 177 |
$found = false; |
| 178 |
|
| 179 |
foreach ( self::BOOL_KEYS as $key ) { |
| 180 |
if ( array_key_exists( $key, $settings ) ) { |
| 181 |
$merged[ $key ] = (bool) $settings[ $key ]; |
| 182 |
$found = true; |
| 183 |
} |
| 184 |
} |
| 185 |
foreach ( self::INT_KEYS as $key ) { |
| 186 |
if ( array_key_exists( $key, $settings ) ) { |
| 187 |
$merged[ $key ] = (int) $settings[ $key ]; |
| 188 |
$found = true; |
| 189 |
} |
| 190 |
} |
| 191 |
foreach ( self::STRING_KEYS as $key ) { |
| 192 |
if ( array_key_exists( $key, $settings ) ) { |
| 193 |
$merged[ $key ] = sanitize_text_field( (string) $settings[ $key ] ); |
| 194 |
$found = true; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
if ( ! $found ) { |
| 199 |
return new \WP_Error( |
| 200 |
'thinkrank_no_valid_social_meta_setting_keys', |
| 201 |
__( 'No valid social meta setting keys were provided.', 'thinkrank' ), |
| 202 |
[ 'status' => 400 ] |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
$result = $mgr->save_settings( 'site', null, $merged ); |
| 207 |
|
| 208 |
return [ |
| 209 |
'success' => (bool) $result, |
| 210 |
'message' => (bool) $result |
| 211 |
? __( 'Social meta settings updated.', 'thinkrank' ) |
| 212 |
: __( 'Failed to update social meta settings.', 'thinkrank' ), |
| 213 |
]; |
| 214 |
} |
| 215 |
} |
| 216 |
|