|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YIKES Inc. Easy Forms. |
|
4
|
|
|
* |
|
5
|
|
|
* @package YIKES\EasyForms |
|
6
|
|
|
* @author Freddie Mixell |
|
7
|
|
|
* @license GPL2 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace YIKES\EasyForms\Recaptcha; |
|
11
|
|
|
|
|
12
|
|
|
use YIKES\EasyForms\Assets\AssetsAware; |
|
13
|
|
|
use YIKES\EasyForms\Assets\AssetsAwareness; |
|
14
|
|
|
use YIKES\EasyForms\Assets\ScriptAsset; |
|
15
|
|
|
use YIKES\EasyForms\Service; |
|
16
|
|
|
use YIKES\EasyForms\View\View; |
|
17
|
|
|
use YIKES\EasyForms\Model\Recaptcha as RecaptchaModel; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Recaptcha |
|
21
|
|
|
* |
|
22
|
|
|
* @since %VERSION% |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Recaptcha implements Service, AssetsAware { |
|
25
|
|
|
|
|
26
|
|
|
use AssetsAwareness; |
|
27
|
|
|
|
|
28
|
|
|
const VIEW_URI = 'views/recaptcha-box'; |
|
29
|
|
|
const JS_HANDLE = 'google-recaptcha-js'; |
|
30
|
|
|
const JS_URI = 'https://www.google.com/recaptcha/api.js'; |
|
31
|
|
|
const JS_DEPENDENCIES = [ 'jquery', 'form-submission-helpers' ]; |
|
32
|
|
|
const JS_VERSION = '1.0.0'; |
|
33
|
|
|
|
|
34
|
|
|
public function register() { |
|
35
|
|
|
$this->register_assets(); |
|
36
|
|
|
|
|
37
|
|
|
add_action( 'easy_forms_do_recaptcha_box', function( $view ) { |
|
38
|
|
|
$this->enqueue_assets(); |
|
39
|
|
|
echo $view->render_partial( static::VIEW_URI ); // phpcs:ignore WordPress.Security.EscapeOutput |
|
40
|
|
|
} ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the context to pass onto the view. |
|
45
|
|
|
* |
|
46
|
|
|
* Override to provide data to the view. |
|
47
|
|
|
* |
|
48
|
|
|
* @since %VERSION% |
|
49
|
|
|
* |
|
50
|
|
|
* @return array Context to pass onto view. |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function get_context() { |
|
53
|
|
|
return $this->recaptcha['recaptcha_options']; |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function get_script_params() { |
|
57
|
|
|
$recaptcha_options = ( new RecaptchaModel() )->setup(); |
|
58
|
|
|
return $recaptcha_options['script_params']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function __get( $name ) { |
|
62
|
|
|
switch ( $name ) { |
|
63
|
|
|
case 'script_params': |
|
64
|
|
|
return $this->get_script_params(); |
|
65
|
|
|
|
|
66
|
|
|
default: |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Load asset objects for use. |
|
73
|
|
|
* |
|
74
|
|
|
* @since %SINCE% |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function load_assets() { |
|
77
|
|
|
$this->assets= [ |
|
78
|
|
|
new ScriptAsset( |
|
79
|
|
|
self::JS_HANDLE, |
|
80
|
|
|
self::JS_URI . $this->script_params, |
|
|
|
|
|
|
81
|
|
|
self::JS_DEPENDENCIES, |
|
82
|
|
|
self::JS_VERSION, |
|
83
|
|
|
ScriptAsset::ENQUEUE_HEADER, |
|
84
|
|
|
true |
|
85
|
|
|
), |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
} |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.