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
formidable / classes / models / FrmFormApi.php

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

279 lines 5.6 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 return false; // Cache is expired
197 }
198
199 $version = FrmAppHelper::plugin_version();
200 $for_current = isset( $cache['version'] ) && $cache['version'] == $version;
201 if ( ! $for_current ) {
202 // Force a new check.
203 return false;
204 }
205
206 return json_decode( $cache['value'], true );
207 }
208
209 /**
210 * @since 3.06
211 *
212 * @param array $addons
213 *
214 * @return void
215 */
216 protected function set_cached( $addons ) {
217 FrmAppHelper::filter_gmt_offset();
218
219 $data = array(
220 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
221 'value' => json_encode( $addons ),
222 'version' => FrmAppHelper::plugin_version(),
223 );
224
225 update_option( $this->cache_key, $data, 'no' );
226 }
227
228 /**
229 * @since 3.06
230 *
231 * @return void
232 */
233 public function reset_cached() {
234 delete_option( $this->cache_key );
235 }
236
237 /**
238 * @since 3.06
239 * @return array
240 */
241 public function error_for_license() {
242 $errors = array();
243 if ( ! empty( $this->license ) ) {
244 $errors = $this->get_error_from_response();
245 }
246
247 return $errors;
248 }
249
250 /**
251 * @since 3.06
252 * @return array
253 */
254 public function get_error_from_response( $addons = array() ) {
255 if ( empty( $addons ) ) {
256 $addons = $this->get_api_info();
257 }
258 $errors = array();
259 if ( isset( $addons['error'] ) ) {
260 $errors[] = $addons['error']['message'];
261 do_action( 'frm_license_error', $addons['error'] );
262 }
263
264 return $errors;
265 }
266
267 /**
268 * Check if a template is new.
269 *
270 * @since 6.0
271 *
272 * @param array $addon
273 * @return bool
274 */
275 protected function is_new( $addon ) {
276 return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' );
277 }
278 }
279