tokens_empty( $_POST['strava_token'] ) ) { return array(); } // Redirect only if all the right options are in place. if ( isset( $value[0]['type'] ) && 'updated' === $value[0]['type'] ) { // Make sure there were no settings errors. if ( isset( $_POST['option_page'] ) && $_POST['option_page'] === $this->option_page ) { // Make sure we're on our settings page. // Only re-auth if client ID and secret were saved. if ( ! empty( $_POST['strava_client_id'] ) && ! empty( $_POST['strava_client_secret'] ) ) { $client_id = $_POST['strava_client_id']; $client_secret = $_POST['strava_client_secret']; $redirect = rawurlencode( admin_url( "options-general.php?page={$this->page_name}" ) ); $url = "https://www.strava.com/oauth/authorize?client_id={$client_id}&response_type=code&redirect_uri={$redirect}&approval_prompt=force"; wp_redirect( $url ); exit(); } } } return $value; } public function add_strava_menu() { add_options_page( __( 'Strava Settings', 'wp-strava' ), __( 'Strava', 'wp-strava' ), 'manage_options', $this->page_name, array( $this, 'print_strava_options' ) ); } public function init() { $this->tokens = $this->get_tokens(); // Only validate additional athlete information if all fields are present. $this->adding_athlete = ! ( empty( $_POST['strava_client_id'] ) && empty( $_POST['strava_client_secret'] ) ); //only update when redirected back from strava if ( ! isset( $_GET['settings-updated'] ) && isset( $_GET['page'] ) && $_GET['page'] === $this->page_name ) { if ( isset( $_GET['code'] ) ) { $token = $this->fetch_token( $_GET['code'] ); if ( $token ) { // Translators: strava token add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'New Strava token retrieved. %s', 'wp-strava' ), $this->feedback ), 'updated' ); $this->add_token( $token ); update_option( 'strava_token', $this->tokens ); } else { add_settings_error( 'strava_token', 'strava_token', $this->feedback ); } } elseif ( isset( $_GET['error'] ) ) { // Translators: authentication error mess add_settings_error( 'strava_token', 'strava_token', sprintf( __( 'Error authenticating at Strava: %s', 'wp-strava' ), str_replace( '_', ' ', $_GET['error'] ) ) ); } } } public function register_strava_settings() { $this->init(); add_settings_section( 'strava_api', __( 'Strava API', 'wp-strava' ), array( $this, 'print_api_instructions' ), 'wp-strava' ); if ( $this->tokens_empty( $this->tokens ) ) { register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) ); register_setting( $this->option_page, 'strava_client_secret', array( $this, 'sanitize_client_secret' ) ); register_setting( $this->option_page, 'strava_nickname', array( $this, 'sanitize_nickname' ) ); add_settings_field( 'strava_client_id', __( 'Strava Client ID', 'wp-strava' ), array( $this, 'print_client_input' ), 'wp-strava', 'strava_api' ); add_settings_field( 'strava_client_secret', __( 'Strava Client Secret', 'wp-strava' ), array( $this, 'print_secret_input' ), 'wp-strava', 'strava_api' ); add_settings_field( 'strava_nickname', __( 'Strava Nickname', 'wp-strava' ), array( $this, 'print_nickname_input' ), 'wp-strava', 'strava_api' ); } else { register_setting( $this->option_page, 'strava_token', array( $this, 'sanitize_token' ) ); add_settings_field( 'strava_token', __( 'Strava Token', 'wp-strava' ), array( $this, 'print_token_input' ), 'wp-strava', 'strava_api' ); // Add additional fields register_setting( $this->option_page, 'strava_client_id', array( $this, 'sanitize_client_id' ) ); register_setting( $this->option_page, 'strava_client_secret', array( $this, 'sanitize_client_secret' ) ); register_setting( $this->option_page, 'strava_nickname', array( $this, 'sanitize_nickname' ) ); add_settings_field( 'strava_client_id', __( 'Additional Athlete Client ID', 'wp-strava' ), array( $this, 'print_client_input' ), 'wp-strava', 'strava_api' ); add_settings_field( 'strava_client_secret', __( 'Additional Athlete Client Secret', 'wp-strava' ), array( $this, 'print_secret_input' ), 'wp-strava', 'strava_api' ); add_settings_field( 'strava_nickname', __( 'Additional Athlete Nickname', 'wp-strava' ), array( $this, 'print_nickname_input' ), 'wp-strava', 'strava_api' ); } // Google Maps API. register_setting( $this->option_page, 'strava_gmaps_key', array( $this, 'sanitize_gmaps_key' ) ); add_settings_section( 'strava_gmaps', __( 'Google Maps', 'wp-strava' ), array( $this, 'print_gmaps_instructions' ), 'wp-strava' ); add_settings_field( 'strava_gmaps_key', __( 'Static Maps Key', 'wp-strava' ), array( $this, 'print_gmaps_key_input' ), 'wp-strava', 'strava_gmaps' ); // System of Measurement. register_setting( $this->option_page, 'strava_som', array( $this, 'sanitize_som' ) ); add_settings_section( 'strava_options', __( 'Options', 'wp-strava' ), null, 'wp-strava' ); add_settings_field( 'strava_som', __( 'System of Measurement', 'wp-strava' ), array( $this, 'print_som_input' ), 'wp-strava', 'strava_options' ); // Hide Time Option. register_setting( $this->option_page, 'strava_hide_time', array( $this, 'sanitize_hide_time' ) ); add_settings_field( 'strava_hide_time', __( 'Hide Activity Time', 'wp-strava' ), array( $this, 'print_hide_time_input' ), 'wp-strava', 'strava_options' ); // Clear cache. register_setting( $this->option_page, 'strava_cache_clear', array( $this, 'sanitize_cache_clear' ) ); add_settings_section( 'strava_cache', __( 'Cache', 'wp-strava' ), null, 'wp-strava' ); add_settings_field( 'strava_cache_clear', __( 'Clear cache (images & transient data)', 'wp-strava' ), array( $this, 'print_clear_input' ), 'wp-strava', 'strava_cache' ); } public function print_api_instructions() { $signup_url = 'http://www.strava.com/developers'; $settings_url = 'https://www.strava.com/settings/api'; $icon_url = 'https://plugins.svn.wordpress.org/wp-strava/assets/icon-128x128.png'; $blog_name = get_bloginfo( 'name' ); // Translators: Strava "app" name $app_name = sprintf( __( '%s Strava', 'wp-strava' ), $blog_name ); $site_url = site_url(); // Translators: Strava "app" description $description = sprintf( __( 'WP-Strava for %s', 'wp-strava' ), $blog_name ); printf( __( "
Steps:
Steps:
%s', 'wp-strava' ), print_r( $strava_info, true ) ); // phpcs:ignore -- Debug output. return false; } $this->feedback .= __( 'Missing Client ID or Client Secret.', 'wp-strava' ); return false; } public function print_gmaps_key_input() { ?> * @since 1.7.1 */ public function print_hide_time_input() { ?> hide_time, 'on' ); ?>/> * @since 1.7.1 */ public function sanitize_hide_time( $checked ) { if ( 'on' === $checked ) { return $checked; } return null; } public function print_clear_input() { ?> query( "DELETE FROM {$wpdb->options} WHERE `option_name` LIKE '_transient_timeout_strava_latest_map_%'" ); $wpdb->query( "DELETE FROM {$wpdb->options} WHERE `option_name` LIKE '_transient_strava_latest_map_%'" ); $wpdb->query( "DELETE FROM {$wpdb->options} WHERE `option_name` LIKE 'strava_latest_map%'" ); } return null; } /** * Gets all saved strava tokens as an array. * * @return array * @author Justin Foell