move_listings_to_renewal(); $this->move_listings_to_expired(); $this->send_renewal_reminders(); $this->delete_expired_listings(); } /** * Move listings to renewal status (only if applicable). * * @since 1.0.0 * @access private */ private function move_listings_to_renewal() { $general_settings = get_option( 'acadp_general_settings' ); $email_settings = get_option( 'acadp_email_template_listing_renewal' ); $can_renew = empty( $general_settings['has_listing_renewal'] ) ? false : true; $email_threshold = (int) $email_settings['email_threshold']; if( $can_renew && $email_threshold > 0 ) { // Define the query $args = array( 'post_type' => 'acadp_listings', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'listing_status', 'value' => 'post_status', 'compare' => '=' ), array( 'key' => "DATE_SUB(expiry_date, INTERVAL $email_threshold DAY)", 'value' => current_time( 'mysql' ), 'compare' => '<', 'type' => 'DATETIME' ) ) ); $acadp_query = new WP_Query( $args ); // Start the Loop global $post; if( $acadp_query->have_posts() ) { while( $acadp_query->have_posts() ) { $acadp_query->the_post(); // Update the post_meta into the database update_post_meta( $post->ID, 'listing_status', 'renewal' ); // Send emails acadp_email_listing_owner_listing_renewal( $post->ID ); } } // Use reset postdata to restore orginal query wp_reset_postdata(); } } /** * Move listings to expired status (only if applicable). * * @since 1.0.0 * @access private */ private function move_listings_to_expired() { $email_settings = get_option( 'acadp_email_template_listing_expired' ); // Define the query $args = array( 'post_type' => 'acadp_listings', 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => array( array( 'key' => 'expiry_date', 'value' => current_time( 'mysql' ), 'compare' => '<', 'type' => 'DATETIME' ) ) ); $acadp_query = new WP_Query( $args ); // Start the Loop global $post; if( $acadp_query->have_posts() ) { while( $acadp_query->have_posts() ) { $acadp_query->the_post(); // Update the post into the database $acadp_post = array( 'ID' => $post->ID, 'post_status' => 'private' ); wp_update_post( $acadp_post ); // Update the post_meta into the database update_post_meta( $post->ID, 'listing_status', 'expired' ); // Send emails acadp_email_listing_owner_listing_expired( $post->ID ); acadp_email_admin_listing_expired( $post->ID ); } } // Use reset postdata to restore orginal query wp_reset_postdata(); } /** * Send renewal reminders to expired listings (only if applicable) * * @since 1.0.0 * @access private */ private function send_renewal_reminders() { $general_settings = get_option( 'acadp_general_settings' ); $email_settings = get_option( 'acadp_email_template_renewal_reminder' ); $can_renew = empty( $general_settings['has_listing_renewal'] ) ? false : true; $reminder_threshold = (int) $email_settings['reminder_threshold']; if( $can_renew && $reminder_threshold > 0 ) { // Define the query $args = array( 'post_type' => 'acadp_listings', 'posts_per_page' => -1, 'post_status' => 'private', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'listing_status', 'value' => 'expired', 'compare' => '=' ), array( 'key' => "DATE_ADD(expiry_date, INTERVAL $reminder_threshold DAY)", 'value' => current_time( 'mysql' ), 'compare' => '<', 'type' => 'DATETIME' ) ) ); $acadp_query = new WP_Query( $args ); // Start the Loop global $post; if( $acadp_query->have_posts() ) { while( $acadp_query->have_posts() ) { $acadp_query->the_post(); // Send emails acadp_email_listing_owner_listing_renewal_reminder( $post->ID ); } } // Use reset postdata to restore orginal query wp_reset_postdata(); } } /** * Delete expired listings (only if applicable) * * @since 1.0.0 * @access private */ private function delete_expired_listings() { $general_settings = get_option( 'acadp_general_settings' ); $email_settings = get_option( 'acadp_email_template_renewal_reminder' ); $delete_threshold = max( $general_settings['delete_expired_listings'], $email_settings['reminder_threshold'] ); if( $delete_threshold > 0 ) { // Define the query $args = array( 'post_type' => 'acadp_listings', 'posts_per_page' => -1, 'post_status' => 'private', 'meta_query' => array( 'relation' => 'AND', array( 'key' => 'listing_status', 'value' => 'expired', 'compare' => '=' ), array( 'key' => "DATE_ADD(expiry_date, INTERVAL $delete_threshold DAY)", 'value' => current_time( 'mysql' ), 'compare' => '<', 'type' => 'DATETIME' ) ) ); $acadp_query = new WP_Query( $args ); // Start the Loop global $post; if( $acadp_query->have_posts() ) { while( $acadp_query->have_posts() ) { $acadp_query->the_post(); // Delete the listing wp_delete_post( $post->ID, true ); } } // Use reset postdata to restore orginal query wp_reset_postdata(); } } }