MP_Addon_Push_Callback

Class MP_Addon_Push_Callback.


Source Source

File: calculator-push-notification-add-on/inc/MP_Addon_Push_Callback.php

class MP_Addon_Push_Callback {


	/**
	 * Email settings.
	 *
	 * @var mixed|void
	 *
	 * @since 1.0.0
	 * @access private
	 */
	private $options_email;


	/**
	 * Style settings.
	 *
	 * @var mixed|void
	 *
	 * @since 1.0.0
	 * @access private
	 */
	private $options_style;



	/**
	 * Callback constructor.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function __construct() {

		$this->options_email = get_option( MortgagePlatform::$prefix . 'email' );
		$this->options_style = get_option( MortgagePlatform::$prefix . 'style' );
	}


	/**
	 * Action Push Notification.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function push_notification( $post_id ) {

		if ( isset( $this->options_email['allow_browser_notification'] ) ) {

			$found = false;
			$pushes = wp_cache_get('push_addon_posts',MortgagePlatform::$domain, false, $found);
			if(!$found){
				$pushes  = new \WP_Query(
					array(
						'post_type'      => 'admin_browser_push',
						'posts_per_page' => - 1,
					)
				);
				wp_cache_set('push_addon_posts', $pushes, MortgagePlatform::$domain, WEEK_IN_SECONDS);
			}

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

					$subscription_data = get_post_meta( $pushes->post->ID, 'admin_browser_push', true );
					$subscription      = $subscription_data['subscription'];

					$subscription = Subscription::create( $subscription );

					$site_url = get_site_url( '', '/wp-admin/post.php?post=' . $post_id . '&action=edit' );
					if ( $this->options_style['bitly'] ) {
						$bitly    = new MP_Bitly();
						$site_url = $bitly->get_short( $site_url );
					}
					$payload = array(
						'title' => __( 'New Lead', 'CalculatorsPushNotificationAddon' ),
						'body'  => __( 'You have a new Lead', 'CalculatorsPushNotificationAddon' ),
						//'icon'=> '/android-chrome-192x192.png',
						'data'  => array(
							'url' => $site_url,
						),
					);
					$payload = wp_json_encode( $payload );
					$webpush = new MP_Addon_Push_WebPushNotification();
					$res     = $webpush->send_push( $subscription, $payload );

					if ( $res ) {
						MP_Logs::set_logs( $post_id, 'push', 'Success' );
					}
				}
			}

			wp_reset_postdata();

		}

	}


	/**
	 * Subscribe Admin.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function webpush_save_subscriber() {

		MP_Callback::check_nonce( $_GET['nonce'], 'mortgage_json-request' );

		$json = file_get_contents( 'php://input', true );
		$data = json_decode( $json );
		$data = json_decode( wp_json_encode( $data ), true );

		$subscription_data = $data['subscription'];
		$subscription      = Subscription::create( $subscription_data );

		$payload = array(
			'title' => __( 'Subscribe to Push Notifications', 'CalculatorsPushNotificationAddon' ),
			'body'  => __( 'Thank you for enabling push notifications for ', 'CalculatorsPushNotificationAddon' ) . $data['browser'],
			//'icon'=> '/android-chrome-192x192.png',
			'data'  => array(
				'url' => '',
			),

		);
		$payload = wp_json_encode( $payload );
		$webpush = new MP_Addon_Push_WebPushNotification();
		$webpush->send_push( $subscription, $payload );

		$post_data = array(
			'post_title'  => wp_strip_all_tags( $data['userid'] ),
			'post_status' => 'publish',
			'post_type'   => 'admin_browser_push',
		);

		$post_id = wp_insert_post( $post_data, true );
		if ( $post_id ) {
			add_post_meta( $post_id, 'admin_browser_push', $data );
		}

		wp_cache_delete('push_addon_posts', MortgagePlatform::$domain);
		wp_cache_delete('push_addon_leads', MortgagePlatform::$domain);

		wp_die();
	}


	/**
	 * Unsubscribe Admin.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function webpush_unsubscriber() {

		MP_Callback::check_nonce( $_GET['nonce'], 'mortgage_json-request' );

		$json = file_get_contents( 'php://input' );
		$data = json_decode( $json );
		$data = json_decode( wp_json_encode( $data ), true );

		if ( $data['endpoint'] ) {

			$found = false;
			$pushes = wp_cache_get('push_addon_posts',MortgagePlatform::$domain, false, $found);
			if(!$found){
				$pushes  = new \WP_Query(
					array(
						'post_type'      => 'admin_browser_push',
						'posts_per_page' => - 1,
					)
				);
				wp_cache_set('push_addon_posts', $pushes, MortgagePlatform::$domain, WEEK_IN_SECONDS);
			}

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

					$subscription_data = get_post_meta( $pushes->post->ID, 'admin_browser_push', true );
					$endpoint          = $subscription_data['subscription']['endpoint'];

					if ( $data['endpoint'] === $endpoint ) {
						wp_delete_post( $pushes->post->ID );
					}
				}
			}

			wp_reset_postdata();
		}

		wp_cache_delete('push_addon_posts', MortgagePlatform::$domain);
		wp_cache_delete('push_addon_leads', MortgagePlatform::$domain);

		wp_die();
	}



	/**
	 * Send manually push notification to lead from the lead page.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function json_send_push() {

		MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_json-request' );

		$post_id = $_POST['leadId'];
		$title   = $_POST['pushTitle'];
		$body    = $_POST['pushText'];
		$url     = ! empty( $_POST['pushURL'] ) ? $_POST['pushURL'] : site_url( '/' );

		$push_data = get_post_meta( $post_id, MortgagePlatform::$prefix . 'push', true );

		if ( '' === $push_data ) {
			echo esc_html__( 'The Lead has not push data', 'CalculatorsPushNotificationAddon' );
			wp_die();
		}
		$subscription = $push_data['subscription'];

		$webpush      = new MP_Addon_Push_WebPushNotification();
		$subscription = Subscription::create( $subscription );

		$payload = array(
			'title' => esc_attr( $title ),
			'body'  => esc_attr( $body ),
			//'icon'=> '/android-chrome-192x192.png',
			'data'  => array(
				'url' => $url,
			),
		);

		$payload = wp_json_encode( $payload );
		$res     = $webpush->send_push( $subscription, $payload );

		/*preg_match ( '/[(.*?)]/' ,$res, $matches );
		$status = $matches[1];

		if ($status == 'x' ) {
			delete_post_meta($post_id, MortgagePlatform::$prefix.'push');
		}*/

		preg_match( '/](.*?)https/', $res, $matches );
		$pushsend = $matches[1];

		MP_Logs::set_logs( $post_id, 'lead_push_manually', $pushsend );

		echo esc_attr( $pushsend );

		wp_die();
	}


	/**
	 * Send manually push notification to all leads from the lead page.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function json_send_push_all() {

		MP_Callback::check_nonce( $_POST['nonce'], 'mortgage_json-request' );

		$title = $_POST['pushTitle'];
		$body  = $_POST['pushText'];
		$url   = ! empty( $_POST['pushURL'] ) ? $_POST['pushURL'] : site_url( '/' );

		$found = false;
		$leads = wp_cache_get('push_addon_leads',MortgagePlatform::$domain, false, $found);
		if(!$found){
			$leads = new \WP_Query(
				array(
					'post_type'      => MortgagePlatform::$lead,
					'posts_per_page' => - 1,
					'post_status'    => array( 'publish', 'pending' ),
					'meta_query'     => array(
						array(
							'key'     => MortgagePlatform::$prefix . 'push',
							'value'   => '',
							'compare' => '!=',
						),
					),
				)
			);
			wp_cache_set('push_addon_leads', $leads, MortgagePlatform::$domain, WEEK_IN_SECONDS);
		}


		if ( $leads->have_posts() ) {

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

				$push_data    = get_post_meta( $leads->post->ID, MortgagePlatform::$prefix . 'push', true );
				$subscription = $push_data['subscription'];

				$webpush      = new MP_Addon_Push_WebPushNotification();
				$subscription = Subscription::create( $subscription );

				$payload = array(
					'title' => $title,
					'body'  => $body,
					//'icon'=> '/android-chrome-192x192.png',
					'data'  => array(
						'url' => $url,
					),
				);
				$payload = wp_json_encode( $payload );
				$res     = $webpush->send_push( $subscription, $payload );

				/*preg_match ( '/[(.*?)]/' ,$res, $matches );
				$status = $matches[1];

				if ($status == 'x' ) {
					delete_post_meta($leads->post->ID, MortgagePlatform::$prefix.'push');
				}*/

				preg_match( '/](.*?)https/', $res, $matches );
				$pushsend = $matches[1];

				MP_Logs::set_logs( $leads->post->ID, 'lead_push_manually', $pushsend );

			}
		} else {
			esc_html_e( 'Not found leads subscribed to push notifications', 'CalculatorsPushNotificationAddon' );
			wp_die();
		}

		wp_reset_postdata();

		preg_match( '/](.*?)https/', $res, $matches );
		$pushsend = $matches[1];

		wp_send_json( $pushsend );

	}



}

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.