| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
/** |
| 6 |
* PS_Meta Class |
| 7 |
*/ |
| 8 |
class PS_Meta |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Contains instance or null |
| 12 |
* |
| 13 |
* @var object|null |
| 14 |
*/ |
| 15 |
private static $instance = null ; |
| 16 |
/** |
| 17 |
* Returns instance of PS_Meta. |
| 18 |
* |
| 19 |
* @return object |
| 20 |
*/ |
| 21 |
public static function get_instance() |
| 22 |
{ |
| 23 |
if ( null === self::$instance ) { |
| 24 |
self::$instance = new self(); |
| 25 |
} |
| 26 |
return self::$instance; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor for PS_Meta. |
| 31 |
*/ |
| 32 |
public function __construct() |
| 33 |
{ |
| 34 |
add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
| 35 |
add_action( 'save_post', array( $this, 'save_metaboxes' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Adds the meta box container. |
| 40 |
* |
| 41 |
* @param array $post_type array of post types. |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function add_metaboxes( $post_type ) |
| 45 |
{ |
| 46 |
// Get all available post types. |
| 47 |
$post_types = get_post_types( array( |
| 48 |
'public' => true, |
| 49 |
'exclude_from_search' => false, |
| 50 |
), 'names' ); |
| 51 |
unset( $post_types['protected_areas'] ); |
| 52 |
add_meta_box( |
| 53 |
'shortcode', |
| 54 |
__( 'Shortcode Configuration', 'content-protector' ), |
| 55 |
array( $this, 'render_shortcode_configuration' ), |
| 56 |
'protected_areas', |
| 57 |
'side', |
| 58 |
'high' |
| 59 |
); |
| 60 |
add_meta_box( |
| 61 |
'copy_shortcode', |
| 62 |
__( 'Shortcode', 'content-protector' ), |
| 63 |
array( $this, 'render_copy_shortcode' ), |
| 64 |
'protected_areas', |
| 65 |
'normal', |
| 66 |
'default' |
| 67 |
); |
| 68 |
add_meta_box( |
| 69 |
'passster_global', |
| 70 |
__( 'Passster (Global Protection)', 'content-protector' ), |
| 71 |
array( $this, 'render_passster_global' ), |
| 72 |
apply_filters( 'passster_post_types', $post_types ), |
| 73 |
'side', |
| 74 |
'default' |
| 75 |
); |
| 76 |
add_meta_box( |
| 77 |
'passster', |
| 78 |
__( 'Passster (Page Protection)', 'content-protector' ), |
| 79 |
array( $this, 'render_passster' ), |
| 80 |
apply_filters( 'passster_post_types', $post_types ), |
| 81 |
'side', |
| 82 |
'default' |
| 83 |
); |
| 84 |
add_meta_box( |
| 85 |
'passster_link', |
| 86 |
__( 'Passster (Link Protection)', 'content-protector' ), |
| 87 |
array( $this, 'render_passster_link' ), |
| 88 |
apply_filters( 'passster_post_types', $post_types ), |
| 89 |
'side', |
| 90 |
'default' |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Render copy shortcode metabox. |
| 96 |
* |
| 97 |
* @param WP_Post $post The post object. |
| 98 |
*/ |
| 99 |
public function render_copy_shortcode( $post ) |
| 100 |
{ |
| 101 |
$shortcode = ''; |
| 102 |
// user restriction. |
| 103 |
$user_restriction_type = get_post_meta( $post->ID, 'passster_user_restriction_type', true ); |
| 104 |
$user_restriction = get_post_meta( $post->ID, 'passster_user_restriction', true ); |
| 105 |
// texts. |
| 106 |
$headline = get_post_meta( $post->ID, 'passster_headline', true ); |
| 107 |
$instruction = get_post_meta( $post->ID, 'passster_instruction', true ); |
| 108 |
$placeholder = get_post_meta( $post->ID, 'passster_placeholder', true ); |
| 109 |
$button = get_post_meta( $post->ID, 'passster_button', true ); |
| 110 |
$id = get_post_meta( $post->ID, 'passster_id', true ); |
| 111 |
$hide = get_post_meta( $post->ID, 'passster_hide', true ); |
| 112 |
// build atts array to validate. |
| 113 |
$atts = array(); |
| 114 |
// Add protection based on type. |
| 115 |
$protection_type = get_post_meta( $post->ID, 'passster_protection_type', true ); |
| 116 |
switch ( $protection_type ) { |
| 117 |
case 'password': |
| 118 |
$password = get_post_meta( $post->ID, 'passster_password', true ); |
| 119 |
$shortcode = '[passster password="' . $password . '" '; |
| 120 |
break; |
| 121 |
case 'captcha': |
| 122 |
$shortcode = '[passster captcha="true" '; |
| 123 |
break; |
| 124 |
} |
| 125 |
// Add area. |
| 126 |
$shortcode .= 'area="' . $post->ID . '" '; |
| 127 |
// Add user restriction. |
| 128 |
if ( !empty($user_restriction_type) && !empty($user_restriction) ) { |
| 129 |
|
| 130 |
if ( 'user-role' === $user_restriction_type ) { |
| 131 |
$shortcode .= 'role="' . $user_restriction . '" '; |
| 132 |
} else { |
| 133 |
$shortcode .= 'user="' . $user_restriction . '" '; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
if ( !empty($headline) ) { |
| 138 |
$shortcode .= 'headline="' . $headline . '" '; |
| 139 |
} |
| 140 |
if ( !empty($instruction) ) { |
| 141 |
$shortcode .= 'instruction="' . $instruction . '" '; |
| 142 |
} |
| 143 |
if ( !empty($placeholder) ) { |
| 144 |
$shortcode .= 'placeholder="' . $placeholder . '" '; |
| 145 |
} |
| 146 |
if ( !empty($button) ) { |
| 147 |
$shortcode .= 'button="' . $button . '" '; |
| 148 |
} |
| 149 |
if ( !empty($hide) && 'yes' === $hide ) { |
| 150 |
$shortcode .= 'hide="true" '; |
| 151 |
} |
| 152 |
if ( !empty($id) ) { |
| 153 |
$shortcode .= 'id="' . $id . '" '; |
| 154 |
} |
| 155 |
$shortcode .= ']'; |
| 156 |
// Check for space before closing shortcode. |
| 157 |
$shortcode = str_replace( ' ]', ']', $shortcode ); |
| 158 |
update_post_meta( $post->ID, 'passster_area_shortcode', $shortcode ); |
| 159 |
?> |
| 160 |
<p> |
| 161 |
<?php |
| 162 |
echo esc_html( $shortcode ) ; |
| 163 |
?> |
| 164 |
</p> |
| 165 |
<?php |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Render passwords metabox. |
| 170 |
* |
| 171 |
* @param WP_Post $post The post object. |
| 172 |
*/ |
| 173 |
public function render_passwords( $post ) |
| 174 |
{ |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Render advanced options metabox. |
| 179 |
* |
| 180 |
* @param WP_Post $post The post object. |
| 181 |
*/ |
| 182 |
public function render_advanced_options( $post ) |
| 183 |
{ |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Render global password metabox. |
| 188 |
* |
| 189 |
* @param WP_Post $post The post object. |
| 190 |
*/ |
| 191 |
public function render_passster_global( $post ) |
| 192 |
{ |
| 193 |
wp_nonce_field( 'passster_nonce_check', 'passster_nonce_check_value' ); |
| 194 |
$activate_global_protection = get_post_meta( $post->ID, 'passster_activate_global_protection', true ); |
| 195 |
// check if protection active. |
| 196 |
|
| 197 |
if ( true == $activate_global_protection ) { |
| 198 |
$activate_global_protection = 'checked="checked"'; |
| 199 |
} else { |
| 200 |
$activate_global_protection = ''; |
| 201 |
} |
| 202 |
|
| 203 |
?> |
| 204 |
<div class="passster-meta passster-admin"> |
| 205 |
<p> |
| 206 |
<span><?php |
| 207 |
esc_html_e( 'Activate Global Protection', 'content-protector' ); |
| 208 |
?></span><br> |
| 209 |
<label class="switch" for="passster-activate-global-protection"> |
| 210 |
<input type="checkbox" class="toggle-checkbox" id="passster-activate-global-protection" name="passster-activate-global-protection" value="true" <?php |
| 211 |
echo $activate_global_protection ; |
| 212 |
?> /> |
| 213 |
<span class="slider round"></span> |
| 214 |
</label> |
| 215 |
</p> |
| 216 |
<p> |
| 217 |
<?php |
| 218 |
_e( 'Configure the password, texts and everything else in Passster (Page Protection).', 'content-protector' ); |
| 219 |
?> |
| 220 |
</p> |
| 221 |
<small> |
| 222 |
<?php |
| 223 |
_e( 'If activated every page will be redirected to that page as long as the user does not have unlocked the content. The global protection requires the cookie option activated (set by default).', 'content-protector' ); |
| 224 |
?> |
| 225 |
</small> |
| 226 |
</div> |
| 227 |
<?php |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Render page protection metabox. |
| 232 |
* |
| 233 |
* @param WP_Post $post The post object. |
| 234 |
*/ |
| 235 |
public function render_passster( $post ) |
| 236 |
{ |
| 237 |
wp_nonce_field( 'passster_nonce_check', 'passster_nonce_check_value' ); |
| 238 |
$activate_protection = get_post_meta( $post->ID, 'passster_activate_protection', true ); |
| 239 |
$protection_type = get_post_meta( $post->ID, 'passster_protection_type', true ); |
| 240 |
$password = get_post_meta( $post->ID, 'passster_password', true ); |
| 241 |
// check if protection active. |
| 242 |
|
| 243 |
if ( true == $activate_protection ) { |
| 244 |
$activate_protection = 'checked="checked"'; |
| 245 |
} else { |
| 246 |
$activate_protection = ''; |
| 247 |
} |
| 248 |
|
| 249 |
$protection_types = array( |
| 250 |
'password' => __( 'Password', 'content-protector' ), |
| 251 |
'captcha' => __( 'Captcha', 'content-protector' ), |
| 252 |
); |
| 253 |
?> |
| 254 |
<div class="passster-meta passster-admin"> |
| 255 |
<p> |
| 256 |
<span><?php |
| 257 |
esc_html_e( 'Activate Protection', 'content-protector' ); |
| 258 |
?></span><br> |
| 259 |
<label class="switch" for="passster-activate-protection"> |
| 260 |
<input type="checkbox" class="toggle-checkbox" id="passster-activate-protection" name="passster-activate-protection" value="true" <?php |
| 261 |
echo $activate_protection ; |
| 262 |
?> /> |
| 263 |
<span class="slider round"></span> |
| 264 |
</label> |
| 265 |
</p> |
| 266 |
<p> |
| 267 |
<label><?php |
| 268 |
esc_html_e( 'Protection Type', 'content-protector' ); |
| 269 |
?></label><br> |
| 270 |
<select id="passster-protection-type" name="passster-protection-type"> |
| 271 |
<?php |
| 272 |
foreach ( $protection_types as $key => $value ) { |
| 273 |
?> |
| 274 |
<?php |
| 275 |
|
| 276 |
if ( $key === $protection_type ) { |
| 277 |
?> |
| 278 |
<option selected="selected" value="<?php |
| 279 |
echo esc_attr( $key ) ; |
| 280 |
?>"><?php |
| 281 |
echo esc_html( $value ) ; |
| 282 |
?></option> |
| 283 |
<?php |
| 284 |
} else { |
| 285 |
?> |
| 286 |
<option value="<?php |
| 287 |
echo esc_attr( $key ) ; |
| 288 |
?>"><?php |
| 289 |
echo esc_html( $value ) ; |
| 290 |
?></option> |
| 291 |
<?php |
| 292 |
} |
| 293 |
|
| 294 |
?> |
| 295 |
<?php |
| 296 |
} |
| 297 |
?> |
| 298 |
</select> |
| 299 |
</p> |
| 300 |
<p class="passster-hide-option"> |
| 301 |
<label><?php |
| 302 |
esc_html_e( 'Password', 'content-protector' ); |
| 303 |
?></label><br> |
| 304 |
<input type="text" name="passster-password" id="passster-password" value="<?php |
| 305 |
echo esc_attr( $password ) ; |
| 306 |
?>" /> |
| 307 |
</p> |
| 308 |
<?php |
| 309 |
?> |
| 310 |
<?php |
| 311 |
?> |
| 312 |
<?php |
| 313 |
?> |
| 314 |
<?php |
| 315 |
?> |
| 316 |
<p> |
| 317 |
<?php |
| 318 |
_e( 'With <b style="color:#7200e5">Passster Pro</b> you can also use multiple passwords, Password Lists, Google ReCaptcha v2/v3 and link protection.', 'content-protector' ); |
| 319 |
?> |
| 320 |
</p> |
| 321 |
<p> |
| 322 |
<?php |
| 323 |
esc_html_e( 'You can also modify the headline, description, placeholder and the button label per page', 'content-protector' ); |
| 324 |
?> |
| 325 |
</p> |
| 326 |
<?php |
| 327 |
?> |
| 328 |
</div> |
| 329 |
<?php |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Render shortcode metabox. |
| 334 |
* |
| 335 |
* @param WP_Post $post The post object. |
| 336 |
*/ |
| 337 |
public function render_shortcode_configuration( $post ) |
| 338 |
{ |
| 339 |
wp_nonce_field( 'passster_nonce_check', 'passster_nonce_check_value' ); |
| 340 |
$protection_type = get_post_meta( $post->ID, 'passster_protection_type', true ); |
| 341 |
$password = get_post_meta( $post->ID, 'passster_password', true ); |
| 342 |
$protection_types = array( |
| 343 |
'password' => __( 'Password', 'content-protector' ), |
| 344 |
'captcha' => __( 'Captcha', 'content-protector' ), |
| 345 |
); |
| 346 |
?> |
| 347 |
<div class="passster-meta passster-admin"> |
| 348 |
<p> |
| 349 |
<label><?php |
| 350 |
esc_html_e( 'Protection Type', 'content-protector' ); |
| 351 |
?></label><br> |
| 352 |
<select id="passster-protection-type" name="passster-protection-type"> |
| 353 |
<?php |
| 354 |
foreach ( $protection_types as $key => $value ) { |
| 355 |
?> |
| 356 |
<?php |
| 357 |
|
| 358 |
if ( $key === $protection_type ) { |
| 359 |
?> |
| 360 |
<option selected="selected" value="<?php |
| 361 |
echo esc_attr( $key ) ; |
| 362 |
?>"><?php |
| 363 |
echo esc_html( $value ) ; |
| 364 |
?></option> |
| 365 |
<?php |
| 366 |
} else { |
| 367 |
?> |
| 368 |
<option value="<?php |
| 369 |
echo esc_attr( $key ) ; |
| 370 |
?>"><?php |
| 371 |
echo esc_html( $value ) ; |
| 372 |
?></option> |
| 373 |
<?php |
| 374 |
} |
| 375 |
|
| 376 |
?> |
| 377 |
<?php |
| 378 |
} |
| 379 |
?> |
| 380 |
</select> |
| 381 |
</p> |
| 382 |
<p class="passster-hide-option"> |
| 383 |
<label><?php |
| 384 |
esc_html_e( 'Password', 'content-protector' ); |
| 385 |
?></label><br> |
| 386 |
<input type="text" name="passster-password" id="passster-password" value="<?php |
| 387 |
echo esc_attr( $password ) ; |
| 388 |
?>" /> |
| 389 |
</p> |
| 390 |
<?php |
| 391 |
?> |
| 392 |
<?php |
| 393 |
?> |
| 394 |
<?php |
| 395 |
?> |
| 396 |
<?php |
| 397 |
?> |
| 398 |
<p> |
| 399 |
<?php |
| 400 |
_e( 'With <b style="color:#7200e5">Passster Pro</b> you can also use multiple passwords, Password Lists, Google ReCaptcha v2/v3 and link protection.', 'content-protector' ); |
| 401 |
?> |
| 402 |
</p> |
| 403 |
<p> |
| 404 |
<?php |
| 405 |
esc_html_e( 'You can also modify the headline, description, placeholder and the button label per shortcode', 'content-protector' ); |
| 406 |
?> |
| 407 |
</p> |
| 408 |
<?php |
| 409 |
?> |
| 410 |
</div> |
| 411 |
<?php |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Render link metabox. |
| 416 |
* |
| 417 |
* @param WP_Post $post The post object. |
| 418 |
*/ |
| 419 |
public function render_passster_link( $post ) |
| 420 |
{ |
| 421 |
wp_nonce_field( 'passster_nonce_check', 'passster_nonce_check_value' ); |
| 422 |
$link = get_post_meta( $post->ID, 'passster_link', true ); |
| 423 |
$bitly_on = ''; |
| 424 |
?> |
| 425 |
<div class="passster-meta passster-admin"> |
| 426 |
<?php |
| 427 |
?> |
| 428 |
<?php |
| 429 |
?> |
| 430 |
<p> |
| 431 |
<?php |
| 432 |
_e( 'With <b style="color:#7200e5">Passster Pro</b> you can also use multiple passwords, Password Lists, Google ReCaptcha v2/v3 and link protection.', 'content-protector' ); |
| 433 |
?> |
| 434 |
</p> |
| 435 |
<?php |
| 436 |
?> |
| 437 |
</div> |
| 438 |
<?php |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Save the meta when the post is saved. |
| 443 |
* |
| 444 |
* @param int $post_id The ID of the post being saved. |
| 445 |
*/ |
| 446 |
public function save_metaboxes( $post_id ) |
| 447 |
{ |
| 448 |
// Check if our nonce is set. |
| 449 |
if ( !isset( $_POST['passster_nonce_check_value'] ) ) { |
| 450 |
return $post_id; |
| 451 |
} |
| 452 |
// Verify that the nonce is valid. |
| 453 |
if ( !wp_verify_nonce( $_POST['passster_nonce_check_value'], 'passster_nonce_check' ) ) { |
| 454 |
return $post_id; |
| 455 |
} |
| 456 |
// If this is an autosave, our form has not been submitted, so we don't want to do anything. |
| 457 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 458 |
return $post_id; |
| 459 |
} |
| 460 |
// Check the user's permissions. |
| 461 |
if ( !current_user_can( 'edit_post', $post_id ) ) { |
| 462 |
return $post_id; |
| 463 |
} |
| 464 |
// global protection meta. |
| 465 |
|
| 466 |
if ( isset( $_POST['passster-activate-global-protection'] ) && !empty($_POST['passster-activate-global-protection']) ) { |
| 467 |
update_post_meta( $post_id, 'passster_activate_global_protection', $_POST['passster-activate-global-protection'] ); |
| 468 |
// Activate page protection also. |
| 469 |
update_post_meta( $post_id, 'passster_activate_protection', true ); |
| 470 |
// Activate cookie setting. |
| 471 |
$general_settings = wp_parse_args( get_option( 'passster_general_settings' ), PS_Admin::get_defaults( 'passster_general_settings' ) ); |
| 472 |
$general_settings['toggle_cookie'] = 'on'; |
| 473 |
update_option( 'passster_general_settings', $general_settings ); |
| 474 |
update_option( 'passster_global_id', $post_id ); |
| 475 |
} else { |
| 476 |
update_post_meta( $post_id, 'passster_activate_global_protection', null ); |
| 477 |
//delete_option( 'passster_global_id' ); |
| 478 |
} |
| 479 |
|
| 480 |
// page protection meta. |
| 481 |
|
| 482 |
if ( isset( $_POST['passster-activate-protection'] ) && !empty($_POST['passster-activate-protection']) ) { |
| 483 |
update_post_meta( $post_id, 'passster_activate_protection', $_POST['passster-activate-protection'] ); |
| 484 |
} else { |
| 485 |
update_post_meta( $post_id, 'passster_activate_protection', null ); |
| 486 |
} |
| 487 |
|
| 488 |
if ( isset( $_POST['passster-protection-type'] ) && !empty($_POST['passster-protection-type']) ) { |
| 489 |
update_post_meta( $post_id, 'passster_protection_type', sanitize_text_field( $_POST['passster-protection-type'] ) ); |
| 490 |
} |
| 491 |
if ( isset( $_POST['passster-password'] ) && !empty($_POST['passster-password']) ) { |
| 492 |
update_post_meta( $post_id, 'passster_password', sanitize_text_field( $_POST['passster-password'] ) ); |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
} |