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

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

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