Automattic / jetpack

Security, performance, marketing, and design tools — Jetpack is made by WordPress experts to make WP sites safer and faster, and help you grow your traffic.
https://jetpack.com/
Other
1.59k stars 798 forks source link

Publicize: use get_the_excerpt() and the_content filter when building og:description #6181

Open snarfed opened 7 years ago

snarfed commented 7 years ago

hi! right now, when og:description comes from the post excerpt or content, it pulls them directly from the db, bypassing plugins that otherwise modify them via filter and hooks. this can be a problem for sites that depend on those modifications.

Steps to reproduce the issue

as an example, i author and store post content in markdown, and use a plugin to render that to HTML. jetpack currently puts the raw markdown content directly into og:description. e.g. with this post content:

I talked with [Sally](http://sally.com/) recently...

What I expected

I expected the post content to be rendered, then HTML tags stripped, resulting in:

<meta property="og:description" content="I talked with Sally recently...

What happened instead

instead, the raw markdown link syntax surfaced in og:description:

<meta property="og:description" content="I talked with [Sally]( recently...

...and ultimately also in the facebook preview and twitter card. :/

snarfed commented 7 years ago

the patch below fixes this for me. it's currently up and running on https://snarfed.org/ . i'm putting together a PR, but i wanted to give you all a heads up and check that this sounds ok first. thanks in advance!

@@ -100,10 +104,11 @@ function jetpack_og_tags() {

  $tags['og:url']         = get_permalink( $data->ID );
  if ( ! post_password_required() ) {
-     if ( ! empty( $data->post_excerpt ) ) {
-         $tags['og:description'] = preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $data->post_excerpt, array() ) ) );
+     $excerpt = get_the_excerpt( $data );
+     if ( ! empty( $excerpt ) ) {
+         $tags['og:description'] = preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $excerpt, array() ) ) );
      } else {
-         $exploded_content_on_more_tag = explode( '<!--more-->', $data->post_content );
+         $exploded_content_on_more_tag = explode( '<!--more-->', apply_filters( 'the_content', $data->post_content ) );
          $tags['og:description'] = wp_trim_words( preg_replace( '@https?://[\S]+@', '', strip_shortcodes( wp_kses( $exploded_content_on_more_tag[0], array() ) ) ) );
      }
  }
jeherve commented 7 years ago

Related: #2899

snarfed commented 7 years ago

fwiw, i briefly looked at a couple other markdown plugins just now that work the same way, ie store raw markdown in post_content and render it dynamically, and they both suffer from the same problem:

stale[bot] commented 6 years ago

This issue has been marked as stale. This happened because:

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.