| 1 |
<?php |
| 2 |
//helper snippets |
| 3 |
$post_my_form_only = ' no-post-my-form'; |
| 4 |
if(is_plugin_active( 'post-my-contact-form-7/cf7-2-post.php' )){ |
| 5 |
$post_my_form_only=''; |
| 6 |
} |
| 7 |
?> |
| 8 |
<li class="cf7sg-tag-dynamic_select-post"> |
| 9 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_post_query','{$field_name_slug}_dynamic_list',10,3); |
| 10 |
/** |
| 11 |
* Filter post query for dynamic dropdown options. |
| 12 |
* @param array $args an arra of query terms. |
| 13 |
* @param string $name the field name being populated. |
| 14 |
* @param string $cf7_key the form unique key. |
| 15 |
* @return array an arra of query terms. |
| 16 |
*/ |
| 17 |
function {$field_name_slug}_dynamic_list($query_args, $field, $cf7_key){ |
| 18 |
//if you have a dynamic dropdown field with posts as list source. |
| 19 |
//$query_args array to filter query arguments used to populate dynamic dropdown of posts. |
| 20 |
//these arguments are passed to the function get_posts($query_args). (codex: https://codex.wordpress.org/Template_Tags/get_posts) |
| 21 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !== $field){ |
| 22 |
return $query_args; |
| 23 |
} |
| 24 |
//setup your custom query... |
| 25 |
return $query_args; |
| 26 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('query arguments to retrieve posts for dynamic dropdown.','cf7-grid-layout')?> |
| 27 |
</li> |
| 28 |
<li class="cf7sg-tag-dynamic_select-taxonomy"> |
| 29 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_option_label','{$field_name_slug}_dynamic_option_label',10,4); |
| 30 |
/** |
| 31 |
* Filter dropdown options label for dynamic drodpwn list of taxonomy terms. |
| 32 |
* @param string $label option label value. |
| 33 |
* @param WP_Term $term the term object being used to populate this option. |
| 34 |
* @param string $name the field name being populated. |
| 35 |
* @param string $cf7_key the form unique key. |
| 36 |
* @return string $label option label value. |
| 37 |
*/ |
| 38 |
function {$field_name_slug}_dynamic_option_label($label, $term, $name, $cf7_key){ |
| 39 |
//these are the label users will see when the dropdown opens. |
| 40 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !== $field){ |
| 41 |
return $label; |
| 42 |
} |
| 43 |
return $label; |
| 44 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the option label.','cf7-grid-layout')?> |
| 45 |
</li> |
| 46 |
<li class="cf7sg-tag-dynamic_select-taxonomy"> |
| 47 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_option_attributes','{$field_name_slug}_dynamic_option_attributes',10,4); |
| 48 |
/** |
| 49 |
* Filter dropdown options attributes. |
| 50 |
* @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. |
| 51 |
* @param WP_Term $term the term object being used to populate this option. |
| 52 |
* @param string $name the field name being populated. |
| 53 |
* @param string $cf7_key the form unique key. |
| 54 |
* @return array array of $value=>$name pairs which will be used for populating select options attributes. |
| 55 |
*/ |
| 56 |
function {$field_name_slug}_dynamic_option_attributes($attributes, $term, $name, $cf7_key){ |
| 57 |
//these are the optional attributes you can add to your dynamic list option when populating from taxonomy terms. |
| 58 |
//this is especially useful in the context of select2 fields, as much richer options can be built. |
| 59 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !== $field){ |
| 60 |
return $attributes; |
| 61 |
} |
| 62 |
//example: $attributes['class'] = array($term->slug); |
| 63 |
return $attributes; |
| 64 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the option attributes.','cf7-grid-layout')?> |
| 65 |
</li> |
| 66 |
<li class="cf7sg-tag-dynamic_select-post"> |
| 67 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_option_label','{$field_name_slug}_dynamic_option_label',10,4); |
| 68 |
/** |
| 69 |
* Filter dropdown options label for dynamic drodpwn list of existing posts. |
| 70 |
* @param string $label option label value. |
| 71 |
* @param WP_Post $post the post object being used to populate this option. |
| 72 |
* @param string $name the field name being populated. |
| 73 |
* @param string $cf7_key the form unique key. |
| 74 |
* @return string $label option label value. |
| 75 |
*/ |
| 76 |
function {$field_name_slug}_dynamic_option_label($label, $post, $name, $cf7_key){ |
| 77 |
//these are the label users will see when the dropdown opens. |
| 78 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !== $field){ |
| 79 |
return $label; |
| 80 |
} |
| 81 |
//setup a custom label |
| 82 |
return $label; |
| 83 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the option label.','cf7-grid-layout')?> |
| 84 |
</li> |
| 85 |
<li class="cf7sg-tag-dynamic_select-post"> |
| 86 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_option_attributes','{$field_name_slug}_dynamic_option_attributes',10,4); |
| 87 |
/** |
| 88 |
* Filter dropdown options attributes. |
| 89 |
* @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. |
| 90 |
* @param WP_Post $post the post object being used to populate this option. |
| 91 |
* @param string $name the field name being populated. |
| 92 |
* @param string $cf7_key the form unique key. |
| 93 |
* @return array array of $value=>$name pairs which will be used for populating select options attributes. |
| 94 |
*/ |
| 95 |
function {$field_name_slug}_dynamic_option_attributes($attributes, $post, $name, $cf7_key){ |
| 96 |
//these are the optional attributes you can add to your dynamic list option when populating from existing posts. |
| 97 |
//this is especially useful in the conctext of select2 fields, as much richer options can be built. |
| 98 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !==$field){ |
| 99 |
return $attributes; |
| 100 |
} |
| 101 |
//example: $attributes['class'] = array('author-'.$post->post_author); |
| 102 |
return $attributes; |
| 103 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the option attributes.','cf7-grid-layout')?> |
| 104 |
</li> |
| 105 |
<li class="cf7sg-tag-dynamic_select"> |
| 106 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_default_value','{$field_name_slug}_dynamic_default_option',10,3); |
| 107 |
/** |
| 108 |
* Filter dynamic dropdown default empty label. |
| 109 |
* @param string $label the label for the default value, this is null by default and not shown. |
| 110 |
* @param string $name the field name being populated. |
| 111 |
* @param string $cf7_key the form unique key. |
| 112 |
* @return string the label for the default value, returning a non-null value with display this as the first option. |
| 113 |
*/ |
| 114 |
function {$field_name_slug}_dynamic_default_option($default, $field, $cf7_key){ |
| 115 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !== $field){ |
| 116 |
return $default; |
| 117 |
} |
| 118 |
$default = 'Please select an option...'; |
| 119 |
return $default; |
| 120 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the default option label.','cf7-grid-layout')?> |
| 121 |
</li> |
| 122 |
<li class="cf7sg-tag-dynamic_select-post-tags <?=$post_my_form_only?>"> |
| 123 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_new_post','{$field_name_slug}_select2_newpost',10,7); |
| 124 |
/** |
| 125 |
* Filter custom options from tag enabled select2 dynamic-dropdown fields |
| 126 |
* where the source of options come from post titles. Filter is fired when a new value is submitted. |
| 127 |
* 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. |
| 128 |
* @param string $post_name the new post slug. |
| 129 |
* @param string $field_name the name of the form field. |
| 130 |
* @param string $title new value being submitted for a new post title. |
| 131 |
* @param string $post_type the post type from which this dropdown was built |
| 132 |
* @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. |
| 133 |
* @param array $submitted_data array of other submitted $field=>$value pairs. |
| 134 |
* @param string $key the form unique key. |
| 135 |
* @return string value to be stored for this field. |
| 136 |
*/ |
| 137 |
function {$field_name_slug}_select2_newpost($post_name, $field, $title, $post_type, $args, $submitted_data, $cf7_key){ |
| 138 |
/* |
| 139 |
Filter active when saving forms using Post My CF7 Form plugin. |
| 140 |
select2 dynamic dropdowns can have an option for custom user values to be selected/inserted. |
| 141 |
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. |
| 142 |
|
| 143 |
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. |
| 144 |
$post_name new post slug to set. |
| 145 |
$field name of dynamic dropdown. |
| 146 |
$title string submitted by the user. |
| 147 |
$post_type the post type which was used to build the original dropdown list. |
| 148 |
$args an array of any additional arguments set to limit the original dropdown list, such as taxonomy terms. |
| 149 |
$cf7_key unique form key to identify your form, $cf7_id is its post_id. |
| 150 |
$submitted_data array of other submitted $field=>$value pairs. |
| 151 |
*/ |
| 152 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !== $field){ |
| 153 |
return $default; |
| 154 |
} |
| 155 |
//create your new post using wp_insert_post(); |
| 156 |
$post_name = 'new_sbummision'; |
| 157 |
return $post_name; |
| 158 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('user added option.','cf7-grid-layout')?> |
| 159 |
</li> |
| 160 |
<li class="cf7sg-tag-dynamic_select-filter <?=$post_my_form_only?>"> |
| 161 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_filter_select2_submission','{$field_name_slug}_select2_filter_values',10,4); |
| 162 |
/** |
| 163 |
* Filter custom otions from tag enabled select2 dynamic-dropdown fields |
| 164 |
* where the source of options come from a filter. Filter is fired when values are submitted. |
| 165 |
* Return updated values if any are custom values so that saved/draft submissions will reflect the correct value saved in the DB, |
| 166 |
* @param array $values an array submitted values (several values can be submitted in the case of a tabbed/table input field). |
| 167 |
* @param string $field_name the name of the form field. |
| 168 |
* @param array $submitted_data array of other submitted $field=>$value pairs. |
| 169 |
* @param string $key the form unique key. |
| 170 |
* @return string value to be saved for this field. |
| 171 |
*/ |
| 172 |
function {$field_name_slug}_select2_filter_values($values, $field, $submitted_data, $cf7_key){ |
| 173 |
/* |
| 174 |
Filter active when saving forms using Post My CF7 Form plugin. |
| 175 |
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. |
| 176 |
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. |
| 177 |
$values selected by user. |
| 178 |
$field name of dynamic dropdown. |
| 179 |
$cf7_key unique form key to identify your form, $cf7_id is its post_id. |
| 180 |
$submitted_data array of other submitted $field=>$value pairs. |
| 181 |
*/ |
| 182 |
if('{$form_key}'!==$cf7_key && '{$field_name}' !== $field){ |
| 183 |
return $default; |
| 184 |
} |
| 185 |
//do something... |
| 186 |
return $values; |
| 187 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('user selection with user added option.','cf7-grid-layout')?> |
| 188 |
</li> |
| 189 |
<li class="cf7sg-tag-dynamic_select-filter"> |
| 190 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_dynamic_dropdown_custom_options','{$field_name_slug}_dynamic_options',10,3); |
| 191 |
/** |
| 192 |
* Filter dropdown options for dynamic drodpwn list of taxonomy terms. |
| 193 |
* @param mixed $options the opttion to filter. |
| 194 |
* @param string $name the field name being populated. |
| 195 |
* @param string $cf7_key the form unique key. |
| 196 |
* @return mixed $options return either an array of <option value>=><option label> pairs or a html string of option elements which can be grouped if required. |
| 197 |
*/ |
| 198 |
function {$field_name_slug}_dynamic_options($options, $name, $cf7_key){ |
| 199 |
//these are the label users will see when the dropdown opens. |
| 200 |
//you can group your options if need be. Let's assume you have an array of arrays of data to display in groups. |
| 201 |
$data = ... //fetch your data, either from the databse or some other source. |
| 202 |
foreach($data as $group_label=>$options){ |
| 203 |
$options += '<optgroup label=[dqt]'.$group_label.'[dqt]>'; |
| 204 |
foreach($options as $label=>$value){ |
| 205 |
$options += '<option value=[dqt]'.$value.'[dqt]>'.$label.'</option>'; |
| 206 |
} |
| 207 |
$options += '</optgroup>'; |
| 208 |
} |
| 209 |
return $options; |
| 210 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the options.','cf7-grid-layout')?> |
| 211 |
</li> |
| 212 |
<li class="cf7sg-tag-dynamic_select cf7sg-tag-radio cf7sg-tag-checkbox cf7sg-tag-select cf7sg-tag-acceptance"> |
| 213 |
<a class="helper" data-cf72post="add_filter( 'cf7sg_mail_tag_{$field_type}','{$field_name_slug}_mail_tag_value',10,3); |
| 214 |
/** |
| 215 |
* Filter the value inserted in the mail tag. |
| 216 |
* @since 2.9.0. |
| 217 |
* @param string $replaced value to filter. |
| 218 |
* @param string $name name of the field being inserted in the mail. |
| 219 |
* @param string $cf7_key unique form key identifier. |
| 220 |
* @return string a value to replace. |
| 221 |
*/ |
| 222 |
function {$field_name_slug}_mail_tag_value($replaced, $name, $cf7_key){ |
| 223 |
//check to make sure you have the right field in the right form. |
| 224 |
if( '{$field_name_slug}' !== $name || '{$form_key}'!==$cf7_key) return $replaced; |
| 225 |
//$replaced is the value inserted in the mail if you have used this field as a mail tag. |
| 226 |
$replaced; //this is currently set to the selected value and is a slug, you need to programmatically fetch your value and set it up. |
| 227 |
return $replaced; |
| 228 |
}" href="javascript:void(0);"><?=__('Filter','cf7-grid-layout')?></a> <?=__('the mail tag value.','cf7-grid-layout')?> |
| 229 |
</li> |
| 230 |
|