Created
July 13, 2021 17:26
-
-
Save patrickposner/ca96f892d9c75f2ba8be52f4ffa9abae to your computer and use it in GitHub Desktop.
Use Passster for WooCommerce product protection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_before_single_product_summary', function() { | |
$post_id = get_the_id(); | |
$activate_protection = get_post_meta( $post_id, 'passster_activate_protection', true ); | |
// user restriction. | |
$user_restriction_type = get_post_meta( $post_id, 'passster_user_restriction_type', true ); | |
$user_restriction = get_post_meta( $post_id, 'passster_user_restriction', true ); | |
// texts. | |
$headline = get_post_meta( $post_id, 'passster_headline', true ); | |
$instruction = get_post_meta( $post_id, 'passster_instruction', true ); | |
$placeholder = get_post_meta( $post_id, 'passster_placeholder', true ); | |
$button = get_post_meta( $post_id, 'passster_button', true ); | |
$id = get_post_meta( $post_id, 'passster_id', true ); | |
if ( ! $activate_protection ) { | |
return; | |
} | |
// build atts array to validate. | |
$atts = array(); | |
$protection_type = get_post_meta( $post_id, 'passster_protection_type', true ); | |
if ( \ps_fs()->is_plan_or_trial__premium_only( 'pro' ) ) { | |
switch ( $protection_type ) { | |
case 'password': | |
$password = get_post_meta( $post_id, 'passster_password', true ); | |
$atts['password'] = $password; | |
$shortcode = '[passster password="' . $password . '" protection="full" '; | |
break; | |
case 'passwords': | |
$passwords = get_post_meta( $post_id, 'passster_passwords', true ); | |
$atts['passwords'] = $passwords; | |
$shortcode = '[passster passwords="' . $passwords . '" protection="full" '; | |
break; | |
case 'password_list': | |
$password_list = get_post_meta( $post_id, 'passster_password_list', true ); | |
$atts['password_list'] = $password_list; | |
$shortcode = '[passster password_list="' . $password_list . '" protection="full" '; | |
break; | |
case 'captcha': | |
$atts['captcha'] = true; | |
$shortcode = '[passster captcha="true" protection="full" '; | |
break; | |
case 'recaptcha': | |
$atts['recaptcha'] = true; | |
$shortcode = '[passster recaptcha="true" protection="full" '; | |
break; | |
case 'api': | |
$api = get_post_meta( $post_id, 'passster_api', true ); | |
$atts['api'] = $api; | |
$shortcode = '[passster api="' . $api . '" protection="full" '; | |
break; | |
} | |
if ( ! empty( $user_restriction_type ) && ! empty( $user_restriction ) ) { | |
if ( 'user-role' === $user_restriction_type ) { | |
$atts['role'] = $user_restriction; | |
} else { | |
$atts['user'] = $user_restriction; | |
} | |
} | |
if ( ! empty( $headline ) ) { | |
$shortcode .= 'headline="' . $headline . '" '; | |
} | |
if ( ! empty( $instruction ) ) { | |
$shortcode .= 'instruction="' . $instruction . '" '; | |
} | |
if ( ! empty( $placeholder ) ) { | |
$shortcode .= 'placeholder="' . $placeholder . '" '; | |
} | |
if ( ! empty( $button ) ) { | |
$shortcode .= 'button="' . $button . '" '; | |
} | |
if ( ! empty( $id ) ) { | |
$shortcode .= 'id="' . $id . '" '; | |
} | |
} else { | |
switch ( $protection_type ) { | |
case 'password': | |
$password = get_post_meta( $post_id, 'passster_password', true ); | |
$atts['password'] = $password; | |
$shortcode = '[passster password="' . $password . '" protection="full" '; | |
break; | |
case 'captcha': | |
$atts['captcha'] = true; | |
$shortcode = '[passster captcha="true" protection="full" '; | |
break; | |
} | |
} | |
$shortcode .= ']{content}[/passster]'; | |
// check if valid before restrict anything. | |
$valid = passster\PS_Conditional::is_valid( $atts ); | |
if ( ! $valid ) { | |
// replace placeholder with content. | |
$shortcode = str_replace( '{content}', $content, $shortcode ); | |
echo do_shortcode( $shortcode ); | |
exit; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment