public function envato_market_admin_init()
{
if (!function_exists('envato_market')) {
return;
}
global $wp_settings_sections;
if (!isset($wp_settings_sections[envato_market()->get_slug()])) {
// means we're running the admin_init hook before envato market gets to setup settings area.
// good - this means our oauth prompt will appear first in the list of settings blocks
register_setting(envato_market()->get_slug(), envato_market()->get_option_name());
}
// pull our custom options across to envato.
$option = get_option('envato_setup_wizard', array());
$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);
//add_thickbox();
if (!empty($_POST['oauth_session']) && !empty($_POST['bounce_nonce']) && wp_verify_nonce($_POST['bounce_nonce'], 'envato_oauth_bounce_' . $this->envato_username)) {
// request the token from our bounce url.
$my_theme = wp_get_theme();
$oauth_nonce = get_option('envato_oauth_' . $this->envato_username);
if (!$oauth_nonce) {
// this is our 'private key' that is used to request a token from our api bounce server.
// only hosts with this key are allowed to request a token and a refresh token
// the first time this key is used, it is set and locked on the server.
$oauth_nonce = wp_create_nonce('envato_oauth_nonce_' . $this->envato_username);
update_option('envato_oauth_' . $this->envato_username, $oauth_nonce);
}
$response = wp_remote_post($this->oauth_script, array('method' => 'POST', 'timeout' => 15, 'redirection' => 1, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'body' => array('oauth_session' => $_POST['oauth_session'], 'oauth_nonce' => $oauth_nonce, 'get_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();
$class = 'error';
echo "<div class=\"{$class}\"><p>" . sprintf(esc_html__('Something went wrong while trying to retrieve oauth token: %s'), $error_message) . '</p></div>';
} else {
$token = @json_decode(wp_remote_retrieve_body($response), true);
$result = false;
if (is_array($token) && !empty($token['access_token'])) {
$token['oauth_session'] = $_POST['oauth_session'];
$result = $this->_manage_oauth_token($token);
}
if ($result !== true) {
echo 'Failed to get oAuth token. Please go back and try again';
exit;
}
}
}
add_settings_section(envato_market()->get_option_name() . '_' . $this->envato_username . '_oauth_login', sprintf(esc_html__('Login for %s updates'), $this->envato_username), array($this, 'render_oauth_login_description_callback'), envato_market()->get_slug());
// Items setting.
add_settings_field($this->envato_username . 'oauth_keys', esc_html__('oAuth Login'), array($this, 'render_oauth_login_fields_callback'), envato_market()->get_slug(), envato_market()->get_option_name() . '_' . $this->envato_username . '_oauth_login');
}