Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

jsarnowski / jsarnowski/jet-engine   php

Repository URL to install this package:

/ modules / forms / notifications.php

<?php
/**
 * Form notifications class
 */

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
	die;
}

if ( ! class_exists( 'Jet_Engine_Booking_Forms_Notifications' ) ) {

	/**
	 * Define Jet_Engine_Booking_Forms_Notifications class
	 */
	class Jet_Engine_Booking_Forms_Notifications {

		public $form            = null;
		public $data            = null;
		public $notifications   = array();
		public $manager         = null;
		public $handler         = null;
		public $log             = array();
		public $log_status      = false;
		public $specific_status = false;

		public $headers;
		public $email_data;

		/**
		 * Constructor for the class
		 */
		function __construct( $form = null, $data = array(), $manager, $handler ) {

			$this->form    = $form;
			$this->data    = $data;
			$this->manager = $manager;
			$this->handler = $handler;

			$this->notifications = $this->manager->editor->get_notifications( $this->form );

			add_action(
				'jet-engine/forms/booking/notification/register_user',
				array( $this, 'register_user' )
			);

			add_action(
				'jet-engine/forms/booking/notification/update_user',
				array( $this, 'update_user' )
			);

			add_action(
				'jet-engine/forms/booking/notification/webhook',
				array( $this, 'webhook' )
			);

			add_action(
				'jet-engine/forms/booking/notification/hook',
				array( $this, 'hook' )
			);

			add_action(
				'jet-engine/forms/booking/notification/insert_post',
				array( $this, 'insert_post' )
			);

			add_action(
				'jet-engine/forms/booking/notification/email',
				array( $this, 'email' )
			);

			add_action(
				'jet-engine/forms/booking/notification/redirect',
				array( $this, 'do_redirect' )
			);

			add_action(
				'jet-engine/forms/booking/notification/activecampaign',
				array( $this, 'activecampaign' )
			);

			add_action(
				'jet-engine/forms/booking/notification/mailchimp',
				array( $this, 'mailchimp' )
			);

			add_action(
				'jet-engine/forms/booking/notification/getresponse',
				array( $this, 'getresponse' )
			);

			add_action(
				'jet-engine/forms/booking/notification/update_options',
				array( $this, 'update_options' )
			);

			add_action( 'jet-engine/forms/booking/email/send-before', array( $this, 'send_before' ) );
			add_action( 'jet-engine/forms/booking/email/send-after', array( $this, 'send_after' ) );

		}

		/**
		 * Unregister notification
		 *
		 * @param  [type] $type [description]
		 * @return [type]       [description]
		 */
		public function unregister_notification_type( $type ) {

			switch ( $type ) {

				case 'register_user':
					remove_action(
						'jet-engine/forms/booking/notification/register_user',
						array( $this, 'register_user' )
					);
					break;

				case 'update_user':
					remove_action(
						'jet-engine/forms/booking/notification/update_user',
						array( $this, 'update_user' )
					);
					break;

				case 'webhook':
					remove_action(
						'jet-engine/forms/booking/notification/webhook',
						array( $this, 'webhook' )
					);
					break;

				case 'hook':
					remove_action(
						'jet-engine/forms/booking/notification/hook',
						array( $this, 'hook' )
					);
					break;

				case 'insert_post':
					remove_action(
						'jet-engine/forms/booking/notification/insert_post',
						array( $this, 'insert_post' )
					);
					break;

				case 'redirect':
					remove_action(
						'jet-engine/forms/booking/notification/redirect',
						array( $this, 'do_redirect' )
					);
					break;

				case 'activecampaign':
					remove_action(
						'jet-engine/forms/booking/notification/activecampaign',
						array( $this, 'activecampaign' )
					);
					break;

				default:
					remove_all_actions( 'jet-engine/forms/booking/notification/' . $type );
					break;
			}
		}

		/**
		 * Unregister notification bu index
		 *
		 * @param  [type] $index [description]
		 * @return [type]        [description]
		 */
		public function unregister_notification( $index ) {

			if ( isset( $this->notifications[ $index ] ) ) {
				unset( $this->notifications[ $index ] );
			}

		}

		/**
		 * Returns all registered notifications
		 *
		 * @return [type] [description]
		 */
		public function get_all() {
			return $this->notifications;
		}

		/**
		 * Send form notifications
		 *
		 * @return [type] [description]
		 */
		public function send() {

			if ( empty( $this->notifications ) ) {
				$this->notifications = $this->manager->editor->get_notifications( $this->form );
			}

			do_action( 'jet-engine/forms/notifications/before-send', $this );

			foreach ( $this->notifications as $index => $notification ) {

				/**
				 * Process single notification
				 */
				do_action( 'jet-engine/forms/booking/notification/' . $notification['type'], $notification, $this );

			}

			if ( empty( $this->log ) ) {
				return false;
			} else {
				return count( $this->log ) === count( array_filter( $this->log ) );
			}

		}

		/**
		 * Check if all notifications are successfully processed
		 *
		 * @return boolean [description]
		 */
		public function is_success() {

			if ( empty( $this->log ) ) {
				return false;
			} else {
				return count( $this->log ) === count( array_filter( $this->log ) );
			}

		}

		/**
		 * Perform redirect notification
		 *
		 * @param  [type] $notification [description]
		 * @return [type]               [description]
		 */
		public function do_redirect( $notification ) {

			$this->log[] = true;

			if ( ! $this->is_success() ) {
				return;
			}

			$type = ! empty( $notification['redirect_type'] ) ? $notification['redirect_type'] : 'static_page';

			switch ( $type ) {
				case 'static_page':
					$to_page = ! empty( $notification['redirect_page'] ) ? $notification['redirect_page'] : false;
					$to_url  = ! empty( $to_page ) ? get_permalink( $to_page ) : false;
					break;

				case 'current_page':
					$to_url = $this->handler->refer;
					break;

				default:
					$to_url = ! empty( $notification['redirect_url'] ) ? $notification['redirect_url'] : false;
					break;
			}

			if ( ! $to_url ) {
				return;
			} else {

				if ( ! empty( $notification['redirect_hash'] ) ) {
					$to_url = trailingslashit( $to_url ) . '#' . $notification['redirect_hash'];
				}

				if ( ! empty( $notification['redirect_args'] ) ) {

					$redirect_args = array();

					foreach ( $notification['redirect_args'] as $arg ) {
						$redirect_args[ $arg ] = ! empty( $this->data[ $arg ] ) ? $this->data[ $arg ] : 0;
					}

					$to_url = add_query_arg( $redirect_args, $to_url );

				}

				if ( ! $this->handler->is_ajax() ) {
					wp_safe_redirect( $to_url );
					die();
				} else {
					$this->handler->add_response_data( array( 'redirect' => $to_url ) );
				}
			}

		}

		/**
		 * Call a webhook notification
		 *
		 * @param  [type] $notification [description]
		 * @return [type]               [description]
		 */
		public function webhook( $notification ) {

			$webhook_url = ! empty( $notification['webhook_url'] ) ? esc_url( $notification['webhook_url'] ) : false;

			if ( ! $webhook_url ) {
				return;
			}

			$args = array(
				'body' => $this->data,
			);

			/**
			 * Filter webhook arguments
			 */
			$args = apply_filters(
				'jet-engine/forms/booking/notification/webhook/request-args', $args, $notification, $this
			);

			$response = wp_remote_post( $webhook_url, $args );

			$this->log[] = true;

			/**
			 * Firtes whe webhook response recieved
			 */
			do_action( 'jet-engine/forms/booking/notification/webhook/response', $response, $notification, $this );

		}

		/**
		 * Insert post notification
		 *
		 * @param  [type] $notification [description]
		 * @return [type]               [description]
		 */
		public function insert_post( $notification ) {

			$post_type = ! empty( $notification['post_type'] ) ? $notification['post_type'] : false;

			if ( ! $post_type || ! post_type_exists( $post_type ) ) {
				return;
			}
Loading ...