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

1,738 lines 73.3 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 4.2.1
28 * @access private
29 * @var string $plugin_name The ID of this plugin.
30 */
31 private static $admin_notice_pages = array(
32 'admin.php'=>array('cf7sg_help'=>'page'),
33 'edit.php'=>array('wpcf7_contact_form'=>'post_type'),
34 'post.php'=> 'wpcf7_contact_form',
35 'edit-tags.php'=>array('wpcf7_contact_form'=>'post_type'),
36 'index.php'=>'',//dashboard ,
37 'plugins.php'=>'',
38 //'options-general.php'=>'page=',
39 );
40 /**
41 * The ID of this plugin.
42 *
43 * @since 1.0.0
44 * @access private
45 * @var string $plugin_name The ID of this plugin.
46 */
47 private $plugin_name;
48
49 /**
50 * The ID of this plugin.
51 *
52 * @since 4.6.0
53 * @access public
54 * @var Constant CF7SG_OPTION Options key.
55 */
56 const CF7SG_OPTION = 'cf7sg-plugin-version';
57
58 /**
59 * The version of this plugin.
60 *
61 * @since 1.0.0
62 * @access private
63 * @var string $version The current version of this plugin.
64 */
65 private $version;
66 /**
67 * Initialize the class and set its properties.
68 *
69 * @since 1.0.0
70 * @param string $plugin_name The name of this plugin.
71 * @param string $version The version of this plugin.
72 */
73 public function __construct( $plugin_name, $version ) {
74 $this->plugin_name = $plugin_name;
75 $this->version = $version;
76 }
77 /**
78 * get cf7 post type set by cf7 plugin.
79 *
80 *@since 3.0.0
81 *@return string post type.
82 */
83 private function cf7_post_type(){
84 $type = 'wpcf7_post_type_notset';
85 if(class_exists('WPCF7_ContactForm') ) {
86 $type = WPCF7_ContactForm::post_type;
87 }
88 return $type;
89 }
90 /**
91 * Hack to add scripts/styles to edit page.
92 * Hooked on 'admin_enqueue_scripts'.
93 *@since 1.5.0
94 */
95 public function popular_extentions_scripts(){
96 $screen = get_current_screen();
97 if (empty($screen) || $this->cf7_post_type() != $screen->post_type){
98 return;
99 }
100 $plugin_dir = plugin_dir_url( __DIR__ );
101
102 switch( $screen->base ){
103 case 'post':
104 $page_hook = '';
105 if('add' == $screen->action) $page_hook = 'contact_page_wpcf7-new';
106 else $page_hook = 'toplevel_page_wpcf7';
107 //unhook this function.
108 remove_action('admin_enqueue_scripts', array($this, 'popular_extentions_scripts'),999,0);
109 do_action('admin_enqueue_scripts', $page_hook);
110 $plugin_page ='';
111 add_action('admin_enqueue_scripts', array($this, 'popular_extentions_scripts'),999,0);
112 break;
113 case 'edit': //table.
114 break;
115 }
116 }
117 /**
118 * Function to spoof the wpcf7 plugin admin scripts loading for extensions
119 * Hooked on 'admin_print_scripts' and used to fix the issue with the mailchimp ext.
120 *@since 1.5.1
121 */
122 public function print_extentions_scripts(){
123 $screen = get_current_screen();
124 if (empty($screen) || $this->cf7_post_type() != $screen->post_type){
125 return;
126 }
127 global $plugin_page;
128 $plugin_page = 'wpcf7';
129 }
130
131
132 /**
133 * Register the stylesheets for the admin area.
134 *
135 * @since 1.0.0
136 */
137 public function enqueue_styles($page) {
138
139 if( in_array($page, array('toplevel_page_wpcf7', 'contact_page_wpcf7-new')) ) return;
140
141 $screen = get_current_screen();
142 $plugin_dir = plugin_dir_url( __DIR__ );
143 if('plugins'==$screen->base){
144 /** @since 4.1.0 */
145 wp_enqueue_style('plugin-update',$plugin_dir . 'admin/css/cf7sg-plugin-update.css', array(), $this->version, 'all');
146 return;
147 }
148 if (empty($screen) || $this->cf7_post_type() != $screen->post_type){
149 return;
150 }
151 switch( $screen->base ){
152 case 'post':
153 wp_enqueue_style( "cf7-grid-post-css", $plugin_dir . 'admin/css/cf7-grid-layout-post.css', array(), $this->version, 'all' );
154 //dynamic tag
155 wp_enqueue_style('cf7sg-dynamic-tag-css', $plugin_dir . 'admin/css/cf7sg-dynamic-tag.css', array(), $this->version, 'all' );
156 //benchmark tag
157 wp_enqueue_style('cf7sg-benchmark-tag-css', $plugin_dir . 'admin/css/cf7sg-benchmark-tag.css', array(), $this->version, 'all' );
158 //codemirror
159 wp_enqueue_style( "codemirror-css", $plugin_dir . 'assets/codemirror/codemirror.css', array(), $this->version, 'all' );
160 /** @since 4.0 enable dark theme */
161 wp_enqueue_style( "codemirror-theme-light-css", $plugin_dir . 'assets/codemirror/theme/paraiso-light.css', array(), $this->version, 'all' );
162 wp_enqueue_style( "codemirror-theme-dark-css", $plugin_dir . 'assets/codemirror/theme/material-ocean.css', array(), $this->version, 'all' );
163 wp_enqueue_style( "codemirror-foldgutter-css", $plugin_dir . 'assets/codemirror/addon/fold/foldgutter.css', array(), $this->version, 'all' );
164 wp_enqueue_style( "codemirror-dialog-css", $plugin_dir . 'assets/codemirror/addon/dialog/dialog.css', array(), $this->version, 'all' );
165 wp_enqueue_style( "codemirror-matchesonscrollbar-css", $plugin_dir . 'assets/codemirror/addon/search/matchesonscrollbar.css', array(), $this->version, 'all' );
166 wp_enqueue_style( 'smart-grid-css', $plugin_dir . 'assets/css.gs/smart-grid.admin.css', array(), $this->version, 'all');
167 wp_enqueue_style('dashicons');
168 wp_enqueue_style('select2-style', $plugin_dir . 'assets/select2/css/select2.min.css', array(), $this->version, 'all' );
169 /** @since 4.4.0 Enabled user style enqueue */
170 do_action('cf7sg_enqueue_admin_editor_styles');
171 break;
172 case 'edit': //table.
173 wp_enqueue_style( $this->plugin_name, $plugin_dir . 'admin/css/cf7-grid-layout-admin.css', array(), $this->version, 'all' );
174 /** @since 4.4.0 Enabled user style enqueue */
175 do_action('cf7sg_enqueue_admin_table_styles');
176 break;
177 }
178
179 }
180
181 /**
182 * Register the JavaScript for the admin area.
183 *
184 * @since 1.0.0
185 */
186 public function enqueue_scripts($page) {
187
188 $plugin_dir = plugin_dir_url( __DIR__ );
189 if( in_array($page, array('toplevel_page_wpcf7', 'contact_page_wpcf7-new')) ){
190 wp_enqueue_script( $this->plugin_name, $plugin_dir . 'admin/js/cf7-grid-layout-admin.js', array( 'jquery' ), $this->version, true );
191 return;
192 }
193 $screen = get_current_screen();
194 if('plugins'==$screen->base){
195 /** @since 4.1.0 */
196 wp_enqueue_script('cf7sg-plugin-update', $plugin_dir . 'admin/js/cf7sg-plugin-update.js', array('jquery'));
197 /* translators: message displayed when succesful update to validate new version */
198 wp_localize_script('cf7sg-plugin-update', 'cf7sg',array(
199 'msg'=> __('Validating your update...','cf7-grid-layout'),
200 'nonce'=> wp_create_nonce( 'cf7sg_udpate_plugin' ),
201 'error'=>__('Unable to validate, please reload!', 'cf7-grid-layout')
202 ));
203 return;
204 }
205 if ($this->cf7_post_type() != $screen->post_type){
206 return;
207 }
208
209 switch( $screen->base ){
210 case 'post':
211 /* register codemirror editor & addons. */
212 //codemirror script
213 wp_register_script( 'cf7-codemirror-js',
214 $plugin_dir . 'assets/codemirror/codemirror.js',
215 null, "5.32", true
216 );
217 /** @since 3.1.2 initialise codemirror after library load and parse as attribute to anonymous functtion in cf7-grid-codemirror.js */
218 wp_add_inline_script('cf7-codemirror-js',
219 'const cmInitialSettings = {
220 value:"",autoCloseTags:true,
221 extraKeys: {"Ctrl-Space": "autocomplete", "Ctrl-/": "toggleComment", "Ctrl-J": "toMatchingTag"},
222 lineNumbers: true, styleActiveLine: true,
223 matchBrackets: true, tabSize:2, lineWrapping: true, addModeClass: true,
224 foldGutter: true, autofocus:false,
225 gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
226 }
227 const codeMirror_5_32 = CodeMirror(document.getElementById("cf7-codemirror"),cmInitialSettings);
228 const cssCodeMirror_5_32 = CodeMirror(document.getElementById("cf7-css-codemirror"),cmInitialSettings);
229 const jsCodeMirror_5_32 = CodeMirror(document.getElementById("cf7-js-codemirror"),cmInitialSettings);');
230 //matchtags.
231 wp_enqueue_script( 'codemirror-closetag-js',
232 $plugin_dir . 'assets/codemirror/addon/edit/closetag.js',
233 array('cf7-codemirror-js'), $this->version, true
234 );
235 //fold code
236 wp_enqueue_script( 'codemirror-foldcode-js',
237 $plugin_dir . 'assets/codemirror/addon/fold/foldcode.js',
238 array('cf7-codemirror-js'), $this->version, true
239 );
240 wp_enqueue_script( 'codemirror-foldgutter-js',
241 $plugin_dir . 'assets/codemirror/addon/fold/foldgutter.js',
242 array('cf7-codemirror-js'), $this->version, true
243 );
244 wp_enqueue_script( 'codemirror-indent-fold-js',
245 $plugin_dir . 'assets/codemirror/addon/fold/indent-fold.js',
246 array('cf7-codemirror-js'), $this->version, true
247 );
248 wp_enqueue_script( 'codemirror-xml-fold-js',
249 $plugin_dir . 'assets/codemirror/addon/fold/xml-fold.js',
250 array('cf7-codemirror-js'), $this->version, true
251 );
252 wp_enqueue_script( 'codemirror-matchtag-js',
253 $plugin_dir . 'assets/codemirror/addon/edit/matchtags.js',
254 array('cf7-codemirror-js','codemirror-xml-fold-js'), $this->version, true
255 );
256 wp_enqueue_script( 'codemirror-brace-fold-js',
257 $plugin_dir . 'assets/codemirror/addon/fold/brace-fold.js',
258 array('cf7-codemirror-js'), $this->version, true
259 );
260 wp_enqueue_script( 'codemirror-comment-fold-js',
261 $plugin_dir . 'assets/codemirror/addon/fold/comment-fold.js',
262 array('cf7-codemirror-js'), $this->version, true
263 );
264 wp_enqueue_script( 'codemirror-mixed-js',
265 $plugin_dir . 'assets/codemirror/mode/htmlmixed/htmlmixed.js',
266 array('cf7-codemirror-js'), $this->version, true
267 );
268 wp_enqueue_script( 'codemirror-javascript-js',
269 $plugin_dir . 'assets/codemirror/mode/javascript/javascript.js',
270 array('cf7-codemirror-js'), $this->version, true
271 );
272 wp_enqueue_script( 'codemirror-xml-js',
273 $plugin_dir . 'assets/codemirror/mode/xml/xml.js',
274 array('cf7-codemirror-js'), $this->version, true
275 );
276 wp_enqueue_script( 'codemirror-css-js',
277 $plugin_dir . 'assets/codemirror/mode/css/css.js',
278 array('cf7-codemirror-js'), $this->version, true
279 );
280 //overlay for shortcode highligh
281 wp_enqueue_script( 'codemirror-overlay-js',
282 $plugin_dir . 'assets/codemirror/addon/mode/overlay.js',
283 array('cf7-codemirror-js'), $this->version, true
284 );
285 /**
286 * @since 1.3.0 enable search codemirror
287 */
288 //overlay for shortcode highligh
289 wp_enqueue_script( 'codemirror-search-js',
290 $plugin_dir . 'assets/codemirror/addon/search/search.js',
291 array('cf7-codemirror-js'), $this->version, true
292 );
293 wp_enqueue_script( 'codemirror-jumptoline-js',
294 $plugin_dir . 'assets/codemirror/addon/search/jump-to-line.js',
295 array('cf7-codemirror-js'), $this->version, true
296 );
297 wp_enqueue_script( 'codemirror-matchesonscrollbar-js',
298 $plugin_dir . 'assets/codemirror/addon/search/matchesonscrollbar.js',
299 array('cf7-codemirror-js'), $this->version, true
300 );
301 wp_enqueue_script( 'codemirror-searchcursor-js',
302 $plugin_dir . 'assets/codemirror/addon/search/searchcursor.js',
303 array('cf7-codemirror-js'), $this->version, true
304 );
305 wp_enqueue_script( 'codemirror-annotatescrollbar-js',
306 $plugin_dir . 'assets/codemirror/addon/scroll/annotatescrollbar.js',
307 array('cf7-codemirror-js'), $this->version, true
308 );
309 wp_enqueue_script( 'codemirror-dialog-js',
310 $plugin_dir . 'assets/codemirror/addon/dialog/dialog.js',
311 array('cf7-codemirror-js'), $this->version, true
312 );
313
314 //js beautify
315 wp_enqueue_script( 'beautify-js',
316 $plugin_dir . 'assets/beautify/beautify.js',
317 array('jquery'), $this->version, true
318 );
319 wp_enqueue_script( 'beautify-html-js',
320 $plugin_dir . 'assets/beautify/beautify-html.js',
321 array('beautify-js'), $this->version, true
322 );
323 wp_enqueue_script('jquery-select2', $plugin_dir . 'assets/select2/js/select2.min.js', array( 'jquery' ), $this->version, true );
324 /** @since 4.0 enable dark theme */
325 $cm_light = apply_filters('cf7sg_admin_editor_theme', '');
326 $user_theme = get_user_meta(get_current_user_id(),'_cf7sg_cm_theme', true);
327 if(!empty($cm_light) && file_exists(get_stylesheet_directory().'/'.$cm_light) ){
328 wp_enqueue_style( "codemirror-theme-css", get_stylesheet_directory_uri().'/'.$cm_light, array(), $this->version, 'all' );
329 $user_theme = basename($cm_light, '.css');
330 update_user_meta(get_current_user_id(),'_cf7sg_cm_theme', '');
331 }else{
332 if(empty($user_theme)){
333 $user_theme = 'paraiso-light';
334 update_user_meta(get_current_user_id(),'_cf7sg_cm_theme', 'light');
335 }else{
336 $user_theme = ('light'==$user_theme ? 'paraiso-light' : 'material-ocean');
337 }
338 }
339 $cm_js_light = apply_filters('cf7sg_admin_js_editor_theme', '');
340 $user_js_theme = get_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', true);
341 if(!empty($cm_js_light) && file_exists(get_stylesheet_directory().'/'.$cm_js_light) ){
342 wp_enqueue_style( "codemirror-js-theme-css", get_stylesheet_directory_uri().'/'.$cm_js_light, array(), $this->version, 'all' );
343 $user_js_theme = basename($cm_js_light, '.css');
344 update_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', '');
345 }else{
346 if(empty($user_js_theme)){
347 $user_js_theme = 'paraiso-light';
348 update_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', 'light');
349 }else{
350 $user_js_theme = ('light'==$user_js_theme ? 'paraiso-light' : 'material-ocean');
351 }
352 }
353 $cm_css_light = apply_filters('cf7sg_admin_css_editor_theme', '');
354 $user_css_theme = get_user_meta(get_current_user_id(),'_cf7sg_css_cm_theme', true);
355 if(!empty($cm_css_light) && file_exists(get_stylesheet_directory().'/'.$cm_css_light) ){
356 wp_enqueue_style( "codemirror-css-theme-css", get_stylesheet_directory_uri().'/'.$cm_css_light, array(), $this->version, 'all' );
357 $user_css_theme = basename($cm_css_light, '.css');
358 update_user_meta(get_current_user_id(),'_cf7sg_css_cm_theme', '');
359 }else{
360 if(empty($user_css_theme)){
361 $user_css_theme = 'paraiso-light';
362 update_user_meta(get_current_user_id(),'_cf7sg_css_cm_theme', 'light');
363 }else{
364 $user_css_theme = ('light'==$user_css_theme ? 'paraiso-light' : 'material-ocean');
365 }
366 }
367 //enqueue the cf7 scripts.
368 wpcf7_admin_enqueue_scripts( 'wpcf7' );
369 wp_enqueue_script('jquery-clibboard', $plugin_dir . 'assets/clipboard/clipboard.min.js', array('jquery'),$this->version,true);
370 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 );
371 wp_localize_script(
372 'cf7-grid-codemirror-js',
373 'cf7sgeditor',
374 array(
375 'mode' => 'shortcode',
376 'theme' => array(
377 'light'=>'paraiso-light',
378 'dark'=>'material-ocean',
379 'user'=>$user_theme
380 ),
381 'jstheme' => array(
382 'light'=>'paraiso-light',
383 'dark'=>'material-ocean',
384 'user'=>$user_js_theme
385 ),
386 'csstheme' => array(
387 'light'=>'paraiso-light',
388 'dark'=>'material-ocean',
389 'user'=>$user_css_theme
390 ),
391 'jserror'=>__('There is a <strong>Javascript error on the page</strong> which prevents the editor from loading properly.', 'cf7-grid-layout'),
392 'fixhtmlform'=>__('The editor failed to load. You may recover your form by copying it into a new form editor, do you wish to continue?', 'cf7-grid-layout'),
393 )
394 );
395 global $post;
396
397 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 );
398 wp_localize_script(
399 $this->plugin_name,
400 'cf7grid',
401 array(
402 'preHTML' => apply_filters('cf7sg_pre_cf7_field_html', '<div class="field"><label></label>', $post->post_name),
403 'postHTML' => apply_filters('cf7sg_post_cf7_field_html', '<p class="info-tip"></p></div>', $post->post_name),
404 'requiredHTML' => apply_filters('cf7sg_required_cf7_field_html', '<em>*</em>', $post->post_name),
405 'ui' => apply_filters('cf7sg_grid_ui', true, $post->post_name)
406 )
407 );
408 wp_enqueue_script( 'cf7sg-dynamic-tag-js', $plugin_dir . 'admin/js/cf7sg-dynamic-tag.js', array('jquery','wpcf7-admin-taggenerator' ), $this->version, true );
409 wp_enqueue_script( 'cf7-benchmark-tag-js', $plugin_dir . 'admin/js/cf7-benchmark-tag.js', array('jquery','wpcf7-admin-taggenerator' ), $this->version, true );
410 /** @since 3.2.0 */
411 wp_enqueue_script('cf7sg-mail-tag-js', $plugin_dir.'admin/js/mail-tag-helper.js', array('jquery','jquery-clibboard'));
412 wp_localize_script('cf7sg-mail-tag-js','mailTagHelper',
413 array(
414 'msg'=>__('Click to copy!','cf7-grid-layout'),
415 'filter'=> __('Filter mailTag %s', 'cf7-grid-layout')
416 )
417 );
418 /** @since 3.3.0 enqueue ui-grid helpers js */
419 wp_enqueue_script('ui-grid-helpers-js', $plugin_dir.'admin/js/ui-custom-helper.js', array('jquery'));
420 do_action('cf7sg_enqueue_admin_editor_scripts');
421 break;
422 case 'edit':
423 //wp_enqueue_script( $this->plugin_name.'-quick-edit', plugin_dir_url( __FILE__ ) . 'js/cf7-grid-layout-quick-edit.js', false, $this->version, true );
424 /** @since 4.4.0 Enabled user script enqueue */
425 do_action('cf7sg_enqueue_admin_table_scripts');
426 break;
427 }
428 }
429
430 /**
431 * Adds a new sub-menu to replace the 'Add New' CF7 menu
432 * Add a new sub-menu to the Contact main menu, as well as remove the current default
433 *
434 */
435 public function add_cf7_sub_menu(){
436
437 $hook = add_submenu_page(
438 'wpcf7',
439 __cf7sg( 'Edit Contact Form'),
440 __cf7sg( 'Add New'),
441 'wpcf7_edit_contact_forms',
442 'post-new.php?post_type=wpcf7_contact_form'
443 );
444 //initial cf7 object when creating new form
445 //add_action( 'load-' . $hook, array($this, 'setup_cf7_object'));
446 //remove_submenu_page( $menu_slug, $submenu_slug );
447 remove_submenu_page( 'wpcf7', 'wpcf7-new' );
448 remove_meta_box('slugdiv', $this->cf7_post_type(), 'normal');
449 }
450 /**
451 * Change the submenu order
452 * @since 1.0.0
453 */
454 public function change_cf7_submenu_order( $menu_ord ) {
455 global $submenu;
456 // Enable the next line to see all menu orders
457 if(!isset($submenu['wpcf7']) ){
458 return $menu_ord;
459 }
460 if( is_network_admin() ){
461 return $menu_ord;
462 }
463 $arr = array();
464 foreach($submenu['wpcf7'] as $menu){
465 switch($menu[2]){
466 case 'post-new.php?post_type=wpcf7_contact_form':
467 //push to the front
468 array_unshift($arr, $menu);
469 break;
470 default:
471 $arr[]=$menu;
472 break;
473 }
474 }
475 $submenu['wpcf7'] = $arr;
476 return $menu_ord;
477 }
478 /**
479 * Modify the regsitered cf7 post tppe
480 * THis function enables public capability and amind UI visibility for the cf7 post type. Hooked late on `register_post_type_args`
481 * @since 1.0.0
482 * @param Array $args array of post parameters being registered
483 * @param String $post_type post type breing resgistered
484 */
485
486 public function modify_cf7_post_type_args($args, $post_type){
487 if($post_type === $this->cf7_post_type() ) {
488 // debug_msg($args, 'pre-args');
489 // $system_dropdowns = get_option('_cf7sg_dynamic_dropdown_system_taxonomy',array());
490 /** @since 2.8.3 add wpcf7_type (registered in assets/cf7-table.php) taxonomy to cf7 post type */
491 $system_taxonomy = array('wpcf7_type');
492 // if(!empty($system_dropdowns)){
493 // foreach($system_dropdowns as $id=>$list){
494 // $system_taxonomy = array_merge($system_taxonomy, $list);
495 // }
496 // }
497 if(!empty($args['taxonomies'])){
498 $system_taxonomy = array_merge($args['taxonomies'], $system_taxonomy);
499 $system_taxonomy = array_unique($system_taxonomy);
500 }
501 $args['taxonomies'] = $system_taxonomy;
502
503 $args['public'] = false;
504 $args['show_ui']= true;
505 $args['show_in_menu']= 'wpcf7';
506 $args['supports'] = array('title', 'author');
507 $args['labels']['add_new_item'] = 'Add New Form';
508 $args['labels']['edit_item'] = 'Edit Form';
509 $args['labels']['new_item'] = 'New Form';
510 $args['labels']['view_item'] = 'View Form';
511 $args['labels']['view_items'] = 'View Forms';
512 $args['labels']['search_items'] = 'Search Forms';
513 $args['labels']['not_found'] = 'No forms found.';
514 $args['labels']['not_found_in_trash'] = 'No forms found in Trash.';
515 $args['labels']['parent_item_colon'] = '';
516 $args['labels']['attributes'] = 'Form Attributes';
517 $args['labels']['insert_into_item'] = 'Insert into form';
518 $args['labels']['uploaded_to_this_item'] = 'Uploaded to this form';
519 $args['labels']['filter_items_list'] = 'Filter forms list';
520 $args['labels']['items_list_navigation'] = 'Forms list navigation';
521 $args['labels']['items_list'] = 'Forms list';
522 /** @since 2.8.1 fix missing delete_posts notices*/
523 // $args['capabilities']['delete_posts']= 'wpcf7_delete_posts';
524 /** @since 3.0.0 enable better capability management */
525 // $args['capability_type'] = 'page';
526
527 $args['map_meta_cap']=true; //allow finer capability mapping.
528 $args['capabilities']['edit_post'] = 'wpcf7_edit_contact_form';
529 $args['capabilities']['read_post'] = 'wpcf7_read_contact_form';
530 $args['capabilities']['delete_post'] = 'wpcf7_delete_contact_form';
531 $args['capabilities']['edit_posts'] = 'wpcf7_edit_contact_forms';
532 $args['capabilities']['edit_others_posts']= 'wpcf7_edit_others_contact_forms';
533 $args['capabilities']['edit_published_posts']= 'wpcf7_edit_published_contact_forms';
534 $args['capabilities']['delete_posts']= 'wpcf7_delete_contact_forms';
535 $args['capabilities']['delete_published_posts']= 'wpcf7_delete_published_contact_forms';
536 $args['capabilities']['delete_others_posts']= 'wpcf7_delete_others_contact_forms';
537 $args['capabilities']['publish_posts']= 'wpcf7_publish_contact_forms';
538 $args['capabilities']['read_private_posts']= 'wpcf7_publish_contact_forms';
539
540 // debug_msg($args);
541 }
542 return $args;
543 }
544 /**
545 * Register custom taxonomy for dynamic dropdown lists
546 * Hooked on 'init'
547 * @since 1.0.0
548 *
549 **/
550 public function register_dynamic_dropdown_taxonomy(){
551 //register the dynamic dropdown taxonomies.
552 $dropdowns = get_option('_cf7sg_dynamic_dropdown_taxonomy',array());
553 //debug_msg($dropdowns);
554 $created = array();
555 foreach($dropdowns as $post_lists){
556 foreach($post_lists as $slug=>$taxonomy){
557 if(!isset($created[$slug])){
558 if(is_array($taxonomy)){
559 $this->register_dynamic_dropdown($taxonomy);
560 $created[$slug] = $slug;
561 }
562 }
563 }
564 }
565 /** @since 5.0 register dynamic_select list */
566 do_action('cf7sg_register_dynamic_lists');
567 // debug_msg('created dynamic select ');
568 }
569 /**
570 * function to regsiter dyanmic dropdown taxonomies
571 *
572 * @since 1.0.0
573 * @param Object $taxonomy_object std object with parameters $taxonomy_object->slug, $taxonomy_object->singular, $taxonomy_object->plural, $taxonomy_object->hierarchical .
574 **/
575 protected function register_dynamic_dropdown($taxonomy_array){
576 $slug = $taxonomy_array['slug'];
577 $name = $taxonomy_array['singular'];
578 $plural = $taxonomy_array['plural'];
579 $is_hierarchical = $taxonomy_array['hierarchical'];
580
581 $labels = array(
582 'name' => $plural,
583 'singular_name' => $name,
584 'menu_name' => $plural,
585 'all_items' => 'All '.$plural,
586 'parent_item' => 'Parent '.$name,
587 'parent_item_colon' => 'Parent '.$name.':',
588 'new_item_name' => 'New '.$name.' Name',
589 'add_new_item' => 'Add New '.$name,
590 'edit_item' => 'Edit '.$name,
591 'update_item' => 'Update '.$name,
592 'view_item' => 'View '.$name,
593 'separate_items_with_commas' => 'Separate '.$plural.' with commas',
594 'add_or_remove_items' => 'Add or remove '.$plural,
595 'choose_from_most_used' => 'Choose from the most used',
596 'popular_items' => 'Popular '.$plural,
597 'search_items' => 'Search '.$plural,
598 'not_found' => 'Not Found',
599 'no_terms' => 'No '.$plural,
600 'items_list' => $plural.' list',
601 'items_list_navigation' => $plural.' list navigation',
602 );
603 //labels can be modified post registration
604 $args = array(
605 'labels' => $labels,
606 'hierarchical' => $is_hierarchical,
607 'public' => false,
608 'show_ui' => true,
609 'show_admin_column' => false,
610 'show_in_nav_menus' => false,
611 'show_tagcloud' => false,
612 'show_in_quick_edit' => false,
613 'description' => 'Contact Form 7 dynamic dropdown taxonomy list',
614 );
615
616 register_taxonomy( $slug, $this->cf7_post_type(), $args );
617 }
618 /**
619 * CF7 Form saved from backend, check if dynamic-select are used
620 * Hooked on 'wpcf7_save_contact_form'
621 * @since 1.0.0
622 * @param WPCF7_Contact_Form $cf7_form cf7 form object
623 */
624 public function save_factory_metas($cf7_form){
625 $cf7_post_id = $cf7_form->id();
626 //get the tags used in this form
627 $tags = $cf7_form->scan_form_tags(); //get your form tags
628 $dynamic_lists = cf7sg_get_dynamic_lists();
629 $dynamic_tags = array_keys($dynamic_lists);
630 $dynamic_fields = array();
631 $form_classes = array();
632 foreach($tags as $tag){
633 if(in_array($tag['basetype'],$dynamic_tags)){
634 $dynamic_fields[$tag['basetype']]=$tag;
635 $form_classes = array_merge($form_classes, $dynamic_lists[$tag['basetype']]->get_form_classes($tag, $cf7_post_id));
636 }
637 }
638 if(!empty($form_classes)){
639 $classes = get_post_meta($cf7_post_id, '_cf7sg_script_classes', true);
640 if(empty($classes)){
641 $classes = array();
642 }
643 $form_classes = array_diff(array_unique($form_classes), $classes); //any new classes?
644 if(!empty($form_classes)) {
645 update_post_meta($cf7_post_id, '_cf7sg_script_classes', array_merge( $form_classes, $classes));
646 }
647 }
648 //check for newly created taxonomy lists.
649 if( isset($_POST['cf72post_dynamic_select_taxonomies']) ){
650 $created_taxonomies = array();
651 $taxonomies = json_decode(str_replace('\"','"',$_POST['cf72post_dynamic_select_taxonomies']), true);
652 if(empty($taxonomies)){
653 $taxonomies = array();
654 }
655 foreach($taxonomies as $taxonomy){
656 //sanitize fields before saving into the DB.
657 foreach($taxonomy as $key=>$value){
658 $key = sanitize_key($key);
659 $taxonomy[$key] = sanitize_text_field($value);
660 }
661 $created_taxonomies[$taxonomy['slug']] = $taxonomy;
662 }
663 //debug_msg($created_taxonomies);
664 $post_lists = $saved_lists = $system_list = array();
665
666 $dropdowns = get_option('_cf7sg_dynamic_dropdown_taxonomy',array());
667 foreach($dropdowns as $id => $lists){
668 $saved_lists = array_merge($saved_lists, $lists);
669 }
670 foreach($dynamic_fields as $tag){
671 if(isset($tag['values'])){
672 $slug='';
673 foreach($tag['values'] as $values){
674 if(0 == strpos($values, 'slug:') ){
675 $slug = str_replace('slug:', '', $values);
676 break;
677 }
678 }
679 if(empty($slug)) continue; //not a taxonomy list.
680 //is slug newly created?
681 if(isset($created_taxonomies[$slug])){
682 //store this taxonomy.
683 $post_lists[$slug] = $created_taxonomies[$slug];
684 }else if(isset($saved_lists[$slug])){
685 //retain previously saved list if we are stil using it.
686 $post_lists[$slug] = $saved_lists[$slug];
687 }else{ //system taxonomy.
688 $system_list[] = $slug;
689 }
690 //store the taxonomy slug in the cf7 metas.
691 //$post_lists[$slug] = null;
692 }
693 }
694 //list of taxonomy to register.
695 //unset the old value.
696 unset($dropdowns[$cf7_post_id]);
697 //unshift new value to top of array.
698 $dropdowns = array($cf7_post_id => $post_lists) + $dropdowns ;
699
700 update_option('_cf7sg_dynamic_dropdown_taxonomy', $dropdowns);
701 //list of system taxonomy to register
702 $system_dropdowns = get_option('_cf7sg_dynamic_dropdown_system_taxonomy',array());
703 $system_dropdowns[$cf7_post_id] = $system_list;
704 update_option('_cf7sg_dynamic_dropdown_system_taxonomy', $system_dropdowns);
705 }
706 }
707 /**
708 * Print hiddend field on cf7 post submit box
709 * Hooked on 'wpcf7_admin_misc_pub_section'
710 * @since 1.0.0
711 * @param string $post_id cf7 form post id .
712 **/
713 public function dynamic_select_choices($post_id){
714 echo '<input id="cf72post-dynamic-select" type="hidden" name="cf72post_dynamic_select_taxonomies" />';
715 }
716
717 /**
718 * Hide the cf7 form editor page author metabox by default.
719 * hooked to 'hidden_meta_boxes'.
720 *@since 4.6.0
721 *@param Array $hidden array of hidden metabox ids.
722 *@param WP_Screen $screen current amdin page screen object.
723 *@param Boolean $use_defaults wether default settings used.
724 *@return Array array of hidden metabox ids.
725 */
726 public function hide_author_metabox($hidden, $screen, $use_defaults){
727 if( 'wpcf7_contact_form' != $screen->id ) return $hidden;
728
729 if(in_array('authordiv', $hidden)) return $hidden;
730
731 if($use_defaults) $hidden[] = 'authordiv';
732 else {
733 $gs = get_option(self::CF7SG_OPTION, array());
734
735 if(!isset($gs['hide_author'])){
736 $gs['hide_author']=true; //flag it.
737 $uo = get_user_option('metaboxhidden_wpcf7_contact_form');
738 if(false === $uo) $uo = array();
739 $uo = array_unique( array_merge( $uo + array( 'authordiv' ) ) );
740 $hidden = array_unique( array_merge( $hidden + array( 'authordiv' ) ) );
741 update_option(self::CF7SG_OPTION,$gs);
742 update_user_option( get_current_user_id(), 'metaboxhidden_wpcf7_contact_form', $uo);
743 }
744 }
745 return $hidden;
746 }
747 /**
748 * Function to add the metabox to the cf7 post edit screen
749 * This adds the main editor, hooked on 'add_meta_boxes'
750 * @since 1.0.0
751 */
752 public function edit_page_metabox() {
753 //add_meta_box( string $id, string $title, callable $callback, string $screen, string $context, string $priority, array $callback_args)
754 if( post_type_exists( $this->cf7_post_type() ) ) {
755 add_meta_box( 'meta-box-main-cf7-editor',
756 __cf7sg( 'Edit Contact Form' ),
757 array($this , 'main_editor_metabox_display'),
758 $this->cf7_post_type(),
759 'normal',
760 'high'
761 );
762 add_meta_box( 'meta-box-cf7-info',
763 __cf7sg( 'Information' ),
764 array($this , 'info_metabox_display'),
765 $this->cf7_post_type(),
766 'side',
767 'high'
768 );
769 /** @since 1.1.0 add helper metabox */
770 add_meta_box( 'meta-box-cf7sg-helper',
771 __( 'Actions & Filters', 'cf7-grid-layout' ),
772 array($this , 'helper_metabox_display'),
773 $this->cf7_post_type(),
774 'side',
775 'default'
776 );
777 /** @since 4.6.0 enable page redirect */
778 add_meta_box(
779 'cf7sg-redirect', // Unique ID
780 'Redirect on Submission', // Box title
781 array($this, 'display_rediect_metabox'), // Content callback, must be of type callable
782 $this->cf7_post_type() // Post type
783 );
784 }
785 }
786 /**
787 * Callback function to disolay the main editor meta box
788 * @since 1.0.0
789 */
790 public function main_editor_metabox_display($post){
791 if('auto-draft' !== $post->post_status){
792 wpcf7_contact_form($post); //set the post
793 }
794 $post_id = $post->ID;
795 $cf7_form = wpcf7_get_current_contact_form();
796 if ( ! $cf7_form ) {
797 $args = apply_filters('cf7sg_new_cf7_form_template_arguments', array());
798 $cf7_form = WPCF7_ContactForm::get_template($args);
799 }
800 require_once WPCF7_PLUGIN_DIR . '/admin/includes/editor.php';
801 require_once plugin_dir_path( __FILE__ ) . 'partials/cf7-admin-editor-display.php';
802 }
803
804 /**
805 * Re-introduces the wordpress 'wpcf7_admin_misc_pub_section' for plugins to add their fields for submission
806 * Hooked to 'post_submitbox_misc_actions' which fires after post dat/time parameters are printed
807 * @since 1.0.0
808 * @param WP_Post $post post object being edited/created.
809 **/
810 public function cf7_post_submit_action($post){
811 if($this->cf7_post_type() == $post->post_type){
812 do_action( 'wpcf7_admin_misc_pub_section', $post->ID );
813 }
814 }
815 /**
816 * Callback function to disolay the main editor meta box
817 * @since 1.0.0
818 */
819 public function info_metabox_display($post){
820 require_once plugin_dir_path( __FILE__ ) . 'partials/cf7-info-metabox-display.php';
821 }
822 /**
823 * Callback function to disolay the helper meta box
824 * @since 1.0.0
825 */
826 public function helper_metabox_display($post){
827 require_once plugin_dir_path( __FILE__ ) . '/partials/cf7-helper-metabox-display.php';
828 }
829 /**
830 * Display the editor panels (wpcf7 / codemirror / grid)
831 *
832 * @since 1.0.0
833 * @param string $p1 .
834 * @return string $p2 .
835 **/
836 public function grid_editor_panel($form_post){
837 require_once plugin_dir_path( __FILE__ ) . '/partials/cf7-grid-layout-admin-display.php';
838 }
839 /**
840 *
841 *
842 *@since 4.6.0
843 *@param string $param text_description
844 *@return string text_description
845 */
846 public function display_rediect_metabox($post){
847 require_once plugin_dir_path( __FILE__ ) . '/partials/cf7-redirect-metabox-display.php';
848 }
849 /**
850 * Clean up post meta, delete sgcf7 corresponding view post.
851 * Hooked to action 'before_delete_post'.
852 *@since 4.3.0
853 *@param int $post_id post ID.
854 *@param WP_Post post object.
855 */
856 public function delete_post($post_id){
857 if(get_post_type($post_id) != WPCF7_ContactForm::post_type) return;
858 //remove viwing post.
859 $preview_id = get_post_meta($post_id, '_cf7sg_form_page',true);
860 if(!empty($preview_id)) wp_delete_post($preview_id);
861 }
862 /**
863 * Save cf7 post using ajax
864 * Hooked to 'save_post_wpcf7_contact_form'
865 * @since 1.0.0
866 **/
867 public function save_post($post_id, $post, $update){
868 //make sure this is the edit page and not a quick-edit.
869 if( !isset($_POST['_wpcf7nonce']) ){
870 return;
871 }
872 //validate the nonce.
873 check_admin_referer( 'wpcf7-save-contact-form_' . $post_id, '_wpcf7nonce');
874
875 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
876 return;
877 }
878
879 switch($post->post_status){
880 case 'auto-draft':
881 case 'trash':
882 return;
883 default:
884 break;
885 }
886
887 // debug_msg($_POST, 'submitted ');
888 $args = $_REQUEST;
889 $args['id'] = $post_id;
890
891 $args['title'] = isset( $_POST['post_title'] ) ? sanitize_text_field($_POST['post_title'], 'Contact Form', 'save') : null;
892 $args['locale'] = isset( $_POST['wpcf7-locale'] ) ? sanitize_text_field($_POST['wpcf7-locale']) : null;
893 $args['form'] = '';
894 $allowed_tags = wp_kses_allowed_html( 'post' ); //filtered in function below.
895 /** @since 4.8.1 alllow custom input html*/
896 $allowed_tags['input']=array( //add additional input html elements
897 'type'=>1,'name'=>1,'placeholder'=>1,'value'=>1,'maxlength'=>1,'minlength'=>1, //input fields.
898 'spellcheck'=>1,'size'=>1,'readonly'=>1,'pattern'=>1,'list'=>1, //input fields.
899 'class'=>1,
900 'id'=>1,
901 'data-*'=>1,
902 );
903 $allowed_tags['script']=array('type'=>1);
904 $cf7_key = $post->post_name;
905 $allowed_tags = apply_filters('cf7sg_kses_allowed_html',$allowed_tags, $cf7_key);
906 if(isset( $_POST['wpcf7-form'] )){
907 $args['form'] = wp_kses($_POST['wpcf7-form'], $allowed_tags);
908 }
909 $args['mail'] = isset( $_POST['wpcf7-mail'] ) ? wpcf7_sanitize_mail( $_POST['wpcf7-mail'] ): array();
910 $args['mail_2'] = isset( $_POST['wpcf7-mail-2'] ) ? wpcf7_sanitize_mail( $_POST['wpcf7-mail-2'] ): array();
911 $args['messages'] = isset( $_POST['wpcf7-messages'] ) ? $_POST['wpcf7-messages'] : array();
912 foreach($args['messages'] as $key=>$value){
913 $args['messages'][$key] = sanitize_text_field($value);
914 }
915 $args['additional_settings'] = isset( $_POST['wpcf7-additional-settings'] ) ? sanitize_textarea_field($_POST['wpcf7-additional-settings']) : '';
916
917 //need to unhook this function so as not to loop infinitely
918 //debug_msg($args, 'saving cf7 posts');
919 remove_action('save_post_wpcf7_contact_form', array($this, 'save_post'), 10,3);
920 // debug_msg($args['form'],'form ');
921 $contact_form = wpcf7_save_contact_form( $args );
922 add_action('save_post_wpcf7_contact_form', array($this, 'save_post'), 10,3);
923 //flag as a grid form
924 //update_post_meta($post_id, 'cf7_grid_form', true); removed since v2.3 as not used.
925 //save sub-forms if any
926 $sub_forms = json_decode(stripslashes($_POST['cf7sg-embeded-forms']));
927 if(empty($sub_forms)) $sub_forms = array();
928 if(!is_array($sub_forms)) $sub_forms = array($sub_forms);
929 $sanitised_sub_forms = array();
930 foreach($sub_forms as $field){
931 $sanitised_sub_forms[] = sanitize_text_field($field);
932 }
933 update_post_meta($post_id, '_cf7sg_sub_forms', $sanitised_sub_forms);
934 //save form fields which are in tabs or tables.
935 $tt_fields = json_decode(stripslashes($_POST['cf7sg-table-fields']));
936 if(empty($tt_fields)) $tt_fields = array();
937 if(!is_array($tt_fields)) $tt_fields = array($tt_fields);
938 $sanitised_table_fields = array();
939 foreach($tt_fields as $table_fields){
940 if(is_object($table_fields)){ /** @since 2.4.2 track fields in each tables. */
941 $table_fields = get_object_vars($table_fields); /*convert to array*/
942 $table = array_keys($table_fields);
943 $table = sanitize_text_field($table[0]);
944 $table_fields = $table_fields[$table];
945 $sanitised_table_fields[$table]=array();
946 foreach($table_fields as $field) $sanitised_table_fields[$table][] = sanitize_text_field($field);
947 }else $sanitised_table_fields[] = sanitize_text_field($table_fields);
948 }
949 update_post_meta($post_id, '_cf7sg_grid_table_names', $sanitised_table_fields);
950 //tabs
951 $tt_fields = json_decode(stripslashes($_POST['cf7sg-tabs-fields']));
952 if(empty($tt_fields)) $tt_fields = array();
953 if(!is_array($tt_fields)) $tt_fields = array($tt_fields);
954 $sanitised_tab_fields = array();
955 foreach($tt_fields as $tab_field){
956 if(is_object($tab_field)){ /** @since 2.4.2 track fields in each tabs. */
957 $tab_field = get_object_vars($tab_field); /*convert to array*/
958 $tab = array_keys($tab_field);
959 $tab = sanitize_text_field($tab[0]);
960 $tab_field = $tab_field[$tab];
961 $sanitised_tab_fields[$tab]=array();
962 foreach($tab_field as $field) $sanitised_tab_fields[$tab][] = sanitize_text_field($field);
963 }else $sanitised_tab_fields[] = sanitize_text_field($tab_field);
964 }
965 update_post_meta($post_id, '_cf7sg_grid_tabs_names', $sanitised_tab_fields);
966 /** track toggled fields.
967 * @since 2.5*/
968 $tt_fields = json_decode(stripslashes($_POST['cf7sg-toggle-fields']));
969 if(empty($tt_fields)) $tt_fields = array();
970 if(!is_array($tt_fields)) $tt_fields = array($tt_fields);
971 $sanitised_toggled_fields = array();
972 foreach($tt_fields as $tgg_field){
973 if(is_object($tgg_field)){
974 $tgg_field = get_object_vars($tgg_field); /*convert to array*/
975 $tgg = array_keys($tgg_field);
976 $tgg = sanitize_text_field($tgg[0]);
977 $tgg_field = $tgg_field[$tgg];
978 $sanitised_toggled_fields[$tgg]=array();
979 foreach($tgg_field as $field) $sanitised_toggled_fields[$tgg][] = sanitize_text_field($field);
980 }else $sanitised_toggled_fields[] = sanitize_text_field($tgg_field);
981 }
982 update_post_meta($post_id, '_cf7sg_grid_toggled_names', $sanitised_toggled_fields);
983 /** @since 4.0.0 track tabbed toggles */
984 $ttgls = json_decode(stripslashes($_POST['cf7sg-tabbed-toggles']));
985 if(empty($ttgls)) $ttgls = array();
986 if(!is_array($ttgls)) $ttgls = array($ttgls);
987 update_post_meta($post_id, '_cf7sg_grid_tabbed_toggles', $ttgls);
988 /** @since 4.0.0 track grouped toggles */
989 $ttgls = json_decode(stripslashes($_POST['cf7sg-grouped-toggles']));
990 if(empty($ttgls)) $ttgls = array();
991 if(is_object($ttgls)) $ttgls = get_object_vars($ttgls); //convert to array.
992 else $ttgls = array();
993 update_post_meta($post_id, '_cf7sg_grid_grouped_toggles', $ttgls);
994 //flag tab & tables for more efficient front-end display.
995 $has_tabs = ( 'true' === $_POST['cf7sg-has-tabs']) ? true : false;
996 update_post_meta($post_id, '_cf7sg_has_tabs', $has_tabs);
997 $has_tables = ( 'true' === $_POST['cf7sg-has-tables']) ? true : false;
998 update_post_meta($post_id, '_cf7sg_has_tables', $has_tables);
999 $has_toggles = ( 'true' === $_POST['cf7sg-has-toggles']) ? true : false;
1000 update_post_meta($post_id, '_cf7sg_has_toggles', $has_toggles);
1001 /** @since 3.3.5 track toggles in tabs */
1002 // $toggle_in_tabs = json_decode(stripslashes($_POST['cf7sg-toggle-in-tabs']));
1003 // if(empty($toggle_in_tabs)) $toggle_in_tabs = array();
1004 // if(!is_array($toggle_in_tabs)) $toggle_in_tabs = array($toggle_in_tabs);
1005 // update_post_meta($post_id, '_cf7sg_grid_toggle_in_tabs', $toggle_in_tabs);
1006 /**
1007 * @since 1.2.3 disable cf7sg styling/js for non-cf7sg forms.
1008 */
1009 $is_cf7sg = ( 'true' === $_POST['is_cf7sg_form']) ? true : false;
1010 update_post_meta($post_id, '_cf7sg_managed_form', $is_cf7sg);
1011 update_post_meta($post_id, '_cf7sg_version', $this->version);
1012 /** @since 3.0.0 track script classes for loading for js/css in front-end */
1013 $classes = sanitize_text_field($_POST['cf7sg-script-classes']);
1014 $classes = explode(',', trim($classes, ','));
1015 // debug_msg($classes, 'script classes');
1016 update_post_meta($post_id, '_cf7sg_script_classes', $classes);
1017 /**
1018 *@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.
1019 */
1020 /** @since 4.0.0 save user theme pref */
1021 if(isset($_POST['cf7sg_codemirror_theme'])){
1022 update_user_meta(get_current_user_id(),'_cf7sg_cm_theme', $_POST['cf7sg_codemirror_theme']);
1023 }
1024 if(isset($_POST['cf7sg_js_codemirror_theme'])){
1025 update_user_meta(get_current_user_id(),'_cf7sg_js_cm_theme', $_POST['cf7sg_js_codemirror_theme']);
1026 }
1027 //save js file if used.
1028 $path = get_stylesheet_directory();
1029 if(!empty($_POST['cf7sg_js_file'])){
1030 //check if the file name is changed.
1031 if(!empty($_POST['cf7sg_prev_js_file']) && file_exists(ABSPATH. $_POST['cf7sg_prev_js_file'])){
1032 if( !unlink(ABSPATH.$_POST['cf7sg_prev_js_file']) ) debug_msg('CF7SG ADMIN: unable to delete file '.$_POST['cf7sg_prev_js_file']);
1033 }
1034 if( !is_dir("$path/js/") ) mkdir("$path/js/");
1035 file_put_contents( "$path/js/{$cf7_key}.js", stripslashes($_POST['cf7sg_js_file']) );
1036 }else if( isset($_POST['cf7sg_js_file']) && file_exists("$path/js/{$cf7_key}.js") ) { //delete file.
1037 if( !unlink("$path/js/{$cf7_key}.js") ) debug_msg("CF7SG ADMIN: unable to delete file $path/js/{$cf7_key}.js");
1038 }
1039 //save css file.
1040 if(!empty($_POST['cf7sg_css_file'])){
1041 //check if the file is changed.
1042 if(!empty($_POST['cf7sg_prev_css_file']) && file_exists(ABSPATH.$_POST['cf7sg_prev_css_file'])){
1043 if( !unlink(ABSPATH.$_POST['cf7sg_prev_css_file']) ) debug_msg('CF7SG ADMIN: unable to delete file '.$_POST['cf7sg_prev_css_file']);
1044 }
1045 if( !is_dir("$path/css/") ) mkdir("$path/css/");
1046 file_put_contents( $path."/css/{$cf7_key}.css", stripslashes($_POST['cf7sg_css_file']));
1047 }else if( isset($_POST['cf7sg_css_file']) && file_exists("$path/css/{$cf7_key}.css") ) { //delete file.
1048 if( !unlink("$path/css/{$cf7_key}.css") ) debug_msg("CF7SG ADMIN: unable to delete file $path/css/{$cf7_key}.css");
1049 }
1050 //jstags comments.
1051 if(empty($_POST['cf7sg_jstags_comments'])) update_post_meta($post->ID, '_cf7sg_disable_jstags_comments',1);
1052 else update_post_meta($post->ID, '_cf7sg_disable_jstags_comments',0);
1053 /** @since 4.3.0 create/update a preview form post for this form */
1054 $preview_id = get_post_meta($post->ID, '_cf7sg_form_page',true);
1055 $content = '[cf7form cf7key="'.$cf7_key.'"';
1056 $content .= isset($_POST['post_lang_choice']) ? ' lang="'.$_POST['post_lang_choice'].'"]':']';
1057 $prev_page = array(
1058 'post_title'=>sanitize_text_field($_POST['post_title']),
1059 'post_content'=>$content,
1060 'post_status'=>'draft',
1061 'post_type'=>'cf7sg_page',
1062 // 'post_name' => sanitize_title( $title ),
1063 );
1064 /** @since 4.6.0 redirect on submit */
1065 if(isset($_POST['cf7sg_page_redirect'])){
1066 $redirect_to = absint($_POST['cf7sg_page_redirect']);
1067 update_post_meta($post->ID, '_cf7sg_page_redirect',$redirect_to);
1068 if(isset($_POST['cache_cf7sg_submit']) and isset($_POST['cf7sg_cached_time']) and isset($_POST['cf7sg_cached_unit'])){
1069 $cache = floatval( $_POST['cf7sg_cached_time']);
1070 if($cache>0){
1071 $cache=array($cache, absint($_POST['cf7sg_cached_unit']));
1072 update_post_meta($post->ID, '_cf7sg_cache_redirect_data',$cache);
1073 }
1074 }else delete_post_meta($post->ID, '_cf7sg_cache_redirect_data');
1075 }
1076 if(empty($cf7_key)) $prev_page['post_content'] = '[contact-form-7 id="'.$post->ID.'"]';
1077 if( !empty($preview_id) ){
1078 $prev_page['ID'] = $preview_id;
1079 }
1080 $preview_id = wp_insert_post($prev_page, true);
1081 if(!is_wp_error($preview_id)){
1082 update_post_meta($post->ID, '_cf7sg_form_page',$preview_id);
1083 }
1084 /** @since 4.9.2 fire form saving action so as to prevent double save_post hook calls on other plugins */
1085 do_action('cf7sg_save_post',$post_id, $post, $update);
1086 }
1087 /**
1088 * Print default js template,
1089 * Hooked to 'cf7sg_default_custom_js_template'
1090 *@since 4.0.0
1091 *@param string $form_key form key.
1092 */
1093 public function print_default_js($form_key){
1094 $js_file = str_replace(ABSPATH, '', get_stylesheet_directory()."/js/{$form_key}.js");
1095 include_once 'partials/cf7-default-js.php';
1096 }
1097 /**
1098 *
1099 *
1100 *@since 2.3.0
1101 *@param string $new_form_id new form id to duplciate to.
1102 *@param string $form_id form id to duplciate.
1103 */
1104 public function duplicate_form_properties($new_form_id, $form_id){
1105 //these properties will be preceded with an '_' by the cf7 plugin before being duplicated.
1106 $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');
1107 $properties = apply_filters('cf7sg_duplicate_form_properties', $properties);
1108 foreach($properties as $field){
1109 $value = get_post_meta($form_id, $field, true);
1110 if(!empty($value)) update_post_meta($new_form_id,$field, $value);
1111 }
1112 }
1113 /**
1114 * Duplicate form.
1115 *
1116 *@since 2.3.0
1117 *@param string $param text_description
1118 *@return string text_description
1119 */
1120 public function duplicate_cf7_form(){
1121 global $pagenow;
1122 if($pagenow != 'post.php') return;
1123 if(isset($_GET['action']) && 'cf7copy' == $_GET['action']){
1124 $action = 'wpcf7-copy-contact-form_' . $_GET['post'];
1125 if( !wp_verify_nonce( $_GET['_wpnonce'], $action )){
1126 die( 'Security check error: Try to reload the page and try again.' );
1127 }
1128 if ( $form = wpcf7_contact_form( $_GET['post'] ) ) {
1129 $new_form = $form->copy();
1130 $new_form->save();
1131 $this->duplicate_form_properties($new_form->id(), $_GET['post']);
1132 wp_safe_redirect( admin_url( '/post.php?post='.$new_form->id().'&action=edit' ));
1133 exit;
1134 }
1135 }
1136 }
1137 /**
1138 * Filtered allowed html tags & attributes, add data-button to div tags to ensure forms are saved properly.
1139 * Hooked on 'wp_kses_allowed_html'
1140 *@since 1.0.0
1141 *@param array $allowed array of allowed tags and attributes.
1142 *@param mixed $context context in which content is filtered.
1143 *@return array allowed tags and ttribtues.
1144 */
1145 public function custom_kses_rules($allowed, $context){
1146 if(is_array($context)){
1147 return $allowed;
1148 }
1149 if('post'===$context){
1150 $allowed['div']['data-button'] = true; //table buttons.
1151 $allowed['div']['data-form'] = true; //sub-forms.
1152 $allowed['div']['data-on'] = true; //toggles.
1153 $allowed['div']['data-off'] = true; //toggles.
1154 $allowed['div']['data-open'] = true; //accordion.
1155 $allowed['div']['data-group'] = true; //accordion.
1156 $allowed['div']['data-config-field'] = true; //cf7 plugin.
1157 }
1158 return $allowed;
1159 }
1160 /**
1161 * Filters the default form loaded when a new CF7 form is created
1162 * Hooked on 'wpcf7_default_template'
1163 * @since 1.0
1164 * @param string $template the html string for the form tempalte
1165 * @param string $prop the template property required.
1166 */
1167 public function default_cf7_form($template, $prop){
1168 if($prop !== 'form') return $template;
1169 include( plugin_dir_path( __FILE__ ) . '/partials/cf7-default-form.php');
1170 return $template;
1171 }
1172 /**
1173 * Ajax function to return the content of a cf7 form
1174 * Hooked on 'wp_ajax_get_cf7_content'
1175 * @since 1.0.0
1176 * @return String cf7 form content.
1177 **/
1178 public function get_cf7_content(){
1179 if( !isset($_POST['nonce']) ){
1180 echo 'error, nonce failed, try to reload the page.';
1181 wp_die();
1182 }
1183 if(!wp_verify_nonce($_POST['nonce'], 'wpcf7-save-contact-form_' . $_POST['id'], '_wpcf7nonce')){
1184 echo 'error, nonce failed, try to reload the page.';
1185 wp_die();
1186 }
1187 $cf7_key = sanitize_text_field($_POST['cf7_key']);
1188 $sub_forms = get_posts(array(
1189 'post_type' => $this->cf7_post_type(),
1190 'post_name__in' => array($cf7_key)
1191 ));
1192 $form='';
1193 if(!empty($sub_forms)){
1194 $form = get_post_meta($sub_forms[0]->ID, '_form', true);
1195 echo $form;
1196 }else{
1197 echo 'unable to find form '.$cf7_key;
1198 }
1199 wp_die();
1200 }
1201 /**
1202 * Adds a [taxonomy] tag to cf7 forms
1203 * Hooked on 'wpcf7_admin_init'
1204 * @since 1.0.0
1205 **/
1206 public function cf7_shortcode_tags(){
1207 if ( class_exists( 'WPCF7_TagGenerator' ) ) {
1208 /** @since 4.10.0 enable extension of dynamic lists */
1209 $tag_generator = WPCF7_TagGenerator::get_instance();
1210 $tag_generator->add(
1211 'benchmark', //tag id
1212 __( 'benchmark', 'cf7_2_post' ), //tag button label
1213 array($this,'benchmark_tag_generator') //callback
1214 );
1215 }
1216 /** @since 4.10.0 abstract out dynamic lists */
1217 $lists = cf7sg_get_dynamic_lists();
1218 foreach($lists as $l) $l->register_cf7_tag();
1219 }
1220
1221 /**
1222 * Benchmark input screen displayt.
1223 *
1224 * This function is called by cf7 plugin, and is registered with a hooked function above
1225 *
1226 * @since 1.0.0
1227 * @param WPCF7_ContactForm $contact_form the cf7 form object
1228 * @param array $args arguments for this form.
1229 */
1230 function benchmark_tag_generator( $contact_form, $args = '' ) {
1231 $args = wp_parse_args( $args, array() );
1232 include( plugin_dir_path( __FILE__ ) . '/partials/cf7-benchmark-tag.php');
1233 }
1234
1235 /**
1236 * Deactivate this plugin if CF7 plugin is deactivated.
1237 * Hooks on action 'admin_init'
1238 * @since 2.1
1239 */
1240 //public function deactivate_cf7_polylang( $plugin, $network_deactivating ) {
1241 public function check_plugin_dependency() {
1242 //if either the polylang for the cf7 plugin is not active anymore, deactive this extension
1243 if( !is_plugin_active("contact-form-7/wp-contact-form-7.php") ){
1244 deactivate_plugins( "cf7-grid-layout/cf7-grid-layout.php" );
1245 debug_msg("Deactivating Smart Grid");
1246
1247 $button = '<a href="'.network_admin_url('plugins.php').'">Return to Plugins</a></a>';
1248 wp_die( '<p><strong>CF7 smart Grid-layout Extension</strong> requires <strong>Contact Form 7</strong> plugin, and has therefore been deactivated!</p>'.$button );
1249
1250 return false;
1251 }
1252 return true;
1253 }
1254 /**
1255 * Add disabled button message on hover to cf7 messages.
1256 * Hooked to 'wpcf7_messages', see file contact-form-7/includes/contact-form-template.php fn messages().
1257 *@since 2.6.0
1258 *@param array $messages array of messages to filter.
1259 *@return array array of cf7 messages.
1260 */
1261 public function disabled_message($messages){
1262 $messages['submit_disabled'] = array(
1263 'description'
1264 => __( "Hover message for disabled submit/save button", 'cf7-grid-layout' ),
1265 'default'
1266 => __( "Disabled! To enable, check the acceptance field.", 'cf7-grid-layout' ),
1267 );
1268 /** @sicne 2.8.0 */
1269 $messages['max_table_rows'] = array(
1270 'description'
1271 => __( "Message displayed when max tables rows reached.", 'cf7-grid-layout' ),
1272 'default'
1273 => __( "You have reached the maximum number of rows.", 'cf7-grid-layout' ),
1274 );
1275 return $messages;
1276 }
1277 /**
1278 * Adds pretty pointers to help users.
1279 * Hooked on 'admin_enqueue_scripts'
1280 *@since 2.6.0
1281 *@param string $hook_suffix current page.
1282 */
1283 public function pretty_admin_pointers($hook_suffix){
1284 $screen = get_current_screen();
1285 if (!isset($screen) || $this->cf7_post_type() != $screen->post_type || in_array( $hook_suffix, array('post.php', 'post-new.php'), true)){
1286 return;
1287 }
1288 $enqueue_pointer = false;
1289 // Get array list of dismissed pointers for current user and convert it to array
1290 $user_id = get_current_user_id();
1291 $dismissed_pointers = explode( ',', get_user_meta( $user_id, 'dismissed_wp_pointers', true ) );
1292 $pointers = array();
1293 /**
1294 * Filter to add custom pointers for user iterface.
1295 * @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).
1296 * @return array an array of pointers, which will be checked agains the current user.
1297 */
1298 $filter_pointers = apply_filters('cf7sg_plugin_pointers-'.$screen->id, array());
1299 foreach($filter_pointers as $id=>$pointer){
1300 // Check if our pointer is not among dismissed ones
1301 if( !in_array( $id, $dismissed_pointers ) ) {
1302 $enqueue_pointer = true;
1303 $pointers[$id] = array($pointer[0], $pointer[1], $pointer[2], $pointer[3]);
1304 }
1305 }
1306 // Enqueue pointer CSS and JS files, if needed
1307 if( $enqueue_pointer ) {
1308 wp_enqueue_style( 'wp-pointer' );
1309 wp_enqueue_script( 'wp-pointer' );
1310 wp_enqueue_script('cf7sg-pointer-js', plugin_dir_url( __DIR__ ).'admin/js/cf7sg-pointers.js', array('jquery'), $this->version, true);
1311 wp_localize_script('cf7sg-pointer-js', 'cf7sg_pointers',
1312 array('pointers'=>$pointers, 'next'=>__('Next', 'cf7-grid-layout'))
1313 );
1314 // Add footer scripts using callback function
1315 //add_action( 'admin_print_footer_scripts', array($this, 'pretty_pointer_script') );
1316 }
1317 }
1318 /**
1319 * CF7 Table pointer notices.
1320 * Hooked on 'cf7sg_plugin_pointers-edit-wpcf7_contact_form'
1321 *@since 2.6.0
1322 *@param array $pointers array of pointers to suplement
1323 *@return array array of pointers element-id=>hmtl to display key value pairs.
1324 */
1325 public function edit_pointers($pointers){
1326 ob_start();
1327 include_once 'partials/pointers/cf7sg-pointer-update-forms.php';
1328 $content =ob_get_contents();
1329 $pointers['update_forms_pointer'] = array($content, 'left', 'center','');
1330 /* tutorials */
1331 ob_clean();
1332 include_once 'partials/pointers/cf7sg-pointer-tutorials.php';
1333 $content =ob_get_contents();
1334 $pointers['tutorials_pointer'] = array($content, 'left', 'center','a[href="admin.php?page=cf7sg_help"]');
1335 /* new tutorials */
1336 ob_clean();
1337 include_once 'partials/pointers/cf7sg-pointer-tutorial-advance.php';
1338 $content =ob_get_contents();
1339 $pointers['new_tutorials_pointer'] = array($content, 'left', 'center','a[href="admin.php?page=cf7sg_help"]');
1340 /* shortcodes */
1341 ob_clean();
1342 include_once 'partials/pointers/cf7sg-pointer-shortcodes.php';
1343 $content = ob_get_clean();
1344 $pointers['cf7sg_shortcodes'] = array($content, 'top', 'top','');
1345 return $pointers;
1346 }
1347 /**
1348 * CF7 Edit pointer notices.
1349 * Hooked on 'cf7sg_plugin_pointers-wpcf7_contact_form'
1350 *@since 2.6.0
1351 *@param array $pointers array of pointers to suplement
1352 *@return array array of pointers element-id=>hmtl to display key value pairs.
1353 */
1354 public function post_pointers($pointers){
1355 ob_start();
1356 include_once 'partials/pointers/cf7sg-pointer-editor-full-screen.php';
1357 $content =ob_get_contents();
1358 //content / arrow [top,bottom,left,right] / box align [top,bottom,center] / css selector.
1359 $pointers['full_screen'] = array($content, 'left', 'center','#full-screen-cf7');
1360 /* shortcodes */
1361 ob_clean();
1362 include_once 'partials/pointers/cf7sg-pointer-editor-tabs.php';
1363 $content =ob_get_contents();
1364 $pointers['editor_tabs'] = array($content, 'right', 'center','#form-editor-tabs>ul>li.ui-tabs-active');
1365 /* shortcodes */
1366 ob_clean();
1367 include_once 'partials/pointers/cf7sg-pointer-editor-rows-control.php';
1368 $content = ob_get_contents();
1369 if(!empty($content)){
1370 $pointers['row_controls'] = array($content, 'right', 'center','#grid-form>.container>.row>.row-controls');
1371 ob_clean();
1372 }
1373 /* preview form */
1374 ob_clean();
1375 include_once 'partials/pointers/cf7sg-pointer-editor-preview-form.php';
1376 $content = ob_get_contents();
1377 if(!empty($content)){
1378 $pointers['preview_form'] = array($content, 'right', 'center','#preview-form-link');
1379 ob_clean();
1380 }
1381 include_once 'partials/pointers/cf7sg-pointer-editor-column-control.php';
1382 $content = ob_get_contents();
1383 if(!empty($content)){
1384 $pointers['column_controls'] = array($content, 'left', 'center','#grid-form > .container:first-child > .row > .columns:first-child > .grid-column > span.icon-code');
1385 ob_clean();
1386 }
1387 include_once 'partials/pointers/cf7sg-pointer-tag-dynamic-dropdown.php';
1388 $content = ob_get_contents();
1389 $pointers['dynamic_dropdown'] = array($content, 'left', 'center','#top-tags>#tag-generator-list > a[title*="dynamic-dropdown"]');
1390 ob_clean();
1391 /* #optional-editors */
1392 include_once 'partials/pointers/cf7sg-pointers-editor-optional-js-css.php';
1393 $content = ob_get_contents();
1394 $pointers['js_css_editors'] = array($content, 'left', 'center','#optional-editors');
1395 ob_clean();
1396 include_once 'partials/pointers/cf7sg-pointer-tag-benchmark.php';
1397 $content = ob_get_clean();
1398 $pointers['benchmark'] = array($content, 'left', 'center','#top-tags>#tag-generator-list > a[title*="benchmark"]');
1399 return $pointers;
1400 }
1401 /**
1402 * Rest meta_caps for better fine tuning of user caps.
1403 * hooked to 'wpcf7_map_meta_cap'.
1404 *@since 3.0.0
1405 *@param array $caps key->value pairs of capabilities.
1406 *@return array key->value pairs of capabilities.
1407 */
1408 public function reset_meta_cap($caps){
1409 return array(
1410 'wpcf7_read_contact_form'=>'read_post',
1411 'wpcf7_edit_contact_form' =>'edit_post',
1412 'wpcf7_edit_contact_forms' => 'edit_posts',
1413 'wpcf7_edit_others_contact_forms'=>'edit_others_posts',
1414 'wpcf7_edit_published_contact_forms'=>'edit_published_posts',
1415 'wpcf7_delete_contact_form'=>'delete_post',
1416 'wpcf7_delete_contact_forms'=>'delete_posts',
1417 'wpcf7_delete_published_contact_forms'=>'delete_published_posts',
1418 'wpcf7_delete_others_contact_forms'=>'delete_others_posts',
1419 'wpcf7_publish_contact_forms'=>'publish_posts',
1420 'wpcf7_publish_contact_forms'=>'read_private_posts',
1421 'wpcf7_submit'=>'read', /** @since 3.2.1 to fix subscribers_only. */
1422 );
1423 }
1424 /**
1425 * CF7 plugin by default sets form post status to 'publish' regardless of user capability.
1426 * This functions rectifies this. Hooked to 'wp_insert_post_data'.
1427 *@since 3.0.0
1428 *@param array $data sanitised post data.
1429 *@return array post data.
1430 */
1431 public function pending_for_review($data, $post){
1432 if($this->cf7_post_type() != $data['post_type']) return $data;
1433
1434 $post_type_object = get_post_type_object( $this->cf7_post_type() );
1435 //check if user can publish.
1436 if(isset($post['ID']) && !current_user_can($post_type_object->cap->publish_posts, $post['ID']) && $data['post_status']=='publish'){
1437 $data['post_status']='pending';
1438 }
1439 // debug_msg('post status '.$data['post_status']);
1440 return $data;
1441 }
1442 /**
1443 * Enable all cf7 capabilites for editors.
1444 * Hooked to action 'admin_init'.
1445 *@since 3.0.0
1446 */
1447 public function enable_cf7_editor_role(){
1448 global $wp_roles;
1449 if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles();
1450 $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');
1451 $fe = $wp_roles->get_role('editor');
1452 if(!empty($fe)){ /** @since 3.0.3 in case editor role is deleted */
1453 foreach($caps as $cap) $fe->add_cap($cap);
1454 }
1455 $caps[]='wpcf7_manage_integration';
1456 $ad = $wp_roles->get_role('administrator');
1457 if(!empty($ad)){ /** @since 3.0.3 in case admin role is deleted */
1458 foreach($caps as $cap) $ad->add_cap($cap);
1459 }
1460
1461 /** @since 3.1.3 redirect cf7 plugin pages*/
1462 global $pagenow;
1463 if ( $pagenow == 'admin.php' && isset( $_GET['page'] ) ) {
1464 $url = '';
1465 switch($_GET['page']){
1466 case 'wpcf7-new':
1467 $url = "post-new.php?post_type=wpcf7_contact_form" ;
1468 break;
1469 case 'wpcf7':
1470 if(isset($_GET['post'])) $url = "post.php?post={$_GET['post']}&action=edit&classic-editor";
1471 else $url = "edit.php?post_type=wpcf7_contact_form";
1472 break;
1473 }
1474 if(!empty($url)){
1475 wp_redirect( admin_url( $url ));
1476 exit;
1477 }
1478 }
1479 }
1480 /**
1481 * Add grid helper hooks for individual tags.
1482 * Hooked to action 'cf7sg_ui_grid_helper_hooks'.
1483 *@since 3.3.0
1484 */
1485 public function print_helper_hooks(){
1486 require_once plugin_dir_path( __FILE__ ) .'partials/helpers/cf7sg-form-fields.php';
1487 }
1488 /**
1489 * Setup mailtags or toggles.
1490 *
1491 *@since 4.0.0
1492 *@param string $param text_description
1493 *@return string text_description
1494 */
1495 public function setup_cf7_mailtags($mailtags = array()){
1496 // return $mailtags;
1497 $contact_form = WPCF7_ContactForm::get_current();
1498 $fields = get_post_meta($contact_form->id(), '_cf7sg_grid_toggled_names', true);
1499 //update_post_meta($post_id, '_cf7sg_grid_tabbed_toggles', $ttgls);
1500 if(!empty($fields)){
1501 $toggles = array_keys($fields);
1502 $groups = get_post_meta($contact_form->id(), '_cf7sg_grid_grouped_toggles', true);
1503 if(!empty($groups)){
1504 foreach($groups as $group=>$grp_toggles){
1505 $toggles = array_diff($toggles, $grp_toggles);
1506 array_push($toggles, $group);
1507 }
1508 }
1509 foreach($toggles as $tgl) $mailtags[]= 'cf7sg-toggle-'.$tgl;
1510 }
1511 //add a general form tag.
1512 $mailtags[]= 'cf7sg-form-'.get_cf7form_key($contact_form->id());
1513 return $mailtags;
1514 }
1515 /**
1516 * Funciton to to initialise admin notices.
1517 * hooked on 'admin_init'
1518 *@since 4.1.0
1519 */
1520 public function init_notices(){
1521 $grid_settings = get_option( self::CF7SG_OPTION, array());
1522 //if plugin settings exists and this is not an update, then no need to run.
1523 if( !empty($grid_settings) and !isset($grid_settings['update']) ) return;
1524
1525 $warning = false;
1526 $notices = array();
1527
1528 if(isset($grid_settings['update']) and CF7SG_VERSION_FORM_UPDATE==CF7_GRID_VERSION){
1529 $warning = true;
1530 }else if(isset($grid_settings['fv']) ) {
1531 if(version_compare($grid_settings['fv'], CF7SG_VERSION_FORM_UPDATE, '<') ) $warning = true;
1532 }else{ //check the forms directly
1533 global $wpdb;
1534 $post_type = $this->cf7_post_type();
1535 $result = $wpdb->get_col("SELECT pm.meta_value FROM {$wpdb->postmeta} as pm
1536 INNER JOIN {$wpdb->posts} as p on p.ID = pm.post_id
1537 WHERE p.post_type = '{$post_type}'
1538 AND pm.meta_key = '_cf7sg_version'
1539 ORDER BY pm.meta_key
1540 ");
1541 if(!empty($result) and version_compare($result[0], CF7SG_VERSION_FORM_UPDATE, '<') ) $warning = true;
1542 }
1543
1544 if($warning){
1545 $link = admin_url('edit.php?post_type=wpcf7_contact_form');
1546 /* translators: %s is the url to the forms admin table */
1547 $msg = __('You need to <strong>update</strong> your <a href="%s">forms</a>','cf7-grid-layout');
1548 $notices['cf7sg_notice-'.CF7_GRID_VERSION] = array(
1549 'type'=>'notice-warning', //[notice-update|notice-error]
1550 'msg'=>sprintf($msg , $link),
1551 'pages'=>array('plugins.php','edit.php')
1552 );
1553 }
1554 /** @since 4.2.0 new sliders tutorial */
1555 if( isset($grid_settings['update']) ){
1556 $notices['cf7sg_sliders_tutorial'] = array(
1557 'type'=>'notice-update', //[notice-update|notice-error]
1558 /* translators: %s is the link to the tuorial page*/
1559 'msg'=>sprintf( __('There is a new tutorial for <a href="%s">multistep slider forms</a>','cf7-grid-layout'), admin_url('admin.php?page=cf7sg_help')),
1560 'html'=>'<div class="inline-top"><iframe width="230" height="130" src="https://www.youtube.com/embed/WiweQRhOr0g" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div><div class="inline-top"><strong>Learn how to create multi-step multi-slide CF7 forms using a slider construct functionality of the Smart Grid-layout extension plugin.</strong></div>',
1561 'pages'=>array('plugins.php','edit.php')
1562 );
1563 $notices['cf7sg_modular_tutorial'] = array(
1564 'type'=>'notice-update', //[notice-update|notice-error]
1565 /* translators: %s is the link to the tuorial page*/
1566 'msg'=>sprintf( __('There is a new tutorial for <a href="%s">modular form construct</a>','cf7-grid-layout'), admin_url('admin.php?page=cf7sg_help')),
1567 'html'=>'<div class="inline-top"><iframe width="230" height="130" src="https://www.youtube.com/embed/vPc3M5Emro4" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div><div class="inline-top"><strong>Learn how to use modular form construct functionality allowing complex forms to be assembled from other child forms.</strong></div>',
1568 'pages'=>array('plugins.php','edit.php')
1569 );
1570 }
1571 if(!empty($notices)) update_option('cf7sg-admin-notices', $notices);
1572
1573 update_option(self::CF7SG_OPTION, array(
1574 'gv'=>CF7_GRID_VERSION,
1575 'fv'=>CF7SG_VERSION_FORM_UPDATE
1576 ));
1577 }
1578 /**
1579 * Display admin notices
1580 * Hooked on 'admin_notices'
1581 *@since 4.1.0
1582 */
1583 public function admin_notices(){
1584 //check if we have any notices.
1585 global $pagenow;
1586 $notices = get_option('cf7sg-admin-notices', array());
1587
1588 if(empty($notices)) return;
1589
1590 if(!isset(self::$admin_notice_pages[$pagenow])) return;
1591
1592 $notify=false;
1593 $rule = self::$admin_notice_pages[$pagenow];
1594 // debug_msg($rule, 'notice rules ');
1595 if($pagenow=='post.php'){
1596 $screen = get_current_screen();
1597 if($this->cf7_post_type() == $screen->post_type) $notify=true;
1598 }else if( !empty($rule) and is_array($rule) ){
1599 foreach($rule as $key=>$type){
1600 if( isset($_GET[$type]) and $_GET[$type]==$key ) $notify=true;
1601 }
1602 }else $notify = true; //default.
1603
1604 if(!$notify) return; //rules don't match;
1605
1606 //do we have any notices to display?
1607 foreach($notices as $id=>$notice){
1608 if( isset($notice['pages']) and !in_array($pagenow, $notice['pages'])) continue;
1609 $time = isset($notice['time']) ? $notice['time']: 'forever';
1610 $dismiss = "$id-$time";
1611 if ( ! PAnD::is_admin_notice_active( $dismiss ) ) {
1612 unset($notices[$id]);
1613 update_option('cf7sg-admin-notices', $notices);
1614 continue ; //continue foreach loop.
1615 }
1616 if(!isset($notice['html'])) $notice['html'] = '';
1617 ?>
1618 <style>.notice .inline-top{display: inline-block;vertical-align: top;margin-right: 10px;max-width: 300px;}</style>
1619 <div data-dismissible="<?=$dismiss?>" class="notice <?=$notice['type']?> is-dismissible"><p><?=$notice['msg']?></p><?=$notice['html']?></div>
1620 <?php
1621 /** @since 4.7.1 dismiss notices once displayed */
1622 unset($notices[$id]);
1623 update_option('cf7sg-admin-notices', $notices);
1624 }
1625 }
1626 /**
1627 * ajax request to validate plugin update.
1628 * Hooked on 'wp_ajax_validate_cf7sg_version_update'
1629 * @since 4.1.0
1630 */
1631 public function validate_cf7sg_version_update(){
1632 if( !isset($_POST['nonce']) or !wp_verify_nonce($_POST['nonce'], 'cf7sg_udpate_plugin') ){
1633 echo 'error, nonce failed, try to reload the page.';
1634 wp_die();
1635 }
1636 $grid_settings = get_option(self::CF7SG_OPTION, array());
1637 $warning = false;
1638 if(isset($grid_settings['fv']) ) {
1639 if(version_compare($grid_settings['fv'], CF7SG_VERSION_FORM_UPDATE, '<') ) $warning = true;
1640 }
1641 $grid_settings['fv'] = CF7SG_VERSION_FORM_UPDATE;
1642 $grid_settings['gv'] = CF7_GRID_VERSION;
1643 $grid_settings['update'] = true;
1644 update_option(self::CF7SG_OPTION, $grid_settings);
1645 $update_msg = __('Version validated, thank you!','cf7-grid-layout');
1646 if($warning){
1647 $link = admin_url('edit.php?post_type=wpcf7_contact_form');
1648 /* translators: %s link to form table */
1649 $update_msg = __('You need to <strong>update</strong> your <a href="%s">forms</a>','cf7-grid-layout');
1650 $update_msg = sprintf( $update_msg, $link);
1651 }
1652 echo $update_msg;
1653 wp_die();
1654 }
1655 /**
1656 * Check if plugin is getting updated.
1657 * Hooked to 'upgrader_post_install'.
1658 *@since 4.1.0
1659 *@param array $param text_description
1660 *@param array $extras text_description
1661 *@param array $result text_description
1662 *@return array responses for the upgrade process
1663 */
1664 public function post_plugin_upgrade($response, $extras, $result){
1665 if( ( isset($response['destination_name']) and 'cf7-grid-layout' == $response['destination_name'] )
1666 or ( isset($extras['plugin']) and 'cf7-grid-layout/cf7-grid-layout.php' == $extras['plugin'] ) ){
1667 $grid_settings = get_option(self::CF7SG_OPTION, array());
1668 $grid_settings['update'] = true;
1669 update_option(self::CF7SG_OPTION, $grid_settings);
1670 }
1671 return $response;
1672 }
1673 /**
1674 * Preview form post type creation.
1675 * hooked on action 'init'.
1676 *@since 4.3.0
1677 *@param string $param text_description
1678 *@return string text_description
1679 */
1680 // Register Custom Post Type
1681 function register_form_preview_posttype() {
1682
1683 $labels = array(
1684 'name' => _x( 'CF7 Forms', 'Post Type General Name', 'cf7-grid-layout' ),
1685 'singular_name' => _x( 'CF7 Form', 'Post Type Singular Name', 'cf7-grid-layout' ),
1686 'menu_name' => __( 'CF7 Forms', 'cf7-grid-layout' ),
1687 'name_admin_bar' => __( 'CF7 Form', 'cf7-grid-layout' ),
1688 'parent_item_colon' => __( 'Parent Item:', 'cf7-grid-layout' ),
1689 'all_items' => __( 'All Form Pages', 'cf7-grid-layout' ),
1690 'add_new_item' => __( 'Add New Form Page', 'cf7-grid-layout' ),
1691 'add_new' => __( 'Add New', 'cf7-grid-layout' ),
1692 'new_item' => __( 'New Form Page', 'cf7-grid-layout' ),
1693 'edit_item' => __( 'Edit Form Page', 'cf7-grid-layout' ),
1694 'update_item' => __( 'Update Form Page', 'cf7-grid-layout' ),
1695 'view_item' => __( 'View Form Page', 'cf7-grid-layout' ),
1696 'view_items' => __( 'View Form Pages', 'cf7-grid-layout' ),
1697 'search_items' => __( 'Search Form Page', 'cf7-grid-layout' ),
1698 'not_found' => __( 'Not found', 'cf7-grid-layout' ),
1699 'not_found_in_trash' => __( 'Not found in Trash', 'cf7-grid-layout' ),
1700 );
1701 $rewrite = array(
1702 'slug' => 'sgform',
1703 'with_front' => true,
1704 'pages' => true,
1705 'feeds' => false,
1706 );
1707 $args = array(
1708 'label' => __( 'CF7 Form', 'cf7-grid-layout' ),
1709 'description' => __( 'Preview/View CF7 forms on the front-end', 'cf7-grid-layout' ),
1710 'labels' => $labels,
1711 'public' => true,
1712 'show_ui' => false,
1713 'show_in_menu' => false,
1714 'show_in_admin_bar' => false,
1715 'show_in_nav_menus' => false,
1716 'can_export' => false,
1717 'has_archive' => false,
1718 'exclude_from_search' => true,
1719 'publicly_queryable' => true,
1720 // 'query_var' => 'sgcf7',
1721 'rewrite' => $rewrite,
1722 );
1723 register_post_type( 'cf7sg_page', $args );
1724
1725 }
1726 /**
1727 * Load translation files for the CF7 Polylang extension.
1728 * Hoooked to ''
1729 *@since 4.4.0
1730 *@param string $param text_description
1731 *@return string text_description
1732 */
1733 public function load_translation_files($trans){
1734 $trans[$this->plugin_name]=CF7SG_TRANSLATED_VERSION;
1735 return $trans;
1736 }
1737 }
1738