From 6e17f71220583d3f2d2f1d25fb18a62bcb675913 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Wed, 16 Aug 2023 15:32:05 -0300 Subject: [PATCH 1/8] Update API to contain promoted attribute --- docs/api/internal.json | 7 ++++++- .../class-wp-job-manager-promoted-jobs-api.php | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/api/internal.json b/docs/api/internal.json index 29b10685a..ac4ad7d0b 100644 --- a/docs/api/internal.json +++ b/docs/api/internal.json @@ -300,9 +300,14 @@ }, "status": { "type": "string", - "example": "publish", + "example": true, "description": "The job status." }, + "promoted": { + "type": "boolean", + "example": false, + "description": "Whether the job has promoted status on the plugin." + }, "title": { "type": "string", "example": "Senior Software Engineer" diff --git a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php index 76b2c79e4..07af50f9e 100644 --- a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php +++ b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php @@ -234,6 +234,7 @@ private function prepare_item_for_response( WP_Post $item ) { return [ 'id' => (string) $item->ID, 'status' => $item->post_status, + 'promoted' => WP_Job_Manager_Promoted_Jobs::is_promoted( $item->ID ), 'title' => $item->post_title, 'description' => $item->post_content, 'permalink' => get_permalink( $item ), From 52e3cd9b7782a3d6f08c3732546462992ee78151 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Wed, 16 Aug 2023 15:32:25 -0300 Subject: [PATCH 2/8] List all jobs with promoted meta key in the feed --- .../promoted-jobs/class-wp-job-manager-promoted-jobs-api.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php index 07af50f9e..2bc251290 100644 --- a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php +++ b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-api.php @@ -190,8 +190,7 @@ public function get_items() { 'meta_query' => [ [ 'key' => WP_Job_Manager_Promoted_Jobs::PROMOTED_META_KEY, - 'value' => '1', - 'compare' => '=', + 'compare' => 'EXISTS', ], ], ]; From 0501cb6cf187ff698e85667cc8bc390c00de5915 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Wed, 16 Aug 2023 15:33:00 -0300 Subject: [PATCH 3/8] Add a meta key as not promoted as soon the user is redirected to wpjobmanager.com --- includes/admin/class-wp-job-manager-promoted-jobs-admin.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/admin/class-wp-job-manager-promoted-jobs-admin.php b/includes/admin/class-wp-job-manager-promoted-jobs-admin.php index 05ac1f842..465a8d883 100644 --- a/includes/admin/class-wp-job-manager-promoted-jobs-admin.php +++ b/includes/admin/class-wp-job-manager-promoted-jobs-admin.php @@ -209,6 +209,11 @@ public function handle_promote_job() { $verify_endpoint_url = rest_url( '/wpjm-internal/v1/promoted-jobs/verify-token', 'https' ); $verify_endpoint_url = substr( $verify_endpoint_url, strlen( $site_url ) ); + // Make sure the job contains the promoted job meta to be listed in the feed. + if ( ! $is_editing ) { + WP_Job_Manager_Promoted_Jobs::update_promotion( $post_id, false ); + } + update_option( WP_Job_Manager_Promoted_Jobs_Status_Handler::USED_PROMOTED_JOBS_OPTION_KEY, true ); $url = add_query_arg( From 30e4f0ac195b3aea7e4db144d3f413ed9ab7537f Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Wed, 16 Aug 2023 15:34:34 -0300 Subject: [PATCH 4/8] Remove promoted meta key when manually deactivating a promotion --- .../class-wp-job-manager-promoted-jobs.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 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 bf38a8469..024a88a73 100644 --- a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php +++ b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php @@ -149,8 +149,9 @@ public static function is_promoted( $post_id ) { /** * Update promotion. * - * @param int $post_id - * @param bool $promoted + * @param int $post_id + * @param bool|string $promoted `true` to promoted, `false` to not promoted, `force_delete` to delete. + * The deletion is used to force a removal from the feed, deactivating the promotion while syncing. * * @return boolean */ @@ -161,6 +162,10 @@ public static function update_promotion( $post_id, $promoted ) { 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' ); } @@ -172,7 +177,7 @@ public static function update_promotion( $post_id, $promoted ) { * @return boolean */ public static function deactivate_promotion( $post_id ) { - return self::update_promotion( $post_id, false ); + return self::update_promotion( $post_id, 'force_delete' ); } /** From 8ef9d31f86e90fdfc76d1527460066352ac6820b Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Wed, 16 Aug 2023 15:35:19 -0300 Subject: [PATCH 5/8] Update jobs notification to get promoted meta deletion --- ...ob-manager-promoted-jobs-notifications.php | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php index 5392b5428..05483ae32 100644 --- a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php +++ b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php @@ -85,10 +85,9 @@ public static function instance() { public function __construct() { $this->watched_fields = [ 'post' => [ 'post_name', 'post_title', 'post_content', 'post_status' ], - 'meta' => [ WP_Job_Manager_Promoted_Jobs::PROMOTED_META_KEY ], ]; add_action( 'post_updated', [ $this, 'post_updated' ], 10, 3 ); - add_action( 'update_postmeta', [ $this, 'meta_updated' ], 10, 4 ); + add_action( 'deleted_post_meta', [ $this, 'deleted_meta' ], 10, 3 ); add_action( 'shutdown', [ $this, 'maybe_trigger_notification' ] ); add_action( 'job_manager_promoted_jobs_notification', [ $this, 'send_notification' ] ); } @@ -125,22 +124,18 @@ function( $key ) use ( $keys ) { } /** - * Checks if we should send a notification to wpjobmanager.com after a post meta is updated. + * Checks if we should send a notification to wpjobmanager.com after promoted meta key is deleted. * - * @param int $meta_id Meta ID. - * @param int $post_id Post ID. - * @param string $meta_key Meta key. - * @param mixed $meta_value Meta value. - */ - public function meta_updated( $meta_id, $post_id, $meta_key, $meta_value ) { - if ( ! WP_Job_Manager_Promoted_Jobs::is_promoted( $post_id ) ) { - return; - } - if ( in_array( $meta_key, $this->watched_fields['meta'], true ) ) { - $current_value = get_post_meta( $post_id, $meta_key, true ); - if ( $current_value !== $meta_value ) { - $this->watched_fields_changed = true; - } + * @param string[] $meta_ids + * @param int $post_id Post ID. + * @param string $meta_key Meta key. + */ + public function deleted_meta( $meta_ids, $post_id, $meta_key ) { + if ( + WP_Job_Manager_Promoted_Jobs::PROMOTED_META_KEY === $meta_key + && 'job_listing' === get_post_type( $post_id ) + ) { + $this->watched_fields_changed = true; } } From 63db340d060cfcfe88bdbd69665306607c9bc9d7 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Thu, 17 Aug 2023 17:08:03 -0300 Subject: [PATCH 6/8] Enhance PHPDoc --- .../class-wp-job-manager-promoted-jobs-notifications.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php index 05483ae32..f4c0cf4f6 100644 --- a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php +++ b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs-notifications.php @@ -125,6 +125,7 @@ function( $key ) use ( $keys ) { /** * Checks if we should send a notification to wpjobmanager.com after promoted meta key is deleted. + * It happens when a job promotion is deactivated. * * @param string[] $meta_ids * @param int $post_id Post ID. From fa4ba7434f8586744810c05753cf356d6fb830af Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Thu, 17 Aug 2023 17:09:00 -0300 Subject: [PATCH 7/8] Fix swagger documentation Co-authored-by: Fernando Jorge Mota --- docs/api/internal.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/internal.json b/docs/api/internal.json index ac4ad7d0b..3b6183c55 100644 --- a/docs/api/internal.json +++ b/docs/api/internal.json @@ -300,7 +300,7 @@ }, "status": { "type": "string", - "example": true, + "example": "publish", "description": "The job status." }, "promoted": { From 51ebc4b1032916694a0271080d4d8afcf95b8335 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Thu, 17 Aug 2023 17:32:59 -0300 Subject: [PATCH 8/8] Refactor methods to avoid boolean with special string in the same parameter --- .../class-wp-job-manager-promoted-jobs.php | 38 +++++++++++++------ 1 file changed, 27 insertions(+), 11 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..b2281f388 100644 --- a/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php +++ b/includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php @@ -149,23 +149,16 @@ public static function is_promoted( $post_id ) { /** * Update promotion. * - * @param int $post_id - * @param bool|string $promoted `true` to promoted, `false` to not promoted, `force_delete` to delete. - * The deletion is used to force a removal from the feed, deactivating the promotion while syncing. + * @param int $post_id + * @param bool $promoted * * @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 +170,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; } /**