Sync your WordPress posts to standard.site records on your PDS
at main 483 lines 12 kB view raw
1<?php 2declare(strict_types=1); 3/** 4 * Centralized source option building and grouped settings accessors. 5 * 6 * @package Wireservice 7 */ 8 9namespace Wireservice; 10 11if (! defined('ABSPATH')) { 12 exit; 13} 14 15class SourceOptions 16{ 17 /** 18 * Default values for publication settings. 19 * 20 * @var array 21 */ 22 public const PUB_DEFAULTS = [ 23 "name_source" => "wordpress_title", 24 "description_source" => "wordpress_tagline", 25 "custom_name" => "", 26 "custom_description" => "", 27 "icon_source" => "none", 28 "custom_icon_id" => 0, 29 "theme_background" => "", 30 "theme_foreground" => "", 31 "theme_accent" => "", 32 "theme_accent_foreground" => "", 33 "show_in_discover" => "", 34 ]; 35 36 /** 37 * Default values for document settings. 38 * 39 * @var array 40 */ 41 public const DOC_DEFAULTS = [ 42 "enabled" => "0", 43 "title_source" => "wordpress_title", 44 "description_source" => "wordpress_excerpt", 45 "image_source" => "wordpress_featured", 46 "include_content" => "0", 47 ]; 48 49 /** 50 * All valid publication name source keys. 51 * 52 * @var string[] 53 */ 54 private const VALID_PUB_NAME_KEYS = [ 55 "wordpress_title", 56 "yoast_organization", 57 "yoast_website", 58 "custom", 59 ]; 60 61 /** 62 * All valid publication description source keys. 63 * 64 * @var string[] 65 */ 66 private const VALID_PUB_DESC_KEYS = [ 67 "wordpress_tagline", 68 "yoast_homepage", 69 "custom", 70 ]; 71 72 /** 73 * All valid publication icon source keys. 74 * 75 * @var string[] 76 */ 77 private const VALID_PUB_ICON_KEYS = [ 78 "none", 79 "wordpress_site_icon", 80 "custom", 81 ]; 82 83 /** 84 * All valid document title source keys. 85 * 86 * @var string[] 87 */ 88 private const VALID_DOC_TITLE_KEYS = [ 89 "wordpress_title", 90 "yoast_title", 91 "yoast_social_title", 92 "yoast_x_title", 93 "custom", 94 ]; 95 96 /** 97 * All valid document description source keys. 98 * 99 * @var string[] 100 */ 101 private const VALID_DOC_DESC_KEYS = [ 102 "wordpress_excerpt", 103 "yoast_description", 104 "yoast_social_description", 105 "yoast_x_description", 106 "custom", 107 ]; 108 109 /** 110 * All valid document image source keys. 111 * 112 * @var string[] 113 */ 114 private const VALID_DOC_IMAGE_KEYS = [ 115 "none", 116 "wordpress_featured", 117 "yoast_social_image", 118 "yoast_x_image", 119 "custom", 120 ]; 121 122 /** 123 * Validate a publication name source key, returning the default if invalid. 124 * 125 * @param string $key The source key to validate. 126 * @return string The validated key. 127 */ 128 public static function validate_pub_name_key(string $key): string 129 { 130 return in_array($key, self::VALID_PUB_NAME_KEYS, true) 131 ? $key 132 : self::PUB_DEFAULTS["name_source"]; 133 } 134 135 /** 136 * Validate a publication description source key, returning the default if invalid. 137 * 138 * @param string $key The source key to validate. 139 * @return string The validated key. 140 */ 141 public static function validate_pub_desc_key(string $key): string 142 { 143 return in_array($key, self::VALID_PUB_DESC_KEYS, true) 144 ? $key 145 : self::PUB_DEFAULTS["description_source"]; 146 } 147 148 /** 149 * Validate a publication icon source key, returning the default if invalid. 150 * 151 * @param string $key The source key to validate. 152 * @return string The validated key. 153 */ 154 public static function validate_pub_icon_key(string $key): string 155 { 156 return in_array($key, self::VALID_PUB_ICON_KEYS, true) 157 ? $key 158 : self::PUB_DEFAULTS["icon_source"]; 159 } 160 161 /** 162 * Validate a document title source key, returning the default if invalid. 163 * 164 * @param string $key The source key to validate. 165 * @return string The validated key. 166 */ 167 public static function validate_doc_title_key(string $key): string 168 { 169 return in_array($key, self::VALID_DOC_TITLE_KEYS, true) 170 ? $key 171 : self::DOC_DEFAULTS["title_source"]; 172 } 173 174 /** 175 * Validate a document description source key, returning the default if invalid. 176 * 177 * @param string $key The source key to validate. 178 * @return string The validated key. 179 */ 180 public static function validate_doc_desc_key(string $key): string 181 { 182 return in_array($key, self::VALID_DOC_DESC_KEYS, true) 183 ? $key 184 : self::DOC_DEFAULTS["description_source"]; 185 } 186 187 /** 188 * Validate a document image source key, returning the default if invalid. 189 * 190 * @param string $key The source key to validate. 191 * @return string The validated key. 192 */ 193 public static function validate_doc_image_key(string $key): string 194 { 195 return in_array($key, self::VALID_DOC_IMAGE_KEYS, true) 196 ? $key 197 : self::DOC_DEFAULTS["image_source"]; 198 } 199 200 /** 201 * Get publication settings merged with defaults. 202 * 203 * @return array 204 */ 205 public static function get_pub_settings(): array 206 { 207 return wp_parse_args( 208 get_option("wireservice_pub_settings", []), 209 self::PUB_DEFAULTS, 210 ); 211 } 212 213 /** 214 * Get document settings merged with defaults. 215 * 216 * @return array 217 */ 218 public static function get_doc_settings(): array 219 { 220 return wp_parse_args( 221 get_option("wireservice_doc_settings", []), 222 self::DOC_DEFAULTS, 223 ); 224 } 225 226 /** 227 * Get publication name source options for the settings page. 228 * 229 * @param string $custom_name The current custom name value. 230 * @return array 231 */ 232 public static function pub_name_sources(string $custom_name = ""): array 233 { 234 $sources = [ 235 "wordpress_title" => [ 236 "label" => __("WordPress Site Title", "wireservice"), 237 "value" => get_bloginfo("name"), 238 ], 239 ]; 240 241 if (Yoast::is_active()) { 242 $yoast_org = Yoast::get_organization_name(); 243 if ($yoast_org) { 244 $sources["yoast_organization"] = [ 245 "label" => __("Yoast Organization Name", "wireservice"), 246 "value" => $yoast_org, 247 ]; 248 } 249 $yoast_site = Yoast::get_website_name(); 250 if ($yoast_site) { 251 $sources["yoast_website"] = [ 252 "label" => __("Yoast Website Name", "wireservice"), 253 "value" => $yoast_site, 254 ]; 255 } 256 } 257 258 $sources["custom"] = [ 259 "label" => __("Custom", "wireservice"), 260 "value" => $custom_name, 261 ]; 262 263 return $sources; 264 } 265 266 /** 267 * Get publication description source options for the settings page. 268 * 269 * @param string $custom_description The current custom description value. 270 * @return array 271 */ 272 public static function pub_description_sources( 273 string $custom_description = "", 274 ): array { 275 $sources = [ 276 "wordpress_tagline" => [ 277 "label" => __("WordPress Tagline", "wireservice"), 278 "value" => get_bloginfo("description"), 279 ], 280 ]; 281 282 if (Yoast::is_active()) { 283 $yoast_desc = Yoast::get_homepage_description(); 284 if ($yoast_desc) { 285 $sources["yoast_homepage"] = [ 286 "label" => __( 287 "Yoast Homepage Meta Description", 288 "wireservice", 289 ), 290 "value" => $yoast_desc, 291 ]; 292 } 293 } 294 295 $sources["custom"] = [ 296 "label" => __("Custom", "wireservice"), 297 "value" => $custom_description, 298 ]; 299 300 return $sources; 301 } 302 303 /** 304 * Get publication icon source options for the settings page. 305 * 306 * @return array 307 */ 308 public static function pub_icon_sources(): array 309 { 310 $sources = [ 311 "none" => [ 312 "label" => __("None (no icon)", "wireservice"), 313 "value" => "", 314 ], 315 ]; 316 317 $site_icon_url = get_site_icon_url(256); 318 if ($site_icon_url) { 319 $sources["wordpress_site_icon"] = [ 320 "label" => __("WordPress Site Icon", "wireservice"), 321 "value" => $site_icon_url, 322 ]; 323 } 324 325 $sources["custom"] = [ 326 "label" => __("Custom", "wireservice"), 327 "value" => "", 328 ]; 329 330 return $sources; 331 } 332 333 /** 334 * Get document title source options. 335 * 336 * @return array 337 */ 338 public static function doc_title_sources(): array 339 { 340 $sources = [ 341 "wordpress_title" => __("WordPress Post Title", "wireservice"), 342 ]; 343 344 if (Yoast::is_active()) { 345 $sources["yoast_title"] = __("Yoast SEO Title", "wireservice"); 346 $sources["yoast_social_title"] = __( 347 "Yoast Social Title", 348 "wireservice", 349 ); 350 $sources["yoast_x_title"] = __("Yoast X Title", "wireservice"); 351 } 352 353 return $sources; 354 } 355 356 /** 357 * Get document description source options. 358 * 359 * @return array 360 */ 361 public static function doc_description_sources(): array 362 { 363 $sources = [ 364 "wordpress_excerpt" => __("WordPress Excerpt", "wireservice"), 365 ]; 366 367 if (Yoast::is_active()) { 368 $sources["yoast_description"] = __( 369 "Yoast Meta Description", 370 "wireservice", 371 ); 372 $sources["yoast_social_description"] = __( 373 "Yoast Social Description", 374 "wireservice", 375 ); 376 $sources["yoast_x_description"] = __( 377 "Yoast X Description", 378 "wireservice", 379 ); 380 } 381 382 return $sources; 383 } 384 385 /** 386 * Get document cover image source options. 387 * 388 * @return array 389 */ 390 public static function doc_image_sources(): array 391 { 392 $sources = [ 393 "none" => __("None (no cover image)", "wireservice"), 394 "wordpress_featured" => __( 395 "WordPress Featured Image", 396 "wireservice", 397 ), 398 ]; 399 400 if (Yoast::is_active()) { 401 $sources["yoast_social_image"] = __( 402 "Yoast Social Image", 403 "wireservice", 404 ); 405 $sources["yoast_x_image"] = __("Yoast X Image", "wireservice"); 406 } 407 408 return $sources; 409 } 410 411 /** 412 * Get document title source options for the meta box (with "Use global" option). 413 * 414 * @param string $global_source The current global title source key. 415 * @return array 416 */ 417 public static function meta_box_title_sources(string $global_source): array 418 { 419 return self::build_meta_box_sources( 420 self::doc_title_sources(), 421 $global_source, 422 __("WordPress Post Title", "wireservice"), 423 ); 424 } 425 426 /** 427 * Get document description source options for the meta box (with "Use global" option). 428 * 429 * @param string $global_source The current global description source key. 430 * @return array 431 */ 432 public static function meta_box_description_sources( 433 string $global_source, 434 ): array { 435 return self::build_meta_box_sources( 436 self::doc_description_sources(), 437 $global_source, 438 __("WordPress Excerpt", "wireservice"), 439 ); 440 } 441 442 /** 443 * Get document image source options for the meta box (with "Use global" option). 444 * 445 * @param string $global_source The current global image source key. 446 * @return array 447 */ 448 public static function meta_box_image_sources( 449 string $global_source, 450 ): array { 451 return self::build_meta_box_sources( 452 self::doc_image_sources(), 453 $global_source, 454 __("WordPress Featured Image", "wireservice"), 455 ); 456 } 457 458 /** 459 * Build meta box source options with a "Use global setting" prefix. 460 * 461 * @param array $base Base source options from a doc_*_sources() method. 462 * @param string $global_source The current global source key. 463 * @param string $fallback_label Label to use if $global_source is not in $base. 464 * @return array 465 */ 466 private static function build_meta_box_sources( 467 array $base, 468 string $global_source, 469 string $fallback_label, 470 ): array { 471 $base["custom"] = __("Custom", "wireservice"); 472 473 $global_label = $base[$global_source] ?? $fallback_label; 474 475 return [ 476 "" => sprintf( 477 /* translators: %s: source name */ 478 __("Use global setting (%s)", "wireservice"), 479 $global_label, 480 ), 481 ] + $base; 482 } 483}