| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
function wppb_content_restriction_initialize_meta_box() { |
| 5 |
|
| 6 |
add_action( 'add_meta_boxes', 'wppb_content_restriction_add_meta_box' ); |
| 7 |
|
| 8 |
} |
| 9 |
add_action( 'init', 'wppb_content_restriction_initialize_meta_box', 999 ); |
| 10 |
|
| 11 |
function wppb_content_restriction_add_meta_box() { |
| 12 |
|
| 13 |
$post_types = get_post_types( array( 'public' => true ) ); |
| 14 |
|
| 15 |
if( ! empty( $post_types ) ) { |
| 16 |
foreach( $post_types as $post_type ) { |
| 17 |
add_meta_box( |
| 18 |
'wppb_post_content_restriction', |
| 19 |
__( 'Profile Builder Content Restriction', 'profile-builder' ), |
| 20 |
'wppb_content_restriction_meta_box_output', |
| 21 |
$post_type, |
| 22 |
'normal' |
| 23 |
); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
function wppb_content_restriction_meta_box_output( $post ) { |
| 30 |
|
| 31 |
?> |
| 32 |
<div class="wppb-meta-box-fields-wrapper cozmoslabs-form-subsection-wrapper"> |
| 33 |
<h4 class="cozmoslabs-subsection-title"><?php echo esc_html__( 'Display Options', 'profile-builder' ); ?></h4> |
| 34 |
|
| 35 |
<div class="wppb-meta-box-field-wrapper cozmoslabs-form-field-wrapper"> |
| 36 |
<?php |
| 37 |
$wppb_content_restrict_types = apply_filters( 'wppb_single_post_content_restrict_types', array( 'message' => __( 'Message', 'profile-builder' ), 'redirect' => __( 'Redirect', 'profile-builder' ) ) ); |
| 38 |
|
| 39 |
$wppb_content_restrict_type = get_post_meta( $post->ID, 'wppb-content-restrict-type', true ); |
| 40 |
?> |
| 41 |
|
| 42 |
<label class="wppb-meta-box-field-label cozmoslabs-form-field-label"><?php esc_html_e( 'Type of Restriction', 'profile-builder' ); ?></label> |
| 43 |
|
| 44 |
<div class="cozmoslabs-radio-inputs-row"> |
| 45 |
<label class="wppb-meta-box-checkbox-label" for="wppb-content-restrict-type-default"> |
| 46 |
<input type="radio" id="wppb-content-restrict-type-default" value="default" <?php if( empty( $wppb_content_restrict_type ) || $wppb_content_restrict_type == 'default' ) echo 'checked="checked"'; ?> name="wppb-content-restrict-type"> |
| 47 |
<?php echo esc_html__( 'Settings Default', 'profile-builder' ); ?> |
| 48 |
</label> |
| 49 |
|
| 50 |
<?php foreach( $wppb_content_restrict_types as $type_slug => $type_label ) { ?> |
| 51 |
<label class="wppb-meta-box-checkbox-label" for="wppb-content-restrict-type-<?php echo esc_attr( $type_slug ); ?>"> |
| 52 |
<input type="radio" id="wppb-content-restrict-type-<?php echo esc_attr( $type_slug ); ?>" value="<?php echo esc_attr( $type_slug ); ?>" <?php if( $wppb_content_restrict_type == $type_slug ) echo 'checked="checked"'; ?> name="wppb-content-restrict-type"> |
| 53 |
<?php echo esc_html( $type_label ); ?> |
| 54 |
</label> |
| 55 |
<?php } ?> |
| 56 |
</div> |
| 57 |
|
| 58 |
</div> |
| 59 |
|
| 60 |
<div class="wppb-meta-box-field-wrapper cozmoslabs-form-field-wrapper cozmoslabs-checkbox-list-wrapper"> |
| 61 |
<label class="wppb-meta-box-field-label cozmoslabs-form-field-label"><?php esc_html_e( 'Display For', 'profile-builder' ); ?></label> |
| 62 |
|
| 63 |
<?php |
| 64 |
$wppb_roles = get_editable_roles(); |
| 65 |
$wppb_user_status = get_post_meta( $post->ID, 'wppb-content-restrict-user-status', true ); |
| 66 |
$wppb_selected_roles = get_post_meta( $post->ID, 'wppb-content-restrict-user-role' ); |
| 67 |
?> |
| 68 |
|
| 69 |
<div class="cozmoslabs-checkbox-list cozmoslabs-checkbox-4-col-list"> |
| 70 |
|
| 71 |
<div class="cozmoslabs-chckbox-container"> |
| 72 |
<input type="checkbox" value="loggedin" <?php if( ! empty( $wppb_user_status ) && $wppb_user_status == 'loggedin' ) echo 'checked="checked"'; ?> name="wppb-content-restrict-user-status" id="wppb-content-restrict-user-status"> |
| 73 |
<label class="wppb-meta-box-checkbox-label" for="wppb-content-restrict-user-status"> |
| 74 |
<?php echo esc_html__( 'Logged In Users', 'profile-builder' ); ?> |
| 75 |
</label> |
| 76 |
</div> |
| 77 |
|
| 78 |
<?php |
| 79 |
if( ! empty( $wppb_roles ) ) { |
| 80 |
foreach( $wppb_roles as $wppb_role => $wppb_role_details ) { |
| 81 |
?> |
| 82 |
<div class="cozmoslabs-chckbox-container"> |
| 83 |
<input type="checkbox" value="<?php echo esc_attr( $wppb_role ); ?>" <?php if( in_array( $wppb_role, $wppb_selected_roles ) ) echo 'checked="checked"'; ?> name="wppb-content-restrict-user-role[]" id="wppb-content-restrict-user-role-<?php echo esc_attr( $wppb_role ) ?>"> |
| 84 |
<label class="wppb-meta-box-checkbox-label" for="wppb-content-restrict-user-role-<?php echo esc_attr( $wppb_role ) ?>"> |
| 85 |
<?php echo !empty( $wppb_role_details['name'] ) ? esc_html( $wppb_role_details['name'] ) : esc_html( $wppb_role ); ?> |
| 86 |
</label> |
| 87 |
</div> |
| 88 |
<?php } ?> |
| 89 |
|
| 90 |
<p class="cozmoslabs-description"> |
| 91 |
<?php echo esc_html( sprintf( __( 'Checking only "Logged In Users" will show this %s to all logged in users, regardless of user role.', 'profile-builder' ), $post->post_type) ); ?> |
| 92 |
</p> |
| 93 |
|
| 94 |
<p class="cozmoslabs-description"> |
| 95 |
<?php echo esc_html( sprintf( __( 'Checking any user role will show this %s only to users that have one of those user roles assigned.', 'profile-builder' ), $post->post_type) ); ?> |
| 96 |
</p> |
| 97 |
<?php } ?> |
| 98 |
|
| 99 |
</div> |
| 100 |
</div> |
| 101 |
</div> |
| 102 |
|
| 103 |
<?php if( isset( $post->post_type ) && $post->post_type == 'product' ) : ?> |
| 104 |
<div class="wppb-meta-box-fields-wrapper cozmoslabs-form-subsection-wrapper"> |
| 105 |
<h4 class="cozmoslabs-subsection-title"><?php esc_html_e( 'Purchase Options', 'profile-builder' ); ?></h4> |
| 106 |
|
| 107 |
<?php |
| 108 |
$wppb_purchase_user_status = get_post_meta( $post->ID, 'wppb-purchase-restrict-user-status', true ); |
| 109 |
$wppb_selected_purchase_roles = get_post_meta( $post->ID, 'wppb-purchase-restrict-user-role' ); |
| 110 |
?> |
| 111 |
|
| 112 |
<!-- Who Can Purchase? options --> |
| 113 |
<div class="wppb-meta-box-field-wrapper cozmoslabs-form-field-wrapper cozmoslabs-checkbox-list-wrapper"> |
| 114 |
<label class="wppb-meta-box-field-label cozmoslabs-form-field-label"><?php esc_html_e( 'Who can purchase?', 'profile-builder' ); ?></label> |
| 115 |
|
| 116 |
<div class="cozmoslabs-checkbox-list cozmoslabs-checkbox-4-col-list"> |
| 117 |
|
| 118 |
<div class="cozmoslabs-chckbox-container"> |
| 119 |
<input type="checkbox" value="loggedin" <?php if( ! empty( $wppb_purchase_user_status ) ) checked($wppb_purchase_user_status, 'loggedin' ); ?> name="wppb-purchase-restrict-user-status" id="wppb-purchase-restrict-user-status"> |
| 120 |
<label class="wppb-meta-box-checkbox-label" for="wppb-purchase-restrict-user-status"> |
| 121 |
<?php esc_html_e( 'Logged In Users', 'profile-builder' ); ?> |
| 122 |
</label> |
| 123 |
</div> |
| 124 |
|
| 125 |
<?php if( !empty( $wppb_roles ) ): foreach( $wppb_roles as $wppb_role => $wppb_role_details ): ?> |
| 126 |
|
| 127 |
<div class="cozmoslabs-chckbox-container"> |
| 128 |
<input type="checkbox" value="<?php echo esc_attr( $wppb_role ); ?>" <?php if( in_array( $wppb_role, $wppb_selected_purchase_roles ) ) echo 'checked="checked"'; ?> name="wppb-purchase-restrict-user-role[]" id="wppb-purchase-restrict-user-role-<?php echo esc_attr( $wppb_role ) ?>"> |
| 129 |
<label class="wppb-meta-box-checkbox-label" for="wppb-purchase-restrict-user-role-<?php echo esc_attr( $wppb_role ) ?>"> |
| 130 |
<?php echo !empty( $wppb_role_details['name'] ) ? esc_html( $wppb_role_details['name'] ) : esc_html( $wppb_role ); ?> |
| 131 |
</label> |
| 132 |
</div> |
| 133 |
|
| 134 |
<?php endforeach; ?> |
| 135 |
|
| 136 |
<p class="cozmoslabs-description" style="margin-top: 10px;"> |
| 137 |
<?php esc_html_e( 'Select who can purchase this product.', 'profile-builder' ); ?> |
| 138 |
</p> |
| 139 |
|
| 140 |
<?php endif; ?> |
| 141 |
</div> |
| 142 |
|
| 143 |
</div> |
| 144 |
</div> |
| 145 |
<?php endif; ?> |
| 146 |
|
| 147 |
<div id="wppb-meta-box-fields-wrapper-restriction-redirect-url" class="wppb-meta-box-fields-wrapper cozmoslabs-form-subsection-wrapper <?php echo ( $wppb_content_restrict_type == 'redirect' ? 'wppb-content-restriction-enabled' : '' ); ?>"> |
| 148 |
<h4 class="cozmoslabs-subsection-title"><?php echo esc_html__( 'Restriction Redirect URL', 'profile-builder' ); ?></h4> |
| 149 |
|
| 150 |
<div class="wppb-meta-box-field-wrapper cozmoslabs-form-field-wrapper cozmoslabs-toggle-switch"> |
| 151 |
|
| 152 |
<?php $wppb_custom_redirect_url_enabled = get_post_meta( $post->ID, 'wppb-content-restrict-custom-redirect-url-enabled', true ); ?> |
| 153 |
|
| 154 |
<label class="wppb-meta-box-field-label cozmoslabs-form-field-label" for="wppb-content-restrict-custom-redirect-url-enabled"><?php esc_html_e( 'Enable Custom Redirect URL', 'profile-builder' ); ?></label> |
| 155 |
|
| 156 |
<div class="cozmoslabs-toggle-container"> |
| 157 |
<input type="checkbox" value="yes" <?php echo ( ! empty( $wppb_custom_redirect_url_enabled ) ? 'checked="checked"' : '' ); ?> name="wppb-content-restrict-custom-redirect-url-enabled" id="wppb-content-restrict-custom-redirect-url-enabled"> |
| 158 |
<label class="cozmoslabs-toggle-track" for="wppb-content-restrict-custom-redirect-url-enabled"></label> |
| 159 |
</div> |
| 160 |
<div class="cozmoslabs-toggle-description"> |
| 161 |
<p class="cozmoslabs-description"><?php echo esc_html( sprintf( __( 'Enable if you wish to add a custom redirect URL for this %s.', 'profile-builder' ), $post->post_type ) ); ?></p> |
| 162 |
</div> |
| 163 |
</div> |
| 164 |
|
| 165 |
<div class="wppb-meta-box-field-wrapper cozmoslabs-form-field-wrapper wppb-meta-box-field-wrapper-custom-redirect-url <?php echo ( ! empty( $wppb_custom_redirect_url_enabled ) ? 'wppb-content-restriction-enabled' : '' ); ?>"> |
| 166 |
<?php $wppb_custom_redirect_url = get_post_meta( $post->ID, 'wppb-content-restrict-custom-redirect-url', true ); ?> |
| 167 |
|
| 168 |
<label class="wppb-meta-box-field-label cozmoslabs-form-field-label" for="wppb-content-restrict-custom-redirect-url"><?php esc_html_e( 'Custom Redirect URL', 'profile-builder' ); ?></label> |
| 169 |
<input type="text" value="<?php echo ( ! empty( $wppb_custom_redirect_url ) ? esc_url( $wppb_custom_redirect_url ) : '' ); ?>" name="wppb-content-restrict-custom-redirect-url" id="wppb-content-restrict-custom-redirect-url" class="widefat"> |
| 170 |
<p class="cozmoslabs-description cozmoslabs-description-space-left"><?php echo esc_html( sprintf( __( 'Add a URL where you wish to redirect users that do not have access to this %s and try to access it directly.', 'profile-builder' ), $post->post_type ) ); ?></p> |
| 171 |
</div> |
| 172 |
</div> |
| 173 |
|
| 174 |
<div id="wppb-meta-box-fields-wrapper-restriction-custom-messages" class="wppb-meta-box-fields-wrapper cozmoslabs-form-subsection-wrapper"> |
| 175 |
<h4 class="cozmoslabs-subsection-title"><?php echo esc_html__( 'Restriction Messages', 'profile-builder' ); ?></h4> |
| 176 |
|
| 177 |
<div class="wppb-meta-box-field-wrapper cozmoslabs-form-field-wrapper cozmoslabs-toggle-switch"> |
| 178 |
<?php |
| 179 |
$wppb_custom_messages_enabled = get_post_meta( $post->ID, 'wppb-content-restrict-messages-enabled', true ); |
| 180 |
?> |
| 181 |
<label class="wppb-meta-box-field-label cozmoslabs-form-field-label" for="wppb-content-restrict-messages-enabled"><?php esc_html_e( 'Enable Custom Messages', 'profile-builder' ); ?></label> |
| 182 |
|
| 183 |
<div class="cozmoslabs-toggle-container"> |
| 184 |
<input type="checkbox" value="yes" <?php echo ( ! empty( $wppb_custom_messages_enabled ) ? 'checked="checked"' : '' ); ?> name="wppb-content-restrict-messages-enabled" id="wppb-content-restrict-messages-enabled"> |
| 185 |
<label class="cozmoslabs-toggle-track" for="wppb-content-restrict-messages-enabled"></label> |
| 186 |
</div> |
| 187 |
<div class="cozmoslabs-toggle-description"> |
| 188 |
<p class="cozmoslabs-description"><?php echo esc_html( sprintf( __( 'Enable if you wish to add custom messages for this %s.', 'profile-builder' ), $post->post_type ) ); ?></p> |
| 189 |
</div> |
| 190 |
</div> |
| 191 |
|
| 192 |
<div class="wppb-meta-box-field-wrapper-custom-messages <?php echo ( ! empty( $wppb_custom_messages_enabled ) ? 'wppb-content-restriction-enabled' : '' ); ?>"> |
| 193 |
<?php do_action( 'wppb_view_meta_box_content_restrict_restriction_messages_top', $post->ID ); ?> |
| 194 |
|
| 195 |
<div class="cozmoslabs-form-field-wrapper cozmoslabs-wysiwyg-wrapper cozmoslabs-wysiwyg-indented"> |
| 196 |
<label class="cozmoslabs-form-field-label"><?php esc_html_e( 'Messages for logged-out users', 'profile-builder' ); ?></label> |
| 197 |
<?php wp_editor( get_post_meta( $post->ID, 'wppb-content-restrict-message-logged_out', true ), 'wppb-content-restrict-message-logged_out', array( 'textarea_name' => 'wppb-content-restrict-message-logged_out', 'editor_height' => 180 ) ); ?> |
| 198 |
</div> |
| 199 |
|
| 200 |
<div class="cozmoslabs-form-field-wrapper cozmoslabs-wysiwyg-wrapper cozmoslabs-wysiwyg-indented"> |
| 201 |
<label class="cozmoslabs-form-field-label"><?php esc_html_e( 'Messages for logged-in users', 'profile-builder' ); ?></label> |
| 202 |
<?php wp_editor( get_post_meta( $post->ID, 'wppb-content-restrict-message-logged_in', true ), 'wppb-content-restrict-message-logged_in', array( 'textarea_name' => 'wppb-content-restrict-message-logged_in', 'editor_height' => 180 ) ); ?> |
| 203 |
</div> |
| 204 |
|
| 205 |
<?php if( isset( $post->post_type ) && $post->post_type == 'product' ) : ?> |
| 206 |
|
| 207 |
<div class="cozmoslabs-form-field-wrapper cozmoslabs-wysiwyg-wrapper cozmoslabs-wysiwyg-indented"> |
| 208 |
<label class="cozmoslabs-form-field-label"><?php esc_html_e( 'Messages for restricted product purchase', 'profile-builder' ); ?></label> |
| 209 |
<?php wp_editor( wp_kses_post( get_post_meta( $post->ID, 'wppb-content-restrict-message-purchasing_restricted', true ) ), 'wppb-content-restrict-message-purchasing_restricted', array( 'textarea_name' => 'wppb-content-restrict-message-purchasing_restricted', 'editor_height' => 180 ) ); ?> |
| 210 |
</div> |
| 211 |
|
| 212 |
<?php endif; ?> |
| 213 |
</div> |
| 214 |
</div> |
| 215 |
|
| 216 |
<?php |
| 217 |
wp_nonce_field( 'wppb_meta_box_single_content_restriction_nonce', 'wppb_content_restriction_token', false ); |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
function wppb_content_restriction_save_data( $post_id ) { |
| 222 |
|
| 223 |
if( empty( $_POST['wppb_content_restriction_token'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['wppb_content_restriction_token'] ), 'wppb_meta_box_single_content_restriction_nonce' ) ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
// Handle restriction rules |
| 228 |
delete_post_meta( $post_id, 'wppb-content-restrict-type' ); |
| 229 |
|
| 230 |
if( ! empty( $_POST['wppb-content-restrict-type'] ) ) { |
| 231 |
update_post_meta( $post_id, 'wppb-content-restrict-type', sanitize_text_field( $_POST['wppb-content-restrict-type'] ) ); |
| 232 |
} |
| 233 |
|
| 234 |
if( isset( $_POST['wppb-content-restrict-user-status'] ) && $_POST['wppb-content-restrict-user-status'] === 'loggedin' ) { |
| 235 |
delete_post_meta( $post_id, 'wppb-content-restrict-user-role' ); |
| 236 |
|
| 237 |
if( isset( $_POST['wppb-content-restrict-user-role'] ) && is_array( $_POST['wppb-content-restrict-user-role'] ) ) { |
| 238 |
$user_role_ids = array_map( 'sanitize_text_field', $_POST['wppb-content-restrict-user-role'] ); |
| 239 |
foreach( $user_role_ids as $user_role_id ) { |
| 240 |
if( ! empty( $user_role_id ) ) { |
| 241 |
add_post_meta( $post_id, 'wppb-content-restrict-user-role', $user_role_id ); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if( isset( $_POST['wppb-content-restrict-user-status'] ) && $_POST['wppb-content-restrict-user-status'] == 'loggedin' ) { |
| 248 |
update_post_meta( $post_id, 'wppb-content-restrict-user-status', 'loggedin' ); |
| 249 |
} else { |
| 250 |
delete_post_meta( $post_id, 'wppb-content-restrict-user-status' ); |
| 251 |
} |
| 252 |
|
| 253 |
// Handle custom redirect URL |
| 254 |
delete_post_meta( $post_id, 'wppb-content-restrict-custom-redirect-url-enabled' ); |
| 255 |
|
| 256 |
if( isset( $_POST['wppb-content-restrict-custom-redirect-url-enabled'] ) ) { |
| 257 |
update_post_meta( $post_id, 'wppb-content-restrict-custom-redirect-url-enabled', 'yes' ); |
| 258 |
} |
| 259 |
|
| 260 |
update_post_meta( $post_id, 'wppb-content-restrict-custom-redirect-url', ( ! empty( $_POST['wppb-content-restrict-custom-redirect-url'] ) ? esc_url_raw( $_POST['wppb-content-restrict-custom-redirect-url'] ) : '' ) ); |
| 261 |
|
| 262 |
// Handle custom messages |
| 263 |
delete_post_meta( $post_id, 'wppb-content-restrict-messages-enabled' ); |
| 264 |
|
| 265 |
if( isset( $_POST['wppb-content-restrict-messages-enabled'] ) ) { |
| 266 |
update_post_meta( $post_id, 'wppb-content-restrict-messages-enabled', 'yes' ); |
| 267 |
} |
| 268 |
|
| 269 |
update_post_meta( $post_id, 'wppb-content-restrict-message-logged_out', ( ! empty( $_POST['wppb-content-restrict-message-logged_out'] ) ? wp_kses_post( $_POST['wppb-content-restrict-message-logged_out'] ) : '' ) ); |
| 270 |
update_post_meta( $post_id, 'wppb-content-restrict-message-logged_in', ( ! empty( $_POST['wppb-content-restrict-message-logged_in'] ) ? wp_kses_post( $_POST['wppb-content-restrict-message-logged_in'] ) : '' ) ); |
| 271 |
|
| 272 |
// Handle WooCommerce purchase settings |
| 273 |
delete_post_meta( $post_id, 'wppb-purchase-restrict-user-role' ); |
| 274 |
if( isset( $_POST['wppb-purchase-restrict-user-role'] ) && is_array( $_POST['wppb-purchase-restrict-user-role'] ) ) { |
| 275 |
$restrict_roles = array_map( 'sanitize_text_field', $_POST['wppb-purchase-restrict-user-role'] ); |
| 276 |
foreach ( $restrict_roles as $role ) { |
| 277 |
if( !empty( $role ) ) |
| 278 |
add_post_meta($post_id, 'wppb-purchase-restrict-user-role', sanitize_text_field( $role ) ); |
| 279 |
} |
| 280 |
|
| 281 |
} |
| 282 |
|
| 283 |
if( isset( $_POST['wppb-purchase-restrict-user-status'] ) && $_POST['wppb-purchase-restrict-user-status'] === 'loggedin' ) |
| 284 |
update_post_meta( $post_id, 'wppb-purchase-restrict-user-status', 'loggedin' ); |
| 285 |
else |
| 286 |
delete_post_meta( $post_id, 'wppb-purchase-restrict-user-status' ); |
| 287 |
|
| 288 |
// save custom "product purchase restricted" message |
| 289 |
update_post_meta( $post_id, 'wppb-content-restrict-message-purchasing_restricted', ( ! empty( $_POST['wppb-content-restrict-message-purchasing_restricted'] ) ? wp_kses_post($_POST['wppb-content-restrict-message-purchasing_restricted']) : '' ) ); |
| 290 |
|
| 291 |
} |
| 292 |
add_action( 'save_post', 'wppb_content_restriction_save_data' ); |
| 293 |
add_action( 'edit_attachment', 'wppb_content_restriction_save_data' ); |
| 294 |
|