actions
1 year ago
addons
11 months ago
admin
8 months ago
blocks
11 months ago
classes
7 months ago
db-queries
8 months ago
exceptions
2 years ago
form-messages
1 year ago
form-patterns
2 years ago
form-response
2 years ago
generators
2 years ago
migrations
7 months ago
presets
11 months ago
request
2 years ago
autoloader.php
1 year ago
file-upload.php
2 years ago
form-break.php
2 years ago
form-handler.php
11 months ago
form-manager.php
2 years ago
functions.php
2 years ago
live-form.php
2 years ago
plugin.php
2 years ago
form-break.php
287 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder; |
| 5 | |
| 6 | use Jet_Form_Builder\Blocks\Block_Helper; |
| 7 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 8 | use Jet_Form_Builder\Classes\Tools; |
| 9 | |
| 10 | // If this file is called directly, abort. |
| 11 | if ( ! defined( 'WPINC' ) ) { |
| 12 | die; |
| 13 | } |
| 14 | |
| 15 | class Form_Break { |
| 16 | |
| 17 | use Get_Template_Trait; |
| 18 | |
| 19 | private $pages = 0; |
| 20 | private $form_breaks = array(); |
| 21 | private $count_form_breaks = null; |
| 22 | private $current_page = 1; |
| 23 | private $current_form_break = 0; |
| 24 | private $is_editor = false; |
| 25 | private $progress_type = 'default'; |
| 26 | private $has_start = false; |
| 27 | |
| 28 | /** |
| 29 | * @since 3.0.1 |
| 30 | * |
| 31 | * @var int |
| 32 | */ |
| 33 | private $page_offset = 75; |
| 34 | |
| 35 | public function get_pages() { |
| 36 | return $this->pages; |
| 37 | } |
| 38 | |
| 39 | public function get_breaks() { |
| 40 | return $this->form_breaks; |
| 41 | } |
| 42 | |
| 43 | public function get_count_breaks() { |
| 44 | if ( is_null( $this->count_form_breaks ) ) { |
| 45 | $this->count_form_breaks = count( $this->form_breaks ); |
| 46 | } |
| 47 | |
| 48 | return $this->count_form_breaks; |
| 49 | } |
| 50 | |
| 51 | public function get_current() { |
| 52 | return $this->current_page; |
| 53 | } |
| 54 | |
| 55 | public function set_progress_type( $type ) { |
| 56 | if ( ! is_string( $type ) ) { |
| 57 | return; |
| 58 | } |
| 59 | $this->progress_type = $type; |
| 60 | } |
| 61 | |
| 62 | public function set_editor_mode( $is_editor ) { |
| 63 | if ( ! $is_editor ) { |
| 64 | $this->is_editor = false; |
| 65 | $this->pages = 0; |
| 66 | $this->form_breaks = array(); |
| 67 | $this->count_form_breaks = 0; |
| 68 | |
| 69 | return $this; |
| 70 | } |
| 71 | |
| 72 | $this->is_editor = true; |
| 73 | $this->pages = 2; |
| 74 | $this->form_breaks = array( |
| 75 | array( |
| 76 | 'label' => 'Start Page', |
| 77 | ), |
| 78 | array( |
| 79 | 'label' => 'Second Page', |
| 80 | ), |
| 81 | array( |
| 82 | 'label' => 'Last Page', |
| 83 | ), |
| 84 | ); |
| 85 | |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | public function add_progress( $last_progress ) { |
| 90 | $this->form_breaks[] = $last_progress; |
| 91 | |
| 92 | return $this; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | public function set_pages( $blocks, $last_page_from_blocks = true ) { |
| 97 | $count_blocks = count( $blocks ); |
| 98 | $last_break = false; |
| 99 | |
| 100 | foreach ( $blocks as $index => $field ) { |
| 101 | if ( 'core/block' === $field['blockName'] ) { |
| 102 | $this->set_pages( $field['innerBlocks'] ?? array(), false ); |
| 103 | |
| 104 | continue; |
| 105 | } |
| 106 | if ( 'form-break-start' === Block_Helper::delete_namespace( $field ) ) { |
| 107 | $this->has_start = true; |
| 108 | |
| 109 | continue; |
| 110 | |
| 111 | } elseif ( 'form-break-field' !== Block_Helper::delete_namespace( $field ) ) { |
| 112 | continue; |
| 113 | } |
| 114 | $form_break = Plugin::instance()->blocks->get_field_attrs( $field['blockName'], $field['attrs'] ); |
| 115 | |
| 116 | if ( $last_page_from_blocks && $index + 1 === $count_blocks ) { |
| 117 | $last_break = $form_break; |
| 118 | unset( $blocks[ $index ] ); |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | ++$this->pages; |
| 123 | $this->form_breaks[] = $form_break; |
| 124 | } |
| 125 | if ( $last_page_from_blocks && ! empty( $this->form_breaks ) ) { |
| 126 | // phpcs:ignore Universal.Operators.DisallowShortTernary.Found |
| 127 | $this->form_breaks[] = $last_break ?: array( 'label' => __( 'Last Page', 'jet-form-builder' ) ); |
| 128 | } |
| 129 | |
| 130 | return $blocks; |
| 131 | } |
| 132 | |
| 133 | public function with_progress_wrapper( $content, $additional_classes = array() ) { |
| 134 | $type = $this->progress_type ?: 'default'; |
| 135 | |
| 136 | $classes = array_merge( |
| 137 | array( |
| 138 | 'jet-form-builder-progress-pages', |
| 139 | "jfb-progress-type--$type", |
| 140 | ), |
| 141 | $additional_classes |
| 142 | ); |
| 143 | |
| 144 | return sprintf( |
| 145 | '<div class="%1$s" data-type="%2$s">%3$s</div>', |
| 146 | implode( ' ', $classes ), |
| 147 | $type, |
| 148 | $content |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | public function render_default_progress() { |
| 153 | ob_start(); |
| 154 | include Tools::get_global_template( 'fields/progress-bar.php' ); |
| 155 | |
| 156 | return ob_get_clean(); |
| 157 | } |
| 158 | |
| 159 | public function render_progress( $type = '', $additional_classes = array() ) { |
| 160 | if ( $type ) { |
| 161 | $this->set_progress_type( $type ); |
| 162 | } |
| 163 | |
| 164 | switch ( $this->progress_type ) { |
| 165 | case 'default': |
| 166 | $content = $this->render_default_progress(); |
| 167 | break; |
| 168 | default: |
| 169 | $content = apply_filters( "jet-form-builder/render/progress-bar/$this->progress_type", '', $this ); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return $this->with_progress_wrapper( $content, $additional_classes ); |
| 174 | } |
| 175 | |
| 176 | public function get_progress_item_class( $index ) { |
| 177 | $classes = array( 'jet-form-builder-progress-pages__item--wrapper' ); |
| 178 | |
| 179 | if ( $this->is_editor ) { |
| 180 | if ( 0 === $index ) { |
| 181 | $classes[] = 'passed-page'; |
| 182 | } |
| 183 | if ( 1 === $index ) { |
| 184 | $classes[] = 'active-page'; |
| 185 | } |
| 186 | } elseif ( $index === $this->current_form_break ) { |
| 187 | $classes[] = 'active-page'; |
| 188 | } elseif ( $index < $this->current_form_break ) { |
| 189 | $classes[] = 'passed-page'; |
| 190 | } |
| 191 | |
| 192 | return implode( ' ', $classes ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Maybe start new page |
| 197 | * |
| 198 | * @param bool $force_first |
| 199 | * |
| 200 | * @return string |
| 201 | */ |
| 202 | public function maybe_start_page( $force_first = false ) { |
| 203 | if ( 0 >= $this->pages || $this->is_has_start() ) { |
| 204 | return ''; |
| 205 | } |
| 206 | |
| 207 | if ( $force_first ) { |
| 208 | $this->current_page = 1; |
| 209 | $this->current_form_break = 0; |
| 210 | } else { |
| 211 | ++$this->current_page; |
| 212 | ++$this->current_form_break; |
| 213 | } |
| 214 | |
| 215 | ob_start(); |
| 216 | do_action( 'jet-form-builder/before-page-start', $this ); |
| 217 | |
| 218 | $hidden_class = ''; |
| 219 | |
| 220 | if ( 1 < $this->current_page ) { |
| 221 | $hidden_class = 'jet-form-builder-page--hidden'; |
| 222 | } |
| 223 | |
| 224 | include Tools::get_global_template( 'common/start-page.php' ); |
| 225 | |
| 226 | do_action( 'jet-form-builder/after-page-start', $this ); |
| 227 | |
| 228 | return ob_get_clean(); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Maybe start new page |
| 233 | * |
| 234 | * @param $is_last |
| 235 | * @param $field |
| 236 | * |
| 237 | * @return string |
| 238 | */ |
| 239 | public function maybe_end_page( $is_last = false, $field = false ) { |
| 240 | if ( 0 >= $this->pages ) { |
| 241 | return ''; |
| 242 | } |
| 243 | |
| 244 | if ( ! $is_last && 'form-break-field' !== Block_Helper::delete_namespace( $field ) ) { |
| 245 | return ''; |
| 246 | } |
| 247 | |
| 248 | ob_start(); |
| 249 | do_action( 'jet-form-builder/before-page-end', $this ); |
| 250 | |
| 251 | include Tools::get_global_template( 'common/end-page.php' ); |
| 252 | |
| 253 | do_action( 'jet-form-builder/after-page-end', $this ); |
| 254 | |
| 255 | return ob_get_clean(); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @return bool |
| 260 | */ |
| 261 | public function is_has_start(): bool { |
| 262 | $has_start = $this->has_start; |
| 263 | $this->has_start = false; |
| 264 | |
| 265 | return $has_start; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * @since 3.0.1 |
| 270 | * |
| 271 | * @return int |
| 272 | */ |
| 273 | public function get_page_offset(): int { |
| 274 | return $this->page_offset; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * @since 3.0.1 |
| 279 | * |
| 280 | * @param int $offset |
| 281 | */ |
| 282 | public function set_page_offset( int $offset ) { |
| 283 | $this->page_offset = $offset; |
| 284 | } |
| 285 | |
| 286 | } |
| 287 |