Advertisement
Guest User

WP User Frontend add-post.php

a guest
Mar 1st, 2013
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.76 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Add Post form class
  5. *
  6. * @author Tareq Hasan
  7. * @package WP User Frontend
  8. */
  9. class WPUF_Add_Post {
  10.  
  11. function __construct() {
  12. add_shortcode( 'wpuf_addpost', array($this, 'shortcode') );
  13. }
  14.  
  15. /**
  16. * Handles the add post shortcode
  17. *
  18. * @param $atts
  19. */
  20. function shortcode( $atts ) {
  21.  
  22. extract( shortcode_atts( array('post_type' => 'post'), $atts ) );
  23.  
  24. ob_start();
  25.  
  26. if ( is_user_logged_in() ) {
  27. $this->post_form( $post_type );
  28. } else {
  29. printf( __( "This page is restricted. Please %s to view this page.", 'wpuf' ), wp_loginout( get_permalink(), false ) );
  30. }
  31.  
  32. $content = ob_get_contents();
  33. ob_end_clean();
  34.  
  35. return $content;
  36. }
  37.  
  38. /**
  39. * Add posting main form
  40. *
  41. * @param $post_type
  42. */
  43. function post_form( $post_type ) {
  44. global $userdata;
  45.  
  46. $userdata = get_user_by( 'id', $userdata->ID );
  47.  
  48. if ( isset( $_POST['wpuf_post_new_submit'] ) ) {
  49. $nonce = $_REQUEST['_wpnonce'];
  50. if ( !wp_verify_nonce( $nonce, 'wpuf-add-post' ) ) {
  51. wp_die( __( 'Cheating?' ) );
  52. }
  53.  
  54. $this->submit_post();
  55. }
  56.  
  57. $info = __( "Post It!", 'wpuf' );
  58. $can_post = 'yes';
  59.  
  60. $info = apply_filters( 'wpuf_addpost_notice', $info );
  61. $can_post = apply_filters( 'wpuf_can_post', $can_post );
  62. $featured_image = wpuf_get_option( 'enable_featured_image' );
  63.  
  64. $title = isset( $_POST['wpuf_post_title'] ) ? esc_attr( $_POST['wpuf_post_title'] ) : '';
  65. $description = isset( $_POST['wpuf_post_content'] ) ? $_POST['wpuf_post_content'] : '';
  66.  
  67. if ( $can_post == 'yes' ) {
  68. ?>
  69. <div id="wpuf-post-area">
  70. <form id="wpuf_new_post_form" name="wpuf_new_post_form" action="" enctype="multipart/form-data" method="POST">
  71. <?php wp_nonce_field( 'wpuf-add-post' ) ?>
  72.  
  73. <ul class="wpuf-post-form">
  74.  
  75. <?php do_action( 'wpuf_add_post_form_top', $post_type ); //plugin hook ?>
  76. <?php wpuf_build_custom_field_form( 'top' ); ?>
  77.  
  78. <?php if ( $featured_image == 'yes' ) { ?>
  79. <?php if ( current_theme_supports( 'post-thumbnails' ) ) { ?>
  80. <li>
  81. <label for="post-thumbnail"><?php echo wpuf_get_option( 'ft_image_label' ); ?></label>
  82. <div id="wpuf-ft-upload-container">
  83. <div id="wpuf-ft-upload-filelist"></div>
  84. <a id="wpuf-ft-upload-pickfiles" class="button" href="#"><?php echo wpuf_get_option( 'ft_image_btn_label' ); ?></a>
  85. </div>
  86. <div class="clear"></div>
  87. </li>
  88. <?php } else { ?>
  89. <div class="info"><?php _e( 'Your theme doesn\'t support featured image', 'wpuf' ) ?></div>
  90. <?php } ?>
  91. <?php } ?>
  92.  
  93. <li>
  94. <label for="new-post-title">
  95. <?php echo wpuf_get_option( 'title_label' ); ?> <span class="required">*</span>
  96. </label>
  97. <input class="requiredField" type="text" value="<?php echo $title; ?>" name="wpuf_post_title" id="new-post-title" minlength="2">
  98. <div class="clear"></div>
  99. <p class="description"><?php echo stripslashes( wpuf_get_option( 'title_help' ) ); ?></p>
  100. </li>
  101.  
  102. <?php if ( wpuf_get_option( 'allow_cats' ) == 'on' ) { ?>
  103. <li>
  104. <label for="new-post-cat">
  105. <?php echo wpuf_get_option( 'cat_label' ); ?> <span class="required">*</span>
  106. </label>
  107.  
  108. <div class="category-wrap" style="float:left;">
  109. <div id="lvl0">
  110. <?php
  111. $exclude = wpuf_get_option( 'exclude_cats' );
  112. $cat_type = wpuf_get_option( 'cat_type' );
  113.  
  114. if ( $cat_type == 'normal' ) {
  115. 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 );
  116. } else if ( $cat_type == 'ajax' ) {
  117. 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 );
  118. } else {
  119. wpuf_category_checklist();
  120. }
  121. ?>
  122. </div>
  123. </div>
  124. <div class="loading"></div>
  125. <div class="clear"></div>
  126. <p class="description"><?php echo stripslashes( wpuf_get_option( 'cat_help' ) ); ?></p>
  127. </li>
  128. <?php } ?>
  129.  
  130. <?php do_action( 'wpuf_add_post_form_description', $post_type ); ?>
  131. <?php wpuf_build_custom_field_form( 'description' ); ?>
  132.  
  133. <li>
  134. <label for="new-post-desc">
  135. <?php echo wpuf_get_option( 'desc_label' ); ?> <span class="required">*</span>
  136. </label>
  137.  
  138. <?php
  139. $editor = wpuf_get_option( 'editor_type' );
  140. if ( $editor == 'full' ) {
  141. ?>
  142. <div style="float:left;">
  143. <?php wp_editor( $description, 'new-post-desc', array('textarea_name' => 'wpuf_post_content', 'editor_class' => 'requiredField', 'teeny' => false, 'textarea_rows' => 15, 'media_buttons' => false, 'quicktags' => false) ); ?>
  144. </div>
  145. <?php } else if ( $editor == 'rich' ) { ?>
  146. <div style="float:left;">
  147. <?php wp_editor( $description, 'new-post-desc', array('textarea_name' => 'wpuf_post_content', 'editor_class' => 'requiredField', 'teeny' => true, 'textarea_rows' => 15, 'media_buttons' => false, 'quicktags' => false) ); ?>
  148. </div>
  149.  
  150. <?php } else { ?>
  151. <textarea name="wpuf_post_content" class="requiredField" id="new-post-desc" cols="60" rows="8"><?php echo esc_textarea( $description ); ?></textarea>
  152. <?php } ?>
  153.  
  154. <div class="clear"></div>
  155. <p class="description"><?php echo stripslashes( wpuf_get_option( 'desc_help' ) ); ?></p>
  156. </li>
  157.  
  158. <?php
  159. do_action( 'wpuf_add_post_form_after_description', $post_type );
  160.  
  161. $this->publish_date_form();
  162. $this->expiry_date_form();
  163.  
  164. wpuf_build_custom_field_form( 'tag' );
  165.  
  166. if ( wpuf_get_option( 'allow_tags' ) == 'on' ) {
  167. ?>
  168. <li>
  169. <label for="new-post-tags">
  170. <?php echo wpuf_get_option( 'tag_label' ); ?>
  171. </label>
  172. <input type="text" name="wpuf_post_tags" id="new-post-tags" class="new-post-tags">
  173. <p class="description"><?php echo stripslashes( wpuf_get_option( 'tag_help' ) ); ?></p>
  174. <div class="clear"></div>
  175. </li>
  176. <?php
  177. }
  178.  
  179. do_action( 'wpuf_add_post_form_tags', $post_type );
  180. wpuf_build_custom_field_form( 'bottom' );
  181. ?>
  182.  
  183. <li>
  184. <label>&nbsp;</label>
  185. <input class="wpuf_submit" type="submit" name="wpuf_new_post_submit" value="<?php echo esc_attr( wpuf_get_option( 'submit_label' ) ); ?>">
  186. <input type="hidden" name="wpuf_post_type" value="<?php echo $post_type; ?>" />
  187. <input type="hidden" name="wpuf_post_new_submit" value="yes" />
  188. </li>
  189.  
  190. <?php do_action( 'wpuf_add_post_form_bottom', $post_type ); ?>
  191.  
  192. </ul>
  193. </form>
  194. </div>
  195. <?php
  196. } else {
  197. echo '<div class="info">' . $info . '</div>';
  198. }
  199. }
  200.  
  201. /**
  202. * Prints the post publish date on form
  203. *
  204. * @return bool|string
  205. */
  206. function publish_date_form() {
  207. $enable_date = wpuf_get_option( 'enable_post_date' );
  208.  
  209. if ( $enable_date != 'on' ) {
  210. return;
  211. }
  212.  
  213. $timezone_format = _x( 'Y-m-d G:i:s', 'timezone date format' );
  214. $month = date_i18n( 'm' );
  215. $month_array = array(
  216. '01' => 'Jan',
  217. '02' => 'Feb',
  218. '03' => 'Mar',
  219. '04' => 'Apr',
  220. '05' => 'May',
  221. '06' => 'Jun',
  222. '07' => 'Jul',
  223. '08' => 'Aug',
  224. '09' => 'Sep',
  225. '10' => 'Oct',
  226. '11' => 'Nov',
  227. '12' => 'Dec'
  228. );
  229. ?>
  230. <li>
  231. <label for="timestamp-wrap">
  232. <?php _e( 'Publish Time:', 'wpuf' ); ?> <span class="required">*</span>
  233. </label>
  234. <div class="timestamp-wrap">
  235. <select name="mm">
  236. <?php
  237. foreach ($month_array as $key => $val) {
  238. $selected = ( $key == $month ) ? ' selected="selected"' : '';
  239. echo '<option value="' . $key . '"' . $selected . '>' . $val . '</option>';
  240. }
  241. ?>
  242. </select>
  243. <input type="text" autocomplete="off" tabindex="4" maxlength="2" size="2" value="<?php echo date_i18n( 'd' ); ?>" name="jj">,
  244. <input type="text" autocomplete="off" tabindex="4" maxlength="4" size="4" value="<?php echo date_i18n( 'Y' ); ?>" name="aa">
  245. @ <input type="text" autocomplete="off" tabindex="4" maxlength="2" size="2" value="<?php echo date_i18n( 'G' ); ?>" name="hh">
  246. : <input type="text" autocomplete="off" tabindex="4" maxlength="2" size="2" value="<?php echo date_i18n( 'i' ); ?>" name="mn">
  247. </div>
  248. <div class="clear"></div>
  249. <p class="description"></p>
  250. </li>
  251. <?php
  252. }
  253.  
  254. /**
  255. * Prints post expiration date on the form
  256. *
  257. * @return bool|string
  258. */
  259. function expiry_date_form() {
  260. $post_expiry = wpuf_get_option( 'enable_post_expiry' );
  261.  
  262. if ( $post_expiry != 'on' ) {
  263. return;
  264. }
  265. ?>
  266. <li>
  267. <label for="timestamp-wrap">
  268. <?php _e( 'Expiration Time:', 'wpuf' ); ?><span class="required">*</span>
  269. </label>
  270. <select name="expiration-date">
  271. <option value="1">1</option>
  272. </select>
  273. <div class="clear"></div>
  274. <p class="description"><?php _e( 'Post expiration time in day after publishing.', 'wpuf' ); ?></p>
  275. </li>
  276. <?php
  277. }
  278.  
  279. /**
  280. * Validate the post submit data
  281. *
  282. * @global type $userdata
  283. * @param type $post_type
  284. */
  285. function submit_post() {
  286. global $userdata;
  287.  
  288. $errors = array();
  289.  
  290. //if there is some attachement, validate them
  291. if ( !empty( $_FILES['wpuf_post_attachments'] ) ) {
  292. $errors = wpuf_check_upload();
  293. }
  294.  
  295. $title = trim( $_POST['wpuf_post_title'] );
  296. $content = trim( $_POST['wpuf_post_content'] );
  297.  
  298. $tags = '';
  299. if ( isset( $_POST['wpuf_post_tags'] ) ) {
  300. $tags = wpuf_clean_tags( $_POST['wpuf_post_tags'] );
  301. }
  302.  
  303. //validate title
  304. if ( empty( $title ) ) {
  305. $errors[] = __( 'Empty post title', 'wpuf' );
  306. } else {
  307. $title = trim( strip_tags( $title ) );
  308. }
  309.  
  310. //validate cat
  311. if ( wpuf_get_option( 'allow_cats' ) == 'on' ) {
  312. $cat_type = wpuf_get_option( 'cat_type' );
  313. if ( !isset( $_POST['category'] ) ) {
  314. $errors[] = __( 'Please choose a category', 'wpuf' );
  315. } else if ( $cat_type == 'normal' && $_POST['category'][0] == '-1' ) {
  316. $errors[] = __( 'Please choose a category', 'wpuf' );
  317. } else {
  318. if ( count( $_POST['category'] ) < 1 ) {
  319. $errors[] = __( 'Please choose a category', 'wpuf' );
  320. }
  321. }
  322. }
  323.  
  324. //validate post content
  325. if ( empty( $content ) ) {
  326. $errors[] = __( 'Empty post content', 'wpuf' );
  327. } else {
  328. $content = trim( $content );
  329. }
  330.  
  331. //process tags
  332. if ( !empty( $tags ) ) {
  333. $tags = explode( ',', $tags );
  334. }
  335.  
  336. //post attachment
  337. $attach_id = isset( $_POST['wpuf_featured_img'] ) ? intval( $_POST['wpuf_featured_img'] ) : 0;
  338.  
  339. //post type
  340. $post_type = trim( strip_tags( $_POST['wpuf_post_type'] ) );
  341.  
  342. //process the custom fields
  343. $custom_fields = array();
  344.  
  345. $fields = wpuf_get_custom_fields();
  346. if ( is_array( $fields ) ) {
  347.  
  348. foreach ($fields as $cf) {
  349. if ( array_key_exists( $cf['field'], $_POST ) ) {
  350.  
  351. $temp = trim( strip_tags( $_POST[$cf['field']] ) );
  352. //var_dump($temp, $cf);
  353.  
  354. if ( ( $cf['type'] == 'yes' ) && !$temp ) {
  355. $errors[] = sprintf( __( '%s is missing', 'wpuf' ), $cf['label'] );
  356. } else {
  357. $custom_fields[$cf['field']] = $temp;
  358. }
  359. } //array_key_exists
  360. } //foreach
  361. } //is_array
  362.  
  363. $post_date_enable = wpuf_get_option( 'enable_post_date' );
  364. $post_expiry = wpuf_get_option( 'enable_post_expiry' );
  365.  
  366. //check post date
  367. if ( $post_date_enable == 'on' ) {
  368. $month = $_POST['mm'];
  369. $day = $_POST['jj'];
  370. $year = $_POST['aa'];
  371. $hour = $_POST['hh'];
  372. $min = $_POST['mn'];
  373.  
  374. if ( !checkdate( $month, $day, $year ) ) {
  375. $errors[] = __( 'Invalid date', 'wpuf' );
  376. }
  377. }
  378.  
  379. $errors = apply_filters( 'wpuf_add_post_validation', $errors );
  380.  
  381.  
  382. //if not any errors, proceed
  383. if ( $errors ) {
  384. echo wpuf_error_msg( $errors );
  385. return;
  386. }
  387.  
  388. $post_stat = wpuf_get_option( 'post_status' );
  389. $post_author = (wpuf_get_option( 'post_author' ) == 'original' ) ? $userdata->ID : wpuf_get_option( 'map_author' );
  390.  
  391. //users are allowed to choose category
  392. if ( wpuf_get_option( 'allow_cats' ) == 'on' ) {
  393. $post_category = $_POST['category'];
  394. } else {
  395. $post_category = array(wpuf_get_option( 'default_cat' ));
  396. }
  397.  
  398. $my_post = array(
  399. 'post_title' => $title,
  400. 'post_content' => $content,
  401. 'post_status' => $post_stat,
  402. 'post_author' => $post_author,
  403. 'post_category' => $post_category,
  404. 'post_type' => $post_type,
  405. 'tags_input' => $tags
  406. );
  407.  
  408. if ( $post_date_enable == 'on' ) {
  409. $month = $_POST['mm'];
  410. $day = $_POST['jj'];
  411. $year = $_POST['aa'];
  412. $hour = $_POST['hh'];
  413. $min = $_POST['mn'];
  414.  
  415. $post_date = mktime( $hour, $min, 59, $month, $day, $year );
  416. $my_post['post_date'] = date( 'Y-m-d H:i:s', $post_date );
  417. }
  418.  
  419. //plugin API to extend the functionality
  420. $my_post = apply_filters( 'wpuf_add_post_args', $my_post );
  421.  
  422. //var_dump( $_POST, $my_post );die();
  423. //insert the post
  424. $post_id = wp_insert_post( $my_post );
  425.  
  426. if ( $post_id ) {
  427.  
  428. //upload attachment to the post
  429. wpuf_upload_attachment( $post_id );
  430.  
  431. //send mail notification
  432. if ( wpuf_get_option( 'post_notification' ) == 'yes' ) {
  433. wpuf_notify_post_mail( $userdata, $post_id );
  434. }
  435.  
  436. //add the custom fields
  437. if ( $custom_fields ) {
  438. foreach ($custom_fields as $key => $val) {
  439. add_post_meta( $post_id, $key, $val, true );
  440. }
  441. }
  442.  
  443. //set post thumbnail if has any
  444. if ( $attach_id ) {
  445. set_post_thumbnail( $post_id, $attach_id );
  446. }
  447.  
  448. //Set Post expiration date if has any
  449. if ( !empty( $_POST['expiration-date'] ) && $post_expiry == 'on' ) {
  450. $post = get_post( $post_id );
  451. $post_date = strtotime( $post->post_date );
  452. $expiration = (int) $_POST['expiration-date'];
  453. $expiration = $post_date + ($expiration * 60 * 60 * 24);
  454.  
  455. add_post_meta( $post_id, 'expiration-date', $expiration, true );
  456. }
  457.  
  458. //plugin API to extend the functionality
  459. do_action( 'wpuf_add_post_after_insert', $post_id );
  460.  
  461. //echo '<div class="success">' . __('Post published successfully', 'wpuf') . '</div>';
  462. if ( $post_id ) {
  463. $redirect = apply_filters( 'wpuf_after_post_redirect', get_permalink( $post_id ), $post_id );
  464.  
  465. wp_redirect( $redirect );
  466. exit;
  467. }
  468. }
  469. }
  470.  
  471. }
  472.  
  473. $wpuf_postform = new WPUF_Add_Post();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement