Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kibus90/1d13b0fbd34ab2a532084f7a0d9336c8 to your computer and use it in GitHub Desktop.
Save kibus90/1d13b0fbd34ab2a532084f7a0d9336c8 to your computer and use it in GitHub Desktop.
WP Job Manager: Test new filter for job dashboard actions which allows success message
<?php
namespace JobManager;
add_filter( 'job_manager_my_job_actions', __NAMESPACE__ . '\\add_expire_action', 10, 2 );
function add_expire_action( $actions, $job_id ) {
if ( ! function_exists( __NAMESPACE__ . '\\handle_expire_action' ) ) {
return $actions;
}
$job = get_post( $job_id );
if ( $job->post_status === 'publish' ) {
$actions['expire'] = array(
'label' => __( 'Expire', 'wp-job-manager' ),
'nonce' => true
);
}
return $actions;
}
add_filter( 'job_manager_job_dashboard_do_action', __NAMESPACE__ . '\\handle_expire_action', 10, 3 );
function handle_expire_action( $success_message, $action, $job_id ) {
if ( $action !== 'expire' ) {
return $success_message;
}
$job = get_post( $job_id );
// Check status
if ( $job->post_status === 'expired' ) {
throw new \Exception( __( 'This job is already expired.', 'wp-job-manager' ) );
}
// Update
$job->post_status = 'expired';
$updated = wp_update_post( $job );
if ( ! $updated || is_wp_error( $updated ) ) {
throw new \Exception( __( 'Job could not be changed.', 'wp-job-manager' ) );
}
// Message
return sprintf( __( '%s has expired', 'jobwrk' ), wpjm_get_the_job_title( $job ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment