Created
June 20, 2018 22:25
-
-
Save rachelslurs/246b698c6e8ef6ef5bfcdffcc34d77f8 to your computer and use it in GitHub Desktop.
An example of a plugin that chops content the pre Gutenberg way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Content Chopper | |
Plugin URI: https://github.com/rachelslurs | |
Description: A simple content chopper | |
Version: 1.0 | |
Author: Rachel Cantor | |
Author URI: https://github.com/rachelslurs | |
License: GPL2 | |
*/ | |
class ContentChopper { | |
private $plugin_path; | |
private $plugin_url; | |
function __construct() { | |
$this->plugin_path = plugin_dir_path( __FILE__ ); | |
$this->plugin_url = plugin_dir_url( __FILE__ ); | |
require_once( plugin_dir_path( __FILE__ ) . '/htmlawed.php' ); | |
add_action( 'init', array(&$this, 'init') ); | |
} | |
public static function init() { | |
//$more_is_being_used = strpos($content, 'class="more-link">') !== false; | |
if (is_singular()) { | |
wp_die('hello'); | |
add_filter( 'the_content', array(&$this, 'block_extra_content_from_displaying' ) ); | |
} | |
} | |
function block_extra_content_from_displaying( $content ) { | |
global $post; | |
$chunks = self::chop_it($post); | |
// We need to temporarily remove our filter so we can apply the other filters without an infinite loop | |
$content_needs_chopping = false; | |
if ( has_filter( 'the_content', array(&$this, 'block_extra_content_from_displaying' ) ) ) { | |
remove_filter( 'the_content', array(&$this, 'block_extra_content_from_displaying' ) ); | |
$content_needs_chopping = true; | |
} | |
// Run it through the Wordpress gauntlet | |
$snippet = apply_filters( 'the_content', $chunks['snippet'] ); | |
// add our filter back in | |
if ( $content_needs_chopping ) { | |
add_filter( 'the_content', array(&$this, 'block_extra_content_from_displaying' ) ); | |
} | |
$snippet = clean_snippet($snippet); | |
return "<pico>$snippet</pico>"; | |
} | |
function chop_it($post) { | |
$length_type = 'words'; // words or characters | |
$finish = 'sentence'; // exact, word, or sentence | |
$post_body = wpautop($post->post_content); | |
$p_tag = strpos( $post_body, '</p>' ); // find the string position of the first instance of '</p>', else false | |
$length = 40; // use this if $p_tag is false | |
$extended_content = get_extended( $post_body ); | |
$more_is_being_used = $extended_content['extended'] ? true : false; | |
$unfiltered_content = trim($post_body); | |
// If "read more" is on, set the snippet to the first extended content chunk; if not, we cut it ourselves | |
if ( $more_is_being_used) { | |
$snippet = $extended_content['main']; | |
} else { | |
// if the p tag is found, set snippet to be the first paragraph, else fallback to the old way of doing excerpts (40 words, ends on a sentence) | |
$snippet = !!$p_tag ? self::get_first_paragraph($unfiltered_content, $p_tag) : self::text_excerpt( $unfiltered_content, $length, $length_type, $finish ); | |
} | |
$len = strlen($snippet); | |
$remainder = trim( substr( $unfiltered_content, $len) ); | |
// $remainder = reverse_balance_tags($remainder); | |
wp_die($snippet); | |
return htmLawed($snippet); | |
} | |
function get_first_paragraph($text, $position_of_closing_p_tag) { | |
return substr($text, 0, $position_of_closing_p_tag) . '</p>'; | |
} | |
function text_excerpt( $text, $length, $length_type, $finish ) { | |
$tokens = array(); | |
$result = ''; | |
$w = 0; | |
// Divide the string into tokens; HTML tags, or words, followed by any whitespace | |
// (<[^>]+>|[^<>\s]+\s*) | |
preg_match_all( '/(<[^>]+>|[^<>\s]+)\s*/u', $text, $tokens ); | |
foreach ( $tokens[0] as $t ) { // Parse each token | |
if ( $w >= $length && 'sentence' != $finish ) { // Limit reached | |
break; | |
} | |
if ( $t[0] != '<' ) { // Token is not a tag | |
if ( $w >= $length && 'sentence' == $finish && preg_match( '/[\?\.\!]\s*$/uS', $t ) == 1 ) { // Limit reached, continue until ? . or ! occur at the end | |
$result .= trim( $t ); | |
break; | |
} | |
if ( 'words' == $length_type ) { // Count words | |
$w++; | |
} else { // Count/trim characters | |
$chars = trim( $t ); // Remove surrounding space | |
$c = strlen( $chars ); | |
if ( $c + $w > $length && 'sentence' != $finish ) { // Token is too long | |
$c = ( 'word' == $finish ) ? $c : $length - $w; // Keep token to finish word | |
$t = substr( $t, 0, $c ); | |
} | |
$w += $c; | |
} | |
} | |
// Append what's left of the token | |
$result .= $t; | |
} | |
return $result; | |
} | |
} | |
new ContentChopper(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment