PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.1.6
JetFormBuilder — Dynamic Blocks Form Builder v3.1.6
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
.config 2 years ago abstract-captcha 2 years ago admin-tabs 2 years ago assets-build 2 years ago assets-src 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 .babelrc 2 years ago .gitattributes 2 years ago module.php 2 years ago package.json 2 years ago
module.php
373 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 Jet_Form_Builder\Exceptions\Handler_Exception;
14 use JFB_Components\Repository\Repository_Pattern_Trait;
15 use Jet_Form_Builder\Classes\Tools;
16 use Jet_Form_Builder\Exceptions\Repository_Exception;
17 use JFB_Modules\Captcha\Abstract_Captcha\Base_Captcha;
18 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Frontend_Style_It;
19 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Editor_Script;
20 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Separate_Frontend_Script;
21 use JFB_Modules\Captcha\Abstract_Captcha\Captcha_Settings_From_Options;
22 use JFB_Modules\Captcha\Friendly_Captcha\Friendly_Captcha;
23 use JFB_Modules\Captcha\Hcaptcha\Hcaptcha;
24 use JFB_Modules\Captcha\Re_Captcha_V3\Re_Captcha_V3;
25 use JFB_Modules\Captcha\Turnstile\Turnstile;
26 use JFB_Components\Module\Base_Module_After_Install_It;
27 use JFB_Components\Module\Base_Module_Dir_It;
28 use JFB_Components\Module\Base_Module_Dir_Trait;
29 use JFB_Components\Module\Base_Module_Handle_It;
30 use JFB_Components\Module\Base_Module_Handle_Trait;
31 use JFB_Components\Module\Base_Module_It;
32 use JFB_Components\Module\Base_Module_Url_It;
33 use JFB_Components\Module\Base_Module_Url_Trait;
34 use JFB_Modules\Security\Exceptions\Spam_Exception;
35 use Jet_Form_Builder\Plugin;
36
37 /**
38 * @since 3.1.0
39 *
40 * Class Module
41 * @package JFB_Modules\Captcha
42 */
43 final class Module implements
44 Base_Module_It,
45 Base_Module_Url_It,
46 Base_Module_Dir_It,
47 Base_Module_After_Install_It,
48 Base_Module_Handle_It {
49
50 use Base_Module_Handle_Trait;
51 use Base_Module_Url_Trait;
52 use Base_Module_Dir_Trait;
53
54 use Repository_Pattern_Trait;
55
56 const PREFIX = 'jet_form_builder_captcha__';
57
58 /**
59 * @var Base_Captcha[]
60 */
61 private $current = array();
62
63 public function rep_item_id() {
64 return 'captcha';
65 }
66
67 public function on_install() {
68 $this->rep_install();
69
70 Tab_Handler_Manager::instance()->install( new Admin_Tabs\Captcha_Handler() );
71 }
72
73 public function on_uninstall() {
74 $this->rep_clear();
75
76 Tab_Handler_Manager::instance()->uninstall( 'captcha-tab' );
77 }
78
79 public function rep_instances(): array {
80 return apply_filters(
81 'jet-form-builder/captcha/types',
82 array(
83 new Re_Captcha_V3(),
84 new Hcaptcha(),
85 new Turnstile(),
86 new Friendly_Captcha(),
87 )
88 );
89 }
90
91 public function condition(): bool {
92 return true;
93 }
94
95 public function init_hooks() {
96 add_filter( 'jet-form-builder/request-handler/request', array( $this, 'on_request' ) );
97 add_filter( 'jet-form-builder/before-render/submit-field', array( $this, 'on_render_submit' ), 10, 2 );
98 add_filter( 'jet-form-builder/page-config/jfb-settings', array( $this, 'on_localize_config' ) );
99 add_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_config' ) );
100 add_filter( 'jet-form-builder/setup-blocks', array( $this, 'check_is_container_exist' ) );
101 add_filter( 'jet-form-builder/before-end-form', array( $this, 'on_end_render_form' ) );
102 add_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) );
103
104 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
105 add_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_package_assets' ), 0 );
106 add_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) );
107 add_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) );
108 add_action( 'jet-form-builder/enqueue-style', array( $this, 'register_frontend_styles' ) );
109 }
110
111 public function remove_hooks() {
112 remove_filter( 'jet-form-builder/request-handler/request', array( $this, 'on_request' ) );
113 remove_filter( 'jet-form-builder/before-render/submit-field', array( $this, 'on_render_submit' ) );
114 remove_filter( 'jet-form-builder/page-config/jfb-settings', array( $this, 'on_localize_config' ) );
115 remove_filter( 'jet-form-builder/editor/config', array( $this, 'on_localize_config' ) );
116 remove_filter( 'jet-form-builder/setup-blocks', array( $this, 'check_is_container_exist' ) );
117 remove_filter( 'jet-form-builder/before-end-form', array( $this, 'on_end_render_form' ) );
118 remove_filter( 'jet-form-builder/blocks/items', array( $this, 'add_blocks_types' ) );
119
120 remove_action( 'jet-form-builder/editor-assets/before', array( $this, 'enqueue_editor_assets' ) );
121 remove_action(
122 'jet-form-builder/editor-assets/before',
123 array( $this, 'enqueue_editor_package_assets' ),
124 0
125 );
126 remove_action( 'wp_enqueue_scripts', array( $this, 'register_frontend_scripts' ) );
127 remove_action( 'jet_plugins/frontend/register_scripts', array( $this, 'register_frontend_scripts' ) );
128 remove_action( 'jet-form-builder/enqueue-style', array( $this, 'register_frontend_styles' ) );
129 }
130
131 /**
132 * @param $request
133 *
134 * @return mixed
135 * @throws Spam_Exception
136 */
137 public function on_request( $request ) {
138 $this->verify( $request );
139
140 return $request;
141 }
142
143 /**
144 * By default, the captcha is displayed before the submit button.
145 * But if the Captcha Container block is present in the form, we can check it in advance.
146 * This is necessary because the captcha and the button can be in parallel columns.
147 *
148 * @param string $content
149 * @param array $attrs
150 *
151 * @return string
152 * @see Forms_Captcha::check_is_container_exist
153 */
154 public function on_render_submit( string $content, array $attrs ): string {
155 try {
156 $current = $this->get_current();
157 } catch ( Repository_Exception $exception ) {
158 return $content;
159 }
160
161 $type = $attrs['action_type'] ?? '';
162
163 if (
164 'submit' !== $type ||
165 $current->is_exist_container()
166 ) {
167 return $content;
168 }
169
170 return ( $content . $this->render() );
171 }
172
173 /**
174 * If for some reason the submit button has not been rendered in the form,
175 * then to make sure that we have displayed the captcha,
176 * we display it at the end of the rendering of the entire form.
177 *
178 * @param string $content
179 *
180 * @return string
181 */
182 public function on_end_render_form( string $content ): string {
183 $content .= $this->render();
184
185 return $content;
186 }
187
188 public function add_blocks_types( array $block_types ): array {
189 $block_types[] = new Block_Types\Captcha_Container();
190
191 return $block_types;
192 }
193
194 public function register_frontend_scripts() {
195 /** @var Base_Captcha $captcha */
196 foreach ( $this->rep_generate_items() as $captcha ) {
197 if ( ! ( $captcha instanceof Captcha_Separate_Frontend_Script ) ) {
198 continue;
199 }
200 $captcha->register_frontend_scripts();
201 }
202 }
203
204 public function register_frontend_styles() {
205 try {
206 $current = $this->get_current();
207 } catch ( Repository_Exception $exception ) {
208 return;
209 }
210 if ( ! ( $current instanceof Captcha_Frontend_Style_It ) ) {
211 return;
212 }
213 $current->register_frontend_styles();
214 }
215
216 public function enqueue_editor_assets() {
217 wp_enqueue_script(
218 $this->get_handle(),
219 $this->get_url( 'assets-build/js/editor.js' ),
220 array(),
221 jet_form_builder()->get_version(),
222 true
223 );
224
225 /** @var Base_Captcha $captcha */
226 foreach ( $this->rep_generate_items() as $captcha ) {
227 if ( ! ( $captcha instanceof Captcha_Separate_Editor_Script ) ) {
228 continue;
229 }
230 $captcha->enqueue_editor_script();
231 }
232 }
233
234 public function enqueue_editor_package_assets() {
235 wp_enqueue_script(
236 $this->get_handle( 'package' ),
237 $this->get_url( 'assets-build/js/editor.package.js' ),
238 array(),
239 jet_form_builder()->get_version(),
240 true
241 );
242 }
243
244 /**
245 * @param $request
246 *
247 * @throws Spam_Exception
248 */
249 protected function verify( $request ) {
250 try {
251 $this->get_current()->verify( $request );
252 } catch ( Repository_Exception $exception ) {
253 return;
254 }
255 }
256
257 public function render(): string {
258 try {
259 return $this->get_current()->get_output();
260 } catch ( Repository_Exception $exception ) {
261 return '';
262 }
263 }
264
265 /**
266 * Returns captcha settings for current form
267 *
268 * @return Base_Captcha
269 * @throws Repository_Exception
270 */
271 public function get_current(): Base_Captcha {
272 if ( ! jet_fb_live()->form_id ) {
273 throw new Repository_Exception( 'no_captcha' );
274 }
275
276 if ( array_key_exists( (int) jet_fb_live()->form_id, $this->current ) ) {
277 if ( $this->current[ jet_fb_live()->form_id ] instanceof Base_Captcha ) {
278 return $this->current[ jet_fb_live()->form_id ];
279 }
280
281 throw new Repository_Exception( 'no_captcha' );
282 }
283
284 $settings = Plugin::instance()->post_type->get_captcha( jet_fb_live()->form_id );
285
286 if ( ! $settings || ! is_array( $settings ) ) {
287 $this->current[ jet_fb_live()->form_id ] = false;
288
289 throw new Repository_Exception( 'no_captcha' );
290 }
291
292 $captcha = $settings['captcha'] ?? false;
293
294 /**
295 * For backward compatibility
296 */
297 if ( false === $captcha && ! empty( $settings['enabled'] ) ) {
298 $captcha = Re_Captcha_V3::class;
299 }
300
301 /**
302 * @var Base_Captcha $current
303 */
304 $current = $this->rep_clone_item( $captcha );
305
306 $this->current[ jet_fb_live()->form_id ] = $current->sanitize_options( $settings );
307
308 return $this->current[ jet_fb_live()->form_id ];
309 }
310
311 public function on_localize_config( array $config ): array {
312 $captcha_config = array();
313
314 /** @var Base_Captcha $captcha */
315 foreach ( $this->rep_generate_items() as $captcha ) {
316 if ( ! ( $captcha instanceof Captcha_Settings_From_Options ) ) {
317 continue;
318 }
319 $captcha_config[] = $captcha->to_array();
320 }
321
322 $config['captcha-tab-config'] = $captcha_config;
323
324 return $config;
325 }
326
327 public function check_is_container_exist( array $blocks ): array {
328 try {
329 $current = $this->get_current();
330 } catch ( Repository_Exception $exception ) {
331 return $blocks;
332 }
333
334 if ( ! is_null( $current->is_exist_container() ) ) {
335 return $blocks;
336 }
337
338 $current->set_exist_container(
339 ! empty(
340 Block_Helper::find_by_block_name(
341 $blocks,
342 'jet-forms/captcha-container'
343 )
344 )
345 );
346
347 return $blocks;
348 }
349
350 /**
351 * @param string|array $config
352 * @param string $handle
353 *
354 * @return bool
355 */
356 public function add_inline_config( $config, string $handle = '' ): bool {
357 $form_id = jet_fb_live()->form_id;
358
359 if ( ! is_string( $config ) ) {
360 $config = Tools::encode_json( $config );
361 }
362
363 return wp_add_inline_script(
364 $handle ?: Base_Captcha::HANDLE_USER,
365 "
366 window.JetFormBuilderCaptchaConfig = window.JetFormBuilderCaptchaConfig || {};
367 window.JetFormBuilderCaptchaConfig[ $form_id ] = {$config};
368 ",
369 'before'
370 );
371 }
372 }
373