| @@ -1,365 +1,221 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Jet_Form_Builder; | |
| 4 | - | |
| 5 | -use Jet_Form_Builder\Blocks\Types\Base as Block_Type_Base; | |
| 6 | -use Jet_Form_Builder\Classes\Attributes_Trait; | |
| 7 | -use Jet_Form_Builder\Classes\Get_Template_Trait; | |
| 8 | - | |
| 9 | -// If this file is called directly, abort. | |
| 10 | -if ( ! defined( 'WPINC' ) ) { | |
| 11 | - die; | |
| 12 | -} | |
| 13 | - | |
| 14 | -/** | |
| 15 | - * Main file | |
| 16 | - */ | |
| 17 | -class Live_Form { | |
| 18 | - | |
| 19 | - use Attributes_Trait; | |
| 20 | - use Get_Template_Trait; | |
| 21 | - | |
| 22 | - public $form_id = false; | |
| 23 | - private $form = false; | |
| 24 | - | |
| 25 | - | |
| 26 | - private $field_name; | |
| 27 | - private $current_field_data; | |
| 28 | - private $start_new_page = true; | |
| 29 | - public $pages = 0; | |
| 30 | - public $rendered_rows = 0; | |
| 31 | - | |
| 32 | - public $is_hidden_row; | |
| 33 | - public $is_submit_row; | |
| 34 | - public $is_page_break_row; | |
| 35 | - | |
| 36 | - public $current_repeater; | |
| 37 | - public $current_repeater_i; | |
| 38 | - public $preset; | |
| 39 | - public $spec_data; | |
| 40 | - | |
| 41 | - public $page = 0; | |
| 42 | - public $has_prev = false; | |
| 43 | - public $post; | |
| 44 | - | |
| 45 | - /** | |
| 46 | - * Instance. | |
| 47 | - * | |
| 48 | - * Holds the plugin instance. | |
| 49 | - * | |
| 50 | - * @since 1.0.0 | |
| 51 | - * @access public | |
| 52 | - * @static | |
| 53 | - * | |
| 54 | - * @var Plugin | |
| 55 | - */ | |
| 56 | - public static $instance = null; | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Instance. | |
| 60 | - * | |
| 61 | - * Ensures only one instance of the plugin class is loaded or can be loaded. | |
| 62 | - * | |
| 63 | - * @return Plugin An instance of the class. | |
| 64 | - * @since 1.0.0 | |
| 65 | - * @access public | |
| 66 | - * @static | |
| 67 | - */ | |
| 68 | - public static function instance() { | |
| 69 | - | |
| 70 | - if ( is_null( self::$instance ) ) { | |
| 71 | - self::$instance = new self(); | |
| 72 | - } | |
| 73 | - | |
| 74 | - return self::$instance; | |
| 75 | - } | |
| 76 | - | |
| 77 | - /** | |
| 78 | - * Create form instance | |
| 79 | - * | |
| 80 | - * @param [type] $form_id [description] | |
| 81 | - */ | |
| 82 | - private function __construct() { | |
| 83 | - $this->post = get_post(); | |
| 84 | - } | |
| 85 | - | |
| 86 | - public function set_form_id( $form_id ) { | |
| 87 | - $this->form_id = $form_id; | |
| 88 | - | |
| 89 | - return $this; | |
| 90 | - } | |
| 91 | - | |
| 92 | - | |
| 93 | - public function set_repeater( $item, $count = false ) { | |
| 94 | - $this->current_repeater = $item; | |
| 95 | - $this->current_repeater_i = $count; | |
| 96 | - | |
| 97 | - return $this; | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * It turns out the inheritance of such an image | |
| 102 | - * Individual Form Arguments (if not) -> General Form Attributes (if not) -> Default Values | |
| 103 | - * | |
| 104 | - * @param array $attributes | |
| 105 | - * | |
| 106 | - * @return $this | |
| 107 | - */ | |
| 108 | - public function set_specific_data_for_render( $attributes = array() ) { | |
| 109 | - $jf_default_args = Plugin::instance()->post_type->get_default_args(); | |
| 110 | - $jf_args = Plugin::instance()->post_type->get_args( $this->form_id ); | |
| 111 | - | |
| 112 | - $spec_data = array_diff( $jf_args, $jf_default_args ); | |
| 113 | - $attributes = array_merge( $jf_default_args, $attributes ); | |
| 114 | - | |
| 115 | - $spec_data = array_merge( $attributes, $spec_data ); | |
| 116 | - | |
| 117 | - $this->spec_data = ( object ) $spec_data; | |
| 118 | - | |
| 119 | - return $this; | |
| 120 | - } | |
| 121 | - | |
| 122 | - public function is_not_field( $block ) { | |
| 123 | - return Plugin::instance()->form->is_not_field( $block['blockName'] ); | |
| 124 | - } | |
| 125 | - | |
| 126 | - public function is_field( $block, $needle ) { | |
| 127 | - return Plugin::instance()->form->is_field( $block['blockName'], $needle ); | |
| 128 | - } | |
| 129 | - | |
| 130 | - /** | |
| 131 | - * Setup fields prop | |
| 132 | - * | |
| 133 | - * @param $blocks | |
| 134 | - */ | |
| 135 | - public function setup_fields( $blocks ) { | |
| 136 | - foreach ( $blocks as $field ) { | |
| 137 | - if ( $this->is_field( $field, 'form-break' ) ) { | |
| 138 | - $this->pages ++; | |
| 139 | - } | |
| 140 | - } | |
| 141 | - } | |
| 142 | - | |
| 143 | - | |
| 144 | - public static function force_render_field( $name, $arguments = array() ) { | |
| 145 | - | |
| 146 | - if ( empty( $name ) ) { | |
| 147 | - return; | |
| 148 | - } | |
| 149 | - $field = jet_form_builder()->blocks->get_field_by_name( $name ); | |
| 150 | - | |
| 151 | - if ( ! $field instanceof Block_Type_Base ) { | |
| 152 | - return; | |
| 153 | - } | |
| 154 | - $field->set_block_data( $arguments ); | |
| 155 | - | |
| 156 | - return $field->get_block_renderer(); | |
| 157 | - } | |
| 158 | - | |
| 159 | - /** | |
| 160 | - * Open form wrapper | |
| 161 | - * | |
| 162 | - * @param $field | |
| 163 | - * | |
| 164 | - * @return false|string [type] [description] | |
| 165 | - */ | |
| 166 | - public function start_form_row( $field ) { | |
| 167 | - | |
| 168 | - if ( $this->is_field( $field, 'hidden' ) ) { | |
| 169 | - return ''; | |
| 170 | - } | |
| 171 | - | |
| 172 | - if ( ! $this->is_hidden_row ) { | |
| 173 | - $this->rendered_rows ++; | |
| 174 | - } | |
| 175 | - | |
| 176 | - ob_start(); | |
| 177 | - | |
| 178 | - do_action( 'jet-form-builder/before-start-form-row', $this ); | |
| 179 | - | |
| 180 | - $this->add_attribute( 'class', 'jet-form-builder-row' ); | |
| 181 | - | |
| 182 | - if ( $this->is_hidden_row ) { | |
| 183 | - $this->add_attribute( 'class', 'jet-form-builder-row--hidden' ); | |
| 184 | - } | |
| 185 | - | |
| 186 | - if ( $this->is_field( $field, 'submit' ) ) { | |
| 187 | - $this->add_attribute( 'class', 'jet-form-builder-row--submit' ); | |
| 188 | - } | |
| 189 | - | |
| 190 | - if ( $this->is_field( $field, 'form-break' ) ) { | |
| 191 | - $this->add_attribute( 'class', 'jet-form-builder-row--page-break' ); | |
| 192 | - } | |
| 193 | - | |
| 194 | - if ( 1 === $this->rendered_rows ) { | |
| 195 | - $this->add_attribute( 'class', 'jet-form-builder-row--first-visible' ); | |
| 196 | - } | |
| 197 | - | |
| 198 | - include $this->get_global_template( 'common/start-form-row.php' ); | |
| 199 | - | |
| 200 | - do_action( 'jet-form-builder/after-start-form-row', $this ); | |
| 201 | - | |
| 202 | - return ob_get_clean(); | |
| 203 | - } | |
| 204 | - | |
| 205 | - /** | |
| 206 | - * Close form wrapper | |
| 207 | - * | |
| 208 | - * @param $field | |
| 209 | - * | |
| 210 | - * @return false|string [type] [description] | |
| 211 | - */ | |
| 212 | - public function end_form_row( $field ) { | |
| 213 | - | |
| 214 | - if ( $this->is_field( $field, 'hidden' ) ) { | |
| 215 | - return ''; | |
| 216 | - } | |
| 217 | - | |
| 218 | - ob_start(); | |
| 219 | - do_action( 'jet-form-builder/before-end-form-row', $this ); | |
| 220 | - | |
| 221 | - include $this->get_global_template( 'common/end-form-row.php' ); | |
| 222 | - | |
| 223 | - do_action( 'jet-form-builder/after-end-form-row', $this ); | |
| 224 | - | |
| 225 | - return ob_get_clean(); | |
| 226 | - } | |
| 227 | - | |
| 228 | - | |
| 229 | - /** | |
| 230 | - * Maybe start new page | |
| 231 | - * | |
| 232 | - * @return false|string|void | |
| 233 | - */ | |
| 234 | - public function maybe_start_page() { | |
| 235 | - | |
| 236 | - if ( 0 >= $this->pages ) { | |
| 237 | - return ''; | |
| 238 | - } | |
| 239 | - | |
| 240 | - if ( false === $this->start_new_page ) { | |
| 241 | - return ''; | |
| 242 | - } | |
| 243 | - | |
| 244 | - $this->start_new_page = false; | |
| 245 | - $this->page ++; | |
| 246 | - | |
| 247 | - ob_start(); | |
| 248 | - do_action( 'jet-form-builder/before-page-start', $this ); | |
| 249 | - | |
| 250 | - $hidden_class = ''; | |
| 251 | - | |
| 252 | - if ( 1 < $this->page ) { | |
| 253 | - $hidden_class = 'jet-form-page--hidden'; | |
| 254 | - } | |
| 255 | - | |
| 256 | - include $this->get_global_template( 'common/start-page.php' ); | |
| 257 | - | |
| 258 | - do_action( 'jet-form-builder/after-page-start', $this ); | |
| 259 | - | |
| 260 | - return ob_get_clean(); | |
| 261 | - } | |
| 262 | - | |
| 263 | - /** | |
| 264 | - * Maybe start new page | |
| 265 | - * | |
| 266 | - * @param $is_last | |
| 267 | - * @param $field | |
| 268 | - * | |
| 269 | - * @return false|string|void [type] [description] | |
| 270 | - */ | |
| 271 | - public function maybe_end_page( $is_last = false, $field = false ) { | |
| 272 | - | |
| 273 | - if ( 0 >= $this->pages ) { | |
| 274 | - return; | |
| 275 | - } | |
| 276 | - | |
| 277 | - if ( ! $is_last && ! $this->is_field( $field, 'form-break' ) ) { | |
| 278 | - return; | |
| 279 | - } | |
| 280 | - | |
| 281 | - $this->start_new_page = true; | |
| 282 | - $this->has_prev = true; | |
| 283 | - | |
| 284 | - ob_start(); | |
| 285 | - do_action( 'jet-form-builder/before-page-end', $this ); | |
| 286 | - | |
| 287 | - include $this->get_global_template( 'common/end-page.php' ); | |
| 288 | - | |
| 289 | - do_action( 'jet-form-builder/after-page-end', $this ); | |
| 290 | - | |
| 291 | - return ob_get_clean(); | |
| 292 | - } | |
| 293 | - | |
| 294 | - /** | |
| 295 | - * Returns true if field is visible | |
| 296 | - * | |
| 297 | - * @param $field | |
| 298 | - * | |
| 299 | - * @return boolean [description] | |
| 300 | - */ | |
| 301 | - public function is_field_visible( $field ) { | |
| 302 | - | |
| 303 | - // For backward compatibility and hidden fields | |
| 304 | - if ( empty( $field['visibility'] ) ) { | |
| 305 | - return true; | |
| 306 | - } | |
| 307 | - | |
| 308 | - // If is visible for all - show field | |
| 309 | - if ( 'all' === $field['visibility'] ) { | |
| 310 | - return true; | |
| 311 | - } | |
| 312 | - | |
| 313 | - // If is visible for logged in users and user is logged in - show field | |
| 314 | - if ( 'logged_id' === $field['visibility'] && is_user_logged_in() ) { | |
| 315 | - return true; | |
| 316 | - } | |
| 317 | - | |
| 318 | - // If is visible for not logged in users and user is not logged in - show field | |
| 319 | - if ( 'not_logged_in' === $field['visibility'] && ! is_user_logged_in() ) { | |
| 320 | - return true; | |
| 321 | - } | |
| 322 | - | |
| 323 | - return false; | |
| 324 | - | |
| 325 | - } | |
| 326 | - | |
| 327 | - /** | |
| 328 | - * Returns field ID with repeater prefix if needed | |
| 329 | - */ | |
| 330 | - public function get_field_id( $name ) { | |
| 331 | - | |
| 332 | - if ( is_array( $name ) ) { | |
| 333 | - $name = $name['name']; | |
| 334 | - } | |
| 335 | - //Find some solution for the repeater field | |
| 336 | - | |
| 337 | - if ( $this->current_repeater ) { | |
| 338 | - $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : 'repeater'; | |
| 339 | - $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__'; | |
| 340 | - $name = sprintf( '%1$s_%2$s_%3$s', $repeater_name, $index, $name ); | |
| 341 | - } | |
| 342 | - | |
| 343 | - return $name; | |
| 344 | - } | |
| 345 | - | |
| 346 | - /** | |
| 347 | - * Returns field name with repeater prefix if needed | |
| 348 | - */ | |
| 349 | - public function get_field_name( $name ) { | |
| 350 | - | |
| 351 | - //Find some solution for the repeater field | |
| 352 | - if ( $this->current_repeater ) { | |
| 353 | - | |
| 354 | - $repeater_name = ! empty( $this->current_repeater['name'] ) ? $this->current_repeater['name'] : 'repeater'; | |
| 355 | - $index = ( false !== $this->current_repeater_i ) ? $this->current_repeater_i : '__i__'; | |
| 356 | - | |
| 357 | - $name = sprintf( '%1$s[%2$s][%3$s]', $repeater_name, $index, $name ); | |
| 358 | - } | |
| 359 | - | |
| 360 | - return $name; | |
| 361 | - | |
| 362 | - } | |
| 363 | - | |
| 364 | - | |
| 365 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Jet_Form_Builder; | |
| 4 | + | |
| 5 | +use Jet_Form_Builder\Blocks\Block_Helper; | |
| 6 | +use Jet_Form_Builder\Blocks\Types\Base as Block_Type_Base; | |
| 7 | +use Jet_Form_Builder\Classes\Arguments\Form_Arguments; | |
| 8 | +use Jet_Form_Builder\Classes\Attributes_Trait; | |
| 9 | +use Jet_Form_Builder\Classes\Get_Template_Trait; | |
| 10 | +use Jet_Form_Builder\Classes\Instance_Trait; | |
| 11 | + | |
| 12 | +// If this file is called directly, abort. | |
| 13 | +if ( ! defined( 'WPINC' ) ) { | |
| 14 | + die; | |
| 15 | +} | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * @method static Live_Form instance() | |
| 19 | + * | |
| 20 | + * Class Live_Form | |
| 21 | + * @package Jet_Form_Builder | |
| 22 | + */ | |
| 23 | +class Live_Form { | |
| 24 | + | |
| 25 | + use Attributes_Trait; | |
| 26 | + use Get_Template_Trait; | |
| 27 | + use Instance_Trait; | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * @var bool|int | |
| 31 | + */ | |
| 32 | + public $form_id = false; | |
| 33 | + /** @var Form_Arguments */ | |
| 34 | + public $spec_data; | |
| 35 | + // phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore | |
| 36 | + public $_conditional_blocks = array(); | |
| 37 | + public $blocks = array(); | |
| 38 | + // phpcs:enable PSR2.Classes.PropertyDeclaration.Underscore | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * @var Form_Break | |
| 42 | + */ | |
| 43 | + public $form_break; | |
| 44 | + public $field_names = array(); | |
| 45 | + | |
| 46 | + private $ajax_post_id; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * Create form instance | |
| 50 | + * | |
| 51 | + * @param [type] $form_id [description] | |
| 52 | + */ | |
| 53 | + private function __construct() { | |
| 54 | + $this->spec_data = new Form_Arguments(); | |
| 55 | + | |
| 56 | + add_filter( | |
| 57 | + 'jet-form-builder/setup-blocks', | |
| 58 | + array( $this, 'remove_extra_page_breaks' ) | |
| 59 | + ); | |
| 60 | + add_filter( | |
| 61 | + 'jet-form-builder/after-end-form', | |
| 62 | + array( self::class, 'on_end_render_form' ) | |
| 63 | + ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + public static function on_end_render_form( string $content ): string { | |
| 67 | + self::clear(); | |
| 68 | + | |
| 69 | + return $content; | |
| 70 | + } | |
| 71 | + | |
| 72 | + public static function clear() { | |
| 73 | + remove_filter( | |
| 74 | + 'jet-form-builder/setup-blocks', | |
| 75 | + array( self::$instance, 'remove_extra_page_breaks' ) | |
| 76 | + ); | |
| 77 | + remove_filter( | |
| 78 | + 'jet-form-builder/after-end-form', | |
| 79 | + array( self::class, 'on_end_render_form' ) | |
| 80 | + ); | |
| 81 | + | |
| 82 | + self::$instance = null; | |
| 83 | + } | |
| 84 | + | |
| 85 | + public function set_form_id( $form_id ) { | |
| 86 | + $this->form_id = $form_id; | |
| 87 | + | |
| 88 | + return $this; | |
| 89 | + } | |
| 90 | + | |
| 91 | + /** | |
| 92 | + * @param array $incoming_attributes | |
| 93 | + * | |
| 94 | + * @return $this | |
| 95 | + */ | |
| 96 | + public function set_specific_data_for_render( $incoming_attributes = array() ) { | |
| 97 | + $this->spec_data | |
| 98 | + ->set_form_id( $this->form_id ) | |
| 99 | + ->load( $incoming_attributes ); | |
| 100 | + | |
| 101 | + return $this; | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Setup fields prop | |
| 106 | + * | |
| 107 | + * @return array[] | |
| 108 | + */ | |
| 109 | + public function setup_fields(): array { | |
| 110 | + $this->blocks = apply_filters( | |
| 111 | + 'jet-form-builder/setup-blocks', | |
| 112 | + Block_Helper::get_blocks_by_post( $this->form_id, true, true ) | |
| 113 | + ); | |
| 114 | + | |
| 115 | + jet_form_builder()->regexp->set_field_names( | |
| 116 | + Block_Helper::get_form_field_names( $this->blocks ) | |
| 117 | + ); | |
| 118 | + | |
| 119 | + return $this->blocks; | |
| 120 | + } | |
| 121 | + | |
| 122 | + public function remove_extra_page_breaks( array $blocks ) { | |
| 123 | + return $this->get_form_break()->set_pages( $blocks ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + public function maybe_progress_pages() { | |
| 127 | + if ( ! $this->spec_data->enable_progress || 0 === $this->get_form_break()->get_pages() ) { | |
| 128 | + return ''; | |
| 129 | + } | |
| 130 | + | |
| 131 | + return $this->get_form_break()->render_progress( | |
| 132 | + 'default', | |
| 133 | + array( | |
| 134 | + 'jet-form-builder-progress-pages--global', | |
| 135 | + ) | |
| 136 | + ); | |
| 137 | + } | |
| 138 | + | |
| 139 | + | |
| 140 | + public static function force_render_field( $name, $arguments = array() ) { | |
| 141 | + if ( empty( $name ) ) { | |
| 142 | + return ''; | |
| 143 | + } | |
| 144 | + $field = jet_form_builder()->blocks->get_field_by_name( $name ); | |
| 145 | + | |
| 146 | + if ( ! $field instanceof Block_Type_Base ) { | |
| 147 | + return ''; | |
| 148 | + } | |
| 149 | + $arguments['_static_value'] = true; | |
| 150 | + | |
| 151 | + $field->set_block_data( $arguments ); | |
| 152 | + | |
| 153 | + return $field->get_block_renderer(); | |
| 154 | + } | |
| 155 | + | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Maybe start new page | |
| 159 | + * | |
| 160 | + * @param bool $force_first | |
| 161 | + * | |
| 162 | + * @return false|string|void | |
| 163 | + */ | |
| 164 | + public function maybe_start_page( $force_first = false ) { | |
| 165 | + return $this->get_form_break()->maybe_start_page( $force_first ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * Maybe start new page | |
| 170 | + * | |
| 171 | + * @param $is_last | |
| 172 | + * @param $field | |
| 173 | + * | |
| 174 | + * @return false|string|void [type] [description] | |
| 175 | + */ | |
| 176 | + public function maybe_end_page( $is_last = false, $field = false ) { | |
| 177 | + return $this->get_form_break()->maybe_end_page( $is_last, $field ); | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * @param string $name | |
| 182 | + * | |
| 183 | + * @return Form_Break | |
| 184 | + */ | |
| 185 | + public function get_form_break( $name = '' ) { | |
| 186 | + if ( ! $name && ! $this->form_break ) { | |
| 187 | + $this->form_break = new Form_Break(); | |
| 188 | + } | |
| 189 | + if ( $name && ! $this->isset_form_break( $name ) ) { | |
| 190 | + $this->_conditional_blocks[ $name ] = array( 'break' => new Form_Break() ); | |
| 191 | + } | |
| 192 | + | |
| 193 | + return $name ? $this->_conditional_blocks[ $name ]['break'] : $this->form_break; | |
| 194 | + } | |
| 195 | + | |
| 196 | + public function isset_form_break( $name ) { | |
| 197 | + return isset( $this->_conditional_blocks[ $name ] ); | |
| 198 | + } | |
| 199 | + | |
| 200 | + public function __get( string $name ) { | |
| 201 | + switch ( $name ) { | |
| 202 | + case 'post': | |
| 203 | + return $this->current_post(); | |
| 204 | + default: | |
| 205 | + return null; | |
| 206 | + } | |
| 207 | + } | |
| 208 | + | |
| 209 | + private function current_post() { | |
| 210 | + global $post; | |
| 211 | + | |
| 212 | + if ( wp_doing_ajax() && empty( $post->ID ) && ! $this->ajax_post_id ) { | |
| 213 | + $url = wp_get_referer(); | |
| 214 | + | |
| 215 | + $this->ajax_post_id = url_to_postid( $url ); | |
| 216 | + } | |
| 217 | + | |
| 218 | + return $this->ajax_post_id ? get_post( $this->ajax_post_id ) : $post; | |
| 219 | + } | |
| 220 | + | |
| 221 | +} | |