PluginProbe
Smart Grid-Layout Design for Contact Form 7 / 4.5
Smart Grid-Layout Design for Contact Form 7 v4.5
4.18.0 4.17.0 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 4.0.0 4.0.1 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10 4.11 4.12 4.13 4.14 All 119 releases
cf7-grid-layout / admin / partials / helpers / cf7sg-form-fields.php

cf7sg-form-fields.php in Smart Grid-Layout Design for Contact Form 7 4.5, at admin/partials/helpers/cf7sg-form-fields.php

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