GF_CustomPostTypeMappings

The Gravity Forms Custom Post Type Mappings GF CustomPostTypeMappings class.

Defined (1)

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

/gravity-forms-custom-post-type-mappings.php  
  1. class GF_CustomPostTypeMappings { 
  2.  
  3. private $_plugin_name; //the name of the plugin 
  4.  
  5. //PHP 4 constructor 
  6. function GF_CustomPostTypeMappings() { 
  7. $this->__construct(); 
  8.  
  9. function __construct() { 
  10. $this->init(); 
  11.  
  12. function gf_populate_taxonomy_dropdowns($form) { 
  13. foreach($form['fields'] as &$field) { 
  14. $classes = explode(' ', $field['cssClass']); 
  15.  
  16. foreach($classes as $class) {  
  17. //check if the class starts with 'taxomony-' 
  18. if($this->starts_with($class, 'taxonomy-')) { 
  19. $field['type'] = 'select'; 
  20. $taxonomy = str_replace('taxonomy-', '', $class); 
  21. $field['choices'] = $this->gf_get_taxonomy_dropdown_choices($taxonomy); 
  22.  
  23. return $form; 
  24.  
  25. function gf_get_taxonomy_dropdown_choices($taxonomy, $withEmpty = true) { 
  26. $terms = get_terms($taxonomy, 'hide_empty=0'); 
  27.  
  28. $items = array(); 
  29.  
  30. if($withEmpty) { 
  31. $items[] = array('text' => '--select--', 'value' => ''); 
  32.  
  33. foreach($terms as $term) { 
  34. $items[] = array('value' => $term->term_id, 'text' => $term->name); 
  35.  
  36. return $items; 
  37.  
  38. function gf_set_post_type($post_data, $form) { 
  39. //check if the form we are dealing with saves a post 
  40. if($this->gf_is_form_a_post_form($form)) { 
  41. $classes = explode(' ', $form['cssClass']); 
  42.  
  43. //loop through the classes and see if we have set the posttype class 
  44. foreach($classes as $class) { 
  45. //check if the class starts with 'posttype-' 
  46. if($this->starts_with($class, 'posttype-')) { 
  47. $target_post_type = str_replace('posttype-', '', $class); 
  48.  
  49. if (!isset($target_post_type)) { 
  50. //lastly check if we have saved the mapping in an option 
  51. $options = get_option( $this->_plugin_name ); 
  52.  
  53. $target_post_type = $options["gf_".$form["id"]]; 
  54.  
  55. if (isset($target_post_type)) 
  56. $post_data["post_type"] = $target_post_type; 
  57. return $post_data; 
  58.  
  59. function gf_is_form_a_post_form($form) { 
  60. foreach ($form["fields"] as $field) { 
  61. if(in_array($field["type"],  
  62. array("post_category", "post_title", "post_content",  
  63. "post_excerpt", "post_tags", "post_custom_fields", "post_image"))) 
  64. return true; 
  65. return false; 
  66.  
  67. function gf_save_taxonomy($lead, $form) { 
  68. // Check if the submission contains a WordPress post 
  69. if(isset($lead['post_id'])) { 
  70.  
  71. foreach($form['fields'] as &$field) { 
  72. $classes = explode(' ', $field['cssClass']); 
  73.  
  74. foreach($classes as $class) { 
  75. //check if the class starts with 'taxomony-' 
  76. if($this->starts_with($class, 'taxonomy-')) { 
  77. $taxonomy = str_replace('taxonomy-', '', $class); 
  78. $term_id = (int) $lead[$field['id']]; 
  79. wp_set_object_terms($lead['post_id'], $term_id, $taxonomy); 
  80.  
  81. /** 
  82. * starts_with 
  83. * Tests if a text starts with an given string. 
  84. * @param string 
  85. * @param string 
  86. * @return bool 
  87. */ 
  88. function starts_with($haystack, $needle) { 
  89. return strpos($haystack, $needle) === 0; 
  90.  
  91. /** 
  92. * returns true if a needle can be found in a haystack 
  93. */ 
  94. function str_contains($haystack, $needle) { 
  95. if (empty($haystack) || empty($needle)) 
  96. return false; 
  97.  
  98. $pos = strpos(strtolower($haystack), strtolower($needle)); 
  99.  
  100. if ($pos === false) 
  101. return false; 
  102. else 
  103. return true; 
  104.  
  105. function init() { 
  106. $this->_plugin_name = 'gravity-forms-custom-post-type-mapping'; 
  107.  
  108. //alter the way forms are rendered by inserting taxomony dropdowns if needed 
  109. add_filter ('gform_pre_render' , array(&$this, 'gf_populate_taxonomy_dropdowns') ); 
  110.  
  111. //set the post types for a form 
  112. add_filter("gform_post_data", array(&$this, 'gf_set_post_type'), 10, 2); 
  113.  
  114. //intercept the form save and save any taxonomy links if needed 
  115. add_action('gform_post_submission', array(&$this, 'gf_save_taxonomy'), 10, 2);