PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.5.6
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.5.6
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 5.5.6, at classes/models/FrmFormApi.php

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