Automattic / sensei

Sensei LMS - Online Courses, Quizzes, & Learning
https://senseilms.com
GNU General Public License v2.0
545 stars 198 forks source link

Allow filtering course excerpts #1034

Closed ragulka closed 9 years ago

ragulka commented 9 years ago

I can see that based on https://github.com/woothemes/sensei/issues/91, excerpts are not allowed to be auto-generated, which makes sense if the content may contain sensitive details about the course.

However, the excerpt is simply outputted by doing echo $post->post_excerpt, which does not allow applying any filters to the excerpt. My initial idea was that we could simply apply the get_the_excerpt filters, but this would re-enable auto-generated excerpts, as wp_trim_excerpt is also hooked to get_the_excerpt and is responsible for generating excerpts.

I think it would still be nice to work around this some other way that would still allow excerpts to be filtered. One idea is to create a wrapper function that would temporarily remove the wp_trim_excerpt filter, like this:

function sensei_get_excerpt( $post_id ) {

  if ( is_int( $post_id ) ) {
    $post = get_post( $post_id );
  }
  else if ( ! $post_id ) {
    global $post;
  }

  // Temporarily disable wp_trim_excerpt so that the excerpt
  // will not be autp.generated if empty
  remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );

  // Apply filters to the excerpt
  $excerpt = apply_filters( 'get_the_excerpt', $post->post_excerpt );

  // Re-enable wp_trim_excerpt
  add_filter( 'get_the_excerpt', 'wp_trim_excerpt' );

  return $excerpt;
}

// usage:
echo sensei_get_excerpt( $course->ID );

WordPress trims only auto-generated excerpts anyway, so this would have no effects on manually created excerpts at all.

The only problem I've found with this is that some Woo Themes (for example Canvas) unhook wp_trim_excerpt and use their own woo_trim_excerpt, which produces unexpected results (auto-generates the excerpt) in this case. It would be easy to unhook woo_trim_excerpt as well, but it's not hooked to get_the_excerpt, but is rather called from within woo_remove_dropcap_from_excerpts, and so, cannot be unhooked :(

I'm not sure why it is designed like that, I'm pretty sure there are more compatible ways to do it.

Finally - as a use case, this would make Sensei compatible with WooCommerce Memberships content restriction messages with no changes required on Memberships part.

EDIT: digging through the Sensei codebase it seems that excerpts are still auto-generated (and thus, sensitive content is visible to public, not hidden like the original intent has been) here and here.

dwainm commented 9 years ago

Fixed and merged into release 1.9 here: https://github.com/woothemes/sensei/pull/1037