Plugin Directory


Ignore:
Timestamp:
10/14/2026 04:54:36 PM (2 weeks ago)
Author:
formidableforms
Message:

Update to version 7.0 from GitHub

File:
1 edited

Legend:

Unmodified
Added
Removed
  • formidable/trunk/classes/models/FrmAddon.php

    r3688662 r3746820  
    8181         */
    8282        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;
    8392
    8493        /**
    … …  
    586595                        $response = $this->activate_license( $license );
    587596
    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'] ) ) {
    589600                                $license = '';
    590601                        }
    … …  
    656667         *
    657668         * @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
    664684                update_option( $this->option_name . 'active', $is_valid );
    665685
    … …  
    10031023                $response = $this->get_license_status();
    10041024
    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.
    10071028                        return;
    10081029                }
    … …  
    11431164         */
    11441165        private function activate_license( $license ) {
    1145                 $this->set_license( $license );
    1146                 $this->license = $license;
     1166                $previous_license = $this->license;
     1167                $this->license    = $license;
    11471168
    11481169                $this->die_if_not_allowed();
    … …  
    11541175                $is_valid = false;
    11551176
    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;
    11581180                } else {
     1181                        $this->set_license( $license );
    11591182                        $messages = $this->get_messages();
    11601183
    … …  
    11691192                                $response['success'] = true;
    11701193                        }
    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.
    11751200                $this->update_last_checked( (bool) $is_valid );
    11761201
     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 ) {
    11771222                return $response;
    11781223        }
    … …  
    11981243
    11991244        /**
     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         *
    12001248         * @return array
    12011249         */
    … …  
    12041252
    12051253                $response = array(
    1206                         'status' => 'missing',
    1207                         'error'  => true,
     1254                        'status'       => '',
     1255                        'error'        => true,
     1256                        'inconclusive' => true,
    12081257                );
    12091258
    12101259                if ( ! $this->license ) {
    1211                         $response['error'] = false;
     1260                        $response['status']       = 'missing';
     1261                        $response['error']        = false;
     1262                        $response['inconclusive'] = false;
    12121263                        return $response;
    12131264                }
    … …  
    12161267
    12171268                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.
    12221274                        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'];
    12271277                                }
    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                                }
    12301294                        }
    12311295                } catch ( Exception $e ) {
    12321296                        $response['status'] = $e->getMessage();
    1233                 }
     1297                }//end try
    12341298
    12351299                $this->update_last_checked( $is_valid );
    … …  
    13201384
    13211385        /**
    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
    13251392         */
    13261393        public function send_mothership_request( $action ) {
    1327                 $api_params = array(
     1394                $this->license_response_is_json = false;
     1395                $api_params                     = array(
    13281396                        'edd_action' => $action,
    13291397                        'license'    => $this->license,
    … …  
    13651433
    13661434                        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;
    13681437                        } elseif ( ! empty( $resp['response'] ) && ! empty( $resp['response']['code'] ) ) {
    13691438                                $resp['body'] = wp_strip_all_tags( $resp['body'] );
Note: See TracChangeset for help on using the changeset viewer.