forked from weDevsOfficial/wp-user-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpuf-edit-post.php
executable file
·352 lines (289 loc) · 15.2 KB
/
wpuf-edit-post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
class WPUF_Edit_Post {
function __construct() {
add_shortcode( 'wpuf_edit', array($this, 'shortcode') );
}
/**
* Handles the edit post shortcode
*
* @return string generated form by the plugin
*/
function shortcode() {
ob_start();
if ( is_user_logged_in() ) {
$this->prepare_form();
} else {
printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( '', false ) );
}
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Main edit post form
*
* @global type $wpdb
* @global type $userdata
*/
function prepare_form() {
global $wpdb, $userdata;
$post_id = isset( $_GET['pid'] ) ? intval( $_GET['pid'] ) : 0;
//is editing enabled?
if ( wpuf_get_option( 'enable_post_edit', 'wpuf_others', 'yes' ) != 'yes' ) {
return __( 'Post Editing is disabled', 'wpuf' );
}
$curpost = get_post( $post_id );
if ( !$curpost ) {
return __( 'Invalid post', 'wpuf' );
}
//has permission?
if ( !current_user_can( 'delete_others_posts' ) && ( $userdata->ID != $curpost->post_author ) ) {
return __( 'You are not allowed to edit', 'wpuf' );
}
//perform delete attachment action
if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == "del" ) {
check_admin_referer( 'wpuf_attach_del' );
$attach_id = intval( $_REQUEST['attach_id'] );
if ( $attach_id ) {
wp_delete_attachment( $attach_id );
}
}
//process post
if ( isset( $_POST['wpuf_edit_post_submit'] ) && wp_verify_nonce( $_REQUEST['_wpnonce'], 'wpuf-edit-post' ) ) {
$this->submit_post();
$curpost = get_post( $post_id );
}
//show post form
$this->edit_form( $curpost );
}
function edit_form( $curpost ) {
$post_tags = wp_get_post_tags( $curpost->ID );
$tagsarray = array();
foreach ($post_tags as $tag) {
$tagsarray[] = $tag->name;
}
$tagslist = implode( ', ', $tagsarray );
$categories = get_the_category( $curpost->ID );
$featured_image = wpuf_get_option( 'enable_featured_image', 'wpuf_frontend_posting', 'no' );
?>
<div id="wpuf-post-area">
<form name="wpuf_edit_post_form" id="wpuf_edit_post_form" action="" enctype="multipart/form-data" method="POST">
<?php wp_nonce_field( 'wpuf-edit-post' ) ?>
<ul class="wpuf-post-form">
<?php do_action( 'wpuf_add_post_form_top', $curpost->post_type, $curpost ); //plugin hook ?>
<?php wpuf_build_custom_field_form( 'top', true, $curpost->ID ); ?>
<?php if ( $featured_image == 'yes' ) { ?>
<?php if ( current_theme_supports( 'post-thumbnails' ) ) { ?>
<li>
<label for="post-thumbnail"><?php echo wpuf_get_option( 'ft_image_label', 'wpuf_labels', 'Featured Image' ); ?></label>
<div id="wpuf-ft-upload-container">
<div id="wpuf-ft-upload-filelist">
<?php
$style = '';
if ( has_post_thumbnail( $curpost->ID ) ) {
$style = ' style="display:none"';
$post_thumbnail_id = get_post_thumbnail_id( $curpost->ID );
echo wpuf_feat_img_html( $post_thumbnail_id );
}
?>
</div>
<a id="wpuf-ft-upload-pickfiles" class="button" href="#"><?php echo wpuf_get_option( 'ft_image_btn_label', 'wpuf_labels', 'Upload Image' ); ?></a>
</div>
<div class="clear"></div>
</li>
<?php } else { ?>
<div class="info"><?php _e( 'Your theme doesn\'t support featured image', 'wpuf' ) ?></div>
<?php } ?>
<?php } ?>
<li>
<label for="new-post-title">
<?php echo wpuf_get_option( 'title_label', 'wpuf_labels', 'Post Title' ); ?> <span class="required">*</span>
</label>
<input type="text" name="wpuf_post_title" id="new-post-title" minlength="2" value="<?php echo esc_html( $curpost->post_title ); ?>">
<div class="clear"></div>
<p class="description"><?php echo stripslashes( wpuf_get_option( 'title_help', 'wpuf_labels' ) ); ?></p>
</li>
<?php if ( wpuf_get_option( 'allow_cats', 'wpuf_frontend_posting', 'on' ) == 'on' ) { ?>
<li>
<label for="new-post-cat">
<?php echo wpuf_get_option( 'cat_label', 'wpuf_labels', 'Category' ); ?> <span class="required">*</span>
</label>
<?php
$exclude = wpuf_get_option( 'exclude_cats', 'wpuf_frontend_posting' );
$cat_type = wpuf_get_option( 'cat_type', 'wpuf_frontend_posting' );
$cats = get_the_category( $curpost->ID );
$selected = 0;
if ( $cats ) {
$selected = $cats[0]->term_id;
}
//var_dump( $cats );
//var_dump( $selected );
?>
<div class="category-wrap" style="float:left;">
<div id="lvl0">
<?php
if ( $cat_type == 'normal' ) {
wp_dropdown_categories( 'show_option_none=' . __( '-- Select --', 'wpuf' ) . '&hierarchical=1&hide_empty=0&orderby=name&name=category[]&id=cat&show_count=0&title_li=&use_desc_for_title=1&class=cat requiredField&exclude=' . $exclude . '&selected=' . $selected );
} else if ( $cat_type == 'ajax' ) {
wp_dropdown_categories( 'show_option_none=' . __( '-- Select --', 'wpuf' ) . '&hierarchical=1&hide_empty=0&orderby=name&name=category[]&id=cat-ajax&show_count=0&title_li=&use_desc_for_title=1&class=cat requiredField&depth=1&exclude=' . $exclude . '&selected=' . $selected );
} else {
wpuf_category_checklist( $curpost->ID, false, 'category', $exclude);
}
?>
</div>
</div>
<div class="loading"></div>
<div class="clear"></div>
<p class="description"><?php echo stripslashes( wpuf_get_option( 'cat_help', 'wpuf_labels' ) ); ?></p>
</li>
<?php } ?>
<?php do_action( 'wpuf_add_post_form_description', $curpost->post_type, $curpost ); ?>
<?php wpuf_build_custom_field_form( 'description', true, $curpost->ID ); ?>
<li>
<label for="new-post-desc">
<?php echo wpuf_get_option( 'desc_label', 'wpuf_labels', 'Post Content' ); ?> <span class="required">*</span>
</label>
<?php
$editor = wpuf_get_option( 'editor_type', 'wpuf_frontend_posting', 'normal' );
if ( $editor == 'full' ) {
?>
<div style="float:left;">
<?php wp_editor( $curpost->post_content, 'new-post-desc', array('textarea_name' => 'wpuf_post_content', 'editor_class' => 'requiredField', 'teeny' => false, 'textarea_rows' => 8) ); ?>
</div>
<?php } else if ( $editor == 'rich' ) { ?>
<div style="float:left;">
<?php wp_editor( $curpost->post_content, 'new-post-desc', array('textarea_name' => 'wpuf_post_content', 'editor_class' => 'requiredField', 'teeny' => true, 'textarea_rows' => 8) ); ?>
</div>
<?php } else { ?>
<textarea name="wpuf_post_content" class="requiredField" id="new-post-desc" cols="60" rows="8"><?php echo esc_textarea( $curpost->post_content ); ?></textarea>
<?php } ?>
<div class="clear"></div>
<p class="description"><?php echo stripslashes( wpuf_get_option( 'desc_help', 'wpuf_labels' ) ); ?></p>
</li>
<?php do_action( 'wpuf_add_post_form_after_description', $curpost->post_type, $curpost ); ?>
<?php wpuf_build_custom_field_form( 'tag', true, $curpost->ID ); ?>
<?php if ( wpuf_get_option( 'allow_tags', 'wpuf_frontend_posting' ) == 'on' ) { ?>
<li>
<label for="new-post-tags">
<?php echo wpuf_get_option( 'tag_label', 'wpuf_labels', 'Tags' ); ?>
</label>
<input type="text" name="wpuf_post_tags" id="new-post-tags" value="<?php echo $tagslist; ?>">
<p class="description"><?php echo stripslashes( wpuf_get_option( 'tag_help', 'wpuf_labels' ) ); ?></p>
<div class="clear"></div>
</li>
<?php } ?>
<?php do_action( 'wpuf_add_post_form_tags', $curpost->post_type, $curpost ); ?>
<?php wpuf_build_custom_field_form( 'bottom', true, $curpost->ID ); ?>
<li>
<label> </label>
<input class="wpuf_submit" type="submit" name="wpuf_edit_post_submit" value="<?php echo esc_attr( wpuf_get_option( 'update_label', 'wpuf_labels', 'Update Post' ) ); ?>">
<input type="hidden" name="wpuf_edit_post_submit" value="yes" />
<input type="hidden" name="post_id" value="<?php echo $curpost->ID; ?>">
</li>
</ul>
</form>
</div>
<?php
}
function submit_post() {
global $userdata;
$errors = array();
$title = trim( $_POST['wpuf_post_title'] );
$content = trim( $_POST['wpuf_post_content'] );
$tags = '';
$cat = '';
if ( isset( $_POST['wpuf_post_tags'] ) ) {
$tags = wpuf_clean_tags( $_POST['wpuf_post_tags'] );
}
//if there is some attachement, validate them
if ( !empty( $_FILES['wpuf_post_attachments'] ) ) {
$errors = wpuf_check_upload();
}
if ( empty( $title ) ) {
$errors[] = __( 'Empty post title', 'wpuf' );
} else {
$title = trim( strip_tags( $title ) );
}
//validate cat
if ( wpuf_get_option( 'allow_cats', 'wpuf_frontend_posting', 'on' ) == 'on' ) {
$cat_type = wpuf_get_option( 'cat_type', 'wpuf_frontend_posting', 'normal' );
if ( !isset( $_POST['category'] ) ) {
$errors[] = __( 'Please choose a category', 'wpuf' );
} else if ( $cat_type == 'normal' && $_POST['category'][0] == '-1' ) {
$errors[] = __( 'Please choose a category', 'wpuf' );
} else {
if ( count( $_POST['category'] ) < 1 ) {
$errors[] = __( 'Please choose a category', 'wpuf' );
}
}
}
if ( empty( $content ) ) {
$errors[] = __( 'Empty post content', 'wpuf' );
} else {
$content = trim( $content );
}
if ( !empty( $tags ) ) {
$tags = explode( ',', $tags );
}
//process the custom fields
$custom_fields = array();
$fields = wpuf_get_custom_fields();
if ( is_array( $fields ) ) {
foreach ($fields as $cf) {
if ( array_key_exists( $cf['field'], $_POST ) ) {
if ( is_array( $_POST[$cf['field']] ) ) {
$temp = implode(',', $_POST[$cf['field']]);
} else {
$temp = trim( strip_tags( $_POST[$cf['field']] ) );
}
//var_dump($temp, $cf);
if ( ( $cf['type'] == 'yes' ) && !$temp ) {
$errors[] = sprintf( __( '"%s" is missing', 'wpuf' ), $cf['label'] );
} else {
$custom_fields[$cf['field']] = $temp;
}
} //array_key_exists
} //foreach
} //is_array
//post attachment
$attach_id = isset( $_POST['wpuf_featured_img'] ) ? intval( $_POST['wpuf_featured_img'] ) : 0;
$errors = apply_filters( 'wpuf_edit_post_validation', $errors );
if ( !$errors ) {
//users are allowed to choose category
if ( wpuf_get_option( 'allow_cats', 'wpuf_frontend_posting', 'on' ) == 'on' ) {
$post_category = $_POST['category'];
} else {
$post_category = array( wpuf_get_option( 'default_cat', 'wpuf_frontend_posting' ) );
}
$post_update = array(
'ID' => trim( $_POST['post_id'] ),
'post_title' => $title,
'post_content' => $content,
'post_category' => $post_category,
'tags_input' => $tags
);
//plugin API to extend the functionality
$post_update = apply_filters( 'wpuf_edit_post_args', $post_update );
$post_id = wp_update_post( $post_update );
if ( $post_id ) {
echo '<div class="success">' . __( 'Post updated successfully.', 'wpuf' ) . '</div>';
//upload attachment to the post
wpuf_upload_attachment( $post_id );
//set post thumbnail if has any
if ( $attach_id ) {
set_post_thumbnail( $post_id, $attach_id );
}
//add the custom fields
if ( $custom_fields ) {
foreach ($custom_fields as $key => $val) {
update_post_meta( $post_id, $key, $val, false );
}
}
do_action( 'wpuf_edit_post_after_update', $post_id );
}
} else {
echo wpuf_error_msg( $errors );
}
}
}
$wpuf_edit = new WPUF_Edit_Post();