| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace JP\CC\Site; |
| 5 |
|
| 6 |
use JP\CC\Options; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
/** |
| 14 |
* Class Posts |
| 15 |
* |
| 16 |
* @package JP\CC\Site |
| 17 |
*/ |
| 18 |
class Posts { |
| 19 |
|
| 20 |
public static function init() { |
| 21 |
add_action( 'the_content', array( __CLASS__, 'the_content' ), 1000 ); |
| 22 |
add_filter( 'jp_cc_restricted_message', array( __CLASS__, 'restricted_message_filter' ), 10, 1 ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @param $content |
| 27 |
* |
| 28 |
* @return mixed|string |
| 29 |
*/ |
| 30 |
public static function the_content( $content ) { |
| 31 |
global $post; |
| 32 |
|
| 33 |
if ( ! $post || ! is_object( $post ) || $post->ID <= 0 ) { |
| 34 |
return $content; |
| 35 |
} |
| 36 |
|
| 37 |
if ( ! isset( Restrictions::$protected_posts[ $post->ID ] ) ) { |
| 38 |
Restrictions::$protected_posts[ $post->ID ] = Restrictions::restricted_content(); |
| 39 |
} |
| 40 |
|
| 41 |
$restricted_content = Restrictions::$protected_posts[ $post->ID ]; |
| 42 |
|
| 43 |
if ( ! $restricted_content ) { |
| 44 |
return $content; |
| 45 |
} |
| 46 |
|
| 47 |
if ( isset( $restricted_content['override_default_message'] ) ) { |
| 48 |
$message = $restricted_content['custom_message']; |
| 49 |
} else { |
| 50 |
$message = Options::get( 'default_denial_message', '' ); |
| 51 |
} |
| 52 |
|
| 53 |
if ( empty( $message ) ) { |
| 54 |
$message = __( 'This content is restricted.', 'content-control' ); |
| 55 |
} |
| 56 |
|
| 57 |
return static::format_message( do_shortcode( $message ) ); |
| 58 |
} |
| 59 |
|
| 60 |
public static function restricted_message_filter( $message ) { |
| 61 |
if ( is_feed() ) { |
| 62 |
return $message; |
| 63 |
} |
| 64 |
return do_shortcode( wpautop( $message ) ); |
| 65 |
} |
| 66 |
|
| 67 |
public static function format_message( $message ) { |
| 68 |
global $post; |
| 69 |
|
| 70 |
$restriction = Restrictions::get_rules( $post->ID ); |
| 71 |
|
| 72 |
if ( ! empty( $restriction['show_excerpts'] ) ) { |
| 73 |
$excerpt_length = 50; |
| 74 |
|
| 75 |
if ( has_filter( 'jp_cc_filter_excerpt_length' ) ) { |
| 76 |
$excerpt_length = apply_filters( 'jp_cc_filter_excerpt_length', $excerpt_length ); |
| 77 |
} |
| 78 |
|
| 79 |
$excerpt = static::excerpt_by_id( $post, $excerpt_length ); |
| 80 |
$message = apply_filters( 'jp_cc_restricted_message', $message ); |
| 81 |
$message = $excerpt . $message; |
| 82 |
} else { |
| 83 |
$message = apply_filters( 'jp_cc_restricted_message', $message ); |
| 84 |
} |
| 85 |
|
| 86 |
return $message; |
| 87 |
} |
| 88 |
|
| 89 |
public static function excerpt_by_id( $post, $length = 50, $tags = '<a><em><strong><blockquote><ul><ol><li><p>', $extra = ' . . .' ) { |
| 90 |
if ( is_int( $post ) ) { |
| 91 |
// get the post object of the passed ID |
| 92 |
$post = get_post( $post ); |
| 93 |
} elseif ( ! is_object( $post ) ) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
$more = false; |
| 98 |
|
| 99 |
if ( has_excerpt( $post->ID ) ) { |
| 100 |
$the_excerpt = $post->post_excerpt; |
| 101 |
} elseif ( strstr( $post->post_content, '<!--more-->' ) ) { |
| 102 |
$more = true; |
| 103 |
$length = strpos( $post->post_content, '<!--more-->' ); |
| 104 |
$the_excerpt = $post->post_content; |
| 105 |
} else { |
| 106 |
$the_excerpt = $post->post_content; |
| 107 |
} |
| 108 |
|
| 109 |
$tags = apply_filters( 'jp_cc_excerpt_tags', $tags ); |
| 110 |
|
| 111 |
if ( $more ) { |
| 112 |
$the_excerpt = strip_shortcodes( strip_tags( stripslashes( substr( $the_excerpt, 0, $length ) ), $tags ) ); |
| 113 |
} else { |
| 114 |
$the_excerpt = strip_shortcodes( strip_tags( stripslashes( $the_excerpt ), $tags ) ); |
| 115 |
$the_excerpt = preg_split( '/\b/', $the_excerpt, $length * 2 + 1 ); |
| 116 |
$excerpt_waste = array_pop( $the_excerpt ); |
| 117 |
$the_excerpt = implode( $the_excerpt ); |
| 118 |
$the_excerpt .= $extra; |
| 119 |
} |
| 120 |
|
| 121 |
return wpautop( $the_excerpt ); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|