Changeset 3746820 for formidable/trunk/classes/models/FrmAddon.php
- Timestamp:
- 10/14/2026 04:54:36 PM (2 weeks ago)
- File:
-
- 1 edited
-
formidable/trunk/classes/models/FrmAddon.php (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
formidable/trunk/classes/models/FrmAddon.php
r3688662 r3746820 81 81 */ 82 82 protected $save_status; 83 84 /** 85 * Whether the last license request returned a decoded JSON response. 86 * 87 * @since 7.0 88 * 89 * @var bool 90 */ 91 private $license_response_is_json = false; 83 92 84 93 /** … … 586 595 $response = $this->activate_license( $license ); 587 596 588 if ( ! $response['success'] ) { 597 // Drop the license only when the API reported on it. An unreachable API says 598 // nothing, so the defined license is kept and checked again later. 599 if ( ! $response['success'] && empty( $response['inconclusive'] ) ) { 589 600 $license = ''; 590 601 } … … 656 667 * 657 668 * @since 6.8.3 658 * 659 * @param bool|string $is_valid If license activation was successful. May be a string 'valid'. 660 * 661 * @return void 662 */ 663 protected function maybe_set_active( $is_valid ) { 669 * @since 7.0 Added the $is_conclusive param. 670 * 671 * @param bool|string $is_valid If license activation was successful. May be a string 'valid'. 672 * @param bool $is_conclusive Whether the API reported on the license. False when the 673 * check could not reach a verdict, such as a connection 674 * error, in which case the saved license is left alone. 675 * 676 * @return void 677 */ 678 protected function maybe_set_active( $is_valid, $is_conclusive = true ) { 679 if ( ! $is_valid && ! $is_conclusive ) { 680 // Nothing came back about this license, so don't discard what is saved. 681 return; 682 } 683 664 684 update_option( $this->option_name . 'active', $is_valid ); 665 685 … … 1003 1023 $response = $this->get_license_status(); 1004 1024 1005 if ( ! empty( $this->save_status['response_code'] ) && 429 === $this->save_status['response_code'] ) { 1006 // If we got a rate limit response, don't clear the license. 1025 if ( ! empty( $response['inconclusive'] ) ) { 1026 // The API did not report on this license, so there is nothing to act on. A 1027 // license that activated before stays in place until the API says otherwise. 1007 1028 return; 1008 1029 } … … 1143 1164 */ 1144 1165 private function activate_license( $license ) { 1145 $ this->set_license( $license );1146 $this->license = $license;1166 $previous_license = $this->license; 1167 $this->license = $license; 1147 1168 1148 1169 $this->die_if_not_allowed(); … … 1154 1175 $is_valid = false; 1155 1176 1156 if ( $response['error'] ) { 1157 $response['message'] = $response['status']; 1177 if ( $response['inconclusive'] ) { 1178 $response['message'] = $response['status'] ? $response['status'] : __( 'You had an error communicating with the Formidable API.', 'formidable' ); 1179 $this->license = $previous_license; 1158 1180 } else { 1181 $this->set_license( $license ); 1159 1182 $messages = $this->get_messages(); 1160 1183 … … 1169 1192 $response['success'] = true; 1170 1193 } 1171 1172 $this->maybe_set_active( $is_valid ); 1173 } 1174 1194 } 1195 1196 $this->maybe_set_active( $is_valid, ! $response['inconclusive'] ); 1197 1198 // Record the check under the license now in place, even when the API gave no 1199 // verdict. Otherwise a defined license is retried, and refused, on the next page load. 1175 1200 $this->update_last_checked( (bool) $is_valid ); 1176 1201 1202 if ( $is_valid ) { 1203 // Setting the license active clears the license caches, so the new license details are available here. 1204 return $this->add_activation_response_data( $response ); 1205 } 1206 1207 return $response; 1208 } 1209 1210 /** 1211 * Add extra details to the response for a license that just activated. 1212 * Pro overrides this to include the license type, so the license message 1213 * can be updated without reloading the page. 1214 * 1215 * @since 7.0 1216 * 1217 * @param array $response The activation response. 1218 * 1219 * @return array 1220 */ 1221 protected function add_activation_response_data( $response ) { 1177 1222 return $response; 1178 1223 } … … 1198 1243 1199 1244 /** 1245 * @since 7.0 Added the inconclusive key, so a check that never reached the API 1246 * can be told apart from one where the API reported on the license. 1247 * 1200 1248 * @return array 1201 1249 */ … … 1204 1252 1205 1253 $response = array( 1206 'status' => 'missing', 1207 'error' => true, 1254 'status' => '', 1255 'error' => true, 1256 'inconclusive' => true, 1208 1257 ); 1209 1258 1210 1259 if ( ! $this->license ) { 1211 $response['error'] = false; 1260 $response['status'] = 'missing'; 1261 $response['error'] = false; 1262 $response['inconclusive'] = false; 1212 1263 return $response; 1213 1264 } … … 1216 1267 1217 1268 try { 1218 $response['error'] = false; 1219 $license_data = $this->send_mothership_request( 'activate_license' ); 1220 1221 // $license_data->license will be either "valid" or "invalid" 1269 $license_data = $this->send_mothership_request( 'activate_license' ); 1270 $status = ''; 1271 1272 // $license_data['license'] will be a status such as "valid" or "invalid". A 1273 // string response is an error message rather than a license status. 1222 1274 if ( is_array( $license_data ) ) { 1223 if ( ! empty( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) { 1224 $response['status'] = $license_data['license']; 1225 $this->save_status['status'] = $license_data['license']; 1226 $is_valid = 'valid' === $license_data['license']; 1275 if ( ! empty( $license_data['license'] ) && is_string( $license_data['license'] ) ) { 1276 $status = $license_data['license']; 1227 1277 } 1228 } else { 1229 $response['status'] = $license_data; 1278 } elseif ( is_string( $license_data ) ) { 1279 $status = $license_data; 1280 } 1281 1282 if ( $status ) { 1283 $response['status'] = $status; 1284 } 1285 1286 if ( $status && $this->license_response_is_json && 429 !== (int) ( $this->save_status['response_code'] ?? 0 ) ) { 1287 $response['error'] = false; 1288 $response['inconclusive'] = false; 1289 $is_valid = 'valid' === $status; 1290 1291 if ( in_array( $status, array( 'valid', 'invalid' ), true ) ) { 1292 $this->save_status['status'] = $status; 1293 } 1230 1294 } 1231 1295 } catch ( Exception $e ) { 1232 1296 $response['status'] = $e->getMessage(); 1233 } 1297 }//end try 1234 1298 1235 1299 $this->update_last_checked( $is_valid ); … … 1320 1384 1321 1385 /** 1322 * @param string $action 1323 * 1324 * @return string 1386 * @since 7.0 The return type covers the decoded body, which is an array when the 1387 * API answers with JSON, and an error message string otherwise. 1388 * 1389 * @param string $action The request to send, such as activate_license. 1390 * 1391 * @return array|string 1325 1392 */ 1326 1393 public function send_mothership_request( $action ) { 1327 $api_params = array( 1394 $this->license_response_is_json = false; 1395 $api_params = array( 1328 1396 'edd_action' => $action, 1329 1397 'license' => $this->license, … … 1365 1433 1366 1434 if ( null !== $json_res ) { 1367 $message = is_array( $json_res ) && isset( $json_res['error'] ) ? $json_res['error'] : $json_res; 1435 $this->license_response_is_json = true; 1436 $message = is_array( $json_res ) && isset( $json_res['error'] ) ? $json_res['error'] : $json_res; 1368 1437 } elseif ( ! empty( $resp['response'] ) && ! empty( $resp['response']['code'] ) ) { 1369 1438 $resp['body'] = wp_strip_all_tags( $resp['body'] );
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)