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 / builder.php

<?php
/**
 * Form builder class
 */

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

if ( ! class_exists( 'Jet_Engine_Booking_Forms_Builder' ) ) {

	/**
	 * Define Jet_Engine_Booking_Forms_Builder class
	 */
	class Jet_Engine_Booking_Forms_Builder {

		public $form_id            = null;
		public $post               = null;
		public $fields             = array();
		public $fields_settings    = array();
		public $args               = array();
		public $settings           = array();
		public $attrs              = array();
		public $rows               = array();
		public $captcha            = false;
		public $preset             = false;
		public $is_hidden_row      = true;
		public $is_submit_row      = false;
		public $is_page_break_row  = false;
		public $rendered_rows      = 0;
		public $pages              = 0;
		public $page               = 0;
		public $has_prev           = false;
		public $start_new_page     = true;
		public $manager            = null;
		public $current_repeater   = false;
		public $current_repeater_i = false;

		/**
		 * Constructor for the class
		 */
		function __construct( $form_id = null, $fields = false, $args = array(), $captcha = false ) {

			if ( ! $form_id ) {
				return;
			}

			$this->form_id = $form_id;

			$this->setup_fields( $fields );

			$this->args = wp_parse_args( $args, array(
				'fields_layout' => 'column',
				'label_tag'     => 'div',
				'rows_divider'  => false,
				'required_mark' => '*',
				'submit_type'   => 'reload',
			) );

			if ( empty( $post ) ) {
				global $post;
			}

			$this->post    = $post;
			$this->captcha = $captcha;
			$this->preset  = new Jet_Engine_Booking_Forms_Preset( $this->form_id );

		}

		/**
		 * Set manager instance
		 *
		 * @param [type] $manager [description]
		 */
		public function set_manager( $manager ) {
			$this->manager = $manager;
		}

		/**
		 * Setup fields prop
		 */
		public function setup_fields( $fields = false ) {

			$raw_fields = '';

			if ( $fields ) {
				$raw_fields = $fields;
			} else {
				$raw_fields = get_post_meta( $this->form_id, '_form_data', true );
				$raw_fields = json_decode( wp_unslash( $raw_fields ), true );
			}

			if ( empty( $raw_fields ) ) {
				return;
			}

			// Ensure fields sorted by rows
			usort( $raw_fields, function( $a, $b ) {

				if ( $a['y'] == $b['y'] ) {
					return 0;
				}
				return ( $a['y'] < $b['y'] ) ? -1 : 1;

			} );

			$repeater_index = false;

			foreach ( $raw_fields as $index => $field ) {

				if ( $this->is_repeater_start( $field['settings'] ) ) {
					$repeater_index = $index;
					continue;
				}

				if ( $this->is_repeater_end( $field['settings'] ) ) {
					$repeater_index = false;
					unset( $raw_fields[ $index ] );
					continue;
				}

				if ( false !== $repeater_index ) {

					if ( empty( $raw_fields[ $repeater_index ]['settings']['repeater_fields'] ) ) {
						$raw_fields[ $repeater_index ]['settings']['repeater_fields'] = array();
					}

					$raw_fields[ $repeater_index ]['settings']['repeater_fields'][] = $field;
					unset( $raw_fields[ $index ] );
				}

			}

			$this->fields = $raw_fields;
			$this->fields_settings = wp_list_pluck( $raw_fields, 'settings' );

			$this->rows = $this->get_sorted_fields( $raw_fields );

		}

		/**
		 * Public function get sorted form fields
		 */
		public function get_sorted_fields( $raw_fields = array() ) {

			$sorted = array();
			$y      = false;

			foreach ( $raw_fields as $field ) {

				$is_page_break = ! empty( $field['settings']['is_page_break'] ) ? true : false;

				if ( $is_page_break ) {
					$this->pages++;
				}

				if ( false === $y ) {
					$y = $field['y'];
				}

				if ( $field['y'] === $y ) {

					if ( empty( $sorted[ $y ] ) ) {
						$sorted[ $y ] = array();
					}

					$sorted[ $y ][] = $field;

				} else {

					usort( $sorted[ $y ], function( $a, $b ) {

						if ( $a['x'] == $b['x'] ) {
							return 0;
						}
						return ( $a['x'] < $b['x'] ) ? -1 : 1;

					} );

					$y = $field['y'];

					$sorted[ $y ][] = $field;

				}

			}

			// Ensure last row is sorted
			usort( $sorted[ $y ], function( $a, $b ) {

				if ( $a['x'] == $b['x'] ) {
					return 0;
				}
				return ( $a['x'] < $b['x'] ) ? -1 : 1;

			} );

			return $sorted;
		}

		public function get_author_meta( $key ) {

			$post_id = get_the_ID();

			if ( ! $post_id ) {
				return null;
			}

			global $authordata;

			if ( $authordata ) {
				return get_the_author_meta( $key );
			}

			$post = get_post( $post_id );

			if ( ! $post ) {
				return null;
			}

			return get_the_author_meta( $key, $post->post_author );

		}

		/**
		 * Get hidden value
		 *
		 * @return string
		 */
		public function get_hidden_val( $args = array() ) {

			$from = isset( $args['hidden_value'] ) ? $args['hidden_value'] : '';

			switch ( $from ) {

				case 'post_id':
					if ( ! $this->post ) {
						return null;
					} else {
						return $this->post->ID;
					}

				case 'post_title':
					if ( ! $this->post ) {
						return null;
					} else {
						return get_the_title( $this->post->ID );
					}

				case 'post_url':
					if ( ! $this->post ) {
						return null;
					} else {
						return get_permalink( $this->post->ID );
					}

				case 'post_meta':

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

					$key = ! empty( $args['hidden_value_field'] ) ? $args['hidden_value_field'] : '';

					if ( ! $key ) {
						return null;
					} else {
						return get_post_meta( $this->post->ID, $key, true );
					}

				case 'query_var':

					$key = ! empty( $args['query_var_key'] ) ? $args['query_var_key'] : '';

					if ( ! $key ) {
						return null;
					} else {
						return isset( $_GET[ $key ] ) ? esc_attr( $_GET[ $key ] ) : null;
					}

				case 'user_id':
					if ( ! is_user_logged_in() ) {
						return null;
					} else {
						return get_current_user_id();
					}

				case 'user_email':
					if ( ! is_user_logged_in() ) {
						return null;
					} else {
						$user = wp_get_current_user();
						return $user->user_email;
					}

				case 'user_name':
					if ( ! is_user_logged_in() ) {
						return null;
					} else {
						$user = wp_get_current_user();
						return $user->display_name;
					}

				case 'user_meta':

					$key = ! empty( $args['hidden_value_field'] ) ? $args['hidden_value_field'] : '';

					if ( ! $key ) {
						return null;
					}

					if ( ! is_user_logged_in() ) {
						return null;
					} else {
						return get_user_meta( get_current_user_id(), $key, true );
					}

				case 'author_id':

					return $this->get_author_meta( 'ID' );

				case 'author_email':

					return $this->get_author_meta( 'user_email' );

				case 'author_name':

					return $this->get_author_meta( 'display_name' );

				case 'current_date':

					$format = ! empty( $args['date_format'] ) ? $args['date_format'] : get_option( 'date_format' );

					return date_i18n( $format );

				case 'manual_input':
					return ! empty( $args['default'] ) ? $args['default'] : 0;

				default:

					$value = ! empty( $args['default'] ) ? $args['default'] : '';
					return apply_filters( 'jet-engine/forms/hidden-value/' . $from, $value );

			}
Loading ...