| 1 |
<?php |
| 2 |
|
| 3 |
namespace JFB_Modules\Block_Parsers; |
| 4 |
|
| 5 |
use Jet_Form_Builder\Actions\Events\Bad_Request\Bad_Request_Event; |
| 6 |
use Jet_Form_Builder\Exceptions\Action_Exception; |
| 7 |
use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 8 |
use Jet_Form_Builder\Exceptions\Request_Exception; |
| 9 |
use Jet_Form_Builder\Live_Form; |
| 10 |
use Jet_Form_Builder\Request\Exceptions\Sanitize_Value_Exception; |
| 11 |
use Jet_Form_Builder\Request\Parser_Context; |
| 12 |
use Jet_Form_Builder\Request\Request_Tools; |
| 13 |
use JFB_Components\Module\Base_Module_Dir_It; |
| 14 |
use JFB_Components\Module\Base_Module_Dir_Trait; |
| 15 |
use JFB_Components\Module\Base_Module_Static_Instance_It; |
| 16 |
use JFB_Components\Module\Base_Module_Static_Instance_Trait; |
| 17 |
use JFB_Components\Module\Base_Module_After_Install_It; |
| 18 |
use JFB_Components\Module\Base_Module_It; |
| 19 |
use Jet_Form_Builder\Blocks\Block_Helper; |
| 20 |
use JFB_Components\Repository\Repository_Pattern_Trait; |
| 21 |
use JFB_Modules\Security\Exceptions\Spam_Exception; |
| 22 |
|
| 23 |
// If this file is called directly, abort. |
| 24 |
if ( ! defined( 'WPINC' ) ) { |
| 25 |
die; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @method static Module instance() |
| 30 |
* |
| 31 |
* @since 3.1.0 |
| 32 |
* |
| 33 |
* Class Module |
| 34 |
* @package Jet_Form_Builder\Blocks\Block_Sanitizer |
| 35 |
*/ |
| 36 |
final class Module implements |
| 37 |
Base_Module_It, |
| 38 |
Base_Module_After_Install_It, |
| 39 |
Base_Module_Static_Instance_It, |
| 40 |
Base_Module_Dir_It { |
| 41 |
|
| 42 |
const EMPTY_BLOCK_ERROR = '0'; |
| 43 |
const NOT_FIELD_HAS_INNER = '1'; |
| 44 |
const FIELD_HAS_INNER = '2'; |
| 45 |
const IS_CONDITIONAL = '3'; |
| 46 |
|
| 47 |
use Repository_Pattern_Trait; |
| 48 |
use Base_Module_Static_Instance_Trait; |
| 49 |
use Base_Module_Dir_Trait; |
| 50 |
|
| 51 |
private $context; |
| 52 |
|
| 53 |
public static function get_instance_id(): string { |
| 54 |
return 'block-parsers'; |
| 55 |
} |
| 56 |
|
| 57 |
public function condition(): bool { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
public function on_install() { |
| 62 |
require_once $this->get_dir( 'parser-context.php' ); |
| 63 |
|
| 64 |
$this->rep_install(); |
| 65 |
$this->context = new Parser_Context(); |
| 66 |
} |
| 67 |
|
| 68 |
public function on_uninstall() { |
| 69 |
$this->rep_clear(); |
| 70 |
unset( $this->context ); |
| 71 |
} |
| 72 |
|
| 73 |
public function init_hooks() { |
| 74 |
add_action( 'jet-form-builder/request', array( $this, 'init_request' ), 0 ); |
| 75 |
} |
| 76 |
|
| 77 |
public function remove_hooks() { |
| 78 |
remove_action( 'jet-form-builder/request', array( $this, 'init_request' ), 0 ); |
| 79 |
} |
| 80 |
|
| 81 |
public function rep_instances(): array { |
| 82 |
|
| 83 |
return apply_filters( |
| 84 |
'jet-form-builder/parsers-request/register', |
| 85 |
array( |
| 86 |
new Fields\Default_Parser(), |
| 87 |
new Fields\Date_Field_Parser(), |
| 88 |
new Fields\Wysiwyg_Field_Parser(), |
| 89 |
new Fields\Text_Field_Parser(), |
| 90 |
new Fields\Repeater_Field_Parser(), |
| 91 |
new Fields\Media_Field_Parser(), |
| 92 |
new Fields\Datetime_Field_Parser(), |
| 93 |
new Fields\Hidden_Field_Parser(), |
| 94 |
) |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @throws Action_Exception|Request_Exception |
| 100 |
*/ |
| 101 |
public function init_request() { |
| 102 |
|
| 103 |
do_action( 'jet-form-builder/request-handler/before-init', $this ); |
| 104 |
|
| 105 |
Live_Form::instance() |
| 106 |
->set_form_id( jet_fb_handler()->get_form_id() ) |
| 107 |
->set_specific_data_for_render(); |
| 108 |
|
| 109 |
/** |
| 110 |
* @throws Spam_Exception |
| 111 |
*/ |
| 112 |
$request = apply_filters( 'jet-form-builder/request-handler/request', Request_Tools::get_request() ); |
| 113 |
|
| 114 |
$this->get_context() |
| 115 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 116 |
->set_files( Request_Tools::get_files( $_FILES ) ) |
| 117 |
->set_request( $request ); |
| 118 |
|
| 119 |
$this->get_context()->apply( jet_fb_request_handler()->_fields ); |
| 120 |
|
| 121 |
$this->get_context()->update_request( |
| 122 |
apply_filters_deprecated( |
| 123 |
'jet-form-builder/form-handler/form-data', |
| 124 |
array( $this->get_context()->resolve_request( false ) ), |
| 125 |
'3.1.0', |
| 126 |
'jet-form-builder/request' |
| 127 |
), |
| 128 |
array() |
| 129 |
); |
| 130 |
|
| 131 |
$errors = iterator_to_array( $this->get_context()->iterate_errors_list() ); |
| 132 |
|
| 133 |
if ( empty( $errors ) ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// log errors |
| 138 |
new Sanitize_Value_Exception( 'has_errors', $errors ); |
| 139 |
|
| 140 |
jet_fb_events()->execute( Bad_Request_Event::class ); |
| 141 |
|
| 142 |
// break the form submission |
| 143 |
throw new Request_Exception( 'validation_failed' ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* @param $block_name |
| 148 |
* |
| 149 |
* @return bool |
| 150 |
*/ |
| 151 |
public function isset_parser( $block_name ): bool { |
| 152 |
$type = Block_Helper::delete_namespace( $block_name ); |
| 153 |
|
| 154 |
return $this->rep_isset_item( $type ); |
| 155 |
} |
| 156 |
|
| 157 |
public function get_parser( $slug ): Field_Data_Parser { |
| 158 |
try { |
| 159 |
return $this->rep_clone_item( $slug ); |
| 160 |
} catch ( Repository_Exception $exception ) { |
| 161 |
return $this->rep_clone_item_or_die( 'default' ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
public function get_context(): Parser_Context { |
| 166 |
return $this->context; |
| 167 |
} |
| 168 |
|
| 169 |
public function install( Field_Data_Parser $item ): bool { |
| 170 |
return $this->rep_install_item_soft( $item ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @param Field_Data_Parser|string $item |
| 175 |
*/ |
| 176 |
public function uninstall( $item ) { |
| 177 |
$this->rep_remove( $item ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
} |
| 182 |
|