MP_SheduleEmails

Class MP_SheduleEmails


Description Description

See also See also


Top ↑

Source Source

File: mortgage-platform/Inc/MP_SheduleEmails.php

class MP_SheduleEmails extends MP_SendEmail {


	/**
	 * Enable or disable the schedule for sending emails.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function schedule_send_email() {

		if ( isset( get_option( MortgagePlatform::$prefix . 'email' )['allow_resend_email'] ) ) {

			new MP_Cron(
				array(
					'events' => array(
						MortgagePlatform::$prefix . 'first_send_email' => array(
							'callback'      => array( $this, 'schedule_first_send_email' ),
							'interval_name' => 'hourly',
							'interval_sec'  => HOUR_IN_SECONDS * 1,
							'interval_desc' => 'Every hour',
						),
						MortgagePlatform::$prefix . 'second_send_email' => array(
							'callback'      => array( $this, 'schedule_second_send_email' ),
							'interval_name' => 'hourly',
							'interval_sec'  => HOUR_IN_SECONDS * 1,
							'interval_desc' => 'Every hour',
						),
					),
				)
			);

		} else {

			wp_clear_scheduled_hook( MortgagePlatform::$prefix . 'first_send_email' );
			wp_clear_scheduled_hook( MortgagePlatform::$prefix . 'second_send_email' );

		}
	}


	/**
	 * Send first email by schedule.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function schedule_first_send_email() {

		$leads = new \WP_Query(
			array(
				'post_type'      => MortgagePlatform::$lead,
				'posts_per_page' => -1,
				'post_status'    => array( 'publish', 'pending' ),
				'meta_key'       => MortgagePlatform::$prefix . 'first_send',
				'meta_value'     => array( time(), time() - HOUR_IN_SECONDS + HOUR_IN_SECONDS ),
				'meta_compare'   => 'BETWEEN',
			)
		);

		if ( $leads->have_posts() ) {
			while ( $leads->have_posts() ) {
				$leads->the_post();

				$eol = PHP_EOL;

				$mail_from = get_option( MortgagePlatform::$prefix . 'email' )['email'] ? get_option( MortgagePlatform::$prefix . 'email' )['email'] : get_option( 'admin_email' );

				$post_id      = $leads->post->ID;
				$mail_to      = get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_email', true );
				$mail_subject = get_option( MortgagePlatform::$prefix . 'email' )['first_email_subject'] ? get_option( MortgagePlatform::$prefix . 'email' )['first_email_subject'] : '';
				$mail_text    = get_option( MortgagePlatform::$prefix . 'email' )['first_email_text'] ? get_option( MortgagePlatform::$prefix . 'email' )['first_email_text'] : '';

				$headers  = 'MIME-Version: 1.0' . $eol;
				$headers .= 'Content-type:text/html;charset=UTF-8' . $eol;
				$headers .= 'From: ' . $mail_from . $eol;

				$mailsend = $this->send_email( $mail_to, $mail_subject, $mail_text, $headers );

				if ( $mailsend ) {

					delete_post_meta( $post_id, MortgagePlatform::$prefix . 'first_send' );

					$mailsend = $mailsend ? 'Success' : 'Error';

					//$lead_log['lead_first_email_by_schedule'] = current_time( "Y-m-d H:i:s" ) . ' : ' . $mailsend;
					//add_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_log', wp_slash( $lead_log ), '' );

					MP_Logs::set_logs( $post_id, 'lead_first_email_by_schedule', $mailsend );

				}
			}
		}

		wp_reset_postdata();
	}


	/**
	 * Send second email by schedule.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function schedule_second_send_email() {

		$leads = new \WP_Query(
			array(
				'post_type'      => MortgagePlatform::$lead,
				'posts_per_page' => -1,
				'post_status'    => array( 'publish', 'pending' ),
				'meta_key'       => MortgagePlatform::$prefix . 'second_send',
				'meta_value'     => array( time(), time() - HOUR_IN_SECONDS + HOUR_IN_SECONDS ),
				'meta_compare'   => 'BETWEEN',
			)
		);

		if ( $leads->have_posts() ) {
			while ( $leads->have_posts() ) {
				$leads->the_post();

				$eol = PHP_EOL;

				$mail_from = get_option( MortgagePlatform::$prefix . 'email' )['email'] ? get_option( MortgagePlatform::$prefix . 'email' )['email'] : get_option( 'admin_email' );

				$post_id      = $leads->post->ID;
				$mail_to      = get_post_meta( $post_id, MortgagePlatform::$prefix . 'lead_email', true );
				$mail_subject = get_option( MortgagePlatform::$prefix . 'email' )['second_email_subject'] ? get_option( MortgagePlatform::$prefix . 'email' )['second_email_subject'] : '';
				$mail_text    = get_option( MortgagePlatform::$prefix . 'email' )['second_email_text'] ? get_option( MortgagePlatform::$prefix . 'email' )['second_email_text'] : '';

				$headers  = 'MIME-Version: 1.0' . $eol;
				$headers .= 'Content-type:text/html;charset=UTF-8' . $eol;
				$headers .= 'From: ' . $mail_from . $eol;

				$mailsend = $this->send_email( $mail_to, $mail_subject, $mail_text, $headers );

				remove_filter( 'wp_mail_content_type', array( $this, 'set_html_content_type' ) );

				if ( $mailsend ) {

					delete_post_meta( $post_id, MortgagePlatform::$prefix . 'second_send' );

					$mailsend = $mailsend ? 'Success' : 'Error';

					//$lead_log['lead_second_email_by_schedule'] = current_time( "Y-m-d H:i:s" ) . ' : ' . $mailsend;
					//add_metadata( 'post', $post_id, MortgagePlatform::$prefix . 'lead_log', wp_slash( $lead_log ), '' );

					MP_Logs::set_logs( $post_id, 'lead_second_email_by_schedule', $mailsend );

				}
			}
		}

		wp_reset_postdata();
	}

}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.