| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
class PS_Public { |
| 7 |
/** |
| 8 |
* Contains instance or null |
| 9 |
* |
| 10 |
* @var object|null |
| 11 |
*/ |
| 12 |
private static $instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor for PS_Public |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
add_shortcode( 'content_protector', array($this, 'render_shortcode') ); |
| 19 |
add_shortcode( 'passster', array($this, 'render_shortcode') ); |
| 20 |
add_filter( 'the_content', array($this, 'filter_the_content') ); |
| 21 |
add_filter( 'acf_the_content', array($this, 'filter_the_content') ); |
| 22 |
add_filter( 'get_the_excerpt', array($this, 'filter_the_content') ); |
| 23 |
add_action( 'template_redirect', array($this, 'check_global_proctection') ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns instance of PS_Public. |
| 28 |
* |
| 29 |
* @return object |
| 30 |
*/ |
| 31 |
public static function get_instance() { |
| 32 |
if ( null === self::$instance ) { |
| 33 |
self::$instance = new self(); |
| 34 |
} |
| 35 |
return self::$instance; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Render the Passster shortcode. |
| 40 |
* |
| 41 |
* @param array $atts array of attributes. |
| 42 |
* @param string|null $content the current content. |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function render_shortcode( array $atts, string $content = null ) : string { |
| 47 |
// check if valid before restrict anything. |
| 48 |
$valid = PS_Conditional::is_valid( $atts ); |
| 49 |
$options = get_option( 'passster' ); |
| 50 |
if ( $valid ) { |
| 51 |
if ( !empty( $atts['area'] ) ) { |
| 52 |
$area_id = esc_html( $atts['area'] ); |
| 53 |
$area = get_post( $area_id ); |
| 54 |
$content = $area->post_content; |
| 55 |
do_action( 'passster_content_unlocked' ); |
| 56 |
return apply_filters( 'the_content', str_replace( '{post-id}', get_the_id(), $content ) ); |
| 57 |
} else { |
| 58 |
$content = apply_filters( 'the_content', $content ); |
| 59 |
do_action( 'passster_content_unlocked' ); |
| 60 |
return apply_filters( 'passster_content', $content ); |
| 61 |
} |
| 62 |
} |
| 63 |
// do nothing if no atts. |
| 64 |
if ( empty( $atts ) ) { |
| 65 |
return $content; |
| 66 |
} |
| 67 |
// Set default form. |
| 68 |
$form = PS_Form::get_password_form(); |
| 69 |
// Password. |
| 70 |
if ( !empty( $atts['password'] ) ) { |
| 71 |
$form = PS_Form::get_password_form(); |
| 72 |
$form = str_replace( '[PASSSTER_TYPE]', 'password', $form ); |
| 73 |
} |
| 74 |
// Area. |
| 75 |
if ( !empty( $atts['area'] ) ) { |
| 76 |
$area_id = esc_html( $atts['area'] ); |
| 77 |
$form = str_replace( '[PASSSTER_AREA]', $area_id, $form ); |
| 78 |
} |
| 79 |
// Page. |
| 80 |
if ( !empty( $atts['protection'] ) ) { |
| 81 |
$form = str_replace( '[PASSSTER_PROTECTION]', 'full', $form ); |
| 82 |
} else { |
| 83 |
if ( !empty( $atts['area'] ) ) { |
| 84 |
$form = str_replace( '[PASSSTER_PROTECTION]', 'area', $form ); |
| 85 |
} |
| 86 |
} |
| 87 |
// Redirect. |
| 88 |
if ( !empty( $atts['redirect'] ) ) { |
| 89 |
$form = str_replace( '[PASSSTER_REDIRECT]', esc_url( $atts['redirect'] ), $form ); |
| 90 |
} else { |
| 91 |
$form = str_replace( '[PASSSTER_REDIRECT]', '', $form ); |
| 92 |
} |
| 93 |
// headline. |
| 94 |
if ( !empty( $options['hide_headline'] ) ) { |
| 95 |
$form = str_replace( '[PASSSTER_FORM_HEADLINE]', '', $form ); |
| 96 |
} elseif ( !empty( $atts['headline'] ) ) { |
| 97 |
$form = str_replace( '[PASSSTER_FORM_HEADLINE]', esc_html( $atts['headline'] ), $form ); |
| 98 |
} else { |
| 99 |
$form = str_replace( '[PASSSTER_FORM_HEADLINE]', $options['headline'], $form ); |
| 100 |
} |
| 101 |
// instruction. |
| 102 |
if ( !empty( $atts['instruction'] ) ) { |
| 103 |
$form = str_replace( '[PASSSTER_FORM_INSTRUCTIONS]', esc_html( $atts['instruction'] ), $form ); |
| 104 |
} else { |
| 105 |
$form = str_replace( '[PASSSTER_FORM_INSTRUCTIONS]', $options['instruction'], $form ); |
| 106 |
} |
| 107 |
// placeholder. |
| 108 |
if ( !empty( $atts['placeholder'] ) ) { |
| 109 |
$form = str_replace( '[PASSSTER_PLACEHOLDER]', esc_html( $atts['placeholder'] ), $form ); |
| 110 |
} else { |
| 111 |
$form = str_replace( '[PASSSTER_PLACEHOLDER]', $options['placeholder'], $form ); |
| 112 |
} |
| 113 |
// button. |
| 114 |
if ( !empty( $atts['button'] ) ) { |
| 115 |
$form = str_replace( '[PASSSTER_BUTTON_LABEL]', esc_html( $atts['button'] ), $form ); |
| 116 |
} else { |
| 117 |
$form = str_replace( '[PASSSTER_BUTTON_LABEL]', $options['button_label'], $form ); |
| 118 |
} |
| 119 |
// modify id. |
| 120 |
if ( !empty( $atts['id'] ) ) { |
| 121 |
$form = str_replace( '[PASSSTER_ID]', 'ps-' . esc_html( $atts['id'] ), $form ); |
| 122 |
} else { |
| 123 |
$form = str_replace( '[PASSSTER_ID]', 'ps-' . wp_rand( 10, 1000 ), $form ); |
| 124 |
} |
| 125 |
// hide or not. |
| 126 |
if ( !empty( $atts['hide'] ) ) { |
| 127 |
$form = str_replace( '[PASSSTER_HIDE]', ' passster-hide', $form ); |
| 128 |
} else { |
| 129 |
$form = str_replace( '[PASSSTER_HIDE]', '', $form ); |
| 130 |
} |
| 131 |
// ACF field. |
| 132 |
if ( !empty( $atts['acf'] ) ) { |
| 133 |
$form = str_replace( '[PASSSTER_ACF]', ' data-acf="' . esc_html( $atts['acf'] ) . '"', $form ); |
| 134 |
} else { |
| 135 |
$form = str_replace( '[PASSSTER_ACF]', '', $form ); |
| 136 |
} |
| 137 |
return $form; |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Filters the_content with Passster. |
| 142 |
* |
| 143 |
* @param string $content given content. |
| 144 |
* |
| 145 |
* @return string |
| 146 |
* @throws Exception |
| 147 |
*/ |
| 148 |
public function filter_the_content( string $content ) : string { |
| 149 |
$post_id = get_the_id(); |
| 150 |
$activate_protection = get_post_meta( $post_id, 'passster_activate_protection', true ); |
| 151 |
// user restriction. |
| 152 |
$user_restriction_type = get_post_meta( $post_id, 'passster_user_restriction_type', true ); |
| 153 |
$user_restriction = get_post_meta( $post_id, 'passster_user_restriction', true ); |
| 154 |
// Redirection. |
| 155 |
$redirection = get_post_meta( $post_id, 'passster_redirect_url', true ); |
| 156 |
// texts. |
| 157 |
$headline = get_post_meta( $post_id, 'passster_headline', true ); |
| 158 |
$instruction = get_post_meta( $post_id, 'passster_instruction', true ); |
| 159 |
$placeholder = get_post_meta( $post_id, 'passster_placeholder', true ); |
| 160 |
$button = get_post_meta( $post_id, 'passster_button', true ); |
| 161 |
$id = get_post_meta( $post_id, 'passster_id', true ); |
| 162 |
if ( !$activate_protection ) { |
| 163 |
return $content; |
| 164 |
} |
| 165 |
// build atts array to validate. |
| 166 |
$atts = array(); |
| 167 |
$shortcode = ''; |
| 168 |
$password = get_post_meta( $post_id, 'passster_password', true ); |
| 169 |
$atts['password'] = $password; |
| 170 |
$shortcode = '[passster password="' . $password . '" protection="full" '; |
| 171 |
if ( !empty( $redirection ) ) { |
| 172 |
$shortcode .= 'redirect="' . $redirection . '" '; |
| 173 |
} |
| 174 |
if ( !empty( $headline ) ) { |
| 175 |
$shortcode .= 'headline="' . $headline . '" '; |
| 176 |
} |
| 177 |
if ( !empty( $instruction ) ) { |
| 178 |
$shortcode .= 'instruction="' . $instruction . '" '; |
| 179 |
} |
| 180 |
if ( !empty( $placeholder ) ) { |
| 181 |
$shortcode .= 'placeholder="' . $placeholder . '" '; |
| 182 |
} |
| 183 |
if ( !empty( $button ) ) { |
| 184 |
$shortcode .= 'button="' . $button . '" '; |
| 185 |
} |
| 186 |
if ( !empty( $id ) ) { |
| 187 |
$shortcode .= 'id="' . $id . '" '; |
| 188 |
} |
| 189 |
$shortcode .= ']{content}[/passster]'; |
| 190 |
// check if valid before restrict anything. |
| 191 |
$valid = PS_Conditional::is_valid( $atts ); |
| 192 |
if ( $valid ) { |
| 193 |
return $content; |
| 194 |
} |
| 195 |
// replace placeholder with content. |
| 196 |
$shortcode = str_replace( '{content}', $content, $shortcode ); |
| 197 |
return do_shortcode( $shortcode ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Redirect if global protection is activated and no password is set. |
| 202 |
* |
| 203 |
* @return void |
| 204 |
* @throws Exception |
| 205 |
*/ |
| 206 |
public function check_global_proctection() { |
| 207 |
$options = get_option( 'passster' ); |
| 208 |
// Allow Elementor editing the page. |
| 209 |
$elementor_preview = filter_input( INPUT_GET, 'elementor-preview', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 210 |
if ( $elementor_preview ) { |
| 211 |
return; |
| 212 |
} |
| 213 |
// Allow Live Canvas Editor. |
| 214 |
$live_canvas_preview = filter_input( INPUT_GET, 'lc_action_launch_editing', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 215 |
if ( $live_canvas_preview ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
if ( !isset( $options['global_protection_id'] ) ) { |
| 219 |
return; |
| 220 |
} |
| 221 |
if ( !isset( $options['activate_global_protection'] ) ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
// Build $atts array based on protection settings. |
| 225 |
$post_id = esc_html( $options['global_protection_id'] ); |
| 226 |
$is_active = esc_html( $options['activate_global_protection'] ); |
| 227 |
$atts = array(); |
| 228 |
$password = get_post_meta( $post_id, 'passster_password', true ); |
| 229 |
$atts['password'] = esc_html( $password ); |
| 230 |
if ( !empty( $post_id ) ) { |
| 231 |
if ( $is_active ) { |
| 232 |
if ( is_page( $post_id ) || is_single( $post_id ) ) { |
| 233 |
return; |
| 234 |
} |
| 235 |
// Check excluded pages. |
| 236 |
if ( isset( $options['exclude_pages'] ) ) { |
| 237 |
foreach ( $options['exclude_pages'] as $excluded_page_id ) { |
| 238 |
if ( is_page( $excluded_page_id ) ) { |
| 239 |
return; |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
// Check if cookie is set. |
| 244 |
$cookie = esc_html( $_COOKIE['passster'] ); |
| 245 |
if ( empty( $cookie ) || !PS_Conditional::is_valid( $atts ) ) { |
| 246 |
$global_protection_url = get_permalink( $post_id ); |
| 247 |
wp_redirect( esc_url_raw( $global_protection_url ) ); |
| 248 |
exit; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
} |
| 255 |
|