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

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