Advertisement
Guest User

Untitled

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