-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpuf-add-post.php
488 lines (403 loc) · 19.5 KB
/
wpuf-add-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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
/**
* Add Post form class
*
* @author Tareq Hasan
* @package WP User Frontend
*/
class WPUF_Add_Post {
function __construct() {
add_shortcode( 'wpuf_addpost', array($this, 'shortcode') );
}
/**
* Handles the add post shortcode
*
* @param $atts
*/
function shortcode( $atts ) {
extract( shortcode_atts( array('post_type' => 'post'), $atts ) );
ob_start();
if ( is_user_logged_in() ) {
$this->post_form( $post_type );
} else {
printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( get_permalink(), false ) );
}
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Add posting main form
*
* @param $post_type
*/
function post_form( $post_type ) {
global $userdata;
$userdata = get_user_by( 'id', $userdata->ID );
if ( isset( $_POST['wpuf_post_new_submit'] ) ) {
$nonce = $_REQUEST['_wpnonce'];
if ( !wp_verify_nonce( $nonce, 'wpuf-add-post' ) ) {
wp_die( __( 'Cheating?' ) );
}
$this->submit_post();
}
$info = __( "Post It!", 'wpuf' );
$can_post = 'yes';
$info = apply_filters( 'wpuf_addpost_notice', $info );
$can_post = apply_filters( 'wpuf_can_post', $can_post );
$featured_image = wpuf_get_option( 'enable_featured_image', 'wpuf_frontend_posting', 'no' );
$title = isset( $_POST['wpuf_post_title'] ) ? esc_attr( $_POST['wpuf_post_title'] ) : '';
$description = isset( $_POST['wpuf_post_content'] ) ? $_POST['wpuf_post_content'] : '';
if ( $can_post == 'yes' ) {
?>
<div id="wpuf-post-area">
<form id="wpuf_new_post_form" name="wpuf_new_post_form" action="" enctype="multipart/form-data" method="POST">
<?php wp_nonce_field( 'wpuf-add-post' ) ?>
<ul class="wpuf-post-form">
<?php do_action( 'wpuf_add_post_form_top', $post_type ); //plugin hook ?>
<?php wpuf_build_custom_field_form( 'top' ); ?>
<?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_frontend_posting', 'Featured Image' ); ?></label>
<div id="wpuf-ft-upload-container">
<div id="wpuf-ft-upload-filelist"></div>
<a id="wpuf-ft-upload-pickfiles" class="button" href="#"><?php echo wpuf_get_option( 'ft_image_btn_label', 'wpuf_frontend_posting', '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 class="requiredField" type="text" value="<?php echo $title; ?>" name="wpuf_post_title" id="new-post-title" minlength="2">
<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>
<div class="category-wrap" style="float:left;">
<div id="lvl0">
<?php
$exclude = wpuf_get_option( 'exclude_cats', 'wpuf_frontend_posting' );
$cat_type = wpuf_get_option( 'cat_type', 'wpuf_frontend_posting', 'normal' );
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 );
} 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 );
} else {
wpuf_category_checklist(0, 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', $post_type ); ?>
<?php wpuf_build_custom_field_form( 'description' ); ?>
<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' );
if ( $editor == 'full' ) {
?>
<div style="float:left;">
<?php wp_editor( $description, '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( $description, '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( $description ); ?></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', $post_type );
$this->publish_date_form();
$this->expiry_date_form();
wpuf_build_custom_field_form( 'tag' );
if ( wpuf_get_option( 'allow_tags', 'wpuf_frontend_posting', 'on' ) == '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" class="new-post-tags">
<p class="description"><?php echo stripslashes( wpuf_get_option( 'tag_help', 'wpuf_labels' ) ); ?></p>
<div class="clear"></div>
</li>
<?php
}
do_action( 'wpuf_add_post_form_tags', $post_type );
wpuf_build_custom_field_form( 'bottom' );
?>
<li>
<label> </label>
<input class="wpuf_submit" type="submit" name="wpuf_new_post_submit" value="<?php echo esc_attr( wpuf_get_option( 'submit_label', 'wpuf_labels', 'Submit' ) ); ?>">
<input type="hidden" name="wpuf_post_type" value="<?php echo $post_type; ?>" />
<input type="hidden" name="wpuf_post_new_submit" value="yes" />
</li>
<?php do_action( 'wpuf_add_post_form_bottom', $post_type ); ?>
</ul>
</form>
</div>
<?php
} else {
echo '<div class="info">' . $info . '</div>';
}
}
/**
* Prints the post publish date on form
*
* @return bool|string
*/
function publish_date_form() {
$enable_date = wpuf_get_option( 'enable_post_date', 'wpuf_frontend_posting', 'off' );
if ( $enable_date != 'on' ) {
return;
}
$timezone_format = _x( 'Y-m-d G:i:s', 'timezone date format' );
$month = date_i18n( 'm' );
$month_array = array(
'01' => 'Jan',
'02' => 'Feb',
'03' => 'Mar',
'04' => 'Apr',
'05' => 'May',
'06' => 'Jun',
'07' => 'Jul',
'08' => 'Aug',
'09' => 'Sep',
'10' => 'Oct',
'11' => 'Nov',
'12' => 'Dec'
);
?>
<li>
<label for="timestamp-wrap">
<?php _e( 'Publish Time:', 'wpuf' ); ?> <span class="required">*</span>
</label>
<div class="timestamp-wrap">
<select name="mm">
<?php
foreach ($month_array as $key => $val) {
$selected = ( $key == $month ) ? ' selected="selected"' : '';
echo '<option value="' . $key . '"' . $selected . '>' . $val . '</option>';
}
?>
</select>
<input type="text" autocomplete="off" tabindex="4" maxlength="2" size="2" value="<?php echo date_i18n( 'd' ); ?>" name="jj">,
<input type="text" autocomplete="off" tabindex="4" maxlength="4" size="4" value="<?php echo date_i18n( 'Y' ); ?>" name="aa">
@ <input type="text" autocomplete="off" tabindex="4" maxlength="2" size="2" value="<?php echo date_i18n( 'G' ); ?>" name="hh">
: <input type="text" autocomplete="off" tabindex="4" maxlength="2" size="2" value="<?php echo date_i18n( 'i' ); ?>" name="mn">
</div>
<div class="clear"></div>
<p class="description"></p>
</li>
<?php
}
/**
* Prints post expiration date on the form
*
* @return bool|string
*/
function expiry_date_form() {
$post_expiry = wpuf_get_option( 'enable_post_expiry', 'wpuf_frontend_posting' );
if ( $post_expiry != 'on' ) {
return;
}
?>
<li>
<label for="timestamp-wrap">
<?php _e( 'Expiration Time:', 'wpuf' ); ?><span class="required">*</span>
</label>
<select name="expiration-date">
<?php
for ($i = 1; $i <= 90; $i++) {
if ( $i % 2 != 0 ) {
continue;
}
printf( '<option value="%1$d">%1$d %2$s</option>', $i, __( 'days', 'wpuf' ) );
}
?>
</select>
<div class="clear"></div>
<p class="description"><?php _e( 'Post expiration time in day after publishing.', 'wpuf' ); ?></p>
</li>
<?php
}
/**
* Validate the post submit data
*
* @global type $userdata
* @param type $post_type
*/
function submit_post() {
global $userdata;
$errors = array();
var_dump( $_POST );
//if there is some attachement, validate them
if ( !empty( $_FILES['wpuf_post_attachments'] ) ) {
$errors = wpuf_check_upload();
}
$title = trim( $_POST['wpuf_post_title'] );
$content = trim( $_POST['wpuf_post_content'] );
$tags = '';
if ( isset( $_POST['wpuf_post_tags'] ) ) {
$tags = wpuf_clean_tags( $_POST['wpuf_post_tags'] );
}
//validate title
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' );
}
}
}
//validate post content
if ( empty( $content ) ) {
$errors[] = __( 'Empty post content', 'wpuf' );
} else {
$content = trim( $content );
}
//process tags
if ( !empty( $tags ) ) {
$tags = explode( ',', $tags );
}
//post attachment
$attach_id = isset( $_POST['wpuf_featured_img'] ) ? intval( $_POST['wpuf_featured_img'] ) : 0;
//post type
$post_type = trim( strip_tags( $_POST['wpuf_post_type'] ) );
//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_date_enable = wpuf_get_option( 'enable_post_date', 'wpuf_frontend_posting' );
$post_expiry = wpuf_get_option( 'enable_post_expiry', 'wpuf_frontend_posting' );
//check post date
if ( $post_date_enable == 'on' ) {
$month = $_POST['mm'];
$day = $_POST['jj'];
$year = $_POST['aa'];
$hour = $_POST['hh'];
$min = $_POST['mn'];
if ( !checkdate( $month, $day, $year ) ) {
$errors[] = __( 'Invalid date', 'wpuf' );
}
}
$errors = apply_filters( 'wpuf_add_post_validation', $errors );
//if not any errors, proceed
if ( $errors ) {
echo wpuf_error_msg( $errors );
return;
}
$post_stat = wpuf_get_option( 'post_status', 'wpuf_frontend_posting' );
$post_author = (wpuf_get_option( 'post_author', 'wpuf_frontend_posting' ) == 'original' ) ? $userdata->ID : wpuf_get_option( 'map_author', 'wpuf_frontend_posting' );
//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' ));
}
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => $post_stat,
'post_author' => $post_author,
'post_category' => $post_category,
'post_type' => $post_type,
'tags_input' => $tags
);
if ( $post_date_enable == 'on' ) {
$month = $_POST['mm'];
$day = $_POST['jj'];
$year = $_POST['aa'];
$hour = $_POST['hh'];
$min = $_POST['mn'];
$post_date = mktime( $hour, $min, 59, $month, $day, $year );
$my_post['post_date'] = date( 'Y-m-d H:i:s', $post_date );
}
//plugin API to extend the functionality
$my_post = apply_filters( 'wpuf_add_post_args', $my_post );
//var_dump( $_POST, $my_post );die();
//insert the post
$post_id = wp_insert_post( $my_post );
if ( $post_id ) {
//upload attachment to the post
wpuf_upload_attachment( $post_id );
//send mail notification
if ( wpuf_get_option( 'post_notification', 'wpuf_others', 'yes' ) == 'yes' ) {
wpuf_notify_post_mail( $userdata, $post_id );
}
//add the custom fields
if ( $custom_fields ) {
foreach ($custom_fields as $key => $val) {
add_post_meta( $post_id, $key, $val, true );
}
}
//set post thumbnail if has any
if ( $attach_id ) {
set_post_thumbnail( $post_id, $attach_id );
}
//Set Post expiration date if has any
if ( !empty( $_POST['expiration-date'] ) && $post_expiry == 'on' ) {
$post = get_post( $post_id );
$post_date = strtotime( $post->post_date );
$expiration = (int) $_POST['expiration-date'];
$expiration = $post_date + ($expiration * 60 * 60 * 24);
add_post_meta( $post_id, 'expiration-date', $expiration, true );
}
//plugin API to extend the functionality
do_action( 'wpuf_add_post_after_insert', $post_id );
//echo '<div class="success">' . __('Post published successfully', 'wpuf') . '</div>';
if ( $post_id ) {
$redirect = apply_filters( 'wpuf_after_post_redirect', get_permalink( $post_id ), $post_id );
wp_redirect( $redirect );
exit;
}
}
}
}
$wpuf_postform = new WPUF_Add_Post();