PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.12
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.12
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
formidable / classes / models / FrmFormTemplateApi.php

FrmFormTemplateApi.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.12, at classes/models/FrmFormTemplateApi.php

201 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmFormTemplateApi extends FrmFormApi {
7
8 protected static $code_option_name = 'frm_free_license_code';
9
10 private static $base_api_url = 'https://formidableforms.com/wp-json/form-templates/v1/';
11
12 protected static $free_license;
13
14 /**
15 * @var int
16 */
17 protected $new_days = 30;
18
19 /**
20 * @var string
21 */
22 protected $cache_timeout = '+12 hours';
23
24 /**
25 * @since 3.06
26 *
27 * @return void
28 */
29 protected function set_cache_key() {
30 $this->cache_key = 'frm_form_templates_l';
31
32 if ( ! empty( $this->license ) ) {
33 $this->cache_key .= md5( $this->license );
34 } else {
35 $free_license = $this->get_free_license();
36 if ( $free_license ) {
37 $this->cache_key .= md5( $free_license );
38 }
39 }
40 }
41
42 /**
43 * @since 3.06
44 *
45 * @return string
46 */
47 protected function api_url() {
48 $url = self::$base_api_url . 'list';
49
50 if ( empty( $this->license ) ) {
51 $free_license = $this->get_free_license();
52
53 if ( $free_license ) {
54 $url .= '?l=' . urlencode( base64_encode( $free_license ) );
55 $url .= '&v=' . FrmAppHelper::plugin_version();
56 }
57 }
58
59 return $url;
60 }
61
62 /**
63 * @since 3.06
64 *
65 * @return string[]
66 */
67 protected function skip_categories() {
68 return array();
69 }
70
71 /**
72 * @return string
73 */
74 public function get_free_license() {
75 if ( ! isset( self::$free_license ) ) {
76 self::$free_license = get_option( self::$code_option_name );
77 }
78
79 return self::$free_license;
80 }
81
82 /**
83 * Check to make sure the free code is being used.
84 *
85 * @since 4.09.02
86 *
87 * @return bool
88 */
89 public function has_free_access() {
90 $free_access = $this->get_free_license();
91 if ( ! $free_access ) {
92 return false;
93 }
94
95 $templates = $this->get_api_info();
96 $contact_form = 20872734;
97 return isset( $templates[ $contact_form ] ) && ! empty( $templates[ $contact_form ]['url'] );
98 }
99
100 /**
101 * @param string $code the code from the email sent for the API.
102 *
103 * @return void
104 */
105 private static function verify_code( $code ) {
106 $base64_code = base64_encode( $code );
107 $api_url = self::$base_api_url . 'code?l=' . urlencode( $base64_code );
108 $response = wp_remote_get( $api_url );
109
110 self::handle_verify_response_errors_if_any( $response );
111
112 $decoded = json_decode( $response['body'] );
113 $successful = ! empty( $decoded->response );
114
115 if ( $successful ) {
116 self::on_api_verify_code_success( $code );
117 } else {
118 wp_send_json_error( new WP_Error( $decoded->code, $decoded->message ) );
119 }
120 }
121
122 /**
123 * @return void
124 */
125 private static function clear_template_cache_before_getting_free_templates() {
126 delete_option( 'frm_form_templates_l' );
127 }
128
129 /**
130 * @param array $response
131 *
132 * @return void
133 */
134 private static function handle_verify_response_errors_if_any( $response ) {
135 if ( is_wp_error( $response ) ) {
136 wp_send_json_error( $response );
137 }
138
139 if ( ! is_array( $response ) ) {
140 wp_send_json_error();
141 }
142 }
143
144 /**
145 * @param string $code The base64 encoded code.
146 *
147 * @return void
148 */
149 private static function on_api_verify_code_success( $code ) {
150 self::$free_license = $code;
151 update_option( self::$code_option_name, $code, 'no' );
152
153 $data = array();
154 $key = FrmAppHelper::get_param( 'key', '', 'post', 'sanitize_key' );
155
156 // Remove message from Inbox page.
157 $message = new FrmInbox();
158 $message->remove( 'free_templates' );
159
160 if ( $key ) {
161 self::clear_template_cache_before_getting_free_templates();
162
163 $data['urlByKey'] = array();
164 $api = new self();
165 $templates = $api->get_api_info();
166
167 foreach ( $templates as $template ) {
168 if ( ! isset( $template['url'] ) || ! in_array( 'free', $template['categories'], true ) ) {
169 continue;
170 }
171
172 $data['urlByKey'][ $template['key'] ] = $template['url'];
173 }
174
175 if ( ! isset( $data['urlByKey'][ $key ] ) ) {
176 $error = new WP_Error( 400, 'We were unable to retrieve the template' );
177 wp_send_json_error( $error );
178 }
179
180 $data['url'] = $data['urlByKey'][ $key ];
181 }//end if
182
183 wp_send_json_success( $data );
184 }
185
186 /**
187 * AJAX Hook for signing free users up for a template API key
188 *
189 * @return void
190 */
191 public static function signup() {
192 $code = FrmAppHelper::get_param( 'code', '', 'post' );
193
194 if ( ! $code ) {
195 wp_send_json_error();
196 }
197
198 self::verify_code( $code );
199 }
200 }
201