99use Yoast \WP \SEO \Repositories \Indexable_Repository ;
1010use Yoast \WP \SEO \Surfaces \Values \Meta ;
1111
12+ // Fix Schema
13+ \add_filter ('wpseo_schema_webpage_type ' , __NAMESPACE__ . '\\fix_schema_webpage_type ' );
14+ /**
15+ * Add CollectionPage schema to custom post type pages
16+ *
17+ * @param string|string[] $type
18+ *
19+ * @return string|string[]
20+ */
21+ function fix_schema_webpage_type ($ type )
22+ {
23+ $ pfcpt = Plugin::get_instance ();
24+ if (!$ pfcpt ->is_query_page_for_custom_post_type ()) {
25+ return $ type ;
26+ }
27+
28+ $ type = (array ) $ type ;
29+ if (\in_array ('CollectionPage ' , $ type , true )) {
30+ return $ type ;
31+ }
32+
33+ $ type [] = 'CollectionPage ' ;
34+
35+ return $ type ;
36+ }
37+
38+
1239// Fix Yoast SEO breadcrumbs
1340\add_filter ('wpseo_breadcrumb_indexables ' , __NAMESPACE__ . '\\fix_home_breadcrumbs ' , 10 , 2 );
1441\add_filter ('wpseo_breadcrumb_indexables ' , __NAMESPACE__ . '\\fix_taxonomy_breadcrumbs ' , 10 , 2 );
1744/**
1845 * Fix Yoast breadcrumbs on post
1946 *
47+ * @param Indexable[] $indexables
2048 * @param Meta_Tags_Context $context
21- * @return array
49+ *
50+ * @return Indexable[]
2251 */
23- function fix_post_breadcrumbs (array $ indexables , $ context )
52+ function fix_post_breadcrumbs (array $ indexables , $ context ): array
2453{
2554 $ current_post_type = $ context ->indexable ->object_sub_type ?? null ;
2655 if (!$ current_post_type || !\is_singular ($ current_post_type )) {
@@ -58,9 +87,11 @@ function fix_post_breadcrumbs(array $indexables, $context)
5887/**
5988 * Fix Yoast breadcrumbs on taxonomy
6089 *
61- * @return array
90+ * @param Indexable[] $indexables
91+ *
92+ * @return Indexable[]
6293 */
63- function fix_taxonomy_breadcrumbs (array $ indexables , Meta_Tags_Context $ context )
94+ function fix_taxonomy_breadcrumbs (array $ indexables , Meta_Tags_Context $ context ): array
6495{
6596 $ current_taxonomy = $ context ->indexable ->object_sub_type ?? null ;
6697 if (!\is_tax ($ current_taxonomy )) {
@@ -114,10 +145,12 @@ function fix_taxonomy_breadcrumbs(array $indexables, Meta_Tags_Context $context)
114145/**
115146 * Fix Yoast breadcrumbs on home
116147 *
148+ * @param Indexable[] $indexables
117149 * @param Meta_Tags_Context $context
118- * @return array
150+ *
151+ * @return Indexable[]
119152 */
120- function fix_home_breadcrumbs (array $ indexables , $ context )
153+ function fix_home_breadcrumbs (array $ indexables , $ context ): array
121154{
122155 $ pfcpt = Plugin::get_instance ();
123156 if (!$ pfcpt ->is_query_page_for_custom_post_type ()) {
@@ -140,12 +173,12 @@ function fix_home_breadcrumbs(array $indexables, $context)
140173 $ front_page_id = $ yoast ->helpers ->current_page ->get_front_page_id ();
141174 if ($ front_page_id === 0 ) {
142175 $ home_page_ancestor = $ indexable_repository ->find_for_home_page ();
143- if (\is_a ($ home_page_ancestor , Indexable::class)) {
176+ if (! \is_bool ( $ home_page_ancestor ) && \is_a ($ home_page_ancestor , Indexable::class)) {
144177 $ static_ancestors [] = $ home_page_ancestor ;
145178 }
146179 } else {
147180 $ static_ancestor = $ indexable_repository ->find_by_id_and_type ($ front_page_id , 'post ' );
148- if (\is_a ($ static_ancestor , Indexable::class) && $ static_ancestor ->post_status !== 'unindexed ' ) {
181+ if (! \is_bool ( $ static_ancestor ) && \is_a ($ static_ancestor , Indexable::class) && $ static_ancestor ->post_status !== 'unindexed ' ) {
149182 $ static_ancestors [] = $ static_ancestor ;
150183 }
151184 }
@@ -158,16 +191,6 @@ function fix_home_breadcrumbs(array $indexables, $context)
158191 return $ indexables ;
159192}
160193
161- function add_filter_once ($ hook , $ callback , $ priority = 10 , $ args = 1 )
162- {
163- $ singular = function () use ($ hook , $ callback , $ priority , $ args , &$ singular ) {
164- \call_user_func_array ($ callback , \func_get_args ());
165- \remove_filter ($ hook , $ singular , $ priority );
166- };
167-
168- return \add_filter ($ hook , $ singular , $ priority , $ args );
169- }
170-
171194/**
172195 * Shortcircuit get_option to return the page ID for a custom post type
173196 *
@@ -180,31 +203,8 @@ function set_page_for_custom_post_type(): void
180203 return ;
181204 }
182205
183- /**
184- * Trick Yoast SEO for_current_page logic which determines the current indexable
185- *
186- * @see \Yoast\WP\SEO\Repositories\Indexable_Repository::for_current_page
187- *
188- * @param mixed $value
189- * @return mixed
190- */
191- \add_filter ('pre_option_show_on_front ' , function ($ value ) {
192- global $ wp_current_filter ;
193- if (
194- \is_array ($ wp_current_filter )
195- && isset ($ wp_current_filter [1 ])
196- && $ wp_current_filter [1 ] === 'wp_robots '
197- ) {
198- $ bt = \debug_backtrace ();
199- if (
200- isset ($ bt [3 ])
201- && isset ($ bt [3 ]['file ' ])
202- && \basename ($ bt [3 ]['file ' ]) === 'current-page-helper.php '
203- ) {
204- return 'page ' ;
205- }
206- }
207- return $ value ;
206+ \add_filter ('wpseo_frontend_page_type_simple_page_id ' , function () {
207+ return \get_queried_object_id ();
208208 });
209209
210210 /**
@@ -215,25 +215,46 @@ function set_page_for_custom_post_type(): void
215215 * @param mixed $value
216216 * @return mixed
217217 */
218- \add_filter ('pre_option_page_for_posts ' , function ($ value ) {
219- global $ wp_current_filter ;
220- if (
221- \is_array ($ wp_current_filter )
222- && isset ($ wp_current_filter [1 ])
223- && $ wp_current_filter [1 ] === 'wp_robots '
224- ) {
218+ if (\get_option ('show_on_front ' ) === 'page ' ) {
219+ \add_filter ('pre_option_show_on_front ' , function ($ value ) {
225220 $ bt = \debug_backtrace ();
226221 if (
227- isset ($ bt [3 ])
228- && isset ($ bt [3 ]['file ' ])
229- && \basename ($ bt [3 ]['file ' ]) === 'current-page-helper.php '
222+ isset ($ bt [3 ]['file ' ])
223+ && \str_ends_with ($ bt [3 ]['file ' ], 'wordpress-seo/src/helpers/current-page-helper.php ' )
230224 ) {
231- return \get_queried_object_id () ;
225+ return null ;
232226 }
233- }
234- // return \get_queried_object_id();
235- return $ value ;
236- });
227+ return $ value ;
228+ });
229+ }
230+
231+ // /**
232+ // * Trick Yoast SEO for_current_page logic which determines the current indexable
233+ // *
234+ // * @see \Yoast\WP\SEO\Repositories\Indexable_Repository::for_current_page
235+ // *
236+ // * @param mixed $value
237+ // * @return mixed
238+ // */
239+ // \add_filter('pre_option_page_for_posts', function ($value) {
240+ // global $wp_current_filter;
241+ // if (
242+ // \is_array($wp_current_filter)
243+ // && isset($wp_current_filter[1])
244+ // && $wp_current_filter[1] === 'wp_robots'
245+ // ) {
246+ // $bt = \debug_backtrace();
247+ // if (
248+ // isset($bt[3])
249+ // && isset($bt[3]['file'])
250+ // && \basename($bt[3]['file']) === 'current-page-helper.php'
251+ // ) {
252+ // return \get_queried_object_id();
253+ // }
254+ // }
255+ // // return \get_queried_object_id();
256+ // return $value;
257+ // });
237258}
238259
239- \add_action ('pfcpt/template_redirect ' , __NAMESPACE__ . '\\set_page_for_custom_post_type ' );
260+ \add_action ('wp ' , __NAMESPACE__ . '\\set_page_for_custom_post_type ' );
0 commit comments