GFCPTAddonBase

Base class for the GFCPT Addon.

Defined (1)

The class is defined in the following location(s).

/gfcptaddonbase.php  
  1. class GFCPTAddonBase { 
  2.  
  3. protected $_has_tag_inputs = false; 
  4. protected $_included_js; 
  5. protected $_tag_inputs = array(); 
  6. protected $_tag_map = array(); 
  7. protected $_tag_terms = array(); 
  8.  
  9.  
  10. /** 
  11. * Main initilize method for wiring up all the hooks 
  12. */ 
  13. public function init() { 
  14. //alter the way forms are rendered by inserting taxomony dropdowns, radios and checkboxes 
  15. add_filter('gform_pre_render' , array(&$this, 'setup_form') ); 
  16.  
  17. //alter the way forms are rendered by the admin too! 
  18. add_filter('gform_admin_pre_render' , array(&$this, 'setup_form') ); 
  19.  
  20. //alter the form for submission - this is mainly for checkboxes 
  21. add_filter('gform_pre_submission_filter', array(&$this, 'setup_form') ); 
  22.  
  23. //set the post type when saving a post 
  24. add_filter("gform_post_data", array(&$this, 'set_post_values'), 10, 2); 
  25.  
  26. //intercept the form save and save any taxonomy links if needed 
  27. add_action('gform_post_submission', array(&$this, 'save_taxonomies'), 10, 2); 
  28.  
  29. //enqueue scripts to the page 
  30. add_action('gform_enqueue_scripts', array(&$this, 'enqueue_custom_scripts'), 10, 2); 
  31.  
  32. add_action('wp_print_scripts', array(&$this, 'enqueue_scripts'), 10, 2); 
  33.  
  34. add_filter("gform_preview_styles", array(&$this, 'preview_print_styles'), 10, 2); 
  35.  
  36. /** 
  37. * Setup the form with any taxonomies etc 
  38. */ 
  39. function setup_form( $form ) { 
  40.  
  41. //loop thru all fields 
  42. foreach($form['fields'] as &$field) { 
  43.  
  44. //see if the field is using a taxonomy 
  45. $taxonomy = $this->get_field_taxonomy( $field ); 
  46.  
  47. if($taxonomy) { 
  48. $this->setup_taxonomy_field( $field, $taxonomy ); 
  49. continue; 
  50.  
  51. //if its a select then check if we have set a post type 
  52. if ($field['type'] == 'select') { 
  53.  
  54. $post_type = $this->get_field_post_type( $field ); 
  55.  
  56. if ($post_type) { 
  57. $this->setup_post_type_field( $field, $post_type ); 
  58. continue; 
  59.  
  60.  
  61. return $form; 
  62.  
  63. function enqueue_scripts() { 
  64. if ($this->_has_tag_inputs) { 
  65. $script_block = ''; 
  66. if (sizeof($this->_tag_inputs)>0) { 
  67. $script_block = 'var gfcpt_tag_inputs = {"tag_inputs": ['; 
  68. $input_ids = array(); 
  69. foreach($this->_tag_inputs as $input_id => $taxonomy) { 
  70. $input_ids[] = '{input: "'.$input_id.'", taxonomy: "'.$taxonomy.'"}'; 
  71. $script_block .= implode(', ', $input_ids); 
  72. $script_block .= ']}; 
  73. '; 
  74.  
  75. if (sizeof($this->_tag_terms)>0) { 
  76. $script_block .= 'var gfcpt_tag_taxonomies = []; 
  77. '; 
  78. foreach($this->_tag_terms as $taxonomy => $terms) { 
  79. $script_block .= 'gfcpt_tag_taxonomies["'.$taxonomy.'"] = ["'.implode('", "', $terms).'"]; 
  80. '; 
  81. if (strlen($script_block) > 0) { 
  82. ?> 
  83. <script type='text/javascript'> 
  84. <?php 
  85. echo $script_block; 
  86. ?> 
  87. </script> 
  88. <?php 
  89.  
  90. function preview_print_styles($styles, $form) { 
  91. return array('gfcpt_jquery_ui_theme', 'gfcpt_tagit_css'); 
  92.  
  93. function enqueue_custom_scripts($form, $is_ajax) { 
  94. //loop thru all fields 
  95. foreach($form['fields'] as &$field) { 
  96. //if its a text field, see if we have set it to save to a taxonomy 
  97. if ($field['type'] == 'text' && array_key_exists('saveToTaxonomy', $field)) { 
  98. $saveToTaxonomy = $field['saveToTaxonomy']; 
  99.  
  100. if (taxonomy_exists($saveToTaxonomy) && array_key_exists('taxonomyEnhanced', $field)) { 
  101. if ($field['taxonomyEnhanced']) { 
  102.  
  103. $this->_has_tag_inputs = true; 
  104.  
  105. $tag_input_id = '#input_'.$form['id'].'_'.$field['id']; 
  106.  
  107. $this->_tag_inputs[$tag_input_id] = $saveToTaxonomy; 
  108.  
  109. if ( !array_key_exists($saveToTaxonomy, $this->_tag_terms) ) { 
  110. //get the existing taxonomies and add them to an array to render later 
  111. $terms = get_terms($saveToTaxonomy, 'orderby=name&hide_empty=0&fields=names'); 
  112. $this->_tag_terms[$saveToTaxonomy] = $terms; 
  113.  
  114. if (!$this->_included_js) { 
  115.  
  116. //enqueue some scripts for the enhaced UI 
  117. $this->_included_js = true; 
  118.  
  119. wp_register_style( 
  120. $handle = 'gfcpt_jquery_ui_theme',  
  121. $src = plugins_url( 'css/custom/jquery-ui-1.8.16.custom.css' , __FILE__ ) ); 
  122. wp_enqueue_style('gfcpt_jquery_ui_theme'); 
  123.  
  124. wp_register_style( 
  125. $handle = 'gfcpt_tagit_css',  
  126. $src = plugins_url( 'css/jquery.tagit.css' , __FILE__ ) ); 
  127. wp_enqueue_style('gfcpt_tagit_css'); 
  128.  
  129.  
  130. wp_register_script( 
  131. $handle = 'gfcpt_jquery_ui',  
  132. $src = plugins_url( 'js/jquery-ui-1.8.16.custom.min.js' , __FILE__ ),  
  133. $deps = array('jquery') ); 
  134.  
  135. wp_enqueue_script('gfcpt_jquery_ui'); 
  136.  
  137. wp_register_script( 
  138. $handle = 'gfcpt_tagit_js',  
  139. $src = plugins_url( 'js/tag-it.js' , __FILE__ ),  
  140. $deps = array('gfcpt_jquery_ui') ); 
  141.  
  142. wp_enqueue_script('gfcpt_tagit_js'); 
  143.  
  144. wp_register_script( 
  145. $handle = 'gfcpt_tagit_init_js',  
  146. $src = plugins_url( 'js/tag-it.init.js' , __FILE__ ),  
  147. $deps = array('gfcpt_tagit_js') ); 
  148.  
  149. wp_enqueue_script('gfcpt_tagit_init_js'); 
  150.  
  151. }  
  152.  
  153. /** 
  154. * Set the post values (if neccessary) 
  155. */ 
  156. function set_post_values( $post_data, $form ) { 
  157.  
  158. //check if the form saves a post 
  159. if ( $this->is_form_a_post_form($form) ) { 
  160. $target_post_type = $this->get_form_post_type( $form ); 
  161.  
  162. if ($target_post_type) 
  163. $post_data["post_type"] = $target_post_type; 
  164.  
  165. //then check if we have set a parent 
  166. $parent_post_id = $this->get_form_parent_post_id( $form ); 
  167.  
  168. if ($parent_post_id > 0) { 
  169. $post_data["post_parent"] = $parent_post_id; 
  170. return $post_data; 
  171.  
  172.  
  173. /** 
  174. * Checks if a form includes a 'post field' 
  175. */ 
  176. function is_form_a_post_form( $form ) { 
  177. foreach ($form["fields"] as $field) { 
  178. if(in_array($field["type"],  
  179. array("post_category", "post_title", "post_content",  
  180. "post_excerpt", "post_tags", "post_custom_fields", "post_image"))) 
  181. return true; 
  182. return false; 
  183.  
  184. /** 
  185. * override this to get the post type for a form 
  186. */ 
  187. function get_form_post_type( $form ) { 
  188. return null; 
  189.  
  190. /** 
  191. * override this to get the taxonomy for a field 
  192. */ 
  193. function get_field_taxonomy( $field ) { 
  194. return null; 
  195.  
  196. /** 
  197. * override this to get the post type for a field 
  198. */ 
  199. function get_field_post_type( $field ) { 
  200. return null; 
  201.  
  202. /** 
  203. * override this to get the parent Id for a form 
  204. */ 
  205. function get_form_parent_post_id( $form ) { 
  206. return 0; 
  207.  
  208. /** 
  209. * setup a field if it is linked to a post type 
  210. */ 
  211. function setup_post_type_field( &$field, $post_type ) { 
  212. $first_choice = $field['choices'][0]['text']; 
  213. $field['choices'] = $this->load_post_type_choices( $post_type, $first_choice ); 
  214.  
  215. function load_post_type_choices($post_type, $first_choice = '') { 
  216. $posts = $this->load_posts_hierarchical( $post_type ); 
  217. if ($first_choice === '' || $first_choice === 'First Choice') { 
  218. // if no default option is specified, dynamically create based on post type name 
  219. $post_type_obj = get_post_type_object($post_type); 
  220. $choices[] = array('text' => "-- select a {$post_type_obj->labels->singular_name} --", 'value' => ''); 
  221. } else { 
  222. $choices[] = array('text' => $first_choice, 'value' => ''); 
  223.  
  224. foreach($posts as $post) { 
  225. $choices[] = array('value' => $post->ID, 'text' => $post->post_title); 
  226.  
  227. return $choices; 
  228.  
  229. /** 
  230. * Get a hierarchical list of posts 
  231. */ 
  232. function load_posts_hierarchical( $post_type ) { 
  233. $args = array( 
  234. 'post_type' => $post_type,  
  235. 'numberposts' => -1,  
  236. 'orderby' => 'title',  
  237. 'post_status' => 'publish' 
  238. ); 
  239. $posts = get_posts( $args ); 
  240. return $this->walk_posts( $posts ); 
  241.  
  242. /** 
  243. * Helper function to recursively 'walk' the posts 
  244. */ 
  245. function walk_posts( $input_array, $parent_id=0, &$out_array=array(), $level=0 ) { 
  246. foreach ( $input_array as $item ) { 
  247. if ( $item->post_parent == $parent_id ) { 
  248. $item->post_title = str_repeat('--', $level) . $item->post_title; 
  249. $out_array[] = $item; 
  250. $this->walk_posts( $input_array, $item->ID, $out_array, $level+1 ); 
  251. return $out_array; 
  252.  
  253. /** 
  254. * setup a field if it is linked to a taxonomy 
  255. */ 
  256. function setup_taxonomy_field( &$field, $taxonomy ) { 
  257. $first_choice = $field['choices'][0]['text']; 
  258. $field['choices'] = $this->load_taxonomy_choices( $taxonomy, $field['type'], $first_choice ); 
  259.  
  260. //now check if we are dealing with a checkbox list and do some extra magic 
  261. if ( $field['type'] == 'checkbox' ) { 
  262. //clear the inputs first 
  263. $field['inputs'] = array(); 
  264.  
  265. $counter = 0; 
  266. //recreate the inputs so they are captured correctly on form submission 
  267. foreach( $field['choices'] as $choice ) { 
  268. $counter++; 
  269. if ( ($counter % 10) == 0 ) $counter++; //thanks to Peter Schuster for the help on this fix 
  270. $id = floatval( $field['id'] . '.' . $counter ); 
  271. $field['inputs'][] = array('label' => $choice['text'], 'id' => $id); 
  272.  
  273. /** 
  274. * Load any taxonomy terms 
  275. */ 
  276. function load_taxonomy_choices($taxonomy, $type, $first_choice = '') { 
  277. $choices = array(); 
  278.  
  279. if ($type === 'select') { 
  280. $terms = $this->load_taxonomy_hierarchical( $taxonomy ); 
  281. if ($first_choice === '' || $first_choice === 'First Choice') { 
  282. // if no default option is specified, dynamically create based on taxonomy name 
  283. $taxonomy = get_taxonomy($taxonomy); 
  284. $choices[] = array('text' => "-- select a {$taxonomy->labels->singular_name} --", 'value' => ''); 
  285. } else { 
  286. $choices[] = array('text' => $first_choice, 'value' => ''); 
  287. } else { 
  288. $terms = get_terms($taxonomy, 'orderby=name&hide_empty=0'); 
  289.  
  290. if ( !array_key_exists("errors", $terms) ) { 
  291. foreach($terms as $term) { 
  292. $choices[] = array('value' => $term->term_id, 'text' => $term->name); 
  293.  
  294. return $choices; 
  295.  
  296. /** 
  297. * Get a hierarchical list of taxonomies 
  298. */ 
  299. function load_taxonomy_hierarchical( $taxonomy ) { 
  300. $args = array( 
  301. 'taxonomy' => $taxonomy,  
  302. 'orderby' => 'name',  
  303. 'hierarchical' => 1,  
  304. 'hide_empty' => 0 
  305. ); 
  306. $terms = get_categories( $args ); 
  307.  
  308. if ( array_key_exists("errors", $terms) ) { 
  309. return $terms; 
  310. else 
  311. return $this->walk_terms( $terms ); 
  312.  
  313. /** 
  314. * Helper function to recursively 'walk' the taxonomy terms 
  315. */ 
  316. function walk_terms( $input_array, $parent_id=0, &$out_array=array(), $level=0 ) { 
  317. foreach ( $input_array as $item ) { 
  318. if ( $item->parent == $parent_id ) { 
  319. $item->name = str_repeat('--', $level) . $item->name; 
  320. $out_array[] = $item; 
  321. $this->walk_terms( $input_array, $item->term_id, $out_array, $level+1 ); 
  322. return $out_array; 
  323.  
  324. /** 
  325. * Loop through all fields and save any linked taxonomies 
  326. */ 
  327. function save_taxonomies( $entry, $form ) { 
  328. // Check if the submission contains a WordPress post 
  329. if ( isset ( $entry['post_id'] ) ) { 
  330.  
  331. foreach( $form['fields'] as &$field ) { 
  332.  
  333. $taxonomy = $this->get_field_taxonomy( $field ); 
  334.  
  335. if ( !$taxonomy ) continue; 
  336.  
  337. $this->save_taxonomy_field( $field, $entry, $taxonomy ); 
  338.  
  339. /** 
  340. * Save linked taxonomies for a sinle field 
  341. */ 
  342. function save_taxonomy_field( &$field, $entry, $taxonomy ) { 
  343. if ( array_key_exists( 'type', $field ) && $field['type'] == 'checkbox' ) { 
  344. $term_ids = array(); 
  345. foreach ( $field['inputs'] as $input ) { 
  346. $term_id = (int) $entry[ (string) $input['id'] ]; 
  347. if ( $term_id > 0 ) 
  348. $term_ids[] = $term_id; 
  349. if ( !empty ( $term_ids )) 
  350. wp_set_object_terms( $entry['post_id'], $term_ids, $taxonomy, true ); 
  351. } else if ( array_key_exists( 'type', $field ) && $field['type'] == 'text' ) { 
  352. $terms = $entry[$field['id']]; 
  353. if ( !empty($terms) ) 
  354. wp_set_post_terms( $entry['post_id'], $terms, $taxonomy ); 
  355. } else { 
  356. $term_id = (int) $entry[$field['id']]; 
  357. if ( $term_id > 0 ) 
  358. wp_set_object_terms( $entry['post_id'], $term_id, $taxonomy, true );