File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/lib/custom-excerpt.php
<?php
/*//////////////////////////////////////
// Set Excerpt Length for The Loop
//////////////////////////////////////*/
if ( ! class_exists( 'GTM_Excerpt' ) ) {
class GTM_Excerpt {
private static $instance;
public $length;
public $is_applied = false; // For checking adding of actions/filters
public $is_alt = false;
public $alt_length;
public static function getInstance() {
if ( is_null( static::$instance ) ) {
static::$instance = new self();
}
return static::$instance;
}
/* Set excerpt length */
public static function set( $length ) {
$instance = static::getInstance();
$instance->length = intval( $length );
$instance->apply();
}
/* Set alternative length */
public static function set_alt( $alt_length ) {
$instance = static::getInstance();
$instance->is_alt = true;
$instance->alt_length = $alt_length;
}
/* Revert alternative length */
public static function revert_alt() {
$instance = static::getInstance();
$instance->is_alt = false;
$instance->alt_length = null;
}
/* Apply actions/filters for the loop */
public function apply() {
if ( ! $this->is_applied ) {
add_filter( 'excerpt_length', [$this, 'excerpt_length'], 99 );
add_action( 'loop_end', [$this, 'done'] );
add_action( 'loop_no_results', [$this, 'done'] ); // WP 4.9+
$this->is_applied = true;
}
}
/* Override current excerpt length */
public function excerpt_length( $length ) {
if ( $this->is_alt ) {
return $this->alt_length;
} else {
return $this->length;
}
}
/* Remove custom excerpt length after loop */
public function done( $wp_query = null ) {
/* MY ADDED FILTER */ remove_filter( 'excerpt_length', [$this, 'excerpt_length'], 99 );
remove_action( 'loop_end', [$this, 'done'] );
remove_action( 'loop_no_results', [$this, 'done'] ); // WP 4.9+
$this->is_applied = false;
}
}
}
// Get the custom excerpt only, return empty when NO custom excerpt inputted
function gtm_get_custom_excerpt( $post = null ) {
if ( has_excerpt( $post ) ) {
return get_the_excerpt( $post );
}
}
// Get the automatic excerpt only, return empty when custom excerpt inputted
function gtm_get_automatic_excerpt_only( $post = null ) {
if ( ! has_excerpt( $post ) ) {
return get_the_excerpt( $post );
}
}
// Always get an automatic excerpt
function gtm_get_automatic_excerpt( $post = null ) {
$post = get_post( $post );
if ( empty( $post ) ) {
return '';
}
if ( post_password_required( $post ) ) {
return esc_html__( 'There is no excerpt because this is a protected post.', 'gutenmate' );
}
return apply_filters( 'get_the_excerpt', '', $post );
}