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