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

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