PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / bulk-editor / infrastructure / posts / default-template-resolver.php

default-template-resolver.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/bulk-editor/infrastructure/posts/default-template-resolver.php

142 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 namespace Yoast\WP\SEO\Bulk_Editor\Infrastructure\Posts;
5
6 use Yoast\WP\SEO\Helpers\Options_Helper;
7
8 /**
9 * Resolves a post's SEO/social fields to the raw template string for display in a replacement-variable editor.
10 *
11 * When a post has no stored value, falls back to the post type's configured template from options.
12 * Only the first level of fallback is applied; deeper rendering-chain fallbacks (e.g. social description
13 * falling back to meta description or excerpt) are intentionally omitted — those produce dynamic values
14 * that cannot be represented as a raw template.
15 */
16 class Default_Template_Resolver {
17
18 /**
19 * The options helper.
20 *
21 * @var Options_Helper
22 */
23 private $options_helper;
24
25 /**
26 * The constructor.
27 *
28 * @param Options_Helper $options_helper The options helper.
29 */
30 public function __construct( Options_Helper $options_helper ) {
31 $this->options_helper = $options_helper;
32 }
33
34 /**
35 * Returns the raw SEO title template for a post, falling back to the post type's configured template when empty.
36 *
37 * Returns the unresolved template string (e.g. `%%title%% %%sep%% %%sitename%%`) so the caller
38 * can display it in a replacement-variable editor rather than showing the expanded value.
39 * Priority: stored value → user-configured post type template (`title-{post_type}`) → installation default.
40 *
41 * @param int $post_id The post ID (unused; kept for a consistent method signature).
42 * @param string $post_type The post type slug.
43 * @param string $stored_value The raw stored title (empty string when never explicitly saved).
44 *
45 * @return string The raw template string, or an empty string when no template is configured.
46 */
47 public function resolve_seo_title( int $post_id, string $post_type, string $stored_value ): string {
48 return $this->resolve( $post_type, $stored_value, 'title-', true );
49 }
50
51 /**
52 * Returns the raw meta description template for a post, falling back to the post type's configured template when empty.
53 *
54 * Returns the unresolved template string so the caller can display it in a replacement-variable
55 * editor. Unlike SEO title there is no installation-level default, so an empty stored value
56 * returns an empty string when the user has not configured a post type template.
57 *
58 * @param int $post_id The post ID (unused; kept for a consistent method signature).
59 * @param string $post_type The post type slug.
60 * @param string $stored_value The raw stored description (empty string when never explicitly saved).
61 *
62 * @return string The raw template string, or an empty string when no template is configured.
63 */
64 public function resolve_meta_description( int $post_id, string $post_type, string $stored_value ): string {
65 return $this->resolve( $post_type, $stored_value, 'metadesc-', false );
66 }
67
68 /**
69 * Returns the raw social title template for a post, falling back to the post type's configured template when empty.
70 *
71 * Returns the unresolved template string so the caller can display it in a replacement-variable editor.
72 * Only resolves a template when OpenGraph is enabled.
73 * Priority: stored value → user-configured post type template (`social-title-{post_type}`) → installation default.
74 *
75 * @param int $post_id The post ID (unused; kept for a consistent method signature).
76 * @param string $post_type The post type slug.
77 * @param string $stored_value The raw stored social title (empty string when never explicitly saved).
78 *
79 * @return string The raw template string, or an empty string when no template is configured.
80 */
81 public function resolve_social_title( int $post_id, string $post_type, string $stored_value ): string {
82 if ( $stored_value !== '' ) {
83 return $stored_value;
84 }
85
86 if ( $this->options_helper->get( 'opengraph', false ) !== true ) {
87 return '';
88 }
89
90 return $this->resolve( $post_type, '', 'social-title-', true );
91 }
92
93 /**
94 * Returns the raw social description template for a post, falling back to the post type's configured template when empty.
95 *
96 * Returns the unresolved template string so the caller can display it in a replacement-variable editor.
97 * Only resolves a template when OpenGraph is enabled. Unlike social title there is no installation-level default.
98 *
99 * @param int $post_id The post ID (unused; kept for a consistent method signature).
100 * @param string $post_type The post type slug.
101 * @param string $stored_value The raw stored social description (empty string when never explicitly saved).
102 *
103 * @return string The raw template string, or an empty string when no template is configured.
104 */
105 public function resolve_social_description( int $post_id, string $post_type, string $stored_value ): string {
106 if ( $stored_value !== '' ) {
107 return $stored_value;
108 }
109
110 if ( $this->options_helper->get( 'opengraph', false ) !== true ) {
111 return '';
112 }
113
114 return $this->resolve( $post_type, '', 'social-description-', false );
115 }
116
117 /**
118 * Resolves a raw template string for a given post type, stored value, and option key prefix.
119 *
120 * @param string $post_type The post type slug.
121 * @param string $stored_value The raw stored value (empty string when never explicitly saved).
122 * @param string $option_key_prefix The option key prefix (e.g. `title-`, `metadesc-`).
123 * @param bool $use_installation_default Whether to fall back to the installation default when the configured template is empty.
124 *
125 * @return string The raw template string, or an empty string when no template is configured.
126 */
127 private function resolve( string $post_type, string $stored_value, string $option_key_prefix, bool $use_installation_default ): string {
128 if ( $stored_value !== '' ) {
129 return $stored_value;
130 }
131
132 $option_key = $option_key_prefix . $post_type;
133 $template = (string) $this->options_helper->get( $option_key, '' );
134
135 if ( $use_installation_default && $template === '' ) {
136 $template = (string) $this->options_helper->get_title_default( $option_key );
137 }
138
139 return $template;
140 }
141 }
142