PluginProbe
WP-Members Membership Plugin / trunk
WP-Members Membership Plugin vtrunk
trunk 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.4.2 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.4.9.1 3.4.9.2 3.4.9.3 3.4.9.4 3.4.9.5 3.4.9.6 3.4.9.7 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 All 34 releases
wp-members / includes / admin / class-wp-members-admin-posts.php

class-wp-members-admin-posts.php in WP-Members Membership Plugin trunk, at includes/admin/class-wp-members-admin-posts.php

506 lines 16.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The WP_Members Post Class.
4 *
5 * @package WP-Members
6 * @subpackage WP_Members Post Object Class
7 * @since 3.3.0
8 */
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit();
13 }
14
15 class WP_Members_Admin_Posts {
16
17 /**
18 * Function to add block/unblock to the bulk dropdown list.
19 *
20 * @since 2.9.2
21 * @since 3.3.0 Changed from wpmem_bulk_posts_action().
22 *
23 * @global object $wpmem The WP_Members object.
24 */
25 static function bulk_action() {
26 global $wpmem;
27 if ( ( isset( $_GET['post_type'] ) && ( 'page' == $_GET['post_type'] || 'post' == $_GET['post_type'] || array_key_exists( $_GET['post_type'], $wpmem->post_types ) ) ) || ! isset( $_GET['post_type'] ) ) { ?>
28 <script type="text/javascript">
29 jQuery(document).ready(function() {
30 jQuery('<option>').val('unblock').text('<?php _e( 'Unrestrict', 'wp-members' ) ?>').appendTo("select[name='action']");
31 jQuery('<option>').val('block').text('<?php _e( 'Restrict', 'wp-members' ) ?>').appendTo("select[name='action']");
32 jQuery('<option>').val('hide').text('<?php _e( 'Hide', 'wp-members' ) ?>').appendTo("select[name='action']");
33 jQuery('<option>').val('unblock').text('<?php _e( 'Unrestrict', 'wp-members' ) ?>').appendTo("select[name='action2']");
34 jQuery('<option>').val('block').text('<?php _e( 'Restrict', 'wp-members' ) ?>').appendTo("select[name='action2']");
35 jQuery('<option>').val('hide').text('<?php _e( 'Hide', 'wp-members' ) ?>').appendTo("select[name='action2']");
36 });
37 </script><?php
38 }
39 }
40
41
42 /**
43 * Function to handle bulk actions at page load.
44 *
45 * @since 2.9.2
46 * @since 3.3.0 Changed from wpmem_posts_page_load().
47 *
48 * @global object $wpmem The WP_Members object.
49 */
50 static function page_load() {
51
52 global $wpmem;
53
54 $wp_list_table = _get_list_table( 'WP_Posts_List_Table' );
55 $action = $wp_list_table->current_action();
56 $sendback = '';
57
58 switch ( $action ) {
59
60 case ( 'unblock' ):
61 case ( 'block' ):
62 case ( 'hide' ):
63 // Validate nonce.
64 check_admin_referer( 'bulk-posts' );
65 // Get the posts.
66 $posts = wpmem_get( 'post', '', 'request' );
67 // Convert action.
68 $status = ( 'hide' == $action ) ? 2 : ( ( 'block' == $action ) ? 1 : 0 );
69 // Update posts.
70 $x = '';
71 if ( $posts ) {
72 foreach ( $posts as $post_id ) {
73 // Keep a count of posts updated.
74 $x++;
75 // Make sure $post_id is just an integer.
76 $post_id = (int)$post_id;
77 // Get the post type.
78 $post = get_post( $post_id );
79 $type = $post->post_type;
80 // Update accordingly.
81 self::set_block_status( $status, $post_id, $post->post_type );
82 }
83 // Set the return message.
84 $arr = array(
85 'a' => 'updated',
86 'n' => $x,
87 'post_type' => $type,
88 );
89 if ( isset( $_GET['post_status'] ) && 'all' != $_GET['post_status'] ) {
90 $arr['post_status'] = sanitize_text_field( $_GET['post_status'] );
91 }
92
93 $sendback = add_query_arg( array( $arr ), '', $sendback );
94
95 } else {
96 // Set the return message.
97 $sendback = add_query_arg( array( 'a' => 'none' ), '', $sendback );
98 }
99 break;
100
101 default:
102 return;
103
104 }
105
106 // If we did not return already, we need to wp_safe_redirect.
107 wp_safe_redirect( esc_url_raw( $sendback ) );
108 exit();
109 }
110
111
112 /**
113 * Function to echo admin update message.
114 *
115 * @since 2.8.2
116 * @since 3.3.0 Changed from wpmem_posts_admin_notices().
117 *
118 * @global $pagenow
119 * @global $post_type
120 */
121 static function notices() {
122
123 global $pagenow, $post_type;
124 if ( $pagenow == 'edit.php' && isset( $_REQUEST['a'] ) ) {
125 $msg = ( $_REQUEST['a'] == 'block' ) ? sprintf( esc_html__( '%s restricted', 'wp-members' ), $post_type ) : sprintf( esc_html__( '%s unrestricted', 'wp-members' ), $post_type );
126 echo '<div class="updated"><p>' . esc_html( $_REQUEST['n'] ) . ' ' . esc_html( $msg ) . '</p></div>';
127 }
128 }
129
130
131 /**
132 * Adds the blocking meta boxes for post and page editor screens.
133 *
134 * @since 2.8.0
135 * @since 3.3.0 Changed from wpmem_block_meta_add().
136 *
137 * @global object $wp_post_types The Post Type object.
138 * @global object $wpmem The WP-Members object.
139 */
140 static function block_meta_add() {
141
142 global $wp_post_types, $wpmem;
143
144 // Build an array of post types
145 $post_arr = array(
146 'post' => 'Posts',
147 'page' => 'Pages',
148 );
149 if ( isset( $wpmem->post_types ) ) {
150 foreach ( $wpmem->post_types as $key => $val ) {
151 $post_arr[ $key ] = $val;
152 }
153 }
154
155 foreach ( $post_arr as $key => $val ) {
156 if ( isset( $wp_post_types[ $key ] ) ) {
157 $post_type = $wp_post_types[ $key ];
158 /**
159 * Filter the post meta box title.
160 *
161 * @since 2.9.0
162 *
163 * @param Post restriction title.
164 */
165 $post_title = apply_filters( 'wpmem_admin_' . $key . '_meta_title', sprintf( esc_html__( '%s Restriction', 'wp-members' ), $post_type->labels->singular_name ) );
166
167 add_meta_box( 'wpmem-block-meta-id', $post_title, array( 'WP_Members_Admin_Posts', 'block_meta' ), $key, 'side', 'high'
168 // ,array( '__back_compat_meta_box' => true, ) // @todo Convert to Block and declare this for backwards compat ONLY!
169 );
170 }
171 }
172 }
173
174
175 /**
176 * Builds the meta boxes for post and page editor screens.
177 *
178 * @since 2.8.0
179 * @since 3.3.0 Changed from wpmem_block_meta().
180 *
181 * @global object $post The WordPress post object.
182 * @global object $wp_post_types The Post Type object.
183 * @global object $wpmem The WP-Members object.
184 */
185 static function block_meta() {
186
187 global $post, $wp_post_types, $wpmem;
188
189 wp_nonce_field( 'wpmem_block_meta_nonce', 'wpmem_block_meta_nonce' );
190
191 $post_type = $wp_post_types[ $post->post_type ];
192 $post_meta_value = get_post_meta( $post->ID, '_wpmem_block', true );
193 $post_meta_value = ( null == $post_meta_value ) ? $wpmem->block[ $post->post_type ] : $post_meta_value;
194 $post_meta_settings = array(
195 '0' => array( 'text' => esc_html__( 'Unrestricted', 'wp-members' ), 'icon' => '<span id="wpmem_post_icon_0" class="dashicons dashicons-unlock" ' . ( ( 0 != $post_meta_value ) ? 'style="display:none;"' : '' ) . '></span>' ),
196 '1' => array( 'text' => esc_html__( 'Restricted', 'wp-members' ), 'icon' => '<span id="wpmem_post_icon_1" class="dashicons dashicons-lock" ' . ( ( 1 != $post_meta_value ) ? 'style="display:none;"' : '' ) . '></span>' ),
197 '2' => array( 'text' => esc_html__( 'Hidden', 'wp-members' ), 'icon' => '<span id="wpmem_post_icon_2" class="dashicons dashicons-hidden" ' . ( ( 2 != $post_meta_value ) ? 'style="display:none;"' : '' ) . '></span>' ),
198 ); ?>
199 <p><?php
200 foreach ( $post_meta_settings as $key => $value ) {
201 echo $value['icon'];
202 } ?> <?php
203 _e( 'Status:', 'wp-members' ); ?> <span id="wpmem_post_block_status"><?php echo $post_meta_settings[ $post_meta_value ]['text']; ?></span> <a href="#" class="hide-if-no-js" id="wpmem_edit_block_status"><?php _e( 'Edit' ); ?></a>
204 </p>
205 <p>
206 <div id="wpmem_block">
207 <?php
208 $original_value = ''; $original_label = '';
209 foreach ( $post_meta_settings as $key => $value ) {
210 $original_value = ( $post_meta_value == $key ) ? $key : $original_value;
211 $original_label = ( $post_meta_value == $key ) ? $value['text'] : $original_label;
212 echo '<input type="radio" id="wpmem_block_status_' . $key . '" name="wpmem_block" value="' . $key . '" ' . checked( $post_meta_value, $key, false ) . ' /><label>' . $value['text'] . '</label><br />';
213 }
214 echo '<input type="hidden" id="wpmem_block_original_value" name="wpmem_block_original_value" value="' . $original_value . '" />';
215 echo '<input type="hidden" id="wpmem_block_original_label" name="wpmem_block_original_label" value="' . $original_label . '" />';
216 ?>
217 <p>
218 <a href="#" class="hide-if-no-js button" id="wpmem_ok_block_status"><?php echo _e( 'Ok' ); ?></a>
219 <!--<a href="#" class="hide-if-no-js" id="wpmem_cancel_block_status"><?php _e( 'Cancel' ); ?></a>--><?php // @todo Cannot use this until js is updated to set the radio group back to the original value (otherwise it's confusing for the user). ?>
220 </p>
221 </div>
222 </p>
223 <?php
224 /**
225 * Fires after the post block meta box.
226 *
227 * Allows actions at the end of the block meta box on pages and posts.
228 *
229 * @since 2.8.8
230 * @since 3.2.0 Changed to $post_meta_value (same as $block).
231 *
232 * @param $post object The WP Post Object.
233 * @param post_meta_value string The WP-Members block value: 0|1|2 for unblock|block|hide.
234 */
235 do_action( 'wpmem_admin_after_block_meta', $post, $post_meta_value );
236 }
237
238
239 /**
240 * Saves the meta boxes data for post and page editor screens.
241 *
242 * @since 2.8.0
243 * @since 3.3.0 Change from wpmem_block_meta_save().
244 *
245 * @global object $post
246 * @global object $wpmem
247 * @param int $post_id The post ID
248 */
249 static function block_meta_save( $post_id ) {
250
251 global $post, $wpmem;
252
253 // Quit if we are doing autosave.
254 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
255 return;
256 }
257 // Quit if the nonce isn't there, or is wrong.
258 if ( ! isset( $_POST['wpmem_block_meta_nonce'] ) || ! wp_verify_nonce( $_POST['wpmem_block_meta_nonce'], 'wpmem_block_meta_nonce' ) ) {
259 return;
260 }
261 // Quit if it's a post revision
262 if ( false !== wp_is_post_revision( $post_id ) ) {
263 return;
264 }
265 // Quit if the current user cannot edit posts.
266 if ( ! current_user_can( 'edit_posts' ) ) {
267 return;
268 }
269
270 // Get value.
271 $block = ( isset( $_POST['wpmem_block'] ) ) ? sanitize_text_field( $_POST['wpmem_block'] ) : null;
272
273 // Set the value.
274 self::set_block_status( $block, $post_id, $post->post_type );
275
276 /**
277 * Fires after the post block meta box is saved.
278 *
279 * Allows actions to be hooked to the meta save process.
280 *
281 * @since 2.8.8
282 *
283 * @param $post object The WP Post Object.
284 * @param $block boolean The WP-Members block value.
285 */
286 do_action( 'wpmem_admin_block_meta_save', $post, $block, '' );
287 }
288
289
290 /**
291 * Adds WP-Members blocking status to Posts Table columns.
292 *
293 * @since 2.8.3
294 * @since 3.3.0 Changed from wpmem_post_columns().
295 *
296 * @global object $wpmem The WP-Members Object
297 * @param array $columns The array of table columns.
298 * @return array $columns
299 */
300 static function columns( $columns ) {
301 global $wpmem;
302 $post_type = ( isset( $_REQUEST['post_type'] ) ) ? sanitize_text_field( $_REQUEST['post_type'] ) : 'post';
303
304 if ( $post_type == 'page' || $post_type == 'post' || array_key_exists( $post_type, $wpmem->post_types ) ) {
305 $columns['wpmem_block'] = esc_html__( 'Status', 'wp-members' );
306 }
307 return $columns;
308 }
309
310
311 /**
312 * Adds blocking status to the Post Table column.
313 *
314 * @since 2.8.3
315 * @since 3.3.0 Changed from wpmem_post_columns_content().
316 *
317 * @global object $wpmem The WP_Members Object.
318 * @param string $column_name
319 * @param int $post_ID
320 */
321 static function columns_content( $column_name, $post_ID ) {
322
323 global $wpmem;
324 $post_type = sanitize_text_field( wpmem_get( 'post_type', 'post', 'request' ) );
325
326 if ( $column_name == 'wpmem_block' ) {
327
328 $block_meta = get_post_meta( $post_ID, '_wpmem_block', true );
329
330 // Backward compatibility for old block/unblock meta.
331 if ( ! $block_meta ) {
332 // Check for old meta.
333 $old_block = get_post_meta( $post_ID, 'block', true );
334 $old_unblock = get_post_meta( $post_ID, 'unblock', true );
335 $block_meta = ( $old_block ) ? 1 : ( ( $old_unblock ) ? 0 : $block_meta );
336 }
337
338 if ( $wpmem->block[ $post_type ] == 1 ) {
339 $block_span = array( 'lock', 'green', esc_html__( 'Restricted', 'wp-members' ) );
340 }
341 if ( $wpmem->block[ $post_type ] == 0 ) {
342 $block_span = array( 'unlock', 'red', esc_html__( 'Unrestricted', 'wp-members' ) );
343 }
344 if ( $wpmem->block[ $post_type ] == 1 && $block_meta == '0' ) {
345 $block_span = array( 'unlock', 'red', esc_html__( 'Unrestricted', 'wp-members' ) );
346 } elseif ( $wpmem->block[ $post_type ] == 0 && $block_meta == '1' ) {
347 $block_span = array( 'lock', 'green', esc_html__( 'Restricted', 'wp-members' ) );
348 } elseif ( 2 == $block_meta ) {
349 $block_span = array( 'hidden', '', esc_html__( 'Hidden', 'wp-members' ) );
350 }
351 echo '<span class="dashicons dashicons-' . $block_span[0] . '" style="color:' . $block_span[1] . '" title="' . $block_span[2] . '"></span>';
352 }
353 }
354
355 /**
356 * Adds filter option for Posts > All Posts to filter by restriction.
357 *
358 * @since 3.5.5
359 */
360 static function filter_by_restriction() {
361
362 global $typenow, $wpmem;
363
364 // Only do this for managed post types.
365 if ( 'post' == $typenow || 'page' == $typenow || array_key_exists( $typenow, $wpmem->post_types ) ) {
366
367 $restrictions = array(
368 'unrestricted' => __( 'Unrestricted', 'wp-members' ),
369 'restricted' => __( 'Restricted', 'wp-members' ),
370 'hidden' => __( 'Hidden', 'wp-members' ),
371 );
372
373 $sanitized_value = wpmem_get_sanitized( 'restriction_filter', '', 'get' );
374 echo '<select name="restriction_filter">';
375 echo '<option value="">' . esc_html__( 'Filter by restriction', 'wp-members' ) . '</option>';
376 foreach ( $restrictions as $restriction_meta => $restriction_vals ) {
377 printf(
378 '<option value="%s"%s>%s</option>',
379 esc_attr( $restriction_meta ),
380 $restriction_meta == $sanitized_value ? ' selected="selected"' : '',
381 esc_html( $restriction_vals )
382 );
383 }
384 echo '</select>';
385 }
386 }
387
388 /**
389 * Filters the Posts > All Posts view by restriction.
390 *
391 * @since 3.5.5
392 */
393 static function restriction_filter( $query ) {
394 global $pagenow, $wpmem;
395
396 // What post type is being handled here?
397 $post_type = wpmem_get_sanitized( 'post_type', false, 'get' );
398 // Get the restriction filter value.
399 $restriction_filter = wpmem_get_sanitized( 'restriction_filter', false, 'get' );
400
401 if ( is_admin() && $pagenow == 'edit.php' && $restriction_filter ) {
402
403 // Switch based on value.
404 switch ( $restriction_filter ) {
405 case 'unrestricted':
406 if ( 0 == $wpmem->block[ $post_type ] ) {
407 $meta_query[] = array(
408 'relation' => 'OR',
409 array(
410 'key' => '_wpmem_block',
411 'compare' => 'NOT EXISTS',
412 ),
413 array(
414 'key' => '_wpmem_block',
415 'value' => 0,
416 'compare' => '='
417 )
418 );
419 $query->set( 'meta_query', $meta_query );
420 } else {
421 $query->query_vars['meta_key'] = '_wpmem_block';
422 $query->query_vars['meta_value'] = 0;
423 }
424 break;
425 case 'restricted':
426 if ( 1 == $wpmem->block[ $post_type ] ) {
427 $meta_query[] = array(
428 'relation' => 'OR',
429 array(
430 'key' => '_wpmem_block',
431 'compare' => 'NOT EXISTS',
432 ),
433 array(
434 'key' => '_wpmem_block',
435 'value' => 1,
436 'compare' => '='
437 )
438 );
439 $query->set( 'meta_query', $meta_query );
440 } else {
441 $query->query_vars['meta_key'] = '_wpmem_block';
442 $query->query_vars['meta_value'] = 1;
443 }
444 break;
445 case 'hidden':
446 $query->query_vars['meta_key'] = '_wpmem_block';
447 $query->query_vars['meta_value'] = 2;
448 break;
449 }
450 }
451
452 //rktgk_what_is( $query, true );
453 }
454
455 /**
456 * Sets custom block status for a post.
457 *
458 * @since 3.2.0
459 * @since 3.3.0 Changed from wpmem_set_block_status().
460 *
461 * @global object $wpmem The WP_Members object class.
462 * @param int $status 0|1|2 for unblock|block|hide
463 * @param int $post_id The post ID to set a meta value.
464 * @param string $post_type The post type.
465 */
466 static function set_block_status( $status, $post_id, $post_type ) {
467 global $wpmem;
468
469 // Previous value.
470 $prev_value = get_post_meta( $post_id, '_wpmem_block', true );
471
472 // Update accordingly.
473 if ( false !== $prev_value && $status != $prev_value ) {
474 if ( $status == $wpmem->block[ $post_type ] ) {
475 delete_post_meta( $post_id, '_wpmem_block' );
476 } else {
477 update_post_meta( $post_id, '_wpmem_block', $status );
478 }
479 } elseif ( ! $prev_value && $status != $wpmem->block[ $post_type ] ) {
480 update_post_meta( $post_id, '_wpmem_block', $status );
481 } elseif ( $status != $prev_value ) {
482 delete_post_meta( $post_id, '_wpmem_block' );
483 }
484
485 // If the value is to hide, delete the transient so that it updates.
486 if ( 2 == $status || ( 2 == $prev_value && $status != $prev_value ) ) {
487 $wpmem->update_hidden_posts();
488 }
489 }
490
491 /**
492 * Adds shortcode dropdown to post editor tinymce.
493 *
494 * @since 3.0
495 * @since 3.3.2 Added to posts class as static
496 *
497 * @global object $wpmem_shortcode The WP_Members_TinyMCE_Buttons object.
498 */
499 static function load_tinymce() {
500 if ( version_compare( get_bloginfo( 'version' ), '3.9', '>=' ) ) {
501 global $wpmem, $wpmem_shortcode;
502 include( $wpmem->path . 'includes/admin/class-wp-members-tinymce-buttons.php' );
503 $wpmem_shortcode = new WP_Members_TinyMCE_Buttons;
504 }
505 }
506 }