Skip to content

Commit c13389c

Browse files
authored
Merge pull request #3 from nlemoine/wpseo
Wpseo
2 parents 08b2903 + 549594f commit c13389c

5 files changed

Lines changed: 171 additions & 85 deletions

File tree

phpstan.neon.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
parameters:
2-
level: 5
2+
level: 7
33
paths:
44
- src/
55
editorUrl: 'vscode://file/%%file%%:%%line%%'
66
scanDirectories:
77
- wp-content/plugins/wordpress-seo
88
scanFiles:
99
- vendor/php-stubs/acf-pro-stubs/acf-pro-stubs.php
10-
- vendor/wpsyntex/polylang-stubs/polylang-stubs.php
10+
- vendor/wpsyntex/polylang-stubs/polylang-stubs.php

src/integrations/polylang.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function get_translated_page_id_cache_key(string $language_slug): string
2323
* @see https://github.com/polylang/polylang/blob/88ee8ed65af4f92c0225e4faa46d5f3e640173ba/frontend/frontend-static-pages.php#L122-L146
2424
*
2525
* @param string $url not used
26-
* @param object $language language in which we want the translation
26+
* @param PLL_Language $language language in which we want the translation
2727
* @param int $queried_object_id id of the queried object
2828
*/
2929
function set_is_posts_page($url, $language, $queried_object_id): string
@@ -35,6 +35,13 @@ function set_is_posts_page($url, $language, $queried_object_id): string
3535
$GLOBALS['wp_query']->is_posts_page = true;
3636
return $url;
3737
}
38+
/**
39+
* Undocumented function
40+
*
41+
* @param string $url
42+
* @param PLL_Language $language
43+
* @param int $queried_object_id
44+
*/
3845
function reset_is_posts_page($url, $language, $queried_object_id): string
3946
{
4047
if (!\is_home()) {
@@ -49,6 +56,10 @@ function reset_is_posts_page($url, $language, $queried_object_id): string
4956

5057
/**
5158
* Set translasted IDs
59+
*
60+
* @param int[] $page_ids
61+
*
62+
* @return int[]
5263
*/
5364
function set_translated_page_id(array $page_ids): array
5465
{
@@ -149,7 +160,11 @@ function get_default_language_page_id(int $page_id): int
149160
if (!empty(\pll_current_language())) {
150161
return $page_id;
151162
}
152-
$default_page_id = \pll_get_post($page_id, \pll_default_language());
163+
$default_language = \pll_default_language();
164+
if (!$default_language) {
165+
return $page_id;
166+
}
167+
$default_page_id = \pll_get_post($page_id, $default_language);
153168
return $default_page_id ? $default_page_id : $page_id;
154169
}
155170
\add_filter('pfcpt/post_type_from_id/page_id', __NAMESPACE__ . '\\get_default_language_page_id');
@@ -159,7 +174,7 @@ function get_default_language_page_id(int $page_id): int
159174
*
160175
* @param int $post_id
161176
* @param WP_Post $post
162-
* @param array $translations
177+
* @param int[] $translations
163178
*/
164179
function on_page_for_custom_post_type_change($post_id, $post, $translations): void
165180
{
@@ -176,6 +191,9 @@ function on_page_for_custom_post_type_change($post_id, $post, $translations): vo
176191
}
177192

178193
$post_type = \array_search($default_page_for_post_type_id, $page_ids, true);
194+
if (!\is_string($post_type)) {
195+
return;
196+
}
179197

180198
// Flush cache/rules
181199
$pfcpt->flush_rewrite_rules($post_type);

src/integrations/wordpress-seo.php

Lines changed: 80 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,33 @@
99
use Yoast\WP\SEO\Repositories\Indexable_Repository;
1010
use 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);
@@ -17,10 +44,12 @@
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');

src/plugin.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ public function update_post_type_args(array $args, string $post_type): array
198198
public function get_page_slug(int $page_id): ?string
199199
{
200200
$page_url = \get_permalink($page_id);
201-
return $page_url ? \trim(\parse_url($page_url, PHP_URL_PATH), '/') : null;
201+
if ($page_url === false) {
202+
return null;
203+
}
204+
$page_path = \parse_url($page_url, PHP_URL_PATH);
205+
return \is_string($page_path) ? \trim($page_path, '/') : null;
202206
}
203207

204208
/**
@@ -365,8 +369,10 @@ public function page_for_post_type_field(array $args): void
365369
/**
366370
* Add an indicator to show if a page is set as a post type archive.
367371
*
368-
* @param array $post_states an array of post states to display after the post title
372+
* @param string[] $post_states an array of post states to display after the post title
369373
* @param WP_Post $post the current post object
374+
*
375+
* @return string[]
370376
*/
371377
public function display_post_states($post_states, $post): array
372378
{
@@ -524,7 +530,9 @@ public function is_query_page_for_custom_post_type(?WP_Query $query = null): boo
524530
/**
525531
* Change the template hierarchy on pages for custom post type
526532
*
527-
* @param array<string> $templates
533+
* @param string[] $templates
534+
*
535+
* @return string[]
528536
*/
529537
public function set_home_template_hierarchy(array $templates): array
530538
{
@@ -579,6 +587,16 @@ public function set_page_for_custom_post_type_query(WP_Query $query): void
579587
$query->{$this->get_conditional_name($post_type)} = true;
580588
$query->set('post_type', $post_type);
581589
$query->{self::QUERY_VAR_IS_PFCPT} = $post_type;
590+
$query->is_posts_page = true;
591+
592+
// Prevent WP from mistakenly thinking this is a front page
593+
// When 'posts' is set as show_on_front
594+
// https://github.com/WordPress/wordpress-develop/blob/781953641607c4d5b0743a6924af0e820fd54871/src/wp-includes/class-wp-query.php#L4323-L4325
595+
if (\get_option('show_on_front') === 'posts') {
596+
\add_filter('pre_option_show_on_front', function ($value) {
597+
return null;
598+
});
599+
}
582600

583601
\add_filter('home_template_hierarchy', [$this, 'set_home_template_hierarchy']);
584602
\add_filter('frontpage_template_hierarchy', '__return_empty_array');
@@ -616,6 +634,8 @@ public function get_page_id_from_query(WP_Query $query): ?int
616634

617635
/**
618636
* Get page ids.
637+
*
638+
* @return int[]
619639
*/
620640
public function get_page_ids(bool $apply_filters = true): array
621641
{
@@ -798,6 +818,8 @@ private function delete_option(string $post_type): void
798818

799819
/**
800820
* Get post types.
821+
*
822+
* @return WP_Post_Type[]
801823
*/
802824
private function get_post_types(): array
803825
{

0 commit comments

Comments
 (0)