| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/* Verifies whether the current post or the post with the provided id has any restrictions in place */ |
| 5 |
function wppb_content_restriction_is_post_restricted( $post_id = null ) { |
| 6 |
|
| 7 |
//fixes some php warnings with Onfleek theme |
| 8 |
if( is_array( $post_id ) || empty( $post_id ) ) |
| 9 |
$post_id = null; |
| 10 |
|
| 11 |
global $post, $wppb_show_content, $wppb_is_post_restricted_arr; |
| 12 |
|
| 13 |
// If we have a cached result, return it |
| 14 |
if( isset( $wppb_is_post_restricted_arr[$post_id] ) ) { |
| 15 |
return $wppb_is_post_restricted_arr[$post_id]; |
| 16 |
} |
| 17 |
|
| 18 |
$post_obj = $post; |
| 19 |
|
| 20 |
if( ! is_null( $post_id ) ) { |
| 21 |
$post_obj = get_post( $post_id ); |
| 22 |
} |
| 23 |
|
| 24 |
// This filter was added in order to take advantage of the existing functions that hook to the_content and check to see if the post is restricted or not |
| 25 |
$t = apply_filters( 'wppb_content_restriction_post_check', '', $post_obj ); |
| 26 |
|
| 27 |
// Cache the result for further usage |
| 28 |
if( $wppb_show_content === false ) { |
| 29 |
$wppb_is_post_restricted_arr[$post_id] = true; |
| 30 |
} else { |
| 31 |
$wppb_is_post_restricted_arr[$post_id] = false; |
| 32 |
} |
| 33 |
|
| 34 |
return $wppb_is_post_restricted_arr[$post_id]; |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
function wppb_content_restriction_check_user_access( $user_status, $user_roles = array(), $user_id = 0 ) { |
| 39 |
|
| 40 |
$user_roles_without_logged_in_option = apply_filters( 'wppb_content_restriction_enable_user_roles_without_logged_in_option', true ); |
| 41 |
|
| 42 |
if ( empty( $user_status ) && empty( $user_roles ) ) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
if ( $user_status === 'loggedin' || ( $user_roles_without_logged_in_option && ! empty( $user_roles ) ) ) { |
| 47 |
if ( is_user_logged_in() ) { |
| 48 |
if ( ! empty( $user_roles ) ) { |
| 49 |
$user_data = get_userdata( $user_id ); |
| 50 |
|
| 51 |
if ( empty( $user_data ) || empty( $user_data->roles ) ) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
foreach ( $user_roles as $restricted_role ) { |
| 56 |
if ( in_array( $restricted_role, $user_data->roles, true ) ) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
function wppb_content_restriction_get_restricted_term_for_post( $post_id ) { |
| 74 |
|
| 75 |
$post = get_post( $post_id ); |
| 76 |
|
| 77 |
if ( empty( $post ) ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
$taxonomies = get_object_taxonomies( $post->post_type, 'names' ); |
| 82 |
|
| 83 |
if ( empty( $taxonomies ) ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
$terms = wp_get_post_terms( $post_id, $taxonomies ); |
| 88 |
|
| 89 |
if ( empty( $terms ) || is_wp_error( $terms ) ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
foreach ( $terms as $term ) { |
| 94 |
$user_status = get_term_meta( $term->term_id, 'wppb-content-restrict-user-status', true ); |
| 95 |
$user_roles = get_term_meta( $term->term_id, 'wppb-content-restrict-user-role' ); |
| 96 |
|
| 97 |
if ( ! empty( $user_status ) || ! empty( $user_roles ) ) { |
| 98 |
return $term; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
function wppb_content_restriction_get_restricted_term_from_query() { |
| 106 |
|
| 107 |
if ( ! is_category() && ! is_tag() && ! is_tax() ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
$term = get_queried_object(); |
| 112 |
|
| 113 |
if ( empty( $term ) || empty( $term->term_id ) || empty( $term->taxonomy ) ) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
$user_status = get_term_meta( $term->term_id, 'wppb-content-restrict-user-status', true ); |
| 118 |
$user_roles = get_term_meta( $term->term_id, 'wppb-content-restrict-user-role' ); |
| 119 |
|
| 120 |
if ( empty( $user_status ) && empty( $user_roles ) ) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
return $term; |
| 125 |
} |
| 126 |
|
| 127 |
/* Returns the restriction message added by the admin in the settings page or a default message if the first one is missing */ |
| 128 |
function wppb_get_restriction_content_message( $message_type = '', $post_id = 0 ) { |
| 129 |
|
| 130 |
$wppb_content_restriction_settings = get_option( 'wppb_content_restriction_settings', 'not_found' ); |
| 131 |
$wppb_content_restriction_message = ''; |
| 132 |
|
| 133 |
if( $message_type == 'logged_out' ) { |
| 134 |
$wppb_content_restriction_message = ( ( $wppb_content_restriction_settings != 'not_found' && ! empty( $wppb_content_restriction_settings['message_logged_out'] ) ) ? $wppb_content_restriction_settings['message_logged_out'] : __( 'You must be logged in to view this content.', 'profile-builder' ) ); |
| 135 |
} elseif ( $message_type == 'logged_in' ) { |
| 136 |
$wppb_content_restriction_message = ( ( $wppb_content_restriction_settings != 'not_found' && ! empty( $wppb_content_restriction_settings['message_logged_in'] ) ) ? $wppb_content_restriction_settings['message_logged_in'] : __( 'This content is restricted for your user role.', 'profile-builder' ) ); |
| 137 |
} elseif ( $message_type == 'purchasing_restricted' ) { |
| 138 |
$wppb_content_restriction_message = ( ( $wppb_content_restriction_settings != 'not_found' && ! empty( $wppb_content_restriction_settings['purchasing_restricted'] ) ) ? $wppb_content_restriction_settings['purchasing_restricted'] : __( 'This product cannot be purchased by your user role.', 'profile-builder' ) ); |
| 139 |
} else { |
| 140 |
$wppb_content_restriction_message = apply_filters( 'wppb_get_restriction_content_message_default', $wppb_content_restriction_message, $message_type, $wppb_content_restriction_settings ); |
| 141 |
} |
| 142 |
|
| 143 |
$custom_message_enabled = get_post_meta( $post_id, 'wppb-content-restrict-messages-enabled', true ); |
| 144 |
|
| 145 |
if( ! empty( $post_id ) && ! empty( $custom_message_enabled ) ) { |
| 146 |
$custom_message = get_post_meta( $post_id, 'wppb-content-restrict-message-' . $message_type, true ); |
| 147 |
|
| 148 |
if( ! empty( $custom_message ) ) { |
| 149 |
$wppb_content_restriction_message = $custom_message; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return wp_kses_post( $wppb_content_restriction_message ); |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
function wppb_get_term_restriction_content_message( $message_type = '', $term_id = 0 ) { |
| 158 |
|
| 159 |
$wppb_content_restriction_settings = get_option( 'wppb_content_restriction_settings', 'not_found' ); |
| 160 |
$wppb_content_restriction_message = ''; |
| 161 |
|
| 162 |
if( $message_type == 'logged_out' ) { |
| 163 |
$wppb_content_restriction_message = ( ( $wppb_content_restriction_settings != 'not_found' && ! empty( $wppb_content_restriction_settings['message_logged_out'] ) ) ? $wppb_content_restriction_settings['message_logged_out'] : __( 'You must be logged in to view this content.', 'profile-builder' ) ); |
| 164 |
} elseif ( $message_type == 'logged_in' ) { |
| 165 |
$wppb_content_restriction_message = ( ( $wppb_content_restriction_settings != 'not_found' && ! empty( $wppb_content_restriction_settings['message_logged_in'] ) ) ? $wppb_content_restriction_settings['message_logged_in'] : __( 'This content is restricted for your user role.', 'profile-builder' ) ); |
| 166 |
} else { |
| 167 |
$wppb_content_restriction_message = apply_filters( 'wppb_get_restriction_content_message_default', $wppb_content_restriction_message, $message_type, $wppb_content_restriction_settings ); |
| 168 |
} |
| 169 |
|
| 170 |
$custom_message_enabled = get_term_meta( $term_id, 'wppb-content-restrict-messages-enabled', true ); |
| 171 |
|
| 172 |
if( ! empty( $term_id ) && ! empty( $custom_message_enabled ) ) { |
| 173 |
$custom_message = get_term_meta( $term_id, 'wppb-content-restrict-message-' . $message_type, true ); |
| 174 |
|
| 175 |
if( ! empty( $custom_message ) ) { |
| 176 |
$wppb_content_restriction_message = $custom_message; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
return wp_kses_post( $wppb_content_restriction_message ); |
| 181 |
} |
| 182 |
|
| 183 |
/* Returns the restriction message with any tags processed */ |
| 184 |
function wppb_content_restriction_process_content_message( $type, $user_ID, $post_id = 0 ) { |
| 185 |
|
| 186 |
$message = wppb_get_restriction_content_message( $type, $post_id ); |
| 187 |
$user_info = get_userdata( $user_ID ); |
| 188 |
$message = wppb_content_restriction_merge_tags( $message, $user_info, $post_id ); |
| 189 |
|
| 190 |
return '<span class="wppb-frontend-restriction-message wppb-content-restriction-message">'. $message .'</span>'; |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
function wppb_content_restriction_process_term_content_message( $type, $user_ID, $term_id = 0 ) { |
| 195 |
|
| 196 |
$message = wppb_get_term_restriction_content_message( $type, $term_id ); |
| 197 |
$user_info = get_userdata( $user_ID ); |
| 198 |
$message = wppb_content_restriction_merge_term_tags( $message, $user_info, $term_id ); |
| 199 |
|
| 200 |
return '<span class="wppb-frontend-restriction-message wppb-content-restriction-message">'. $message .'</span>'; |
| 201 |
} |
| 202 |
|
| 203 |
/* Return the restriction message to be displayed to the user. If the current post is not restricted / it was not checked to see if it is restricted an empty string is returned */ |
| 204 |
function wppb_content_restriction_get_post_message( $post_id = 0 ) { |
| 205 |
|
| 206 |
global $post, $user_ID, $wppb_show_content; |
| 207 |
|
| 208 |
if( ! empty( $post_id ) ) { |
| 209 |
$post = get_post( $post_id ); |
| 210 |
} |
| 211 |
|
| 212 |
// If the $wppb_show_content global is different than false then the post is either not restricted or not processed for restriction |
| 213 |
if( $wppb_show_content !== false ) { |
| 214 |
return ''; |
| 215 |
} |
| 216 |
|
| 217 |
if( ! is_user_logged_in() ) { |
| 218 |
$message_type = 'logged_out'; |
| 219 |
} else { |
| 220 |
$message_type = 'logged_in'; |
| 221 |
} |
| 222 |
|
| 223 |
$message_type = apply_filters( 'wppb_get_restricted_post_message_type', $message_type ); |
| 224 |
|
| 225 |
$message = wppb_content_restriction_process_content_message( $message_type, $user_ID, $post->ID ); |
| 226 |
|
| 227 |
// Filter the restriction message before returning it |
| 228 |
$message = apply_filters( 'wppb_restriction_message_' . $message_type, $message, $post->post_content, $post, $user_ID ); |
| 229 |
|
| 230 |
return do_shortcode( $message ); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
/* Checks to see if the current post is restricted and if any redirect URLs are in place the user is redirected to the URL with the highest priority */ |
| 235 |
function wppb_content_restriction_post_redirect() { |
| 236 |
// try not to overwrite $post. Can have side-effects with other plugins. |
| 237 |
global $post; |
| 238 |
$woo_shop_or_post = $post; |
| 239 |
|
| 240 |
if( function_exists( 'wc_get_page_id' ) ) {//redirect restriction for woocommerce shop page |
| 241 |
if ( !is_singular() && !( is_post_type_archive('product') || is_page(wc_get_page_id('shop')) ) ){ |
| 242 |
return; |
| 243 |
} |
| 244 |
|
| 245 |
if( is_post_type_archive('product') || is_page(wc_get_page_id('shop')) ){ |
| 246 |
$woo_shop_or_post = get_post( wc_get_page_id('shop') ); |
| 247 |
} |
| 248 |
} |
| 249 |
else { |
| 250 |
if (!is_singular()) { |
| 251 |
return; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ( !($woo_shop_or_post instanceof WP_Post) ){ |
| 256 |
return; |
| 257 |
} |
| 258 |
|
| 259 |
$woo_shop_or_post->ID = apply_filters( 'wppb_restricted_post_redirect_post_id', $woo_shop_or_post->ID ); |
| 260 |
|
| 261 |
$redirect_url = ''; |
| 262 |
$post_restriction_type = get_post_meta( $woo_shop_or_post->ID, 'wppb-content-restrict-type', true ); |
| 263 |
$settings = get_option( 'wppb_content_restriction_settings', array() ); |
| 264 |
$general_restriction_type = ( ! empty( $settings['restrict_type'] ) ? $settings['restrict_type'] : 'message' ); |
| 265 |
|
| 266 |
if( $post_restriction_type !== 'redirect' && $general_restriction_type !== 'redirect' ) { |
| 267 |
return; |
| 268 |
} |
| 269 |
|
| 270 |
if( ! in_array( $post_restriction_type, array( 'default', 'redirect' ) ) ) { |
| 271 |
return; |
| 272 |
} |
| 273 |
|
| 274 |
if( ! wppb_content_restriction_is_post_restricted( $woo_shop_or_post->ID ) ) { |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
// Get the redirect URL from the post meta if enabled |
| 279 |
if( $post_restriction_type === 'redirect' ) { |
| 280 |
$post_redirect_url_enabled = get_post_meta( $woo_shop_or_post->ID, 'wppb-content-restrict-custom-redirect-url-enabled', true ); |
| 281 |
$post_redirect_url = get_post_meta( $woo_shop_or_post->ID, 'wppb-content-restrict-custom-redirect-url', true ); |
| 282 |
|
| 283 |
$redirect_url = ( ! empty( $post_redirect_url_enabled ) && ! empty( $post_redirect_url ) ? $post_redirect_url : '' ); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
// If the post doesn't have a custom redirect URL set, get the default from the Settings page |
| 288 |
if( empty( $redirect_url ) ) { |
| 289 |
$redirect_url = ( ! empty( $settings['redirect_url'] ) ? $settings['redirect_url'] : '' ); |
| 290 |
} |
| 291 |
|
| 292 |
if( empty( $redirect_url ) ) { |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
// To avoid a redirect loop we break in case the redirect URL is the same as the current page URL |
| 297 |
$current_url = wppb_curpageurl(); |
| 298 |
|
| 299 |
if( $current_url == $redirect_url ) { |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
// Allow filtering whether to add the 'redirect_to' parameter |
| 304 |
$add_redirect_to = apply_filters( 'wppb_add_redirect_to_param', true, $current_url ); |
| 305 |
|
| 306 |
// Build the query arguments |
| 307 |
$query_args = array( 'wppb_referer_url' => urlencode( wppb_curpageurl() ) ); |
| 308 |
|
| 309 |
if ( $add_redirect_to ) { |
| 310 |
$query_args['redirect_to'] = $current_url; |
| 311 |
} |
| 312 |
// Pass the correct referer URL forward |
| 313 |
$redirect_url = add_query_arg( $query_args, wppb_add_missing_http( $redirect_url ) ); |
| 314 |
// Redirect |
| 315 |
nocache_headers(); |
| 316 |
wp_redirect( apply_filters( 'wppb_restricted_post_redirect_url', $redirect_url ) ); |
| 317 |
exit; |
| 318 |
|
| 319 |
} |
| 320 |
add_action( 'template_redirect', 'wppb_content_restriction_post_redirect' ); |
| 321 |
|
| 322 |
/* Checks to see if the current taxonomy archive is restricted and if any redirect URLs are in place the user is redirected to the URL with the highest priority */ |
| 323 |
function wppb_content_restriction_taxonomy_redirect() { |
| 324 |
|
| 325 |
$restricted_term = wppb_content_restriction_get_restricted_term_from_query(); |
| 326 |
|
| 327 |
if ( ! $restricted_term ) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
$redirect_url = ''; |
| 332 |
$term_restriction_type = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-type', true ); |
| 333 |
$settings = get_option( 'wppb_content_restriction_settings', array() ); |
| 334 |
$general_restriction_type = ( ! empty( $settings['restrict_type'] ) ? $settings['restrict_type'] : 'message' ); |
| 335 |
|
| 336 |
if ( $term_restriction_type !== 'redirect' && $general_restriction_type !== 'redirect' ) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
if ( ! in_array( $term_restriction_type, array( 'default', 'redirect' ), true ) ) { |
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
$term_user_status = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-user-status', true ); |
| 345 |
$term_user_roles = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-user-role' ); |
| 346 |
|
| 347 |
if ( wppb_content_restriction_check_user_access( $term_user_status, $term_user_roles, get_current_user_id() ) ) { |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
if ( $term_restriction_type === 'redirect' ) { |
| 352 |
$term_redirect_url_enabled = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-custom-redirect-url-enabled', true ); |
| 353 |
$term_redirect_url = get_term_meta( $restricted_term->term_id, 'wppb-content-restrict-custom-redirect-url', true ); |
| 354 |
|
| 355 |
$redirect_url = ( ! empty( $term_redirect_url_enabled ) && ! empty( $term_redirect_url ) ? $term_redirect_url : '' ); |
| 356 |
} |
| 357 |
|
| 358 |
// If the taxonomy doesn't have a custom redirect URL set, get the default from the Settings page |
| 359 |
if ( empty( $redirect_url ) ) { |
| 360 |
$redirect_url = ( ! empty( $settings['redirect_url'] ) ? $settings['redirect_url'] : '' ); |
| 361 |
} |
| 362 |
|
| 363 |
if ( empty( $redirect_url ) ) { |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
// To avoid a redirect loop we break in case the redirect URL is the same as the current page URL |
| 368 |
$current_url = wppb_curpageurl(); |
| 369 |
|
| 370 |
if ( $current_url == $redirect_url ) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
// Allow filtering whether to add the 'redirect_to' parameter |
| 375 |
$add_redirect_to = apply_filters( 'wppb_add_redirect_to_param', true, $current_url ); |
| 376 |
|
| 377 |
// Build the query arguments |
| 378 |
$query_args = array( 'wppb_referer_url' => urlencode( wppb_curpageurl() ) ); |
| 379 |
|
| 380 |
if ( $add_redirect_to ) { |
| 381 |
$query_args['redirect_to'] = $current_url; |
| 382 |
} |
| 383 |
|
| 384 |
// Pass the correct referer URL forward |
| 385 |
$redirect_url = add_query_arg( $query_args, wppb_add_missing_http( $redirect_url ) ); |
| 386 |
|
| 387 |
// Redirect |
| 388 |
nocache_headers(); |
| 389 |
wp_redirect( apply_filters( 'wppb_restricted_term_redirect_url', $redirect_url, $restricted_term ) ); |
| 390 |
exit; |
| 391 |
} |
| 392 |
add_action( 'template_redirect', 'wppb_content_restriction_taxonomy_redirect' ); |
| 393 |
|
| 394 |
/* Function that searches and replaces merge tags with their values */ |
| 395 |
function wppb_content_restriction_merge_tags( $text, $user_info, $post_id = 0 ) { |
| 396 |
|
| 397 |
$merge_tags = apply_filters( 'wppb_content_restriction_merge_tags', array( 'display_name', 'unrestricted_user_roles', 'current_user_role' ) ); |
| 398 |
|
| 399 |
if( ! empty( $merge_tags ) ) { |
| 400 |
foreach( $merge_tags as $merge_tag ) { |
| 401 |
$text = str_replace( '{{'. $merge_tag .'}}', apply_filters( 'wppb_content_restriction_merge_tag_'. $merge_tag, '', $user_info, $post_id ), $text ); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
return $text; |
| 406 |
|
| 407 |
} |
| 408 |
|
| 409 |
/* Function that searches and replaces merge tags with their values for taxonomy restriction messages */ |
| 410 |
function wppb_content_restriction_merge_term_tags( $text, $user_info, $term_id = 0 ) { |
| 411 |
|
| 412 |
$merge_tags = apply_filters( 'wppb_content_restriction_merge_tags', array( 'display_name', 'unrestricted_user_roles', 'current_user_role' ) ); |
| 413 |
|
| 414 |
if( ! empty( $merge_tags ) ) { |
| 415 |
foreach( $merge_tags as $merge_tag ) { |
| 416 |
$text = str_replace( '{{'. $merge_tag .'}}', apply_filters( 'wppb_term_content_restriction_merge_tag_'. $merge_tag, '', $user_info, $term_id ), $text ); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
return $text; |
| 421 |
|
| 422 |
} |
| 423 |
|
| 424 |
/* Add functionality for display_name tag */ |
| 425 |
function wppb_content_restriction_tag_display_name( $value, $user_info, $post_id = 0 ) { |
| 426 |
|
| 427 |
if( ! empty( $user_info->display_name ) ) { |
| 428 |
return $user_info->display_name; |
| 429 |
} else if( ! empty( $user_info->user_login ) ) { |
| 430 |
return $user_info->user_login; |
| 431 |
} else { |
| 432 |
return ''; |
| 433 |
} |
| 434 |
|
| 435 |
} |
| 436 |
add_filter( 'wppb_content_restriction_merge_tag_display_name', 'wppb_content_restriction_tag_display_name', 10, 3 ); |
| 437 |
add_filter( 'wppb_term_content_restriction_merge_tag_display_name', 'wppb_content_restriction_tag_display_name', 10, 3 ); |
| 438 |
|
| 439 |
/* Add functionality for unrestricted_user_roles tag */ |
| 440 |
function wppb_content_restriction_tag_unrestricted_user_roles( $value, $user_info, $post_id = 0 ) { |
| 441 |
|
| 442 |
if( $post_id != 0 ) { |
| 443 |
$unrestricted_user_roles = get_post_meta( $post_id, 'wppb-content-restrict-user-role' ); |
| 444 |
|
| 445 |
if( ! empty( $unrestricted_user_roles ) ) { |
| 446 |
$user_roles = implode( ', ', $unrestricted_user_roles ); |
| 447 |
|
| 448 |
return $user_roles; |
| 449 |
} else { |
| 450 |
return ''; |
| 451 |
} |
| 452 |
} else { |
| 453 |
return ''; |
| 454 |
} |
| 455 |
|
| 456 |
} |
| 457 |
add_filter( 'wppb_content_restriction_merge_tag_unrestricted_user_roles', 'wppb_content_restriction_tag_unrestricted_user_roles', 10, 3 ); |
| 458 |
|
| 459 |
/* Add functionality for unrestricted_user_roles tag on taxonomy restriction messages */ |
| 460 |
function wppb_content_restriction_term_tag_unrestricted_user_roles( $value, $user_info, $term_id = 0 ) { |
| 461 |
|
| 462 |
if( $term_id != 0 ) { |
| 463 |
$unrestricted_user_roles = get_term_meta( $term_id, 'wppb-content-restrict-user-role' ); |
| 464 |
|
| 465 |
if( ! empty( $unrestricted_user_roles ) ) { |
| 466 |
$user_roles = implode( ', ', $unrestricted_user_roles ); |
| 467 |
|
| 468 |
return $user_roles; |
| 469 |
} else { |
| 470 |
return ''; |
| 471 |
} |
| 472 |
} else { |
| 473 |
return ''; |
| 474 |
} |
| 475 |
|
| 476 |
} |
| 477 |
add_filter( 'wppb_term_content_restriction_merge_tag_unrestricted_user_roles', 'wppb_content_restriction_term_tag_unrestricted_user_roles', 10, 3 ); |
| 478 |
|
| 479 |
/* Add functionality for current_user_role tag */ |
| 480 |
function wppb_content_restriction_tag_current_user_role( $value, $user_info, $post_id = 0 ) { |
| 481 |
|
| 482 |
if( ! empty( $user_info ) && ! empty( $user_info->roles ) ) { |
| 483 |
$user_role = implode( ', ', $user_info->roles ); |
| 484 |
|
| 485 |
return $user_role; |
| 486 |
} else { |
| 487 |
return ''; |
| 488 |
} |
| 489 |
|
| 490 |
} |
| 491 |
add_filter( 'wppb_content_restriction_merge_tag_current_user_role', 'wppb_content_restriction_tag_current_user_role', 10, 3 ); |
| 492 |
add_filter( 'wppb_term_content_restriction_merge_tag_current_user_role', 'wppb_content_restriction_tag_current_user_role', 10, 3 ); |
| 493 |
|
| 494 |
/* Content restriction shortcode */ |
| 495 |
function wppb_content_restriction_shortcode( $atts, $content = null ) { |
| 496 |
|
| 497 |
$args = shortcode_atts( |
| 498 |
array( |
| 499 |
'user_roles' => array(), |
| 500 |
'display_to' => '', |
| 501 |
'message' => '', |
| 502 |
'users_id' => array() |
| 503 |
), |
| 504 |
$atts |
| 505 |
); |
| 506 |
|
| 507 |
// Message to replace the content of checks do not match |
| 508 |
if( ! empty( $args['message'] ) ) { |
| 509 |
$message = '<span class="wppb-shortcode-restriction-message">' . wp_kses_post( $args['message'] ) . '</span>'; |
| 510 |
} else { |
| 511 |
$type = ( is_user_logged_in() ? 'logged_in' : 'logged_out' ); |
| 512 |
$message = '<span class="wppb-content-restriction-message">' . wpautop( wppb_get_restriction_content_message( $type ) ) . '</span>'; |
| 513 |
} |
| 514 |
|
| 515 |
/* |
| 516 |
* Filter the message |
| 517 |
* |
| 518 |
* @param string $message - the current message, whether it is the default one from the settings or |
| 519 |
* the one set in the shortcode attributes |
| 520 |
* @param array $args - the shortcode attributes |
| 521 |
* |
| 522 |
*/ |
| 523 |
$message = apply_filters( 'wppb_content_restriction_shortcode_message', $message, $args ); |
| 524 |
|
| 525 |
if( is_user_logged_in() ) { |
| 526 |
// Show for administrators |
| 527 |
if( current_user_can( 'manage_options' ) ) { |
| 528 |
return do_shortcode( $content ); |
| 529 |
} |
| 530 |
|
| 531 |
if( $args['display_to'] == 'not_logged_in' ) { |
| 532 |
return $message; |
| 533 |
} |
| 534 |
|
| 535 |
if( ! empty($args['users_id'] ) ){ |
| 536 |
$users_id=array_map('trim', explode(',', $args['users_id'])); |
| 537 |
$current_user_id = get_current_user_id(); // the current id user |
| 538 |
if( ! empty($current_user_id)){ |
| 539 |
if(in_array($current_user_id, $users_id)) |
| 540 |
{ |
| 541 |
return do_shortcode( $content ); |
| 542 |
} |
| 543 |
else{ |
| 544 |
return $message; |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
elseif ( $args['display_to'] == 'not_role' ){ |
| 549 |
if( ! empty( $args['user_roles'] ) ){ |
| 550 |
$user_roles = array_map( 'trim', explode( ',', $args['user_roles'] ) ); |
| 551 |
$user_data = get_userdata( get_current_user_id() ); |
| 552 |
|
| 553 |
if( ! empty( $user_data->roles ) ){ |
| 554 |
$common_user_roles = array_intersect( $user_roles, $user_data->roles ); |
| 555 |
|
| 556 |
if( ! empty( $common_user_roles ) ) { |
| 557 |
return $message; |
| 558 |
} else { |
| 559 |
return do_shortcode( $content ); |
| 560 |
} |
| 561 |
} |
| 562 |
} |
| 563 |
else{ |
| 564 |
return $message; |
| 565 |
} |
| 566 |
|
| 567 |
} |
| 568 |
elseif( ! empty( $args['user_roles'] ) && ( $args['display_to'] != 'not_role' || !isset( $args['display_to'] ) ) ) { |
| 569 |
$user_roles = array_map( 'trim', explode( ',', $args['user_roles'] ) ); |
| 570 |
$user_data = get_userdata( get_current_user_id() ); |
| 571 |
|
| 572 |
if( ! empty( $user_data->roles ) ) { |
| 573 |
$common_user_roles = array_intersect( $user_roles, $user_data->roles ); |
| 574 |
|
| 575 |
if( ! empty( $common_user_roles ) ) { |
| 576 |
return do_shortcode( $content ); |
| 577 |
} else { |
| 578 |
return $message; |
| 579 |
} |
| 580 |
} |
| 581 |
} else { |
| 582 |
|
| 583 |
return do_shortcode( $content ); |
| 584 |
} |
| 585 |
|
| 586 |
} else { |
| 587 |
if( $args['display_to'] == 'not_logged_in' ) { |
| 588 |
return do_shortcode( $content ); |
| 589 |
} else { |
| 590 |
return $message; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
} |
| 595 |
add_shortcode( 'wppb-restrict', 'wppb_content_restriction_shortcode' ); |
| 596 |
|