first commit

This commit is contained in:
2023-09-12 21:41:04 +02:00
commit 3361a7f053
13284 changed files with 2116755 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
<?php
namespace JP\CC\Site;
use JP\CC\Options;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Feeds {
public static function init() {
add_action( 'the_excerpt', array( __CLASS__, 'filter_feed_posts' ) );
add_action( 'the_content', array( __CLASS__, 'filter_feed_posts' ) );
add_filter( 'jp_cc_restricted_message', array( __CLASS__, 'restricted_message_filter' ), 10, 1 );
}
public static function filter_feed_posts( $content ) {
global $post;
if ( ! is_feed() ) {
return $content;
}
if ( ! isset( Restrictions::$protected_posts[ $post->ID ] ) ) {
Restrictions::$protected_posts[ $post->ID ] = Restrictions::restricted_content();
}
$restricted_content = Restrictions::$protected_posts[ $post->ID ];
if ( ! $restricted_content ) {
return $content;
}
if ( isset( $restricted_content['override_default_message'] ) ) {
$message = $restricted_content['custom_message'];
} else {
$message = Options::get( 'default_denial_message', '' );
// custom messages could include shortcodes.
}
if ( empty( $message ) ) {
$message = __( 'This content is restricted.', 'content-control' );
}
//return Posts::format_message( $message );
return Posts::format_message( do_shortcode( $message ) );
}
public static function restricted_message_filter( $message ) {
if ( ! is_feed() ) {
return $message;
}
return do_shortcode( $message );
}
}

View File

@@ -0,0 +1,124 @@
<?php
namespace JP\CC\Site;
use JP\CC\Options;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Posts
*
* @package JP\CC\Site
*/
class Posts {
public static function init() {
add_action( 'the_content', array( __CLASS__, 'the_content' ), 1000 );
add_filter( 'jp_cc_restricted_message', array( __CLASS__, 'restricted_message_filter' ), 10, 1 );
}
/**
* @param $content
*
* @return mixed|string
*/
public static function the_content( $content ) {
global $post;
if ( ! $post || ! is_object( $post ) || $post->ID <= 0 ) {
return $content;
}
if ( ! isset( Restrictions::$protected_posts[ $post->ID ] ) ) {
Restrictions::$protected_posts[ $post->ID ] = Restrictions::restricted_content();
}
$restricted_content = Restrictions::$protected_posts[ $post->ID ];
if ( ! $restricted_content ) {
return $content;
}
if ( isset( $restricted_content['override_default_message'] ) ) {
$message = $restricted_content['custom_message'];
} else {
$message = Options::get( 'default_denial_message', '' );
}
if ( empty( $message ) ) {
$message = __( 'This content is restricted.', 'content-control' );
}
return static::format_message( do_shortcode( $message ) );
}
public static function restricted_message_filter( $message ) {
if ( is_feed() ) {
return $message;
}
return do_shortcode( wpautop( $message ) );
}
public static function format_message( $message ) {
global $post;
$restriction = Restrictions::get_rules( $post->ID );
if ( ! empty( $restriction['show_excerpts'] ) ) {
$excerpt_length = 50;
if ( has_filter( 'jp_cc_filter_excerpt_length' ) ) {
$excerpt_length = apply_filters( 'jp_cc_filter_excerpt_length', $excerpt_length );
}
$excerpt = static::excerpt_by_id( $post, $excerpt_length );
$message = apply_filters( 'jp_cc_restricted_message', $message );
$message = $excerpt . $message;
} else {
$message = apply_filters( 'jp_cc_restricted_message', $message );
}
return $message;
}
public static function excerpt_by_id( $post, $length = 50, $tags = '<a><em><strong><blockquote><ul><ol><li><p>', $extra = ' . . .' ) {
if ( is_int( $post ) ) {
// get the post object of the passed ID
$post = get_post( $post );
} elseif ( ! is_object( $post ) ) {
return false;
}
$more = false;
if ( has_excerpt( $post->ID ) ) {
$the_excerpt = $post->post_excerpt;
} elseif ( strstr( $post->post_content, '<!--more-->' ) ) {
$more = true;
$length = strpos( $post->post_content, '<!--more-->' );
$the_excerpt = $post->post_content;
} else {
$the_excerpt = $post->post_content;
}
$tags = apply_filters( 'jp_cc_excerpt_tags', $tags );
if ( $more ) {
$the_excerpt = strip_shortcodes( strip_tags( stripslashes( substr( $the_excerpt, 0, $length ) ), $tags ) );
} else {
$the_excerpt = strip_shortcodes( strip_tags( stripslashes( $the_excerpt ), $tags ) );
$the_excerpt = preg_split( '/\b/', $the_excerpt, $length * 2 + 1 );
$excerpt_waste = array_pop( $the_excerpt );
$the_excerpt = implode( $the_excerpt );
$the_excerpt .= $extra;
}
return wpautop( $the_excerpt );
}
}

View File

@@ -0,0 +1,166 @@
<?php
namespace JP\CC\Site;
use JP\CC\Options;
use JP\CC\Conditions;
use JP\CC\Is;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Restrictions {
public static $protected_posts = array();
public static function init() {
add_action( 'template_redirect', array( __CLASS__, 'template_redirect' ) );
}
public static function get_rules( $post_id ) {
return isset( static::$protected_posts[ $post_id ] ) ? static::$protected_posts[ $post_id ] : false;
}
public static function template_redirect() {
global $post;
$restriction = static::restricted_content();
if ( ! $restriction ) {
return;
}
// Cache the post if restricted.
if ( is_singular() && ! is_archive()) {
static::$protected_posts[ $post->ID ] = $restriction;
}
if ( 'redirect' === $restriction['protection_method'] ) {
static::redirect( $restriction );
}
}
public static function restricted_content() {
$restrictions = Options::get( 'restrictions' );
$restriced_content = false;
if ( ! $restrictions || empty( $restrictions ) ) {
return $restriced_content;
}
foreach ( $restrictions as $restriction ) {
if ( static::content_match( $restriction ) ) {
$roles = ! empty( $restriction['roles'] ) ? $restriction['roles'] : array();
if ( Is::access_blocked( $restriction['who'], $roles, array( 'context' => 'content_restrictions' ) ) ) {
$restriced_content = $restriction;
}
break;
}
}
return $restriced_content;
}
public static function redirect( $restriction ) {
if ( empty( $restriction['redirect_type'] ) ) {
return;
}
$redirect = false;
switch ( $restriction['redirect_type'] ) {
case 'login':
$redirect = wp_login_url( static::current_url() );
break;
case 'home':
$redirect = home_url();
break;
case 'custom':
$redirect = $restriction['redirect_url'];
break;
default:
// Do not redirect if not one of our values.
}
if ( $redirect ) {
wp_redirect( $redirect );
exit;
}
}
public static function content_match( $restriction ) {
$content_match = false;
if ( empty( $restriction['conditions'] ) ) {
return $content_match;
}
// All Groups Must Return True. Break if any is false and set $loadable to false.
foreach ( $restriction['conditions'] as $group => $conditions ) {
// Groups are false until a condition proves true.
$group_check = false;
// At least one group condition must be true. Break this loop if any condition is true.
foreach ( $conditions as $condition ) {
// The not operand value is missing, set it to false.
if ( ! isset ( $condition['not_operand'] ) ) {
$condition['not_operand'] = false;
}
$match = ( ! $condition['not_operand'] && static::check_condition( $condition ) ) || ( $condition['not_operand'] && ! static::check_condition( $condition ) );
// If any condition passes, set $group_check true and break.
if ( $match ) {
$group_check = true;
break;
}
}
// If any group of conditions doesn't pass, popup is not loadable.
if ( ! $group_check ) {
$content_match = false;
break;
} else {
$content_match = true;
}
}
return $content_match;
}
/**
* @return string
*/
public static function current_url() {
$protocol = ( ! empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] !== 'off' ) || $_SERVER['SERVER_PORT'] == 443 ? 'https://' : 'http://';
return $protocol . $_SERVER['HTTP_HOST'] . $_SERVER["REQUEST_URI"];
}
public static function check_condition( $settings = array() ) {
$condition = Conditions::instance()->get_condition( $settings['target'] );
if ( ! $condition ) {
return false;
}
return call_user_func( $condition['callback'], $settings );
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace JP\CC\Site;
use JP\CC\Helpers;
use JP\CC\Widget;
use JP\CC\Is;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class JP\CC\Site\Widgets
*/
class Widgets {
/**
* Initialize Widgets
*/
public static function init() {
add_action( 'sidebars_widgets', array( __CLASS__, 'exclude_widgets' ) );
}
/**
* Checks for and excludes widgets based on their chosen options.
*
* @param array $widget_areas An array of widget areas and their widgets.
*
* @return array The modified $widget_area array.
*/
public static function exclude_widgets( $widget_areas ) {
if ( is_admin() || Helpers::is_customize_preview() ) {
return $widget_areas;
}
foreach ( $widget_areas as $widget_area => $widgets ) {
if ( ! empty( $widgets ) && 'wp_inactive_widgets' != $widget_area ) {
foreach ( $widgets as $position => $widget_id ) {
$options = Widget::get_options( $widget_id );
// If not accessible then exclude this item.
$exclude = ! Is::accessible( $options['which_users'], $options['roles'], 'widget' );
$exclude = apply_filters( 'jp_cc_should_exclude_widget', $exclude, $options, $widget_id );
// unset non-visible item
if ( $exclude ) {
unset( $widget_areas[ $widget_area ][ $position ] );
}
}
}
}
return $widget_areas;
}
}