/** * Loop through the fields being edited and if they include Post fields, update the Entry's post object * * @param array $form Gravity Forms form * * @return void */ function maybe_update_post_fields($form) { $post_id = $this->entry['post_id']; // Security check if (false === GVCommon::has_cap('edit_post', $post_id)) { do_action('gravityview_log_error', 'The current user does not have the ability to edit Post #' . $post_id); return; } $update_entry = false; $updated_post = $original_post = get_post($post_id); foreach ($this->entry as $field_id => $value) { //todo: only run through the edit entry configured fields $field = RGFormsModel::get_field($form, $field_id); if (class_exists('GF_Fields')) { $field = GF_Fields::create($field); } if (GFCommon::is_post_field($field)) { // Get the value of the field, including $_POSTed value $value = RGFormsModel::get_field_value($field); // Convert the field object in 1.9 to an array for backward compatibility $field_array = GVCommon::get_field_array($field); switch ($field_array['type']) { case 'post_title': case 'post_content': case 'post_excerpt': $updated_post->{$field_array['type']} = $value; break; case 'post_tags': wp_set_post_tags($post_id, $value, false); break; case 'post_category': $categories = is_array($value) ? array_values($value) : (array) $value; $categories = array_filter($categories); wp_set_post_categories($post_id, $categories, false); // prepare value to be saved in the entry $field = GFCommon::add_categories_as_choices($field, ''); // if post_category is type checkbox, then value is an array of inputs if (isset($value[strval($field_id)])) { foreach ($value as $input_id => $val) { $input_name = 'input_' . str_replace('.', '_', $input_id); $this->entry[strval($input_id)] = RGFormsModel::prepare_value($form, $field, $val, $input_name, $this->entry['id']); } } else { $input_name = 'input_' . str_replace('.', '_', $field_id); $this->entry[strval($field_id)] = RGFormsModel::prepare_value($form, $field, $value, $input_name, $this->entry['id']); } break; case 'post_custom_field': $input_type = RGFormsModel::get_input_type($field); $custom_field_name = $field_array['postCustomFieldName']; // Only certain custom field types are supported if (!in_array($input_type, array('list', 'fileupload'))) { update_post_meta($post_id, $custom_field_name, $value); } break; case 'post_image': $value = ''; break; } //ignore fields that have not changed if ($value === rgget((string) $field_id, $this->entry)) { continue; } // update entry if ('post_category' !== $field->type) { $this->entry[strval($field_id)] = $value; } $update_entry = true; } } if ($update_entry) { $return_entry = GFAPI::update_entry($this->entry); if (is_wp_error($return_entry)) { do_action('gravityview_log_error', 'Updating the entry post fields failed', $return_entry); } else { do_action('gravityview_log_debug', 'Updating the entry post fields for post #' . $post_id . ' succeeded'); } } $return_post = wp_update_post($updated_post, true); if (is_wp_error($return_post)) { do_action('gravityview_log_error', 'Updating the post content failed', $return_post); } else { do_action('gravityview_log_debug', 'Updating the post content for post #' . $post_id . ' succeeded'); } }