| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Abstract class for building dynamic lists. |
| 5 |
* |
| 6 |
* @since 4.10.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
abstract class CF7SG_Dynamic_list{ |
| 10 |
protected $tag_id; |
| 11 |
protected $label; |
| 12 |
protected static $instances; |
| 13 |
|
| 14 |
public function __construct($tag_id, $label){ |
| 15 |
$this->tag_id = $tag_id; |
| 16 |
$this->label = $label; |
| 17 |
$this->register(); |
| 18 |
} |
| 19 |
/** |
| 20 |
* function returns an array of dynamic field styles value=>label pairs for the admin tag generator. |
| 21 |
* these styles will be radio fields on the tag generator form. The selected style for a given field |
| 22 |
* will be parametrised as a class on the dynamic field HTML element. |
| 23 |
* so for example the select2 jquery dropdown style would have a select2 class added to the select field. |
| 24 |
* @return Array value=>label pairs. |
| 25 |
*/ |
| 26 |
abstract public function admin_generator_tag_styles(); |
| 27 |
/** |
| 28 |
* Function to get classes to be added to the form wrapper. |
| 29 |
* these classes will be passed in the resource enqueue action, allowing for specific js/css resources |
| 30 |
* to be queued up and loaded on the page where the form is being displayed. |
| 31 |
* @param WPCF7_FormTag $tag cf7 tag object for the form field. |
| 32 |
* @param int $form_id cf7 fomr post ID.. |
| 33 |
* @return Array an array of classes to be added to the form to which this tag belonggs to. |
| 34 |
*/ |
| 35 |
abstract public function get_form_classes($tag, $form_id); |
| 36 |
|
| 37 |
/** |
| 38 |
* Register a [dynamic_display] shortcode with CF7. |
| 39 |
* called by funciton above |
| 40 |
* This function registers a callback function to expand the shortcode for the googleMap form fields. |
| 41 |
* @since 1.0.0 |
| 42 |
* @param Array $attrs array of attribute key=>value pairs to be included in the html element tag. |
| 43 |
* @param Array $options array of value=>label pairs of options. |
| 44 |
* @param Array $option_attrs array of value=>attribute pairs for each options, such as permalinks for post sources.. |
| 45 |
* @param Boolean $is_multiselect if the field has multiple selection enabled.. |
| 46 |
* @param String $selected default selected value. |
| 47 |
* @return String an html string representing the input field to a=be added to the field wrapper and into the form. |
| 48 |
*/ |
| 49 |
abstract public function get_dynamic_html_field($attrs, $options, $option_attrs, $is_multiselect, $selected); |
| 50 |
|
| 51 |
/** |
| 52 |
* Function called by the public class method when the Post My CF7 Form plugin maps a form submission |
| 53 |
* to a post. |
| 54 |
* @since 4.10.0 |
| 55 |
* @param String $post_id the id of the post to which this submission was mapped |
| 56 |
* @param String post ID of form being submitted |
| 57 |
* @param String $cf7_key the unique form key to identity the form being submitted |
| 58 |
* @param Array $post_fields form fields mapped to post fields, form-field-name => post-field-name key value pairs |
| 59 |
* @param Array $post_meta_fields form fields mapped to post meta fields, form-field-name => post-meta-field-name key value pairs |
| 60 |
* @param Array $submitted_data data submited in the form, form-field-name => submitted-value key value pairs |
| 61 |
*@return string text_description |
| 62 |
*/ |
| 63 |
abstract public function save_form_2_post($post_id, $form_id, $cf7_key, $post_fields, $post_meta_fields, $submitted_data); |
| 64 |
|
| 65 |
/** |
| 66 |
* Called to register stylesheet resources on the frontend. |
| 67 |
* IMPORTANT: use wp_register_style() |
| 68 |
*@since 4.10.0 |
| 69 |
* @param Boolean $airplane if true load only local resources. |
| 70 |
*/ |
| 71 |
abstract public function register_styles($airplane); |
| 72 |
|
| 73 |
/** |
| 74 |
* Called to register stylesheet resources on the frontend. |
| 75 |
* IMPORTANT: use wp_register_script() |
| 76 |
*@since 4.10.0 |
| 77 |
* @param Boolean $airplane if true load only local resources. |
| 78 |
*/ |
| 79 |
abstract public function register_scripts($airplane); |
| 80 |
|
| 81 |
/** |
| 82 |
* function to register and track newly created dynamic list. |
| 83 |
*/ |
| 84 |
protected function register(){ |
| 85 |
if(!isset(self::$instances)) self::$instances = array(); |
| 86 |
self::$instances[$this->tag_id] = $this; |
| 87 |
// debug_msg(self::$instances, 'registration '); |
| 88 |
} |
| 89 |
|
| 90 |
static public function get_instances($tag_id=null){ |
| 91 |
if(!isset(self::$instances)) self::$instances = array(); |
| 92 |
$instance = self::$instances; |
| 93 |
if(isset($tag_id)){ |
| 94 |
$instance = isset(self::$instances[$tag_id]) ? self::$instances[$tag_id]:false; |
| 95 |
} |
| 96 |
// debug_msg(self::$instances, 'get isntances '); |
| 97 |
// debug_msg($instance, 'get isntance '.$tag_id); |
| 98 |
return $instance; |
| 99 |
} |
| 100 |
/** |
| 101 |
* Function to register the admin tag. |
| 102 |
* called by the admin/class-cf7-grid-layout-admin.php cf7_shortcode_tags() method which is hooked to |
| 103 |
* 'wpcf7_admin_init'. |
| 104 |
*/ |
| 105 |
public function register_cf7_tag(){ |
| 106 |
if ( class_exists( 'WPCF7_TagGenerator' ) ) { |
| 107 |
$tag_generator = WPCF7_TagGenerator::get_instance(); |
| 108 |
$tag_generator->add( |
| 109 |
$this->tag_id, //tag id |
| 110 |
$this->label, //tag button label |
| 111 |
array($this,'admin_tag_generator') //callback |
| 112 |
); |
| 113 |
} |
| 114 |
} |
| 115 |
/** |
| 116 |
* Dynamic select screen displayt. |
| 117 |
* |
| 118 |
* This function is called by cf7 plugin, and is registered with a hooked function above |
| 119 |
* |
| 120 |
* @since 1.0.0 |
| 121 |
* @param WPCF7_ContactForm $contact_form the cf7 form object |
| 122 |
* @param array $args arguments for this form. |
| 123 |
*/ |
| 124 |
public function admin_tag_generator( $contact_form, $args = ''){ |
| 125 |
$args = wp_parse_args( $args, array() ); |
| 126 |
add_action('cf7sg_', array($this, 'admin_generator_tag_styles')); |
| 127 |
include_once plugin_dir_path( __FILE__ ) . '/admin/cf7-dynamic-tag-display.php'; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Register the form front-end shortcode. |
| 132 |
* Called by public/class-cf7-grid-layout-public.php register_cf7_shortcode() methid |
| 133 |
* which is hooked on 'wpcf7_init'. |
| 134 |
* This function registers a callback function to expand the shortcode for the save button field. |
| 135 |
* @since 2.0.0 |
| 136 |
*/ |
| 137 |
public function register_cf7_shortcode() { |
| 138 |
if( function_exists('wpcf7_add_form_tag') ) { |
| 139 |
//dynamic select |
| 140 |
wpcf7_add_form_tag( |
| 141 |
array( $this->tag_id, $this->tag_id.'*' ), |
| 142 |
array($this,'get_shortcode_html'), |
| 143 |
true //has name |
| 144 |
); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Function to retrieve dynamic-dropdown attributes |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* @param WPCF7_FormTag $tag cf7 tag object of basetype dynamic_select. |
| 153 |
* @return array an array of attributes with 'source'=>[post|taxonomy|filter], . |
| 154 |
**/ |
| 155 |
public function get_dynamic_attributes($tag){ |
| 156 |
if(is_array($tag) && isset($tag['basetype']) && 'dynamic_select' === $tag['basetype']){ |
| 157 |
$tag = new WPCF7_FormTag($tag); |
| 158 |
} |
| 159 |
if( !($tag instanceof WPCF7_FormTag) || 'dynamic_select' !== $tag['basetype']){ |
| 160 |
return false; |
| 161 |
} |
| 162 |
$source = array(); |
| 163 |
if(empty($tag->values)) debug_msg($tag, "CF7SG ERROR: malformed dynamic dropdown tag, unable to retrieve values"); |
| 164 |
foreach($tag->values as $values){ |
| 165 |
if(0 === strpos($values, 'slug:') ){ |
| 166 |
$source['source'] = "taxonomy"; |
| 167 |
$source['taxonomy'] = str_replace('slug:', '', $values); |
| 168 |
} |
| 169 |
if(0 === strpos($values, 'source:post')){ |
| 170 |
$source['source'] = "post"; |
| 171 |
$source['post'] = str_replace('source:post:', '', $values); |
| 172 |
} |
| 173 |
if(0 === strpos($values, 'taxonomy:')){ |
| 174 |
if(empty($source['taxonomy'])){ |
| 175 |
$source['taxonomy'] = array(); |
| 176 |
} |
| 177 |
$values = str_replace('taxonomy:', '', $values); |
| 178 |
$exp = explode(":", $values); |
| 179 |
if(!empty($exp) && is_array($exp)){ |
| 180 |
$source['taxonomy'][$exp[1]] = $exp[0]; |
| 181 |
} |
| 182 |
} |
| 183 |
if(0 === strpos($values, 'source:filter')){ |
| 184 |
$source['source'] = "filter"; |
| 185 |
} |
| 186 |
} |
| 187 |
return $source; |
| 188 |
} |
| 189 |
/** |
| 190 |
* function to display the html field in the form. |
| 191 |
* @since 4.10.0 |
| 192 |
* @param String $field_name the tag field name designated in the tag help screen |
| 193 |
* @return String a set of html fields to capture the googleMap information |
| 194 |
*/ |
| 195 |
public function get_shortcode_html($field_name){ |
| 196 |
$tag = new WPCF7_FormTag( $field_name ); |
| 197 |
$source = $this->get_dynamic_attributes($tag); |
| 198 |
|
| 199 |
$validation_error = wpcf7_get_validation_error( $tag->name ); |
| 200 |
$class = wpcf7_form_controls_class( $tag->type, 'cf7sg-dynamic-dropdown' ); |
| 201 |
if ( $validation_error ) { |
| 202 |
$class .= ' wpcf7-not-valid'; |
| 203 |
} |
| 204 |
$class = $tag->get_class_option( $class ); |
| 205 |
|
| 206 |
$id = $tag->get_id_option(); |
| 207 |
// $default = $tag->get_default_option(); |
| 208 |
$has_permalinks = false; |
| 209 |
/** @since 3.3.0 allow multiple */ |
| 210 |
$select_attributes = ''; |
| 211 |
$option_attrs = array(); |
| 212 |
// $option_attributes[$default] = ' '; |
| 213 |
// $name_suffix=''; |
| 214 |
foreach($tag->options as $tag_option){ |
| 215 |
switch($tag_option){ |
| 216 |
case 'multiple': |
| 217 |
$is_multiple = true; |
| 218 |
break; |
| 219 |
case 'permalinks': /** @since 4.0 */ |
| 220 |
$has_permalinks = true; |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
$options = array(); |
| 225 |
$cf7_form = wpcf7_get_current_contact_form(); |
| 226 |
$cf7_key = get_cf7form_key($cf7_form->id()); |
| 227 |
$filter_options = false; |
| 228 |
|
| 229 |
if(!empty($tag->values)){ |
| 230 |
if('taxonomy' == $source['source']){ |
| 231 |
$taxonomy_query= array('hide_empty' => false, 'taxonomy'=>$source['taxonomy']); |
| 232 |
$taxonomy_query = apply_filters('cf7sg_dynamic_dropdown_taxonomy_query', $taxonomy_query, $tag->name, $cf7_key); |
| 233 |
//check the WP version |
| 234 |
global $wp_version; |
| 235 |
if ( $wp_version >= 4.5 ) { |
| 236 |
$terms = get_terms($taxonomy_query); //WP>= 4.5 the get_terms does not take a taxonomy slug field |
| 237 |
}else{ |
| 238 |
$terms = get_terms($source['taxonomy'], $taxonomy_query); |
| 239 |
} |
| 240 |
if( is_wp_error( $terms )) { |
| 241 |
debug_msg($terms, 'Unable to retrieve taxonomy <em>'.$source['taxonomy'].'</em> terms'); |
| 242 |
$terms = array(); |
| 243 |
}else{ |
| 244 |
if(!empty($terms)) $selected = $terms[0]->slug; |
| 245 |
foreach($terms as $term){ |
| 246 |
/** |
| 247 |
* Filter dropdown options labels. |
| 248 |
* @param string $label option label value. |
| 249 |
* @param mixed $term the term object being used to populate this option. |
| 250 |
* @param string $name the field name being populated. |
| 251 |
* @param string $cf7_key the form unique key. |
| 252 |
* @return string $label option label value. |
| 253 |
* @since 2.0.0 |
| 254 |
*/ |
| 255 |
$label = apply_filters('cf7sg_dynamic_dropdown_option_label', $term->name, $term, $tag->name, $cf7_key); |
| 256 |
$options[$term->slug] = $label; |
| 257 |
|
| 258 |
/** |
| 259 |
* Filter dropdown options attributes. |
| 260 |
* @param array $attributes an array of <attribute>=>$value pairs which will be used for populating select options, instead of a string $value, an array of values can be passed such as classes. |
| 261 |
* @param mixed either WP_Post or WP_Term object being used to populate this option. |
| 262 |
* @param String $name the field name being populated. |
| 263 |
* @param String $cf7_key the form unique key. |
| 264 |
* @return Array array of $value=>$name pairs which will be used for populating select options attributes. |
| 265 |
* @since 2.0.0 |
| 266 |
*/ |
| 267 |
$attributes = apply_filters('cf7sg_dynamic_dropdown_option_attributes', array(), $term, $tag->name, $cf7_key); |
| 268 |
if(!empty($attributes)){ |
| 269 |
foreach($attributes as $attribute => $avalue){ |
| 270 |
if(is_array($avalue)){ |
| 271 |
$separator = ' '; |
| 272 |
if('style' === $attribute ) $separator = ';'; |
| 273 |
$avalue = implode( $separator, $avalue); |
| 274 |
} |
| 275 |
$option_attrs[$term->slug] = ' '.$attribute.'="'.$avalue.'"'; |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
$filter_options = true; |
| 281 |
}else if('post' == $source['source']){ |
| 282 |
$args = array( |
| 283 |
'post_type' => $source['post'], |
| 284 |
'post_status' => 'publish', |
| 285 |
'posts_per_page' => -1 |
| 286 |
); |
| 287 |
if(!empty($source['taxonomy'])){ |
| 288 |
$tax = array(); |
| 289 |
if(sizeof($source['taxonomy']) > 1){ |
| 290 |
$tax['relation'] = 'AND'; |
| 291 |
} |
| 292 |
foreach($source['taxonomy'] as $term => $taxonomy){ |
| 293 |
$tax[]=array( |
| 294 |
'taxonomy' => $taxonomy, |
| 295 |
'field' => 'slug', |
| 296 |
'terms' => $term |
| 297 |
); |
| 298 |
} |
| 299 |
$args['tax_query'] = $tax; |
| 300 |
} |
| 301 |
/** |
| 302 |
* Filter post query for dynamic dropdown options. |
| 303 |
* @param array $args an arra of query terms. |
| 304 |
* @param string $name the field name being populated. |
| 305 |
* @param string $cf7_key the form unique key. |
| 306 |
* @return array an arra of query terms. |
| 307 |
*/ |
| 308 |
$args = apply_filters('cf7sg_dynamic_dropdown_post_query', $args, $tag->name, $cf7_key); |
| 309 |
$posts = get_posts($args); |
| 310 |
if(!empty($posts)){ |
| 311 |
$selected = $posts[0]->post_name; |
| 312 |
|
| 313 |
foreach($posts as $post){ |
| 314 |
/** |
| 315 |
* Filter dropdown options labels. |
| 316 |
* @param string $label option label value. |
| 317 |
* @param mixed $post the post object being used to populate this option. |
| 318 |
* @param string $name the field name being populated. |
| 319 |
* @param string $cf7_key the form unique key. |
| 320 |
* @return string $label option label value. |
| 321 |
* @since 2.0.0 |
| 322 |
*/ |
| 323 |
$label = apply_filters('cf7sg_dynamic_dropdown_option_label', $post->post_title, $post, $tag->name, $cf7_key); |
| 324 |
$options[$post->post_name] = $label; |
| 325 |
/** |
| 326 |
* Filter dropdown options attributes. |
| 327 |
* @param Array $attributes an array of <attribute>=>$value pairs which will be used for populating select options, instead of a string $value, an array of values can be passed such as classes. |
| 328 |
* @param mixed either WP_Post or WP_Term object being used to populate this option. |
| 329 |
* @param String $name the field name being populated. |
| 330 |
* @param String $cf7_key the form unique key. |
| 331 |
* @return Array array of $value=>$name pairs which will be used for populating select options attributes. |
| 332 |
* @since 2.0.0 |
| 333 |
*/ |
| 334 |
$attributes = array(); |
| 335 |
if($has_permalinks){ |
| 336 |
$attributes['data-permalink'] = get_permalink($post); |
| 337 |
} |
| 338 |
$attributes = apply_filters('cf7sg_dynamic_dropdown_option_attributes', $attributes, $post, $tag->name, $cf7_key); |
| 339 |
if(!empty($attributes)){ |
| 340 |
foreach($attributes as $attribute => $avalue){ |
| 341 |
if(is_array($avalue)){ |
| 342 |
$separator = ' '; |
| 343 |
if('style' === $attribute ) $separator = ';'; |
| 344 |
$avalue = implode( $separator, $avalue); |
| 345 |
} |
| 346 |
$option_attrs[$post->post_name] = ' '.$attribute.'="'.$avalue.'"'; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
$filter_options = true; |
| 352 |
}else if('filter' == $source['source']){ |
| 353 |
/** |
| 354 |
* Get custom options from filter source. |
| 355 |
* @param Array $options an array of value=>label pairs of options. Alternatively 2 arrays can be filtered, see return parameter description. |
| 356 |
* @param String $field_name name of field being populated. |
| 357 |
* @param String $cf7_key form unique key. |
| 358 |
* @return Array either an array of value=>label pairs, or 2 arrays, 'values'=>array(value=>label), 'attributes'=>array(value=>attribute string), in order to add a set of attributes to be added to each option html element tag. |
| 359 |
*/ |
| 360 |
$custom_options = apply_filters('cf7sg_dynamic_dropdown_custom_options', array(), $tag->name, $cf7_key); |
| 361 |
if(isset($custom_options['values']) && is_array($custom_options['values'])){ |
| 362 |
$options = $custom_options['values']; |
| 363 |
}else $options = $custom_options; |
| 364 |
$custom_options = apply_filters('cf7sg_dynamic_dropdown_custom_options', array(), $tag->name, $cf7_key); |
| 365 |
if(isset($custom_options['attributes']) && is_array($custom_options['attributes'])){ |
| 366 |
$option_attrs = $custom_options['attributes']; |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
if($filter_options){ //true if either taxonomy or post dropdpwn; |
| 371 |
/** |
| 372 |
* Allow filtering of options populated by posts or taxonomies. |
| 373 |
* @param array $options an array of $value=>$name pairs which will be used for populating select options. |
| 374 |
* @param string $name the field name being populated. |
| 375 |
* @param string $cf7_key the form unique key. |
| 376 |
* @return array array of $value=>$name pairs which will be used for populating select options. |
| 377 |
* @since 1.4.0 |
| 378 |
*/ |
| 379 |
$options = apply_filters('cf7sg_dynamic_dropdown_filter_options', $options, $tag->name, $cf7_key); |
| 380 |
} |
| 381 |
|
| 382 |
$tag_name = sanitize_html_class( $tag->name ); |
| 383 |
/** @since 3.3.0 enable custom attributes on select element*/ |
| 384 |
$attributes = apply_filters('cf7sg_dynamic_dropdown_attributes', array(), $tag->name, $cf7_key); |
| 385 |
$attributes['id']=$id; |
| 386 |
$attributes['class']=$class; |
| 387 |
$attributes['name']=$tag->name; |
| 388 |
|
| 389 |
/** @since 4.0 */ |
| 390 |
if($has_permalinks) $class.=' cf7sg-permalinks'; |
| 391 |
|
| 392 |
/** |
| 393 |
* @since 2.2 allows custom filtered $options to be an html string. |
| 394 |
*/ |
| 395 |
if(!is_array( $options )) $options = array(); |
| 396 |
/** |
| 397 |
* Filter dynamic dropdown default empty value label. |
| 398 |
* @param string $label the label for the default value, this is null by default and not shown. |
| 399 |
* @param string $name the field name being populated. |
| 400 |
* @param string $cf7_key the form unique key. |
| 401 |
* @return string the label for the default value, returning a non-null value with display this as the first option. |
| 402 |
*/ |
| 403 |
$default_value = apply_filters('cf7sg_dynamic_dropdown_default_value', null, $tag->name, $cf7_key); |
| 404 |
if(!is_null($default_value)){ |
| 405 |
$options['']=$default_value; |
| 406 |
$selected=''; |
| 407 |
} |
| 408 |
|
| 409 |
$html = '<span class="wpcf7-form-control-wrap '.$tag_name.'">'.PHP_EOL; |
| 410 |
$html .= $this->get_dynamic_html_field($attributes, $options, $option_attrs, $is_multiple,$selected).PHP_EOL; |
| 411 |
$html .='</span>'.PHP_EOL; |
| 412 |
|
| 413 |
return $html; |
| 414 |
} |
| 415 |
/** |
| 416 |
* |
| 417 |
* |
| 418 |
* @since 5.0.0 |
| 419 |
* @param mixed $class array of classes or string to set class for form and dependency loading . |
| 420 |
* @param Object $cf7_form cf7 post id against which set the c$lass . |
| 421 |
**/ |
| 422 |
public function update_form_classes($class, $cf7_id=null){ |
| 423 |
if(empty($cf7_id)){ |
| 424 |
//get latest form |
| 425 |
if(function_exists('wpcf7_get_current_contact_form')){ |
| 426 |
$cf7_form = wpcf7_get_current_contact_form(); |
| 427 |
if(!empty($cf7_form)) $cf7_id = $cf7_form->id(); |
| 428 |
} |
| 429 |
} |
| 430 |
if(empty($cf7_id)){ |
| 431 |
debug_msg('Unable to get Contact Form 7 ID to set class: '.$class); |
| 432 |
return; |
| 433 |
} |
| 434 |
$classes = get_post_meta($cf7_id, '_cf7sg_classes', true); |
| 435 |
if(empty($classes)){ |
| 436 |
$classes = array(); |
| 437 |
} |
| 438 |
if(is_array($class)){ |
| 439 |
foreach($class as $class_name){ |
| 440 |
$classes[$class_name] = true; |
| 441 |
} |
| 442 |
}else{ |
| 443 |
$classes[$class] = true; |
| 444 |
} |
| 445 |
update_post_meta($cf7_id, '_cf7sg_classes', $classes); |
| 446 |
return; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if(!function_exists('cf7sg_get_dynamic_lists')){ |
| 451 |
function cf7sg_get_dynamic_lists($tag_id=null){ |
| 452 |
return CF7SG_Dynamic_list::get_instances($tag_id); |
| 453 |
} |
| 454 |
} |
| 455 |
|