| @@ -1,23 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - die( 'You are not allowed to call this page directly.' ); | |
| 4 | -} | |
| 5 | 2 | |
| 6 | 3 | class FrmFormApi { |
| 7 | 4 | |
| 8 | - protected $license = ''; | |
| 5 | + protected $license = ''; | |
| 9 | 6 | protected $cache_key = ''; |
| 10 | 7 | protected $cache_timeout = '+6 hours'; |
| 11 | 8 | |
| 12 | 9 | /** |
| 13 | - * The number of days an add-on is new. | |
| 14 | - * | |
| 15 | - * @var int $new_days | |
| 16 | - */ | |
| 17 | - protected $new_days = 90; | |
| 18 | - | |
| 19 | - /** | |
| 20 | 10 | * @since 3.06 |
| 21 | 11 | */ |
| 22 | 12 | public function __construct( $license = null ) { |
| 23 | 13 | $this->set_license( $license ); |
| @@ -25,10 +15,8 @@ | ||
| 25 | 15 | } |
| 26 | 16 | |
| 27 | 17 | /** |
| 28 | 18 | * @since 3.06 |
| 29 | - * | |
| 30 | - * @return void | |
| 31 | 19 | */ |
| 32 | 20 | private function set_license( $license ) { |
| 33 | 21 | if ( $license === null ) { |
| 34 | 22 | $edd_update = $this->get_pro_updater(); |
| @@ -48,10 +36,8 @@ | ||
| 48 | 36 | } |
| 49 | 37 | |
| 50 | 38 | /** |
| 51 | 39 | * @since 3.06 |
| 52 | - * | |
| 53 | - * @return void | |
| 54 | 40 | */ |
| 55 | 41 | protected function set_cache_key() { |
| 56 | 42 | $this->cache_key = 'frm_addons_l' . ( empty( $this->license ) ? '' : md5( $this->license ) ); |
| 57 | 43 | } |
| @@ -74,61 +60,37 @@ | ||
| 74 | 60 | $url .= '?l=' . urlencode( base64_encode( $this->license ) ); |
| 75 | 61 | } |
| 76 | 62 | |
| 77 | 63 | $addons = $this->get_cached(); |
| 78 | - if ( is_array( $addons ) ) { | |
| 64 | + if ( ! empty( $addons ) ) { | |
| 79 | 65 | return $addons; |
| 80 | 66 | } |
| 81 | 67 | |
| 82 | - // We need to know the version number to allow different downloads. | |
| 83 | - $agent = 'formidable/' . FrmAppHelper::plugin_version(); | |
| 84 | - if ( class_exists( 'FrmProDb' ) ) { | |
| 85 | - $agent = 'formidable-pro/' . FrmProDb::$plug_version; | |
| 86 | - } | |
| 87 | - | |
| 88 | - $response = wp_remote_get( | |
| 89 | - $url, | |
| 90 | - array( | |
| 91 | - 'timeout' => 25, | |
| 92 | - 'user-agent' => $agent . '; ' . get_bloginfo( 'url' ), | |
| 93 | - ) | |
| 94 | - ); | |
| 95 | - | |
| 68 | + $response = wp_remote_get( $url ); | |
| 96 | 69 | if ( is_array( $response ) && ! is_wp_error( $response ) ) { |
| 97 | - $addons = $response['body'] ? json_decode( $response['body'], true ) : array(); | |
| 98 | - } | |
| 70 | + $addons = $response['body']; | |
| 71 | + if ( ! empty( $addons ) ) { | |
| 72 | + $addons = json_decode( $addons, true ); | |
| 99 | 73 | |
| 100 | - if ( ! is_array( $addons ) ) { | |
| 101 | - $addons = array(); | |
| 102 | - } | |
| 103 | - | |
| 104 | - foreach ( $addons as $k => $addon ) { | |
| 105 | - if ( ! is_array( $addon ) ) { | |
| 106 | - continue; | |
| 107 | - } | |
| 108 | - | |
| 109 | - if ( isset( $addon['categories'] ) ) { | |
| 110 | - $cats = array_intersect( $this->skip_categories(), $addon['categories'] ); | |
| 111 | - if ( ! empty( $cats ) ) { | |
| 112 | - unset( $addons[ $k ] ); | |
| 113 | - continue; | |
| 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 | + } | |
| 114 | 82 | } |
| 115 | - } | |
| 116 | 83 | |
| 117 | - if ( ! array_key_exists( 'is_new', $addon ) && array_key_exists( 'released', $addon ) ) { | |
| 118 | - $addons[ $k ]['is_new'] = $this->is_new( $addon ); | |
| 84 | + $this->set_cached( $addons ); | |
| 119 | 85 | } |
| 120 | 86 | } |
| 121 | 87 | |
| 122 | - $this->set_cached( $addons ); | |
| 123 | - | |
| 124 | 88 | return $addons; |
| 125 | 89 | } |
| 126 | 90 | |
| 127 | 91 | /** |
| 128 | 92 | * @since 3.06 |
| 129 | - * | |
| 130 | - * @return string | |
| 131 | 93 | */ |
| 132 | 94 | protected function api_url() { |
| 133 | 95 | return 'https://formidableforms.com/wp-json/s11edd/v1/updates/'; |
| 134 | 96 | } |
| @@ -134,10 +96,8 @@ | ||
| 134 | 96 | } |
| 135 | 97 | |
| 136 | 98 | /** |
| 137 | 99 | * @since 3.06 |
| 138 | - * | |
| 139 | - * @return string[] | |
| 140 | 100 | */ |
| 141 | 101 | protected function skip_categories() { |
| 142 | 102 | return array( 'WordPress Form Templates', 'WordPress Form Style Templates' ); |
| 143 | 103 | } |
| @@ -143,12 +103,9 @@ | ||
| 143 | 103 | } |
| 144 | 104 | |
| 145 | 105 | /** |
| 146 | 106 | * @since 3.06 |
| 147 | - * | |
| 148 | 107 | * @param object $license_plugin The FrmAddon object |
| 149 | - * @param array $addons | |
| 150 | - * | |
| 151 | 108 | * @return array |
| 152 | 109 | */ |
| 153 | 110 | public function get_addon_for_license( $license_plugin, $addons = array() ) { |
| 154 | 111 | if ( empty( $addons ) ) { |
| @@ -154,9 +111,9 @@ | ||
| 154 | 111 | if ( empty( $addons ) ) { |
| 155 | 112 | $addons = $this->get_api_info(); |
| 156 | 113 | } |
| 157 | 114 | $download_id = $license_plugin->download_id; |
| 158 | - $plugin = array(); | |
| 115 | + $plugin = array(); | |
| 159 | 116 | if ( empty( $download_id ) && ! empty( $addons ) ) { |
| 160 | 117 | foreach ( $addons as $addon ) { |
| 161 | 118 | if ( strtolower( $license_plugin->plugin_name ) == strtolower( $addon['title'] ) ) { |
| 162 | 119 | return $addon; |
| @@ -175,9 +132,8 @@ | ||
| 175 | 132 | public function get_pro_updater() { |
| 176 | 133 | if ( FrmAppHelper::pro_is_installed() && is_callable( 'FrmProAppHelper::get_updater' ) ) { |
| 177 | 134 | $updater = FrmProAppHelper::get_updater(); |
| 178 | 135 | $this->set_license( $updater->license ); |
| 179 | - | |
| 180 | 136 | return $updater; |
| 181 | 137 | } |
| 182 | 138 | |
| 183 | 139 | return false; |
| @@ -189,38 +145,22 @@ | ||
| 189 | 145 | */ |
| 190 | 146 | protected function get_cached() { |
| 191 | 147 | $cache = get_option( $this->cache_key ); |
| 192 | 148 | |
| 193 | - FrmAppHelper::filter_gmt_offset(); | |
| 194 | - | |
| 195 | 149 | if ( empty( $cache ) || empty( $cache['timeout'] ) || current_time( 'timestamp' ) > $cache['timeout'] ) { |
| 196 | 150 | return false; // Cache is expired |
| 197 | 151 | } |
| 198 | 152 | |
| 199 | - $version = FrmAppHelper::plugin_version(); | |
| 200 | - $for_current = isset( $cache['version'] ) && $cache['version'] == $version; | |
| 201 | - if ( ! $for_current ) { | |
| 202 | - // Force a new check. | |
| 203 | - return false; | |
| 204 | - } | |
| 205 | - | |
| 206 | 153 | return json_decode( $cache['value'], true ); |
| 207 | 154 | } |
| 208 | 155 | |
| 209 | 156 | /** |
| 210 | 157 | * @since 3.06 |
| 211 | - * | |
| 212 | - * @param array $addons | |
| 213 | - * | |
| 214 | - * @return void | |
| 215 | 158 | */ |
| 216 | 159 | protected function set_cached( $addons ) { |
| 217 | - FrmAppHelper::filter_gmt_offset(); | |
| 218 | - | |
| 219 | 160 | $data = array( |
| 220 | 161 | 'timeout' => strtotime( $this->cache_timeout, current_time( 'timestamp' ) ), |
| 221 | 162 | 'value' => json_encode( $addons ), |
| 222 | - 'version' => FrmAppHelper::plugin_version(), | |
| 223 | 163 | ); |
| 224 | 164 | |
| 225 | 165 | update_option( $this->cache_key, $data, 'no' ); |
| 226 | 166 | } |
| @@ -226,10 +166,8 @@ | ||
| 226 | 166 | } |
| 227 | 167 | |
| 228 | 168 | /** |
| 229 | 169 | * @since 3.06 |
| 230 | - * | |
| 231 | - * @return void | |
| 232 | 170 | */ |
| 233 | 171 | public function reset_cached() { |
| 234 | 172 | delete_option( $this->cache_key ); |
| 235 | 173 | } |
| @@ -242,9 +180,8 @@ | ||
| 242 | 180 | $errors = array(); |
| 243 | 181 | if ( ! empty( $this->license ) ) { |
| 244 | 182 | $errors = $this->get_error_from_response(); |
| 245 | 183 | } |
| 246 | - | |
| 247 | 184 | return $errors; |
| 248 | 185 | } |
| 249 | 186 | |
| 250 | 187 | /** |
| @@ -259,20 +196,7 @@ | ||
| 259 | 196 | if ( isset( $addons['error'] ) ) { |
| 260 | 197 | $errors[] = $addons['error']['message']; |
| 261 | 198 | do_action( 'frm_license_error', $addons['error'] ); |
| 262 | 199 | } |
| 263 | - | |
| 264 | 200 | return $errors; |
| 265 | - } | |
| 266 | - | |
| 267 | - /** | |
| 268 | - * Check if a template is new. | |
| 269 | - * | |
| 270 | - * @since 6.0 | |
| 271 | - * | |
| 272 | - * @param array $addon | |
| 273 | - * @return bool | |
| 274 | - */ | |
| 275 | - protected function is_new( $addon ) { | |
| 276 | - return strtotime( $addon['released'] ) > strtotime( '-' . $this->new_days . ' days' ); | |
| 277 | 201 | } |
| 278 | 202 | } |