Skip to content

Commit

Permalink
Repair edit job listing screen. (#2802)
Browse files Browse the repository at this point in the history
  • Loading branch information
gikaragia authored Apr 16, 2024
2 parents 278c51d + 42aae9c commit 0e2c21f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 16 deletions.
40 changes: 38 additions & 2 deletions includes/abstracts/abstract-wp-job-manager-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ public function process() {
* @param array $atts Attributes to use in the view handler.
*/
public function output( $atts = [] ) {
WP_Job_Manager\WP_Job_Manager_Recaptcha::enqueue_scripts();
$step_key = $this->get_step_key( $this->step );
$this->show_errors();
$this->show_messages();
Expand Down Expand Up @@ -323,7 +322,44 @@ public function clear_fields() {
*/
public function enqueue_scripts() {
_deprecated_function( __METHOD__, '$$next-version$$', 'WP_Job_Manager\WP_Job_Manager_Form::enqueue_scripts' );
WP_Job_Manager\WP_Job_Manager_Recaptcha::enqueue_scripts();
WP_Job_Manager\WP_Job_Manager_Recaptcha::instance()->enqueue_scripts();
}


/**
* Output the reCAPTCHA field.
*
* @deprecated
*/
public function display_recaptcha_field() {
_deprecated_function( __METHOD__, '$$next-version$$', 'WP_Job_Manager\WP_Job_Manager_Form::display_recaptcha_field' );
WP_Job_Manager\WP_Job_Manager_Recaptcha::instance()->display_recaptcha_field();
}

/**
* Validate a reCAPTCHA field.
*
* @param bool $success
*
* @deprecated
*
* @return bool|\WP_Error
*/
public function validate_recaptcha_field( $success ) {
_deprecated_function( __METHOD__, '$$next-version$$', 'WP_Job_Manager\WP_Job_Manager_Form::validate_recaptcha_field' );
return WP_Job_Manager\WP_Job_Manager_Recaptcha::instance()->validate_recaptcha_field( $success );
}

/**
* Checks whether reCAPTCHA has been set up and is available.
*
* @deprecated
*
* @return bool
*/
public function is_recaptcha_available() {
_deprecated_function( __METHOD__, '$$next-version$$', 'WP_Job_Manager\WP_Job_Manager_Form::is_recaptcha_available' );
return WP_Job_Manager\WP_Job_Manager_Recaptcha::instance()->is_recaptcha_available();
}

/**
Expand Down
53 changes: 40 additions & 13 deletions includes/class-wp-job-manager-recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,56 @@ private function __construct() {
$this->secret_key = get_option( self::RECAPTCHA_SECRET_KEY );
$this->recaptcha_version = get_option( self::RECAPTCHA_VERSION, 'v2' );

if ( $this->use_recaptcha_field() ) {
add_action( 'submit_job_form_end', [ $this, 'display_recaptcha_field' ] );
add_filter( 'submit_job_form_validate_fields', [ $this, 'validate_recaptcha_field' ] );
add_filter( 'submit_draft_job_form_validate_fields', [ $this, 'validate_recaptcha_field' ] );
}

/**
* Enables the reCAPTCHA field on the form. To do that, it checks if the provided option is enabled and if it is
* it adds the necessary hooks to display and validate the reCAPTCHA field.
*
* @param string $recaptcha_enabled_option The options name to check if the reCAPTCHA field is enabled.
* @param array $display_hooks The hooks to display the reCAPTCHA field.
* @param array $validate_hooks The hooks to validate the reCAPTCHA field.
*
* @return void
*/
public function maybe_enable_recaptcha( string $recaptcha_enabled_option, array $display_hooks, array $validate_hooks ) {
if ( $this->use_recaptcha_field( $recaptcha_enabled_option ) ) {
foreach ( $display_hooks as $display_hook ) {
add_action( $display_hook, [ $this, 'display_recaptcha_field' ] );
}

foreach ( $validate_hooks as $validate_hook ) {
add_filter( $validate_hook, [ $this, 'validate_recaptcha_field' ] );
}

add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
}
}

/**
* Use reCAPTCHA field on the form?
*
* @param string $option_name The options name to check if the reCAPTCHA field is enabled.
*
* @return bool
*/
public function use_recaptcha_field() {
private function use_recaptcha_field( $option_name ) {
if ( ! $this->is_recaptcha_available() ) {
return false;
}
return 1 === absint( get_option( 'job_manager_enable_recaptcha_job_submission' ) );

return 1 === absint( get_option( $option_name ) );
}

/**
* Enqueue the scripts for the form.
* Enqueue the scripts and add appropriate hooks for the recaptcha to load.
*
* @access private
*/
public static function enqueue_scripts() {
public function enqueue_scripts() {
$instance = self::instance();

if (
$instance->use_recaptcha_field() &&
in_array( $instance->recaptcha_version, [ 'v2', 'v3' ], true )
) {
if ( in_array( $instance->recaptcha_version, [ 'v2', 'v3' ], true ) ) {
$recaptcha_version = $instance->recaptcha_version;
$recaptcha_url = '';

Expand All @@ -100,6 +121,8 @@ public static function enqueue_scripts() {
/**
* Checks whether reCAPTCHA has been set up and is available.
*
* @access private
*
* @return bool
*/
public function is_recaptcha_available() {
Expand All @@ -116,7 +139,9 @@ public function is_recaptcha_available() {
}

/**
* Dispaly the reCAPTCHA field in the form.
* Display the reCAPTCHA field in the form.
*
* @access private
*
* @return void
*/
Expand All @@ -142,6 +167,8 @@ public function display_recaptcha_field() {
*
* @param bool $success
*
* @access private
*
* @return bool|WP_Error
*/
public function validate_recaptcha_field( $success ) {
Expand Down
6 changes: 5 additions & 1 deletion includes/forms/class-wp-job-manager-form-submit-job.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ public function __construct() {
WP_Job_Manager_Helper_Renewals::instance( $this );

// Recaptcha support.
WP_Job_Manager\WP_Job_Manager_Recaptcha::instance();
WP_Job_Manager\WP_Job_Manager_Recaptcha::instance()->maybe_enable_recaptcha(
'job_manager_enable_recaptcha_job_submission',
[ 'submit_job_form_end' ],
[ 'submit_job_form_validate_fields', 'submit_draft_job_form_validate_fields' ]
);

if ( $this->use_agreement_checkbox() ) {
add_action( 'submit_job_form_end', [ $this, 'display_agreement_checkbox_field' ] );
Expand Down

0 comments on commit 0e2c21f

Please sign in to comment.