PluginProbe
Smart Grid-Layout Design for Contact Form 7 / 4.5
Smart Grid-Layout Design for Contact Form 7 v4.5
4.18.0 4.17.0 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 4.0.0 4.0.1 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10 4.11 4.12 4.13 4.14 All 119 releases
cf7-grid-layout / 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.5, at assets/cf7-admin-table/admin/cf7-post-admin-table.php

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