예제 #1
0
 public static function process()
 {
     $output = [];
     theme_features::check_referer();
     theme_features::check_nonce();
     $type = isset($_GET['type']) && is_string($_GET['type']) ? $_GET['type'] : null;
     $post_id = isset($_POST['post-id']) && is_numeric($_POST['post-id']) ? (int) $_POST['post-id'] : null;
     if (!$post_id) {
         $output['status'] = 'error';
         $output['code'] = 'invaild_post_id';
         $output['msg'] = ___('Invaild post id param.');
         die(theme_features::json_format($output));
     }
     $post = theme_cache::get_post($post_id);
     if (!$post || $post->post_type !== 'post') {
         die(theme_features::json_format(['status' => 'error', 'code' => 'post_not_exist', 'msg' => ___('Post does not exist.')]));
     }
     /**
      * check user logged
      */
     if (!theme_cache::is_user_logged_in()) {
         $output['status'] = 'error';
         $output['code'] = 'need_login';
         $output['msg'] = '<a href="' . wp_login_url(theme_cache::get_permalink($post->ID)) . '" title="' . ___('Go to log-in') . '">' . ___('Sorry, please log-in.') . '</a>';
         die(theme_features::json_format($output));
     }
     $rater_id = theme_cache::get_current_user_id();
     switch ($type) {
         /**
          * incr point
          */
         case 'incr':
             /**
              * points
              */
             $points = isset($_POST['points']) && is_numeric($_POST['points']) ? (int) $_POST['points'] : null;
             if (!in_array($points, self::get_point_values())) {
                 $output['status'] = 'error';
                 $output['code'] = 'invaild_point_value';
                 $output['msg'] = ___('Invaild point value.');
                 die(theme_features::json_format($output));
             }
             /**
              * incr post raters
              */
             $post_raters = self::incr_post_raters($post_id, $rater_id, $points);
             if ($post_raters !== true) {
                 die(theme_features::json_format($post_raters));
             } else {
                 /**
                  * incr post points
                  */
                 $points_count = self::incr_post_points_count($post_id, $points);
                 if (!$points_count) {
                     $output['status'] = 'error';
                     $output['code'] = 'error_incr_points_count';
                     $output['msg'] = ___('Sorry, system can not increase post points count.');
                     die(theme_features::json_format($output));
                 }
                 /**
                  * incr rater posts
                  */
                 $rater_posts = self::incr_rater_posts($post_id, $rater_id, $points);
                 if ($rater_posts !== true) {
                     $output['status'] = 'error';
                     $output['code'] = 'error_incr_rater_posts';
                     $output['msg'] = ___('System can not increase rater posts.');
                     die(theme_features::json_format($output));
                 }
                 /**
                  * increase post author points
                  */
                 theme_custom_point::incr_user_points($post->post_author, $points);
                 /**
                  * add point history for rater
                  */
                 self::add_history_for_rater($post_id, $rater_id, $points);
                 /**
                  * add point history for post author
                  */
                 self::add_history_for_post_author($post_id, $rater_id, $points);
                 /**
                  * decrease rater points
                  */
                 theme_custom_point::decr_user_points($rater_id, $points);
                 /**
                  * success
                  */
                 $output['status'] = 'success';
                 $output['points'] = (int) self::get_post_points_count($post_id);
                 $output['msg'] = ___('Operation successful, thank you for your participation.');
                 die(theme_features::json_format($output));
             }
             break;
         default:
             $output['status'] = 'error';
             $output['code'] = 'invaild_type';
             $output['msg'] = ___('Invaild type param.');
             die(theme_features::json_format($output));
     }
     die(theme_features::json_format($output));
 }