| 1 |
<?php |
| 2 |
//helper snippets |
| 3 |
/* |
| 4 |
Available replacement varaibles: |
| 5 |
{$form_key} - unique form key. |
| 6 |
{$form_key_slug} - unique form key slug for function names. |
| 7 |
($field_type) - tag field id, eg dynamic_select. |
| 8 |
($field_name) - unique field name. |
| 9 |
($field_name_slug) - unique field name slug for function names. |
| 10 |
[dqt] - double quote for html attributes. |
| 11 |
*/ |
| 12 |
$post_my_form_only = ' no-post-my-form'; |
| 13 |
if(is_plugin_active( 'post-my-contact-form-7/cf7-2-post.php' )){ |
| 14 |
$post_my_form_only=''; |
| 15 |
} |
| 16 |
?> |
| 17 |
<li class="cf7sg-tag-dynamic_list-post"> |
| 18 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_{$field_type}_post_query','{$field_name_slug}_dynamic_list',10,3); |
| 19 |
/** |
| 20 |
* Filter post query for dynamic dropdown options. |
| 21 |
* @param array $args an arra of query terms. |
| 22 |
* @param WPCF7_FormTag $tag the field being populated. |
| 23 |
* @param string $cf7_key the form unique key. |
| 24 |
* @return array an arra of query terms. |
| 25 |
*/ |
| 26 |
function {$field_name_slug}_dynamic_list($query_args, $tag, $cf7_key){ |
| 27 |
//if you have a dynamic dropdown field with posts as list source. |
| 28 |
//$query_args array to filter query arguments used to populate dynamic dropdown of posts. |
| 29 |
//these arguments are passed to the function get_posts($query_args). (codex: https://codex.wordpress.org/Template_Tags/get_posts) |
| 30 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $tag->name){ |
| 31 |
return $query_args; |
| 32 |
} |
| 33 |
//setup your custom query... |
| 34 |
return $query_args; |
| 35 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('query arguments to retrieve posts for dynamic dropdown.','cf7-grid-layout')?> |
| 36 |
</li> |
| 37 |
<li class="cf7sg-tag-dynamic_list-taxonomy"> |
| 38 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_{$field_type}_option_label','{$field_name_slug}_dynamic_option_label',10,4); |
| 39 |
/** |
| 40 |
* Filter dropdown options label for dynamic drodpwn list of taxonomy terms. |
| 41 |
* @param string $label option label value. |
| 42 |
* @param WP_Term $term the term object being used to populate this option. |
| 43 |
* @param WPCF7_FormTag $tag the field being populated. |
| 44 |
* @param string $cf7_key the form unique key. |
| 45 |
* @return string $label option label value. |
| 46 |
*/ |
| 47 |
function {$field_name_slug}_dynamic_option_label($label, $term, $tag, $cf7_key){ |
| 48 |
//these are the label users will see when the dropdown opens. |
| 49 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $tag->name){ |
| 50 |
return $label; |
| 51 |
} |
| 52 |
return $label; |
| 53 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the option label.','cf7-grid-layout')?> |
| 54 |
</li> |
| 55 |
<li class="cf7sg-tag-dynamic_list-taxonomy"> |
| 56 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_{$field_type}_taxonomy_query','{$field_name_slug}_taxonomy_query',10,4); |
| 57 |
/** |
| 58 |
* Filter dropdown taxonomy query parameter. |
| 59 |
* (see https://developer.wordpress.org/reference/classes/wp_term_query/__construct/) |
| 60 |
* @param array $args array of taxonomy query attributes. |
| 61 |
* @param WPCF7_FormTag $tag the field being populated. |
| 62 |
* @param string $cf7_key the form unique key. |
| 63 |
* @param array $branch an array of term IDs representing the current taxonomy branch being queried, the last term is the query parent value. |
| 64 |
* @return array of query attributes. |
| 65 |
*/ |
| 66 |
function {$field_name_slug}_taxonomy_query($args, $tag, $cf7_key, $branch){ |
| 67 |
//these are the label users will see when the dropdown opens. |
| 68 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $tag->name){ |
| 69 |
return $args; |
| 70 |
} |
| 71 |
//use only the child terms of a parent. |
| 72 |
if($args['parent']!=0) $args=null; |
| 73 |
// or if you want to list only the 2nd level of your tree, |
| 74 |
if(count($args)>2) $args=null; |
| 75 |
return $args; |
| 76 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the taxonomy query.','cf7-grid-layout')?> |
| 77 |
</li> |
| 78 |
<li class="cf7sg-tag-dynamic_list-taxonomy cf7sg_filter_taxonomy_images"> |
| 79 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_{$field_type}_options_attributes','{$field_name_slug}_dynamic_option_attributes',10,4); |
| 80 |
/** |
| 81 |
* Filter dropdown options attributes. |
| 82 |
* @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. |
| 83 |
* @param WP_Term $term the term object being used to populate this option. |
| 84 |
* @param WPCF7_FormTag $tag the field tag object. |
| 85 |
* @param string $cf7_key the form unique key. |
| 86 |
* @return array array of $value=>$name pairs which will be used for populating select options attributes. |
| 87 |
*/ |
| 88 |
function {$field_name_slug}_dynamic_option_attributes($attributes, $term, $tag, $cf7_key){ |
| 89 |
//these are the optional attributes you can add to your dynamic list option when populating from taxonomy terms. |
| 90 |
//this is especially useful in the context of select2 fields, as much richer options can be built. |
| 91 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $tag->name){ |
| 92 |
return $attributes; |
| 93 |
} |
| 94 |
//example: $attributes['class'] = array($term->slug); |
| 95 |
//if this is a dynamic_checkbox with imagegrid option selected, then you will will need to provide image sources for each term, |
| 96 |
//$attributes['data-thumbnail'] = <url of image> |
| 97 |
|
| 98 |
return $attributes; |
| 99 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('options attributes.','cf7-grid-layout')?> |
| 100 |
</li> |
| 101 |
<li class="cf7sg-tag-dynamic_list-post"> |
| 102 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_{$field_type}_option_label','{$field_name_slug}_dynamic_option_label',10,4); |
| 103 |
/** |
| 104 |
* Filter dropdown options label for dynamic drodpwn list of existing posts. |
| 105 |
* @param string $label option label value. |
| 106 |
* @param WP_Post $post the post object being used to populate this option. |
| 107 |
* @param WPCF7_FormTag $tag the field being populated. |
| 108 |
* @param string $cf7_key the form unique key. |
| 109 |
* @return string $label option label value. |
| 110 |
*/ |
| 111 |
function {$field_name_slug}_dynamic_option_label($label, $post, $tag, $cf7_key){ |
| 112 |
//these are the label users will see when the dropdown opens. |
| 113 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $tag->name){ |
| 114 |
return $label; |
| 115 |
} |
| 116 |
//setup a custom label |
| 117 |
return $label; |
| 118 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the option label.','cf7-grid-layout')?> |
| 119 |
</li> |
| 120 |
<li class="cf7sg-tag-dynamic_list-post"> |
| 121 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_list_options_attributes','{$field_name_slug}_dynamic_option_attributes',10,4); |
| 122 |
/** |
| 123 |
* Filter dropdown options attributes. |
| 124 |
* @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. |
| 125 |
* @param WP_Post $post the post object being used to populate this option. |
| 126 |
* @param WPCF7_FormTag $tag the field tag object. |
| 127 |
* @param string $cf7_key the form unique key. |
| 128 |
* @return array array of $value=>$name pairs which will be used for populating select options attributes. |
| 129 |
*/ |
| 130 |
function {$field_name_slug}_dynamic_option_attributes($attributes, $post, $tag, $cf7_key){ |
| 131 |
//these are the optional attributes you can add to your dynamic list option when populating from existing posts. |
| 132 |
//this is especially useful in the conctext of select2 fields, as much richer options can be built. |
| 133 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !==$tag->name){ |
| 134 |
return $attributes; |
| 135 |
} |
| 136 |
//example: $attributes['class'] = array('author-'.$post->post_author); |
| 137 |
//if you selected the Image Grid option on dynamic_checkbox field, the plugin inserts the post featured image as, |
| 138 |
//$attributes['data-thumbnail'] = <url of 'thumbnail' sized image> which you can change if you want. |
| 139 |
return $attributes; |
| 140 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the option attributes.','cf7-grid-layout')?> |
| 141 |
</li> |
| 142 |
<li class="cf7sg-tag-dynamic_list"> |
| 143 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_{$field_type}_default_value','{$field_name_slug}_dynamic_default_option',10,3); |
| 144 |
/** |
| 145 |
* Filter dynamic dropdown default empty label. |
| 146 |
* @param string $label the label for the default value, this is null by default and not shown. |
| 147 |
* @param WPCF7_FormTag $tag the field being populated. |
| 148 |
* @param string $cf7_key the form unique key. |
| 149 |
* @return string the label for the default value, returning a non-null value with display this as the first option. |
| 150 |
*/ |
| 151 |
function {$field_name_slug}_dynamic_default_option($default_label, $tag, $cf7_key){ |
| 152 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $tag->name){ |
| 153 |
return $default_label; |
| 154 |
} |
| 155 |
$default_label = 'Please select an option...'; |
| 156 |
return $default_label; |
| 157 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the default option label.','cf7-grid-layout')?> |
| 158 |
</li> |
| 159 |
<li class="cf7sg-tag-dynamic_select-post-tags <?=$post_my_form_only?>"> |
| 160 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_new_post','{$field_name_slug}_select2_newpost',10,7); |
| 161 |
/** |
| 162 |
* Filter custom options from tag enabled select2 dynamic-dropdown fields |
| 163 |
* where the source of options come from post titles. Filter is fired when a new value is submitted. |
| 164 |
* This plugin does not take any further action, ie no post of $post_type will be created. It is upto you to do so and return the slug of the newly created post. |
| 165 |
* @param String $post_name the new post slug. |
| 166 |
* @param String $field name of the form field. |
| 167 |
* @param String $title new value being submitted for a new post title. |
| 168 |
* @param String $post_type the post type from which this dropdown was built |
| 169 |
* @param Array $args an array of additional parameters that was set in the tag, for example the taxonomy and terms from which to filter the posts for the dynamic list. |
| 170 |
* @param Array $submitted_data array of other submitted $field=>$value pairs. |
| 171 |
* @param String $key the form unique key. |
| 172 |
* @return String value to be stored for this field, the post slug. |
| 173 |
*/ |
| 174 |
function {$field_name_slug}_select2_newpost($post_name, $field, $title, $post_type, $args, $submitted_data, $cf7_key){ |
| 175 |
/* |
| 176 |
Filter active when saving forms using Post My CF7 Form plugin. |
| 177 |
select2 dynamic dropdowns can have an option for custom user values to be selected/inserted. |
| 178 |
If a user searches an option which is not available they can simply insert it. You need to enable the tagging functionality (https://select2.org/tagging) by adding a 'tags' class to your cf7 tag. |
| 179 |
|
| 180 |
When this option is enabled for a dynamic dropdown with posts as its source of option list, then you need to hook this filter which is fired when a the user has submitted a new custom post title in the select2 field. This plugin takes not action to create the new post. This is your reponsibility. |
| 181 |
$post_name new post slug to set. |
| 182 |
$field name of dynamic dropdown. |
| 183 |
$title string submitted by the user. |
| 184 |
$post_type the post type which was used to build the original dropdown list. |
| 185 |
$args an array of any additional arguments set to limit the original dropdown list, such as taxonomy terms. |
| 186 |
$cf7_key unique form key to identify your form, $cf7_id is its post_id. |
| 187 |
$submitted_data array of other submitted $field=>$value pairs. |
| 188 |
*/ |
| 189 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $field){ |
| 190 |
return $default; |
| 191 |
} |
| 192 |
//create your new post using wp_insert_post(); |
| 193 |
$post_name = 'new_submision'; |
| 194 |
return $post_name; |
| 195 |
}" href="javascript:void(0);"><?=__('Insert','cf7-grid-layout')?></a> <?=__('user added post.','cf7-grid-layout')?> |
| 196 |
</li> |
| 197 |
<li class="cf7sg-tag-dynamic_select-post-tags <?=$post_my_form_only?>"> |
| 198 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_new_term','{$field_name_slug}_select2_newterm',10,7); |
| 199 |
/** |
| 200 |
* Filter custom options from tag enabled select2 dynamic-select fields |
| 201 |
* where the source of options come from taxonomy terms. Filter is fired when a new value is submitted. |
| 202 |
* This plugin will attempt to insert the new term, whether filtered or not, |
| 203 |
* however you can return an empty string to stop the term being inserted. |
| 204 |
* @param String $term_name the new term name. |
| 205 |
* @param String $field name of the form field. |
| 206 |
* @param String $taxonomy the taxonomy for which the new term is to be added to. |
| 207 |
* @param Array $submitted_data array of other submitted $field=>$value pairs. |
| 208 |
* @param String $key the form unique key. |
| 209 |
* @return String name of the new term, empty value will not be inserted. |
| 210 |
*/ |
| 211 |
function {$field_name_slug}_select2_newpost($term_name, $field, $taxonomy, $submitted_data, $cf7_key){ |
| 212 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $field){ |
| 213 |
return $default; |
| 214 |
} |
| 215 |
//change the name if need be. |
| 216 |
return $term_name; |
| 217 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('user added term.','cf7-grid-layout')?> |
| 218 |
</li> |
| 219 |
<li class="cf7sg-tag-dynamic_select-filter <?=$post_my_form_only?>"> |
| 220 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_filter_select2_submission','{$field_name_slug}_select2_filter_values',10,4); |
| 221 |
/** |
| 222 |
* Filter custom otions from tag enabled select2 dynamic-dropdown fields |
| 223 |
* where the source of options come from a filter. Filter is fired when values are submitted. |
| 224 |
* Return updated values if any are custom values so that saved/draft submissions will reflect the correct value saved in the DB, |
| 225 |
* @param array $values an array submitted values (several values can be submitted in the case of a tabbed/table input field). |
| 226 |
* @param string $field_name the name of the form field. |
| 227 |
* @param array $submitted_data array of other submitted $field=>$value pairs. |
| 228 |
* @param string $key the form unique key. |
| 229 |
* @return string value to be saved for this field. |
| 230 |
*/ |
| 231 |
function {$field_name_slug}_select2_filter_values($values, $field, $submitted_data, $cf7_key){ |
| 232 |
/* |
| 233 |
Filter active when saving forms using Post My CF7 Form plugin. |
| 234 |
For a dynamic dropdown with custom option list set by a filter, you need to hook this filter which is fired when a user has submitted select2 field value(s). You can return the filtered selected value(s) which will be stored in the mapped post. |
| 235 |
This filter is especially useful when the select2 tags have been enabled (https://select2.org/tagging) and your user may have submitted new values which your original filtered list did not contain. |
| 236 |
$values selected by user. |
| 237 |
$field name of dynamic dropdown. |
| 238 |
$cf7_key unique form key to identify your form, $cf7_id is its post_id. |
| 239 |
$submitted_data array of other submitted $field=>$value pairs. |
| 240 |
*/ |
| 241 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $field){ |
| 242 |
return $values; |
| 243 |
} |
| 244 |
//do something... |
| 245 |
return $values; |
| 246 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('user added option.','cf7-grid-layout')?> |
| 247 |
</li> |
| 248 |
<li class="cf7sg-tag-dynamic_list-filter cf7sg_filter_source"> |
| 249 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_custom_{$field_type}','{$field_name_slug}_dynamic_options',10,3); |
| 250 |
/** |
| 251 |
* Filter dropdown options for dynamic drodpwn list of taxonomy terms. |
| 252 |
* @param Array $options the option to filter. |
| 253 |
* @param WPCF7_FormTag $tag field tag object. |
| 254 |
* @param string $cf7_key the form unique key. |
| 255 |
* @return Array $options return either an array of <option value>=><option label> pairs or 2 arrays, one for values and another for attributes. |
| 256 |
*/ |
| 257 |
function {$field_name_slug}_dynamic_options($options, $tag, $cf7_key){ |
| 258 |
if('{$form_key}'!==$cf7_key || '{$field_name}' !== $tag->name){ |
| 259 |
return $options; |
| 260 |
} |
| 261 |
//these are the label users will see when the dropdown opens. |
| 262 |
//you can group your options if need be. Let's assume you have an array of arrays of data to display in groups. |
| 263 |
$data = ... //fetch your data, either from the database or some other source. |
| 264 |
foreach($data as $value=>$label){ |
| 265 |
$options[$value]=$label; |
| 266 |
//if you are displaying more complex select2 fields, or imagegrid for dynamic checkbox, then add extra parameters into a 2nd array of attributes, |
| 267 |
$options['values'][$value]=$label; |
| 268 |
$options['attributes'][$values]= array('data-thumbnail'=>'<image url>'); |
| 269 |
} |
| 270 |
return $options; |
| 271 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('custom options .','cf7-grid-layout')?> |
| 272 |
</li> |
| 273 |
<li class="cf7sg-tag-dynamic_list cf7sg-tag-dynamic_list cf7sg-tag-radio cf7sg-tag-checkbox cf7sg-tag-select cf7sg-tag-acceptance"> |
| 274 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_mail_tag_{$field_type}','{$field_name_slug}_mail_tag_value',10,3); |
| 275 |
/** |
| 276 |
* Filter the value inserted in the mail tag. |
| 277 |
* @since 2.9.0. |
| 278 |
* @param string $replaced value to filter. |
| 279 |
* @param string $name name of the field being inserted in the mail. |
| 280 |
* @param string $cf7_key unique form key identifier. |
| 281 |
* @return string a value to replace. |
| 282 |
*/ |
| 283 |
function {$field_name_slug}_mail_tag_value($replaced, $name, $cf7_key){ |
| 284 |
//check to make sure you have the right field in the right form. |
| 285 |
if( '{$field_name_slug}' !== $name || '{$form_key}'!==$cf7_key) return $replaced; |
| 286 |
//$replaced is the value inserted in the mail if you have used this field as a mail tag. |
| 287 |
$replaced; //this is currently set to the selected value and is a slug, you need to programmatically fetch your value and set it up. |
| 288 |
return $replaced; |
| 289 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the mail tag value.','cf7-grid-layout')?> |
| 290 |
</li> |
| 291 |
|