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