aubreypwd / WordPress

[DEPRECATED] See https://github.com/aubreypwd/wordpress-develop
https://github.com/WordPress/WordPress
Other
0 stars 0 forks source link

#50002 — Hack in get_sample_permalink() can cause filters like post_link, and pre_post_link to have inaccurate post_status in admin #5

Open aubreypwd opened 4 years ago

aubreypwd commented 4 years ago

Add a filter for post_link and see that, in the admin, sometimes the \WP_Post comes back with ->post_status set to public even when the post is draft. It can be confirmed by using get_post_status which seems to give the correct post status.

aubreypwd commented 4 years ago

image

As you can see the permalink structure, when viewing a post in the admin (that's in draft), seems to go through post_link filter, but when it does the status is wrong on the post.

aubreypwd commented 4 years ago

Found this coming from https://github.com/aubreypwd/WordPress/blob/9ff4499281663b0c772787fd4a60538288f842e9/wp-includes/link-template.php#L119

aubreypwd commented 4 years ago

Found that on "file": "/app/public/wp-admin/includes/post.php" "line": 1360, my draft post always has publish as post_status...

aubreypwd commented 4 years ago

https://github.com/aubreypwd/WordPress/blob/9ff4499281663b0c772787fd4a60538288f842e9/wp-admin/includes/post.php#L1346

This is our culprit, here the post_status, for some reason, is being set to publish even though it's not really true?

aubreypwd commented 4 years ago

https://github.com/aubreypwd/WordPress/commit/5fbca12c9e2a462c2ae2d4d3d71dae84473d1a11

(Also https://core.trac.wordpress.org/changeset/6633) seems to be the source of the change/addition by "matt" and is 12 year old code...

aubreypwd commented 4 years ago

Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.

Not seeing a current ticket in Trac: https://core.trac.wordpress.org/search?q=post_status+get_permalink+post_link&noquickjump=1&changeset=on&ticket=on

aubreypwd commented 4 years ago

When the "hack" is present, we get:

http://wordpress.test/%postname%/

When it is commented out, we get:

http://wordpress.test/?p=1

So it seems the whole goal of setting the post (untruthfully) to publish is to get the %postname% format.

aubreypwd commented 4 years ago

With the hack present, this happens:

https://github.com/aubreypwd/WordPress/blob/9ff4499281663b0c772787fd4a60538288f842e9/wp-includes/link-template.php#L168

When it is not, that if is not processed.

aubreypwd commented 4 years ago
[24-Apr-2020 18:32:06 UTC] stdClass Object
(
    [/app/public/wp-content/mu-plugins/dev.php] => 5
    [0] => http://wordpress.test/?p=1
    [1] => draft
)

[24-Apr-2020 18:32:06 UTC] stdClass Object
(
    [/app/public/wp-content/mu-plugins/dev.php] => 5
    [0] => http://wordpress.test/%postname%/
    [1] => publish
)

[24-Apr-2020 18:32:06 UTC] stdClass Object
(
    [/app/public/wp-content/mu-plugins/dev.php] => 5
    [0] => http://wordpress.test/?p=1
    [1] => draft
)

[24-Apr-2020 18:32:06 UTC] stdClass Object
(
    [/app/public/wp-content/mu-plugins/dev.php] => 5
    [0] => http://wordpress.test/?p=1
    [1] => draft
)

Logging the current result of :

<?php

add_filter( 'post_link', function( string $permalink, WP_Post $post, bool $leavename ) {
    // if ( stristr( $permalink, '%' ) ) {
        error_log( print_r( (object) [ __FILE__ => __LINE__,
            $permalink,
            $post->post_status,
        ], true ) );
    // }
}, 10, 3 );
aubreypwd commented 4 years ago

Got some code written up:

Diff: https://github.com/aubreypwd/WordPress/compare/5.4...aubreypwd:issue/5 Patch: https://github.com/aubreypwd/WordPress/compare/5.4...aubreypwd:issue/5.patch

This moves the hack to the right place where we don't have to abuse post_status to achieve the same affect, and it uses the $post->filter = 'sample' flag instead to decide when to perform the conditions of the if above.

aubreypwd commented 4 years ago

Trac ticket: https://core.trac.wordpress.org/ticket/50002