awesomemotive / all-in-one-seo-pack

All in One SEO plugin for WordPress SEO
https://aioseo.com
339 stars 155 forks source link

aioseo_save_post filter not saving/persisting data #3219

Closed calebsmithdev closed 6 months ago

calebsmithdev commented 6 months ago

Following the docs here, I am trying to use the aioseo_save_post filter to automatically adjust the robots to noindex and nofollow depending on the checked state of an ACF form value. Below is an example of my code.

public function adjust_noindex_ungated($post) {
        $hero_group = get_field('hero', $post->post_id);
        $is_gated = $hero_group['show_the_form'] ?? false;
        if(!$is_gated) {
            $post->robots_default = false;
            $post->robots_nofollow = true;
            $post->robots_noindex = true;
        }

        return $post;
}

The $post value looks correct, but the values do not save and I see the values being reverted back to the default data state once it hits this line of code.

https://github.com/awesomemotive/all-in-one-seo-pack/blob/fcdbdd48252ca408eb020af2e2af5f5f333a4bf0/app/Common/Models/Post.php#L280

Shouldn't the filter happen after the sanitization and setting of default data?

arnaudbroes commented 6 months ago

@calebsmithdev it indeed looks like this filter wasn't very well thought out. We definitely still want to sanitize whatever's passed through the filter, just for security purposes, but what the filter really ought to be doing is filter the new data instead of the existing data on the AIOSEO post object.

I'm going to update the code to this in our 4.6.2 update -

$thePost = self::getPost( $postId );
$data    = apply_filters( 'aioseo_save_post', $data, $thePost );

// Before setting the data, we check if the title/description are the same as the defaults and clear them if so.
$data    = self::checkForDefaultFormat( $postId, $thePost, $data );
$thePost = self::sanitizeAndSetDefaults( $postId, $thePost, $data );

I assume that will work for you?

calebsmithdev commented 6 months ago

That works perfectly! Thanks for taking a look at this.

arnaudbroes commented 6 months ago

@calebsmithdev no problem, thanks for confirming. This has been staged for our 4.6.2 release so I'll be closing the issue now.