PluginProbe
Smart Grid-Layout Design for Contact Form 7 / 4.12
Smart Grid-Layout Design for Contact Form 7 v4.12
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 / assets / cf7-admin-table / admin / cf7-post-admin-table.php

cf7-post-admin-table.php in Smart Grid-Layout Design for Contact Form 7 4.12, at assets/cf7-admin-table/admin/cf7-post-admin-table.php

607 lines 20.0 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 cf7 custom post table.
5 *
6 * @link http://syllogic.in
7 * @since 1.1.0
8 *
9 * @package Cf7_Polylang
10 * @subpackage Cf7_Polylang/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_Polylang
20 * @subpackage Cf7_Polylang/admin
21 * @author Aurovrata V. <vrata@syllogic.in>
22 */
23 if(!class_exists('CF7SG_WP_Post_Table')){
24
25 class CF7SG_WP_Post_Table {
26 /**
27 * A CF7 list table object.
28 *
29 * @since 1.1.0
30 * @access private
31 * @var CF7SG_WP_Post_Table $singleton cf7 admin list table object.
32 */
33 private static $singleton;
34 /**
35 * A CF7 list table object.
36 *
37 * @since 1.1.0
38 * @access private
39 * @var array $forms_key_ids an array of key=>id pairs
40 */
41 private static $forms_key_ids;
42 private $version;
43 /**
44 * A flag to monitor if hooks are in place.
45 *
46 * @since 1.1.0
47 * @access private
48 * @var boolean $hooks_set true if hooks are set.
49 */
50 private $hooks_set;
51
52 protected function __construct(){
53 $this->hooks_set= false;
54 $this->version = "1.3";
55 }
56 /**
57 * get cf7 post type set by cf7 plugin.
58 *
59 *@since 3.0.0
60 *@return string post type.
61 */
62 static private function cf7_post_type(){
63 $type = 'wpcf7_post_type_notset';
64 if(class_exists('WPCF7_ContactForm') ) {
65 $type = WPCF7_ContactForm::post_type;
66 }
67 return $type;
68 }
69 public static function set_table(){
70 if(null === self::$singleton ){
71 self::$singleton = new self();
72 }
73 return self::$singleton;
74 }
75
76 public function hooks(){
77 if( !$this->hooks_set ){
78 $this->hooks_set= true;
79 return false;
80 }
81 return $this->hooks_set;
82 }
83 /**
84 * Register the stylesheets for the admin area.
85 *
86 * @since 1.1.0
87 */
88 public function enqueue_styles() {
89 $screen = get_current_screen();
90 if (self::cf7_post_type() != $screen->post_type){
91 return;
92 }
93
94 switch( $screen->base ){
95 case 'post':
96 //for the future
97 break;
98 case 'edit':
99 wp_enqueue_style( 'cf7sg-post-table-css', plugin_dir_url( __FILE__ ) . 'css/cf7-admin-table.css', false, $this->version );
100 break;
101 }
102 }
103 public function enqueue_script() {
104 $screen = get_current_screen();
105 if (self::cf7_post_type() != $screen->post_type){
106 return;
107 }
108
109 switch( $screen->base ){
110 case 'post':
111 //for the future
112 break;
113 case 'edit':
114 wp_enqueue_script( 'cf7sg-post-table-js', plugin_dir_url( __FILE__ ) . 'js/cf7-post-table.js', false, $this->version, true );
115 // wp_localize_script('cf7sg-post-table-js','cf7_2_post_admin', array('keys'=>$keys));
116 break;
117 }
118 }
119
120 /**
121 * Store a psir of key=>id values for each form
122 *
123 * @since 1.2.0
124 * @param string $key form key.
125 * @param string $id form post id.
126 **/
127 private static function set_key_id($key, $id){
128 if(null === self::$forms_key_ids){
129 self::$forms_key_ids = array();
130 }
131 self::$forms_key_ids[$key]=$id;
132 }
133 /**
134 * Get a form id from its key if set
135 *
136 * @since 1.2.0
137 * @param string $key form key.
138 * @return string form post id.
139 **/
140 public static function form_id($key){
141 if(null === self::$forms_key_ids){
142 self::$forms_key_ids = array();
143 }
144 // debug_msg(self::$forms_key_ids, "$key -> ? ");
145 if(isset(self::$forms_key_ids[$key])) return self::$forms_key_ids[$key];
146 else{
147 $form_id = 0;
148 $forms = get_posts(array(
149 'post_type' => self::cf7_post_type(),
150 'post_name__in' => array($key)
151 ));
152 if(!empty($forms)){
153 $form_id = $forms[0]->ID;
154 wp_reset_postdata();
155 }
156 self::$forms_key_ids[$key]=$form_id;
157 return $form_id;
158 }
159 }
160 /**
161 * Get a form key from its id
162 *
163 * @since 1.2.0
164 * @param string $id form id.
165 * @return string form post key.
166 **/
167 public static function form_key($id){
168 if(null === self::$forms_key_ids){
169 self::$forms_key_ids = array();
170 }
171 if(false !== ($key = array_search($id, self::$forms_key_ids))) return $key;
172 else{
173 $key = null;
174 $form = get_post($id);
175 if(!empty($form)){
176 $key = $form->post_name;
177 self::$forms_key_ids[$key] = $id;
178 }
179 return $key;
180 }
181 }
182 /**
183 * Checks if this is the admin table list page
184 *
185 * @since 1.1.3
186 */
187 public static function is_cf7_admin_page(){
188 if(!isset($_GET['post_type']) || false === strpos($_GET['post_type'], self::cf7_post_type()) ){
189 return false;
190 }else{
191 $screen = get_current_screen();
192 return ( 'edit' == $screen->base && '' == $screen->action );
193 }
194 }
195 /**
196 * check if this is a cf7 edit page.
197 *
198 * @since 1.1.3
199 * @return bool true is this is the edit page
200 */
201 public static function is_cf7_edit_page(){
202 if(!isset($_GET['page']) || false === strpos($_GET['page'],'wpcf7') ){
203 return false;
204 }else{
205 if(isset($_GET['post']) ){
206 global $post_ID; //need to set the global post ID to make sure it is available for polylang.
207 $post_ID = $_GET['post'];
208 }
209 if(function_exists('get_current_screen')){
210 $screen = get_current_screen(); //use screen option after intial basic check else it may throw fatal error
211 return ( 'contact_page_wpcf7-new' == $screen->base || 'toplevel_page_wpcf7' == $screen->base );
212 }else{
213 return false;
214 }
215 }
216 }
217 /**
218 * Modify the regsitered cf7 post tppe
219 * THis function enables public capability and amind UI visibility for the cf7 post type. Hooked late on `init`
220 * @since 1.0.0
221 *
222 */
223 public function modify_cf7_post_type(){
224 if(post_type_exists( self::cf7_post_type() ) ) {
225 global $wp_post_types;
226 $wp_post_types[self::cf7_post_type()]->public = false;
227 $wp_post_types[self::cf7_post_type()]->show_ui = true;
228 }
229 }
230
231 /**
232 * Adds a new sub-menu
233 * Add a new sub-menu to the Contact main menu, as well as remove the current default
234 *
235 */
236 public function add_cf7_sub_menu(){
237 //remove_submenu_page( $menu_slug, $submenu_slug );
238 remove_submenu_page( 'wpcf7', 'wpcf7' );
239 $hook = add_submenu_page(
240 'wpcf7',
241 __( 'Edit Form Types', 'cf7-grid-layout' ),
242 __( 'Form Types', 'cf7-grid-layout' ),
243 'wpcf7_read_contact_forms',
244 'edit-tags.php?taxonomy=wpcf7_type&post_type=wpcf7_contact_form'
245 );
246 /** @since 4.0.0 helper sub-menu */
247 $hook = add_submenu_page(
248 'wpcf7',
249 __( 'Smart Grid Helper Tutorials ', 'cf7-grid-layout' ),
250 __( 'Tutorials', 'cf7-grid-layout' ),
251 'wpcf7_read_contact_forms',
252 'cf7sg_help',
253 array($this, 'display_helper_page')
254 );
255 }
256 /**
257 * Display helper tutorial page
258 * called by add_sbuenu_page()
259 *@since 4.0.0
260 */
261 public function display_helper_page(){
262 require_once plugin_dir_path( __FILE__ ) .'partials/cf7sg-tutorial-page.php';
263 }
264 /**
265 * Change the submenu order
266 * @since 1.0.0
267 */
268 public function change_cf7_submenu_order( $menu_ord ) {
269 global $submenu;
270 // Enable the next line to see all menu orders
271 if(!isset($submenu['wpcf7']) ){
272 return $menu_ord;
273 }
274 //debug_msg($submenu['wpcf7']);
275 if( is_network_admin() ){
276 return $menu_ord;
277 }
278 $arr = array();
279 //debug_msg($submenu['wpcf7']);
280 foreach($submenu['wpcf7'] as $menu){
281 switch($menu[2]){
282 case 'cf7_post': //do nothing, we hide this submenu
283 $arr[]=$menu;
284 break;
285 case 'edit.php?post_type=wpcf7_contact_form':
286 //push to the front
287 array_unshift($arr, $menu);
288 break;
289 default:
290 $arr[]=$menu;
291 break;
292 }
293 }
294 $submenu['wpcf7'] = $arr;
295 return $menu_ord;
296 }
297 /**
298 * Modify cf7 post type list table columns
299 * Hooked on 'modify_{$post_type}_posts_columns', to remove the default columns
300 *
301 */
302 public function modify_cf7_list_columns($columns){
303 if(isset($columns['title'])) $columns['title'] = __('Form','contact-form-7');
304 if(isset($columns['date'])) unset($columns['date']);
305 $columns['shortcode'] = 'Shortcode<br /><span class="cf7-help-tip"><a href="javascript:void();">What\'s this?</a><span class="cf7-short-info">Use this shortcode the same way you would use the contact-form-7 shortcode. (See the plugin page for more information )</span></span>';
306 $columns['cf7_key'] = __('Form key', 'cf7-grid-layout');
307 return $columns;
308 }
309 /**
310 * Populate custom columns in cf7 list table
311 * @since 1.0.0
312 *
313 */
314 public function populate_custom_column( $column, $post_id ) {
315 switch ( $column ) {
316 case 'shortcode' :
317
318 $form = get_post($post_id);
319 $output = "\n" . '<span class="shortcode cf7-2-post-shortcode"><input type="text"'
320 . ' onfocus="this.select();" readonly="readonly"'
321 . ' value="' . esc_attr( '[cf7form cf7key="'.$form->post_name.'"]' ) . '"'
322 . ' class="large-text code" /></span>';
323
324 echo trim( $output );
325
326 break;
327 case 'cf7_key':
328 $form = get_post($post_id);
329 $update = '';
330 if( get_post_meta($post_id, '_cf7sg_managed_form', true) ){
331 $version = get_post_meta($post_id, '_cf7sg_version', true);
332 if(version_compare($version, CF7SG_VERSION_FORM_UPDATE, '<')) $update = 'cf7sg-update';
333 }
334 if(empty($update)){
335 $errors = get_post_meta($post_id, '_config_errors', true);
336 if(!empty($errors)) $update = 'cf7-errors';
337 }
338 echo '<span class="cf7-form-key" data-update="'.$update.'">'.$form->post_name.'</span>';
339 break;
340 }
341 }
342
343 /**
344 * Modify the quick action links in the contact table.
345 * Since this plugin replaces the default contact form list table
346 * for the more std WP table, we need to modify the quick links to match the default ones.
347 * This function is hooked on 'post_row_actions'
348 * @since 1.1.0
349 * @param Array $actions quick link actions
350 * @param WP_Post $post the current row's post object
351 */
352 public function modify_cf7_list_row_actions($actions, $post){
353 //check for your post type
354 if('trash'==$post->post_status) return array();
355
356 if ($post->post_type ==self::cf7_post_type()){
357 $form = WPCF7_ContactForm::get_instance($post->ID);
358 $url = admin_url( 'post.php?post=' . absint( $form->id() ) . '&action=edit');
359
360 if ( current_user_can( 'wpcf7_edit_contact_form', $form->id() ) ) {
361 /** @since 3.0.0 removed edit/trash as taken care by WP core */
362 $copy_link = wp_nonce_url(
363 add_query_arg( array( 'action' => 'cf7copy' ), $url ),
364 'wpcf7-copy-contact-form_' . absint( $form->id() )
365 );
366
367 $actions['copy'] = sprintf(
368 '<a href="%1$s">%2$s</a>',
369 esc_url( $copy_link ),
370 esc_html( __cf7sg( 'Duplicate' ) )
371 );
372 }
373 }
374 return $actions;
375 }
376 // /**
377 // * Redirect to new table list on form delete
378 // * hooks on 'wp_redirect'
379 // * @since 1.1.3
380 // * @var string $location a fully formed url
381 // * @var int $status the html redirect status code
382 // */
383 // public function filter_cf7_redirect($location, $status){
384 // //debug_msg($status, 'redirecting ...'.$location);
385 //
386 // if( self::is_cf7_admin_page() || self::is_cf7_edit_page() ){
387 // if( 'delete' == wpcf7_current_action()){
388 // global $post_ID;
389 // do_action('wpcf7_post_delete',$post_ID);
390 //
391 // return admin_url('edit.php?post_type=wpcf7_contact_form');
392 // }
393 // }
394 // return $location;
395 // }
396
397 // /**
398 // * Function to populate the quick edit form
399 // * Hooked on 'quick_edit_custom_box' action
400 // *
401 // * @since 1.0.0
402 // * @param string $column_name column name to add edit field.
403 // * @param string $post_type post type being displayed.
404 // * @return string echos the html fields.
405 // **/
406 // public function quick_edit_box( $column_name, $post_type ) {
407 // if(self::cf7_post_type() != $post_type){
408 // return;
409 // }
410 // static $printNonce = TRUE;
411 // if ( $printNonce ) {
412 // $printNonce = FALSE;
413 // wp_nonce_field( plugin_basename( __DIR__ ), 'cf7_key_nonce' );
414 // }
415 // switch ( $column_name ) {
416 // case 'cf7_key':
417 //
418 // break;
419 // default:
420 // echo '';
421 // break;
422 // }
423 // }
424 /**
425 *cf7-form Shortcode handler
426 *
427 * @since 1.0.0
428 * @param Array $atts array of attributes.
429 * @return string $p2 .
430 **/
431 public function shortcode( $atts ) {
432 $a = array_merge( array(
433 'cf7key' => '',
434 ), $atts );
435
436 if(empty($a['cf7key'])){
437 return '<em>' . __('cf7-form shortcode missing key attribute','cf7-grid-layout') . '</em>';
438 }
439 /** @since 4.4.0 enable field values */
440 $hidden = apply_filters('cf7sg_include_hidden_form_fields', array(),$a['cf7key']);
441 $fields = array();
442 foreach($atts as $key=>$atts_val){
443 $field = explode('/',$atts_val);
444 if(is_array($field) && 'cf7sg'==$field[0]){
445 switch(count($field)){
446 case 2:
447 $field = explode('=',$field[1]);
448 $fields[$field[0]] = isset($field[1]) ? trim($field[1],'"'):'';
449 break;
450 case 3:
451 if('hidden'==$field[1]){
452 $field = explode('=',$field[2]);
453 $hidden[$field[0]] = isset($field[1]) ? trim($field[1],'"'):'';
454 }
455 break;
456 }
457 unset($a[$key]);
458 }
459 }
460
461 //else get the post ID
462 $args = array(
463 'post_type' => self::cf7_post_type(),
464 'name' => $a['cf7key'],
465
466 );
467 if(isset($a['lang'])) $args['lang'] = $a['lang'];
468 $form = get_posts($args);
469 if(!empty($form)){
470 $id = $form[0]->ID;
471 if( !isset($a['lang']) ){ /** @since 4.4 allow different lang */
472 $id = apply_filters('cf7_form_shortcode_form_id',$id, $atts);
473 }
474 wp_reset_postdata();
475 $attributes ='';
476 foreach($a as $key=>$value){
477 $attributes .= ' '.$key.'="'.$value.'"';
478 }
479 /** @since 4.4.0 diffrentiate preview forms */
480 if( isset($_GET['post_type']) && 'cf7sg_page'==$_GET['post_type'] && isset($_GET['preview']) ){
481 $hidden['_cf7sg_preview']=true;
482 if(isset($_COOKIE['_cf7sg_'.$a['cf7key']])){
483 $fields = array_merge( json_decode( stripslashes($_COOKIE['_cf7sg_'.$a['cf7key']]), true),$fields);
484 }
485 }
486 if(!empty($hidden)){
487 add_filter('wpcf7_form_hidden_fields', function($hide) use ($hidden, $id) {
488 $form = wpcf7_get_current_contact_form();
489 // debug_msg($hidden, "$form->id() add hidden " );
490 if(empty($form)) return $hide;
491 if($form->id()!=$id) return $hide;
492 return array_merge($hide, $hidden);
493 },PHP_INT_MAX,1);
494 }
495 if(!empty($fields)){
496 add_filter('cf7sg_prefill_form_fields', function($pairs, $key) use ($fields, $a){
497 if($a['cf7key']==$key) return array_merge($pairs, $fields);
498 },PHP_INT_MAX,2);
499 }
500 return do_shortcode('[contact-form-7 id="'.$id.'"'.$attributes.']');
501 }else{
502 return '<em>' . __('cf7form shortcode key error, unable to find form, did you update your form key?','cf7-grid-layout') . '</em>';
503 }
504 }
505
506 /**
507 * Register a form type taxoonmy for classifying forms
508 * Hooked to 'init' action
509 * @since 1.0.0
510 **/
511 public function register_cf7_taxonomy(){
512 if(!class_exists('WPCF7_ContactForm')){
513 return;
514 }
515 $plural = 'Form Types';
516 $name = 'Form Type';
517 $is_hierarchical = true;
518 $slug = 'wpcf7_type';
519 $labels = array(
520 'name' => $plural,
521 'singular_name' => $name,
522 'menu_name' => $plural,
523 'all_items' => 'All '.$plural,
524 'parent_item' => 'Parent '.$name,
525 'parent_item_colon' => 'Parent '.$name.':',
526 'new_item_name' => 'New '.$name.' Name',
527 'add_new_item' => 'Add New '.$name,
528 'edit_item' => 'Edit '.$name,
529 'update_item' => 'Update '.$name,
530 'view_item' => 'View '.$name,
531 'separate_items_with_commas' => 'Separate '.$plural.' with commas',
532 'add_or_remove_items' => 'Add or remove '.$plural,
533 'choose_from_most_used' => 'Choose from the most used',
534 'popular_items' => 'Popular '.$plural,
535 'search_items' => 'Search '.$plural,
536 'not_found' => 'Not Found',
537 'no_terms' => 'No '.$plural,
538 'items_list' => $plural.' list',
539 'items_list_navigation' => $plural.' list navigation',
540 );
541 //labels can be modified post registration
542 $args = array(
543 'labels' => $labels,
544 'hierarchical' => $is_hierarchical,
545 'public' => true,
546 'show_ui' => true,
547 'show_in_menu' => true,
548 'show_admin_column' => true,
549 'show_in_nav_menus' => false,
550 'show_tagcloud' => false,
551 'show_in_quick_edit' => true,
552 'description' => 'Contact Form 7 types',
553 );
554
555 register_taxonomy( $slug, self::cf7_post_type(), $args );
556 }
557 /**
558 * Add a script to the admin table page to highlight form uddates.
559 * hooked to 'admin_footer'.
560 *@since 2.6.0
561 *@param string $param text_description
562 *@return string text_description
563 */
564 public function update_form_highlight(){
565 $screen = get_current_screen();
566 if (!isset($screen) || self::cf7_post_type() != $screen->post_type){
567 return;
568 }
569 switch( $screen->base ){
570 case 'edit':
571 /** @since 4.11.5 enable cf7 form errors to be notified */
572 ?>
573 <script type="text/javascript">
574 (function($){
575 $(document).ready(function(){
576 $('tbody#the-list tr').each(function(){
577 var $tr = $(this);
578 var update = $tr.find('.cf7-form-key').data('update');
579 switch(update){
580 case 'cf7sg-update':
581 $tr.find('a.row-title').addClass(update).after('<span class="cf7sg-popup display-none"><?=__('Please udpate this form, simply edit and update.', 'cf7-grid-layout')?></span>').parent().css('position','relative');
582 break;
583 case 'cf7-errors':
584 $tr.find('a.row-title').addClass(update).after('<span class="cf7sg-popup display-none"><?=__('CF7 plugin has found misconfiguration errors on this form.', 'cf7-grid-layout')?></span>').parent().css('position','relative');
585 break;
586 }
587 });
588 });
589 })(jQuery);
590 </script>
591 <?php
592 break;
593 }
594 }
595 } //end class
596 if(!function_exists('get_cf7form_id')){
597 function get_cf7form_id($cf7_key){
598 return CF7SG_WP_Post_Table::form_id($cf7_key);
599 }
600 }
601 if(!function_exists('get_cf7form_key')){
602 function get_cf7form_key($cf7_id){
603 return CF7SG_WP_Post_Table::form_key($cf7_id);
604 }
605 }
606 }
607