PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7
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 +116 -45 6.266.7 View file →
@@ -4,34 +4,20 @@
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 21 *
36 22 * @return void
37 23 */
@@ -40,9 +26,12 @@
40 26
41 27 if ( ! empty( $this->license ) ) {
42 28 $this->cache_key .= md5( $this->license );
43 29 } else {
44 - $this->cache_key .= '01fd72122f4a90f15684915c729d4368';
30 + $free_license = $this->get_free_license();
31 + if ( $free_license ) {
32 + $this->cache_key .= md5( $free_license );
33 + }
45 34 }
46 35 }
47 36
48 37 /**
@@ -53,9 +42,14 @@
53 42 protected function api_url() {
54 43 $url = self::$base_api_url . 'list';
55 44
56 45 if ( empty( $this->license ) ) {
57 - $url .= '?l=RlJFRVRFTVBMQVRFUyEhIQ%3D%3D&v=' . FrmAppHelper::plugin_version();
46 + $free_license = $this->get_free_license();
47 +
48 + if ( $free_license ) {
49 + $url .= '?l=' . urlencode( base64_encode( $free_license ) );
50 + $url .= '&v=' . FrmAppHelper::plugin_version();
51 + }
58 52 }
59 53
60 54 return $url;
61 55 }
@@ -62,9 +56,9 @@
62 56
63 57 /**
64 58 * @since 3.06
65 59 *
66 - * @return string[]
60 + * @return array
67 61 */
68 62 protected function skip_categories() {
69 63 return array();
70 64 }
@@ -69,56 +63,133 @@
69 63 return array();
70 64 }
71 65
72 66 /**
73 - * Set the free license code option value.
67 + * @return string
68 + */
69 + public function get_free_license() {
70 + if ( ! isset( self::$free_license ) ) {
71 + self::$free_license = get_option( self::$code_option_name );
72 + }
73 +
74 + return self::$free_license;
75 + }
76 +
77 + /**
78 + * Check to make sure the free code is being used.
74 79 *
75 - * @since 6.25
80 + * @since 4.09.02
76 81 *
77 - * @param string $code The license code to set.
82 + * @return bool
83 + */
84 + public function has_free_access() {
85 + $free_access = $this->get_free_license();
86 + if ( ! $free_access ) {
87 + return false;
88 + }
89 +
90 + $templates = $this->get_api_info();
91 + $contact_form = 20872734;
92 + return isset( $templates[ $contact_form ] ) && ! empty( $templates[ $contact_form ]['url'] );
93 + }
94 +
95 + /**
96 + * @param string $code the code from the email sent for the API
78 97 *
79 98 * @return void
80 99 */
81 - public static function set_free_license_code( $code ) {
82 - update_option( self::$code_option_name, $code, 'no' );
100 + private static function verify_code( $code ) {
101 + $base64_code = base64_encode( $code );
102 + $api_url = self::$base_api_url . 'code?l=' . urlencode( $base64_code );
103 + $response = wp_remote_get( $api_url );
104 +
105 + self::handle_verify_response_errors_if_any( $response );
106 +
107 + $decoded = json_decode( $response['body'] );
108 + $successful = ! empty( $decoded->response );
109 +
110 + if ( $successful ) {
111 + self::on_api_verify_code_success( $code );
112 + } else {
113 + wp_send_json_error( new WP_Error( $decoded->code, $decoded->message ) );
114 + }
83 115 }
84 116
85 117 /**
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.
118 + * @return void
91 119 */
92 - public static function get_free_license_code() {
93 - return get_option( self::$code_option_name );
120 + private static function clear_template_cache_before_getting_free_templates() {
121 + delete_option( 'frm_form_templates_l' );
94 122 }
95 123
96 124 /**
97 - * AJAX Hook for signing free users up for a template API key
125 + * @param array $response
98 126 *
99 127 * @return void
100 128 */
101 - public static function signup() {
102 - _deprecated_function( __METHOD__, '6.20' );
129 + private static function handle_verify_response_errors_if_any( $response ) {
130 + if ( is_wp_error( $response ) ) {
131 + wp_send_json_error( $response );
132 + }
133 +
134 + if ( ! is_array( $response ) ) {
135 + wp_send_json_error();
136 + }
103 137 }
104 138
105 139 /**
106 - * @return string
140 + * @param string $code the base64 encoded code
141 + *
142 + * @return void
107 143 */
108 - public function get_free_license() {
109 - _deprecated_function( __METHOD__, '6.20' );
110 - return '';
144 + private static function on_api_verify_code_success( $code ) {
145 + self::$free_license = $code;
146 + update_option( self::$code_option_name, $code, 'no' );
147 +
148 + $data = array();
149 + $key = FrmAppHelper::get_param( 'key', '', 'post', 'sanitize_key' );
150 +
151 + // Remove message from Inbox page.
152 + $message = new FrmInbox();
153 + $message->remove( 'free_templates' );
154 +
155 + if ( $key ) {
156 + self::clear_template_cache_before_getting_free_templates();
157 +
158 + $data['urlByKey'] = array();
159 + $api = new self();
160 + $templates = $api->get_api_info();
161 +
162 + foreach ( $templates as $template ) {
163 + if ( ! isset( $template['url'] ) || ! in_array( 'free', $template['categories'], true ) ) {
164 + continue;
165 + }
166 +
167 + $data['urlByKey'][ $template['key'] ] = $template['url'];
168 + }
169 +
170 + if ( ! isset( $data['urlByKey'][ $key ] ) ) {
171 + $error = new WP_Error( 400, 'We were unable to retrieve the template' );
172 + wp_send_json_error( $error );
173 + }
174 +
175 + $data['url'] = $data['urlByKey'][ $key ];
176 + }
177 +
178 + wp_send_json_success( $data );
111 179 }
112 180
113 181 /**
114 - * Check to make sure the free code is being used.
182 + * AJAX Hook for signing free users up for a template API key
115 183 *
116 - * @since 4.09.02
117 - *
118 - * @return bool
184 + * @return void
119 185 */
120 - public function has_free_access() {
121 - _deprecated_function( __METHOD__, '6.20' );
122 - return true;
186 + public static function signup() {
187 + $code = FrmAppHelper::get_param( 'code', '', 'post' );
188 +
189 + if ( ! $code ) {
190 + wp_send_json_error();
191 + }
192 +
193 + self::verify_code( $code );
123 194 }
124 195 }