Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change deactivation approach #2562

Merged
merged 8 commits into from
Aug 18, 2023
5 changes: 5 additions & 0 deletions docs/api/internal.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,11 @@
"example": "publish",
"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"
Expand Down
5 changes: 5 additions & 0 deletions includes/admin/class-wp-job-manager-promoted-jobs-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on setting the flag only if we're creating a new job

}

update_option( WP_Job_Manager_Promoted_Jobs_Status_Handler::USED_PROMOTED_JOBS_OPTION_KEY, true );

$url = add_query_arg(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ public function get_items() {
'meta_query' => [
[
'key' => WP_Job_Manager_Promoted_Jobs::PROMOTED_META_KEY,
'value' => '1',
'compare' => '=',
'compare' => 'EXISTS',
],
],
];
Expand Down Expand Up @@ -234,6 +233,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 ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you foresee any changes we need to make on WPJMCOM to use this field?

I am just thinking about the use-case here. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, I don't think so. But I added it because it can be useful in the future and make the feed more concise (we don't have promoted and not promoted jobs in the feed without a differentiation).
We could use that, in case we want to list all the promoted jobs as a type of extra promotion, for example.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks!

'title' => $item->post_title,
'description' => $item->post_content,
'permalink' => get_permalink( $item ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}
Expand Down Expand Up @@ -125,22 +124,19 @@ 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.
fjorgemota marked this conversation as resolved.
Show resolved Hide resolved
* It happens when a job promotion is deactivated.
*
* @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;
}
}

Expand Down
29 changes: 25 additions & 4 deletions includes/promoted-jobs/class-wp-job-manager-promoted-jobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,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 );

return update_post_meta( $post_id, self::PROMOTED_META_KEY, $promoted ? '1' : '0' );
}

Expand All @@ -172,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, false );
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;
}

/**
Expand Down
Loading