PageRenderTime 56ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/gravity-forms-update-post/gravityforms-update-post.php

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