function save($post, $nonce, $attachment = null)
 {
     require_once WPPR_PLUGIN_DIR . '/models/group-model.php';
     $model = new Group_Model();
     $has_attachment = false;
     if (isset($post)) {
         if (isset($nonce) && wp_verify_nonce($nonce, 'form_group')) {
             isset($post['group_id']) && !empty($post['group_id']) ? $is_update = true : ($is_update = false);
             $model->post_slug = sanitize_title($post['group_name']);
             $postdata = array('post_title' => $post['group_name'], 'post_content' => $post['description'], 'post_status' => 'publish', 'post_author' => $this->user_id, 'post_type' => 'groups', 'post_name' => $model->post_slug);
             $meta_location = $post['location'];
             $meta_is_private = !isset($post['is_private']) ? 0 : $post['is_private'];
             if ($is_update) {
                 $post_id = $post['group_id'];
                 $ID = array('ID' => $post_id);
                 $postdata = array_merge($postdata, $ID);
                 $details = get_post($post_id);
                 $post_name = $details->post_name;
                 if ($post_name != $model->post_slug) {
                     //verify new group name is not exists
                     if (!$model->is_group_exist()) {
                         $result = wp_update_post($postdata);
                         update_post_meta($post_id, '_group_location', $meta_location);
                         update_post_meta($post_id, '_is_private', $meta_is_private);
                         $status_code = 0;
                         $status_msg = 'Your group was successfully updated.';
                     } else {
                         $status_code = 1;
                         $status_msg = 'The group name is no longer available. Please try a new one.';
                     }
                 } else {
                     $result = wp_update_post($postdata);
                     update_post_meta($post_id, '_group_location', $meta_location);
                     update_post_meta($post_id, '_is_private', $meta_is_private);
                     $status_code = 0;
                     $status_msg = 'Your group was successfully updated.';
                 }
             } else {
                 if (!$model->is_group_exist()) {
                     $post_id = wp_insert_post($postdata);
                     $result = $model->add_group_member($post_id, $this->user_id, 1, 1);
                     //group_id, user_id, is_approved, is_admin
                     add_post_meta($post_id, '_group_location', $meta_location, true);
                     add_post_meta($post_id, '_group_total', 1, true);
                     add_post_meta($post_id, '_is_private', $meta_is_private, true);
                     $status_code = 0;
                     $status_msg = 'Your new group was successfully created.';
                 } else {
                     $status_code = 1;
                     $status_msg = 'The group name is no longer available. Please try a new one.';
                 }
             }
             //Upload and attach image
             if (isset($attachment) && @$attachment['name']) {
                 $has_attachment = true;
                 $upload_folder = get_option('pr_group_logo_path');
                 $valid_image = $this->verify_image($attachment);
                 if ($valid_image) {
                     $uploaded_file = $this->process_image($attachment);
                     if ($uploaded_file !== false) {
                         $this->attach_image($post_id, $uploaded_file, $is_update);
                     }
                 }
             }
             if ($has_attachment && (!$valid_image || $uploaded_file === false)) {
                 $status_msg .= 'However, the image file is invalid or not uploaded successfully.';
             }
         }
         $result = array('status_code' => $status_code, 'status_msg' => $status_msg);
     }
     return $result;
 }
Ejemplo n.º 2
0
function join_group()
{
    if (isset($_POST['group_id']) && intval($_POST['group_id'])) {
        require_once WP_PLUGIN_DIR . '/pr-membership/models/group-model.php';
        $model = new Group_Model();
        check_ajax_referer('pr-join-a-group-event', 'security');
        $group_id = $_POST['group_id'];
        $user_id = get_current_user_id();
        $group_privacy = get_post_meta($group_id, '_is_private', true);
        $total_member = get_post_meta($group_id, '_group_total', true);
        if ($group_privacy == 1) {
            $approval = 0;
            $total_member = $total_member;
        } else {
            $approval = 1;
            $total_member = $total_member + 1;
        }
        // $group_id, $user_id, $is_admin
        $result = $model->add_group_member($group_id, $user_id, $approval);
        if ($result) {
            if ($group_privacy == 0) {
                update_post_meta($group_id, '_group_total', $total_member + 1, $total_member);
            }
            $result_code = 0;
            $result_msg = "success";
        } else {
            $result_code = 1;
            $result_msg = "failed";
        }
        wp_send_json(array('result_code' => $result_code, 'result_msg' => $result_msg, 'group_id' => $group_id, 'group_privacy' => $group_privacy, 'member_total' => $total_member));
        wp_die();
    }
}