| 1 |
<?php |
| 2 |
/** |
| 3 |
* API Endpoint: Editor Sidebar Settings |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.17.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\REST_API\Settings; |
| 12 |
|
| 13 |
use Parsely\Content_Helper\Suggestion_Defaults; |
| 14 |
|
| 15 |
/** |
| 16 |
* Endpoint for saving and retrieving Content Intelligence Editor Sidebar |
| 17 |
* settings. |
| 18 |
* |
| 19 |
* @since 3.17.0 |
| 20 |
* |
| 21 |
* @phpstan-import-type Subvalue_Spec from Base_Settings_Endpoint |
| 22 |
*/ |
| 23 |
class Endpoint_Editor_Sidebar_Settings extends Base_Settings_Endpoint { |
| 24 |
/** |
| 25 |
* The minimum desired excerpt length, in characters. |
| 26 |
* |
| 27 |
* @since 3.24.0 |
| 28 |
* |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
public const MIN_EXCERPT_LENGTH = Suggestion_Defaults::MIN_LENGTH; |
| 32 |
|
| 33 |
/** |
| 34 |
* The maximum desired excerpt length, in characters. |
| 35 |
* |
| 36 |
* @since 3.24.0 |
| 37 |
* |
| 38 |
* @var int |
| 39 |
*/ |
| 40 |
public const MAX_EXCERPT_LENGTH = Suggestion_Defaults::MAX_LENGTH; |
| 41 |
|
| 42 |
/** |
| 43 |
* The default desired excerpt length, in characters. |
| 44 |
* |
| 45 |
* @since 3.24.0 |
| 46 |
* |
| 47 |
* @var int |
| 48 |
*/ |
| 49 |
public const DEFAULT_EXCERPT_LENGTH = Suggestion_Defaults::DEFAULT_LENGTH; |
| 50 |
|
| 51 |
/** |
| 52 |
* The default persona used when generating excerpts. |
| 53 |
* |
| 54 |
* @since 3.24.0 |
| 55 |
* |
| 56 |
* @var string |
| 57 |
*/ |
| 58 |
public const DEFAULT_EXCERPT_PERSONA = Suggestion_Defaults::DEFAULT_PERSONA; |
| 59 |
|
| 60 |
/** |
| 61 |
* The default tone used when generating excerpts. |
| 62 |
* |
| 63 |
* @since 3.24.0 |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
public const DEFAULT_EXCERPT_TONE = Suggestion_Defaults::DEFAULT_TONE; |
| 68 |
|
| 69 |
/** |
| 70 |
* Returns the endpoint's name. |
| 71 |
* |
| 72 |
* @since 3.17.0 |
| 73 |
* |
| 74 |
* @return string |
| 75 |
*/ |
| 76 |
public static function get_endpoint_name(): string { |
| 77 |
return 'editor-sidebar'; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns the meta entry's key. |
| 82 |
* |
| 83 |
* @since 3.13.0 |
| 84 |
* @since 3.17.0 Moved from Editor_Sidebar_Settings_Endpoint. |
| 85 |
* |
| 86 |
* @return string The meta entry's key. |
| 87 |
*/ |
| 88 |
protected function get_meta_key(): string { |
| 89 |
return 'parsely_content_helper_settings_editor_sidebar'; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the endpoint's subvalues specifications. |
| 94 |
* |
| 95 |
* @since 3.13.0 |
| 96 |
* @since 3.17.0 Moved from Editor_Sidebar_Settings_Endpoint. |
| 97 |
* @since 3.24.0 Added the ExcerptSuggestions `Length` setting. |
| 98 |
* @since 3.24.0 Removed the ExcerptSuggestions `Open` setting, as the panel's |
| 99 |
* collapsed state is now persisted by the block editor itself. |
| 100 |
* @since 3.24.0 The ExcerptSuggestions and TitleSuggestions defaults come |
| 101 |
* from the site-wide settings of their respective features. |
| 102 |
* |
| 103 |
* @return array<string, Subvalue_Spec> |
| 104 |
*/ |
| 105 |
protected function get_subvalues_specs(): array { |
| 106 |
$excerpt_options = Suggestion_Defaults::get_feature_options( $this->parsely, 'excerpt_suggestions' ); |
| 107 |
$title_options = Suggestion_Defaults::get_feature_options( $this->parsely, 'title_suggestions' ); |
| 108 |
|
| 109 |
return array( |
| 110 |
'ExcerptSuggestions' => array( |
| 111 |
'values' => array( |
| 112 |
'Length' => array(), |
| 113 |
'Persona' => array(), |
| 114 |
'Tone' => array(), |
| 115 |
), |
| 116 |
'default' => array( |
| 117 |
'Length' => Suggestion_Defaults::get_default_length( $excerpt_options ), |
| 118 |
'Persona' => Suggestion_Defaults::get_default_persona( $excerpt_options ), |
| 119 |
'Tone' => Suggestion_Defaults::get_default_tone( $excerpt_options ), |
| 120 |
), |
| 121 |
), |
| 122 |
'InitialTabName' => array( |
| 123 |
'values' => array( 'tools', 'performance' ), |
| 124 |
'default' => 'tools', |
| 125 |
), |
| 126 |
'PerformanceStats' => array( |
| 127 |
'values' => array( |
| 128 |
'Period' => array( '10m', '1h', '2h', '4h', '24h', '7d', '30d' ), |
| 129 |
'VisibleDataPoints' => array( 'views', 'visitors', 'avgEngaged', 'recirculation' ), |
| 130 |
'VisiblePanels' => array( 'overview', 'categories', 'referrers' ), |
| 131 |
), |
| 132 |
'default' => array( |
| 133 |
'Period' => '7d', |
| 134 |
'VisibleDataPoints' => array( 'views', 'visitors', 'avgEngaged', 'recirculation' ), |
| 135 |
'VisiblePanels' => array( 'overview', 'categories', 'referrers' ), |
| 136 |
), |
| 137 |
), |
| 138 |
'RelatedPosts' => array( |
| 139 |
'values' => array( |
| 140 |
'Metric' => array( 'views', 'avg_engaged' ), |
| 141 |
'Open' => array( true, false ), |
| 142 |
'Period' => array( '10m', '1h', '2h', '4h', '24h', '7d', '30d' ), |
| 143 |
), |
| 144 |
'default' => array( |
| 145 |
'Metric' => 'views', |
| 146 |
'Open' => false, |
| 147 |
'Period' => '7d', |
| 148 |
), |
| 149 |
), |
| 150 |
'SmartLinking' => array( |
| 151 |
'values' => array( |
| 152 |
'MaxLinks' => array(), |
| 153 |
'MaxLinkWords' => array(), |
| 154 |
'Open' => array( true, false ), |
| 155 |
), |
| 156 |
'default' => array( |
| 157 |
'MaxLinks' => 10, |
| 158 |
'MaxLinkWords' => 4, |
| 159 |
'Open' => false, |
| 160 |
), |
| 161 |
), |
| 162 |
'TitleSuggestions' => array( |
| 163 |
'values' => array( |
| 164 |
'Open' => array( true, false ), |
| 165 |
'Persona' => array(), |
| 166 |
'Tone' => array(), |
| 167 |
), |
| 168 |
'default' => array( |
| 169 |
'Open' => false, |
| 170 |
'Persona' => Suggestion_Defaults::get_default_persona( $title_options ), |
| 171 |
'Tone' => Suggestion_Defaults::get_default_tone( $title_options ), |
| 172 |
), |
| 173 |
), |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Returns the composite keys of the settings that inherit their default. |
| 179 |
* |
| 180 |
* These are the settings whose default the plugin's settings page makes |
| 181 |
* configurable. |
| 182 |
* |
| 183 |
* @since 3.24.1 |
| 184 |
* |
| 185 |
* @return array<string> The composite keys. |
| 186 |
*/ |
| 187 |
protected function get_inheritable_keys(): array { |
| 188 |
return array( |
| 189 |
'ExcerptSuggestions.Length', |
| 190 |
'ExcerptSuggestions.Persona', |
| 191 |
'ExcerptSuggestions.Tone', |
| 192 |
'TitleSuggestions.Persona', |
| 193 |
'TitleSuggestions.Tone', |
| 194 |
); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Returns the defaults that the inheritable settings had before 3.24.0 made |
| 199 |
* them configurable. |
| 200 |
* |
| 201 |
* Spelled out rather than taken from Suggestion_Defaults, whose constants |
| 202 |
* describe the present rather than the past. `Length` is absent, as it did |
| 203 |
* not exist before it became configurable. |
| 204 |
* |
| 205 |
* @since 3.24.1 |
| 206 |
* |
| 207 |
* @return array<string, mixed> The defaults, as composite key => value pairs. |
| 208 |
*/ |
| 209 |
protected function get_legacy_defaults(): array { |
| 210 |
return array( |
| 211 |
'ExcerptSuggestions.Persona' => 'journalist', |
| 212 |
'ExcerptSuggestions.Tone' => 'neutral', |
| 213 |
'TitleSuggestions.Persona' => 'journalist', |
| 214 |
'TitleSuggestions.Tone' => 'neutral', |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Sanitizes the passed subvalue. |
| 220 |
* |
| 221 |
* Extends the parent implementation with a range check for the desired |
| 222 |
* excerpt length, which the enumerated valid values cannot express. |
| 223 |
* |
| 224 |
* @since 3.24.0 |
| 225 |
* |
| 226 |
* @param string $composite_key The subvalue's key. |
| 227 |
* @param mixed $value The value to sanitize. |
| 228 |
* @return mixed The sanitized subvalue. |
| 229 |
*/ |
| 230 |
protected function sanitize_subvalue( string $composite_key, $value ) { |
| 231 |
if ( 'ExcerptSuggestions.Length' === $composite_key ) { |
| 232 |
if ( ! is_int( $value ) || |
| 233 |
$value < self::MIN_EXCERPT_LENGTH || |
| 234 |
$value > self::MAX_EXCERPT_LENGTH |
| 235 |
) { |
| 236 |
return Suggestion_Defaults::get_default_length( |
| 237 |
Suggestion_Defaults::get_feature_options( $this->parsely, 'excerpt_suggestions' ) |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
return $value; |
| 242 |
} |
| 243 |
|
| 244 |
return parent::sanitize_subvalue( $composite_key, $value ); |
| 245 |
} |
| 246 |
} |
| 247 |
|