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

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