Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 24.14 KB | None | 0 0
  1. <?php
  2.  
  3. class DWQA_Handle {
  4.     public function __construct() {
  5.         // question
  6.         add_action( 'wp_loaded', array( $this, 'submit_question' ), 11 );
  7.         add_action( 'wp_loaded', array( $this, 'update_question' ) );
  8.  
  9.         // answer
  10.         add_action( 'wp_loaded', array( $this, 'insert_answer') );
  11.         add_action( 'wp_loaded', array( $this, 'update_answer' ) );
  12.  
  13.         // comment
  14.         add_action( 'wp_loaded', array( $this, 'insert_comment' ) );
  15.         add_action( 'wp_loaded', array( $this, 'update_comment' ) );
  16.     }
  17.  
  18.     public function insert_answer() {
  19.         global $dwqa_options;
  20.  
  21.         if ( ! isset( $_POST['dwqa-action'] ) || ! isset( $_POST['submit-answer'] ) ) {
  22.             return false;
  23.         }
  24.  
  25.         if ( !dwqa_current_user_can( 'post_answer' ) ) {
  26.             dwqa_add_notice( __( 'You do not have permission to submit answer.', 'dwqa' ), 'error' );
  27.             return false;
  28.         }
  29.  
  30.         if ( 'add-answer' !== $_POST['dwqa-action'] ) {
  31.             return false;
  32.         }
  33.  
  34.         if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( esc_html(  $_POST['_wpnonce'] ), '_dwqa_add_new_answer' ) ) {
  35.             dwqa_add_notice( __( '&quot;Helllo&quot;, Are you cheating huh?.', 'dwqa' ), 'error' );
  36.         }
  37.  
  38.         if ( $_POST['submit-answer'] == __( 'Delete draft', 'dwqa' ) ) {
  39.             $draft = isset( $_POST['answer-id'] ) ? intval( $_POST['answer-id'] ) : 0;
  40.             if ( $draft )
  41.                 wp_delete_post( $draft );
  42.         }
  43.  
  44.         if ( empty( $_POST['answer-content'] ) ) {
  45.             dwqa_add_notice( __( 'Answer content is empty', 'dwqa' ), 'error' );
  46.         }
  47.  
  48.         if ( empty( $_POST['question_id'] ) ) {
  49.             dwqa_add_notice( __( 'Question is empty', 'dwqa' ), 'error' );
  50.         }
  51.  
  52.         if ( !is_user_logged_in() && ( empty( $_POST['user-email'] ) || !is_email( sanitize_email( $_POST['user-email'] ) ) ) ) {
  53.             dwqa_add_notice( __( 'Missing email information', 'dwqa' ), 'error' );
  54.         }
  55.  
  56.         if ( !is_user_logged_in() && ( empty( $_POST['user-name'] ) ) ) {
  57.             dwqa_add_notice( __( 'Missing name information', 'dwqa' ), 'error' );
  58.         }
  59.  
  60.         if ( !dwqa_valid_captcha( 'single-question' ) ) {
  61.             dwqa_add_notice( __( 'Captcha is not correct', 'dwqa' ), 'error' );
  62.         }
  63.  
  64.         $user_id = 0;
  65.         $is_anonymous = false;
  66.         if ( is_user_logged_in() ) {
  67.             $user_id = get_current_user_id();
  68.         } else {
  69.             $is_anonymous = true;
  70.             if ( isset( $_POST['user-email'] ) && is_email( $_POST['user-email'] ) ) {
  71.                 $post_author_email = sanitize_email( $_POST['user-email'] );
  72.             }
  73.             if ( isset( $_POST['user-name'] ) && !empty( $_POST['user-name'] ) ) {
  74.                 $post_author_name = $_POST['user-name'];
  75.             }
  76.         }
  77.  
  78.         $question_id = intval( $_POST['question_id'] );
  79.  
  80.         $answer_title = __( 'Answer for ', 'dwqa' ) . get_post_field( 'post_title', $question_id );
  81.         $answ_content = apply_filters( 'dwqa_prepare_answer_content', $_POST['answer-content'] );
  82.  
  83.         $answers = array(
  84.             'comment_status' => 'open',
  85.             'post_author'    => $user_id,
  86.             'post_content'   => $answ_content,
  87.             'post_title'     => $answer_title,
  88.             'post_type'      => 'dwqa-answer',
  89.             'post_parent'    => $question_id,
  90.         );
  91.  
  92.         $answers['post_status'] = isset( $_POST['save-draft'] )
  93.                                     ? 'draft'
  94.                                         : ( isset( $_POST['dwqa-status'] ) && $_POST['dwqa-status'] ? $_POST['dwqa-status'] : 'publish' );
  95.  
  96.         // make sure anonymous cannot submit private answer
  97.         if ( !is_user_logged_in() && 'publish' !== $answers['post_status'] ) {
  98.             $answers['post_status'] = 'publish';
  99.         }
  100.  
  101.         // if question status is private, answer is private too
  102.         if ( 'private' == get_post_status( $question_id ) ) {
  103.             $answers['post_status'] = 'private';
  104.         }
  105.  
  106.         do_action( 'dwqa_prepare_add_answer' );
  107.  
  108.         if ( dwqa_count_notices( 'error' ) > 0 ) {
  109.             return false;
  110.         }
  111.  
  112.         $answer_id = wp_insert_post( $answers );
  113.  
  114.         if ( !is_wp_error( $answer_id ) ) {
  115.             if ( $answers['post_status'] != 'draft' ) {
  116.                 update_post_meta( $question_id, '_dwqa_status', 'answered' );
  117.                 update_post_meta( $question_id, '_dwqa_answered_time', time() );
  118.                 update_post_meta( $answer_id, '_dwqa_votes', 0 );
  119.                 $answer_count = get_post_meta( $question_id, '_dwqa_answers_count', true );
  120.                 update_post_meta( $question_id, '_dwqa_answers_count', (int) $answer_count + 1 );
  121.             }
  122.             update_post_meta( $answer_id, '_question', $question_id  );
  123.  
  124.             if ( $is_anonymous ) {
  125.                 update_post_meta( $answer_id, '_dwqa_is_anonymous', true );
  126.  
  127.                 if ( isset( $post_author_email ) && is_email( $post_author_email ) ) {
  128.                     update_post_meta( $answer_id, '_dwqa_anonymous_email', $post_author_email );
  129.                 }
  130.  
  131.                 if ( isset( $post_author_name ) && !empty( $post_author_name ) ) {
  132.                     update_post_meta( $answer_id, '_dwqa_anonymous_name', $post_author_name );
  133.                 }
  134.  
  135.                 if ( !dwqa_is_followed( $question_id, sanitize_email( $post_author_email ) ) ) {
  136.                     add_post_meta( $question_id, '_dwqa_followers', sanitize_email( $post_author_email ) );
  137.                 }
  138.             } else {
  139.                 if ( !dwqa_is_followed( $question_id, get_current_user_id() ) ) {
  140.                     add_post_meta( $question_id, '_dwqa_followers', get_current_user_id() );
  141.                 }
  142.             }
  143.  
  144.             $latest_activity_args = array(
  145.                 'text' => 'answered',
  146.                 'date' => get_post_field( 'post_date', $answer_id ),
  147.                 'user_id' => $answers['post_author'],
  148.                 'act_id' => $answer_id
  149.             );
  150.  
  151.             wp_update_post( array(
  152.                 'ID' => absint( $question_id ),
  153.                 'post_modified' => time(),
  154.                 'post_modified_gmt' => time()
  155.             ) );
  156.  
  157.             update_post_meta( $question_id, '_latest_activity', $latest_activity_args );
  158.  
  159.             do_action( 'dwqa_add_answer', $answer_id, $question_id );
  160.             exit( wp_redirect( get_permalink( $question_id ) ) );
  161.         } else {
  162.             dwqa_add_wp_error_message( $answer_id );
  163.         }
  164.     }
  165.  
  166.     public function update_answer() {
  167.         if ( isset( $_POST['dwqa-edit-answer-submit'] ) ) {
  168.  
  169.             $answer_id = isset( $_POST['answer_id'] ) ? $_POST['answer_id'] : false;
  170.             if ( !dwqa_current_user_can( 'edit_answer', $answer_id ) && !dwqa_current_user_can( 'manage_answer' ) ) {
  171.                 dwqa_add_notice( __( "You do not have permission to edit answer.", 'dwqa' ), 'error' );
  172.             }
  173.  
  174.             if ( !isset( $_POST['_wpnonce'] ) && !wp_verify_nonce( esc_html( $_POST['_wpnonce'] ), '_dwqa_edit_answer' ) ) {
  175.                 dwqa_add_notice( __( 'Hello, Are you cheating huh?', 'dwqa' ), 'error' );
  176.             }
  177.  
  178.             $answer_content = apply_filters( 'dwqa_prepare_edit_answer_content', $_POST['answer_content'] );
  179.             if ( empty( $answer_content ) ) {
  180.                 dwqa_add_notice( __( 'You must enter a valid answer content.', 'dwqa' ), 'error' );
  181.             }
  182.  
  183.             if ( !$answer_id ) {
  184.                 dwqa_add_notice( __( 'Answer is missing.', 'dwqa' ), 'error' );
  185.             }
  186.  
  187.             if ( 'dwqa-answer' !== get_post_type( $answer_id ) ) {
  188.                 dwqa_add_notice( __( 'This post is not answer.', 'dwqa' ), 'error' );
  189.             }
  190.  
  191.             do_action( 'dwqa_prepare_update_answer', $answer_id );
  192.  
  193.             if ( dwqa_count_notices( 'error' ) > 0 ) {
  194.                 return false;
  195.             }
  196.  
  197.             $args = array(
  198.                 'ID' => $answer_id,
  199.                 'post_content' => $answer_content
  200.             );
  201.  
  202.             $new_answer_id = wp_update_post( $args );
  203.  
  204.             if ( !is_wp_error( $new_answer_id ) ) {
  205.                 $old_post = get_post( $answer_id  );
  206.                 $new_post = get_post( $new_answer_id );
  207.                 do_action( 'dwqa_update_answer', $new_answer_id, $old_post, $new_post );
  208.                 $question_id = get_post_meta( $new_answer_id, '_question', true );
  209.                 wp_safe_redirect( get_permalink( $question_id ) . '#answer-' . $new_answer_id );
  210.             } else {
  211.                 dwqa_add_wp_error_message( $new_answer_id );
  212.                 return false;
  213.             }
  214.             exit();
  215.         }
  216.     }
  217.  
  218.     public function insert_comment() {
  219.         global $current_user;
  220.         if ( isset( $_POST['comment-submit'] ) ) {
  221.             if ( ! dwqa_current_user_can( 'post_comment' ) ) {
  222.                 dwqa_add_notice( __( 'You can\'t post comment', 'dwqa' ), 'error', true );
  223.             }
  224.             if ( ! isset( $_POST['comment_post_ID'] ) ) {
  225.                 dwqa_add_notice( __( 'Missing post id.', 'dwqa' ), 'error', true );
  226.             }
  227.             $comment_content = isset( $_POST['comment'] ) ? $_POST['comment'] : '';
  228.             $comment_content = apply_filters( 'dwqa_pre_comment_content', $comment_content );
  229.  
  230.             if ( empty( $comment_content ) ) {
  231.                 dwqa_add_notice( __( 'Please enter your comment content', 'dwqa' ), 'error', true );
  232.             }
  233.  
  234.             $args = array(
  235.                 'comment_post_ID'   => intval( $_POST['comment_post_ID'] ),
  236.                 'comment_content'   => $comment_content,
  237.                 'comment_parent'    => isset( $_POST['comment_parent']) ? intval( $_POST['comment_parent'] ) : 0,
  238.                 'comment_type'      => 'dwqa-comment'
  239.             );
  240.  
  241.             if ( is_user_logged_in() ) {
  242.                 $args['user_id'] = $current_user->ID;
  243.                 $args['comment_author'] = $current_user->display_name;
  244.             } else {
  245.                 if ( ! isset( $_POST['email'] ) || ! sanitize_email( $_POST['email'] ) ) {
  246.                     dwqa_add_notice( __( 'Missing email information', 'dwqa' ), 'error', true );
  247.                 }
  248.  
  249.                 if ( ! isset( $_POST['name'] ) || empty( $_POST['name'] ) ) {
  250.                     dwqa_add_notice( __( 'Missing name information', 'dwqa' ), 'error', true );
  251.                 }
  252.  
  253.                 $args['comment_author'] = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : 'Anonymous';
  254.                 $args['comment_author_email'] = sanitize_email(  $_POST['email'] );
  255.                 $args['comment_author_url'] = isset( $_POST['url'] ) ? esc_url( $_POST['url'] ) : '';
  256.                 $args['user_id']    = -1;
  257.             }
  258.  
  259.             if ( dwqa_count_notices( 'error', true ) > 0 ) {
  260.                 return false;
  261.             }
  262.  
  263.             $comment_id = wp_insert_comment( $args );
  264.  
  265.             $question_id = absint( $_POST['comment_post_ID'] );
  266.             if ( 'dwqa-answer' == get_post_type( $question_id ) ) {
  267.                 $question_id = dwqa_get_question_from_answer_id( $question_id );
  268.             }
  269.  
  270.             global $comment;
  271.             $comment = get_comment( $comment_id );
  272.             $client_id = isset( $_POST['clientId'] ) ? absint( $_POST['clientId'] ) : false;
  273.  
  274.             $latest_activity_args = array(
  275.                 'text' => 'commented',
  276.                 'date' => $comment->comment_date,
  277.                 'user_id' => $comment->user_id,
  278.                 'act_id' => $comment->comment_ID
  279.             );
  280.  
  281.             wp_update_post( array(
  282.                 'ID' => absint( $question_id ),
  283.                 'post_modified' => time(),
  284.                 'post_modified_gmt' => time()
  285.             ) );
  286.  
  287.             update_post_meta( $question_id, '_latest_activity', $latest_activity_args );
  288.  
  289.             if ( is_user_logged_in() ) {
  290.                 if ( !dwqa_is_followed( $question_id, $comment->user_id ) ) {
  291.                     add_post_meta( $question_id, '_dwqa_followers', $comment->user_id );
  292.                 }
  293.             } else {
  294.                 if ( !dwqa_is_followed( $question_id, $comment->comment_author_email ) ) {
  295.                     add_post_meta( $question_id, '_dwqa_followers', $comment->comment_author_email );
  296.                 }
  297.             }
  298.  
  299.             do_action( 'dwqa_add_comment', $comment_id, $client_id );
  300.  
  301.             $redirect_to = get_permalink( $question_id );
  302.  
  303.             if ( isset( $_GET['ans-page'] ) ) {
  304.                 $redirect_to = add_query_arg( 'ans-page', absint( $_GET['ans-page'] ), $redirect_to );
  305.             }
  306.  
  307.             $redirect_to = apply_filters( 'dwqa_submit_comment_redirect', $redirect_to, $question_id, $comment );
  308.  
  309.             wp_safe_redirect( $redirect_to );
  310.         }
  311.     }
  312.  
  313.     public function update_comment() {
  314.         global $post_submit_filter;
  315.         if ( isset( $_POST['dwqa-edit-comment-submit'] ) ) {
  316.             if ( ! isset( $_POST['comment_id']) ) {
  317.                 dwqa_add_notice( __( 'Comment is missing', 'dwqa' ), 'error' );
  318.             }
  319.             $comment_id = intval( $_POST['comment_id'] );
  320.             $comment_content = isset( $_POST['comment_content'] ) ? esc_html( $_POST['comment_content'] ) : '';
  321.             $comment_content = apply_filters( 'dwqa_pre_update_comment_content', $comment_content );
  322.  
  323.             if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['_wpnonce'] ), '_dwqa_edit_comment' ) ) {
  324.                 dwqa_add_notice( __( 'Are you cheating huh?', 'dwqa' ), 'error' );
  325.             }
  326.  
  327.             if ( !dwqa_current_user_can( 'edit_comment', $comment_id ) && !dwqa_current_user_can( 'manage_comment' ) ) {
  328.                 dwqa_add_notice( __( 'You do not have permission to edit comment.', 'dwqa' ), 'error' );
  329.             }
  330.  
  331.             if ( strlen( $comment_content ) <= 0 || ! isset( $comment_id ) || ( int )$comment_id <= 0 ) {
  332.                 dwqa_add_notice( __( 'Comment content must not be empty.', 'dwqa' ), 'error' );
  333.             } else {
  334.                 $commentarr = array(
  335.                     'comment_ID'        => $comment_id,
  336.                     'comment_content'   => $comment_content
  337.                 );
  338.  
  339.                 $intval = wp_update_comment( $commentarr );
  340.                 if ( !is_wp_error( $intval ) ) {
  341.                     $comment = get_comment( $comment_id );
  342.                     exit( wp_safe_redirect( dwqa_get_question_link( $comment->comment_post_ID ) ) );
  343.                 }else {
  344.                     dwqa_add_wp_error_message( $intval );
  345.                 }
  346.             }
  347.         }
  348.     }
  349.  
  350.     public function submit_question() {
  351.         global $dwqa_options;
  352.  
  353.         if ( isset( $_POST['dwqa-question-submit'] ) ) {
  354.             global $dwqa_current_error;
  355.             $valid_captcha = dwqa_valid_captcha( 'question' );
  356.  
  357.             $dwqa_submit_question_errors = new WP_Error();
  358.  
  359.             if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( esc_html( $_POST['_wpnonce'] ), '_dwqa_submit_question' ) ) {
  360.                 if ( $valid_captcha ) {
  361.                     if ( empty( $_POST['question-title'] ) ) {
  362.                         dwqa_add_notice( __( 'You must enter a valid question title.', 'dwqa' ), 'error' );
  363.                         return false;
  364.                     }
  365.  
  366.                     if ( !is_user_logged_in() ) {
  367.                         if ( empty( $_POST['_dwqa_anonymous_email'] ) || !is_email( sanitize_email( $_POST['_dwqa_anonymous_email'] ) ) ) {
  368.                             dwqa_add_notice( __( 'Missing email information', 'dwqa' ), 'error' );
  369.                             return false;
  370.                         }
  371.  
  372.                         if ( empty( $_POST['_dwqa_anonymous_name'] ) ) {
  373.                             dwqa_add_notice( __( 'Missing name information', 'dwqa' ), 'error' );
  374.                             return false;
  375.                         }
  376.                     }
  377.  
  378.                     $title = esc_html( $_POST['question-title'] );
  379.  
  380.                     $category = isset( $_POST['question-category'] ) ?
  381.                                 intval( $_POST['question-category'] ) : 0;
  382.                     if ( ! term_exists( $category, 'dwqa-question_category' ) ) {
  383.                         $category = 0;
  384.                     }
  385.  
  386.                     $tags = isset( $_POST['question-tag'] ) ?
  387.                                 esc_html( $_POST['question-tag'] ): '';
  388.  
  389.                     $content = isset( $_POST['question-content'] ) ? $_POST['question-content'] : '';
  390.                     $content = apply_filters( 'dwqa_prepare_question_content', $content );
  391.  
  392.  
  393.                     if ( empty( $_POST['question-job'] ) ) {
  394.                         dwqa_add_notice( __( 'You must enter a valid job.', 'dwqa' ), 'error' );
  395.                         return false;
  396.                     }
  397.                     if ( empty( $_POST['question-address'] ) ) {
  398.                         dwqa_add_notice( __( 'You must enter a valid address.', 'dwqa' ), 'error' );
  399.                         return false;
  400.                     }
  401.  
  402.                     $job = isset( $_POST['question-job'] ) ? $_POST['question-job'] : '';
  403.                     $address = isset( $_POST['question-address'] ) ? $_POST['question-address'] : '';
  404.  
  405.                     $user_id = 0;
  406.                     $is_anonymous = false;
  407.                     if ( is_user_logged_in() ) {
  408.                         $user_id = get_current_user_id();
  409.                     } else {
  410.                         //$post_author_email = $_POST['user-email'];
  411.                         if ( isset( $_POST['login-type'] ) && $_POST['login-type'] == 'sign-in' ) {
  412.                             $user = wp_signon( array(
  413.                                 'user_login'    => isset( $_POST['user-name'] ) ? esc_html( $_POST['user-name'] ) : '',
  414.                                 'user_password' => isset( $_POST['user-password'] ) ? esc_html( $_POST['user-password'] ) : '',
  415.                             ), false );
  416.  
  417.                             if ( ! is_wp_error( $user ) ) {
  418.                                 global $current_user;
  419.                                 $current_user = $user;
  420.                                 get_currentuserinfo();
  421.                                 $user_id = $user->data->ID;
  422.                             } else {
  423.                                 $dwqa_current_error = $user;
  424.                                 return false;
  425.                             }
  426.                         } elseif ( isset( $_POST['login-type'] ) && $_POST['login-type'] == 'sign-up' ) {
  427.                             //Create new user
  428.                             $users_can_register = get_option( 'users_can_register' );
  429.                             if ( isset( $_POST['user-email'] ) && isset( $_POST['user-name-signup'] )
  430.                                     && $users_can_register && ! email_exists( $_POST['user-email'] )
  431.                                         && ! username_exists( $_POST['user-name-signup'] ) ) {
  432.  
  433.                                 if ( isset( $_POST['password-signup'] ) ) {
  434.                                     $password = esc_html( $_POST['password-signup'] );
  435.                                 } else {
  436.                                     $password = wp_generate_password( 12, false );
  437.                                 }
  438.  
  439.                                 $user_id = wp_create_user(
  440.                                     esc_html( $_POST['user-name-signup'] ),
  441.                                     $password,
  442.                                     sanitize_email( $_POST['user-email'] )
  443.                                 );
  444.                                 if ( is_wp_error( $user_id ) ) {
  445.                                     $dwqa_current_error = $user_id;
  446.                                     return false;
  447.                                 }
  448.                                 wp_new_user_notification( $user_id, $password );
  449.                                 $user = wp_signon( array(
  450.                                     'user_login'    => esc_html( $_POST['user-name-signup'] ),
  451.                                     'user_password' => $password,
  452.                                 ), false );
  453.                                 if ( ! is_wp_error( $user ) ) {
  454.                                     global $current_user;
  455.                                     $current_user = $user;
  456.                                     get_currentuserinfo();
  457.                                     $user_id = $user->data->ID;
  458.                                 } else {
  459.                                     $dwqa_current_error = $user;
  460.                                     return false;
  461.                                 }
  462.                             } else {
  463.                                 $message = '';
  464.                                 if ( ! $users_can_register ) {
  465.                                     $message .= __( 'User Registration was disabled.','dwqa' ).'<br>';
  466.                                 }
  467.                                 if ( isset( $_POST['user-name'] ) && email_exists( sanitize_email( $_POST['user-email'] ) ) ) {
  468.                                     $message .= __( 'This email is already registered, please choose another one.','dwqa' ).'<br>';
  469.                                 }
  470.                                 if ( isset( $_POST['user-name'] ) && username_exists( esc_html( $_POST['user-name'] ) ) ) {
  471.                                     $message .= __( 'This username is already registered. Please use another one.','dwqa' ).'<br>';
  472.                                 }
  473.                                 // $dwqa_current_error = new WP_Error( 'submit_question', $message );
  474.                                 dwqa_add_notice( $message, 'error' );
  475.                                 return false;
  476.                             }
  477.                         } else {
  478.                             $is_anonymous = true;
  479.                             $question_author_email = isset( $_POST['_dwqa_anonymous_email'] ) && is_email( $_POST['_dwqa_anonymous_email'] ) ? sanitize_email( $_POST['_dwqa_anonymous_email'] ) : false;
  480.                             $question_author_name = isset( $_POST['_dwqa_anonymous_name'] ) && !empty( $_POST['_dwqa_anonymous_name'] ) ? $_POST['_dwqa_anonymous_name'] : false;
  481.                             $user_id = 0;
  482.                         }
  483.                     }
  484.  
  485.                     $post_status = ( isset( $_POST['question-status'] ) && esc_html( $_POST['question-status'] ) ) ? $_POST['question-status'] : 'publish';
  486.  
  487.                     // make sure anonymous cannot submit private question
  488.                     if ( !is_user_logged_in() && 'publish' !== $post_status ) {
  489.                         $post_status = 'publish';
  490.                     }
  491.  
  492.                     //Enable review mode
  493.                     global $dwqa_general_settings;
  494.                     if ( isset( $dwqa_general_settings['enable-review-question'] )
  495.                         && $dwqa_general_settings['enable-review-question']
  496.                         && $post_status != 'private' && ! current_user_can( 'manage_options' ) ) {
  497.                          $post_status = 'pending';
  498.                     }
  499.  
  500.                     $postarr = array(
  501.                         'comment_status' => 'open',
  502.                         'post_author'    => $user_id,
  503.                         'post_content'   => $content,
  504.                         'post_status'    => $post_status,
  505.                         'post_title'     => $title,
  506.                         'post_type'      => 'dwqa-question',
  507.                         'tax_input'      => array(
  508.                             'dwqa-question_category'    => array( $category ),
  509.                             'dwqa-question_tag'         => explode( ',', $tags )
  510.                         )
  511.                     );
  512.  
  513.                     do_action( 'dwqa_before_submit_question' );
  514.  
  515.                     if ( dwqa_count_notices( 'error' ) > 0 ) {
  516.                         return false;
  517.                     }
  518.  
  519.                     if ( apply_filters( 'dwqa-current-user-can-add-question', dwqa_current_user_can( 'post_question' ), $postarr ) ) {
  520.                         $new_question = $this->insert_question( $postarr );
  521.  
  522.                         update_post_meta( $new_question, '_dwqa_question_job', $job );
  523.                         update_post_meta( $new_question, '_dwqa_question_address', $address );
  524.  
  525.                     } else {
  526.                         //$dwqa_submit_question_errors->add( 'submit_question',  __( 'You do not have permission to submit question.', 'dwqa' ) );
  527.                         dwqa_add_notice( __( 'You do not have permission to submit question.', 'dwqa' ), 'error' );
  528.                         $new_question = $dwqa_submit_question_errors;
  529.                     }
  530.  
  531.                     if ( dwqa_count_notices( 'error' ) == 0 ) {
  532.                         if ( $is_anonymous ) {
  533.                             update_post_meta( $new_question, '_dwqa_anonymous_email', $question_author_email );
  534.                             update_post_meta( $new_question, '_dwqa_anonymous_name', $question_author_name );
  535.                             update_post_meta( $new_question, '_dwqa_is_anonymous', true );
  536.                             add_post_meta( $new_question, '_dwqa_followers', $question_author_email );
  537.                         } else {
  538.                             add_post_meta( $new_question, '_dwqa_followers', $user_id );
  539.                         }
  540.  
  541.                         if ( isset( $dwqa_options['enable-review-question'] ) && $dwqa_options['enable-review-question'] && !current_user_can( 'manage_options' ) && $post_status != 'private' ) {
  542.                             dwqa_add_notice( __( 'Your question is waiting moderator.', 'dwqa' ), 'success' );
  543.                         } else {
  544.                             exit( wp_safe_redirect( get_permalink( $new_question ) ) );
  545.                         }
  546.                     }
  547.                 } else {
  548.                     // $dwqa_submit_question_errors->add( 'submit_question', __( 'Captcha is not correct','dwqa' ) );
  549.                     dwqa_add_notice( __( 'Captcha is not correct', 'dwqa' ), 'error' );
  550.                 }
  551.             } else {
  552.                 // $dwqa_submit_question_errors->add( 'submit_question', __( 'Are you cheating huh?','dwqa' ) );
  553.                 dwqa_add_notice( __( 'Are you cheating huh?', 'dwqa' ), 'error' );
  554.             }
  555.             //$dwqa_current_error = $dwqa_submit_question_errors;
  556.         }
  557.     }
  558.  
  559.     public function update_question() {
  560.         if ( isset( $_POST['dwqa-edit-question-submit'] ) ) {
  561.             if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( esc_html( $_POST['_wpnonce'] ), '_dwqa_edit_question' ) ) {
  562.  
  563.                 $question_id = isset( $_POST['question_id'] ) ? $_POST['question_id'] : false;
  564.                 if ( !dwqa_current_user_can( 'edit_question', $question_id ) && !dwqa_current_user_can( 'manage_question' ) ) {
  565.                     dwqa_add_notice( __( "You do not have permission to edit question", 'dwqa' ), 'error' );
  566.                 }
  567.  
  568.                 $question_title = apply_filters( 'dwqa_prepare_edit_question_title', $_POST['question_title'] );
  569.                 if ( empty( $question_title ) ) {
  570.                     dwqa_add_notice( __( 'You must enter a valid question title.', 'dwqa' ), 'error' );
  571.                 }
  572.  
  573.                 if ( !$question_id ) {
  574.                     dwqa_add_notice( __( 'Question is missing.', 'dwqa' ), 'error' );
  575.                 }
  576.  
  577.                 if ( 'dwqa-question' !== get_post_type( $question_id ) ) {
  578.                     dwqa_add_notice( __( 'This post is not question.', 'dwqa' ), 'error' );
  579.                 }
  580.  
  581.                 $question_content = apply_filters( 'dwqa_prepare_edit_question_content', $_POST['question_content'] );
  582.  
  583.                 $tags = isset( $_POST['question-tag'] ) ? esc_html( $_POST['question-tag'] ): '';
  584.                 $category = isset( $_POST['question-category'] ) ? intval( $_POST['question-category'] ) : 0;
  585.                 if ( ! term_exists( $category, 'dwqa-question_category' ) ) {
  586.                     $category = 0;
  587.                 }
  588.  
  589.                 do_action( 'dwqa_prepare_update_question', $question_id );
  590.  
  591.                 if ( dwqa_count_notices( 'error' ) > 0 ) {
  592.                     return false;
  593.                 }
  594.  
  595.                 $args = array(
  596.                     'ID' => $question_id,
  597.                     'post_content' => $question_content,
  598.                     'post_title' => $question_title,
  599.                     'tax_input' => array(
  600.                         'dwqa-question_category' => array( $category ),
  601.                         'dwqa-question_tag'     => explode( ',', $tags )
  602.                     ),
  603.                 );
  604.  
  605.                 $new_question_id = wp_update_post( $args );
  606.  
  607.                 if ( !is_wp_error( $new_question_id ) ) {
  608.                     $old_post = get_post( $question_id );
  609.                     $new_post = get_post( $new_question_id );
  610.                     do_action( 'dwqa_update_question', $new_question_id, $old_post, $new_post );
  611.                     wp_safe_redirect( get_permalink( $new_question_id ) );
  612.                 } else {
  613.                     dwqa_add_wp_error_message( $new_question_id );
  614.                     return false;
  615.                 }
  616.             } else {
  617.                 dwqa_add_notice( __( 'Hello, Are you cheating huh?', 'dwqa' ), 'error' );
  618.                 return false;
  619.             }
  620.             exit(0);
  621.         }
  622.     }
  623.  
  624.     public function insert_question( $args ) {
  625.         if ( is_user_logged_in() ) {
  626.             $user_id = get_current_user_id();
  627.         } elseif ( dwqa_current_user_can( 'post_question' ) ) {
  628.             $user_id = 0;
  629.         } else {
  630.             return false;
  631.         }
  632.  
  633.         $args = wp_parse_args( $args, array(
  634.             'comment_status' => 'open',
  635.             'post_author'    => $user_id,
  636.             'post_content'   => '',
  637.             'post_status'    => 'pending',
  638.             'post_title'     => '',
  639.             'post_type'      => 'dwqa-question',
  640.         ) );
  641.  
  642.         $new_question = wp_insert_post( $args, true );
  643.         var_dump("expression");
  644.         die();
  645.         if ( ! is_wp_error( $new_question ) ) {
  646.  
  647.             if ( isset( $args['tax_input'] ) ) {
  648.                 foreach ( $args['tax_input'] as $taxonomy => $tags ) {
  649.                     wp_set_post_terms( $new_question, $tags, $taxonomy );
  650.                 }
  651.             }
  652.             update_post_meta( $new_question, '_dwqa_status', 'open' );
  653.             update_post_meta( $new_question, '_dwqa_views', 0 );
  654.             update_post_meta( $new_question, '_dwqa_votes', 0 );
  655.             update_post_meta( $new_question, '_dwqa_answers_count', 0 );
  656.             $date = get_post_field( 'post_date', $new_question );
  657.             // dwqa_log_last_activity_on_question( $new_question, 'Create question', $date );
  658.             //Call action when add question successfull
  659.             do_action( 'dwqa_add_question', $new_question, $user_id );
  660.         }
  661.         return $new_question;
  662.     }
  663. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement