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