| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
use Exception ; |
| 6 |
class PS_Public |
| 7 |
{ |
| 8 |
/** |
| 9 |
* Contains instance or null |
| 10 |
* |
| 11 |
* @var object|null |
| 12 |
*/ |
| 13 |
private static $instance = null ; |
| 14 |
/** |
| 15 |
* Constructor for PS_Public |
| 16 |
*/ |
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
add_shortcode( 'content_protector', array( $this, 'render_shortcode' ) ); |
| 20 |
add_shortcode( 'passster', array( $this, 'render_shortcode' ) ); |
| 21 |
add_filter( 'the_content', array( $this, 'filter_the_content' ) ); |
| 22 |
add_filter( 'acf_the_content', array( $this, 'filter_the_content' ) ); |
| 23 |
add_filter( 'get_the_excerpt', array( $this, 'filter_the_content' ) ); |
| 24 |
add_filter( |
| 25 |
'passster_compatibility_actions', |
| 26 |
array( $this, 'add_compatibilities' ), |
| 27 |
10, |
| 28 |
2 |
| 29 |
); |
| 30 |
add_action( 'template_redirect', array( $this, 'check_global_proctection' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Returns instance of PS_Public. |
| 35 |
* |
| 36 |
* @return object |
| 37 |
*/ |
| 38 |
public static function get_instance() |
| 39 |
{ |
| 40 |
if ( null === self::$instance ) { |
| 41 |
self::$instance = new self(); |
| 42 |
} |
| 43 |
return self::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Render the Passster shortcode. |
| 48 |
* |
| 49 |
* @param array $atts array of attributes. |
| 50 |
* @param string|null $content the current content. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public function render_shortcode( array $atts, string $content = null ) : string |
| 55 |
{ |
| 56 |
// check if valid before restrict anything. |
| 57 |
$valid = PS_Conditional::is_valid( $atts ); |
| 58 |
$options = get_option( 'passster' ); |
| 59 |
if ( $valid ) { |
| 60 |
|
| 61 |
if ( !empty($atts['area']) ) { |
| 62 |
$area_id = esc_html( $atts['area'] ); |
| 63 |
$area = get_post( $area_id ); |
| 64 |
$content = apply_filters( 'passster_compatibility_actions', $area->post_content, $area_id ); |
| 65 |
do_action( 'passster_content_unlocked' ); |
| 66 |
return apply_filters( 'the_content', str_replace( '{post-id}', get_the_id(), $content ) ); |
| 67 |
} else { |
| 68 |
$content = apply_filters( 'the_content', $content ); |
| 69 |
do_action( 'passster_content_unlocked' ); |
| 70 |
return apply_filters( 'passster_content', $content ); |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
// do nothing if no atts. |
| 75 |
if ( empty($atts) ) { |
| 76 |
return $content; |
| 77 |
} |
| 78 |
// Set default form. |
| 79 |
$form = PS_Form::get_password_form(); |
| 80 |
// Password. |
| 81 |
|
| 82 |
if ( !empty($atts['password']) ) { |
| 83 |
$form = PS_Form::get_password_form(); |
| 84 |
$form = str_replace( '[PASSSTER_TYPE]', 'password', $form ); |
| 85 |
} |
| 86 |
|
| 87 |
// Area. |
| 88 |
|
| 89 |
if ( !empty($atts['area']) ) { |
| 90 |
$area_id = esc_html( $atts['area'] ); |
| 91 |
$form = str_replace( '[PASSSTER_AREA]', $area_id, $form ); |
| 92 |
} |
| 93 |
|
| 94 |
// Page. |
| 95 |
|
| 96 |
if ( !empty($atts['protection']) ) { |
| 97 |
$form = str_replace( '[PASSSTER_PROTECTION]', 'full', $form ); |
| 98 |
} else { |
| 99 |
if ( !empty($atts['area']) ) { |
| 100 |
$form = str_replace( '[PASSSTER_PROTECTION]', 'area', $form ); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Redirect. |
| 105 |
|
| 106 |
if ( !empty($atts['redirect']) ) { |
| 107 |
$form = str_replace( '[PASSSTER_REDIRECT]', $atts['redirect'], $form ); |
| 108 |
} else { |
| 109 |
$form = str_replace( '[PASSSTER_REDIRECT]', '', $form ); |
| 110 |
} |
| 111 |
|
| 112 |
// headline. |
| 113 |
|
| 114 |
if ( !empty($atts['headline']) ) { |
| 115 |
$form = str_replace( '[PASSSTER_FORM_HEADLINE]', esc_html( $atts['headline'] ), $form ); |
| 116 |
} else { |
| 117 |
$form = str_replace( '[PASSSTER_FORM_HEADLINE]', $options['headline'], $form ); |
| 118 |
} |
| 119 |
|
| 120 |
// instruction. |
| 121 |
|
| 122 |
if ( !empty($atts['instruction']) ) { |
| 123 |
$form = str_replace( '[PASSSTER_FORM_INSTRUCTIONS]', esc_html( $atts['instruction'] ), $form ); |
| 124 |
} else { |
| 125 |
$form = str_replace( '[PASSSTER_FORM_INSTRUCTIONS]', $options['instruction'], $form ); |
| 126 |
} |
| 127 |
|
| 128 |
// placeholder. |
| 129 |
|
| 130 |
if ( !empty($atts['placeholder']) ) { |
| 131 |
$form = str_replace( '[PASSSTER_PLACEHOLDER]', esc_html( $atts['placeholder'] ), $form ); |
| 132 |
} else { |
| 133 |
$form = str_replace( '[PASSSTER_PLACEHOLDER]', $options['placeholder'], $form ); |
| 134 |
} |
| 135 |
|
| 136 |
// button. |
| 137 |
|
| 138 |
if ( !empty($atts['button']) ) { |
| 139 |
$form = str_replace( '[PASSSTER_BUTTON_LABEL]', esc_html( $atts['button'] ), $form ); |
| 140 |
} else { |
| 141 |
$form = str_replace( '[PASSSTER_BUTTON_LABEL]', $options['button_label'], $form ); |
| 142 |
} |
| 143 |
|
| 144 |
// modify id. |
| 145 |
|
| 146 |
if ( !empty($atts['id']) ) { |
| 147 |
$form = str_replace( '[PASSSTER_ID]', 'ps-' . esc_html( $atts['id'] ), $form ); |
| 148 |
} else { |
| 149 |
$form = str_replace( '[PASSSTER_ID]', 'ps-' . wp_rand( 10, 1000 ), $form ); |
| 150 |
} |
| 151 |
|
| 152 |
// hide or not. |
| 153 |
|
| 154 |
if ( !empty($atts['hide']) ) { |
| 155 |
$form = str_replace( '[PASSSTER_HIDE]', ' passster-hide', $form ); |
| 156 |
} else { |
| 157 |
$form = str_replace( '[PASSSTER_HIDE]', '', $form ); |
| 158 |
} |
| 159 |
|
| 160 |
// ACF field. |
| 161 |
|
| 162 |
if ( !empty($atts['acf']) ) { |
| 163 |
$form = str_replace( '[PASSSTER_ACF]', ' data-acf="' . esc_html( $atts['acf'] ) . '"', $form ); |
| 164 |
} else { |
| 165 |
$form = str_replace( '[PASSSTER_ACF]', '', $form ); |
| 166 |
} |
| 167 |
|
| 168 |
return $form; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Filters the_content with Passster. |
| 173 |
* |
| 174 |
* @param string $content given content. |
| 175 |
* |
| 176 |
* @return string |
| 177 |
* @throws Exception |
| 178 |
*/ |
| 179 |
public function filter_the_content( string $content ) : string |
| 180 |
{ |
| 181 |
$post_id = get_the_id(); |
| 182 |
$activate_protection = get_post_meta( $post_id, 'passster_activate_protection', true ); |
| 183 |
// user restriction. |
| 184 |
$user_restriction_type = get_post_meta( $post_id, 'passster_user_restriction_type', true ); |
| 185 |
$user_restriction = get_post_meta( $post_id, 'passster_user_restriction', true ); |
| 186 |
// Redirection. |
| 187 |
$redirection = get_post_meta( $post_id, 'passster_redirect_url', true ); |
| 188 |
// texts. |
| 189 |
$headline = get_post_meta( $post_id, 'passster_headline', true ); |
| 190 |
$instruction = get_post_meta( $post_id, 'passster_instruction', true ); |
| 191 |
$placeholder = get_post_meta( $post_id, 'passster_placeholder', true ); |
| 192 |
$button = get_post_meta( $post_id, 'passster_button', true ); |
| 193 |
$id = get_post_meta( $post_id, 'passster_id', true ); |
| 194 |
if ( !$activate_protection ) { |
| 195 |
return $content; |
| 196 |
} |
| 197 |
// build atts array to validate. |
| 198 |
$atts = array(); |
| 199 |
$shortcode = ''; |
| 200 |
$password = get_post_meta( $post_id, 'passster_password', true ); |
| 201 |
$atts['password'] = $password; |
| 202 |
$shortcode = '[passster password="' . $password . '" protection="full" '; |
| 203 |
if ( !empty($redirection) ) { |
| 204 |
$shortcode .= 'redirect="' . $redirection . '" '; |
| 205 |
} |
| 206 |
if ( !empty($headline) ) { |
| 207 |
$shortcode .= 'headline="' . $headline . '" '; |
| 208 |
} |
| 209 |
if ( !empty($instruction) ) { |
| 210 |
$shortcode .= 'instruction="' . $instruction . '" '; |
| 211 |
} |
| 212 |
if ( !empty($placeholder) ) { |
| 213 |
$shortcode .= 'placeholder="' . $placeholder . '" '; |
| 214 |
} |
| 215 |
if ( !empty($button) ) { |
| 216 |
$shortcode .= 'button="' . $button . '" '; |
| 217 |
} |
| 218 |
if ( !empty($id) ) { |
| 219 |
$shortcode .= 'id="' . $id . '" '; |
| 220 |
} |
| 221 |
$shortcode .= ']{content}[/passster]'; |
| 222 |
// check if valid before restrict anything. |
| 223 |
$valid = PS_Conditional::is_valid( $atts ); |
| 224 |
if ( $valid ) { |
| 225 |
return $content; |
| 226 |
} |
| 227 |
// replace placeholder with content. |
| 228 |
$shortcode = str_replace( '{content}', $content, $shortcode ); |
| 229 |
return do_shortcode( $shortcode ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Adding compatibility modifications before ajax output. |
| 234 |
* |
| 235 |
* @param string $content current content. |
| 236 |
* |
| 237 |
* @return string |
| 238 |
*/ |
| 239 |
public function add_compatibilities( string $content, $post_id = null ) : string |
| 240 |
{ |
| 241 |
if ( is_null( $post_id ) ) { |
| 242 |
$post_id = get_the_id(); |
| 243 |
} |
| 244 |
// use meta field instead. |
| 245 |
$use_meta = apply_filters( 'passster_use_meta_field', false ); |
| 246 |
|
| 247 |
if ( $use_meta ) { |
| 248 |
$meta_key = apply_filters( 'passster_meta_key', 'field_name' ); |
| 249 |
$content = get_post_meta( $post_id, $meta_key, true ); |
| 250 |
} |
| 251 |
|
| 252 |
// use meta field instead. |
| 253 |
$use_acf = apply_filters( 'passster_use_acf_field', false ); |
| 254 |
|
| 255 |
if ( $use_acf ) { |
| 256 |
$meta_key = apply_filters( 'passster_acf_key', 'field_name' ); |
| 257 |
$content = get_field( $meta_key, $post_id ); |
| 258 |
} |
| 259 |
|
| 260 |
// Tablepress. |
| 261 |
|
| 262 |
if ( class_exists( 'TablePress' ) ) { |
| 263 |
\TablePress::$controller = \TablePress::load_controller( 'frontend' ); |
| 264 |
\TablePress::$controller->init_shortcodes(); |
| 265 |
} |
| 266 |
|
| 267 |
// prepare for Visual Composer. |
| 268 |
if ( class_exists( 'WPBMap' ) ) { |
| 269 |
\WPBMap::addAllMappedShortcodes(); |
| 270 |
} |
| 271 |
// Skip shortcodes. |
| 272 |
remove_filter( 'the_content', 'do_shortcode', 11 ); |
| 273 |
$content = apply_filters( 'the_content', $content ); |
| 274 |
add_filter( 'the_content', 'do_shortcode', 11 ); |
| 275 |
return $content; |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Redirect if global protection is activated and no password is set. |
| 280 |
* |
| 281 |
* @return void |
| 282 |
* @throws Exception |
| 283 |
*/ |
| 284 |
public function check_global_proctection() |
| 285 |
{ |
| 286 |
$options = get_option( 'passster' ); |
| 287 |
// Allow Elementor editing the page. |
| 288 |
if ( isset( $_GET['elementor-preview'] ) ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
// Allow Live Canvas Editor. |
| 292 |
if ( isset( $_GET['lc_action_launch_editing'] ) ) { |
| 293 |
return; |
| 294 |
} |
| 295 |
if ( !isset( $options['global_protection_id'] ) ) { |
| 296 |
return; |
| 297 |
} |
| 298 |
if ( !isset( $options['activate_global_protection'] ) ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
// Build $atts array based on protection settings. |
| 302 |
$post_id = $options['global_protection_id']; |
| 303 |
$is_active = $options['activate_global_protection']; |
| 304 |
$atts = array(); |
| 305 |
$password = get_post_meta( $post_id, 'passster_password', true ); |
| 306 |
$atts['password'] = esc_html( $password ); |
| 307 |
if ( !empty($post_id) ) { |
| 308 |
|
| 309 |
if ( $is_active ) { |
| 310 |
if ( is_page( $post_id ) || is_single( $post_id ) ) { |
| 311 |
return; |
| 312 |
} |
| 313 |
// Check excluded pages. |
| 314 |
if ( isset( $options['exclude_pages'] ) ) { |
| 315 |
foreach ( $options['exclude_pages'] as $excluded_page_id ) { |
| 316 |
if ( is_page( $excluded_page_id ) ) { |
| 317 |
return; |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
// Check if cookie is set. |
| 322 |
|
| 323 |
if ( empty($_COOKIE['passster']) || !PS_Conditional::is_valid( $atts ) ) { |
| 324 |
$global_protection_url = get_permalink( $post_id ); |
| 325 |
wp_redirect( esc_url_raw( $global_protection_url ) ); |
| 326 |
exit; |
| 327 |
} |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
} |