2013-02-03 20:38:55 -06:00
< ? php
2014-09-10 23:35:08 -05:00
/**
2017-12-08 12:33:29 -06:00
* V3 - http://strava.github.io/api/v3/oauth/
2017-05-10 12:17:25 -05:00
*
2014-09-10 23:35:08 -05:00
* Set up an "API Application" at Strava
* Save the Client ID and Client Secret in WordPress - redirect to strava oauth/authorize URL for permission
2014-09-12 12:28:53 -05:00
* Get redirected back to this settings page with ?code= or ?error=
2014-09-10 23:35:08 -05:00
* Use code to retrieve auth token
*/
2013-02-03 20:38:55 -06:00
class WPStrava_Settings {
2019-04-01 16:21:13 -05:00
private $ids = array ();
2017-12-26 14:18:13 -06:00
private $page_name = 'wp-strava-options' ;
private $option_page = 'wp-strava-settings-group' ;
2017-07-21 23:20:43 -05:00
private $adding_athlete = true ;
2017-05-10 12:17:25 -05:00
2019-09-21 17:59:40 -05:00
/**
* Settings constructor.
*
* @author Justin Foell <justin@foell.org>
* @since 2.0
*/
public function __construct () {
$this -> ids = $this -> get_ids ();
}
2019-08-02 11:54:40 -05:00
/**
* Register actions & filters for menus and authentication.
*
* @author Justin Foell <justin@foell.org>
* @since 0.62
*/
2013-02-03 20:38:55 -06:00
public function hook () {
2013-02-17 21:19:08 -06:00
add_action ( 'admin_init' , array ( $this , 'register_strava_settings' ) );
add_action ( 'admin_menu' , array ( $this , 'add_strava_menu' ) );
2014-09-23 13:39:48 -05:00
add_filter ( 'plugin_action_links_' . WPSTRAVA_PLUGIN_NAME , array ( $this , 'settings_link' ) );
2019-09-21 22:02:26 -05:00
add_action ( 'in_plugin_update_message-wp-strava/wp-strava.php' , array ( $this , 'plugin_update_message' ), 10 , 2 );
add_action ( 'after_plugin_row_wp-strava/wp-strava.php' , array ( $this , 'ms_plugin_update_message' ), 10 , 2 );
2014-09-10 23:35:08 -05:00
}
2019-08-02 11:54:40 -05:00
/**
* Add the strava settings menu.
*
* @author Justin Foell <justin@foell.org>
* @since 0.62
*/
2013-02-17 21:19:08 -06:00
public function add_strava_menu () {
2017-12-08 12:33:29 -06:00
add_options_page (
__ ( 'Strava Settings' , 'wp-strava' ),
__ ( 'Strava' , 'wp-strava' ),
'manage_options' ,
$this -> page_name ,
array ( $this , 'print_strava_options' )
);
2013-02-03 20:38:55 -06:00
}
2019-08-02 11:54:40 -05:00
/**
* Register settings using the WP Settings API.
*
* @author Justin Foell <justin@foell.org>
* @since 0.62
*/
2013-02-17 21:19:08 -06:00
public function register_strava_settings () {
2017-05-10 13:50:39 -05:00
add_settings_section ( 'strava_api' , __ ( 'Strava API' , 'wp-strava' ), array ( $this , 'print_api_instructions' ), 'wp-strava' );
2013-02-17 21:19:08 -06:00
2019-04-01 14:50:36 -05:00
$this -> adding_athlete = $this -> is_adding_athlete ();
2019-04-01 16:21:13 -05:00
if ( $this -> ids_empty ( $this -> ids ) ) {
2014-09-10 23:35:08 -05:00
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' ) );
2017-07-21 16:12:15 -05:00
register_setting ( $this -> option_page , 'strava_nickname' , array ( $this , 'sanitize_nickname' ) );
2013-02-17 22:14:10 -06:00
2014-09-10 23:35:08 -05:00
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' );
2017-07-21 16:12:15 -05:00
add_settings_field ( 'strava_nickname' , __ ( 'Strava Nickname' , 'wp-strava' ), array ( $this , 'print_nickname_input' ), 'wp-strava' , 'strava_api' );
2014-09-10 23:35:08 -05:00
} else {
2019-04-01 16:21:13 -05:00
register_setting ( $this -> option_page , 'strava_id' , array ( $this , 'sanitize_id' ) );
add_settings_field ( 'strava_id' , __ ( 'Saved ID' , 'wp-strava' ), array ( $this , 'print_id_input' ), 'wp-strava' , 'strava_api' );
2017-07-21 16:12:15 -05:00
// 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' );
2014-09-10 23:35:08 -05:00
}
2017-05-10 12:17:25 -05:00
2017-05-10 13:50:39 -05:00
// 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' );
2013-02-17 22:14:10 -06:00
2017-05-10 13:50:39 -05:00
// 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' );
2013-02-17 22:14:10 -06:00
add_settings_field ( 'strava_som' , __ ( 'System of Measurement' , 'wp-strava' ), array ( $this , 'print_som_input' ), 'wp-strava' , 'strava_options' );
2017-05-10 14:39:00 -05:00
2019-08-02 11:54:40 -05:00
// Hide Options.
2019-03-01 15:21:02 -06:00
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' );
2019-08-02 11:54:40 -05:00
register_setting ( $this -> option_page , 'strava_hide_elevation' , array ( $this , 'sanitize_hide_elevation' ) );
add_settings_field ( 'strava_hide_elevation' , __ ( 'Hide Activity Elevation' , 'wp-strava' ), array ( $this , 'print_hide_elevation_input' ), 'wp-strava' , 'strava_options' );
2019-03-01 15:21:02 -06:00
2017-05-10 14:39:00 -05:00
// 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' );
2013-02-03 20:38:55 -06:00
}
2019-08-02 11:54:40 -05:00
/**
* Print the Strava setup instructions.
*
* @author Justin Foell <justin@foell.org>
* @since 0.62
*/
2013-02-17 22:14:10 -06:00
public function print_api_instructions () {
2014-09-11 09:07:25 -05:00
$settings_url = 'https://www.strava.com/settings/api' ;
2017-12-26 14:18:13 -06:00
$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 );
2017-05-10 12:17:25 -05:00
$site_url = site_url ();
2017-12-26 14:18:13 -06:00
// Translators: Strava "app" description
$description = sprintf ( __ ( 'WP-Strava for %s' , 'wp-strava' ), $blog_name );
2017-12-08 12:33:29 -06:00
printf ( __ ( " <p>Steps:</p>
2014-09-10 23:35:08 -05:00
<ol>
2018-01-26 13:27:57 -06:00
<li>Create your free API Application/Connection here: <a href='%1 \$ s'>%2 \$ s</a> using the following information:</li>
2014-09-10 23:35:08 -05:00
<ul>
2018-01-26 13:27:57 -06:00
<li>App Icon: <strong>upload <a href='%3 \$ s'>this image</a></strong></li>
2017-09-29 11:25:22 -05:00
<li>Application Name: <strong>%4 \$ s</strong></li>
<li>Category: OK to leave at default 'other'</li>
<li>Club: OK to leave blank</li>
<li>Website: <strong>%5 \$ s</strong></li>
<li>Application Description: <strong>%6 \$ s</strong></li>
<li>Authorization Callback Domain: <strong>%7 \$ s</strong></li>
2014-09-10 23:35:08 -05:00
</ul>
2017-05-10 12:17:25 -05:00
<li>Once you've created your API Application at strava.com, enter the <strong>Client ID</strong> and <strong>Client Secret</strong> below, which can now be found on that same strava API Settings page.
2019-04-01 16:21:13 -05:00
<li>After saving your Client ID and Secret, you'll be redirected to strava to authorize your API Application. If successful, your Strava ID will display in a table, next to your nickname.</li>
<li>If you need to re-authorize your API Application, erase your Strava ID next to your nickname and click 'Save Changes' to start over.</li>
2017-09-29 11:25:22 -05:00
</ol> " , 'wp-strava' ),
$settings_url ,
$settings_url ,
$icon_url ,
$app_name ,
$site_url ,
$description ,
wp_parse_url ( $site_url , PHP_URL_HOST )
);
2013-02-17 22:14:10 -06:00
}
2017-05-10 12:17:25 -05:00
2019-08-02 11:54:40 -05:00
/**
* Print the google maps instructions.
*
* @author Justin Foell <justin@foell.org>
* @since 1.1
*/
2017-05-10 13:50:39 -05:00
public function print_gmaps_instructions () {
$maps_url = 'https://developers.google.com/maps/documentation/static-maps/' ;
2017-12-08 12:33:29 -06:00
printf ( __ ( " <p>Steps:</p>
2017-05-10 13:50:39 -05:00
<ol>
2018-01-26 13:27:57 -06:00
<li>To use Google map images, you must create a Static Maps API Key. Create a free key by going here: <a href='%1 \$ s'>%2 \$ s</a> and clicking <strong>Get a Key</strong></li>
2017-05-10 13:50:39 -05:00
<li>Once you've created your Google Static Maps API Key, enter the key below.
</ol> " , 'wp-strava' ), $maps_url , $maps_url );
}
2019-08-02 11:54:40 -05:00
/**
* Print the settings page container.
*
* @author Justin Foell <justin@foell.org>
* @since 0.62
*/
2013-02-17 21:19:08 -06:00
public function print_strava_options () {
2019-04-01 16:21:13 -05:00
include WPSTRAVA_PLUGIN_DIR . 'templates/admin-settings.php' ;
2013-02-03 20:38:55 -06:00
}
2014-09-10 23:35:08 -05:00
2019-08-02 11:54:40 -05:00
/**
* Print the client ID input
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
*/
2014-09-10 23:35:08 -05:00
public function print_client_input () {
2017-12-26 14:18:13 -06:00
?>
<input type="text" id="strava_client_id" name="strava_client_id" value="" />
<?php
2013-02-03 20:38:55 -06:00
}
2019-08-02 11:54:40 -05:00
/**
* Print the client secret input
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
*/
2014-09-10 23:35:08 -05:00
public function print_secret_input() {
2017-12-26 14:18:13 -06:00
?>
<input type="text" id="strava_client_secret" name="strava_client_secret" value="" />
<?php
2017-07-21 16:12:15 -05:00
}
2019-08-02 11:54:40 -05:00
/**
* Print the nickname input
*
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
*/
2017-07-21 16:12:15 -05:00
public function print_nickname_input() {
2019-04-01 16:21:13 -05:00
$nickname = $this->ids_empty( $this->ids ) ? __( 'Default', 'wp-strava' ) : '';
2017-12-26 14:18:13 -06:00
?>
<input type="text" name="strava_nickname[]" value="<?php echo $nickname; ?>" />
<?php
2013-02-03 20:38:55 -06:00
}
2019-08-02 11:54:40 -05:00
/**
2019-09-13 12:13:44 -05:00
* Print the strava ID(s).
*
* Renamed from print_token_input().
2019-08-02 11:54:40 -05:00
*
* @author Justin Foell <justin@foell.org>
2019-09-13 12:13:44 -05:00
* @since 2.0
2019-08-02 11:54:40 -05:00
*/
2019-04-01 16:21:13 -05:00
public function print_id_input() {
foreach ( $this->get_all_ids() as $id => $nickname ) {
2017-07-21 16:12:15 -05:00
?>
2019-04-01 16:21:13 -05:00
<input type="text" name="strava_id[]" value="<?php echo esc_attr( $id ); ?>" />
<input type="text" name="strava_nickname[]" value="<?php echo esc_attr( $nickname ); ?>" />
2017-07-21 16:12:15 -05:00
<br/>
<?php
}
2013-02-03 20:38:55 -06:00
}
2019-08-02 11:54:40 -05:00
/**
* Sanitize the client ID.
*
* @param string $client_id
* @return string
2019-08-02 14:57:22 -05:00
* @author Justin Foell <justin@foell.org>
2019-08-02 11:54:40 -05:00
* @since 1.2.0
*/
2014-09-10 23:35:08 -05:00
public function sanitize_client_id( $client_id ) {
2017-07-21 23:20:43 -05:00
// Return early if not trying to add an additional athlete.
if ( ! $this->adding_athlete ) {
return $client_id;
}
2014-09-10 23:35:08 -05:00
if ( ! is_numeric( $client_id ) ) {
add_settings_error( 'strava_client_id', 'strava_client_id', __( 'Client ID must be a number.', 'wp-strava' ) );
}
return $client_id;
}
2019-08-02 11:54:40 -05:00
/**
* Sanitize the client secret.
*
* @param string $client_secret
* @return string
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.2.0
*/
2014-09-10 23:35:08 -05:00
public function sanitize_client_secret( $client_secret ) {
2017-07-21 23:20:43 -05:00
// Return early if not trying to add an additional athlete.
if ( ! $this->adding_athlete ) {
return $client_secret;
}
2017-12-26 14:18:13 -06:00
if ( '' === trim( $client_secret ) ) {
2014-09-10 23:35:08 -05:00
add_settings_error( 'strava_client_secret', 'strava_client_secret', __( 'Client Secret is required.', 'wp-strava' ) );
}
return $client_secret;
2013-02-03 20:38:55 -06:00
}
2019-08-02 11:54:40 -05:00
/**
* Sanitize the nicknames - make sure we've got the same number of nicknames sa tokens.
*
* @param array $nicknames Nicknames for the athletes saved.
* @return array
* @author Justin Foell <justin@foell.org>
* @since 1.2.0
*/
2017-07-21 16:12:15 -05:00
public function sanitize_nickname( $nicknames ) {
2017-07-21 23:20:43 -05:00
if ( ! $this->adding_athlete ) {
2017-08-11 17:28:29 -05:00
2019-04-01 16:21:13 -05:00
// Chop $nicknames to same size as ids.
$nicknames = array_slice( $nicknames, 0, count( $_POST['strava_id'] ) );
2017-07-21 23:20:43 -05:00
2019-04-01 16:21:13 -05:00
// Remove indexes from $nicknames that have empty ids.
foreach ( $_POST['strava_id'] as $index => $id ) {
$id = trim( $id );
if ( empty( $id ) ) {
2017-12-08 12:33:29 -06:00
unset( $nicknames[ $index ] );
}
2017-07-21 23:20:43 -05:00
}
2017-12-08 12:33:29 -06:00
// Process $nicknames so indexes start with zero.
$nicknames = array_merge( $nicknames, array() );
2017-07-21 23:20:43 -05:00
}
foreach ( $nicknames as $index => $nickname ) {
2017-12-26 14:18:13 -06:00
if ( '' === trim( $nickname ) ) {
2017-07-21 16:12:15 -05:00
add_settings_error( 'strava_nickname', 'strava_nickname', __( 'Nickname is required.', 'wp-strava' ) );
return $nicknames;
}
}
return $nicknames;
}
2019-08-02 11:54:40 -05:00
/**
2019-09-13 12:13:44 -05:00
* Sanitize the ID.
*
* Renamed from sanitize_token().
2019-08-02 11:54:40 -05:00
*
* @param string $token
* @return string
* @author Justin Foell <justin@foell.org>
2019-09-13 12:13:44 -05:00
* @since 2.0
2019-08-02 11:54:40 -05:00
*/
2019-04-01 16:21:13 -05:00
public function sanitize_id( $id ) {
return $id;
2013-02-03 20:38:55 -06:00
}
2019-08-02 11:54:40 -05:00
/**
* Print the GMaps key input.
*
* @author Justin Foell <justin@foell.org>
* @since 1.1
*/
2017-05-10 13:50:39 -05:00
public function print_gmaps_key_input() {
2017-12-26 14:18:13 -06:00
?>
<input type="text" id="strava_gmaps_key" name="strava_gmaps_key" value="<?php echo $this->gmaps_key; ?>" />
<?php
2017-05-10 13:50:39 -05:00
}
2019-08-02 11:54:40 -05:00
/**
* Sanitize GMaps key input.
*
* @param string $key
* @return string
* @author Justin Foell <justin@foell.org>
* @since 1.1
*/
2017-05-10 13:50:39 -05:00
public function sanitize_gmaps_key( $key ) {
return $key;
}
2019-08-02 11:54:40 -05:00
/**
* Print System of Measure option.
*
* @author Justin Foell <justin@foell.org>
* @since 0.62
*/
2013-02-17 22:14:10 -06:00
public function print_som_input() {
?>
2014-09-10 23:35:08 -05:00
<select id="strava_som" name="strava_som">
2017-12-26 14:18:13 -06:00
<option value="metric" <?php selected( $this->som, 'metric' ); ?>><?php esc_html_e( 'Metric', 'wp-strava' ); ?></option>
<option value="english" <?php selected( $this->som, 'english' ); ?>><?php esc_html_e( 'English', 'wp-strava' ); ?></option>
2014-09-10 23:35:08 -05:00
</select>
2013-02-17 22:14:10 -06:00
<?php
}
2019-08-02 11:54:40 -05:00
/**
* Sanitize System of Measure input.
*
* @param string $som Input from System of Measure dropdown.
* @return string
* @author Justin Foell <justin@foell.org>
* @since 0.62
*/
2013-02-17 22:14:10 -06:00
public function sanitize_som( $som ) {
return $som;
}
2019-03-01 15:21:02 -06:00
/**
* Display the Hide Time Checkbox.
*
* @author Justin Foell <justin@foell.org>
* @since 1.7.1
*/
public function print_hide_time_input() {
?>
<input type="checkbox" id="strava_hide_time" name="strava_hide_time" <?php checked( $this->hide_time, 'on' ); ?>/>
<?php
}
/**
* Sanitize the Hide Time Checkbox.
*
* @param string $checked 'on' or null.
* @return string 'on' if checked.
* @author Justin Foell <justin@foell.org>
* @since 1.7.1
*/
public function sanitize_hide_time( $checked ) {
if ( 'on' === $checked ) {
return $checked;
}
return null;
}
2019-08-02 14:23:46 -05:00
/**
* Display the Hide Elevation Checkbox.
*
* @author Justin Foell <justin@foell.org>
* @since 1.7.2
*/
public function print_hide_elevation_input() {
?>
<input type="checkbox" id="strava_hide_elevation" name="strava_hide_elevation" <?php checked( $this->hide_elevation, 'on' ); ?>/>
<?php
}
/**
* Sanitize the Hide Elevation Checkbox.
*
* @param string $checked 'on' or null.
* @return string 'on' if checked.
* @author Justin Foell <justin@foell.org>
* @since 1.7.2
*/
public function sanitize_hide_elevation( $checked ) {
if ( 'on' === $checked ) {
return $checked;
}
return null;
}
2019-08-02 11:54:40 -05:00
/**
* Print checkbox option to clear cache.
*
* @author Justin Foell <justin@foell.org>
* @since 1.1
*/
2017-05-10 14:39:00 -05:00
public function print_clear_input() {
2017-12-26 14:18:13 -06:00
?>
<input type="checkbox" id="strava_cache_clear" name="strava_cache_clear" />
<?php
2017-05-10 14:39:00 -05:00
}
2019-08-02 11:54:40 -05:00
/**
* Clear Strava cache if checkbox is checked.
*
* @param string $checked Clear cache checkbox status.
* @return void
* @author Justin Foell <justin@foell.org>
* @since 1.1
*/
2017-05-10 14:39:00 -05:00
public function sanitize_cache_clear( $checked ) {
if ( 'on' === $checked ) {
2018-01-26 16:11:34 -06:00
global $wpdb;
$wpdb->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%'" );
2019-04-01 16:21:13 -05:00
delete_option( 'strava_token' );
2017-05-10 14:39:00 -05:00
}
return null;
}
2017-07-21 16:12:15 -05:00
/**
2019-04-01 16:21:13 -05:00
* Gets all saved strava ids as an array.
2017-07-21 16:12:15 -05:00
*
* @return array
2019-02-01 14:17:22 -06:00
* @author Justin Foell <justin@foell.org>
2019-04-01 16:21:13 -05:00
* @since 2.0.0
2017-07-21 16:12:15 -05:00
*/
2019-04-01 16:21:13 -05:00
public function get_ids() {
$ids = get_option( 'strava_id' );
if ( ! is_array( $ids ) ) {
$ids = array( $ids );
2017-07-21 16:12:15 -05:00
}
2019-04-01 16:21:13 -05:00
foreach ( $ids as $index => $id ) {
if ( empty( $id ) ) {
unset( $ids[ $index ] );
$ids = array_values( $ids ); // Rebase array keys after unset @see https://stackoverflow.com/a/5943165/2146022
2017-07-21 16:12:15 -05:00
}
}
2019-04-01 16:21:13 -05:00
return $ids;
2017-07-21 16:12:15 -05:00
}
2017-08-11 17:28:29 -05:00
/**
2019-04-01 16:21:13 -05:00
* Returns first (default) ID saved.
2017-08-11 17:28:29 -05:00
*
* @return string|null
2019-02-01 14:17:22 -06:00
* @author Justin Foell <justin@foell.org>
2017-08-11 17:28:29 -05:00
* @since 1.2.0
*/
2019-04-01 16:21:13 -05:00
public function get_default_id() {
$ids = $this->get_ids();
return isset( $ids[0] ) ? $ids[0] : null;
2017-08-11 17:28:29 -05:00
}
/**
2019-04-01 16:21:13 -05:00
* Get all IDs and their nicknames in one array.
2017-08-11 17:28:29 -05:00
*
* @return void
2019-02-01 14:17:22 -06:00
* @author Justin Foell <justin@foell.org>
2019-04-01 16:21:13 -05:00
* @since 2.0.0
2017-08-11 17:28:29 -05:00
*/
2019-04-01 16:21:13 -05:00
public function get_all_ids() {
$ids = $this->get_ids();
2017-08-11 17:28:29 -05:00
$nicknames = $this->nickname;
2017-12-26 14:18:13 -06:00
$all = array();
$number = 1;
2019-04-01 16:21:13 -05:00
foreach ( $ids as $index => $id ) {
2017-08-11 17:28:29 -05:00
if ( ! empty( $nicknames[ $index ] ) ) {
2019-04-01 16:21:13 -05:00
$all[ $id ] = $nicknames[ $index ];
2017-08-11 17:28:29 -05:00
} else {
2019-04-01 16:21:13 -05:00
$all[ $id ] = $this->get_default_nickname( $number );
2017-08-11 17:28:29 -05:00
}
$number++;
}
return $all;
}
/**
* Returns default nickname 'Default' / 'Athlete n'.
*
2019-02-01 14:17:22 -06:00
* @author Justin Foell <justin@foell.org>
2017-08-11 17:28:29 -05:00
* @since 1.2.0
*
2017-12-08 12:33:29 -06:00
* @param integer $number Athlete number (default 1).
2017-08-11 17:28:29 -05:00
* @return string
*/
private function get_default_nickname( $number = 1 ) {
2018-01-26 13:27:57 -06:00
// Translators: Athlete number if no nickname present.
2017-12-26 14:18:13 -06:00
return ( 1 === $number ) ? __( 'Default', 'wp-strava' ) : sprintf( __( 'Athlete %s', 'wp-strava' ), $number );
2017-08-11 17:28:29 -05:00
}
2017-07-21 16:12:15 -05:00
/**
2019-04-01 16:21:13 -05:00
* Checks for valid IDs.
2017-07-21 16:12:15 -05:00
*
2019-02-01 14:17:22 -06:00
* @author Justin Foell <justin@foell.org>
2017-08-11 17:28:29 -05:00
* @since 1.2.0
2017-07-21 16:12:15 -05:00
*
2019-04-01 16:21:13 -05:00
* @param string|array Single ID or array of IDs.
2017-07-21 16:12:15 -05:00
* @return boolean True if empty.
*/
2019-04-01 16:21:13 -05:00
public function ids_empty( $ids ) {
if ( empty( $ids ) ) {
2017-07-21 16:12:15 -05:00
return true;
}
2019-04-01 16:21:13 -05:00
if ( is_array( $ids ) ) {
foreach ( $ids as $id ) {
if ( ! empty( $id ) ) {
2017-07-21 16:12:15 -05:00
return false;
}
}
}
return true;
}
/**
2019-04-01 16:21:13 -05:00
* Add an ID if it's not already there, and save to the DB.
2017-07-21 16:12:15 -05:00
*
2019-04-01 16:21:13 -05:00
* @param string $id
2017-07-21 16:12:15 -05:00
*
2019-02-01 14:17:22 -06:00
* @author Justin Foell <justin@foell.org>
2019-04-01 16:21:13 -05:00
* @since 2.0.0
2017-07-21 16:12:15 -05:00
*/
2019-04-01 16:21:13 -05:00
public function add_id( $id ) {
if ( false === array_search( $id, $this->ids, true ) ) {
$this->ids[] = $id;
update_option( 'strava_id', $this->ids );
2017-07-21 16:12:15 -05:00
}
}
2019-04-01 14:50:36 -05:00
/**
* Undocumented function
*
2019-06-03 14:27:14 -05:00
* @param int $id Strava API Client ID
* @param string $secret Strava API Client Secret
* @param stdClass $info
2019-04-01 16:21:13 -05:00
* @author Justin Foell <justin.foell@webdevstudios.com>
2019-04-01 14:50:36 -05:00
* @since 2.0.0
*/
2019-06-03 14:27:14 -05:00
public function save_info( $id, $secret, $info ) {
2019-04-01 16:21:13 -05:00
$infos = get_option( 'strava_info', array() );
2019-06-03 14:27:14 -05:00
$infos = array_filter( $infos, array( $this, 'filter_by_id' ), ARRAY_FILTER_USE_KEY ); // Remove old IDs.
$info->client_secret = $secret;
2019-04-01 16:21:13 -05:00
$infos[ $id ] = $info;
update_option( 'strava_info', $infos );
2019-04-01 14:50:36 -05:00
}
2019-06-03 14:27:14 -05:00
/**
* array_filter() callback to remove info for IDs we no longer have.
*
* @param int $key Strava Client ID
* @return boolean True if Client ID is in $this->ids, false otherwise.
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
public function filter_by_id( $key ) {
if ( in_array( $key, $this->ids ) ) {
return true;
}
return false;
}
2019-04-01 14:50:36 -05:00
/**
* Undocumented function
*
* @return void
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
public function delete_id_secret() {
delete_option( 'strava_client_id' );
delete_option( 'strava_client_secret' );
}
/**
* Undocumented function
*
* @param [type] $value
* @return boolean
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
public function is_settings_updated( $value ) {
return isset( $value[0]['type'] ) && 'updated' === $value[0]['type'];
}
/**
* Undocumented function
*
* @return boolean
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
public function is_option_page() {
return isset( $_POST['option_page'] ) && $_POST['option_page'] === $this->option_page;
}
/**
* Undocumented function
*
* @return boolean
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
public function is_settings_page() {
return isset( $_GET['page'] ) && $_GET['page'] === $this->page_name;
}
2019-04-01 16:21:13 -05:00
/**
* Undocumented function
*
* @return string
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
2019-04-01 14:50:36 -05:00
public function get_page_name() {
return $this->page_name;
}
/**
* Undocumented function
*
* @return boolean
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 2.0.0
*/
private function is_adding_athlete() {
return ! ( empty( $_POST['strava_client_id'] ) && empty( $_POST['strava_client_secret'] ) );
}
2019-08-02 11:54:40 -05:00
/**
* Getter for Strava settings in wp_options.
*
* @param string $name Option name without the 'strava_' prefix.
* @return mixed
* @since 0.62
*/
2013-02-17 22:14:10 -06:00
public function __get( $name ) {
2017-07-21 16:12:15 -05:00
if ( ! strpos( 'strava_', $name ) ) {
$name = "strava_{$name}";
}
// Else.
return get_option( $name );
2013-02-17 22:14:10 -06:00
}
2017-05-10 12:17:25 -05:00
2019-08-02 11:54:40 -05:00
/**
* Link to the settings on the plugin list page.
*
* @param array $links Array of plugin links.
* @return array Links with settings added.
* @author Justin Foell <justin@foell.org>
* @since 1.0
*/
2017-05-10 12:17:25 -05:00
public function settings_link( $links ) {
2017-05-26 10:53:24 -05:00
$settings_link = '<a href="' . admin_url( "options-general.php?page={$this->page_name}" ) . '">' . __( 'Settings', 'wp-strava' ) . '</a>';
2017-12-26 14:18:13 -06:00
$links[] = $settings_link;
2014-09-23 13:39:48 -05:00
return $links;
}
2019-09-21 22:02:26 -05:00
/**
* Plugin Upgrade Notice.
*
* @param array $data Plugin data with readme additions.
* @param array $response Response from wp.org.
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.3
*/
public function plugin_update_message( $data, $response ) {
if ( isset( $data['upgrade_notice'] ) ) {
2019-09-21 23:17:17 -05:00
echo wp_kses_post( $data['upgrade_notice'] );
2019-09-21 22:02:26 -05:00
}
}
/**
* Plugin Upgrade Notice (multisite).
*
* @param string $file Relative path to plugin, i.e. wp-strava/wp-strava.php.
* @param array $plugin Plugin data with readme additions.
* @author Justin Foell <justin.foell@webdevstudios.com>
* @since 1.7.3
*/
public function ms_plugin_update_message( $file, $plugin ) {
2019-09-21 23:17:17 -05:00
if ( is_multisite() && ! is_network_admin() && version_compare( $plugin['Version'], $plugin['new_version'], '<' ) ) {
2019-09-21 22:02:26 -05:00
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
printf(
'<tr class="plugin-update-tr"><td colspan="%s" class="plugin-update update-message notice inline notice-warning notice-alt"><div class="update-message"><h4 style="margin: 0; font-size: 14px;">%s</h4>%s</div></td></tr>',
$wp_list_table->get_column_count(),
$plugin['Name'],
2019-09-21 23:17:17 -05:00
wp_kses_post( $plugin['upgrade_notice'] )
2019-09-21 22:02:26 -05:00
);
}
}
2017-05-26 10:53:24 -05:00
}