PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.3.2
JetFormBuilder — Dynamic Blocks Form Builder v3.3.2
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / captcha / module.php
jetformbuilder / modules / captcha Last commit date
abstract-captcha 2 years ago admin-tabs 2 years ago assets-build 2 years ago block-types 2 years ago blocks-metadata 2 years ago friendly-captcha 2 years ago hcaptcha 2 years ago re-captcha-v3 2 years ago turnstile 2 years ago module.php 2 years ago
module.php
377 lines
1 <?php
2
3
4 namespace JFB_Modules\Captcha;
5
6 // If this file is called directly, abort.
7 if ( ! defined( 'WPINC' ) ) {
8 die;
9 }
10
11 use Jet_Form_Builder\Admin\Tabs_Handlers\Tab_Handler_Manager;
12 use Jet_Form_Builder\Blocks\Block_Helper;
13 use JFB_Components\Repository\Repository_Pattern_Trait;
14 use Jet_Form_Builder\Classes\Tools;
15 use Jet_Form_Builder\Exceptions\Repository_Exception;
16 use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha;
17 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Frontend_Style_It;
18 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Editor_Script;
19 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Frontend_Script;
20 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Settings_From_Options;
21 use JFB_Modules\Captcha\Friendly_Captcha\Friendly_Captcha;
22 use JFB_Modules\Captcha\Hcaptcha\Hcaptcha;
23 use JFB_Modules\Captcha\Re_Captcha_V3\Re_Captcha_V3;
24 use JFB_Modules\Captcha\Turnstile\Turnstile;
25 use JFB_Components\Module\Base_Module_After_Install_It;
26 use JFB_Components\Module\Base_Module_Dir_It;
27 use JFB_Components\Module\Base_Module_Dir_Trait;
28 use JFB_Components\Module\Base_Module_Handle_It;
29 use JFB_Components\Module\Base_Module_Handle_Trait;
30 use JFB_Components\Module\Base_Module_It;
31 use JFB_Components\Module\Base_Module_Url_It;
32 use JFB_Components\Module\Base_Module_Url_Trait;
33 use JFB_Modules\Security\Exceptions\Spam_Exception;
34 use Jet_Form_Builder\Plugin;
35
36 /**
37 * @since 3.1.0
38 *
39 * Class Module
40 * @package JFB_Modules\Captcha
41 */
42 final class Module implements
43 Base_Module_It,
44 Base_Module_Url_It,
45 Base_Module_Dir_It,
46 Base_Module_After_Install_It,
47 Base_Module_Handle_It {
48
49 use Base_Module_Handle_Trait;
50 use Base_Module_Url_Trait;
51 use Base_Module_Dir_Trait;
52
53 use Repository_Pattern_Trait;
54
55 const PREFIX = 'jet_form_builder_captcha__';
56
57 /**
58 * @var Base_Captcha[]
59 */
60 private $current = array();
61
62 public function rep_item_id() {
63 return 'captcha';
64 }
65
66 public function on_install() {
67 $this->rep_install();
68
69 Tab_Handler_Manager::instance()->install( new Admin_Tabs\Captcha_Handler() );
70 }
71
72 public function on_uninstall() {
73 $this->rep_clear();
74
75 Tab_Handler_Manager::instance()->uninstall( 'captcha-tab' );
76 }
77
78 public function rep_instances(): array {
79 return apply_filters(
80 'jet-form-builder/captcha/types',
81 array(
82 new Re_Captcha_V3(),
83 new Hcaptcha(),
84 new Turnstile(),
85 new Friendly_Captcha(),
86 )
87 );
88 }
89
90 public function condition(): bool {
91 return true;
92 }
93
94 public function init_hooks() {
95 add_filter( 'jet-form-builder/request-handler/request', array( $this, 'on_request' ) );
96 add_filter( 'jet-form-builder/before-render-field', array( $this, 'on_render_field' ), 10, 3 );
97 add_filter( 'jet-form-builder/page-config/jfb-settings', array( $this, 'on_localize_config' ) );
98 add_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_config' ) );
99 add_filter( 'jet-form-builder/setup-blocks', array( $this, 'check_is_container_exist' ) );
100 add_filter( 'jet-form-builder/before-end-form', array( $this, 'on_end_render_form' ) );
101 add_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) );
102
103 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
104 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_package_assets' ), 0 );
105 add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) );
106 add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) );
107 add_action( 'jet-form-builder/enqueue-style', array( $this, 'register_frontend_styles' ) );
108 }
109
110 public function remove_hooks() {
111 remove_filter( 'jet-form-builder/request-handler/request', array( $this, 'on_request' ) );
112 remove_filter( 'jet-form-builder/before-render-field', array( $this, 'on_render_field' ) );
113 remove_filter( 'jet-form-builder/page-config/jfb-settings', array( $this, 'on_localize_config' ) );
114 remove_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_config' ) );
115 remove_filter( 'jet-form-builder/setup-blocks', array( $this, 'check_is_container_exist' ) );
116 remove_filter( 'jet-form-builder/before-end-form', array( $this, 'on_end_render_form' ) );
117 remove_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) );
118
119 remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
120 remove_action(
121 'jet-form-builder/editor-assets/before',
122 array( $this, 'enqueue_editor_package_assets' ),
123 0
124 );
125 remove_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) );
126 remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) );
127 remove_action( 'jet-form-builder/enqueue-style', array( $this, 'register_frontend_styles' ) );
128 }
129
130 /**
131 * @param $request
132 *
133 * @return mixed
134 * @throws Spam_Exception
135 */
136 public function on_request( $request ) {
137 $this->verify( $request );
138
139 return $request;
140 }
141
142 /**
143 * By default, the captcha is displayed before the submit button.
144 * But if the Captcha Container block is present in the form, we can check it in advance.
145 * This is necessary because the captcha and the button can be in parallel columns.
146 *
147 * @param string $content
148 * @param string $field_name
149 * @param array $attrs
150 *
151 * @return string
152 * @see Forms_Captcha::check_is_container_exist
153 */
154 public function on_render_field( string $content, string $field_name, array $attrs ): string {
155 if ( 'submit-field' !== $field_name ) {
156 return $content;
157 }
158
159 try {
160 $current = $this->get_current();
161 } catch ( Repository_Exception $exception ) {
162 return $content;
163 }
164
165 $type = $attrs['action_type'] ?? '';
166
167 if (
168 'submit' !== $type ||
169 $current->is_exist_container()
170 ) {
171 return $content;
172 }
173
174 return ( $content . $this->render() );
175 }
176
177 /**
178 * If for some reason the submit button has not been rendered in the form,
179 * then to make sure that we have displayed the captcha,
180 * we display it at the end of the rendering of the entire form.
181 *
182 * @param string $content
183 *
184 * @return string
185 */
186 public function on_end_render_form( string $content ): string {
187 $content .= $this->render();
188
189 return $content;
190 }
191
192 public function add_blocks_types( array $block_types ): array {
193 $block_types[] = new Block_Types\Captcha_Container();
194
195 return $block_types;
196 }
197
198 public function register_frontend_scripts() {
199 /** @var Base_Captcha $captcha */
200 foreach ( $this->rep_generate_items() as $captcha ) {
201 if ( ! ( $captcha instanceof Captcha_Separate_Frontend_Script ) ) {
202 continue;
203 }
204 $captcha->register_frontend_scripts();
205 }
206 }
207
208 public function register_frontend_styles() {
209 try {
210 $current = $this->get_current();
211 } catch ( Repository_Exception $exception ) {
212 return;
213 }
214 if ( ! ( $current instanceof Captcha_Frontend_Style_It ) ) {
215 return;
216 }
217 $current->register_frontend_styles();
218 }
219
220 public function enqueue_editor_assets() {
221 wp_enqueue_script(
222 $this->get_handle(),
223 $this->get_url( 'assets-build/js/editor.js' ),
224 array(),
225 jet_form_builder()->get_version(),
226 true
227 );
228
229 /** @var Base_Captcha $captcha */
230 foreach ( $this->rep_generate_items() as $captcha ) {
231 if ( ! ( $captcha instanceof Captcha_Separate_Editor_Script ) ) {
232 continue;
233 }
234 $captcha->enqueue_editor_script();
235 }
236 }
237
238 public function enqueue_editor_package_assets() {
239 wp_enqueue_script(
240 $this->get_handle( 'package' ),
241 $this->get_url( 'assets-build/js/editor.package.js' ),
242 array(),
243 jet_form_builder()->get_version(),
244 true
245 );
246 }
247
248 /**
249 * @param $request
250 *
251 * @throws Spam_Exception
252 */
253 protected function verify( $request ) {
254 try {
255 $this->get_current()->verify( $request );
256 } catch ( Repository_Exception $exception ) {
257 return;
258 }
259 }
260
261 public function render(): string {
262 try {
263 return $this->get_current()->get_output();
264 } catch ( Repository_Exception $exception ) {
265 return '';
266 }
267 }
268
269 /**
270 * Returns captcha settings for current form
271 *
272 * @return Base_Captcha
273 * @throws Repository_Exception
274 */
275 public function get_current(): Base_Captcha {
276 if ( ! jet_fb_live()->form_id ) {
277 throw new Repository_Exception( 'no_captcha' );
278 }
279
280 if ( array_key_exists( (int) jet_fb_live()->form_id, $this->current ) ) {
281 if ( $this->current[ jet_fb_live()->form_id ] instanceof Base_Captcha ) {
282 return $this->current[ jet_fb_live()->form_id ];
283 }
284
285 throw new Repository_Exception( 'no_captcha' );
286 }
287
288 $settings = Plugin::instance()->post_type->get_captcha( jet_fb_live()->form_id );
289
290 if ( ! $settings || ! is_array( $settings ) ) {
291 $this->current[ jet_fb_live()->form_id ] = false;
292
293 throw new Repository_Exception( 'no_captcha' );
294 }
295
296 $captcha = $settings['captcha'] ?? false;
297
298 /**
299 * For backward compatibility
300 */
301 if ( false === $captcha && ! empty( $settings['enabled'] ) ) {
302 $captcha = Re_Captcha_V3::class;
303 }
304
305 /**
306 * @var Base_Captcha $current
307 */
308 $current = $this->rep_clone_item( $captcha );
309
310 $this->current[ jet_fb_live()->form_id ] = $current->sanitize_options( $settings );
311
312 return $this->current[ jet_fb_live()->form_id ];
313 }
314
315 public function on_localize_config( array $config ): array {
316 $captcha_config = array();
317
318 /** @var Base_Captcha $captcha */
319 foreach ( $this->rep_generate_items() as $captcha ) {
320 if ( ! ( $captcha instanceof Captcha_Settings_From_Options ) ) {
321 continue;
322 }
323 $captcha_config[] = $captcha->to_array();
324 }
325
326 $config['captcha-tab-config'] = $captcha_config;
327
328 return $config;
329 }
330
331 public function check_is_container_exist( array $blocks ): array {
332 try {
333 $current = $this->get_current();
334 } catch ( Repository_Exception $exception ) {
335 return $blocks;
336 }
337
338 if ( ! is_null( $current->is_exist_container() ) ) {
339 return $blocks;
340 }
341
342 $current->set_exist_container(
343 ! empty(
344 Block_Helper::find_by_block_name(
345 $blocks,
346 'jet-forms/captcha-container'
347 )
348 )
349 );
350
351 return $blocks;
352 }
353
354 /**
355 * @param string|array $config
356 * @param string $handle
357 *
358 * @return bool
359 */
360 public function add_inline_config( $config, string $handle = '' ): bool {
361 $form_id = jet_fb_live()->form_id;
362
363 if ( ! is_string( $config ) ) {
364 $config = Tools::encode_json( $config );
365 }
366
367 return wp_add_inline_script(
368 $handle ?: Base_Captcha::HANDLE_USER,
369 "
370 window.JetFormBuilderCaptchaConfig = window.JetFormBuilderCaptchaConfig || {};
371 window.JetFormBuilderCaptchaConfig[ $form_id ] = {$config};
372 ",
373 'before'
374 );
375 }
376 }
377