From 99c974bb5e3a663a3701874971b576ff16c4ef62 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Thu, 17 Aug 2023 17:32:59 -0300 Subject: [PATCH] Refactor methods to avoid boolean with special string in the same parameter --- .../class-wp-job-manager-promoted-jobs.php | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php index 024a88a73..05596fa40 100644 --- a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php +++ b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php @@ -156,16 +156,10 @@ public static function is_promoted( $post_id ) { * @return boolean */ public static function update_promotion( $post_id, $promoted ) { - if ( 'job_listing' !== get_post_type( $post_id ) ) { + if ( ! self::pre_change_promotion( $post_id ) ) { return false; } - delete_option( self::PROMOTED_JOB_TRACK_OPTION ); - - if ( 'force_delete' === $promoted ) { - return delete_post_meta( $post_id, self::PROMOTED_META_KEY ); - } - return update_post_meta( $post_id, self::PROMOTED_META_KEY, $promoted ? '1' : '0' ); } @@ -177,7 +171,30 @@ public static function update_promotion( $post_id, $promoted ) { * @return boolean */ public static function deactivate_promotion( $post_id ) { - return self::update_promotion( $post_id, 'force_delete' ); + if ( ! self::pre_change_promotion( $post_id ) ) { + return false; + } + + return delete_post_meta( $post_id, self::PROMOTED_META_KEY ); + } + + /** + * Run necessary things before promotion change. + * - Check post type. + * - Clear job counter option. + * + * @param int $post_id + * + * @return boolean Whether pre change passed correctly. + */ + private static function pre_change_promotion( $post_id ) { + if ( 'job_listing' !== get_post_type( $post_id ) ) { + return false; + } + + delete_option( self::PROMOTED_JOB_TRACK_OPTION ); + + return true; } /**