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

233 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 ( 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 if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
170 return false; // Cache is expired
171 }
172
173 $version = FrmAppHelper::plugin_version();
174 $for_current = isset( $cache['version'] ) && $cache['version'] == $version;
175 if ( ! $for_current ) {
176 // Force a new check.
177 return false;
178 }
179
180 return json_decode( $cache['value'], true );
181 }
182
183 /**
184 * @since 3.06
185 */
186 protected function set_cached( $addons ) {
187 $data = array(
188 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
189 'value' => json_encode( $addons ),
190 'version' => FrmAppHelper::plugin_version(),
191 );
192
193 update_option( $this->cache_key, $data, 'no' );
194 }
195
196 /**
197 * @since 3.06
198 */
199 public function reset_cached() {
200 delete_option( $this->cache_key );
201 }
202
203 /**
204 * @since 3.06
205 * @return array
206 */
207 public function error_for_license() {
208 $errors = array();
209 if ( ! empty( $this->license ) ) {
210 $errors = $this->get_error_from_response();
211 }
212
213 return $errors;
214 }
215
216 /**
217 * @since 3.06
218 * @return array
219 */
220 public function get_error_from_response( $addons = array() ) {
221 if ( empty( $addons ) ) {
222 $addons = $this->get_api_info();
223 }
224 $errors = array();
225 if ( isset( $addons['error'] ) ) {
226 $errors[] = $addons['error']['message'];
227 do_action( 'frm_license_error', $addons['error'] );
228 }
229
230 return $errors;
231 }
232 }
233