| 1 |
<?php |
| 2 |
/** |
| 3 |
* Read the settings schema ability. |
| 4 |
* |
| 5 |
* @package BetterDocs |
| 6 |
* @since 4.9.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDeveloper\BetterDocs\Abilities\Settings; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
use WPDeveloper\BetterDocs\Abilities\AbilityBase; |
| 16 |
use WPDeveloper\BetterDocs\Abilities\AbilityError; |
| 17 |
|
| 18 |
/** |
| 19 |
* What every BetterDocs setting is: its type, its default, the values it |
| 20 |
* accepts, the tab it lives on, whether it needs Pro, and what changing it |
| 21 |
* costs. |
| 22 |
* |
| 23 |
* The tool to call **before** `bd-update-settings`. BetterDocs has around 250 |
| 24 |
* writable settings on a Pro site and no two sites carry the same set — Pro |
| 25 |
* adds whole tabs, add-ons add fields, filters remove them — so the only |
| 26 |
* reliable list is this one, read from the site in front of you. |
| 27 |
* |
| 28 |
* @since 4.9.0 |
| 29 |
*/ |
| 30 |
class GetSettingsSchema extends AbilityBase { |
| 31 |
|
| 32 |
/** |
| 33 |
* @since 4.9.0 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
$this->id = 'betterdocs/get-settings-schema'; |
| 37 |
$this->label = __( 'Get settings schema', 'betterdocs' ); |
| 38 |
$this->description = __( 'List every BetterDocs setting this site accepts, with its type, default, allowed values, tab, whether it needs Pro, and any consequence of changing it. Call this before bd-update-settings: the settings differ from site to site, and a value that is not in a key\'s allowed list is refused.', 'betterdocs' ); |
| 39 |
$this->capability = 'edit_docs_settings'; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @since 4.9.0 |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public function get_annotations() { |
| 48 |
return [ |
| 49 |
'readonly' => true, |
| 50 |
'destructive' => false, |
| 51 |
'idempotent' => true, |
| 52 |
'priority' => 1.5, |
| 53 |
'openWorldHint' => false |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @since 4.9.0 |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public function get_input_schema() { |
| 63 |
return [ |
| 64 |
'type' => 'object', |
| 65 |
'additionalProperties' => false, |
| 66 |
'properties' => [ |
| 67 |
'tab' => [ |
| 68 |
'type' => 'string', |
| 69 |
'description' => __( 'Only the settings on this tab, by tab id (the ids are in the tabs list this tool returns). Worth using: the whole schema is large.', 'betterdocs' ) |
| 70 |
] |
| 71 |
], |
| 72 |
'default' => [] |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @since 4.9.0 |
| 78 |
* |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
public function get_output_schema() { |
| 82 |
return [ |
| 83 |
'type' => 'object', |
| 84 |
'properties' => [ |
| 85 |
'tabs' => [ |
| 86 |
'type' => 'array', |
| 87 |
'items' => [ |
| 88 |
'type' => 'object', |
| 89 |
'properties' => [ |
| 90 |
'id' => [ 'type' => 'string' ], |
| 91 |
'label' => [ 'type' => 'string' ], |
| 92 |
'included' => [ 'type' => 'boolean' ] |
| 93 |
] |
| 94 |
] |
| 95 |
], |
| 96 |
'settings' => [ 'type' => 'object' ], |
| 97 |
'count' => [ 'type' => 'integer' ] |
| 98 |
] |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @since 4.9.0 |
| 104 |
* |
| 105 |
* @param array $input Validated input. |
| 106 |
* @return array|\WP_Error |
| 107 |
*/ |
| 108 |
public function execute( $input ) { |
| 109 |
$tabs = SettingsSchema::tabs(); |
| 110 |
$schema = SettingsSchema::resolve(); |
| 111 |
$tab = isset( $input['tab'] ) ? (string) $input['tab'] : ''; |
| 112 |
|
| 113 |
if ( '' !== $tab ) { |
| 114 |
$known = wp_list_pluck( $tabs, 'id' ); |
| 115 |
|
| 116 |
if ( ! in_array( $tab, $known, true ) ) { |
| 117 |
return AbilityError::invalid_input( |
| 118 |
'tab', |
| 119 |
sprintf( |
| 120 |
/* translators: %s: tab id. */ |
| 121 |
__( 'There is no settings tab called "%s" on this site.', 'betterdocs' ), |
| 122 |
$tab |
| 123 |
), |
| 124 |
$known |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
$schema = array_filter( |
| 129 |
$schema, |
| 130 |
static function ( array $entry ) use ( $tab ) { |
| 131 |
return $entry['tab'] === $tab; |
| 132 |
} |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
return [ |
| 137 |
'tabs' => $tabs, |
| 138 |
'settings' => $schema, |
| 139 |
'count' => count( $schema ) |
| 140 |
]; |
| 141 |
} |
| 142 |
} |
| 143 |
|