public

  • Download Gist
gravityforms-update-post.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
2012-05-29 13:22:55.009 osascript[72787:903] Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found. Did find:
/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.
--- /Users/bushaw/Downloads/gravity-forms-update-post/gravityforms-update-post.php
+++ gravityforms-update-post.php
@@ -126,6 +126,22 @@
}
public function gform_pre_render($form) {
+ ?>
+ <script type="text/javascript" charset="utf-8">
+ jQuery(function($){
+ if ( $('.gfield_list_row_odd').length > 0 && $('.gfield_list_row_odd').children().length > 1 ) {
+ var lists = $('.gfield_list tbody tr');
+ lists.each(function(){
+ var children = $(this).children();
+ if ( children.length > 2 ) {
+ var values = $(children[0]).find('input').val().split('|');
+ children.each(function(i){ $(this).find('input').val( values[i] ); })
+ };
+ })
+ };
+ })
+ </script>
+ <?php
$this->options['request_id'] = apply_filters($this->name . '_id', $this->options['request_id']);
if ($this->is_allowed()) {
@@ -137,6 +153,9 @@
if (isset($this->post['object'][$field['type']])) {
$field = $this->gform_populate_element($field, $field_type, $this->post['object'][$field['type']]);
}
+ elseif ($field['type'] == 'post_custom_field' && isset($meta[$field['postCustomFieldName']]) && $field_type == 'list') {
+ $field = $this->gform_populate_element($field, $field_type, $meta[$field['postCustomFieldName']]);
+ }
elseif ($field['type'] == 'post_custom_field' && isset($meta[$field['postCustomFieldName']])) {
$field = $this->gform_populate_element($field, $field_type, end($meta[$field['postCustomFieldName']]));
}
@@ -184,7 +203,10 @@
}
}
}
- break;
+ break;
+ case 'list':
+ $field['defaultValue'] = $value;
+ break;
default:
if (is_array($value)) {
$value = implode(', ', $value);
@@ -199,7 +221,7 @@
if ($this->is_allowed() && !$this->is_delete()) {
// If a custom field is unique, delete the old value before we proceed
foreach ($form['fields'] as $field) {
- if ($field['type'] == 'post_custom_field' && isset($field['postCustomFieldUnique'])) {
+ if ( ($field['type'] == 'post_custom_field' && isset($field['postCustomFieldUnique'])) || ($field['type'] == 'post_custom_field' && $field['inputType'] == 'list') ) {
delete_post_meta($this->post['ID'], $field['postCustomFieldName']);
}
}
@@ -226,7 +248,7 @@
array_push($inputs, "input[name=input_$field_id]");
}
- $form_tag .= '<script type="text/javascript">jQuery(document).load(function(){ jQuery("' . implode(', ', $inputs) . '").trigger("click"); });</script>';
+ $form_tag .= '<script type="text/javascript">jQuery(document).load(function() { jQuery("' . implode(', ', $inputs) . '").trigger("click"); });</script>';
$this->form['conditional'] = array();
}
@@ -334,7 +356,7 @@
private function get_capability($type, $post_type) {
$capability = null;
- if (is_null($this->post['type_object'])) {
+ if (!isset($this->post['type_object']) || is_null($this->post['type_object'])) {
$this->post['type_object'] = get_post_type_object($this->post['object']['post_type']);
}
gravityforms-update-post.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
<?php
/*
Plugin Name: Gravity Forms - Update Post
Plugin URI: http://p51labs.com
Description: Allow Gravity Forms to Update Post Content and the data associated with it.
Version: 0.5.2
Author: Kevin Miller
Author URI: http://p51labs.com
Contributer: Ron Sparks
Contributer URI: http://ronsparks.net
 
------------------------------------------------------------------------
Copyright 2012 P51 Labs
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
$gform_update_post = new GFUpdatePost();
 
class GFUpdatePost {
public $options = array(
'request_id' => 'gform_post_id'
,'post_status' => 'default'
,'capabilities' => array(
'update' => 'default'
,'delete' => 'disable'
)
,'entries' => true
);
private $name = 'gform_update_post';
private $post = array(
'ID' => null
,'object' => null
,'type_object' => null
);
private $form = array(
'conditional' => array()
);
public function __construct() {
add_action('init', array(&$this, 'init'), 100);
}
public function init() {
$this->options = apply_filters($this->name . '_options', $this->options);
if (isset($_REQUEST[$this->options['request_id']]) && is_numeric($_REQUEST[$this->options['request_id']])) {
$this->get_post_object($_REQUEST[$this->options['request_id']]);
add_filter('gform_form_tag', array(&$this, 'gform_form_tag'), 50, 2);
add_filter('gform_submit_button', array(&$this, 'gform_submit_button'), 50, 2);
add_filter('gform_pre_render', array(&$this, 'gform_pre_render'));
add_filter('gform_confirmation', array(&$this, 'gform_confirmation_delete'), 10, 4);
add_filter('gform_validation', array(&$this, 'gform_validation'));
add_action('gform_post_data', array(&$this, 'gform_post_data'), 10, 2);
add_action('gform_post_submission', array(&$this, 'gform_post_submission'), 10, 2);
}
add_filter('gform_tooltips', array(&$this, 'gform_tooltips'));
add_action('gform_field_standard_settings', array(&$this, 'gform_field_standard_settings'), 10, 2);
add_action('gform_editor_js', array(&$this, 'gform_editor_js'));
}
public function gform_field_standard_settings($position, $form_id) {
if ($position == 700) :
?>
<li class="post_custom_field_unique field_setting">
<input type="checkbox" id="field_unique_custom_meta_value" onclick="SetFieldProperty('postCustomFieldUnique', this.checked);" />
<label for="field_unique_custom_meta_value" class="inline">
<?php _e('Unique Custom Field?'); ?>
<?php gform_tooltip('form_field_unique_custom_meta_value') ?>
</label>
</li>
<?php
endif;
}
public function gform_editor_js() {
?>
<script type="text/javascript">
var fieldTypes = [ 'post_custom_field' ];
for (var i = 0; i < fieldTypes.length; i++) {
fieldSettings[fieldTypes[i]] += ', .post_custom_field_unique';
}
jQuery(document).bind('gform_load_field_settings', function(event, field, form) {
jQuery('#field_unique_custom_meta_value').attr('checked', field['postCustomFieldUnique'] == true);
});
</script>
<?php
}
public function gform_tooltips($tooltips) {
$tooltips['form_field_unique_custom_meta_value'] = "<h6>Unique Meta Field</h6>Check this box to ensure this meta field is saved as unique.";
return $tooltips;
}
public function get_post_object($id) {
global $wp_post_types, $wp_taxonomies;
$this->post = array(
'ID' => $id
,'object' => get_post($id, ARRAY_A)
,'taxonomies' => array()
);
foreach ($wp_taxonomies as $taxonomy => $properties) {
if (in_array($this->post['object']['post_type'], $properties->object_type)) {
$this->post['taxonomies'][$taxonomy == 'post_tag' ? 'post_tags' : $taxonomy] = wp_get_object_terms($this->post['ID'], $taxonomy);
}
}
}
public function gform_pre_render($form) {
?>
<script type="text/javascript" charset="utf-8">
jQuery(function($){
if ( $('.gfield_list_row_odd').length > 0 && $('.gfield_list_row_odd').children().length > 1 ) {
var lists = $('.gfield_list tbody tr');
lists.each(function(){
var children = $(this).children();
if ( children.length > 2 ) {
var values = $(children[0]).find('input').val().split('|');
children.each(function(i){ $(this).find('input').val( values[i] ); })
};
})
};
})
</script>
<?php
$this->options['request_id'] = apply_filters($this->name . '_id', $this->options['request_id']);
if ($this->is_allowed()) {
$meta = get_post_custom($this->post['ID']);
foreach ($form['fields'] as &$field) {
$field_type = RGFormsModel::get_input_type($field);
if (isset($this->post['object'][$field['type']])) {
$field = $this->gform_populate_element($field, $field_type, $this->post['object'][$field['type']]);
}
elseif ($field['type'] == 'post_custom_field' && isset($meta[$field['postCustomFieldName']]) && $field_type == 'list') {
$field = $this->gform_populate_element($field, $field_type, $meta[$field['postCustomFieldName']]);
}
elseif ($field['type'] == 'post_custom_field' && isset($meta[$field['postCustomFieldName']])) {
$field = $this->gform_populate_element($field, $field_type, end($meta[$field['postCustomFieldName']]));
}
elseif (isset($this->post['taxonomies'][$field_type]) || (!empty($field['populateTaxonomy']) && $this->post['taxonomies'][$field['populateTaxonomy']])) {
$field = $this->gform_populate_element($field, $field_type, isset($this->post['taxonomies'][$field_type]) ? $this->post['taxonomies'][$field_type] : $this->post['taxonomies'][$field['populateTaxonomy']], 'name');
}
if (!empty($field['defaultValue']) && !empty($field['conditionalLogic']) && is_array($field['conditionalLogic'])) {
foreach ($field['conditionalLogic']['rules'] as $rule) {
if (!in_array($rule['fieldId'], $this->form['conditional'])) {
array_push($this->form['conditional'], $rule['fieldId']);
}
}
}
$field = apply_filters($this->name . '_field_default_value', $field);
}
}
return $form;
}
public function gform_populate_element($field, $field_type, $value, $value_index = false) {
if ($value_index) {
$new_value = array();
foreach ($value as $object) {
array_push($new_value, is_object($object) ? $object->$value_index : $object[$value_index]);
}
$value = $new_value;
}
$value = maybe_unserialize($value);
switch ($field_type) {
case 'select':
case 'multiselect':
case 'checkbox':
case 'radio':
$value = !is_array($value) ? array($value) : $value;
if (isset($field['choices'])) {
foreach ($field['choices'] as &$choice) {
foreach ($value as $term) {
if (($value_index && isset($term[$value_index]) && $term[$value_index] == $choice['text']) || $choice['text'] == $term || $term == $choice['value']) {
$choice['isSelected'] = true;
}
}
}
}
break;
case 'list':
$field['defaultValue'] = $value;
break;
default:
if (is_array($value)) {
$value = implode(', ', $value);
}
$field['defaultValue'] = $value;
break;
}
return $field;
}
public function gform_post_data($post_data, $form) {
if ($this->is_allowed() && !$this->is_delete()) {
// If a custom field is unique, delete the old value before we proceed
foreach ($form['fields'] as $field) {
if ( ($field['type'] == 'post_custom_field' && isset($field['postCustomFieldUnique'])) || ($field['type'] == 'post_custom_field' && $field['inputType'] == 'list') ) {
delete_post_meta($this->post['ID'], $field['postCustomFieldName']);
}
}
$post_data['ID'] = $this->post['ID'];
$post_data['post_type'] = $this->post['object']['post_type'];
$this->options['post_status'] = apply_filters($this->name . '_status', $this->options['post_status'], $form);
if (in_array($this->options['post_status'], array('draft', 'publish', 'pending', 'future', 'private', 'inherit'))) {
$post_data['post_status'] = $this->options['post_status'] == 'inherit' ? $this->post['object']['post_status'] : $this->options['post_status'];
}
}
return $post_data;
}
public function gform_form_tag($form_tag, $form) {
if ($this->is_allowed()) {
$form_tag .= '<input type="hidden" name="' . $this->options['request_id'] . '" value="' . $this->post['ID'] . '" class="gform_hidden" />';
if (!empty($this->form['conditional'])) {
$inputs = array();
foreach ($this->form['conditional'] as $field_id) {
array_push($inputs, "input[name=input_$field_id]");
}
$form_tag .= '<script type="text/javascript">jQuery(document).load(function() { jQuery("' . implode(', ', $inputs) . '").trigger("click"); });</script>';
$this->form['conditional'] = array();
}
}
return $form_tag;
}
public function gform_submit_button($button, $form) {
if ($this->options['capabilities']['delete'] != 'disable') {
$button .= apply_filters($this->name . '_delete_button', '<input type="submit" id="' . $this->name . '_delete_button_' . $form["id"] . '" name="' . $this->name . '_delete_button" class="button gform_button" value="' . __('Delete') . '" />', $form);
}
return $button;
}
public function gform_validation($validation_result) {
if ($this->is_delete()) {
$validation_result['is_valid'] = true;
foreach($validation_result['form']['fields'] as &$field) {
$field['failed_validation'] = false;
}
}
return $validation_result;
}
public function gform_post_submission($entry, $form) {
global $wpdb;
$this->options['entries'] = apply_filters($this->name . '_entries', $this->options['entries'], $form);
if (!$this->options['entries'] || $this->is_delete()) {
$tables = (object) array(
'lead_table' => RGFormsModel::get_lead_table_name()
,'lead_notes_table' => RGFormsModel::get_lead_notes_table_name()
,'lead_detail_table' => RGFormsModel::get_lead_details_table_name()
,'lead_detail_long_table' => RGFormsModel::get_lead_details_long_table_name()
);
$queries = array(
$wpdb->prepare("DELETE FROM $tables->lead_detail_long_table WHERE lead_detail_id IN (SELECT id FROM $tables->lead_detail_table WHERE lead_id = %d)", $entry['id'])
,$wpdb->prepare("DELETE FROM $tables->lead_detail_table WHERE lead_id = %d", $entry['id'])
,$wpdb->prepare("DELETE FROM $tables->lead_notes_table WHERE lead_id = %d", $entry['id'])
,$wpdb->prepare("DELETE FROM $tables->lead_table WHERE id = %d", $entry['id'])
);
foreach ($queries as $query) {
$wpdb->query($query);
}
}
if ($this->is_delete()) {
wp_delete_post($this->post['ID']);
}
// If a custom field is unique, get all the rows and combine them into one
foreach ($form['fields'] as $field) {
if ($field['type'] == 'post_custom_field' && isset($field['postCustomFieldUnique'])) {
$meta = get_post_meta($this->post['ID'], $field['postCustomFieldName']);
delete_post_meta($this->post['ID'], $field['postCustomFieldName']);
add_post_meta($this->post['ID'], $field['postCustomFieldName'], $meta, true);
}
}
}
public function gform_confirmation_delete($confirmation, $form, $lead, $ajax) {
if ($this->is_delete()) {
$confirmation = apply_filters($this->name . '_confirmation_delete', $confirmation);
}
return $confirmation;
}
private function is_allowed($type = 'update') {
if (!is_user_logged_in() || is_null($this->post['object']) || !in_array($type, array_keys($this->options['capabilities']))) {
return false;
}
$allowed = false;
switch ($this->options['capabilities'][$type]) {
case 'author':
$current_user = wp_get_current_user();
$allowed = $current_user->ID == $this->post['object']['post_author'];
break;
case 'default':
$allowed = current_user_can($this->get_capability($type, $this->post['object']['post_type']));
break;
default:
$allowed = current_user_can($this->options['capabilities'][$type]);
break;
}
return $allowed;
}
private function is_delete() {
return isset($_REQUEST[$this->name . '_delete_button']) && $this->is_allowed('delete');
}
private function get_capability($type, $post_type) {
$capability = null;
if (!isset($this->post['type_object']) || is_null($this->post['type_object'])) {
$this->post['type_object'] = get_post_type_object($this->post['object']['post_type']);
}
switch ($type) {
case 'update':
$capability = $this->post['type_object']->cap->edit_posts;
break;
case 'delete':
$capability = $this->post['type_object']->cap->delete_posts;
break;
}
return $capability;
}
private function pre($output) {
echo '<pre>';
print_r($output);
echo '</pre>';
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.