| 1 |
<?php |
| 2 |
/** |
| 3 |
* Content Intelligence: Suggestion defaults |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.24.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Content_Helper; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
|
| 15 |
/** |
| 16 |
* Holds the vocabulary and the site-wide generation defaults shared by the |
| 17 |
* suggestion features. |
| 18 |
* |
| 19 |
* This is the single source of the tones and personas. They are injected into |
| 20 |
* the Editor Sidebar, so that the plugin's settings page can offer the same |
| 21 |
* choices as the editor without restating them. |
| 22 |
* |
| 23 |
* @since 3.24.0 |
| 24 |
*/ |
| 25 |
class Suggestion_Defaults { |
| 26 |
/** |
| 27 |
* The minimum desired excerpt length, in characters. |
| 28 |
* |
| 29 |
* Kept in sync with MIN_EXCERPT_LENGTH in excerpt-suggestions/constants.ts. |
| 30 |
* |
| 31 |
* @since 3.24.0 |
| 32 |
* |
| 33 |
* @var int |
| 34 |
*/ |
| 35 |
public const MIN_LENGTH = 50; |
| 36 |
|
| 37 |
/** |
| 38 |
* The maximum desired excerpt length, in characters. |
| 39 |
* |
| 40 |
* Kept in sync with MAX_EXCERPT_LENGTH in excerpt-suggestions/constants.ts. |
| 41 |
* |
| 42 |
* @since 3.24.0 |
| 43 |
* |
| 44 |
* @var int |
| 45 |
*/ |
| 46 |
public const MAX_LENGTH = 300; |
| 47 |
|
| 48 |
/** |
| 49 |
* The default desired excerpt length, in characters. |
| 50 |
* |
| 51 |
* Kept in sync with DEFAULT_EXCERPT_LENGTH in |
| 52 |
* excerpt-suggestions/constants.ts. |
| 53 |
* |
| 54 |
* @since 3.24.0 |
| 55 |
* |
| 56 |
* @var int |
| 57 |
*/ |
| 58 |
public const DEFAULT_LENGTH = 160; |
| 59 |
|
| 60 |
/** |
| 61 |
* The tone used when none is configured. |
| 62 |
* |
| 63 |
* Kept in sync with DEFAULT_TONE in common/utils/constants.ts. |
| 64 |
* |
| 65 |
* @since 3.24.0 |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
public const DEFAULT_TONE = 'neutral'; |
| 70 |
|
| 71 |
/** |
| 72 |
* The persona used when none is configured. |
| 73 |
* |
| 74 |
* Kept in sync with DEFAULT_PERSONA in common/utils/constants.ts. |
| 75 |
* |
| 76 |
* @since 3.24.0 |
| 77 |
* |
| 78 |
* @var string |
| 79 |
*/ |
| 80 |
public const DEFAULT_PERSONA = 'journalist'; |
| 81 |
|
| 82 |
/** |
| 83 |
* Returns a Content Intelligence feature's site-wide options. |
| 84 |
* |
| 85 |
* @since 3.24.0 |
| 86 |
* |
| 87 |
* @param Parsely $parsely The Parsely instance holding the options. |
| 88 |
* @param string $feature_id The feature's name. |
| 89 |
* @return array<string, mixed> The feature's options. |
| 90 |
*/ |
| 91 |
public static function get_feature_options( Parsely $parsely, string $feature_id ): array { |
| 92 |
/** @var array<string, mixed> $options */ |
| 93 |
$options = $parsely->get_options()['content_helper'][ $feature_id ] ?? array(); |
| 94 |
|
| 95 |
return $options; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the predefined tones, keyed by their stored value. |
| 100 |
* |
| 101 |
* The custom tone is absent, as the Tone Selector adds it. It is a UI |
| 102 |
* affordance rather than part of the vocabulary, and a site-wide default |
| 103 |
* cannot carry the free-text value that gives it meaning. |
| 104 |
* |
| 105 |
* @since 3.24.0 |
| 106 |
* |
| 107 |
* @return array<string, string> The tones, as value => label pairs. |
| 108 |
*/ |
| 109 |
public static function get_tones(): array { |
| 110 |
return array( |
| 111 |
'neutral' => __( 'Neutral', 'wp-parsely' ), |
| 112 |
'formal' => __( 'Formal', 'wp-parsely' ), |
| 113 |
'humorous' => __( 'Humorous', 'wp-parsely' ), |
| 114 |
'confident' => __( 'Confident', 'wp-parsely' ), |
| 115 |
'provocative' => __( 'Provocative', 'wp-parsely' ), |
| 116 |
'serious' => __( 'Serious', 'wp-parsely' ), |
| 117 |
'inspirational' => __( 'Inspirational', 'wp-parsely' ), |
| 118 |
'skeptical' => __( 'Skeptical', 'wp-parsely' ), |
| 119 |
'conversational' => __( 'Conversational', 'wp-parsely' ), |
| 120 |
'analytical' => __( 'Analytical', 'wp-parsely' ), |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Returns the predefined personas, keyed by their stored value. |
| 126 |
* |
| 127 |
* The custom persona is absent, for the same reason as the custom tone. |
| 128 |
* |
| 129 |
* @since 3.24.0 |
| 130 |
* |
| 131 |
* @return array<string, string> The personas, as value => label pairs. |
| 132 |
*/ |
| 133 |
public static function get_personas(): array { |
| 134 |
return array( |
| 135 |
'journalist' => __( 'Journalist', 'wp-parsely' ), |
| 136 |
'editorialWriter' => __( 'Editorial Writer', 'wp-parsely' ), |
| 137 |
'investigativeReporter' => __( 'Investigative Reporter', 'wp-parsely' ), |
| 138 |
'techAnalyst' => __( 'Tech Analyst', 'wp-parsely' ), |
| 139 |
'businessAnalyst' => __( 'Business Analyst', 'wp-parsely' ), |
| 140 |
'culturalCommentator' => __( 'Cultural Commentator', 'wp-parsely' ), |
| 141 |
'scienceCorrespondent' => __( 'Science Correspondent', 'wp-parsely' ), |
| 142 |
'politicalAnalyst' => __( 'Political Analyst', 'wp-parsely' ), |
| 143 |
'healthWellnessAdvocate' => __( 'Health and Wellness Advocate', 'wp-parsely' ), |
| 144 |
'environmentalJournalist' => __( 'Environmental Journalist', 'wp-parsely' ), |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Returns a feature's site-wide default excerpt length. |
| 150 |
* |
| 151 |
* Falls back to the shipped default when the option is missing, which is |
| 152 |
* the case for installations that predate these settings, or when it holds |
| 153 |
* an out-of-range value. |
| 154 |
* |
| 155 |
* @since 3.24.0 |
| 156 |
* |
| 157 |
* @param array<string, mixed> $feature_options The feature's options. |
| 158 |
* @return int The default length, in characters. |
| 159 |
*/ |
| 160 |
public static function get_default_length( array $feature_options ): int { |
| 161 |
$length = $feature_options['default_length'] ?? self::DEFAULT_LENGTH; |
| 162 |
|
| 163 |
if ( ! is_int( $length ) || |
| 164 |
$length < self::MIN_LENGTH || |
| 165 |
$length > self::MAX_LENGTH |
| 166 |
) { |
| 167 |
return self::DEFAULT_LENGTH; |
| 168 |
} |
| 169 |
|
| 170 |
return $length; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Returns a feature's site-wide default tone. |
| 175 |
* |
| 176 |
* @since 3.24.0 |
| 177 |
* |
| 178 |
* @param array<string, mixed> $feature_options The feature's options. |
| 179 |
* @return string The default tone. |
| 180 |
*/ |
| 181 |
public static function get_default_tone( array $feature_options ): string { |
| 182 |
$tone = $feature_options['default_tone'] ?? self::DEFAULT_TONE; |
| 183 |
|
| 184 |
if ( ! is_string( $tone ) || ! isset( self::get_tones()[ $tone ] ) ) { |
| 185 |
return self::DEFAULT_TONE; |
| 186 |
} |
| 187 |
|
| 188 |
return $tone; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Returns a feature's site-wide default persona. |
| 193 |
* |
| 194 |
* @since 3.24.0 |
| 195 |
* |
| 196 |
* @param array<string, mixed> $feature_options The feature's options. |
| 197 |
* @return string The default persona. |
| 198 |
*/ |
| 199 |
public static function get_default_persona( array $feature_options ): string { |
| 200 |
$persona = $feature_options['default_persona'] ?? self::DEFAULT_PERSONA; |
| 201 |
|
| 202 |
if ( ! is_string( $persona ) || ! isset( self::get_personas()[ $persona ] ) ) { |
| 203 |
return self::DEFAULT_PERSONA; |
| 204 |
} |
| 205 |
|
| 206 |
return $persona; |
| 207 |
} |
| 208 |
} |
| 209 |
|