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

203 lines 4.1 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 * @param object $license_plugin The FrmAddon object
108 * @return array
109 */
110 public function get_addon_for_license( $license_plugin, $addons = array() ) {
111 if ( empty( $addons ) ) {
112 $addons = $this->get_api_info();
113 }
114 $download_id = $license_plugin->download_id;
115 $plugin = array();
116 if ( empty( $download_id ) && ! empty( $addons ) ) {
117 foreach ( $addons as $addon ) {
118 if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) {
119 return $addon;
120 }
121 }
122 } elseif ( isset( $addons[ $download_id ] ) ) {
123 $plugin = $addons[ $download_id ];
124 }
125
126 return $plugin;
127 }
128
129 /**
130 * @since 3.06
131 */
132 public function get_pro_updater() {
133 if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) {
134 $updater = FrmProAppHelper::get_updater();
135 $this->set_license( $updater->license );
136 return $updater;
137 }
138
139 return false;
140 }
141
142 /**
143 * @since 3.06
144 * @return array
145 */
146 protected function get_cached() {
147 $cache = get_option( $this->cache_key );
148
149 if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) {
150 return false; // Cache is expired
151 }
152
153 return json_decode( $cache['value'], true );
154 }
155
156 /**
157 * @since 3.06
158 */
159 protected function set_cached( $addons ) {
160 $data = array(
161 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ),
162 'value' => json_encode( $addons ),
163 );
164
165 update_option( $this->cache_key, $data, 'no' );
166 }
167
168 /**
169 * @since 3.06
170 */
171 public function reset_cached() {
172 delete_option( $this->cache_key );
173 }
174
175 /**
176 * @since 3.06
177 * @return array
178 */
179 public function error_for_license() {
180 $errors = array();
181 if ( ! empty( $this->license ) ) {
182 $errors = $this->get_error_from_response();
183 }
184 return $errors;
185 }
186
187 /**
188 * @since 3.06
189 * @return array
190 */
191 public function get_error_from_response( $addons = array() ) {
192 if ( empty( $addons ) ) {
193 $addons = $this->get_api_info();
194 }
195 $errors = array();
196 if ( isset( $addons['error'] ) ) {
197 $errors[] = $addons['error']['message'];
198 do_action( 'frm_license_error', $addons['error'] );
199 }
200 return $errors;
201 }
202 }
203