PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.0
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmFormTemplateApi.php +108 -56 6.316.0 View file →
@@ -4,37 +4,21 @@
4 4 }
5 5
6 6 class FrmFormTemplateApi extends FrmFormApi {
7 7
8 - /**
9 - * @var string
10 - */
11 - protected static $code_option_name = 'frm_free_license_code';
8 + protected static $code_option_name = 'frm_free_license_code';
12 9
13 - /**
14 - * @var string
15 - */
16 10 private static $base_api_url = 'https://formidableforms.com/wp-json/form-templates/v1/';
17 11
18 - /**
19 - * @var string|null
20 - */
21 12 protected static $free_license;
22 13
23 14 /**
24 - * @var int
15 + * @var int $new_days
25 16 */
26 17 protected $new_days = 30;
27 18
28 19 /**
29 - * @var string
30 - */
31 - protected $cache_timeout = '+12 hours';
32 -
33 - /**
34 20 * @since 3.06
35 - *
36 - * @return void
37 21 */
38 22 protected function set_cache_key() {
39 23 $this->cache_key = 'frm_form_templates_l';
40 24
@@ -40,22 +24,28 @@
40 24
41 25 if ( ! empty( $this->license ) ) {
42 26 $this->cache_key .= md5( $this->license );
43 27 } else {
44 - $this->cache_key .= '01fd72122f4a90f15684915c729d4368';
28 + $free_license = $this->get_free_license();
29 + if ( $free_license ) {
30 + $this->cache_key .= md5( $free_license );
31 + }
45 32 }
46 33 }
47 34
48 35 /**
49 36 * @since 3.06
50 - *
51 - * @return string
52 37 */
53 38 protected function api_url() {
54 39 $url = self::$base_api_url . 'list';
55 40
56 41 if ( empty( $this->license ) ) {
57 - $url .= '?l=RlJFRVRFTVBMQVRFUyEhIQ%3D%3D&v=' . FrmAppHelper::plugin_version();
42 + $free_license = $this->get_free_license();
43 +
44 + if ( $free_license ) {
45 + $url .= '?l=' . urlencode( base64_encode( $free_license ) );
46 + $url .= '&v=' . FrmAppHelper::plugin_version();
47 + }
58 48 }
59 49
60 50 return $url;
61 51 }
@@ -61,10 +51,8 @@
61 51 }
62 52
63 53 /**
64 54 * @since 3.06
65 - *
66 - * @return string[]
67 55 */
68 56 protected function skip_categories() {
69 57 return array();
70 58 }
@@ -69,56 +57,120 @@
69 57 return array();
70 58 }
71 59
72 60 /**
73 - * Set the free license code option value.
61 + * @return string
62 + */
63 + public function get_free_license() {
64 + if ( ! isset( self::$free_license ) ) {
65 + self::$free_license = get_option( self::$code_option_name );
66 + }
67 +
68 + return self::$free_license;
69 + }
70 +
71 + /**
72 + * Check to make sure the free code is being used.
74 73 *
75 - * @since 6.25
76 - *
77 - * @param string $code The license code to set.
78 - *
79 - * @return void
74 + * @since 4.09.02
80 75 */
81 - public static function set_free_license_code( $code ) {
82 - update_option( self::$code_option_name, $code, false );
76 + public function has_free_access() {
77 + $free_access = $this->get_free_license();
78 + if ( ! $free_access ) {
79 + return false;
80 + }
81 +
82 + $templates = $this->get_api_info();
83 + $contact_form = 20872734;
84 + return isset( $templates[ $contact_form ] ) && ! empty( $templates[ $contact_form ]['url'] );
83 85 }
84 86
85 87 /**
86 - * Get the free license code option value.
87 - *
88 - * @since 6.25
89 - *
90 - * @return false|string The license code, or false if not set.
88 + * @param string $code the code from the email sent for the API
91 89 */
92 - public static function get_free_license_code() {
93 - return get_option( self::$code_option_name );
90 + private static function verify_code( $code ) {
91 + $base64_code = base64_encode( $code );
92 + $api_url = self::$base_api_url . 'code?l=' . urlencode( $base64_code );
93 + $response = wp_remote_get( $api_url );
94 +
95 + self::handle_verify_response_errors_if_any( $response );
96 +
97 + $decoded = json_decode( $response['body'] );
98 + $successful = ! empty( $decoded->response );
99 +
100 + if ( $successful ) {
101 + self::on_api_verify_code_success( $code );
102 + } else {
103 + wp_send_json_error( new WP_Error( $decoded->code, $decoded->message ) );
104 + }
94 105 }
95 106
107 + private static function clear_template_cache_before_getting_free_templates() {
108 + delete_option( 'frm_form_templates_l' );
109 + }
110 +
96 111 /**
97 - * AJAX Hook for signing free users up for a template API key
98 - *
99 - * @return void
112 + * @param array $response
100 113 */
101 - public static function signup() {
102 - _deprecated_function( __METHOD__, '6.20' );
114 + private static function handle_verify_response_errors_if_any( $response ) {
115 + if ( is_wp_error( $response ) ) {
116 + wp_send_json_error( $response );
117 + }
118 +
119 + if ( ! is_array( $response ) ) {
120 + wp_send_json_error();
121 + }
103 122 }
104 123
105 124 /**
106 - * @return string
125 + * @param string $code the base64 encoded code
107 126 */
108 - public function get_free_license() {
109 - _deprecated_function( __METHOD__, '6.20' );
110 - return '';
127 + private static function on_api_verify_code_success( $code ) {
128 + self::$free_license = $code;
129 + update_option( self::$code_option_name, $code, 'no' );
130 +
131 + $data = array();
132 + $key = FrmAppHelper::get_param( 'key', '', 'post', 'sanitize_key' );
133 +
134 + // Remove message from Inbox page.
135 + $message = new FrmInbox();
136 + $message->remove( 'free_templates' );
137 +
138 + if ( $key ) {
139 + self::clear_template_cache_before_getting_free_templates();
140 +
141 + $data['urlByKey'] = array();
142 + $api = new self();
143 + $templates = $api->get_api_info();
144 +
145 + foreach ( $templates as $template ) {
146 + if ( ! isset( $template['url'] ) || ! in_array( 'free', $template['categories'], true ) ) {
147 + continue;
148 + }
149 +
150 + $data['urlByKey'][ $template['key'] ] = $template['url'];
151 + }
152 +
153 + if ( ! isset( $data['urlByKey'][ $key ] ) ) {
154 + $error = new WP_Error( 400, 'We were unable to retrieve the template' );
155 + wp_send_json_error( $error );
156 + }
157 +
158 + $data['url'] = $data['urlByKey'][ $key ];
159 + }
160 +
161 + wp_send_json_success( $data );
111 162 }
112 163
113 164 /**
114 - * Check to make sure the free code is being used.
115 - *
116 - * @since 4.09.02
117 - *
118 - * @return bool
165 + * AJAX Hook for signing free users up for a template API key
119 166 */
120 - public function has_free_access() {
121 - _deprecated_function( __METHOD__, '6.20' );
122 - return true;
167 + public static function signup() {
168 + $code = FrmAppHelper::get_param( 'code', '', 'post' );
169 +
170 + if ( ! $code ) {
171 + wp_send_json_error();
172 + }
173 +
174 + self::verify_code( $code );
123 175 }
124 176 }