| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get global 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 |
* Retrieves ThinkRank global SEO settings. |
| 20 |
* |
| 21 |
* ThinkRank stores global SEO as per-post-type templates in the |
| 22 |
* `thinkrank_global_seo_settings` option, so this ability optionally filters |
| 23 |
* by a single post type. |
| 24 |
*/ |
| 25 |
class Get_Global_Settings extends Ability_Base { |
| 26 |
/** |
| 27 |
* Option storing per-post-type global SEO templates. |
| 28 |
*/ |
| 29 |
private const OPTION_NAME = 'thinkrank_global_seo_settings'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
$this->id = 'thinkrank/get-global-settings'; |
| 36 |
$this->label = __( 'Get ThinkRank Global Settings', 'thinkrank' ); |
| 37 |
$this->description = __( 'Retrieve ThinkRank global SEO settings. ThinkRank stores these as per-post-type templates; pass a post type to filter.', 'thinkrank' ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* {@inheritDoc} |
| 42 |
* |
| 43 |
* @return array<string, bool|float|string> |
| 44 |
*/ |
| 45 |
public function get_annotations() { |
| 46 |
return [ |
| 47 |
'readonly' => true, |
| 48 |
'destructive' => false, |
| 49 |
'idempotent' => true, |
| 50 |
'priority' => 1.0, |
| 51 |
'openWorldHint' => false, |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* {@inheritDoc} |
| 57 |
* |
| 58 |
* @return array<string, mixed> |
| 59 |
*/ |
| 60 |
public function get_input_schema() { |
| 61 |
return [ |
| 62 |
'type' => 'object', |
| 63 |
'additionalProperties' => false, |
| 64 |
'properties' => [ |
| 65 |
'post_type' => [ |
| 66 |
'type' => 'string', |
| 67 |
'description' => __( 'Optional post type slug to return settings for. Omit to return all post-type templates.', 'thinkrank' ), |
| 68 |
], |
| 69 |
], |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* {@inheritDoc} |
| 75 |
* |
| 76 |
* @return array<string, mixed> |
| 77 |
*/ |
| 78 |
public function get_output_schema() { |
| 79 |
return [ |
| 80 |
'type' => 'object', |
| 81 |
'properties' => [ |
| 82 |
'post_type' => [ 'type' => 'string' ], |
| 83 |
'settings' => [ 'type' => 'object' ], |
| 84 |
], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Execute ability. |
| 90 |
* |
| 91 |
* @param array<string, mixed> $input Ability input payload. |
| 92 |
* @return array<string, mixed> |
| 93 |
*/ |
| 94 |
public function execute( $input ) { |
| 95 |
$all = get_option( self::OPTION_NAME, [] ); |
| 96 |
if ( ! is_array( $all ) ) { |
| 97 |
$all = []; |
| 98 |
} |
| 99 |
|
| 100 |
$post_type = isset( $input['post_type'] ) ? sanitize_key( (string) $input['post_type'] ) : ''; |
| 101 |
|
| 102 |
if ( '' !== $post_type ) { |
| 103 |
$settings = isset( $all[ $post_type ] ) && is_array( $all[ $post_type ] ) ? $all[ $post_type ] : []; |
| 104 |
|
| 105 |
return [ |
| 106 |
'post_type' => $post_type, |
| 107 |
'settings' => $settings, |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
return [ |
| 112 |
'settings' => $all, |
| 113 |
]; |
| 114 |
} |
| 115 |
} |
| 116 |
|