Yoast / wordpress-seo

Yoast SEO for WordPress
https://yoast.com/wordpress/plugins/seo/
Other
1.76k stars 886 forks source link

Receiving a 'previously used keyphrase' SEO assessment on an already deleted term #19632

Open mhkuu opened 1 year ago

mhkuu commented 1 year ago

Original JIRA task (from IM board)

Please give us a description of what happened

By default, if you create a term (e.g., category or tag), it will use the 'name' of the term as the default focus keyphrase. However, if you were to create two terms, and then change the focus keyphrase so that they both share the same focus keyphrase (e.g. Citizens), you would then receive the assessment of:

Previously used keyphrase: You've used this keyphrase once before. Do not use your keyphrase more than once.

Once you delete one of the other terns (there's no 'trash' filter and it mentions permanently deleting), you would expect that the assessment wouldn't appear anymore for the remaining term. But it still shows the same assessment, and when you click on the text link in the 'once before' part of the assessment, it will show you:

You attempted to edit an item that doesn’t exist. Perhaps it was deleted?

You can resolve this for posts by 'permanently deleting' it when going to the 'trash' filter, but there's no option on this for terms. Updating the term, or resetting the data using the Yoast Test Helper plugin doesn't resolve the issue.

To Reproduce

Step-by-step reproduction instructions

  1. Activate Yoast SEO
  2. Create a tag, call it example
  3. Edit example, confirm the keyphrase is example
  4. Confirm receive this feedback in the SEO analysis.

    Previously used keyphrase: You've not used this keyphrase before, very good.

  5. Create a tag, call it duplicate
  6. Edit duplicate, change the keyphrase from duplicate to example.
  7. Edit example.
  8. Confirm you receive this feedback in the SEO analysis.

    Previously used keyphrase: You've used this keyphrase once before. Do not use your keyphrase more than once.

  9. Remove duplicate.
  10. Edit example.
  11. Confirm you (still) receive this feedback in the SEO analysis.

    Previously used keyphrase: You've used this keyphrase once before. Do not use your keyphrase more than once.

  12. Confirm that clicking on once before gives an error message.

Expected results

  1. After deletion, I expect the feedback for "previously used keyphrase" to disappear.

Actual results

  1. After deletion, the feedback for "previously used keyphrase" persists.

Screenshots, screen recording, code snippet

See some screenshots in the Jira issue.

Technical info

Used versions

leonidasmi commented 1 year ago

After some investigation together with @marinakoleva , this is a technical summary of the culprit:

The source of truth for assessing if a keyword is used elsewhere in the site is the wpseo_taxonomy_meta option (that's being used in the return WPSEO_Taxonomy_Meta::get_keyword_usage( $focuskw, $this->term->term_id, $this->term->taxonomy ); line of the get_focus_keyword_usage() method in WPSEO_Term_Metabox_Formatter.

The issue at hand is that when a term is being deleted, we don't update the wpseo_taxonomy_meta option with this new information, so any keyword usage is not being updated.

A possible/probable solution is to hook into the delete_term action in WPSEO_Taxonomy class (much like we hook into the edit_term action when a term is updated), so that we can update the wpseo_taxonomy_meta option.

Once we do that, then wpseoScriptData.metabox.keyword_usage will contain the updated information even if a term has been deleted.

leonidasmi commented 1 year ago

For the dev that picks this up:

One thing that strikes me as weird is that we have the wpseo_taxonomy_meta option as the source of the truth here, even though that info exists in the terms' indexables as well.

Maybe a good candidate for improvement in the future?