Skip to content

Commit 3834b77

Browse files
committed
Feature: added post lock feature based on posts and user
1 parent 9269ba1 commit 3834b77

7 files changed

+222
-7
lines changed

admin/class-admin-settings.php

+6
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,12 @@ public function enqueue_styles() {
401401

402402
wp_enqueue_style( 'wpuf-admin', WPUF_ASSET_URI . '/css/admin.css', false, WPUF_VERSION );
403403
wp_enqueue_script( 'wpuf-admin-script', WPUF_ASSET_URI . '/js/wpuf-admin.js', array( 'jquery' ), false, WPUF_VERSION );
404+
405+
wp_localize_script( 'wpuf-admin-script', 'wpuf_admin_script', array(
406+
'ajaxurl' => admin_url( 'admin-ajax.php' ),
407+
'nonce' => wp_create_nonce( 'wpuf_nonce' ),
408+
'cleared_schedule_lock' => __( 'Schedule lock has been cleared', 'wp-user-frontend' ),
409+
) );
404410
}
405411

406412
}

admin/html/form-settings-post-edit.php

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
$url = isset( $form_settings['edit_url'] ) ? $form_settings['edit_url'] : '';
77
$update_text = isset( $form_settings['update_text'] ) ? $form_settings['update_text'] : __( 'Update', 'wp-user-frontend' );
88
$subscription = isset( $form_settings['subscription'] ) ? $form_settings['subscription'] : null;
9+
$lock_edit_post = isset( $form_settings['lock_edit_post'] ) ? $form_settings['lock_edit_post'] : '';
910
?>
1011
<table class="form-table">
1112

@@ -93,4 +94,15 @@
9394
<input type="text" name="wpuf_settings[update_text]" value="<?php echo esc_attr( $update_text ); ?>">
9495
</td>
9596
</tr>
97+
98+
<tr class="lock-edit-post">
99+
<th><?php _e( 'Lock User From Editing After', 'wp-user-frontend' ); ?></th>
100+
<td>
101+
<input type="number" name="wpuf_settings[lock_edit_post]" id="lock_edit_post" value="<?php echo $lock_edit_post; ?>">
102+
<span><?php _e( 'hours', 'wp-user-frontend' ); ?></span>
103+
<p class="description">
104+
<?php _e( 'After how many hours user will be locked from editing the submitted post.', 'wp-user-frontend' ) ?>
105+
</p>
106+
</td>
107+
</tr>
96108
</table>

admin/posting.php

+106-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ function __construct() {
1616
// meta boxes
1717
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes') );
1818
add_action( 'add_meta_boxes', array( $this, 'add_meta_box_form_select') );
19-
19+
add_action( 'add_meta_boxes', array( $this, 'add_meta_box_post_lock') );
2020
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script') );
21-
2221
add_action( 'save_post', array( $this, 'save_meta'), 1, 2 ); // save the custom fields
2322
add_action( 'save_post', array( $this, 'form_selection_metabox_save' ), 1, 2 ); // save edit form id
23+
add_action( 'save_post', array( $this, 'post_lock_metabox_save' ), 1, 2 ); // save post lock option
24+
add_action( 'wp_ajax_wpuf_clear_schedule_lock', array($this, 'clear_schedule_lock') );
2425
}
2526

2627
public static function init() {
@@ -101,7 +102,6 @@ function add_meta_box_form_select() {
101102
}
102103
}
103104

104-
105105
/**
106106
* Form selection meta box in post types
107107
*
@@ -127,7 +127,7 @@ function form_selection_metabox() {
127127
<?php } ?>
128128
</select>
129129
<div>
130-
<p><a href="https://wedevs.com/docs/wp-user-frontend-pro/tutorials/purpose-of-the-wpuf-form-metabox/" target="_blank"><?php _e( 'Purpose of this metabox', 'wp-user-frontend' ); ?></a></p>
130+
<p><a href="https://wedevs.com/docs/wp-user-frontend-pro/tutorials/purpose-of-the-wpuf-form-metabox/" target="_blank"><?php _e( 'Learn more', 'wp-user-frontend' ); ?></a></p>
131131
</div>
132132
<?php
133133
}
@@ -158,6 +158,89 @@ function form_selection_metabox_save( $post_id, $post ) {
158158
update_post_meta( $post->ID, '_wpuf_form_id', $_POST['wpuf_form_select'] );
159159
}
160160

161+
/**
162+
* Meta box for post lock
163+
*
164+
* Registers a meta box in public post types to select the desired WPUF
165+
* form select box to assign a form id.
166+
*
167+
* @since 3.0.2
168+
*
169+
* @return void
170+
*/
171+
function add_meta_box_post_lock() {
172+
$post_types = get_post_types( array('public' => true) );
173+
174+
foreach ($post_types as $post_type) {
175+
add_meta_box( 'wpuf-post-lock', __('WPUF Lock User', 'wp-user-frontend'), array($this, 'post_lock_metabox'), $post_type, 'side', 'high' );
176+
}
177+
}
178+
179+
/**
180+
* Post lock meta box in post types
181+
*
182+
* Registered via $this->add_meta_box_post_lock()
183+
*
184+
* @since 3.0.2
185+
*
186+
* @global object $post
187+
*/
188+
function post_lock_metabox() {
189+
global $post;
190+
191+
$msg = '';
192+
$edit_post_lock = get_post_meta( $post->ID, '_wpuf_lock_editing_post', true );
193+
$edit_post_lock_time = get_post_meta( $post->ID, '_wpuf_lock_user_editing_post_time', true );
194+
195+
if( !empty( $edit_post_lock_time ) && $edit_post_lock_time > time() ) {
196+
$time = date( 'Y-m-d H:i:s', $edit_post_lock_time );
197+
$local_time = get_date_from_gmt( $time, get_option('date_format') . ' ' . get_option('time_format') );
198+
$msg = sprintf( __( 'Frontend edit access for this post will be automatically locked after %s, <a id="wpuf_clear_schedule_lock" data="%s" href="#">Clear Schedule Lock</a> Or,', 'wp-user-frontend' ), $local_time, $post->ID );
199+
}
200+
201+
?>
202+
203+
<input type="hidden" name="wpuf_lock_editing_post_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
204+
205+
<p><?php echo $msg; ?></p>
206+
207+
<label>
208+
<input type="hidden" name="wpuf_lock_post" value="no">
209+
<input type="checkbox" name="wpuf_lock_post" value="yes" <?php checked($edit_post_lock, 'yes'); ?>>
210+
<?php _e( 'Lock Post', 'wp-user-frontend' ); ?>
211+
</label>
212+
<p style="margin-top: 10px"><?php _e( 'Lock user from editing this post from the frontend dashboard', 'wp-user-frontend' ); ?></p>
213+
<?php
214+
}
215+
216+
/**
217+
* Save the lock post option
218+
*
219+
* @since 3.0.2
220+
*
221+
* @param int $post_id
222+
* @param object $post
223+
* @return int|void
224+
*/
225+
function post_lock_metabox_save( $post_id, $post ) {
226+
$edit_post_lock_time = isset( $_POST['_wpuf_lock_user_editing_post_time'] ) ? $_POST['_wpuf_lock_user_editing_post_time'] : '';
227+
228+
if ( !isset($_POST['wpuf_lock_post'])) {
229+
return $post->ID;
230+
}
231+
232+
if ( !wp_verify_nonce( $_POST['wpuf_lock_editing_post_nonce'], plugin_basename( __FILE__ ) ) ) {
233+
return $post->ID;
234+
}
235+
236+
// Is the user allowed to edit the post or page?
237+
if ( !current_user_can( 'edit_post', $post->ID ) ) {
238+
return $post->ID;
239+
}
240+
241+
update_post_meta( $post->ID, '_wpuf_lock_editing_post', $_POST['wpuf_lock_post'] );
242+
}
243+
161244
/**
162245
* Meta box to show WPUF Custom Fields
163246
*
@@ -430,4 +513,23 @@ function save_meta( $post_id, $post = null ) {
430513
WPUF_Frontend_Form_Post::update_post_meta( $meta_vars, $post_id );
431514
}
432515

516+
/**
517+
* Clear Schedule lock
518+
*
519+
* @since 3.0.2
520+
*/
521+
public function clear_schedule_lock() {
522+
check_ajax_referer( 'wpuf_nonce', 'nonce' );
523+
524+
$post_id = isset( $_POST['post_id'] ) ? $_POST['post_id'] : '';
525+
if ( !empty( $post_id ) ) {
526+
$edit_post_lock_time = get_post_meta( $post_id, '_wpuf_lock_user_editing_post_time', true );
527+
528+
if ( !empty( $edit_post_lock_time ) ) {
529+
update_post_meta( $post_id, '_wpuf_lock_user_editing_post_time', '' );
530+
}
531+
}
532+
exit;
533+
}
534+
433535
}

assets/js/wpuf-admin.js

+28-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,33 @@ jQuery(function($) {
2626
$(this.children[0]).attr("checked", "checked");
2727
$(".wpuf-form-layouts li").removeClass('active');
2828
$(this).toggleClass('active');
29-
})
29+
});
30+
31+
// Clear schedule lock
32+
$('#wpuf_clear_schedule_lock').on('click', function(e) {
33+
e.preventDefault();
34+
var post_id = $(this).attr('data');
3035

36+
$.ajax({
37+
url: wpuf_admin_script.ajaxurl,
38+
type: 'POST',
39+
data: {
40+
'action' : 'wpuf_clear_schedule_lock',
41+
'nonce' : wpuf_admin_script.nonce,
42+
'post_id' : post_id
43+
},
44+
success:function(data) {
45+
swal({
46+
type: 'success',
47+
title: wpuf_admin_script.cleared_schedule_lock,
48+
showConfirmButton: false,
49+
timer: 1500
50+
});
51+
},
52+
error: function(errorThrown){
53+
console.log(errorThrown);
54+
}
55+
});
56+
$(this).closest("p").hide();
57+
});
3158
});

class/frontend-form-post.php

+29
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,24 @@ function edit_post_shortcode( $atts ) {
179179
return '<div class="wpuf-info">' . __( 'Invalid post', 'wp-user-frontend' ) . '</div>';
180180
}
181181

182+
$edit_post_lock = get_post_meta( $post_id, '_wpuf_lock_editing_post', true );
183+
$edit_post_lock_time = get_post_meta( $post_id, '_wpuf_lock_user_editing_post_time', true );
184+
185+
if ( $edit_post_lock == 'yes' ) {
186+
return '<div class="wpuf-info">' . apply_filters( 'wpuf_edit_post_lock_user_notice', __( 'Your edit access for this post has been locked by an administrator.', 'wp-user-frontend' ) ) . '</div>';
187+
}
188+
189+
if ( !empty( $edit_post_lock_time ) && $edit_post_lock_time < time() ) {
190+
return '<div class="wpuf-info">' . apply_filters( 'wpuf_edit_post_lock_expire_notice', __( 'Your allocated time for editing this post has been expired.', 'wp-user-frontend' ) ) . '</div>';
191+
}
192+
193+
if ( wpuf_get_user()->edit_post_locked() ) {
194+
if ( !empty( wpuf_get_user()->edit_post_lock_reason() ) ) {
195+
return '<div class="wpuf-info">' . wpuf_get_user()->edit_post_lock_reason() . '</div>';
196+
}
197+
return '<div class="wpuf-info">' . apply_filters( 'wpuf_user_edit_post_lock_notice', __( 'Your post edit access has been locked by an administrator.', 'wp-user-frontend' ) ) . '</div>';
198+
}
199+
182200
//is editing enabled?
183201
if ( wpuf_get_option( 'enable_post_edit', 'wpuf_dashboard', 'yes' ) != 'yes' ) {
184202
return '<div class="wpuf-info">' . __( 'Post Editing is disabled', 'wp-user-frontend' ) . '</div>';
@@ -491,6 +509,17 @@ function submit_post() {
491509

492510
$post_id = wp_insert_post( $postarr );
493511

512+
// add _wpuf_lock_editing_post_time meta to
513+
// lock user from editing the published post after a certain time
514+
if ( !$is_update ) {
515+
$lock_edit_post = isset( $form_settings['lock_edit_post'] ) ? floatval( $form_settings['lock_edit_post'] ) : 0;
516+
517+
if ( $post_id && $lock_edit_post > 0 ) {
518+
$lock_edit_post_time = time() + ( $lock_edit_post * 60 * 60 );
519+
update_post_meta( $post_id, '_wpuf_lock_user_editing_post_time', $lock_edit_post_time );
520+
}
521+
}
522+
494523
if ( $post_id ) {
495524

496525
self::update_post_meta( $meta_vars, $post_id );

includes/class-user.php

+18
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ public function lock_reason() {
6161
return get_user_meta( $this->id, 'wpuf_lock_cause', true );
6262
}
6363

64+
/**
65+
* Check if a user's post edit capability is locked
66+
*
67+
* @return boolean
68+
*/
69+
public function edit_post_locked() {
70+
return 'yes' == get_user_meta( $this->id, 'wpuf_edit_postlock', true );
71+
}
72+
73+
/**
74+
* Get the edit post lock reason
75+
*
76+
* @return string
77+
*/
78+
public function edit_post_lock_reason() {
79+
return get_user_meta( $this->id, 'wpuf_edit_post_lock_cause', true );
80+
}
81+
6482
/**
6583
* Handles user subscription
6684
*

includes/free/edit-profile.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,12 @@ function post_lock_form( $profileuser ) {
241241
$wpuf_subscription = $current_user->subscription();
242242
$post_locked = $current_user->post_locked();
243243
$lock_reason = $current_user->lock_reason();
244+
$edit_post_locked = $current_user->edit_post_locked();
245+
$edit_lock_reason = $current_user->edit_post_lock_reason();
244246

245247
if ( is_admin() && current_user_can( 'edit_users' ) ) {
246-
$select = ( $post_locked == true ) ? 'yes' : 'no';
248+
$select = ( $post_locked == true ) ? 'yes' : 'no';
249+
$edit_post_select = ( $edit_post_locked == true ) ? 'yes' : 'no';
247250
?>
248251
<div class="wpuf-user-post-lock">
249252
<h3><?php _e( 'WPUF Post Lock', 'wp-user-frontend' ); ?></h3>
@@ -258,13 +261,29 @@ function post_lock_form( $profileuser ) {
258261
<span class="description"><?php _e( 'Lock user from creating new post.', 'wp-user-frontend' ); ?></span></em>
259262
</td>
260263
</tr>
261-
262264
<tr>
263265
<th><label for="post-lock"><?php _e( 'Lock Reason:', 'wp-user-frontend' ); ?> </label></th>
264266
<td>
265267
<input type="text" name="wpuf_lock_cause" id="wpuf_lock_cause" class="regular-text" value="<?php echo esc_attr( $lock_reason ); ?>" />
266268
</td>
267269
</tr>
270+
271+
<tr>
272+
<th><label for="post-lock"><?php _e( 'Lock Edit Post:', 'wp-user-frontend' ); ?> </label></th>
273+
<td>
274+
<select name="wpuf_edit_postlock" id="edit-post-lock">
275+
<option value="no"<?php selected( $edit_post_select, 'no' ); ?>>No</option>
276+
<option value="yes"<?php selected( $edit_post_select, 'yes' ); ?>>Yes</option>
277+
</select>
278+
<span class="description"><?php _e( 'Lock user from editing post.', 'wp-user-frontend' ); ?></span></em>
279+
</td>
280+
</tr>
281+
<tr>
282+
<th><label for="post-lock"><?php _e( 'Edit Post Lock Reason:', 'wp-user-frontend' ); ?> </label></th>
283+
<td>
284+
<input type="text" name="wpuf_edit_post_lock_cause" id="wpuf_edit_post_lock_cause" class="regular-text" value="<?php echo esc_attr( $edit_lock_reason ); ?>" />
285+
</td>
286+
</tr>
268287
</table>
269288
</div>
270289
<?php
@@ -280,6 +299,8 @@ function post_lock_update( $user_id ) {
280299
if ( is_admin() && current_user_can( 'edit_users' ) ) {
281300
update_user_meta( $user_id, 'wpuf_postlock', $_POST['wpuf_postlock'] );
282301
update_user_meta( $user_id, 'wpuf_lock_cause', $_POST['wpuf_lock_cause'] );
302+
update_user_meta( $user_id, 'wpuf_edit_postlock', $_POST['wpuf_edit_postlock'] );
303+
update_user_meta( $user_id, 'wpuf_edit_post_lock_cause', $_POST['wpuf_edit_post_lock_cause'] );
283304
}
284305
}
285306

0 commit comments

Comments
 (0)