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

235 lines 4.8 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 ( ! empty( $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 if ( is_array( $response ) && ! is_wp_error( $response ) ) {
85 $addons = $response['body'];
86 if ( ! empty( $addons ) ) {
87 $addons = json_decode( $addons, true );
88
89 foreach ( $addons as $k => $addon ) {
90 if ( ! isset( $addon['categories'] ) ) {
91 continue;
92 }
93 $cats = array_intersect( $this->skip_categories(), $addon['categories'] );
94 if ( ! empty( $cats ) ) {
95 unset( $addons[ $k ] );
96 }
97 }
98
99 $this->set_cached( $addons );
100 }
101 }
102
103 if ( empty( $addons ) ) {
104 return array();
105 }
106
107 return $addons;
108 }
109
110 /**
111 * @since 3.06
112 */
113 protected function api_url() {
114 return 'https://formidableforms.com/wp-json/s11edd/v1/updates/';
115 }
116
117 /**
118 * @since 3.06
119 */
120 protected function skip_categories() {
121 return array( 'WordPress Form Templates', 'WordPress Form Style Templates' );
122 }
123
124 /**
125 * @since 3.06
126 *
127 * @param object $license_plugin The FrmAddon object
128 *
129 * @return array
130 */
131 public function get_addon_for_license( $license_plugin, $addons = array() ) {
132 if ( empty( $addons ) ) {
133 $addons = $this->get_api_info();
134 }
135 $download_id = $license_plugin->download_id;
136 $plugin = array();
137 if ( empty( $download_id ) && ! empty( $addons ) ) {
138 foreach ( $addons as $addon ) {
139 if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) {
140 return $addon;
141 }
142 }
143 } elseif ( isset( $addons[ $download_id ] ) ) {
144 $plugin = $addons[ $download_id ];
145 }
146
147 return $plugin;
148 }
149
150 /**
151 * @since 3.06
152 */
153 public function get_pro_updater() {
154 if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) {
155 $updater = FrmProAppHelper::get_updater();
156 $this->set_license( $updater->license );
157
158 return $updater;
159 }
160
161 return false;
162 }
163
164 /**
165 * @since 3.06
166 * @return array
167 */
168 protected function get_cached() {
169 $cache = get_option( $this->cache_key );
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 $data = array(
190 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
191 'value' => json_encode( $addons ),
192 'version' => FrmAppHelper::plugin_version(),
193 );
194
195 update_option( $this->cache_key, $data, 'no' );
196 }
197
198 /**
199 * @since 3.06
200 */
201 public function reset_cached() {
202 delete_option( $this->cache_key );
203 }
204
205 /**
206 * @since 3.06
207 * @return array
208 */
209 public function error_for_license() {
210 $errors = array();
211 if ( ! empty( $this->license ) ) {
212 $errors = $this->get_error_from_response();
213 }
214
215 return $errors;
216 }
217
218 /**
219 * @since 3.06
220 * @return array
221 */
222 public function get_error_from_response( $addons = array() ) {
223 if ( empty( $addons ) ) {
224 $addons = $this->get_api_info();
225 }
226 $errors = array();
227 if ( isset( $addons['error'] ) ) {
228 $errors[] = $addons['error']['message'];
229 do_action( 'frm_license_error', $addons['error'] );
230 }
231
232 return $errors;
233 }
234 }
235