GFCPTAddon1_5

GFCPT Addon class targetting version 1.5 of Gravity Forms.

Defined (1)

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

/gfcptaddon_1-5.php  
  1. class GFCPTAddon1_5 extends GFCPTAddonBase { 
  2.  
  3. /** 
  4. * Override. Include a couple more hooks 
  5. */ 
  6. public function init() { 
  7. //hook up the defaults 
  8. parent::init(); 
  9.  
  10. //then add these for 1.5... 
  11. //add our advanced options to the form builder 
  12. add_action('gform_field_advanced_settings', array(&$this, 'render_field_advanced_settings'), 10, 2); 
  13.  
  14. //include javascript for the form builder 
  15. add_action('gform_editor_js', array(&$this, 'render_editor_js')); 
  16.  
  17. // filter to add a new tooltip 
  18. add_filter('gform_tooltips', array(&$this, 'add_gf_tooltips')); 
  19.  
  20. /** 
  21. * Override. Gets the post type from our new field value 
  22. */ 
  23. function get_field_post_type( $field ) { 
  24. if (array_key_exists('populatePostType', $field)) { 
  25. return $field['populatePostType']; 
  26. } else { 
  27. return false; 
  28.  
  29. /** 
  30. * Override. Gets the taxonomy from our new field value 
  31. */ 
  32. function get_field_taxonomy( $field ) { 
  33. if (array_key_exists('populateTaxonomy', $field)) { 
  34. return $field['populateTaxonomy']; 
  35. } else if (array_key_exists('saveToTaxonomy', $field)) { 
  36. return $field['saveToTaxonomy']; 
  37. } else { 
  38. return false; 
  39.  
  40. /** 
  41. * Override. Gets the custom post type from the post title field value 
  42. */ 
  43. function get_form_post_type( $form ) { 
  44. foreach ( $form['fields'] as $field ) { 
  45. if ( $field['type'] == 'post_title' && $field['saveAsCPT'] ) 
  46. return $field['saveAsCPT']; 
  47. return false; 
  48.  
  49. function get_form_parent_post_id( $form ) { 
  50. foreach ( $form['fields'] as $field ) { 
  51. if ( $field['type'] == 'select' && $field['setParentPost'] ) { 
  52. $parent_id = RGForms::post('input_'.$field['id']); 
  53. return $parent_id; 
  54. return 0; 
  55.  
  56. /** 
  57. * Add tooltips for the new field values 
  58. */ 
  59. function add_gf_tooltips($tooltips) { 
  60. $tooltips["form_field_populate_post_type"] = "<h6>Populate with a Post Type</h6>Check this box to populate this field from a specific post type."; 
  61. $tooltips["form_field_set_parent_post"] = "<h6>Try to set parent</h6>If this is checked, and the form creates a post type, then the parent for the newly created post type will be set from the value of this field. Please note that this only works for heirarcical post typs e.g. pages"; 
  62. $tooltips["form_field_custom_taxonomy"] = "<h6>Populate with a Taxonomy</h6>Check this box to populate this field from a custom taxonomy."; 
  63. $tooltips["form_field_custom_post_type"] = "<h6>Save As Post Type</h6>Check this box to save this form to a specific post type."; 
  64. $tooltips["form_field_save_to_taxonomy"] = "<h6>Save To Taxonomy</h6>Check this box to save this field to a specific custom taxonomy. Please note that the taxonomy must NOT be hierarchical."; 
  65. $tooltips["form_field_tax_enhanced"] = "<h6>Enable Enhanced UI</h6>By selecting this option, this field will be tranformed into a 'tag input' control which makes it more user-friendly for selecting existing and capturing new taxonomies."; 
  66. return $tooltips; 
  67.  
  68. /** 
  69. * Add some advanced settings to the fields 
  70. */ 
  71. function render_field_advanced_settings($position, $form_id) { 
  72. if($position == 50) { 
  73. ?> 
  74. <li class="populate_with_taxonomy_field_setting field_setting" style="display:list-item;"> 
  75. <input type="checkbox" id="field_enable_populate_with_taxonomy" /> 
  76. <label for="field_enable_populate_with_taxonomy" class="inline"> 
  77. <?php _e("Populate with a Taxonomy", "gravityforms"); ?> 
  78. </label> 
  79. <?php gform_tooltip("form_field_custom_taxonomy") ?><br /> 
  80. <select id="field_populate_taxonomy" onchange="SetFieldProperty('populateTaxonomy', jQuery(this).val());" style="margin-top:10px; display:none;"> 
  81. <option value="" style="color:#999;">Select a Taxonomy</option> 
  82. <?php 
  83. $args=array( 
  84. 'public' => true,  
  85. '_builtin' => false 
  86. ); 
  87. $taxonomies = get_taxonomies($args, 'objects'); 
  88. foreach($taxonomies as $taxonomy): ?> 
  89. <option value="<?php echo $taxonomy->name; ?>"><?php echo $taxonomy->label; ?></option> 
  90. <?php endforeach; ?> 
  91. </select> 
  92. </li> 
  93. <li class="populate_with_post_type_field_setting field_setting" style="display:list-item;"> 
  94. <input type="checkbox" class="toggle_setting" id="field_enable_populate_with_post_type" /> 
  95. <label for="field_enable_populate_with_post_type" class="inline"> 
  96. <?php _e("Populate with a Post Type", "gravityforms"); ?> 
  97. </label> 
  98. <?php gform_tooltip("form_field_populate_post_type") ?><br /> 
  99. <div style="margin-top:10px; display:none;"> 
  100. <select id="field_populate_post_type" onchange="SetFieldProperty('populatePostType', jQuery(this).val());"> 
  101. <option value="" style="color:#999;">Select a Post Type</option> 
  102. <?php 
  103. $args=array( 
  104. 'public' => true 
  105. ); 
  106. $post_types = get_post_types($args, 'objects'); 
  107. foreach($post_types as $post_type): ?> 
  108. <option value="<?php echo $post_type->name; ?>"><?php echo $post_type->label; ?></option> 
  109. <?php endforeach; ?> 
  110. </select> 
  111. <input type="checkbox" class="check_parent" onclick="SetFieldProperty('setParentPost', this.checked);" id="field_set_parent_post" /> 
  112. <label for="field_set_parent_post" class="inline"> 
  113. <?php _e("Try to set parent", "gravityforms"); ?> 
  114. </label> 
  115. <?php gform_tooltip("form_field_set_parent_post") ?> 
  116. </div> 
  117. </li> 
  118. <li class="custom_post_type_field_setting field_setting" style="display:list-item;"> 
  119. <input type="checkbox" id="field_enable_custom_post_type" /> 
  120. <label for="field_enable_custom_post_type" class="inline"> 
  121. <?php _e("Save As Post Type", "gravityforms"); ?> 
  122. </label> 
  123. <?php gform_tooltip("form_field_custom_post_type") ?><br /> 
  124. <select id="field_populate_custom_post_type" onchange="SetFieldProperty('saveAsCPT', jQuery(this).val());" style="margin-top:10px; display:none;"> 
  125. <option value="" style="color:#999;">Select a Post Type</option> 
  126. <?php 
  127. $args=array( 
  128. 'public' => true 
  129. ); 
  130. $post_types = get_post_types($args, 'objects'); 
  131. foreach($post_types as $post_type): ?> 
  132. <option value="<?php echo $post_type->name; ?>"><?php echo $post_type->label; ?></option> 
  133. <?php endforeach; ?> 
  134. </select> 
  135. </li> 
  136. <li class="save_to_taxonomy_field_setting field_setting" style="display:list-item;"> 
  137. <input type="checkbox" class="toggle_setting" id="field_enable_save_to_taxonomy" /> 
  138. <label for="field_enable_save_to_taxonomy" class="inline"> 
  139. <?php _e("Save To Taxonomy", "gravityforms"); ?> 
  140. </label> 
  141. <?php gform_tooltip("form_field_save_to_taxonomy") ?> 
  142. <div style="margin-top:10px; display:none;"> 
  143. <select id="field_save_to_taxonomy" onchange="SetFieldProperty('saveToTaxonomy', jQuery(this).val());"> 
  144. <option value="" style="color:#999;">Select a Taxonomy</option> 
  145. <?php 
  146. $args=array( 
  147. 'public' => true,  
  148. '_builtin' => false 
  149. ); 
  150. $taxonomies = get_taxonomies($args, 'objects'); 
  151. foreach($taxonomies as $taxonomy): 
  152. if ($taxonomy->hierarchical === false) {?> 
  153. <option value="<?php echo $taxonomy->name; ?>"><?php echo $taxonomy->label; ?></option> 
  154. <?php } ?> 
  155. <?php endforeach; ?> 
  156. </select> 
  157. <input type="checkbox" class="check_tax_enhanced" onclick="SetFieldProperty('taxonomyEnhanced', this.checked);" id="field_tax_enhanced" /> 
  158. <label for="field_tax_enhanced" class="inline"> 
  159. <?php _e("Enable enhanced UI", "gravityforms"); ?> 
  160. </label> 
  161. <?php gform_tooltip("form_field_tax_enhanced") ?> 
  162. </div> 
  163. </li> 
  164. <?php 
  165.  
  166.  
  167. /** 
  168. * render some custom JS to get the settings to work 
  169. */ 
  170. function render_editor_js() { 
  171. ?> 
  172. <script type='text/javascript'> 
  173.  
  174. jQuery(document).bind("gform_load_field_settings", function(event, field, form) { 
  175. //only show taxonomy for selects and radios 
  176. var valid_types = new Array('select', 'radio', 'checkbox', 'multiselect'); 
  177.  
  178. //alert(field['type']); 
  179.  
  180. if(jQuery.inArray(field['type'], valid_types) != -1) { 
  181.  
  182. var $taxonomy_setting_container = jQuery(".populate_with_taxonomy_field_setting"); 
  183. //show the setting container! 
  184. $taxonomy_setting_container.show(); 
  185.  
  186. //get the saved taxonomy 
  187. var populateTaxonomy = (typeof field['populateTaxonomy'] != 'undefined' && field['populateTaxonomy'] != '') ? field['populateTaxonomy'] : false; 
  188.  
  189. if (populateTaxonomy != false) { 
  190. //check the checkbox if previously checked 
  191. $taxonomy_setting_container.find("input:checkbox").attr("checked", "checked"); 
  192. //set the select and show 
  193. $taxonomy_setting_container.find("select").val(populateTaxonomy).show(); 
  194. } else { 
  195. $taxonomy_setting_container.find("input:checkbox").removeAttr("checked"); 
  196. $taxonomy_setting_container.find("select").val('').hide(); 
  197.  
  198. if (field['type'] == 'select') { 
  199. var $populate_post_type_container = jQuery(".populate_with_post_type_field_setting"); 
  200. $populate_post_type_container.show(); 
  201.  
  202. //get the saved post type 
  203. var populatePostType = (typeof field['populatePostType'] != 'undefined' && field['populatePostType'] != '') ? field['populatePostType'] : false; 
  204.  
  205. if (populatePostType != false) { 
  206. //check the checkbox if previously checked 
  207. $populate_post_type_container.find("input.toggle_setting").attr("checked", "checked"); 
  208. //set the select 
  209. $populate_post_type_container.find("select").val(populatePostType); 
  210. //show the div 
  211. $populate_post_type_container.find("div").show(); 
  212.  
  213. //get the saved check for setting the parent post 
  214. var setParent = (typeof field['setParentPost'] != 'undefined' && field['setParentPost'] != '') ? field['setParentPost'] : false; 
  215. if (setParent != false) { 
  216. $populate_post_type_container.find(".check_parent").attr("checked", "checked"); 
  217. } else { 
  218. $populate_post_type_container.find(".check_parent").removeAttr("checked"); 
  219. } else { 
  220. $taxonomy_setting_container.find("input.toggle_setting").removeAttr("checked"); 
  221. $taxonomy_setting_container.find("select").val(''); 
  222.  
  223.  
  224. } else if (field['type'] == 'post_title') { 
  225. var $cpt_setting_container = jQuery(".custom_post_type_field_setting"); 
  226.  
  227. $cpt_setting_container.show(); 
  228.  
  229. var saveAsCPT = (typeof field['saveAsCPT'] != 'undefined' && field['saveAsCPT'] != '') ? field['saveAsCPT'] : false; 
  230.  
  231. if (saveAsCPT != false) { 
  232. //check the checkbox if previously checked 
  233. $cpt_setting_container.find("input:checkbox").attr("checked", "checked"); 
  234. //set the select and show 
  235. $cpt_setting_container.find("select").val(saveAsCPT).show(); 
  236. } else { 
  237. $cpt_setting_container.find("input:checkbox").removeAttr("checked"); 
  238. $cpt_setting_container.find("select").val('').hide(); 
  239. } else if (field['type'] == 'text') { 
  240. var $tax_setting_container = jQuery('.save_to_taxonomy_field_setting'); 
  241.  
  242. $tax_setting_container.show(); 
  243.  
  244. var saveToTax = (typeof field['saveToTaxonomy'] != 'undefined' && field['saveToTaxonomy'] != '') ? field['saveToTaxonomy'] : false; 
  245.  
  246. if (saveToTax != false) { 
  247. //check the checkbox if previously checked 
  248. $tax_setting_container.find("input.toggle_setting").attr("checked", "checked"); 
  249. //set the select 
  250. $tax_setting_container.find("select").val(saveToTax); 
  251. //show the div 
  252. $tax_setting_container.find("div").show(); 
  253.  
  254. //get the saved check for using enhanced UI 
  255. var useEnhancedUI = (typeof field['taxonomyEnhanced'] != 'undefined' && field['taxonomyEnhanced'] != '') ? field['taxonomyEnhanced'] : false; 
  256. if (useEnhancedUI != false) { 
  257. $tax_setting_container.find(".check_tax_enhanced").attr("checked", "checked"); 
  258. } else { 
  259. $tax_setting_container.find(".check_tax_enhanced").removeAttr("checked"); 
  260.  
  261. } else { 
  262. $tax_setting_container.find("input.toggle_setting").removeAttr("checked"); 
  263. $tax_setting_container.find("div").hide(); 
  264. $tax_setting_container.find(".check_tax_enhanced").removeAttr("checked"); 
  265. $tax_setting_container.find("select").val(''); 
  266. }); 
  267.  
  268. jQuery(".populate_with_taxonomy_field_setting input:checkbox").click(function() { 
  269. var checked = jQuery(this).is(":checked"); 
  270. var $select = jQuery(this).parent(".populate_with_taxonomy_field_setting:first").find("select"); 
  271. if(checked) { 
  272. $select.slideDown(); 
  273.  
  274. //uncheck post type 
  275. var $pt_container = jQuery(this).parents("ul:first").find(".populate_with_post_type_field_setting:first"); 
  276. var $pt_check = $pt_container.find("input.toggle_setting"); 
  277. var $pt_div = $pt_container.find("div"); 
  278. if ($pt_check.is(":checked")) { 
  279.  
  280. SetFieldProperty('populatePostType', ''); 
  281. $pt_div.slideUp(); 
  282. $pt_check.removeAttr('checked'); 
  283.  
  284.  
  285. } else { 
  286. SetFieldProperty('populateTaxonomy', ''); 
  287. $select.slideUp(); 
  288. }); 
  289.  
  290. jQuery(".custom_post_type_field_setting input:checkbox").click(function() { 
  291. var checked = jQuery(this).is(":checked"); 
  292. var $select = jQuery(this).parent(".custom_post_type_field_setting:first").find("select"); 
  293. if(checked) { 
  294. $select.slideDown(); 
  295. } else { 
  296. SetFieldProperty('saveAsCPT', ''); 
  297. $select.slideUp(); 
  298. }); 
  299.  
  300. jQuery(".populate_with_post_type_field_setting .toggle_setting").click(function() { 
  301. var checked = jQuery(this).is(":checked"); 
  302. var $div = jQuery(this).parent(".populate_with_post_type_field_setting:first").find("div"); 
  303. if(checked) { 
  304. $div.slideDown(); 
  305. //uncheck taxonomy 
  306. var $tax_container = jQuery(this).parents("ul:first").find(".populate_with_taxonomy_field_setting:first"); 
  307. var $tax_check = $tax_container.find("input:checkbox"); 
  308. var $tax_select = $tax_container.find("select"); 
  309. if ($tax_check.is(":checked")) { 
  310.  
  311. SetFieldProperty('populateTaxonomy', ''); 
  312. $tax_select.slideUp(); 
  313. $tax_check.removeAttr('checked'); 
  314.  
  315.  
  316. } else { 
  317. SetFieldProperty('populatePostType', ''); 
  318. $div.slideUp(); 
  319. }); 
  320.  
  321. jQuery(".save_to_taxonomy_field_setting .toggle_setting").click(function() { 
  322. var checked = jQuery(this).is(":checked"); 
  323. var $div = jQuery(this).parent(".save_to_taxonomy_field_setting:first").find("div"); 
  324. if(checked) { 
  325. $div.slideDown(); 
  326. } else { 
  327. SetFieldProperty('saveToTaxonomy', ''); 
  328. $div.slideUp(); 
  329. }); 
  330.  
  331. </script> 
  332. <?php 
  333.