PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.9.9
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.9.9
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / features / content-restriction / content-restriction-functions.php

content-restriction-functions.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.9.9, at features/content-restriction/content-restriction-functions.php

335 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /* Returns the restriction message added by the admin in the settings page or a default message if the first one is missing */
39 function wppb_get_restriction_content_message( $message_type = '', $post_id = 0 ) {
40
41 $wppb_content_restriction_settings = get_option( 'wppb_content_restriction_settings', 'not_found' );
42 $wppb_content_restriction_message = '';
43
44 if( $message_type == 'logged_out' ) {
45 $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' ) );
46 } elseif ( $message_type == 'logged_in' ) {
47 $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' ) );
48 } elseif ( $message_type == 'purchasing_restricted' ) {
49 $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' ) );
50 } else {
51 $wppb_content_restriction_message = apply_filters( 'wppb_get_restriction_content_message_default', $wppb_content_restriction_message, $message_type, $wppb_content_restriction_settings );
52 }
53
54 $custom_message_enabled = get_post_meta( $post_id, 'wppb-content-restrict-messages-enabled', true );
55
56 if( ! empty( $post_id ) && ! empty( $custom_message_enabled ) ) {
57 $custom_message = get_post_meta( $post_id, 'wppb-content-restrict-message-' . $message_type, true );
58
59 if( ! empty( $custom_message ) ) {
60 $wppb_content_restriction_message = $custom_message;
61 }
62 }
63
64 return wp_kses_post( $wppb_content_restriction_message );
65
66 }
67
68 /* Returns the restriction message with any tags processed */
69 function wppb_content_restriction_process_content_message( $type, $user_ID, $post_id = 0 ) {
70
71 $message = wppb_get_restriction_content_message( $type, $post_id );
72 $user_info = get_userdata( $user_ID );
73 $message = wppb_content_restriction_merge_tags( $message, $user_info, $post_id );
74
75 return '<span class="wppb-frontend-restriction-message wppb-content-restriction-message">'. $message .'</span>';
76
77 }
78
79 /* 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 */
80 function wppb_content_restriction_get_post_message( $post_id = 0 ) {
81
82 global $post, $user_ID, $wppb_show_content;
83
84 if( ! empty( $post_id ) ) {
85 $post = get_post( $post_id );
86 }
87
88 // If the $wppb_show_content global is different than false then the post is either not restricted or not processed for restriction
89 if( $wppb_show_content !== false ) {
90 return '';
91 }
92
93 if( ! is_user_logged_in() ) {
94 $message_type = 'logged_out';
95 } else {
96 $message_type = 'logged_in';
97 }
98
99 $message_type = apply_filters( 'wppb_get_restricted_post_message_type', $message_type );
100
101 $message = wppb_content_restriction_process_content_message( $message_type, $user_ID, $post->ID );
102
103 // Filter the restriction message before returning it
104 $message = apply_filters( 'wppb_restriction_message_' . $message_type, $message, $post->post_content, $post, $user_ID );
105
106 return do_shortcode( $message );
107
108 }
109
110 /* 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 */
111 function wppb_content_restriction_post_redirect() {
112 // try not to overwrite $post. Can have side-effects with other plugins.
113 global $post;
114 $woo_shop_or_post = $post;
115
116 if( function_exists( 'wc_get_page_id' ) ) {//redirect restriction for woocommerce shop page
117 if ( !is_singular() && !( is_post_type_archive('product') || is_page(wc_get_page_id('shop')) ) ){
118 return;
119 }
120
121 if( is_post_type_archive('product') || is_page(wc_get_page_id('shop')) ){
122 $woo_shop_or_post = get_post( wc_get_page_id('shop') );
123 }
124 }
125 else {
126 if (!is_singular()) {
127 return;
128 }
129 }
130
131 if ( !($woo_shop_or_post instanceof WP_Post) ){
132 return;
133 }
134
135 $woo_shop_or_post->ID = apply_filters( 'wppb_restricted_post_redirect_post_id', $woo_shop_or_post->ID );
136
137 $redirect_url = '';
138 $post_restriction_type = get_post_meta( $woo_shop_or_post->ID, 'wppb-content-restrict-type', true );
139 $settings = get_option( 'wppb_content_restriction_settings', array() );
140 $general_restriction_type = ( ! empty( $settings['restrict_type'] ) ? $settings['restrict_type'] : 'message' );
141
142 if( $post_restriction_type !== 'redirect' && $general_restriction_type !== 'redirect' ) {
143 return;
144 }
145
146 if( ! in_array( $post_restriction_type, array( 'default', 'redirect' ) ) ) {
147 return;
148 }
149
150 if( ! wppb_content_restriction_is_post_restricted( $woo_shop_or_post->ID ) ) {
151 return;
152 }
153
154 // Get the redirect URL from the post meta if enabled
155 if( $post_restriction_type === 'redirect' ) {
156 $post_redirect_url_enabled = get_post_meta( $woo_shop_or_post->ID, 'wppb-content-restrict-custom-redirect-url-enabled', true );
157 $post_redirect_url = get_post_meta( $woo_shop_or_post->ID, 'wppb-content-restrict-custom-redirect-url', true );
158
159 $redirect_url = ( ! empty( $post_redirect_url_enabled ) && ! empty( $post_redirect_url ) ? $post_redirect_url : '' );
160 }
161
162
163 // If the post doesn't have a custom redirect URL set, get the default from the Settings page
164 if( empty( $redirect_url ) ) {
165 $redirect_url = ( ! empty( $settings['redirect_url'] ) ? $settings['redirect_url'] : '' );
166 }
167
168 if( empty( $redirect_url ) ) {
169 return;
170 }
171
172 // To avoid a redirect loop we break in case the redirect URL is the same as the current page URL
173 $current_url = wppb_curpageurl();
174
175 if( $current_url == $redirect_url ) {
176 return;
177 }
178
179 // Pass the correct referer URL forward
180 $redirect_url = add_query_arg( array( 'wppb_referer_url' => urlencode( wppb_curpageurl() ) ), wppb_add_missing_http( $redirect_url ) );
181
182 // Redirect
183 nocache_headers();
184 wp_redirect( apply_filters( 'wppb_restricted_post_redirect_url', $redirect_url ) );
185 exit;
186
187 }
188 add_action( 'template_redirect', 'wppb_content_restriction_post_redirect' );
189
190 /* Function that searches and replaces merge tags with their values */
191 function wppb_content_restriction_merge_tags( $text, $user_info, $post_id = 0 ) {
192
193 $merge_tags = apply_filters( 'wppb_content_restriction_merge_tags', array( 'display_name', 'unrestricted_user_roles', 'current_user_role' ) );
194
195 if( ! empty( $merge_tags ) ) {
196 foreach( $merge_tags as $merge_tag ) {
197 $text = str_replace( '{{'. $merge_tag .'}}', apply_filters( 'wppb_content_restriction_merge_tag_'. $merge_tag, '', $user_info, $post_id ), $text );
198 }
199 }
200
201 return $text;
202
203 }
204
205 /* Add functionality for display_name tag */
206 function wppb_content_restriction_tag_display_name( $value, $user_info, $post_id = 0 ) {
207
208 if( ! empty( $user_info->display_name ) ) {
209 return $user_info->display_name;
210 } else if( ! empty( $user_info->user_login ) ) {
211 return $user_info->user_login;
212 } else {
213 return '';
214 }
215
216 }
217 add_filter( 'wppb_content_restriction_merge_tag_display_name', 'wppb_content_restriction_tag_display_name', 10, 3 );
218
219 /* Add functionality for unrestricted_user_roles tag */
220 function wppb_content_restriction_tag_unrestricted_user_roles( $value, $user_info, $post_id = 0 ) {
221
222 if( $post_id != 0 ) {
223 $unrestricted_user_roles = get_post_meta( $post_id, 'wppb-content-restrict-user-role' );
224
225 if( ! empty( $unrestricted_user_roles ) ) {
226 $user_roles = implode( ', ', $unrestricted_user_roles );
227
228 return $user_roles;
229 } else {
230 return '';
231 }
232 } else {
233 return '';
234 }
235
236 }
237 add_filter( 'wppb_content_restriction_merge_tag_unrestricted_user_roles', 'wppb_content_restriction_tag_unrestricted_user_roles', 10, 3 );
238
239 /* Add functionality for current_user_role tag */
240 function wppb_content_restriction_tag_current_user_role( $value, $user_info, $post_id = 0 ) {
241
242 if( ! empty( $user_info ) && ! empty( $user_info->roles ) ) {
243 $user_role = implode( ', ', $user_info->roles );
244
245 return $user_role;
246 } else {
247 return '';
248 }
249
250 }
251 add_filter( 'wppb_content_restriction_merge_tag_current_user_role', 'wppb_content_restriction_tag_current_user_role', 10, 3 );
252
253 /* Content restriction shortcode */
254 function wppb_content_restriction_shortcode( $atts, $content = null ) {
255
256 $args = shortcode_atts(
257 array(
258 'user_roles' => array(),
259 'display_to' => '',
260 'message' => '',
261 'users_id' => array()
262 ),
263 $atts
264 );
265
266 // Message to replace the content of checks do not match
267 if( ! empty( $args['message'] ) ) {
268 $message = '<span class="wppb-shortcode-restriction-message">' . $args['message'] . '</span>';
269 } else {
270 $type = ( is_user_logged_in() ? 'logged_in' : 'logged_out' );
271 $message = '<span class="wppb-content-restriction-message">' . wpautop( wppb_get_restriction_content_message( $type ) ) . '</span>';
272 }
273
274 /*
275 * Filter the message
276 *
277 * @param string $message - the current message, whether it is the default one from the settings or
278 * the one set in the shortcode attributes
279 * @param array $args - the shortcode attributes
280 *
281 */
282 $message = apply_filters( 'wppb_content_restriction_shortcode_message', $message, $args );
283
284 if( is_user_logged_in() ) {
285 // Show for administrators
286 if( current_user_can( 'manage_options' ) ) {
287 return do_shortcode( $content );
288 }
289
290 if( $args['display_to'] == 'not_logged_in' ) {
291 return $message;
292 }
293
294 if( ! empty($args['users_id'] ) ){
295 $users_id=array_map('trim', explode(',', $args['users_id']));
296 $current_user_id = get_current_user_id(); // the current id user
297 if( ! empty($current_user_id)){
298 if(in_array($current_user_id, $users_id))
299 {
300 return do_shortcode( $content );
301 }
302 else{
303 return $message;
304 }
305 }
306 }
307 elseif( ! empty( $args['user_roles'] ) ) {
308 $user_roles = array_map( 'trim', explode( ',', $args['user_roles'] ) );
309 $user_data = get_userdata( get_current_user_id() );
310
311 if( ! empty( $user_data->roles ) ) {
312 $common_user_roles = array_intersect( $user_roles, $user_data->roles );
313
314 if( ! empty( $common_user_roles ) ) {
315 return do_shortcode( $content );
316 } else {
317 return $message;
318 }
319 }
320 } else {
321
322 return do_shortcode( $content );
323 }
324
325 } else {
326 if( $args['display_to'] == 'not_logged_in' ) {
327 return do_shortcode( $content );
328 } else {
329 return $message;
330 }
331 }
332
333 }
334 add_shortcode( 'wppb-restrict', 'wppb_content_restriction_shortcode' );
335