PluginProbe
Smart Grid-Layout Design for Contact Form 7 / 4.0.0
Smart Grid-Layout Design for Contact Form 7 v4.0.0
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 / class-cf7-grid-layout-admin.php

class-cf7-grid-layout-admin.php in Smart Grid-Layout Design for Contact Form 7 4.0.0, at admin/class-cf7-grid-layout-admin.php

1,405 lines 58.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The admin-specific functionality of the plugin.
5 *
6 * @link http://syllogic.in
7 * @since 1.0.0
8 *
9 * @package Cf7_Grid_Layout
10 * @subpackage Cf7_Grid_Layout/admin
11 */
12
13 /**
14 * The admin-specific functionality of the plugin.
15 *
16 * Defines the plugin name, version, and two examples hooks for how to
17 * enqueue the admin-specific stylesheet and JavaScript.
18 *
19 * @package Cf7_Grid_Layout
20 * @subpackage Cf7_Grid_Layout/admin
21 * @author Aurovrata V. <vrata@syllogic.in>
22 */
23 class Cf7_Grid_Layout_Admin {
24 /**
25 * The ID of this plugin.
26 *
27 * @since 1.0.0
28 * @access private
29 * @var string $plugin_name The ID of this plugin.
30 */
31 private $plugin_name;
32 /**
33 * The version of this plugin.
34 *
35 * @since 1.0.0
36 * @access private
37 * @var string $version The current version of this plugin.
38 */
39 private $version;
40 /**
41 * Initialize the class and set its properties.
42 *
43 * @since 1.0.0
44 * @param string $plugin_name The name of this plugin.
45 * @param string $version The version of this plugin.
46 */
47 public function __construct( $plugin_name, $version ) {
48 $this->plugin_name = $plugin_name;
49 $this->version = $version;
50 }
51 /**
52 * get cf7 post type set by cf7 plugin.
53 *
54 *@since 3.0.0
55 *@return string post type.
56 */
57 private function cf7_post_type(){
58 $type = 'wpcf7_post_type_notset';
59 if(class_exists('WPCF7_ContactForm') ) {
60 $type = WPCF7_ContactForm::post_type;
61 }
62 return $type;
63 }
64 /**
65 * Hack to add scripts/styles to edit page.
66 * Hooked on 'admin_enqueue_scripts'.
67 *@since 1.5.0
68 */
69 public function popular_extentions_scripts(){
70 $screen = get_current_screen();
71 if (empty($screen) || $this->cf7_post_type() != $screen->post_type){
72 return;
73 }
74 $plugin_dir = plugin_dir_url( __DIR__ );
75 switch( $screen->base ){
76 case 'post':
77 $page_hook = '';
78 if('add' == $screen->action) $page_hook = 'contact_page_wpcf7-new';
79 else $page_hook = 'toplevel_page_wpcf7';
80 //unhook this function.
81 remove_action('admin_enqueue_scripts', array($this, 'popular_extentions_scripts'),999,0);
82 do_action('admin_enqueue_scripts', $page_hook);
83 $plugin_page ='';
84 add_action('admin_enqueue_scripts', array($this, 'popular_extentions_scripts'),999,0);
85 break;
86 case 'edit': //table.
87 break;
88 }
89 }
90 /**
91 * Function to spoof the wpcf7 plugin admin scripts loading for extensions
92 * Hooked on 'admin_print_scripts' and used to fix the issue with the mailchimp ext.
93 *@since 1.5.1
94 */
95 public function print_extentions_scripts(){
96 $screen = get_current_screen();
97 if (empty($screen) || $this->cf7_post_type() != $screen->post_type){
98 return;
99 }
100 global $plugin_page;
101 $plugin_page = 'wpcf7';
102 }
103
104
105 /**
106 * Register the stylesheets for the admin area.
107 *
108 * @since 1.0.0
109 */
110 public function enqueue_styles($page) {
111 if( in_array($page, array('toplevel_page_wpcf7', 'contact_page_wpcf7-new')) ) return;
112
113 $screen = get_current_screen();
114 if (empty($screen) || $this->cf7_post_type() != $screen->post_type){
115 return;
116 }
117 $plugin_dir = plugin_dir_url( __DIR__ );
118 switch( $screen->base ){
119 case 'post':
120 wp_enqueue_style( "cf7-grid-post-css", $plugin_dir . 'admin/css/cf7-grid-layout-post.css', array(), $this->version, 'all' );
121 //dynamic tag
122 wp_enqueue_style('cf7sg-dynamic-tag-css', $plugin_dir . 'admin/css/cf7sg-dynamic-tag.css', array(), $this->version, 'all' );
123 //benchmark tag
124 wp_enqueue_style('cf7sg-benchmark-tag-css', $plugin_dir . 'admin/css/cf7sg-benchmark-tag.css', array(), $this->version, 'all' );
125 //codemirror
126 wp_enqueue_style( "codemirror-css", $plugin_dir . 'assets/codemirror/codemirror.css', array(), $this->version, 'all' );
127 /** @since 4.0 enable dark theme */
128 wp_enqueue_style( "codemirror-theme-light-css", $plugin_dir . 'assets/codemirror/theme/paraiso-light.css', array(), $this->version, 'all' );
129 wp_enqueue_style( "codemirror-theme-dark-css", $plugin_dir . 'assets/codemirror/theme/material-ocean.css', array(), $this->version, 'all' );
130 wp_enqueue_style( "codemirror-foldgutter-css", $plugin_dir . 'assets/codemirror/addon/fold/foldgutter.css', array(), $this->version, 'all' );
131 wp_enqueue_style( "codemirror-dialog-css", $plugin_dir . 'assets/codemirror/addon/dialog/dialog.css', array(), $this->version, 'all' );
132 wp_enqueue_style( "codemirror-matchesonscrollbar-css", $plugin_dir . 'assets/codemirror/addon/search/matchesonscrollbar.css', array(), $this->version, 'all' );
133 wp_enqueue_style( 'smart-grid-css', $plugin_dir . 'assets/css.gs/smart-grid.admin.css', array(), $this->version, 'all');
134 wp_enqueue_style('dashicons');
135 wp_enqueue_style('select2-style', $plugin_dir . 'assets/select2/css/select2.min.css', array(), $this->version, 'all' );
136 break;
137 case 'edit': //table.
138 wp_enqueue_style( $this->plugin_name, $plugin_dir . 'admin/css/cf7-grid-layout-admin.css', array(), $this->version, 'all' );
139
140 break;
141 }
142
143 }
144
145 /**
146 * Register the JavaScript for the admin area.
147 *
148 * @since 1.0.0
149 */
150 public function enqueue_scripts($page) {
151 global $post;
152 //debug_msg($screen, $this->custom_type );
153 $plugin_dir = plugin_dir_url( __DIR__ );
154 if( in_array($page, array('toplevel_page_wpcf7', 'contact_page_wpcf7-new')) ){
155 wp_enqueue_script( $this->plugin_name, $plugin_dir . 'admin/js/cf7-grid-layout-admin.js', array( 'jquery' ), $this->version, true );
156 return;
157 }
158 $screen = get_current_screen();
159 if ($this->cf7_post_type() != $screen->post_type){
160 return;
161 }
162 switch( $screen->base ){
163 case 'post':
164 /* register codemirror editor & addons. */
165 //codemirror script
166 wp_register_script( 'cf7-codemirror-js',
167 $plugin_dir . 'assets/codemirror/codemirror.js',
168 null, "5.32", true
169 );
170 /** @since 3.1.2 initialise codemirror after library load and parse as attribute to anonymous functtion in cf7-grid-codemirror.js */
171 wp_add_inline_script('cf7-codemirror-js',
172 'const cmInitialSettings = {
173 value:"",matchTags: {bothTags: true},autoCloseTags:true,
174 extraKeys: {"Ctrl-Space": "autocomplete", "Ctrl-/": "toggleComment", "Ctrl-J": "toMatchingTag"},
175 lineNumbers: true, styleActiveLine: true,
176 matchBrackets: true, tabSize:2, lineWrapping: true, addModeClass: true,
177 foldGutter: true, autofocus:false,
178 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
179 }
180 const codeMirror_5_32 = CodeMirror(document.getElementById("cf7-codemirror"),cmInitialSettings);
181 const cssCodeMirror_5_32 = CodeMirror(document.getElementById("cf7-css-codemirror"),cmInitialSettings);
182 const jsCodeMirror_5_32 = CodeMirror(document.getElementById("cf7-js-codemirror"),cmInitialSettings);');
183 //matchtags.
184 wp_enqueue_script( 'codemirror-matchtag-js',
185 $plugin_dir . 'assets/codemirror/edit/matchtags.js',
186 array('cf7-codemirror-js'), $this->version, true
187 );
188 wp_enqueue_script( 'codemirror-closetag-js',
189 $plugin_dir . 'assets/codemirror/edit/closetag.js',
190 array('cf7-codemirror-js'), $this->version, true
191 );
192 //fold code
193 wp_enqueue_script( 'codemirror-foldcode-js',
194 $plugin_dir . 'assets/codemirror/addon/fold/foldcode.js',
195 array('cf7-codemirror-js'), $this->version, true
196 );
197 wp_enqueue_script( 'codemirror-foldgutter-js',
198 $plugin_dir . 'assets/codemirror/addon/fold/foldgutter.js',
199 array('cf7-codemirror-js'), $this->version, true
200 );
201 wp_enqueue_script( 'codemirror-indent-fold-js',
202 $plugin_dir . 'assets/codemirror/addon/fold/indent-fold.js',
203 array('cf7-codemirror-js'), $this->version, true
204 );
205 wp_enqueue_script( 'codemirror-xml-fold-js',
206 $plugin_dir . 'assets/codemirror/addon/fold/xml-fold.js',
207 array('cf7-codemirror-js'), $this->version, true
208 );
209 wp_enqueue_script( 'codemirror-brace-fold-js',
210 $plugin_dir . 'assets/codemirror/addon/fold/brace-fold.js',
211 array('cf7-codemirror-js'), $this->version, true
212 );
213 wp_enqueue_script( 'codemirror-comment-fold-js',
214 $plugin_dir . 'assets/codemirror/addon/fold/comment-fold.js',
215 array('cf7-codemirror-js'), $this->version, true
216 );
217 wp_enqueue_script( 'codemirror-mixed-js',
218 $plugin_dir . 'assets/codemirror/mode/htmlmixed/htmlmixed.js',
219 array('cf7-codemirror-js'), $this->version, true
220 );
221 wp_enqueue_script( 'codemirror-javascript-js',
222 $plugin_dir . 'assets/codemirror/mode/javascript/javascript.js',
223 array('cf7-codemirror-js'), $this->version, true
224 );
225 wp_enqueue_script( 'codemirror-xml-js',
226 $plugin_dir . 'assets/codemirror/mode/xml/xml.js',
227 array('cf7-codemirror-js'), $this->version, true
228 );
229 wp_enqueue_script( 'codemirror-css-js',
230 $plugin_dir . 'assets/codemirror/mode/css/css.js',
231 array('cf7-codemirror-js'), $this->version, true
232 );
233 //overlay for shortcode highligh
234 wp_enqueue_script( 'codemirror-overlay-js',
235 $plugin_dir . 'assets/codemirror/addon/mode/overlay.js',
236 array('cf7-codemirror-js'), $this->version, true
237 );
238 /**
239 * @since 1.3.0 enable search codemirror
240 */
241 //overlay for shortcode highligh
242 wp_enqueue_script( 'codemirror-search-js',
243 $plugin_dir . 'assets/codemirror/addon/search/search.js',
244 array('cf7-codemirror-js'), $this->version, true
245 );
246 wp_enqueue_script( 'codemirror-jumptoline-js',
247 $plugin_dir . 'assets/codemirror/addon/search/jump-to-line.js',
248 array('cf7-codemirror-js'), $this->version, true
249 );
250 wp_enqueue_script( 'codemirror-matchesonscrollbar-js',
251 $plugin_dir . 'assets/codemirror/addon/search/matchesonscrollbar.js',
252 array('cf7-codemirror-js'), $this->version, true
253 );
254 wp_enqueue_script( 'codemirror-searchcursor-js',
255 $plugin_dir . 'assets/codemirror/addon/search/searchcursor.js',
256 array('cf7-codemirror-js'), $this->version, true
257 );
258 wp_enqueue_script( 'codemirror-annotatescrollbar-js',
259 $plugin_dir . 'assets/codemirror/addon/scroll/annotatescrollbar.js',
260 array('cf7-codemirror-js'), $this->version, true
261 );
262 wp_enqueue_script( 'codemirror-dialog-js',
263 $plugin_dir . 'assets/codemirror/addon/dialog/dialog.js',
264 array('cf7-codemirror-js'), $this->version, true
265 );
266
267 //js beautify
268 wp_enqueue_script( 'beautify-js',
269 $plugin_dir . 'assets/beautify/beautify.js',
270 array('jquery'), $this->version, true
271 );
272 wp_enqueue_script( 'beautify-html-js',
273 $plugin_dir . 'assets/beautify/beautify-html.js',
274 array('beautify-js'), $this->version, true
275 );
276 wp_enqueue_script('jquery-select2', $plugin_dir . 'assets/select2/js/select2.min.js', array( 'jquery' ), $this->version, true );
277 /** @since 4.0 enable dark theme */
278 $cm_light = apply_filters('cf7sg_admin_editor_theme', '');
279 $user_theme = get_user_meta(get_current_user_id(),'_cf7sg_cm_theme', true);
280 if(!empty($cm_light) && file_exists(get_stylesheet_directory().'/'.$cm_light) ){
281 wp_enqueue_style( "codemirror-theme-css", get_stylesheet_directory_uri().'/'.$cm_light, array(), $this->version, 'all' );
282 $user_theme = basename($cm_light, '.css');
283 update_user_meta(get_current_user_id(),'_cf7sg_cm_theme', '');
284 }else{
285 if(empty($user_theme)){
286 $user_theme = 'paraiso-light';
287 update_user_meta(get_current_user_id(),'_cf7sg_cm_theme', 'light');
288 }else{
289 $user_theme = ('light'==$user_theme ? 'paraiso-light' : 'material-ocean');
290 }
291 }
292 $cm_js_light = apply_filters('cf7sg_admin_js_editor_theme', '');
293 $user_js_theme = get_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', true);
294 if(!empty($cm_js_light) && file_exists(get_stylesheet_directory().'/'.$cm_js_light) ){
295 wp_enqueue_style( "codemirror-js-theme-css", get_stylesheet_directory_uri().'/'.$cm_js_light, array(), $this->version, 'all' );
296 $user_js_theme = basename($cm_js_light, '.css');
297 update_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', '');
298 }else{
299 if(empty($user_js_theme)){
300 $user_js_theme = 'paraiso-light';
301 update_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', 'light');
302 }else{
303 $user_js_theme = ('light'==$user_js_theme ? 'paraiso-light' : 'material-ocean');
304 }
305 }
306 $cm_css_light = apply_filters('cf7sg_admin_css_editor_theme', '');
307 $user_css_theme = get_user_meta(get_current_user_id(),'_cf7sg_css_cm_theme', true);
308 if(!empty($cm_css_light) && file_exists(get_stylesheet_directory().'/'.$cm_css_light) ){
309 wp_enqueue_style( "codemirror-css-theme-css", get_stylesheet_directory_uri().'/'.$cm_css_light, array(), $this->version, 'all' );
310 $user_css_theme = basename($cm_css_light, '.css');
311 update_user_meta(get_current_user_id(),'_cf7sg_css_cm_theme', '');
312 }else{
313 if(empty($user_css_theme)){
314 $user_css_theme = 'paraiso-light';
315 update_user_meta(get_current_user_id(),'_cf7sg_css_cm_theme', 'light');
316 }else{
317 $user_css_theme = ('light'==$user_css_theme ? 'paraiso-light' : 'material-ocean');
318 }
319 }
320 //enqueue the cf7 scripts.
321 wpcf7_admin_enqueue_scripts( 'wpcf7' );
322 wp_enqueue_script('jquery-clibboard', $plugin_dir . 'assets/clipboard/clipboard.min.js', array('jquery'),$this->version,true);
323 wp_enqueue_script( 'cf7-grid-codemirror-js', $plugin_dir . 'admin/js/cf7-grid-codemirror.js', array( 'jquery', 'jquery-ui-tabs', 'cf7-codemirror-js' ), $this->version, true );
324 wp_localize_script(
325 'cf7-grid-codemirror-js',
326 'cf7sgeditor',
327 array(
328 'mode' => apply_filters('cf7sg_admin_editor_mode', 'shortcode', $post->post_name),
329 'theme' => array(
330 'light'=>'paraiso-light',
331 'dark'=>'material-ocean',
332 'user'=>$user_theme
333 ),
334 'jstheme' => array(
335 'light'=>'paraiso-light',
336 'dark'=>'material-ocean',
337 'user'=>$user_js_theme
338 ),
339 'csstheme' => array(
340 'light'=>'paraiso-light',
341 'dark'=>'material-ocean',
342 'user'=>$user_css_theme
343 )
344 )
345 );
346
347 wp_enqueue_script( $this->plugin_name, $plugin_dir . 'admin/js/cf7-grid-layout-admin.js', array('cf7-grid-codemirror-js', 'jquery-ui-sortable' ), $this->version, true );
348 wp_localize_script(
349 $this->plugin_name,
350 'cf7grid',
351 array(
352 'preHTML' => apply_filters('cf7sg_pre_cf7_field_html', '<div class="field"><label></label>', $post->post_name),
353 'postHTML' => apply_filters('cf7sg_post_cf7_field_html', '<p class="info-tip"></p></div>', $post->post_name),
354 'requiredHTML' => apply_filters('cf7sg_required_cf7_field_html', '<em>*</em>', $post->post_name),
355 'ui' => apply_filters('cf7sg_grid_ui', true, $post->post_name)
356 )
357 );
358 wp_enqueue_script( 'cf7sg-dynamic-tag-js', $plugin_dir . 'admin/js/cf7sg-dynamic-tag.js', array('jquery','wpcf7-admin-taggenerator' ), $this->version, true );
359 wp_enqueue_script( 'cf7-benchmark-tag-js', $plugin_dir . 'admin/js/cf7-benchmark-tag.js', array('jquery','wpcf7-admin-taggenerator' ), $this->version, true );
360 /** @since 3.2.0 */
361 wp_enqueue_script('cf7sg-mail-tag-js', $plugin_dir.'admin/js/mail-tag-helper.js', array('jquery','jquery-clibboard'));
362 wp_localize_script('cf7sg-mail-tag-js','mailTagHelper',
363 array(
364 'msg'=>__('Click to copy!','cf7-grid-layout'),
365 'filter'=> __('Filter mailTag %s', 'cf7-grid-layout')
366 )
367 );
368 /** @since 3.3.0enqueue ui-grid helpers js */
369 wp_enqueue_script('ui-grid-helpers-js', $plugin_dir.'admin/js/ui-custom-helper.js', array('jquery'));
370 do_action('cf7sg_enqueue_admin_editor_scripts');
371 break;
372 case 'edit':
373 //wp_enqueue_script( $this->plugin_name.'-quick-edit', plugin_dir_url( __FILE__ ) . 'js/cf7-grid-layout-quick-edit.js', false, $this->version, true );
374
375 break;
376 }
377 }
378
379 /**
380 * Adds a new sub-menu to replace the 'Add New' CF7 menu
381 * Add a new sub-menu to the Contact main menu, as well as remove the current default
382 *
383 */
384 public function add_cf7_sub_menu(){
385
386 $hook = add_submenu_page(
387 'wpcf7',
388 __cf7sg( 'Edit Contact Form'),
389 __cf7sg( 'Add New'),
390 'wpcf7_edit_contact_forms',
391 'post-new.php?post_type=wpcf7_contact_form'
392 );
393 //initial cf7 object when creating new form
394 add_action( 'load-' . $hook, array($this, 'setup_cf7_object'));
395 //remove_submenu_page( $menu_slug, $submenu_slug );
396 remove_submenu_page( 'wpcf7', 'wpcf7-new' );
397 remove_meta_box('slugdiv', $this->cf7_post_type(), 'normal');
398 }
399 /**
400 * Change the submenu order
401 * @since 1.0.0
402 */
403 public function change_cf7_submenu_order( $menu_ord ) {
404 global $submenu;
405 // Enable the next line to see all menu orders
406 if(!isset($submenu['wpcf7']) ){
407 return $menu_ord;
408 }
409 if( is_network_admin() ){
410 return $menu_ord;
411 }
412 $arr = array();
413 foreach($submenu['wpcf7'] as $menu){
414 switch($menu[2]){
415 case 'post-new.php?post_type=wpcf7_contact_form':
416 //push to the front
417 array_unshift($arr, $menu);
418 break;
419 default:
420 $arr[]=$menu;
421 break;
422 }
423 }
424 $submenu['wpcf7'] = $arr;
425 return $menu_ord;
426 }
427 /**
428 * Modify the regsitered cf7 post tppe
429 * THis function enables public capability and amind UI visibility for the cf7 post type. Hooked late on `register_post_type_args`
430 * @since 1.0.0
431 * @param Array $args array of post parameters being registered
432 * @param String $post_type post type breing resgistered
433 */
434
435 public function modify_cf7_post_type_args($args, $post_type){
436 if($post_type === $this->cf7_post_type() ) {
437 // debug_msg($args, 'pre-args');
438 // $system_dropdowns = get_option('_cf7sg_dynamic_dropdown_system_taxonomy',array());
439 /** @since 2.8.3 add wpcf7_type (registered in assets/cf7-table.php) taxonomy to cf7 post type */
440 $system_taxonomy = array('wpcf7_type');
441 // if(!empty($system_dropdowns)){
442 // foreach($system_dropdowns as $id=>$list){
443 // $system_taxonomy = array_merge($system_taxonomy, $list);
444 // }
445 // }
446 if(!empty($args['taxonomies'])){
447 $system_taxonomy = array_merge($args['taxonomies'], $system_taxonomy);
448 $system_taxonomy = array_unique($system_taxonomy);
449 }
450 $args['taxonomies'] = $system_taxonomy;
451
452 $args['public'] = false;
453 $args['show_ui']= true;
454 $args['show_in_menu']= 'wpcf7';
455 $args['supports'] = array('title', 'author');
456 $args['labels']['add_new_item'] = 'Add New Form';
457 $args['labels']['edit_item'] = 'Edit Form';
458 $args['labels']['new_item'] = 'New Form';
459 $args['labels']['view_item'] = 'View Form';
460 $args['labels']['view_items'] = 'View Forms';
461 $args['labels']['search_items'] = 'Search Forms';
462 $args['labels']['not_found'] = 'No forms found.';
463 $args['labels']['not_found_in_trash'] = 'No forms found in Trash.';
464 $args['labels']['parent_item_colon'] = '';
465 $args['labels']['attributes'] = 'Form Attributes';
466 $args['labels']['insert_into_item'] = 'Insert into form';
467 $args['labels']['uploaded_to_this_item'] = 'Uploaded to this form';
468 $args['labels']['filter_items_list'] = 'Filter forms list';
469 $args['labels']['items_list_navigation'] = 'Forms list navigation';
470 $args['labels']['items_list'] = 'Forms list';
471 /** @since 2.8.1 fix missing delete_posts notices*/
472 // $args['capabilities']['delete_posts']= 'wpcf7_delete_posts';
473 /** @since 3.0.0 enable better capability management */
474 // $args['capability_type'] = 'page';
475
476 $args['map_meta_cap']=true; //allow finer capability mapping.
477 $args['capabilities']['edit_post'] = 'wpcf7_edit_contact_form';
478 $args['capabilities']['read_post'] = 'wpcf7_read_contact_form';
479 $args['capabilities']['delete_post'] = 'wpcf7_delete_contact_form';
480 $args['capabilities']['edit_posts'] = 'wpcf7_edit_contact_forms';
481 $args['capabilities']['edit_others_posts']= 'wpcf7_edit_others_contact_forms';
482 $args['capabilities']['edit_published_posts']= 'wpcf7_edit_published_contact_forms';
483 $args['capabilities']['delete_posts']= 'wpcf7_delete_contact_forms';
484 $args['capabilities']['delete_published_posts']= 'wpcf7_delete_published_contact_forms';
485 $args['capabilities']['delete_others_posts']= 'wpcf7_delete_others_contact_forms';
486 $args['capabilities']['publish_posts']= 'wpcf7_publish_contact_forms';
487 $args['capabilities']['read_private_posts']= 'wpcf7_publish_contact_forms';
488
489 // debug_msg($args);
490 }
491 return $args;
492 }
493 /**
494 * Register custom taxonomy for dynamic dropdown lists
495 * Hooked on 'init'
496 * @since 1.0.0
497 *
498 **/
499 public function register_dynamic_dropdown_taxonomy(){
500 //register the dynamic dropdown taxonomies.
501 $dropdowns = get_option('_cf7sg_dynamic_dropdown_taxonomy',array());
502 //debug_msg($dropdowns);
503 $created = array();
504 foreach($dropdowns as $post_lists){
505 foreach($post_lists as $slug=>$taxonomy){
506 if(!isset($created[$slug])){
507 if(is_array($taxonomy)){
508 $this->register_dynamic_dropdown($taxonomy);
509 $created[$slug] = $slug;
510 }
511 }
512 }
513 }
514 }
515
516 /**
517 * Function to add the metabox to the cf7 post edit screen
518 * This adds the main editor, hooked on 'add_meta_boxes'
519 * @since 1.0.0
520 */
521 public function main_editor_meta_box() {
522 //add_meta_box( string $id, string $title, callable $callback, string $screen, string $context, string $priority, array $callback_args)
523 if( post_type_exists( $this->cf7_post_type() ) ) {
524 add_meta_box( 'meta-box-main-cf7-editor',
525 __cf7sg( 'Edit Contact Form' ),
526 array($this , 'main_editor_metabox_display'),
527 $this->cf7_post_type(),
528 'normal',
529 'high'
530 );
531 }
532 }
533 /**
534 * Function called on page load when new form is created
535 * hooked to 'load-{page-hook}'
536 * @since 1.0.0
537 **/
538 public function setup_cf7_object(){
539 $args = array();
540 /**
541 * Fix locale setup for new forms using polylang.
542 * @since 2.1.4
543 */
544 if(isset($_GET['locale'])){
545 $args['locale'] = $_GET['locale'];
546 }else if(isset($_GET['new_lang'])){
547 //check for polylang
548 $locale = $_GET['new_lang'];
549 if(function_exists('pll_languages_list')){
550 $langs = pll_languages_list();
551 $locales = pll_languages_list(array('fields'=>'locale'));
552 foreach($langs as $idx => $lang){
553 if($lang == $locale){
554 $locale = $locales[$idx];
555 }
556 }
557 }
558 $args['locale'] =$locale;
559 }else $args['locale'] = get_locale();
560
561 WPCF7_ContactForm::get_template( $args);
562 }
563 /**
564 * Callback function to disolay the main editor meta box
565 * @since 1.0.0
566 */
567 public function main_editor_metabox_display($post){
568 if('auto-draft' !== $post->post_status){
569 wpcf7_contact_form($post); //set the post
570 }
571 $post_id = $post->ID;
572 $cf7_form = wpcf7_get_current_contact_form();
573
574 if ( ! $cf7_form ) {
575 $cf7_form = WPCF7_ContactForm::get_template();
576 }
577
578 // if(empty($post)){
579 // $post_id = -1;
580 // }else{
581 // $post_id = $post->ID;
582 // }
583 require_once WPCF7_PLUGIN_DIR . '/admin/includes/editor.php';
584 require_once plugin_dir_path( __FILE__ ) . 'partials/cf7-admin-editor-display.php';
585 }
586
587 /**
588 * Function to add the metabox to the cf7 post edit screen
589 * This adds the main editor, hooked on 'add_meta_boxes'
590 * @since 1.0.0
591 */
592 public function info_meta_box() {
593 //add_meta_box( string $id, string $title, callable $callback, string $screen, string $context, string $priority, array $callback_args)
594 if(post_type_exists( $this->cf7_post_type() ) ) {
595 add_meta_box( 'meta-box-cf7-info',
596 __cf7sg( 'Information' ),
597 array($this , 'info_metabox_display'),
598 $this->cf7_post_type(),
599 'side',
600 'high'
601 );
602 }
603 }
604 /**
605 * Re-introduces the wordpress 'wpcf7_admin_misc_pub_section' for plugins to add their fields for submission
606 * Hooked to 'post_submitbox_misc_actions' which fires after post dat/time parameters are printed
607 * @since 1.0.0
608 * @param WP_Post $post post object being edited/created.
609 **/
610 public function cf7_post_submit_action($post){
611 if($this->cf7_post_type() == $post->post_type){
612 do_action( 'wpcf7_admin_misc_pub_section', $post->ID );
613 }
614 }
615 /**
616 * Callback function to disolay the main editor meta box
617 * @since 1.0.0
618 */
619 public function info_metabox_display($post){
620 require_once plugin_dir_path( __FILE__ ) . 'partials/cf7-info-metabox-display.php';
621 }
622 /**
623 * Function to add the metabox to the cf7 post edit screen
624 * This adds the helper, hooked on 'add_meta_boxes'
625 * @since 1.1.0
626 */
627 public function helper_meta_box() {
628 //add_meta_box( string $id, string $title, callable $callback, string $screen, string $context, string $priority, array $callback_args).
629 if(post_type_exists( $this->cf7_post_type() ) ) {
630 add_meta_box( 'meta-box-cf7sg-helper',
631 __( 'Actions & Filters', 'cf7-grid-layout' ),
632 array($this , 'helper_metabox_display'),
633 $this->cf7_post_type(),
634 'side',
635 'low'
636 );
637 }
638 }
639 /**
640 * Callback function to disolay the helper meta box
641 * @since 1.0.0
642 */
643 public function helper_metabox_display($post){
644 require_once plugin_dir_path( __FILE__ ) . '/partials/cf7-helper-metabox-display.php';
645 }
646 /**
647 * Display the editor panels (wpcf7 / codemirror / grid)
648 *
649 * @since 1.0.0
650 * @param string $p1 .
651 * @return string $p2 .
652 **/
653 public function grid_editor_panel($form_post){
654 require_once plugin_dir_path( __FILE__ ) . '/partials/cf7-grid-layout-admin-display.php';
655 }
656 /**
657 * Save cf7 post using ajax
658 * Hooked to 'save_post_wpcf7_contact_form'
659 * @since 1.0.0
660 **/
661 public function save_post($post_id, $post, $update){
662 //make sure this is the edit page and not a quick-edit.
663 if( !isset($_POST['_wpcf7nonce']) ){
664 return;
665 }
666 //validate the nonce.
667 check_admin_referer( 'wpcf7-save-contact-form_' . $post_id, '_wpcf7nonce');
668
669 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
670 return;
671 }
672
673 switch($post->post_status){
674 case 'auto-draft':
675 case 'trash':
676 return;
677 default:
678 break;
679 }
680
681 // debug_msg($_POST, 'submitted ');
682 $args = $_REQUEST;
683 $args['id'] = $post_id;
684
685 $args['title'] = isset( $_POST['post_title'] ) ? sanitize_text_field($_POST['post_title'], 'Contact Form', 'save') : null;
686 $args['locale'] = isset( $_POST['wpcf7-locale'] ) ? sanitize_text_field($_POST['wpcf7-locale']) : null;
687 $args['form'] = '';
688 $allowed_tags = wp_kses_allowed_html( 'post' ); //filtered in function below.
689 /** @since 2.10.0 alllow custom input html*/
690 $allowed_tags['input']=array( //add additional input html elements
691 'type'=>1,
692 'class'=>1,
693 'id'=>1,
694 'value'=>1,
695 'data-*'=>1,
696 );
697 $allowed_tags['script']=array('type'=>1);
698 $cf7_key = sanitize_text_field($_POST['post_name']);
699 $allowed_tags = apply_filters('cf7sg_kses_allowed_html',$allowed_tags, $cf7_key);
700 if(isset( $_POST['wpcf7-form'] )){
701 $args['form'] = wp_kses($_POST['wpcf7-form'], $allowed_tags);
702 }
703 $args['mail'] = isset( $_POST['wpcf7-mail'] ) ? wpcf7_sanitize_mail( $_POST['wpcf7-mail'] ): array();
704 $args['mail_2'] = isset( $_POST['wpcf7-mail-2'] ) ? wpcf7_sanitize_mail( $_POST['wpcf7-mail-2'] ): array();
705 $args['messages'] = isset( $_POST['wpcf7-messages'] ) ? $_POST['wpcf7-messages'] : array();
706 foreach($args['messages'] as $key=>$value){
707 $args['messages'][$key] = sanitize_text_field($value);
708 }
709 $args['additional_settings'] = isset( $_POST['wpcf7-additional-settings'] ) ? sanitize_textarea_field($_POST['wpcf7-additional-settings']) : '';
710
711 //need to unhook this function so as not to loop infinitely
712 //debug_msg($args, 'saving cf7 posts');
713 remove_action('save_post_wpcf7_contact_form', array($this, 'save_post'), 10,3);
714 $contact_form = wpcf7_save_contact_form( $args );
715 add_action('save_post_wpcf7_contact_form', array($this, 'save_post'), 10,3);
716 //flag as a grid form
717 //update_post_meta($post_id, 'cf7_grid_form', true); removed since v2.3 as not used.
718 //save sub-forms if any
719 $sub_forms = json_decode(stripslashes($_POST['cf7sg-embeded-forms']));
720 if(empty($sub_forms)) $sub_forms = array();
721 if(!is_array($sub_forms)) $sub_forms = array($sub_forms);
722 $sanitised_sub_forms = array();
723 foreach($sub_forms as $field){
724 $sanitised_sub_forms[] = sanitize_text_field($field);
725 }
726 update_post_meta($post_id, '_cf7sg_sub_forms', $sanitised_sub_forms);
727 //save form fields which are in tabs or tables.
728 $tt_fields = json_decode(stripslashes($_POST['cf7sg-table-fields']));
729 if(empty($tt_fields)) $tt_fields = array();
730 if(!is_array($tt_fields)) $tt_fields = array($tt_fields);
731 $sanitised_table_fields = array();
732 foreach($tt_fields as $table_fields){
733 if(is_object($table_fields)){ /** @since 2.4.2 track fields in each tables. */
734 $table_fields = get_object_vars($table_fields); /*convert to array*/
735 $table = array_keys($table_fields);
736 $table = sanitize_text_field($table[0]);
737 $table_fields = $table_fields[$table];
738 $sanitised_table_fields[$table]=array();
739 foreach($table_fields as $field) $sanitised_table_fields[$table][] = sanitize_text_field($field);
740 }else $sanitised_table_fields[] = sanitize_text_field($table_fields);
741 }
742 update_post_meta($post_id, '_cf7sg_grid_table_names', $sanitised_table_fields);
743 //tabs
744 $tt_fields = json_decode(stripslashes($_POST['cf7sg-tabs-fields']));
745 if(empty($tt_fields)) $tt_fields = array();
746 if(!is_array($tt_fields)) $tt_fields = array($tt_fields);
747 $sanitised_tab_fields = array();
748 foreach($tt_fields as $tab_field){
749 if(is_object($tab_field)){ /** @since 2.4.2 track fields in each tabs. */
750 $tab_field = get_object_vars($tab_field); /*convert to array*/
751 $tab = array_keys($tab_field);
752 $tab = sanitize_text_field($tab[0]);
753 $tab_field = $tab_field[$tab];
754 $sanitised_tab_fields[$tab]=array();
755 foreach($tab_field as $field) $sanitised_tab_fields[$tab][] = sanitize_text_field($field);
756 }else $sanitised_tab_fields[] = sanitize_text_field($tab_field);
757 }
758 update_post_meta($post_id, '_cf7sg_grid_tabs_names', $sanitised_tab_fields);
759 /** track toggled fields.
760 * @since 2.5*/
761 $tt_fields = json_decode(stripslashes($_POST['cf7sg-toggle-fields']));
762 if(empty($tt_fields)) $tt_fields = array();
763 if(!is_array($tt_fields)) $tt_fields = array($tt_fields);
764 $sanitised_toggled_fields = array();
765 foreach($tt_fields as $tgg_field){
766 if(is_object($tgg_field)){
767 $tgg_field = get_object_vars($tgg_field); /*convert to array*/
768 $tgg = array_keys($tgg_field);
769 $tgg = sanitize_text_field($tgg[0]);
770 $tgg_field = $tgg_field[$tgg];
771 $sanitised_toggled_fields[$tgg]=array();
772 foreach($tgg_field as $field) $sanitised_toggled_fields[$tgg][] = sanitize_text_field($field);
773 }else $sanitised_toggled_fields[] = sanitize_text_field($tgg_field);
774 }
775 update_post_meta($post_id, '_cf7sg_grid_toggled_names', $sanitised_toggled_fields);
776 /** @since 4.0.0 track tabbed toggles */
777 $ttgls = json_decode(stripslashes($_POST['cf7sg-tabbed-toggles']));
778 if(empty($ttgls)) $ttgls = array();
779 if(!is_array($ttgls)) $ttgls = array($ttgls);
780 update_post_meta($post_id, '_cf7sg_grid_tabbed_toggles', $ttgls);
781 /** @since 4.0.0 track grouped toggles */
782 $ttgls = json_decode(stripslashes($_POST['cf7sg-grouped-toggles']));
783 if(empty($ttgls)) $ttgls = array();
784 if(is_object($ttgls)) $ttgls = get_object_vars($ttgls); //convert to array.
785 else $ttgls = array();
786 update_post_meta($post_id, '_cf7sg_grid_grouped_toggles', $ttgls);
787 //flag tab & tables for more efficient front-end display.
788 $has_tabs = ( 'true' === $_POST['cf7sg-has-tabs']) ? true : false;
789 update_post_meta($post_id, '_cf7sg_has_tabs', $has_tabs);
790 $has_tables = ( 'true' === $_POST['cf7sg-has-tables']) ? true : false;
791 update_post_meta($post_id, '_cf7sg_has_tables', $has_tables);
792 $has_toggles = ( 'true' === $_POST['cf7sg-has-toggles']) ? true : false;
793 update_post_meta($post_id, '_cf7sg_has_toggles', $has_toggles);
794 /** @since 3.3.5 track toggles in tabs */
795 $toggle_in_tabs = json_decode(stripslashes($_POST['cf7sg-toggle-in-tabs']));
796 if(empty($toggle_in_tabs)) $toggle_in_tabs = array();
797 if(!is_array($toggle_in_tabs)) $toggle_in_tabs = array($toggle_in_tabs);
798 update_post_meta($post_id, '_cf7sg_grid_toggle_in_tabs', $toggle_in_tabs);
799 /**
800 * @since 1.2.3 disable cf7sg styling/js for non-cf7sg forms.
801 */
802 $is_cf7sg = ( 'true' === $_POST['is_cf7sg_form']) ? true : false;
803 update_post_meta($post_id, '_cf7sg_managed_form', $is_cf7sg);
804 update_post_meta($post_id, '_cf7sg_version', $this->version);
805 /** @since 3.0.0 track script classes for loading for js/css in front-end */
806 $classes = sanitize_text_field($_POST['cf7sg-script-classes']);
807 $classes = explode(',', trim($classes, ','));
808 // debug_msg($classes, 'script classes');
809 update_post_meta($post_id, '_cf7sg_script_classes', $classes);
810 /**
811 *@since 2.3.0 the duplicate functionality has been instored and therefore any new meta fields added to this plugin needs to be added to the duplication properties too.
812 */
813 /** @since 4.0.0 save user theme pref */
814 if(isset($_POST['cf7sg_codemirror_theme'])){
815 update_user_meta(get_current_user_id(),'_cf7sg_cm_theme', $_POST['cf7sg_codemirror_theme']);
816 }
817 if(isset($_POST['cf7sg_js_codemirror_theme'])){
818 update_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', $_POST['cf7sg_js_codemirror_theme']);
819 }
820 //save js file if used.
821 $path = get_stylesheet_directory();
822 if(!empty($_POST['cf7sg_js_file'])){
823 //check if the file name is changed.
824 if(!empty($_POST['cf7sg_prev_js_file']) && file_exists(ABSPATH. $_POST['cf7sg_prev_js_file'])){
825 if( !unlink(ABSPATH.$_POST['cf7sg_prev_js_file']) ) debug_msg('CF7SG ADMIN: unable to delete file '.$_POST['cf7sg_prev_js_file']);
826 }
827 if (!is_dir($path)) mkdir($path);
828 file_put_contents( "$path/js/{$cf7_key}.js", stripslashes($_POST['cf7sg_js_file']) );
829 }else if( isset($_POST['cf7sg_js_file']) && file_exists("$path/js/{$cf7_key}.js") ) { //delete file.
830 if( !unlink("$path/js/{$cf7_key}.js") ) debug_msg("CF7SG ADMIN: unable to delete file $path/js/{$cf7_key}.js");
831 }
832 //save css file.
833 if(!empty($_POST['cf7sg_css_file'])){
834 //check if the file is changed.
835 if(!empty($_POST['cf7sg_prev_css_file']) && file_exists(ABSPATH.$_POST['cf7sg_prev_css_file'])){
836 if( !unlink(ABSPATH.$_POST['cf7sg_prev_css_file']) ) debug_msg('CF7SG ADMIN: unable to delete file '.$_POST['cf7sg_prev_css_file']);
837 }
838 if (!is_dir($path)) mkdir($path);
839 file_put_contents( $path."/css/{$cf7_key}.css", stripslashes($_POST['cf7sg_css_file']));
840 }else if( isset($_POST['cf7sg_css_file']) && file_exists("$path/css/{$cf7_key}.css") ) { //delete file.
841 if( !unlink("$path/css/{$cf7_key}.css") ) debug_msg("CF7SG ADMIN: unable to delete file $path/css/{$cf7_key}.css");
842 }
843 //jstags comments.
844 if(empty($_POST['cf7sg_jstags_comments'])) update_post_meta($post->ID, '_cf7sg_disable_jstags_comments',1);
845 else update_post_meta($post->ID, '_cf7sg_disable_jstags_comments',0);
846 }
847 /**
848 * Print default js template,
849 * Hooked to 'cf7sg_default_custom_js_template'
850 *@since 4.0.0
851 *@param string $form_key form key.
852 */
853 public function print_default_js($form_key){
854 $js_file = str_replace(ABSPATH, '', get_stylesheet_directory()."/js/{$form_key}.js");
855 include_once 'partials/cf7-default-js.php';
856 }
857 /**
858 *
859 *
860 *@since 2.3.0
861 *@param string $new_form_id new form id to duplciate to.
862 *@param string $form_id form id to duplciate.
863 */
864 public function duplicate_form_properties($new_form_id, $form_id){
865 //these properties will be preceded with an '_' by the cf7 plugin before being duplicated.
866 $properties = array('_cf7sg_sub_forms', '_cf7sg_grid_table_names', '_cf7sg_grid_tabs_names', '_cf7sg_has_tabs', '_cf7sg_has_tables', '_cf7sg_managed_form','_cf7sg_script_classes', '_cf7sg_version');
867 $properties = apply_filters('cf7sg_duplicate_form_properties', $properties);
868 foreach($properties as $field){
869 $value = get_post_meta($form_id, $field, true);
870 if(!empty($value)) update_post_meta($new_form_id,$field, $value);
871 }
872 }
873 /**
874 * Duplicate form.
875 *
876 *@since 2.3.0
877 *@param string $param text_description
878 *@return string text_description
879 */
880 public function duplicate_cf7_form(){
881 global $pagenow;
882 if($pagenow != 'post.php') return;
883 if(isset($_GET['action']) && 'cf7copy' == $_GET['action']){
884 $action = 'wpcf7-copy-contact-form_' . $_GET['post'];
885 if( !wp_verify_nonce( $_GET['_wpnonce'], $action )){
886 die( 'Security check error: Try to reload the page and try again.' );
887 }
888 if ( $form = wpcf7_contact_form( $_GET['post'] ) ) {
889 $new_form = $form->copy();
890 $new_form->save();
891 $this->duplicate_form_properties($new_form->id(), $_GET['post']);
892 wp_safe_redirect( admin_url( '/post.php?post='.$new_form->id().'&action=edit' ));
893 exit;
894 }
895 }
896 }
897 /**
898 * Filtered allowed html tags & attributes, add data-button to div tags to ensure forms are saved properly.
899 * Hooked on 'wp_kses_allowed_html'
900 *@since 1.0.0
901 *@param array $allowed array of allowed tags and attributes.
902 *@param mixed $context context in which content is filtered.
903 *@return array allowed tags and ttribtues.
904 */
905 public function custom_kses_rules($allowed, $context){
906 if(is_array($context)){
907 return $allowed;
908 }
909 if('post'===$context){
910 $allowed['div']['data-button'] = true; //table buttons.
911 $allowed['div']['data-form'] = true; //sub-forms.
912 $allowed['div']['data-on'] = true; //toggles.
913 $allowed['div']['data-off'] = true; //toggles.
914 $allowed['div']['data-open'] = true; //accordion.
915 $allowed['div']['data-group'] = true; //accordion.
916 $allowed['div']['data-config-field'] = true; //cf7 plugin.
917 }
918 return $allowed;
919 }
920 /**
921 * Filters the default form loaded when a new CF7 form is created
922 * Hooked on 'wpcf7_default_template'
923 * @since 1.0
924 * @param string $template the html string for the form tempalte
925 * @param string $prop the template property required.
926 */
927 public function default_cf7_form($template, $prop){
928 if($prop !== 'form') return $template;
929 include( plugin_dir_path( __FILE__ ) . '/partials/cf7-default-form.php');
930 return $template;
931 }
932 /**
933 * Ajax function to return the content of a cf7 form
934 * Hooked on 'wp_ajax_get_cf7_content'
935 * @since 1.0.0
936 * @return String cf7 form content.
937 **/
938 public function get_cf7_content(){
939 if( !isset($_POST['nonce']) ){
940 echo 'error, nonce failed, try to reload the page.';
941 wp_die();
942 }
943 if(!wp_verify_nonce($_POST['nonce'], 'wpcf7-save-contact-form_' . $_POST['id'], '_wpcf7nonce')){
944 echo 'error, nonce failed, try to reload the page.';
945 wp_die();
946 }
947 $cf7_key = sanitize_text_field($_POST['cf7_key']);
948 $sub_forms = get_posts(array(
949 'post_type' => $this->cf7_post_type(),
950 'post_name__in' => array($cf7_key)
951 ));
952 $form='';
953 if(!empty($sub_forms)){
954 $form = get_post_meta($sub_forms[0]->ID, '_form', true);
955 echo $form;
956 }else{
957 echo 'unable to find form '.$cf7_key;
958 }
959 wp_die();
960 }
961 /**
962 * Adds a [taxonomy] tag to cf7 forms
963 * Hooked on 'wpcf7_admin_init'
964 * @since 1.0.0
965 **/
966 public function cf7_shortcode_tags(){
967 if ( class_exists( 'WPCF7_TagGenerator' ) ) {
968 $tag_generator = WPCF7_TagGenerator::get_instance();
969 $tag_generator->add(
970 'dynamic_select', //tag id
971 __( 'dynamic-dropdown', 'cf7_2_post' ), //tag button label
972 array($this,'dynamic_tag_generator') //callback
973 );
974 $tag_generator->add(
975 'benchmark', //tag id
976 __( 'benchmark', 'cf7_2_post' ), //tag button label
977 array($this,'benchmark_tag_generator') //callback
978 );
979 }
980 }
981 /**
982 * Dynamic select screen displayt.
983 *
984 * This function is called by cf7 plugin, and is registered with a hooked function above
985 *
986 * @since 1.0.0
987 * @param WPCF7_ContactForm $contact_form the cf7 form object
988 * @param array $args arguments for this form.
989 */
990 function dynamic_tag_generator( $contact_form, $args = '' ) {
991 $args = wp_parse_args( $args, array() );
992 include( plugin_dir_path( __FILE__ ) . '/partials/cf7-dynamic-tag-display.php');
993 }
994 /**
995 * Benchmark input screen displayt.
996 *
997 * This function is called by cf7 plugin, and is registered with a hooked function above
998 *
999 * @since 1.0.0
1000 * @param WPCF7_ContactForm $contact_form the cf7 form object
1001 * @param array $args arguments for this form.
1002 */
1003 function benchmark_tag_generator( $contact_form, $args = '' ) {
1004 $args = wp_parse_args( $args, array() );
1005 include( plugin_dir_path( __FILE__ ) . '/partials/cf7-benchmark-tag.php');
1006 }
1007 /**
1008 * Print hiddend field on cf7 post submit box
1009 * Hooked on 'wpcf7_admin_misc_pub_section'
1010 * @since 1.0.0
1011 * @param string $post_id cf7 form post id .
1012 **/
1013 public function dynamic_select_choices($post_id){
1014 echo '<input id="cf72post-dynamic-select" type="hidden" name="cf72post_dynamic_select_taxonomies" />';
1015 }
1016 /**
1017 * CF7 Form saved from backend, check if dynamic-select are used
1018 * Hooked on 'wpcf7_save_contact_form'
1019 * @since 1.0.0
1020 * @param WPCF7_Contact_Form $cf7_form cf7 form object
1021 */
1022 public function save_factory_metas($cf7_form){
1023 $cf7_post_id = $cf7_form->id();
1024 //get the tags used in this form
1025 if( isset($_POST['cf72post_dynamic_select_taxonomies']) ){
1026 $created_taxonomies = array();
1027 $taxonomies = json_decode(str_replace('\"','"',$_POST['cf72post_dynamic_select_taxonomies']), true);
1028 if(empty($taxonomies)){
1029 $taxonomies = array();
1030 }
1031 foreach($taxonomies as $taxonomy){
1032 //sanitize fields before saving into the DB.
1033 foreach($taxonomy as $key=>$value){
1034 $key = sanitize_key($key);
1035 $taxonomy[$key] = sanitize_text_field($value);
1036 }
1037 $created_taxonomies[$taxonomy['slug']] = $taxonomy;
1038 }
1039 //debug_msg($created_taxonomies);
1040 $tags = $cf7_form->scan_form_tags(); //get your form tags
1041 $post_lists = $saved_lists = $system_list = array();
1042
1043 $dropdowns = get_option('_cf7sg_dynamic_dropdown_taxonomy',array());
1044 foreach($dropdowns as $id => $lists){
1045 $saved_lists = array_merge($saved_lists, $lists);
1046 }
1047 foreach($tags as $tag){
1048 if('dynamic_select' == $tag['basetype']){
1049 if(isset($tag['values'])){
1050 $slug='';
1051 foreach($tag['values'] as $values){
1052 if(0 == strpos($values, 'slug:') ){
1053 $slug = str_replace('slug:', '', $values);
1054 break;
1055 }
1056 }
1057 if(empty($slug)) continue; //not a taxonomy list.
1058 //is slug newly created?
1059 if(isset($created_taxonomies[$slug])){
1060 //store this taxonomy.
1061 $post_lists[$slug] = $created_taxonomies[$slug];
1062 }else if(isset($saved_lists[$slug])){
1063 //retain previously saved list if we are stil using it.
1064 $post_lists[$slug] = $saved_lists[$slug];
1065 }else{ //system taxonomy.
1066 $system_list[] = $slug;
1067 }
1068 //store the taxonomy slug in the cf7 metas.
1069 //$post_lists[$slug] = null;
1070 }
1071 }
1072 }
1073 //list of taxonomy to register.
1074 //unset the old value.
1075 unset($dropdowns[$cf7_post_id]);
1076 //unshift new value to top of array.
1077 $dropdowns = array($cf7_post_id => $post_lists) + $dropdowns ;
1078
1079 update_option('_cf7sg_dynamic_dropdown_taxonomy', $dropdowns);
1080 //list of system taxonomy to register
1081 $system_dropdowns = get_option('_cf7sg_dynamic_dropdown_system_taxonomy',array());
1082 $system_dropdowns[$cf7_post_id] = $system_list;
1083 update_option('_cf7sg_dynamic_dropdown_system_taxonomy', $system_dropdowns);
1084
1085 }
1086 }
1087
1088 /**
1089 * function to regsiter dyanmic dropdown taxonomies
1090 *
1091 * @since 1.0.0
1092 * @param Object $taxonomy_object std object with parameters $taxonomy_object->slug, $taxonomy_object->singular, $taxonomy_object->plural, $taxonomy_object->hierarchical .
1093 **/
1094 protected function register_dynamic_dropdown($taxonomy_array){
1095 $slug = $taxonomy_array['slug'];
1096 $name = $taxonomy_array['singular'];
1097 $plural = $taxonomy_array['plural'];
1098 $is_hierarchical = $taxonomy_array['hierarchical'];
1099
1100 $labels = array(
1101 'name' => $plural,
1102 'singular_name' => $name,
1103 'menu_name' => $plural,
1104 'all_items' => 'All '.$plural,
1105 'parent_item' => 'Parent '.$name,
1106 'parent_item_colon' => 'Parent '.$name.':',
1107 'new_item_name' => 'New '.$name.' Name',
1108 'add_new_item' => 'Add New '.$name,
1109 'edit_item' => 'Edit '.$name,
1110 'update_item' => 'Update '.$name,
1111 'view_item' => 'View '.$name,
1112 'separate_items_with_commas' => 'Separate '.$plural.' with commas',
1113 'add_or_remove_items' => 'Add or remove '.$plural,
1114 'choose_from_most_used' => 'Choose from the most used',
1115 'popular_items' => 'Popular '.$plural,
1116 'search_items' => 'Search '.$plural,
1117 'not_found' => 'Not Found',
1118 'no_terms' => 'No '.$plural,
1119 'items_list' => $plural.' list',
1120 'items_list_navigation' => $plural.' list navigation',
1121 );
1122 //labels can be modified post registration
1123 $args = array(
1124 'labels' => $labels,
1125 'hierarchical' => $is_hierarchical,
1126 'public' => false,
1127 'show_ui' => true,
1128 'show_admin_column' => false,
1129 'show_in_nav_menus' => false,
1130 'show_tagcloud' => false,
1131 'show_in_quick_edit' => false,
1132 'description' => 'Contact Form 7 dynamic dropdown taxonomy list',
1133 );
1134
1135 register_taxonomy( $slug, $this->cf7_post_type(), $args );
1136 }
1137 /**
1138 * Deactivate this plugin if CF7 plugin is deactivated.
1139 * Hooks on action 'admin_init'
1140 * @since 2.1
1141 */
1142 //public function deactivate_cf7_polylang( $plugin, $network_deactivating ) {
1143 public function check_plugin_dependency() {
1144 //if either the polylang for the cf7 plugin is not active anymore, deactive this extension
1145 if( !is_plugin_active("contact-form-7/wp-contact-form-7.php") ){
1146 deactivate_plugins( "cf7-grid-layout/cf7-grid-layout.php" );
1147 debug_msg("Deactivating Smart Grid");
1148
1149 $button = '<a href="'.network_admin_url('plugins.php').'">Return to Plugins</a></a>';
1150 wp_die( '<p><strong>CF7 smart Grid-layout Extension</strong> requires <strong>Contact Form 7</strong> plugin, and has therefore been deactivated!</p>'.$button );
1151
1152 return false;
1153 }
1154 return true;
1155 }
1156 /**
1157 * Add disabled button message on hover to cf7 messages.
1158 * Hooked to 'wpcf7_messages', see file contact-form-7/includes/contact-form-template.php fn messages().
1159 *@since 2.6.0
1160 *@param array $messages array of messages to filter.
1161 *@return array array of cf7 messages.
1162 */
1163 public function disabled_message($messages){
1164 $messages['submit_disabled'] = array(
1165 'description'
1166 => __( "Hover message for disabled submit/save button", 'cf7-grid-layout' ),
1167 'default'
1168 => __( "Disabled! To enable, check the acceptance field.", 'cf7-grid-layout' ),
1169 );
1170 /** @sicne 2.8.0 */
1171 $messages['max_table_rows'] = array(
1172 'description'
1173 => __( "Message displayed when max tables rows reached.", 'cf7-grid-layout' ),
1174 'default'
1175 => __( "You have reached the maximum number of rows.", 'cf7-grid-layout' ),
1176 );
1177 return $messages;
1178 }
1179 /**
1180 * Adds pretty pointers to help users.
1181 * Hooked on 'admin_enqueue_scripts'
1182 *@since 2.6.0
1183 *@param string $hook_suffix current page.
1184 */
1185 public function pretty_admin_pointers($hook_suffix){
1186 $screen = get_current_screen();
1187 if (!isset($screen) || $this->cf7_post_type() != $screen->post_type || in_array( $hook_suffix, array('post.php', 'post-new.php'), true)){
1188 return;
1189 }
1190 $enqueue_pointer = false;
1191 // Get array list of dismissed pointers for current user and convert it to array
1192 $user_id = get_current_user_id();
1193 $dismissed_pointers = explode( ',', get_user_meta( $user_id, 'dismissed_wp_pointers', true ) );
1194 $pointers = array();
1195 /**
1196 * Filter to add custom pointers for user iterface.
1197 * @var array $pointers an array of $pointer_id=>array($content, $arrow, $valign) key/value pairs to filter. The key is the pointer id to identify which ones the user has dismissed. The value is an array with the message $content, the position of the $arrow (left|right|top|bottom), the vertical alignment ($valign) of the box (center|top|bottom).
1198 * @return array an array of pointers, which will be checked agains the current user.
1199 */
1200 $filter_pointers = apply_filters('cf7sg_plugin_pointers-'.$screen->id, array());
1201 foreach($filter_pointers as $id=>$pointer){
1202 // Check if our pointer is not among dismissed ones
1203 if( !in_array( $id, $dismissed_pointers ) ) {
1204 $enqueue_pointer = true;
1205 $pointers[$id] = array($pointer[0], $pointer[1], $pointer[2], $pointer[3]);
1206 }
1207 }
1208 // Enqueue pointer CSS and JS files, if needed
1209 if( $enqueue_pointer ) {
1210 wp_enqueue_style( 'wp-pointer' );
1211 wp_enqueue_script( 'wp-pointer' );
1212 wp_enqueue_script('cf7sg-pointer-js', plugin_dir_url( __DIR__ ).'admin/js/cf7sg-pointers.js', array('jquery'), $this->version, true);
1213 wp_localize_script('cf7sg-pointer-js', 'cf7sg_pointers',
1214 array('pointers'=>$pointers, 'next'=>__('Next', 'cf7-grid-layout'))
1215 );
1216 // Add footer scripts using callback function
1217 //add_action( 'admin_print_footer_scripts', array($this, 'pretty_pointer_script') );
1218 }
1219 }
1220 /**
1221 * CF7 Table pointer notices.
1222 * Hooked on 'cf7sg_plugin_pointers-edit-wpcf7_contact_form'
1223 *@since 2.6.0
1224 *@param array $pointers array of pointers to suplement
1225 *@return array array of pointers element-id=>hmtl to display key value pairs.
1226 */
1227 public function edit_pointers($pointers){
1228 ob_start();
1229 include_once 'partials/pointers/cf7sg-pointer-update-forms.php';
1230 $content =ob_get_contents();
1231 $pointers['update_forms_pointer'] = array($content, 'left', 'center','');
1232 /* tutorials */
1233 ob_clean();
1234 include_once 'partials/pointers/cf7sg-pointer-tutorials.php';
1235 $content =ob_get_contents();
1236 $pointers['tutorials_pointer'] = array($content, 'left', 'center','a[href="admin.php?page=admin.php?page=cf7sg_help"]');
1237 /* shortcodes */
1238 ob_clean();
1239 include_once 'partials/pointers/cf7sg-pointer-shortcodes.php';
1240 $content = ob_get_clean();
1241 $pointers['cf7sg_shortcodes'] = array($content, 'top', 'top','');
1242 return $pointers;
1243 }
1244 /**
1245 * CF7 Edit pointer notices.
1246 * Hooked on 'cf7sg_plugin_pointers-wpcf7_contact_form'
1247 *@since 2.6.0
1248 *@param array $pointers array of pointers to suplement
1249 *@return array array of pointers element-id=>hmtl to display key value pairs.
1250 */
1251 public function post_pointers($pointers){
1252 ob_start();
1253 include_once 'partials/pointers/cf7sg-pointer-editor-full-screen.php';
1254 $content =ob_get_contents();
1255 //content / arrow [top,bottom,left,right] / box align [top,bottom,center] / css selector.
1256 $pointers['full_screen'] = array($content, 'left', 'center','#full-screen-cf7');
1257 /* shortcodes */
1258 ob_clean();
1259 include_once 'partials/pointers/cf7sg-pointer-editor-tabs.php';
1260 $content =ob_get_contents();
1261 $pointers['editor_tabs'] = array($content, 'right', 'center','#form-editor-tabs>ul>li.ui-tabs-active');
1262 /* shortcodes */
1263 ob_clean();
1264 include_once 'partials/pointers/cf7sg-pointer-editor-rows-control.php';
1265 $content = ob_get_contents();
1266 if(!empty($content)){
1267 $pointers['row_controls'] = array($content, 'right', 'center','#grid-form>.container>.row>.row-controls');
1268 ob_clean();
1269 }
1270 include_once 'partials/pointers/cf7sg-pointer-editor-column-control.php';
1271 $content = ob_get_contents();
1272 if(!empty($content)){
1273 $pointers['column_controls'] = array($content, 'left', 'center','#grid-form>.container>.row>.columns:first-child>.grid-column>span.icon-code');
1274 ob_clean();
1275 }
1276 include_once 'partials/pointers/cf7sg-pointer-tag-dynamic-dropdown.php';
1277 $content = ob_get_contents();
1278 $pointers['dynamic_dropdown'] = array($content, 'left', 'center','#top-tags>#tag-generator-list > a[title*="dynamic-dropdown"]');
1279 ob_clean();
1280 /* #optional-editors */
1281 include_once 'partials/pointers/cf7sg-pointers-editor-optional-js-css.php';
1282 $content = ob_get_contents();
1283 $pointers['js_css_editors'] = array($content, 'left', 'center','#optional-editors');
1284 ob_clean();
1285 include_once 'partials/pointers/cf7sg-pointer-tag-benchmark.php';
1286 $content = ob_get_clean();
1287 $pointers['benchmark'] = array($content, 'left', 'center','#top-tags>#tag-generator-list > a[title*="benchmark"]');
1288 return $pointers;
1289 }
1290 /**
1291 * Rest meta_caps for better fine tuning of user caps.
1292 * hooked to 'wpcf7_map_meta_cap'.
1293 *@since 3.0.0
1294 *@param array $caps key->value pairs of capabilities.
1295 *@return array key->value pairs of capabilities.
1296 */
1297 public function reset_meta_cap($caps){
1298 return array(
1299 'wpcf7_read_contact_form'=>'read_post',
1300 'wpcf7_edit_contact_form' =>'edit_post',
1301 'wpcf7_edit_contact_forms' => 'edit_posts',
1302 'wpcf7_edit_others_contact_forms'=>'edit_others_posts',
1303 'wpcf7_edit_published_contact_forms'=>'edit_published_posts',
1304 'wpcf7_delete_contact_form'=>'delete_post',
1305 'wpcf7_delete_contact_forms'=>'delete_posts',
1306 'wpcf7_delete_published_contact_forms'=>'delete_published_posts',
1307 'wpcf7_delete_others_contact_forms'=>'delete_others_posts',
1308 'wpcf7_publish_contact_forms'=>'publish_posts',
1309 'wpcf7_publish_contact_forms'=>'read_private_posts',
1310 'wpcf7_submit'=>'read', /** @since 3.2.1 to fix subscribers_only. */
1311 );
1312 }
1313 /**
1314 * CF7 plugin by default sets form post status to 'publish' regardless of user capability.
1315 * This functions rectifies this. Hooked to 'wp_insert_post_data'.
1316 *@since 3.0.0
1317 *@param array $data sanitised post data.
1318 *@return array post data.
1319 */
1320 public function pending_for_review($data){
1321 if($this->cf7_post_type() != $data['post_type']) return $data;
1322
1323 $post_type_object = get_post_type_object( $this->cf7_post_type() );
1324 //check if user can publish.
1325 if(!current_user_can($post_type_object->cap->publish_posts) && $data['post_status']=='publish'){
1326 $data['post_status']='pending';
1327 }
1328 // debug_msg('post status '.$data['post_status']);
1329 return $data;
1330 }
1331 /**
1332 * Enable all cf7 capabilites for editors.
1333 * Hooked to action 'admin_init'.
1334 *@since 3.0.0
1335 */
1336 public function enable_cf7_editor_role(){
1337 global $wp_roles;
1338 if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();
1339 $caps=array('wpcf7_edit_contact_forms','wpcf7_edit_others_contact_forms','wpcf7_edit_published_contact_forms','wpcf7_read_contact_forms','wpcf7_publish_contact_forms','wpcf7_delete_contact_forms','wpcf7_delete_published_contact_forms','wpcf7_delete_others_contact_forms');
1340 $fe = $wp_roles->get_role('editor');
1341 if(!empty($fe)){ /** @since 3.0.3 in case editor role is deleted */
1342 foreach($caps as $cap) $fe->add_cap($cap);
1343 }
1344 $caps[]='wpcf7_manage_integration';
1345 $ad = $wp_roles->get_role('administrator');
1346 if(!empty($ad)){ /** @since 3.0.3 in case admin role is deleted */
1347 foreach($caps as $cap) $ad->add_cap($cap);
1348 }
1349
1350 /** @since 3.1.3 redirect cf7 plugin pages*/
1351 global $pagenow;
1352 if ( $pagenow == 'admin.php' && isset( $_GET['page'] ) ) {
1353 $url = '';
1354 switch($_GET['page']){
1355 case 'wpcf7-new':
1356 $url = "post-new.php?post_type=wpcf7_contact_form" ;
1357 break;
1358 case 'wpcf7':
1359 if(isset($_GET['post'])) $url = "post.php?post={$_GET['post']}&action=edit&classic-editor";
1360 else $url = "edit.php?post_type=wpcf7_contact_form";
1361 break;
1362 }
1363 if(!empty($url)){
1364 wp_redirect( admin_url( $url ));
1365 exit;
1366 }
1367 }
1368 }
1369 /**
1370 * Add grid helper hooks for individual tags.
1371 * Hooked to action 'cf7sg_ui_grid_helper_hooks'.
1372 *@since 3.3.0
1373 */
1374 public function print_helper_hooks(){
1375 require_once plugin_dir_path( __FILE__ ) .'partials/helpers/cf7sg-form-fields.php';
1376 }
1377 /**
1378 * Setup mailtags or toggles.
1379 *
1380 *@since 4.0.0
1381 *@param string $param text_description
1382 *@return string text_description
1383 */
1384 public function setup_cf7_mailtags($mailtags = array()){
1385 // return $mailtags;
1386 $contact_form = WPCF7_ContactForm::get_current();
1387 $fields = get_post_meta($contact_form->id(), '_cf7sg_grid_toggled_names', true);
1388 //update_post_meta($post_id, '_cf7sg_grid_tabbed_toggles', $ttgls);
1389 if(!empty($fields)){
1390 $toggles = array_keys($fields);
1391 $groups = get_post_meta($contact_form->id(), '_cf7sg_grid_grouped_toggles', true);
1392 if(!empty($groups)){
1393 foreach($groups as $group=>$grp_toggles){
1394 $toggles = array_diff($toggles, $grp_toggles);
1395 array_push($toggles, $group);
1396 }
1397 }
1398 foreach($toggles as $tgl) $mailtags[]= 'cf7sg-toggle-'.$tgl;
1399 }
1400 //add a general form tag.
1401 $mailtags[]= 'cf7sg-form-'.get_cf7form_key($contact_form->id());
1402 return $mailtags;
1403 }
1404 }
1405