Envato_Theme_Setup_Wizard::_manage_oauth_token PHP Метод

_manage_oauth_token() приватный Метод

private _manage_oauth_token ( $token )
        private function _manage_oauth_token($token)
        {
            if (is_array($token) && !empty($token['access_token'])) {
                if (self::$_current_manage_token == $token['access_token']) {
                    return false;
                    // stop loops when refresh auth fails.
                }
                self::$_current_manage_token = $token['access_token'];
                // yes! we have an access token. store this in our options so we can get a list of items using it.
                $option = get_option('envato_setup_wizard', array());
                if (!is_array($option)) {
                    $option = array();
                }
                if (empty($option['items'])) {
                    $option['items'] = array();
                }
                // check if token is expired.
                if (empty($token['expires'])) {
                    $token['expires'] = time() + 3600;
                }
                if ($token['expires'] < time() + 120 && !empty($token['oauth_session'])) {
                    // time to renew this token!
                    $my_theme = wp_get_theme();
                    $oauth_nonce = get_option('envato_oauth_' . $this->envato_username);
                    $response = wp_remote_post($this->oauth_script, array('method' => 'POST', 'timeout' => 10, 'redirection' => 1, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array('oauth_session' => $token['oauth_session'], 'oauth_nonce' => $oauth_nonce, 'refresh_token' => 'yes', 'url' => home_url(), 'theme' => $my_theme->get('Name'), 'version' => $my_theme->get('Version')), 'cookies' => array()));
                    if (is_wp_error($response)) {
                        $error_message = $response->get_error_message();
                        echo "Something went wrong while trying to retrieve oauth token: {$error_message}";
                    } else {
                        $new_token = @json_decode(wp_remote_retrieve_body($response), true);
                        $result = false;
                        if (is_array($new_token) && !empty($new_token['new_token'])) {
                            $token['access_token'] = $new_token['new_token'];
                            $token['expires'] = time() + 3600;
                        }
                    }
                }
                // use this token to get a list of purchased items
                // add this to our items array.
                $response = envato_market()->api()->request('https://api.envato.com/v3/market/buyer/purchases', array('headers' => array('Authorization' => 'Bearer ' . $token['access_token'])));
                self::$_current_manage_token = false;
                if (is_array($response) && is_array($response['purchases'])) {
                    // up to here, add to items array
                    foreach ($response['purchases'] as $purchase) {
                        // check if this item already exists in the items array.
                        $exists = false;
                        foreach ($option['items'] as $id => $item) {
                            if (!empty($item['id']) && $item['id'] == $purchase['item']['id']) {
                                $exists = true;
                                // update token.
                                $option['items'][$id]['token'] = $token['access_token'];
                                $option['items'][$id]['token_data'] = $token;
                                $option['items'][$id]['oauth'] = $this->envato_username;
                                if (!empty($purchase['code'])) {
                                    $option['items'][$id]['purchase_code'] = $purchase['code'];
                                }
                            }
                        }
                        if (!$exists) {
                            $option['items'][] = array('id' => '' . $purchase['item']['id'], 'name' => $purchase['item']['name'], 'token' => $token['access_token'], 'token_data' => $token, 'oauth' => $this->envato_username, 'type' => !empty($purchase['item']['wordpress_theme_metadata']) ? 'theme' : 'plugin', 'purchase_code' => !empty($purchase['code']) ? $purchase['code'] : '');
                        }
                    }
                } else {
                    return false;
                }
                if (!isset($option['oauth'])) {
                    $option['oauth'] = array();
                }
                // store our 1 hour long token here. we can refresh this token when it comes time to use it again (i.e. during an update)
                $option['oauth'][$this->envato_username] = $token;
                update_option('envato_setup_wizard', $option);
                $envato_options = envato_market()->get_options();
                $envato_options = $this->_array_merge_recursive_distinct($envato_options, $option);
                update_option(envato_market()->get_option_name(), $envato_options);
                envato_market()->items()->set_themes(true);
                envato_market()->items()->set_plugins(true);
                return true;
            } else {
                return false;
            }
        }