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; }