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-booking   php

Repository URL to install this package:

/ engine-plugin.php

<?php
namespace JET_ABAF;

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

/**
 * Plug-in code into JetEngine
 */
class Engine_Plugin {

	private $done         = false;
	private $deps_added   = false;
	private $booked_dates = array();
	public  $default      = false;

	public function __construct() {

		// Register field for booking form
		add_filter(
			'jet-engine/forms/booking/field-types',
			array( $this, 'register_dates_field' )
		);

		// Regsiter notification for booking form
		add_filter(
			'jet-engine/forms/booking/notification-types',
			array( $this, 'register_booking_notification' )
		);

		add_action(
			'jet-engine/forms/booking/notifications/fields-after',
			array( $this, 'notification_fields' )
		);

		add_filter(
			'jet-engine/calculated-data/ADVANCED_PRICE',
			array( $this, 'macros_advanced_price' ), 10, 2
		);

		add_action(
			'jet-engine/forms/edit-field/before',
			array( $this, 'edit_fields' )
		);

		// Add form field template
		add_action(
			'jet-engine/forms/booking/field-template/check_in_out',
			array( $this, 'field_template' ), 10, 3
		);

		// Register notification handler
		add_filter(
			'jet-engine/forms/booking/notification/apartment_booking',
			array( $this, 'handle_notification' ), 1, 2
		);

		add_filter(
			'jet-engine/forms/gateways/notifications-before',
			array( $this, 'before_form_gateway' ), 1, 2
		);

		add_action(
			'jet-engine/forms/gateways/on-payment-success',
			array( $this, 'on_gateway_success' ), 10, 3
		);

		add_action(
			'jet-engine/forms/editor/macros-list',
			array( $this, 'add_macros_list' ), 10, 0
		);
	}

	/**
	 * Set bookng appointment notification before gateway
	 *
	 * @param  [type] $keep [description]
	 * @param  [type] $all  [description]
	 * @return [type]       [description]
	 */
	public function before_form_gateway( $keep, $all ) {

		foreach ( $all as $index => $notification ) {
			if ( 'apartment_booking' === $notification['type'] && ! in_array( $index, $keep ) ) {
				$keep[] = $index;
			}
		}

		return $keep;

	}

	/**
	 * Finalize booking on internal JetEngine form gateway success
	 *
	 * @return [type] [description]
	 */
	public function on_gateway_success( $form_id, $settings, $form_data ) {

		$booking_id = $form_data['booking_id'];

		if ( $booking_id ) {
			Plugin::instance()->db->update_booking(
				$booking_id,
				array(
					'status' => 'completed',
				)
			);
		}

	}

	/**
	 * Register new dates fields
	 *
	 * @return [type] [description]
	 */
	public function register_dates_field( $fields ) {
		$fields['check_in_out'] = __( 'Check-in/check-out dates' );
		return $fields;
	}

	/**
	 * Register booking notifications
	 *
	 * @param  [type] $notifications [description]
	 * @return [type]                [description]
	 */
	public function register_booking_notification( $notifications ) {
		$notifications['apartment_booking'] = __( 'Apartment booking' );
		return $notifications;
	}

	/**
	 * Macros _advanced_apartment_price processing in the calculator field
	 *
	 */
	public function macros_advanced_price( $macros, $macros_matches ) {
		return $macros;
	}

	/**
	 * Render additional edit fields
	 *
	 * @return [type] [description]
	 */
	public function edit_fields() {
		?>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Layout:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<select type="text" v-model="currentItem.settings.cio_field_layout">
					<option value="single"><?php
						_e( 'Single field', 'jet-engine' );
					?></option>
					<option value="separate"><?php
						_e( 'Separate fields for check in and check out dates', 'jet-engine' );
					?></option>
				</select>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Fields position:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<select type="text" v-model="currentItem.settings.cio_fields_position">
					<option value="inline"><?php
						_e( 'Inline', 'jet-engine' );
					?></option>
					<option value="list"><?php
						_e( 'List', 'jet-engine' );
					?></option>
				</select>
				<div><i>* - For separate fields layout</i></div>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Check In field label:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<input type="text" v-model="currentItem.settings.first_field_label">
				<div><i>* - if you are using separate fields for check in and check out dates,<br> you need to left default "Label" empty and use this option for field label</i></div>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Placeholder:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<input type="text" v-model="currentItem.settings.first_field_placeholder">
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Check Out field label:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<input type="text" placeholder="For separate fields layout" v-model="currentItem.settings.second_field_label">
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Check Out field placeholder:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<input type="text" placeholder="For separate fields layout" v-model="currentItem.settings.second_field_placeholder">
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Date format:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<select type="text" v-model="currentItem.settings.cio_fields_format">
					<option value="YYYY-MM-DD">YYYY-MM-DD</option>
					<option value="MM-DD-YYYY">MM-DD-YYYY</option>
					<option value="DD-MM-YYYY">DD-MM-YYYY</option>
				</select>
				<div><i>* - applies only for date in the form checkin/checkout fields</i></div>
				<div><i>* - for `MM-DD-YYYY` date format use the `/` date separator</i></div>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'Date field separator:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<select type="text" v-model="currentItem.settings.cio_fields_separator">
					<option value="-">-</option>
					<option value=".">.</option>
					<option value="/">/</option>
					<option value="space">Space</option>
				</select>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'check_in_out' === currentItem.settings.type">
			<div class="jet-form-editor__row-label"><?php _e( 'First day of the week:', 'jet-engine' ); ?></div>
			<div class="jet-form-editor__row-control">
				<select type="text" v-model="currentItem.settings.start_of_week">
					<option value="monday"><?php esc_html_e( 'Monday', 'jet-engine' ); ?></option>
					<option value="sunday"><?php esc_html_e( 'Sunday', 'jet-engine' ); ?></option>
				</select>
			</div>
		</div>
		<?php
	}

	/**
	 * Render additional notification fields
	 *
	 * @return [type] [description]
	 */
	public function notification_fields() {
		?>
		<div class="jet-form-editor__row" v-if="'apartment_booking' === currentItem.type">
			<div class="jet-form-editor__row-label"><?php
				_e( 'Apartment ID field:', 'jet-engine' );
			?></div>
			<div class="jet-form-editor__row-control">
				<select v-model="currentItem.booking_apartment_field">
					<option value="">--</option>
					<option v-for="field in availableFields" :value="field">{{ field }}</option>
				</select>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'apartment_booking' === currentItem.type">
			<div class="jet-form-editor__row-label"><?php
				_e( 'Check-in/Check-out date field:', 'jet-engine' );
			?></div>
			<div class="jet-form-editor__row-control">
				<select v-model="currentItem.booking_dates_field">
					<option value="">--</option>
					<option v-for="field in availableFields" :value="field">{{ field }}</option>
				</select>
			</div>
		</div>
		<?php

		$columns = Plugin::instance()->db->get_additional_db_columns();

		if ( $columns ) {
			?>
			<div class="jet-form-editor__row" v-if="'apartment_booking' === currentItem.type">
				<div class="jet-form-editor__row-label">
					<?php _e( 'DB columns map:', 'jet-engine' ); ?>
					<div class="jet-form-editor__row-notice"><?php
						_e( 'Set <i>inserted_post_id</i> to add inserted post ID for Insert Post notification', 'jet-enegine' );
					?></div>
				</div>
				<div class="jet-form-editor__row-fields">
					<?php foreach ( $columns as $column ) {
					?>
					<div class="jet-form-editor__row-map">
						<span><?php echo $column; ?></span>
						<input type="text" v-model="currentItem.db_columns_map_<?php echo $column; ?>">
					</div>
					<?php
					} ?>
				</div>
			</div>
			<?php
		}

		if ( Plugin::instance()->settings->get( 'wc_integration' ) ) {

		?>
		<div class="jet-form-editor__row" v-if="'apartment_booking' === currentItem.type">
			<div class="jet-form-editor__row-label">
				<?php _e( 'WooCommerce Price field:', 'jet-booking' ); ?>
				<div class="jet-form-editor__row-notice"><?php
					_e( 'Select field to get total price from. If not selected price will be get from post meta value.', 'jet-booking' );
				?></div>
			</div>
			<div class="jet-form-editor__row-control">
				<select v-model="currentItem.booking_wc_price">
					<option value="">--</option>
					<option v-for="field in availableFields" :value="field">{{ field }}</option>
				</select>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'apartment_booking' === currentItem.type">
			<div class="jet-form-editor__row-label">
				<?php _e( 'WooCommerce order details:', 'jet-booking' ); ?>
				<div class="jet-form-editor__row-notice"><?php
					_e( 'Set up booking-related info you want to add to the WooCommerce orders and e-mails', 'jet-booking' );
				?></div>
			</div>
			<div class="jet-form-editor__row-control">
				<button type="button" class="button button-secondary" id="jet-booking-wc-details"><?php _e( 'Set up', 'jet-booking' ); ?></button>
			</div>
		</div>
		<div class="jet-form-editor__row" v-if="'apartment_booking' === currentItem.type">
			<div class="jet-form-editor__row-label">
				<?php _e( 'WooCommerce checkout fields map:', 'jet-booking' ); ?>
				<div class="jet-form-editor__row-notice"><?php
					_e( 'Connect WooCommerce checkout fields to appropriate form fields. This allows you to pre-fill WooCommerce checkout fields after redirect to checkout.', 'jet-booking' );
				?></div>
			</div>
			<div class="jet-form-editor__row-fields jet-wc-checkout-fields">
				<?php foreach ( Plugin::instance()->wc->get_checkout_fields() as $field ) {
				?>
				<div class="jet-form-editor__row-map">
					<span><?php echo $field; ?></span>
					<select v-model="currentItem.wc_fields_map__<?php echo $field; ?>">
						<option value="">--</option>
						<option v-for="field in availableFields" :value="field">{{ field }}</option>
					</select>
				</div>
				<?php
				} ?>
			</div>
		</div>
		<?php
		}
Loading ...