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