| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update robots 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 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly. |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Updates ThinkRank site-wide default robots meta settings. |
| 20 |
* |
| 21 |
* These flags form the base robots meta directives applied to the whole site, |
| 22 |
* before any post-type or per-post overrides. |
| 23 |
*/ |
| 24 |
class Update_Robots_Meta_Settings extends Ability_Base { |
| 25 |
/** |
| 26 |
* Option name that stores the global robots meta settings. |
| 27 |
*/ |
| 28 |
private const OPTION = 'thinkrank_global_robot_meta_settings'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Default robots meta directives. |
| 32 |
*/ |
| 33 |
private const DEFAULTS = [ |
| 34 |
'index' => true, |
| 35 |
'noindex' => false, |
| 36 |
'nofollow' => false, |
| 37 |
'noarchive' => false, |
| 38 |
'noimageindex' => false, |
| 39 |
'nosnippet' => false, |
| 40 |
]; |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
*/ |
| 45 |
public function __construct() { |
| 46 |
$this->id = 'thinkrank/update-robots-meta-settings'; |
| 47 |
$this->label = __( 'Update ThinkRank Robots Meta Settings', 'thinkrank' ); |
| 48 |
$this->description = __( 'Update ThinkRank site-wide default robots meta directives (index, noindex, nofollow, noarchive, noimageindex, nosnippet). Read the current values with get-robots-meta-settings first.', 'thinkrank' ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritDoc} |
| 53 |
* |
| 54 |
* @return array<string, bool|float|string> |
| 55 |
*/ |
| 56 |
public function get_annotations() { |
| 57 |
return [ |
| 58 |
'readonly' => false, |
| 59 |
'destructive' => true, |
| 60 |
'idempotent' => true, |
| 61 |
'priority' => 2.0, |
| 62 |
'openWorldHint' => false, |
| 63 |
]; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* {@inheritDoc} |
| 68 |
* |
| 69 |
* @return array<string, mixed> |
| 70 |
*/ |
| 71 |
public function get_input_schema() { |
| 72 |
return [ |
| 73 |
'type' => 'object', |
| 74 |
'additionalProperties' => false, |
| 75 |
'properties' => [ |
| 76 |
'settings' => [ |
| 77 |
'type' => 'object', |
| 78 |
'description' => __( 'Robots meta directives to update.', 'thinkrank' ), |
| 79 |
'properties' => [ |
| 80 |
'index' => [ 'type' => 'boolean' ], |
| 81 |
'noindex' => [ 'type' => 'boolean' ], |
| 82 |
'nofollow' => [ 'type' => 'boolean' ], |
| 83 |
'noarchive' => [ 'type' => 'boolean' ], |
| 84 |
'noimageindex' => [ 'type' => 'boolean' ], |
| 85 |
'nosnippet' => [ 'type' => 'boolean' ], |
| 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_robots_meta_settings_payload', |
| 120 |
__( 'A robots meta settings payload is required.', 'thinkrank' ), |
| 121 |
[ 'status' => 400 ] |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$merged = wp_parse_args( (array) get_option( self::OPTION, [] ), self::DEFAULTS ); |
| 126 |
$found = false; |
| 127 |
|
| 128 |
foreach ( array_keys( self::DEFAULTS ) as $key ) { |
| 129 |
if ( array_key_exists( $key, $settings ) ) { |
| 130 |
$merged[ $key ] = (bool) $settings[ $key ]; |
| 131 |
$found = true; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if ( ! $found ) { |
| 136 |
return new \WP_Error( |
| 137 |
'thinkrank_no_valid_robots_meta_setting_keys', |
| 138 |
__( 'No valid robots meta setting keys were provided.', 'thinkrank' ), |
| 139 |
[ 'status' => 400 ] |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
$result = update_option( self::OPTION, $merged ); |
| 144 |
|
| 145 |
return [ |
| 146 |
'success' => (bool) $result, |
| 147 |
'message' => (bool) $result |
| 148 |
? __( 'Robots meta settings updated.', 'thinkrank' ) |
| 149 |
: __( 'Robots meta settings were unchanged.', 'thinkrank' ), |
| 150 |
]; |
| 151 |
} |
| 152 |
} |
| 153 |
|