Skip to content

Commit

Permalink
Fix warnings and deprecation messages in admin when CF7 is active.
Browse files Browse the repository at this point in the history
Add a live form in the Contact Form 7 admin form view.
  • Loading branch information
kagg-design committed Jul 9, 2024
1 parent 9a82407 commit 6773514
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: captcha, hcaptcha, antispam, abuse, protect form
Requires at least: 5.3
Tested up to: 6.6
Requires PHP: 7.2
Stable tag: 4.3.0
Stable tag: 4.2.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -561,6 +561,10 @@ Instructions for popular native integrations are below:

== Changelog ==

= 4.3.1 =
* Added a live form in the Contact Form 7 admin form view.
* Fixed warnings and deprecation messages in admin when CF7 is active.

= 4.3.0 =
* NOTE: the plugin has been renamed from "hCaptcha for WordPress" to "hCaptcha for WP"
* Dropped support for PHP 7.0 and 7.1. The minimum required PHP version is now 7.2.
Expand Down
46 changes: 43 additions & 3 deletions src/php/CF7/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,30 @@ public function init_hooks(): void {
return;
}

add_action( 'wpcf7_admin_init', [ $this, 'add_tag_generator_hcaptcha' ], 54 );
add_action( 'toplevel_page_wpcf7', [ $this, 'before_toplevel_page_wpcf7' ], 0 );
add_action( 'toplevel_page_wpcf7', [ $this, 'after_toplevel_page_wpcf7' ], 20 );
add_action( 'current_screen', [ $this, 'current_screen' ] );
}

/**
* Current screen.
*
* @param mixed $current_screen Current screen.
*
* @return void
*/
public function current_screen( $current_screen ): void {
$current_screen_id = $current_screen->id ?? '';

if ( ! $this->is_cf7_editor_page( $current_screen_id ) ) {
return;
}

add_action( $current_screen_id, [ $this, 'before_toplevel_page_wpcf7' ], 0 );
add_action( $current_screen_id, [ $this, 'after_toplevel_page_wpcf7' ], 20 );

add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts_before_cf7' ], 0 );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts_after_cf7' ], 20 );

add_action( 'wpcf7_admin_init', [ $this, 'add_tag_generator_hcaptcha' ], 54 );
}

/**
Expand Down Expand Up @@ -255,4 +274,25 @@ public function enqueue_admin_scripts_after_cf7(): void {
$wp_scripts->registered['wpcf7-admin']->extra['data'] = 'var wpcf7 = ' . wp_json_encode( $wpcf7 ) . ';';
}
}

/**
* Check if the current page is a CF7 editor page.
*
* @param string $current_screen_id Hook suffix.
*
* @return bool
*/
private function is_cf7_editor_page( string $current_screen_id ): bool {

if ( ! preg_match( '/^toplevel_page_wpcf7$|.+page_wpcf7-new$/', $current_screen_id ) ) {
return false;
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ( 'toplevel_page_wpcf7' === $current_screen_id ) && ! isset( $_GET['post'] ) ) {
return false;
}

return true;
}
}

0 comments on commit 6773514

Please sign in to comment.