PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.8.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.8.2
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 / FrmFormApi.php

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

285 lines 5.7 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 FrmFormApi {
7
8 protected $license = '';
9 protected $cache_key = '';
10 protected $cache_timeout = '+6 hours';
11
12 /**
13 * The number of days an add-on is new.
14 *
15 * @var int $new_days
16 */
17 protected $new_days = 90;
18
19 /**
20 * @since 3.06
21 */
22 public function __construct( $license = null ) {
23 $this->set_license( $license );
24 $this->set_cache_key();
25 }
26
27 /**
28 * @since 3.06
29 *
30 * @return void
31 */
32 private function set_license( $license ) {
33 if ( $license === null ) {
34 $edd_update = $this->get_pro_updater();
35 if ( ! empty( $edd_update ) ) {
36 $license = $edd_update->license;
37 }
38 }
39 $this->license = $license;
40 }
41
42 /**
43 * @since 3.06
44 * @return string
45 */
46 public function get_license() {
47 return $this->license;
48 }
49
50 /**
51 * @since 3.06
52 *
53 * @return void
54 */
55 protected function set_cache_key() {
56 $this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) );
57 }
58
59 /**
60 * @since 3.06
61 * @return string
62 */
63 public function get_cache_key() {
64 return $this->cache_key;
65 }
66
67 /**
68 * @since 3.06
69 * @return array
70 */
71 public function get_api_info() {
72 $url = $this->api_url();
73 if ( ! empty( $this->license ) ) {
74 $url .= '?l=' . urlencode( base64_encode( $this->license ) );
75 }
76
77 $addons = $this->get_cached();
78 if ( is_array( $addons ) ) {
79 return $addons;
80 }
81
82 // We need to know the version number to allow different downloads.
83 $agent = 'formidable/' . FrmAppHelper::plugin_version();
84 if ( class_exists( 'FrmProDb' ) ) {
85 $agent = 'formidable-pro/' . FrmProDb::$plug_version;
86 }
87
88 $response = wp_remote_get(
89 $url,
90 array(
91 'timeout' => 25,
92 'user-agent' => $agent . '; ' . get_bloginfo( 'url' ),
93 )
94 );
95
96 if ( is_array( $response ) && ! is_wp_error( $response ) ) {
97 $addons = $response['body'] ? json_decode( $response['body'], true ) : array();
98 }
99
100 if ( ! is_array( $addons ) ) {
101 $addons = array();
102 }
103
104 foreach ( $addons as $k => $addon ) {
105 if ( ! is_array( $addon ) ) {
106 continue;
107 }
108
109 if ( isset( $addon['categories'] ) ) {
110 $cats = array_intersect( $this->skip_categories(), $addon['categories'] );
111 if ( ! empty( $cats ) ) {
112 unset( $addons[ $k ] );
113 continue;
114 }
115 }
116
117 if ( ! array_key_exists( 'is_new', $addon ) && array_key_exists( 'released', $addon ) ) {
118 $addons[ $k ]['is_new'] = $this->is_new( $addon );
119 }
120 }
121
122 $this->set_cached( $addons );
123
124 return $addons;
125 }
126
127 /**
128 * @since 3.06
129 *
130 * @return string
131 */
132 protected function api_url() {
133 return 'https://formidableforms.com/wp-json/s11edd/v1/updates/';
134 }
135
136 /**
137 * @since 3.06
138 *
139 * @return string[]
140 */
141 protected function skip_categories() {
142 return array( 'WordPress Form Templates', 'WordPress Form Style Templates' );
143 }
144
145 /**
146 * @since 3.06
147 *
148 * @param object $license_plugin The FrmAddon object.
149 * @param array $addons
150 *
151 * @return array
152 */
153 public function get_addon_for_license( $license_plugin, $addons = array() ) {
154 if ( empty( $addons ) ) {
155 $addons = $this->get_api_info();
156 }
157 $download_id = $license_plugin->download_id;
158 $plugin = array();
159 if ( empty( $download_id ) && ! empty( $addons ) ) {
160 foreach ( $addons as $addon ) {
161 if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) {
162 return $addon;
163 }
164 }
165 } elseif ( isset( $addons[ $download_id ] ) ) {
166 $plugin = $addons[ $download_id ];
167 }
168
169 return $plugin;
170 }
171
172 /**
173 * @since 3.06
174 */
175 public function get_pro_updater() {
176 if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) {
177 $updater = FrmProAppHelper::get_updater();
178 $this->set_license( $updater->license );
179
180 return $updater;
181 }
182
183 return false;
184 }
185
186 /**
187 * @since 3.06
188 * @return array
189 */
190 protected function get_cached() {
191 $cache = get_option( $this->cache_key );
192
193 FrmAppHelper::filter_gmt_offset();
194
195 if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
196 // Cache is expired.
197 return false;
198 }
199
200 $version = FrmAppHelper::plugin_version();
201 $for_current = isset( $cache['version'] ) && $cache['version'] == $version;
202 if ( ! $for_current ) {
203 // Force a new check.
204 return false;
205 }
206
207 return json_decode( $cache['value'], true );
208 }
209
210 /**
211 * @since 3.06
212 *
213 * @param array $addons
214 *
215 * @return void
216 */
217 protected function set_cached( $addons ) {
218 FrmAppHelper::filter_gmt_offset();
219
220 $data = array(
221 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
222 'value' => json_encode( $addons ),
223 'version' => FrmAppHelper::plugin_version(),
224 );
225
226 update_option( $this->cache_key, $data, 'no' );
227 }
228
229 /**
230 * @since 3.06
231 *
232 * @return void
233 */
234 public function reset_cached() {
235 delete_option( $this->cache_key );
236 }
237
238 /**
239 * @since 3.06
240 * @return array
241 */
242 public function error_for_license() {
243 $errors = array();
244 if ( ! empty( $this->license ) ) {
245 $errors = $this->get_error_from_response();
246 }
247
248 return $errors;
249 }
250
251 /**
252 * @since 3.06
253 * @return array
254 */
255 public function get_error_from_response( $addons = array() ) {
256 if ( empty( $addons ) ) {
257 $addons = $this->get_api_info();
258 }
259 $errors = array();
260 if ( isset( $addons['error'] ) ) {
261 if ( is_string( $addons['error'] ) ) {
262 $errors[] = $addons['error'];
263 } elseif ( ! empty( $addons['error']['message'] ) ) {
264 $errors[] = $addons['error']['message'];
265 }
266
267 do_action( 'frm_license_error', $addons['error'] );
268 }
269
270 return $errors;
271 }
272
273 /**
274 * Check if a template is new.
275 *
276 * @since 6.0
277 *
278 * @param array $addon
279 * @return bool
280 */
281 protected function is_new( $addon ) {
282 return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' );
283 }
284 }
285