| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Abstract class for building dynamic lists. |
| 5 |
* |
| 6 |
* @since 4.10.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
class CF7SG_Dynamic_list{ |
| 10 |
/** |
| 11 |
* @since 4.10.0 |
| 12 |
* @access protected |
| 13 |
* @var String $tag_id tag field unique id. |
| 14 |
*/ |
| 15 |
public $tag_id; |
| 16 |
/** |
| 17 |
* @since 4.10.0 |
| 18 |
* @access protected |
| 19 |
* @var String $label tag button label. |
| 20 |
*/ |
| 21 |
public $label; |
| 22 |
/** |
| 23 |
* @since 4.11.0 |
| 24 |
* @access protected |
| 25 |
* @var Array $styles array of style fields available in the tag manager form. |
| 26 |
*/ |
| 27 |
protected $styles = array(); |
| 28 |
/** |
| 29 |
* @since 4.11.0 |
| 30 |
* @access protected |
| 31 |
* @var Array $style_extras array of extra fields appended to each style field for further customisation, can either be style_id => array( $field_value => $field_label) (defaults to a checkbox) or, |
| 32 |
* style_id => array( $field_value => array( |
| 33 |
* 'type'=>'text|number|checkbox' default is checkbox. |
| 34 |
* 'label'=> label text or html string. |
| 35 |
* 'attrs'=> string og attributes to be added to the field. |
| 36 |
* )) |
| 37 |
* for other fields. |
| 38 |
*/ |
| 39 |
protected $style_extras = array(); |
| 40 |
/** |
| 41 |
* @since 4.11.0 |
| 42 |
* @access protected |
| 43 |
* @var Array $other_extras array of extra fields added to the 'Other attributes' fieldset, can either be array( $field_value => $field_label) (defaults to a checkbox) or, |
| 44 |
* array( $field_value => array( |
| 45 |
* 'type'=>'text|number|checkbox' default is checkbox. |
| 46 |
* 'label'=> label text or html string. |
| 47 |
* 'attrs'=> string og attributes to be added to the field. |
| 48 |
* )) |
| 49 |
* for other fields. |
| 50 |
*/ |
| 51 |
protected $other_extras = array(); |
| 52 |
/** |
| 53 |
* @since 4.11.0 |
| 54 |
* @access protected |
| 55 |
* @var String default checkbox, can be set to radio isntead for extra configuration fields using hte set_others_extras_radio(); |
| 56 |
*/ |
| 57 |
protected $other_extras_type = 'checkbox'; |
| 58 |
/** |
| 59 |
* flag that determines if field can accepted nested lists such as hierarchical taxonomies. |
| 60 |
* @since 4.11.0 |
| 61 |
* @access protected |
| 62 |
* @var String default false, can be changed with allow_nesting() method; |
| 63 |
*/ |
| 64 |
protected $nesting = false; |
| 65 |
/** |
| 66 |
* @since 4.10.0 |
| 67 |
* @access protected |
| 68 |
* @var Array $instances array of tag_id=>CF7SG_Dynamic_list objects to keep track of instances. |
| 69 |
*/ |
| 70 |
protected static $instances; |
| 71 |
|
| 72 |
public function __construct($tag_id, $label){ |
| 73 |
$this->tag_id = $tag_id; |
| 74 |
$this->label = $label; |
| 75 |
$this->register(); |
| 76 |
} |
| 77 |
/** |
| 78 |
* set the stles for the tag manager. |
| 79 |
* @since 4.11.0 |
| 80 |
* @param Array $styles array of style fields available in the tag manager form. |
| 81 |
* @param Array $style_extras array of extra fields appended to each style field for further customisation, can either be style_id => array( $field_value => $field_label) (defaults to a checkbox) or, |
| 82 |
* style_id => array( $field_value => array( |
| 83 |
* 'type'=>'text|number|checkbox' default is checkbox. |
| 84 |
* 'label'=> label text or html string. |
| 85 |
* 'attrs'=> string og attributes to be added to the field. |
| 86 |
* )) |
| 87 |
* for other fields. |
| 88 |
*/ |
| 89 |
public function set_styles($styles, $style_extras){ |
| 90 |
$this->styles = $styles; |
| 91 |
$this->style_extras = $style_extras; |
| 92 |
} |
| 93 |
/** |
| 94 |
* Set extra configuratino fields in the 'Other Attributes' fieldset of the tag manager form. |
| 95 |
* @since 4.11.0 |
| 96 |
* @param Array $extras array of extra fields added to the 'Other attributes' fieldset, can either be array( $field_value => $field_label) (defaults to a checkbox) or, |
| 97 |
* array( $field_value => array( |
| 98 |
* 'type'=>'text|number|checkbox' default is checkbox. |
| 99 |
* 'label'=> label text or html string. |
| 100 |
* 'attrs'=> string of attributes to be added to the field. |
| 101 |
* )) |
| 102 |
* for other fields. |
| 103 |
*/ |
| 104 |
public function set_others_extras($extras){ |
| 105 |
$this->other_extras = $extras; |
| 106 |
} |
| 107 |
/** |
| 108 |
* Set the extra configuration fields to be radio fields instead of the default checkbox. |
| 109 |
* @since 4.11.0 |
| 110 |
*/ |
| 111 |
public function set_others_extras_radio(){ |
| 112 |
$this->other_extras_type = 'radio'; |
| 113 |
} |
| 114 |
/** |
| 115 |
* This field can handle nesting of values (hierarchical lists) |
| 116 |
* |
| 117 |
*@since 4.11.0 |
| 118 |
*/ |
| 119 |
public function allow_nesting(){ |
| 120 |
$this->nesting = true; |
| 121 |
} |
| 122 |
/** |
| 123 |
* Return the nesting boolean to determine if the list can display nested lists. |
| 124 |
*@since 4.11.0 |
| 125 |
*@return Boolean false or true. |
| 126 |
*/ |
| 127 |
public function has_nesting(){ |
| 128 |
return $this->nesting; |
| 129 |
} |
| 130 |
/** |
| 131 |
* function returns an array of dynamic field styles value=>label pairs for the admin tag generator. |
| 132 |
* these styles will be radio fields on the tag generator form. The selected style for a given field |
| 133 |
* will be parametrised as a class on the dynamic field HTML element. |
| 134 |
* so for example the select2 jquery dropdown style would have a select2 class added to the select field. |
| 135 |
* @return Array value=>label pairs. |
| 136 |
*/ |
| 137 |
public function get_tag_generator_styles(){ |
| 138 |
return $this->styles; |
| 139 |
} |
| 140 |
/** |
| 141 |
* get extra fields associated with a given style field to display in the tag manager form. |
| 142 |
* @since 4.11.0 |
| 143 |
* @param String $style style id |
| 144 |
*/ |
| 145 |
public function get_style_extras($style){ |
| 146 |
$extras = array(); |
| 147 |
if(isset($this->style_extras[$style])) $extras = $this->style_extras[$style]; |
| 148 |
if(!is_array($extras)) $extras = array($extras); |
| 149 |
return $extras; |
| 150 |
} |
| 151 |
/** |
| 152 |
* get extra fields associated with a given style field to display in the tag manager form. |
| 153 |
* @since 4.11.0 |
| 154 |
* @param String $style style id |
| 155 |
*/ |
| 156 |
public function get_other_extras(){ |
| 157 |
return $this->other_extras; |
| 158 |
} |
| 159 |
/** |
| 160 |
* get the extra fields type |
| 161 |
* |
| 162 |
*@since 4.11.0 |
| 163 |
*@return String checkbox | radio |
| 164 |
*/ |
| 165 |
public function get_other_extras_type(){ |
| 166 |
return $this->other_extras_type; |
| 167 |
} |
| 168 |
/** |
| 169 |
* Function to get classes to be added to the form wrapper. |
| 170 |
* these classes will be passed in the resource enqueue action, allowing for specific js/css resources |
| 171 |
* to be queued up and loaded on the page where the form is being displayed. |
| 172 |
* @param WPCF7_FormTag $tag cf7 tag object for the form field. |
| 173 |
* @param int $form_id cf7 fomr post ID.. |
| 174 |
* @return Array an array of classes to be added to the form to which this tag belongs to. |
| 175 |
*/ |
| 176 |
public function get_form_classes($tag, $form_id){ |
| 177 |
return apply_filters('cf7sg_save_dynamic_list_form_classes', array(), $tag, $form_id); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* function to register and track newly created dynamic list. |
| 182 |
*/ |
| 183 |
protected function register(){ |
| 184 |
if(!isset(self::$instances)) self::$instances = array(); |
| 185 |
self::$instances[$this->tag_id] = $this; |
| 186 |
// debug_msg(self::$instances, 'registration '); |
| 187 |
} |
| 188 |
|
| 189 |
static public function get_instances($tag_id=null){ |
| 190 |
if(!isset(self::$instances)) self::$instances = array(); |
| 191 |
$instance = self::$instances; |
| 192 |
if(isset($tag_id)){ |
| 193 |
$instance = isset(self::$instances[$tag_id]) ? self::$instances[$tag_id]:false; |
| 194 |
} |
| 195 |
// debug_msg(self::$instances, 'get isntances '); |
| 196 |
// debug_msg($instance, 'get isntance '.$tag_id); |
| 197 |
return $instance; |
| 198 |
} |
| 199 |
/** |
| 200 |
* Function to register the admin tag. |
| 201 |
* called by the admin/class-cf7-grid-layout-admin.php cf7_shortcode_tags() method which is hooked to |
| 202 |
* 'wpcf7_admin_init'. |
| 203 |
*/ |
| 204 |
public function register_cf7_tag(){ |
| 205 |
if ( class_exists( 'WPCF7_TagGenerator' ) ) { |
| 206 |
$tag_generator = WPCF7_TagGenerator::get_instance(); |
| 207 |
$tag_generator->add( |
| 208 |
$this->tag_id, //tag id |
| 209 |
$this->label, //tag button label |
| 210 |
array($this,'admin_tag_generator') //callback |
| 211 |
); |
| 212 |
} |
| 213 |
} |
| 214 |
/** |
| 215 |
* Dynamic select screen displayt. |
| 216 |
* |
| 217 |
* This function is called by cf7 plugin, and is registered with a hooked function above |
| 218 |
* |
| 219 |
* @since 4.10.0 |
| 220 |
* @param WPCF7_ContactForm $contact_form the cf7 form object |
| 221 |
* @param array $args arguments for this form. |
| 222 |
*/ |
| 223 |
public function admin_tag_generator( $contact_form, $args = ''){ |
| 224 |
$args = wp_parse_args( $args, array() ); |
| 225 |
do_action('cf7sg_display_dynamic_list_tag_manager', $this->tag_id, $this, $contact_form, $args); |
| 226 |
} |
| 227 |
/** |
| 228 |
* function to display taxonomy dropdown list for posts in tag generator */ |
| 229 |
public function cf7sg_terms_to_options($taxonomy, $is_hierarchical, $parent=0, $level=1){ |
| 230 |
$args = array('hide_empty' => 0); |
| 231 |
if($is_hierarchical){ |
| 232 |
$args['parent'] = $parent; |
| 233 |
} |
| 234 |
//check the WP version |
| 235 |
global $wp_version; |
| 236 |
if ( $wp_version >= 4.5 ) { |
| 237 |
$args['taxonomy'] = $taxonomy; |
| 238 |
$terms = get_terms($args); //WP>= 4.5 the get_terms does not take a taxonomy slug field |
| 239 |
}else{ |
| 240 |
$terms = get_terms($taxonomy, $args); |
| 241 |
} |
| 242 |
if( is_wp_error( $terms ) ){ |
| 243 |
debug_msg('Taxonomy '.$taxonomy.' does not exist'); |
| 244 |
return ''; |
| 245 |
}else if( empty($terms) ){ |
| 246 |
return''; |
| 247 |
} |
| 248 |
if(0==$parent) $class = 'parent'; |
| 249 |
else $class = 'child'; |
| 250 |
$script = ''; |
| 251 |
foreach($terms as $term){ |
| 252 |
$script .='<option class="level-'.$level.'" value="taxonomy:' . $taxonomy . ':' . $term->slug . '" >' . $term->name . '</option>' . PHP_EOL; |
| 253 |
if($is_hierarchical){ |
| 254 |
$script .= $this->cf7sg_terms_to_options($taxonomy, $is_hierarchical, $term->term_id, $level+1); |
| 255 |
} |
| 256 |
} |
| 257 |
return $script; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Register the form front-end shortcode. |
| 262 |
* Called by public/class-cf7-grid-layout-public.php register_cf7_shortcode() methid |
| 263 |
* which is hooked on 'wpcf7_init'. |
| 264 |
* This function registers a callback function to expand the shortcode for the save button field. |
| 265 |
* @since 2.0.0 |
| 266 |
*/ |
| 267 |
public function register_cf7_shortcode() { |
| 268 |
if( function_exists('wpcf7_add_form_tag') ) { |
| 269 |
//dynamic select |
| 270 |
wpcf7_add_form_tag( |
| 271 |
array( $this->tag_id, $this->tag_id.'*' ), |
| 272 |
array($this,'get_shortcode_html'), |
| 273 |
true //has name |
| 274 |
); |
| 275 |
} |
| 276 |
} |
| 277 |
/** |
| 278 |
* Function to retrieve dynamic-dropdown data attributes |
| 279 |
* |
| 280 |
* @since 4.11.0 |
| 281 |
* @param WPCF7_FormTag $tag cf7 tag object of basetype dynamic list. |
| 282 |
* @return Array an array of data attributes . |
| 283 |
**/ |
| 284 |
public function get_data_attributes($tag){ |
| 285 |
if(is_array($tag) && isset($tag['basetype']) && $this->tag_id === $tag['basetype']){ |
| 286 |
$tag = new WPCF7_FormTag($tag); |
| 287 |
} |
| 288 |
if( !($tag instanceof WPCF7_FormTag) || $this->tag_id !== $tag['basetype']){ |
| 289 |
return false; |
| 290 |
} |
| 291 |
$attrs = array(); |
| 292 |
if(empty($tag->values)) debug_msg($tag, "CF7SG ERROR: malformed {$this->tag_id} tag, unable to retrieve values"); |
| 293 |
foreach($tag->values as $values){ |
| 294 |
if(false === stripos($values, 'data-')) continue; |
| 295 |
$a = explode(':',$values); |
| 296 |
if(isset($a[1])) $attrs[$a[0]] = $a[1]; |
| 297 |
} |
| 298 |
return $attrs; |
| 299 |
} |
| 300 |
/** |
| 301 |
* Function to retrieve dynamic-dropdown attributes |
| 302 |
* |
| 303 |
* @since 1.0.0 |
| 304 |
* @param WPCF7_FormTag $tag cf7 tag object of basetype dynamic_select. |
| 305 |
* @return array an array of attributes with 'source'=>[post|taxonomy|filter], . |
| 306 |
**/ |
| 307 |
public function get_dynamic_attributes($tag){ |
| 308 |
if(is_array($tag) && isset($tag['basetype']) && $this->tag_id === $tag['basetype']){ |
| 309 |
$tag = new WPCF7_FormTag($tag); |
| 310 |
} |
| 311 |
if( !($tag instanceof WPCF7_FormTag) || $this->tag_id !== $tag['basetype']){ |
| 312 |
return false; |
| 313 |
} |
| 314 |
$source = array(); |
| 315 |
if(empty($tag->values)) debug_msg($tag, "CF7SG ERROR: malformed {$this->tag_id} tag, unable to retrieve values"); |
| 316 |
foreach($tag->values as $values){ |
| 317 |
if(0 === strpos($values, 'slug:') ){ |
| 318 |
$source['source'] = "taxonomy"; |
| 319 |
$s = explode(':',$values); |
| 320 |
$source['taxonomy'] = $s[1]; |
| 321 |
$source['tree'] = isset($s[2]) && 'tree'==$s[2]; /** @since 4.11 */ |
| 322 |
} |
| 323 |
if(0 === strpos($values, 'source:post')){ |
| 324 |
$source['source'] = "post"; |
| 325 |
$source['post'] = str_replace('source:post:', '', $values); |
| 326 |
} |
| 327 |
if(0 === strpos($values, 'taxonomy:')){ |
| 328 |
if(empty($source['taxonomy'])){ |
| 329 |
$source['taxonomy'] = array(); |
| 330 |
} |
| 331 |
$values = str_replace('taxonomy:', '', $values); |
| 332 |
$exp = explode(":", $values); |
| 333 |
if(!empty($exp) && is_array($exp)){ |
| 334 |
$source['taxonomy'][$exp[1]] = $exp[0]; |
| 335 |
} |
| 336 |
} |
| 337 |
if(0 === strpos($values, 'source:filter')){ |
| 338 |
$source['source'] = "filter"; |
| 339 |
} |
| 340 |
} |
| 341 |
return $source; |
| 342 |
} |
| 343 |
/** |
| 344 |
* function to display the html field in the form. |
| 345 |
* @since 4.10.0 |
| 346 |
* @param String $field_name the tag field name designated in the tag help screen |
| 347 |
* @return String a set of html fields to capture the googleMap information |
| 348 |
*/ |
| 349 |
public function get_shortcode_html($field_name){ |
| 350 |
$tag = new WPCF7_FormTag( $field_name ); |
| 351 |
// debug_msg($tag, 'tag object '); |
| 352 |
$source = $this->get_dynamic_attributes($tag); |
| 353 |
/** @since 4.11.0 enable data attributes */ |
| 354 |
$data_attrs = $this->get_data_attributes($tag); |
| 355 |
|
| 356 |
$validation_error = wpcf7_get_validation_error( $tag->name ); |
| 357 |
$class = wpcf7_form_controls_class( $tag->type, 'cf7sg-dynamic-list cf7sg-'.$this->tag_id ); |
| 358 |
if ( $validation_error ) { |
| 359 |
$class .= ' wpcf7-not-valid'; |
| 360 |
} |
| 361 |
$class = $tag->get_class_option( $class ); |
| 362 |
|
| 363 |
$id = $tag->get_id_option(); |
| 364 |
$selected = $tag->get_default_option(); |
| 365 |
//attributes to be added to the dropdown select field |
| 366 |
$select_attributes = ''; |
| 367 |
//capture any attributes to be added to individual options in dropdpwn list. |
| 368 |
$option_attrs = array(); |
| 369 |
//other attrbutes captures in the tag manager. |
| 370 |
$other_attrs = array(); |
| 371 |
foreach($tag->options as $tag_option){ |
| 372 |
if(false !== stripos($tag_option, ':')) continue; |
| 373 |
$other_attrs[$tag_option] = true; |
| 374 |
} |
| 375 |
$options = array(); |
| 376 |
$cf7_form = wpcf7_get_current_contact_form(); |
| 377 |
$cf7_key = get_cf7form_key($cf7_form->id()); |
| 378 |
$filter_options = false; |
| 379 |
/** @since 4.11 enable branches in nesting lists */ |
| 380 |
$branch = ($this->nesting && isset($source['taxonomy']) && is_taxonomy_hierarchical($source['taxonomy']) && $source['tree']) ? array(0):null; |
| 381 |
if(!empty($tag->values)){ |
| 382 |
if('taxonomy' == $source['source']){ |
| 383 |
$options = $this->get_taxonomy_terms($source, $branch, $tag, $cf7_key); |
| 384 |
|
| 385 |
// if(!empty($options)) $selected = array_key_first($options); |
| 386 |
|
| 387 |
$filter_options = true; |
| 388 |
}else if('post' == $source['source']){ |
| 389 |
$args = array( |
| 390 |
'post_type' => $source['post'], |
| 391 |
'post_status' => 'publish', |
| 392 |
'posts_per_page' => -1 |
| 393 |
); |
| 394 |
if(!empty($source['taxonomy'])){ |
| 395 |
$tax = array(); |
| 396 |
if(sizeof($source['taxonomy']) > 1){ |
| 397 |
$tax['relation'] = 'AND'; |
| 398 |
} |
| 399 |
foreach($source['taxonomy'] as $term => $taxonomy){ |
| 400 |
$tax[]=array( |
| 401 |
'taxonomy' => $taxonomy, |
| 402 |
'field' => 'slug', |
| 403 |
'terms' => $term |
| 404 |
); |
| 405 |
} |
| 406 |
if(count($tax)>1) $tax['relation']='AND'; |
| 407 |
|
| 408 |
$args['tax_query'] = $tax; |
| 409 |
} |
| 410 |
/** |
| 411 |
* Filter post query for dynamic dropdown options. |
| 412 |
* @param array $args an arra of query terms. |
| 413 |
* @param WPCF7_FormTag $tag the field name being populated. |
| 414 |
* @param String $cf7_key the form unique key. |
| 415 |
* @return array an arra of query terms. |
| 416 |
*/ |
| 417 |
$args = apply_filters_deprecated('cf7sg_dynamic_dropdown_post_query', |
| 418 |
array( |
| 419 |
$args, |
| 420 |
$tag->name, |
| 421 |
$cf7_key |
| 422 |
), '4.11.0', 'cf7sg_dynamic_list_post_query' ); |
| 423 |
$args = apply_filters('cf7sg_dynamic_list_post_query', $args, $tag->name, $cf7_key); |
| 424 |
|
| 425 |
$posts = get_posts($args); |
| 426 |
// debug_msg($posts, 'post query '); |
| 427 |
if(!empty($posts)){ |
| 428 |
$post_taxonomies = get_object_taxonomies($source['post']); |
| 429 |
|
| 430 |
// $selected = $posts[0]->post_name; |
| 431 |
|
| 432 |
foreach($posts as $post){ |
| 433 |
/** |
| 434 |
* Filter dropdown options labels. |
| 435 |
* @param String $label option label value. |
| 436 |
* @param mixed $post the post object being used to populate this option. |
| 437 |
* @param WPCF7_FormTag $tag the field name being populated. |
| 438 |
* @param String $cf7_key the form unique key. |
| 439 |
* @return String $label option label value. |
| 440 |
* @since 2.0.0 |
| 441 |
*/ |
| 442 |
$label = $post->post_title; |
| 443 |
$label = apply_filters_deprecated('cf7sg_dynamic_dropdown_option_label', |
| 444 |
array( |
| 445 |
$label, |
| 446 |
$post, |
| 447 |
$tag->name, |
| 448 |
$cf7_key |
| 449 |
), '4.11.0', "cf7sg_{$this->tag_id}_option_label" ); |
| 450 |
$label = apply_filters("cf7sg_{$this->tag_id}_option_label", $label, $post, $tag, $cf7_key); |
| 451 |
$attributes = array(); |
| 452 |
if( isset($other_attrs['permalinks']) ){ |
| 453 |
$attributes['data-permalink'] = get_permalink($post); |
| 454 |
} |
| 455 |
if( isset($other_attrs['thumbnails']) ){ |
| 456 |
$size = apply_filters("cf7sg_{$this->tag_id}_image_size", 'thumbnail', $post, $tag, $cf7_key); |
| 457 |
$attributes['data-thumbnail'] = get_the_post_thumbnail_url($post, $size); |
| 458 |
} |
| 459 |
$filter_attributes = apply_filters_deprecated( 'cf7sg_dynamic_dropdown_option_attributes', |
| 460 |
array( |
| 461 |
array(), |
| 462 |
$post, |
| 463 |
$tag->name, |
| 464 |
$cf7_key ), '4.11.0', "cf7sg_{$this->tag_id}_options_attributes" ); |
| 465 |
/** |
| 466 |
* Filter dropdown options attributes. |
| 467 |
* @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. |
| 468 |
* @param mixed either WP_Post or WP_Term object being used to populate this option. |
| 469 |
* @param WPCF7_FormTag $tag the field name being populated. |
| 470 |
* @param String $cf7_key the form unique key. |
| 471 |
* @return Array array of $value=>$name pairs which will be used for populating select options attributes. |
| 472 |
* @since 4.11.0 |
| 473 |
*/ |
| 474 |
$filter_attributes = apply_filters("cf7sg_{$this->tag_id}_options_attributes", $filter_attributes, $post, $tag, $cf7_key); |
| 475 |
if(is_array($filter_attributes)){ |
| 476 |
if(isset($filter_attributes['class'])){ |
| 477 |
if(!is_array($filter_attributes['class'])) $filter_attributes['class'] = array($filter_attributes['class']); |
| 478 |
$filter_attributes['class'][]='cf7sg-dl'; |
| 479 |
}else $filter_attributes['class']=array('cf7sg-dl'); |
| 480 |
}else $filter_attributes = array('class'=>array('cf7sg-dl')); |
| 481 |
//setup classes for existing post terms. |
| 482 |
if(apply_filters("cf7sg_{$this->tag_id}_include_post_terms_as_class", true, $tag, $cf7_key)){ |
| 483 |
foreach($post_taxonomies as $tx){ |
| 484 |
$ts = get_the_terms($post, $tx); |
| 485 |
if($ts && !is_wp_error($ts)) foreach($ts as $t) $filter_attributes['class'][]="$tx-$t->slug"; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
if(is_array($filter_attributes)){ |
| 490 |
$attributes = array_merge($attributes, $filter_attributes); |
| 491 |
} |
| 492 |
|
| 493 |
$options[$post->post_name] = array($label, $attributes, array()); |
| 494 |
} |
| 495 |
} |
| 496 |
$filter_options = true; |
| 497 |
}else if('filter' == $source['source']){ |
| 498 |
$options = apply_filters_deprecated( 'cf7sg_dynamic_dropdown_custom_options', |
| 499 |
array( |
| 500 |
'', |
| 501 |
$tag->name, |
| 502 |
$cf7_key |
| 503 |
), '4.11.0', "cf7sg_custom_{$this->tag_id}" ); |
| 504 |
if(empty($options)) $options = array(); |
| 505 |
/** @since 4.11.0 more versatile to allow plugins to customise the option attributes */ |
| 506 |
$custom_options = apply_filters("cf7sg_custom_{$this->tag_id}", array(), $tag, $cf7_key); |
| 507 |
|
| 508 |
if(!empty($custom_options)) $options = $custom_options; |
| 509 |
} |
| 510 |
} |
| 511 |
if($filter_options){ //true if either taxonomy or post dropdpwn...but deprecated. |
| 512 |
/** |
| 513 |
* Allow filtering of options populated by posts or taxonomies. |
| 514 |
* @param array $options an array of $value=>$name pairs which will be used for populating select options. |
| 515 |
* @param WPCF7_FormTag $tag the field name being populated. |
| 516 |
* @param String $cf7_key the form unique key. |
| 517 |
* @return array array of $value=>$name pairs which will be used for populating select options. |
| 518 |
* @since 1.4.0 |
| 519 |
*/ |
| 520 |
$options = apply_filters_deprecated('cf7sg_dynamic_dropdown_filter_options', array( $options, $tag->name, $cf7_key), '4.11.0'); |
| 521 |
} |
| 522 |
|
| 523 |
$tag_name = sanitize_html_class( $tag->name ); |
| 524 |
/** @since 3.3.0 enable custom attributes on select element*/ |
| 525 |
$attributes = apply_filters_deprecated('cf7sg_dynamic_dropdown_attributes', |
| 526 |
array(array(), $tag->name, $cf7_key), |
| 527 |
'4.11.0',"cf7sg_{$this->tag_id}_attributes"); |
| 528 |
$attributes = apply_filters("cf7sg_{$this->tag_id}_attributes", $attributes, $tag, $cf7_key); |
| 529 |
if(!empty($id)) $attributes['id']=$id; |
| 530 |
$attributes['name']=$tag->name; |
| 531 |
|
| 532 |
/** @since 4.0 */ |
| 533 |
if(isset($other_attrs['permalinks'])) $class.=' cf7sg-permalinks'; |
| 534 |
$attributes['class']=$class; |
| 535 |
|
| 536 |
/** |
| 537 |
* @since 2.2 allows custom filtered $options to be an html string. |
| 538 |
*/ |
| 539 |
if(!is_array($options) && !is_string($options)) $options = array(); |
| 540 |
/** |
| 541 |
* Filter dynamic dropdown default empty value label. |
| 542 |
* @param String $label the label for the default value, this is null by default and not shown. |
| 543 |
* @param WPCF7_FormTag $tag the field name being populated. |
| 544 |
* @param String $cf7_key the form unique key. |
| 545 |
* @return String the label for the default value, returning a non-null value with display this as the first option. |
| 546 |
*/ |
| 547 |
$default_label = apply_filters_deprecated('cf7sg_dynamic_dropdown_default_value', |
| 548 |
array( |
| 549 |
null, |
| 550 |
$tag->name, |
| 551 |
$cf7_key |
| 552 |
),'4.11.0', "cf7sg_{$this->tag_id}_default_value"); |
| 553 |
$default_label = apply_filters("cf7sg_{$this->tag_id}_default_value", $default_label, $tag, $cf7_key); |
| 554 |
|
| 555 |
if(!is_null($default_label)){ |
| 556 |
$options=array_merge(array(''=>array($default_label, array(), array())), $options); |
| 557 |
$selected=''; |
| 558 |
} |
| 559 |
$other_classes=''; |
| 560 |
if(!empty($other_attrs)) $other_classes = ' cf7sg-dl-'.implode(' cf7sg-dl-', array_keys($other_attrs)); |
| 561 |
$html = '<span class="wpcf7-form-control-wrap cf7sg-dl-'. $source['source'] .' '. $tag_name . $other_classes . '">' . PHP_EOL; |
| 562 |
//add other attributes found in tag. |
| 563 |
$attributes = array_merge($attributes, $data_attrs); |
| 564 |
|
| 565 |
/** |
| 566 |
* Register a [dynamic_display] shortcode with CF7. |
| 567 |
* @since 4.11.0 |
| 568 |
* @param Array $attributes array of attribute key=>value pairs to be included in the html element tag. |
| 569 |
* @param Array $options array of $value=>array($label,$option_attributes, $children) pairs of options, where $option_attributes is an array of attributest to be added to the option element, and $children is an array of options with the same format representing the nested children of this option parent. |
| 570 |
* @param Array $other_attrs array of additional attributes $attr=>true (e.g. permalink=>1) added in the form field tag. |
| 571 |
* @param String $selected default selected value. |
| 572 |
* @return String an html string representing the input field to a=be added to the field wrapper and into the form. |
| 573 |
*/ |
| 574 |
$html .= apply_filters("cf7sg_{$this->tag_id}_html_field", '', $attributes, $options, $other_attrs, $selected ); |
| 575 |
$html .= PHP_EOL . '</span>'. PHP_EOL; |
| 576 |
|
| 577 |
return $html; |
| 578 |
} |
| 579 |
/** |
| 580 |
* Retrieve terms to list in dynamic lists. |
| 581 |
* |
| 582 |
*@since 4.11.0 |
| 583 |
*@param Array $source array of taxonomy attributes. |
| 584 |
*@param Array $branch array of term IDs representing the current branch. |
| 585 |
*@return Array array of WP_Term objects. |
| 586 |
*/ |
| 587 |
protected function get_taxonomy_terms($source, $branch, $tag, $cf7_key){ |
| 588 |
$options = array(); |
| 589 |
$taxonomy_query= array('hide_empty' => false, 'taxonomy'=>$source['taxonomy']); |
| 590 |
if(!empty($branch)) $taxonomy_query['parent']=end($branch); |
| 591 |
$taxonomy_query = apply_filters_deprecated('cf7sg_dynamic_dropdown_taxonomy_query', |
| 592 |
array( |
| 593 |
$taxonomy_query, |
| 594 |
$tag->name, |
| 595 |
$cf7_key |
| 596 |
), '4.11.0', "cf7sg_{$this->tag_id}_taxonomy_query" ); |
| 597 |
$taxonomy_query = apply_filters("cf7sg_{$this->tag_id}_taxonomy_query", $taxonomy_query, $branch, $tag, $cf7_key, $branch); |
| 598 |
//check the WP version |
| 599 |
if(empty($taxonomy_query)) return array(); //nothing more to be done here. |
| 600 |
global $wp_version; |
| 601 |
if ( $wp_version >= 4.5 ) { |
| 602 |
$terms = get_terms($taxonomy_query); //WP>= 4.5 the get_terms does not take a taxonomy slug field |
| 603 |
}else{ |
| 604 |
$terms = get_terms($source['taxonomy'], $taxonomy_query); |
| 605 |
} |
| 606 |
if( is_wp_error( $terms )) { |
| 607 |
debug_msg($terms, 'Unable to retrieve taxonomy <em>'.$source['taxonomy'].'</em> terms'); |
| 608 |
$terms = array(); |
| 609 |
}else{ |
| 610 |
foreach($terms as $term){ |
| 611 |
/** |
| 612 |
* Filter dropdown options labels. |
| 613 |
* @param String $label option label value. |
| 614 |
* @param mixed $term the term object being used to populate this option. |
| 615 |
* @param WPCF7_FormTag $tag the field name being populated. |
| 616 |
* @param String $cf7_key the form unique key. |
| 617 |
* @return String $label option label value. |
| 618 |
* @since 2.0.0 |
| 619 |
*/ |
| 620 |
$label = $term->name; |
| 621 |
$label = apply_filters_deprecated('cf7sg_dynamic_dropdown_option_label', |
| 622 |
array( |
| 623 |
$label, |
| 624 |
$term, |
| 625 |
$tag->name, |
| 626 |
$cf7_key |
| 627 |
), '4.11.0', "cf7sg_{$this->tag_id}_option_label" ); |
| 628 |
$label = apply_filters("cf7sg_{$this->tag_id}_option_label", $label, $term, $tag, $cf7_key); |
| 629 |
|
| 630 |
/** |
| 631 |
* Filter dropdown options attributes. |
| 632 |
* @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. |
| 633 |
* @param mixed either WP_Post or WP_Term object being used to populate this option. |
| 634 |
* @param WPCF7_FormTag $tag the field name being populated. |
| 635 |
* @param String $cf7_key the form unique key. |
| 636 |
* @return Array array of $value=>$name pairs which will be used for populating select options attributes. |
| 637 |
* @since 2.0.0 |
| 638 |
*/ |
| 639 |
$attributes = apply_filters_deprecated( 'cf7sg_dynamic_dropdown_option_attributes', |
| 640 |
array( |
| 641 |
array(), |
| 642 |
$term, |
| 643 |
$tag->name, |
| 644 |
$cf7_key ), '4.11.0', "cf7sg_{$this->tag_id}_options_attributes" ); |
| 645 |
/** @since 4.11.0 more versatile to allow plugins to customise the option attributes */ |
| 646 |
$attributes = apply_filters("cf7sg_{$this->tag_id}_options_attributes", $attributes, $term, $tag, $cf7_key); |
| 647 |
if(is_array($attributes)){ |
| 648 |
if(isset($attributes['class'])){ |
| 649 |
if(!is_array($attributes['class'])) $attributes['class'] = array($attributes['class']); |
| 650 |
$attributes['class'][]='cf7sg-dl'; |
| 651 |
}else $attributes['class']='cf7sg-dl'; |
| 652 |
}else $attributes = array('class'=>'cf7sg-dl'); |
| 653 |
|
| 654 |
$children = array(); |
| 655 |
//need to build hierarchy if branch is active. |
| 656 |
if(!empty($branch)){ |
| 657 |
$branch[]=$term->term_id; |
| 658 |
$children = $this->get_taxonomy_terms($source, $branch, $tag, $cf7_key); |
| 659 |
} |
| 660 |
$options[$term->slug] = array($label, $attributes, $children); |
| 661 |
} |
| 662 |
} |
| 663 |
return $options; |
| 664 |
} |
| 665 |
/** |
| 666 |
* Function to update form classes to track dependencies. |
| 667 |
* |
| 668 |
* @since 4.11.0 |
| 669 |
* @param mixed $class array of classes or string to set class for form and dependency loading . |
| 670 |
* @param Object $cf7_form cf7 post id against which set the c$lass . |
| 671 |
**/ |
| 672 |
public function update_form_classes($class, $cf7_id=null){ |
| 673 |
if(empty($cf7_id)){ |
| 674 |
//get latest form |
| 675 |
if(function_exists('wpcf7_get_current_contact_form')){ |
| 676 |
$cf7_form = wpcf7_get_current_contact_form(); |
| 677 |
if(!empty($cf7_form)) $cf7_id = $cf7_form->id(); |
| 678 |
} |
| 679 |
} |
| 680 |
if(empty($cf7_id)){ |
| 681 |
debug_msg('Unable to get Contact Form 7 ID to set class: '.$class); |
| 682 |
return; |
| 683 |
} |
| 684 |
$classes = get_post_meta($cf7_id, '_cf7sg_classes', true); |
| 685 |
if(empty($classes)){ |
| 686 |
$classes = array(); |
| 687 |
} |
| 688 |
if(is_array($class)){ |
| 689 |
foreach($class as $class_name){ |
| 690 |
$classes[$class_name] = true; |
| 691 |
} |
| 692 |
}else{ |
| 693 |
$classes[$class] = true; |
| 694 |
} |
| 695 |
update_post_meta($cf7_id, '_cf7sg_classes', $classes); |
| 696 |
return; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
if(!function_exists('cf7sg_get_dynamic_lists')){ |
| 701 |
function cf7sg_get_dynamic_lists($tag_id=null){ |
| 702 |
return CF7SG_Dynamic_list::get_instances($tag_id); |
| 703 |
} |
| 704 |
} |
| 705 |
/* build a dynamic select dropdown */ |
| 706 |
if( !function_exists('cf7sg_create_dynamic_select_tag') ){ |
| 707 |
function cf7sg_create_dynamic_select_tag(){ |
| 708 |
//check if there is an existing instance in memory. |
| 709 |
$dl = CF7SG_Dynamic_List::get_instances('dynamic_select'); |
| 710 |
if(false === $dl){ |
| 711 |
$dl = new CF7SG_Dynamic_List('dynamic_select',__( 'dynamic-dropdown', 'cf7-grid-layout' )); |
| 712 |
$dl->set_styles(array( |
| 713 |
'select' => __('HTML Select field','cf7-grid-layout'), |
| 714 |
'select2' => '<a target="_blank" href="https://select2.org/getting-started/basic-usage">'.__('jQuery Select2','cf7-grid-layout').'</a>' |
| 715 |
),array( |
| 716 |
'select2'=>array( |
| 717 |
'tags'=>array( |
| 718 |
'label'=> '<a target="_blank" href="https://select2.org/tagging">'.__('Enable user options','cf7-grid-layout').'</a>', |
| 719 |
'attrs'=>'disabled' |
| 720 |
) |
| 721 |
) |
| 722 |
)); |
| 723 |
$dl->set_others_extras(array( |
| 724 |
'multiple'=> '<a target="_blank" href="https://www.w3schools.com/tags/att_select_multiple.asp">'.__('Enable multiple selection','cf7-grid-layout').'</a>' |
| 725 |
)); |
| 726 |
} |
| 727 |
return $dl; |
| 728 |
} |
| 729 |
} |
| 730 |
add_action('cf7sg_register_dynamic_lists', 'cf7sg_create_dynamic_select_tag'); |
| 731 |
/** |
| 732 |
* Dynamic checkbox |
| 733 |
* @since 4.11.0 |
| 734 |
*/ |
| 735 |
if( !function_exists('cf7sg_create_dynamic_checkbox_tag') ){ |
| 736 |
function cf7sg_create_dynamic_checkbox_tag(){ |
| 737 |
//check if there is an existing instance in memory. |
| 738 |
$dl = CF7SG_Dynamic_List::get_instances('dynamic_checkbox'); |
| 739 |
if(false === $dl){ |
| 740 |
$dl = new CF7SG_Dynamic_List('dynamic_checkbox',__( 'dynamic-checkbox', 'cf7-grid-layout' )); |
| 741 |
$dl->set_styles(array( |
| 742 |
'cf7sg-hybriddd'=> '<a href="https://aurovrata.github.io/hybrid-html-dropdown/">Hybrid Dropdown</a>', |
| 743 |
'cf7sg-treeview'=> sprintf(__('<a href="%s">Treeview dropdown</a>','cf7-grid-layout'),'https://aurovrata.github.io/hybrid-html-dropdown/examples/#hybrid-dropdown-with-treeview-selection'), |
| 744 |
'cf7sg-imagehdd'=> sprintf(__('<a href="%s">Image dropdown</a>','cf7-grid-layout'),'https://aurovrata.github.io/hybrid-html-dropdown/examples/#dropdown-list-with-with-custom-labels-with-images'), |
| 745 |
'cf7sg-imagegrid'=> sprintf(__('<a href="%s">Image grid</a>, no dropdown','cf7-grid-layout'),'https://aurovrata.github.io/hybrid-html-dropdown/examples/#dropdown-list-with-with-custom-labels-with-images') |
| 746 |
),array( |
| 747 |
'cf7sg-imagehdd'=>sprintf('('.__('<a href="%s">Filter</a> term images','cf7-grid-layout').')', 'javascript:void(0);'), |
| 748 |
'cf7sg-imagegrid'=>sprintf('('.__('<a href="%s">Filter</a> term images','cf7-grid-layout').')', 'javascript:void(0);') |
| 749 |
)); |
| 750 |
$dl->set_others_extras_radio(); //default is checkbox. |
| 751 |
$dl->set_others_extras(array( |
| 752 |
'limit'=> __('Limit selections','cf7-grid-layout').'<input type="number" min="1" value="1" class="max-selection"/><input type="hidden" value="" class="data-attribute" />', |
| 753 |
'nolimit' => __('Unlimited selections','cf7-grid-layout') |
| 754 |
)); |
| 755 |
$dl->allow_nesting(); //flag as able to handle hierarchical lists. |
| 756 |
} |
| 757 |
return $dl; |
| 758 |
} |
| 759 |
} |
| 760 |
add_action('cf7sg_register_dynamic_lists', 'cf7sg_create_dynamic_checkbox_tag'); |
| 761 |
|