From ea992be73ca93ce319735e57be72cd18e606eb6c Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Fri, 21 Feb 2020 13:11:49 +1000 Subject: [PATCH] Add PHPCS config, and format the codebase to match. (#63) --- .gitignore | 1 + .travis.yml | 24 +- admin-ui.php | 266 ++++++++++++------- composer.json | 12 +- includes/services/core/http-basic.php | 84 +++--- includes/services/core/oauth1.php | 197 ++++++++------ includes/services/core/oauth2.php | 118 ++++---- includes/services/extended/0-google-base.php | 90 ++++--- includes/services/extended/500px.php | 23 +- includes/services/extended/eventbrite.php | 25 +- includes/services/extended/facebook.php | 68 +++-- includes/services/extended/fitbit.php | 42 +-- includes/services/extended/flickr.php | 30 ++- includes/services/extended/foursquare.php | 17 +- includes/services/extended/github.php | 30 +-- includes/services/extended/google-drive.php | 2 +- includes/services/extended/instagram.php | 13 +- includes/services/extended/instapaper.php | 91 ++++--- includes/services/extended/jetpack.php | 18 +- includes/services/extended/linkedin.php | 37 +-- includes/services/extended/moves.php | 17 +- includes/services/extended/nest.php | 14 +- includes/services/extended/pinterest.php | 20 +- includes/services/extended/pocket.php | 60 +++-- includes/services/extended/runkeeper.php | 24 +- includes/services/extended/strava.php | 55 ++-- includes/services/extended/tripit.php | 27 +- includes/services/extended/tumblr.php | 53 ++-- includes/services/extended/twitter.php | 53 ++-- includes/services/extended/yahoo.php | 34 +-- includes/stores/singlestore.php | 84 +++--- keyring.php | 98 ++++--- phpcs.xml.dist | 81 ++++++ service.php | 96 ++++--- store.php | 5 +- token.php | 21 +- 36 files changed, 1171 insertions(+), 759 deletions(-) create mode 100644 phpcs.xml.dist diff --git a/.gitignore b/.gitignore index 22d0d82f809..7579f74311d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vendor +composer.lock diff --git a/.travis.yml b/.travis.yml index 3eef4f87f7b..1ca86964616 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,13 +8,23 @@ branches: only: - master -# Run Matrix for these PHP versions -php: -- "5.6" -- "7.0" -- "7.2" -- "7.3" -- "7.4" +matrix: + include: + - name: PHP Linting + script: composer php:lint + - name: "PHPUnit Tests: PHP 7.4" + php: 7.4 + - name: "PHPUnit Tests: PHP 7.3" + php: 7.3 + - name: "PHPUnit Tests: PHP 7.2" + php: 7.2 + - name: "PHPUnit Tests: PHP 7.1" + php: 7.1 + - name: "PHPUnit Tests: PHP 7.0" + php: 7.0 + - name: "PHPUnit Tests: PHP 5.6" + php: 5.6 + fast_finish: true before_script: - composer --version diff --git a/admin-ui.php b/admin-ui.php index daaa148caac..6947a5ff653 100644 --- a/admin-ui.php +++ b/admin-ui.php @@ -22,7 +22,7 @@ function __construct() { static function &init() { static $instance = false; - if ( !$instance ) { + if ( ! $instance ) { $instance = new Keyring_Admin_UI; } @@ -41,7 +41,8 @@ function inline_css() { list-style-type: square; margin: .3em 0 .3em 2em; } - + '; switch ( $screen ) { - case 'tokens' : - echo '

' . __( 'Keyring: Service Connections', 'keyring' ) . ' ' . __( 'Add New', 'keyring' ) . '

'; - break; - case 'services' : - echo '

' . __( 'Add New Connection', 'keyring' ) . '

'; - echo '

' . __( '← Back', 'keyring' ) . '

'; - break; - case 'error' : - echo '

' . __( 'Keyring Error!', 'keyring' ) . '

'; - break; - default : - echo '

' . __( 'Keyring', 'keyring' ) . '

'; + case 'tokens': + echo '

' . __( 'Keyring: Service Connections', 'keyring' ) . ' ' . __( 'Add New', 'keyring' ) . '

'; + break; + case 'services': + echo '

' . __( 'Add New Connection', 'keyring' ) . '

'; + echo '

' . __( '← Back', 'keyring' ) . '

'; + break; + case 'error': + echo '

' . __( 'Keyring Error!', 'keyring' ) . '

'; + break; + default: + echo '

' . __( 'Keyring', 'keyring' ) . '

'; } // Output any errors if we have them, then stop, and link back to home. if ( $this->keyring->has_errors() ) { echo '
'; return; @@ -86,7 +87,7 @@ function admin_page_header( $screen = false ) { if ( $this->keyring->has_messages() ) { echo '
'; } @@ -98,21 +99,27 @@ static function admin_page_footer() { function admin_page() { // Handle delete request. Will default back to "tokens" later - if ( isset( $_REQUEST['action'] ) && 'delete' == $_REQUEST['action'] ) { - if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-delete-' . $_REQUEST['service'] . '-' . $_REQUEST['token'] ) ) { + if ( isset( $_REQUEST['action'] ) && 'delete' === $_REQUEST['action'] ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-delete-' . $_REQUEST['service'] . '-' . $_REQUEST['token'] ) ) { Keyring::error( __( 'Invalid/missing delete nonce.', 'keyring' ) ); exit; } - if ( $this->keyring->get_token_store()->delete( array( 'id' => (int) $_REQUEST['token'], 'type' => 'access' ) ) ) + if ( $this->keyring->get_token_store()->delete( + array( + 'id' => (int) $_REQUEST['token'], + 'type' => 'access', + ) + ) ) { Keyring::message( __( 'That connection has been deleted.', 'keyring' ) ); - else + } else { Keyring::message( __( 'Could not delete that connection!', 'keyring' ) ); + } } // Handle test request. Will default back to "tokens" later - if ( isset( $_REQUEST['action'] ) && 'test' == $_REQUEST['action'] ) { - if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-test-' . $_REQUEST['service'] . '-' . $_REQUEST['token'] ) ) { + if ( isset( $_REQUEST['action'] ) && 'test' === $_REQUEST['action'] ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-test-' . $_REQUEST['service'] . '-' . $_REQUEST['token'] ) ) { Keyring::error( __( 'Invalid/missing testing nonce.', 'keyring' ) ); exit; } @@ -120,7 +127,14 @@ function admin_page() { // If the test_connection() method exists, call it for this service/connection $service = $this->keyring->get_service_by_name( $_REQUEST['service'] ); if ( method_exists( $service, 'test_connection' ) ) { - $service->set_token( $this->keyring->get_token_store()->get_token( array( 'id' => $_REQUEST['token'], 'type' => 'request' ) ) ); + $service->set_token( + $this->keyring->get_token_store()->get_token( + array( + 'id' => $_REQUEST['token'], + 'type' => 'request', + ) + ) + ); $test = $service->test_connection(); if ( true === $test ) { @@ -131,21 +145,21 @@ function admin_page() { } else { Keyring::message( __( 'This service does not currently support connection testing.', 'keyring' ) ); } - } // Set up our defaults $service = ''; - if ( !empty( $_REQUEST['service'] ) ) + if ( ! empty( $_REQUEST['service'] ) ) { $service = $_REQUEST['service']; + } $action = 'tokens'; - if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'tokens', 'services', 'request', 'verify', 'manage' ) ) ) { + if ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], array( 'tokens', 'services', 'request', 'verify', 'manage' ), true ) ) { $action = $_REQUEST['action']; } // Prevent non-admins from accessing any management UI - if ( 'manage' == $action && ! current_user_can( 'manage_options' ) ) { + if ( 'manage' === $action && ! current_user_can( 'manage_options' ) ) { $action = 'tokens'; } @@ -158,66 +172,96 @@ function admin_page() { // Nothing else has bailed, so it must be one of our default/core screens. switch ( $action ) { - case 'tokens' : - $this->admin_page_header( 'tokens' ); - - $list_table = new Keyring_Connections_List_Table(); - $list_table->display(); - - $this->admin_page_footer(); - break; - - case 'services' : - $this->admin_page_header( 'services' ); - - $services = $this->keyring->get_registered_services(); - if ( count( $services ) ) { - $configured = $not_configured = array(); - foreach ( $services as $service ) { - if ( $service->is_configured() ) - $configured[] = $service; - else - $not_configured[] = $service; - } - - if ( count( $configured ) ) { - echo '

' . __( 'Click a service to create a new connection:', 'keyring' ) . '

'; - echo '

'; - } else { - echo '

' . __( 'There are no fully-configured services available to connect to.', 'keyring' ) . '

'; - } - if ( current_user_can( 'manage_options' ) && count( $not_configured ) ) { - echo '

' . __( 'The following services need to be configured correctly before you can connect to them.', 'keyring' ) . '

'; - echo ''; } - } - $this->admin_page_footer(); - break; + $this->admin_page_footer(); + break; } } } @@ -230,27 +274,30 @@ class Keyring_Connections_List_Table extends WP_List_Table { function __construct() { $this->keyring = Keyring::init(); - parent::__construct( array( - 'singular' => 'connection', - 'plural' => 'connections', - 'screen' => $this->keyring->admin_page, - ) ); + parent::__construct( + array( + 'singular' => 'connection', + 'plural' => 'connections', + 'screen' => $this->keyring->admin_page, + ) + ); $this->items = Keyring::get_token_store()->get_tokens(); } function no_items() { + /* translators: url */ echo '

' . sprintf( __( 'You haven\'t added any connections yet. Add a New Connection.', 'keyring' ), esc_url( Keyring_Util::admin_url( false, array( 'action' => 'services' ) ) ) ) . '

'; } function get_columns() { return array( - 'service' => __( 'Service', 'keyring' ), - 'avatar' => __( 'Avatar', 'keyring' ), - 'id' => __( 'External ID', 'keyring' ), - 'name' => __( 'Name', 'keyring' ), - 'meta' => __( 'Meta', 'keyring' ), - 'actions' => ' ' + 'service' => __( 'Service', 'keyring' ), + 'avatar' => __( 'Avatar', 'keyring' ), + 'id' => __( 'External ID', 'keyring' ), + 'name' => __( 'Name', 'keyring' ), + 'meta' => __( 'Meta', 'keyring' ), + 'actions' => ' ', ); } @@ -274,21 +321,24 @@ function column_id( $row ) { function column_name( $row ) { // Make a few attempts to get something to display here $name = $row->get_meta( 'name' ); - if ( !$name ) + if ( ! $name ) { $name = $row->get_meta( 'username' ); - if ( !$name ) + } + if ( ! $name ) { $name = trim( $row->get_meta( 'first_name' ) . ' ' . $row->get_meta( 'last_name' ) ); + } - if ( $name ) + if ( $name ) { echo $name; - else + } else { echo '-'; + } } function column_meta( $row ) { echo 'Show'; echo ''; @@ -296,15 +346,33 @@ function column_meta( $row ) { function column_actions( $row ) { $kr_delete_nonce = wp_create_nonce( 'keyring-delete' ); - $delete_nonce = wp_create_nonce( 'keyring-delete-' . $row->get_service()->get_name() . '-' . $row->get_uniq_id() ); + $delete_nonce = wp_create_nonce( 'keyring-delete-' . $row->get_service()->get_name() . '-' . $row->get_uniq_id() ); $kr_test_nonce = wp_create_nonce( 'keyring-test' ); - $test_nonce = wp_create_nonce( 'keyring-test-' . $row->get_service()->get_name() . '-' . $row->get_uniq_id() ); + $test_nonce = wp_create_nonce( 'keyring-test-' . $row->get_service()->get_name() . '-' . $row->get_uniq_id() ); echo ''; - echo 'Delete'; + echo 'Delete'; echo ' | '; - echo 'Test'; + echo 'Test'; echo ''; } } diff --git a/composer.json b/composer.json index 2fefacacd25..350b65ff4d9 100644 --- a/composer.json +++ b/composer.json @@ -2,12 +2,18 @@ "name": "beaulebens/keyring", "description": "An authentication framework for WordPress development. Provides a standardized connection and communication framework for talking to web services.", "require": { - "php": ">=5.3" + "php": ">=5.6" }, "require-dev": { - "phpunit/phpunit": "^5|^6|^7" + "phpunit/phpunit": "^5|^6|^7", + "dealerdirect/phpcodesniffer-composer-installer": "0.5.0", + "phpcompatibility/phpcompatibility-wp": "2.1.0", + "wp-coding-standards/wpcs": "2.2.1" }, "scripts": { - "test-includes": "phpunit includes/oauth-php" + "test-includes": "vendor/bin/phpunit includes/oauth-php", + "php:lint": "vendor/bin/phpcs -p -s", + "php:lint:errors": "vendor/bin/phpcs -p -s --runtime-set ignore_warnings_on_exit 1", + "php:lint:autofix": "vendor/bin/phpcbf" } } diff --git a/includes/services/core/http-basic.php b/includes/services/core/http-basic.php index 73be8eb19f7..bbdc410c4b9 100644 --- a/includes/services/core/http-basic.php +++ b/includes/services/core/http-basic.php @@ -36,37 +36,38 @@ function request_ui() { if ( isset( $_GET['error'] ) ) { echo '
'; } // Even though it doesn't make too much sense, we support request tokens in HTTP Basic // to ensure consistency with other services - $request_token = new Keyring_Request_Token( + $request_token = new Keyring_Request_Token( $this->get_name(), array(), apply_filters( 'keyring_request_token_meta', array( - 'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false + 'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false, ), $this->get_name(), array() // no token ) ); - $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); - $request_token_id = $this->store_token( $request_token ); + $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); + $request_token_id = $this->store_token( $request_token ); Keyring_Util::debug( 'HTTP Basic Stored Request token ' . $request_token_id ); echo apply_filters( 'keyring_' . $this->get_name() . '_request_ui_intro', '' ); // Output basic form for collecting user/pass + /* translators: service name */ echo '

' . sprintf( __( 'Enter your username and password for accessing %s:', 'keyring' ), $this->get_label() ) . '

'; echo '
'; echo ''; @@ -90,7 +91,8 @@ function request_ui() { jQuery( document ).ready( function() { jQuery( '#username' ).focus(); } ); - + store->get_token( array( 'id' => $state, 'type' => 'request' ) ); + $state = empty( $_GET['state'] ) ? '' : preg_replace( '/[^\x20-\x7E]/', '', $_GET['state'] ); + $keyring_request_token = $this->store->get_token( + array( + 'id' => $state, + 'type' => 'request', + ) + ); Keyring_Util::debug( 'HTTP Basic Loaded Request Token ' . $state ); Keyring_Util::debug( $keyring_request_token ); // Remove request token, don't need it any more. - $this->store->delete( array( 'id' => $state, 'type' => 'request' ) ); + $this->store->delete( + array( + 'id' => $state, + 'type' => 'request', + ) + ); } if ( ! strlen( $_POST['username'] ) ) { @@ -121,7 +133,7 @@ function verify_token() { array( 'action' => 'request', 'error' => 'empty', - 'kr_nonce' => wp_create_nonce( 'keyring-request' ) + 'kr_nonce' => wp_create_nonce( 'keyring-request' ), ) ); Keyring_Util::debug( $url ); @@ -146,7 +158,7 @@ function verify_token() { array( 'action' => 'request', 'error' => '401', - 'kr_nonce' => wp_create_nonce( 'keyring-request' ) + 'kr_nonce' => wp_create_nonce( 'keyring-request' ), ) ); Keyring_Util::debug( $url ); @@ -154,7 +166,12 @@ function verify_token() { exit; } - $meta = array_merge( array( 'username' => trim( $_POST['username'] ), $this->build_token_meta( $token ) ) ); + $meta = array_merge( + array( + 'username' => trim( $_POST['username'] ), + $this->build_token_meta( $token ), + ) + ); $access_token = new Keyring_Access_Token( $this->get_name(), @@ -169,11 +186,13 @@ function verify_token() { } function request( $url, array $params = array() ) { - if ( $this->requires_token() && empty( $this->token ) ) - return new Keyring_Error( 'keyring-request-error', __( 'No token' ) ); + if ( $this->requires_token() && empty( $this->token ) ) { + return new Keyring_Error( 'keyring-request-error', __( 'No token', 'keyring' ) ); + } - if ( $this->requires_token() ) + if ( $this->requires_token() ) { $params['headers'] = array( 'Authorization' => 'Basic ' . $this->token ); + } $method = 'GET'; if ( isset( $params['method'] ) ) { @@ -191,26 +210,27 @@ function request( $url, array $params = array() ) { Keyring_Util::debug( $params ); switch ( strtoupper( $method ) ) { - case 'GET': - $res = wp_remote_get( $url, $params ); - break; + case 'GET': + $res = wp_remote_get( $url, $params ); + break; - case 'POST': - $res = wp_remote_post( $url, $params ); - break; + case 'POST': + $res = wp_remote_post( $url, $params ); + break; - default: - Keyring::error( __( 'Unsupported method specified for verify_token.', 'keyring' ) ); - exit; + default: + Keyring::error( __( 'Unsupported method specified for verify_token.', 'keyring' ) ); + exit; } Keyring_Util::debug( $res ); $this->set_request_response_code( wp_remote_retrieve_response_code( $res ) ); - if ( '2' == substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { - if ( $raw_response ) + if ( '2' === substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { + if ( $raw_response ) { return wp_remote_retrieve_body( $res ); - else + } else { return $this->parse_response( wp_remote_retrieve_body( $res ) ); + } } else { return new Keyring_Error( 'keyring-request-error', $res ); } diff --git a/includes/services/core/oauth1.php b/includes/services/core/oauth1.php index 451a08b391f..0f369652f02 100644 --- a/includes/services/core/oauth1.php +++ b/includes/services/core/oauth1.php @@ -18,26 +18,33 @@ class Keyring_Service_OAuth1 extends Keyring_Service { protected $authorize_url = ''; protected $authorize_method = 'GET'; - protected $consumer = null; - protected $signature_method = null; - protected $callback_url = null; + protected $consumer = null; + protected $signature_method = null; + protected $callback_url = null; - var $app_id = null; - var $key = null; - var $secret = null; - var $token = null; - var $authorization_header = false; - var $authorization_realm = ''; + var $app_id = null; + var $key = null; + var $secret = null; + var $token = null; + var $authorization_header = false; + var $authorization_realm = ''; function __construct() { parent::__construct(); // Nonces for the callback URL, which is used during the verify step - $kr_nonce = wp_create_nonce( 'keyring-verify' ); - $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); - $this->callback_url = Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify', 'kr_nonce' => $kr_nonce, 'nonce' => $nonce ) ); + $kr_nonce = wp_create_nonce( 'keyring-verify' ); + $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); + $this->callback_url = Keyring_Util::admin_url( + $this->get_name(), + array( + 'action' => 'verify', + 'kr_nonce' => $kr_nonce, + 'nonce' => $nonce, + ) + ); - if ( !class_exists( 'OAuthRequest' ) ) { + if ( ! class_exists( 'OAuthRequest' ) ) { require dirname( dirname( dirname( __FILE__ ) ) ) . '/oauth-php/OAuth.php'; } } @@ -47,18 +54,18 @@ function __construct() { */ function is_configured() { $creds = $this->get_credentials(); - return !empty( $creds['key'] ) && !empty( $creds['secret'] ); + return ! empty( $creds['key'] ) && ! empty( $creds['secret'] ); } function request_token() { Keyring_Util::debug( 'Keyring_Service_OAuth1::request_token()' ); - if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-request-' . $this->get_name() ) ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-request-' . $this->get_name() ) ) { Keyring::error( __( 'Invalid/missing request nonce.', 'keyring' ) ); exit; } // Need to create a request token now, so that we have a state to pass - $request_token = new Keyring_Request_Token( + $request_token = new Keyring_Request_Token( $this->get_name(), array(), apply_filters( @@ -74,12 +81,13 @@ function request_token() { $this ) ); - $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); - $request_token_id = $this->store_token( $request_token ); - $scope = apply_filters( 'keyring_' . $this->get_name() . '_request_scope', false ); + $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); + $request_token_id = $this->store_token( $request_token ); + $scope = apply_filters( 'keyring_' . $this->get_name() . '_request_scope', false ); Keyring_Util::debug( 'OAuth1 Stored Request token ' . $request_token_id ); - $request_token_url = add_query_arg( array( + $request_token_url = add_query_arg( + array( 'oauth_callback' => urlencode( add_query_arg( @@ -89,7 +97,7 @@ function request_token() { $this->callback_url ) ), - 'scope' => $scope, + 'scope' => $scope, ), $this->request_token_url ); @@ -97,38 +105,44 @@ function request_token() { // Set up OAuth request $req = $this->prepare_request( null, $this->request_token_method, $request_token_url, false ); - $query = ''; + $query = ''; $parsed = parse_url( (string) $req ); - if ( !empty( $parsed['query'] ) && 'POST' == strtoupper( $this->request_token_method ) ) { + if ( ! empty( $parsed['query'] ) && 'POST' === strtoupper( $this->request_token_method ) ) { $request_token_url = str_replace( '?' . $parsed['query'], '', (string) $req ); - $query = $parsed['query']; + $query = $parsed['query']; } else { $request_token_url = (string) $req; } // Go and get a request token switch ( strtoupper( $this->request_token_method ) ) { - case 'GET': - Keyring_Util::debug( "OAuth1 GET Request Token URL: $request_token_url" ); - $res = wp_remote_get( $request_token_url ); - break; - - case 'POST': - Keyring_Util::debug( "OAuth1 POST Request Token URL: $request_token_url" ); - Keyring_Util::debug( $query ); - $res = wp_remote_post( $request_token_url, array( 'body' => $query, 'sslverify' => false ) ); - break; - - default: - Keyring::error( __( 'Unsupported method specified for request_token.', 'keyring' ) ); - exit; + case 'GET': + Keyring_Util::debug( "OAuth1 GET Request Token URL: $request_token_url" ); + $res = wp_remote_get( $request_token_url ); + break; + + case 'POST': + Keyring_Util::debug( "OAuth1 POST Request Token URL: $request_token_url" ); + Keyring_Util::debug( $query ); + $res = wp_remote_post( + $request_token_url, + array( + 'body' => $query, + 'sslverify' => false, + ) + ); + break; + + default: + Keyring::error( __( 'Unsupported method specified for request_token.', 'keyring' ) ); + exit; } Keyring_Util::debug( 'OAuth1 Response' ); Keyring_Util::debug( $res ); // Accept all 2xx response codes - if ( '2' == substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { + if ( '2' === substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { // Get the values returned from the remote service $token = wp_remote_retrieve_body( $res ); parse_str( trim( $token ), $token ); @@ -162,13 +176,14 @@ function request_token() { $this->store->update( $request_token ); } else { Keyring::error( + /* translators: service name */ sprintf( __( 'There was a problem connecting to %s to create an authorized connection. Please try again in a moment.', 'keyring' ), $this->get_label() ) ); return false; } // Redirect user to authorize access - $authorize = add_query_arg( 'oauth_token', urlencode( $token['oauth_token'] ), $this->authorize_url ) ; + $authorize = add_query_arg( 'oauth_token', urlencode( $token['oauth_token'] ), $this->authorize_url ); if ( $this->callback_url ) { // Add reference to our request token to the callback. Use "state" a la OAuth2 for consistency $authorize = add_query_arg( @@ -191,7 +206,7 @@ function request_token() { function verify_token() { Keyring_Util::debug( 'Keyring_Service_OAuth1::verify_token()' ); - if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-verify-' . $this->get_name() ) ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-verify-' . $this->get_name() ) ) { Keyring::error( __( 'Invalid/missing verification nonce.', 'keyring' ) ); exit; } @@ -199,32 +214,49 @@ function verify_token() { // Load up the request token that got us here and globalize it if ( isset( $_GET['state'] ) ) { global $keyring_request_token; - $state = preg_replace( '/[^\x20-\x7E]/', '', $_GET['state'] ); - $keyring_request_token = $this->store->get_token( array( 'id' => $state, 'type' => 'request' ) ); + $state = preg_replace( '/[^\x20-\x7E]/', '', $_GET['state'] ); + $keyring_request_token = $this->store->get_token( + array( + 'id' => $state, + 'type' => 'request', + ) + ); Keyring_Util::debug( 'OAuth1 Loaded Request Token ' . $state ); Keyring_Util::debug( $keyring_request_token ); $secret = $keyring_request_token->token['oauth_token_secret']; // Remove request token, don't need it any more. - $this->store->delete( array( 'id' => $state, 'type' => 'request' ) ); + $this->store->delete( + array( + 'id' => $state, + 'type' => 'request', + ) + ); } // Get an access token, using the temporary token passed back $token = isset( $_GET['oauth_token'] ) ? $_GET['oauth_token'] : false; $access_token_url = $this->access_token_url; - if ( !empty( $_GET['oauth_verifier'] ) ) + if ( ! empty( $_GET['oauth_verifier'] ) ) { $access_token_url = add_query_arg( array( 'oauth_verifier' => urlencode( $_GET['oauth_verifier'] ) ), $access_token_url ); + } // Set up a consumer token and make the request for an access_token $token = new OAuthConsumer( $token, $secret ); $this->set_token( new Keyring_Access_Token( $this->get_name(), $token, array() ) ); - $res = $this->request( $access_token_url, array( 'method' => $this->access_token_method, 'raw_response' => true ) ); + $res = $this->request( + $access_token_url, + array( + 'method' => $this->access_token_method, + 'raw_response' => true, + ) + ); Keyring_Util::debug( 'OAuth1 Access Token Response' ); Keyring_Util::debug( $res ); - if ( !Keyring_Util::is_error( $res ) ) { + if ( ! Keyring_Util::is_error( $res ) ) { $token = $this->parse_access_token( $res ); $access_token = new Keyring_Access_Token( @@ -244,6 +276,7 @@ function verify_token() { exit; } else { Keyring::error( + /* translators: service name */ sprintf( __( 'There was a problem connecting to %s to create an authorized connection. Please try again in a moment.', 'keyring' ), $this->get_label() ) ); return false; @@ -251,8 +284,9 @@ function verify_token() { } function request( $url, array $params = array() ) { - if ( $this->requires_token() && empty( $this->token ) ) + if ( $this->requires_token() && empty( $this->token ) ) { return new Keyring_Error( 'keyring-request-error', __( 'No token', 'keyring' ) ); + } $raw_response = false; if ( isset( $params['raw_response'] ) ) { @@ -280,17 +314,17 @@ function request( $url, array $params = array() ) { if ( isset( $params['body'] ) && $sign_parameters ) { if ( is_string( $params['body'] ) ) { wp_parse_str( $params['body'], $sign_vars ); - } else if ( is_array( $params['body'] ) ) { + } elseif ( is_array( $params['body'] ) ) { $sign_vars = $params['body']; } } - $req = $this->prepare_request( $token, $method, $url, $sign_vars ); + $req = $this->prepare_request( $token, $method, $url, $sign_vars ); $request_url = (string) $req; if ( $this->token && $this->authorization_header ) { - $header = $req->to_header( $this->authorization_realm ); // Gives a complete header string, not just the second half - $bits = explode( ': ', $header, 2 ); + $header = $req->to_header( $this->authorization_realm ); // Gives a complete header string, not just the second half + $bits = explode( ': ', $header, 2 ); $params['headers']['Authorization'] = $bits[1]; // This hack was introduced for Instapaper (http://stackoverflow.com/a/9645033/1507683), which is overly strict on @@ -304,45 +338,52 @@ function request( $url, array $params = array() ) { $request_url = remove_query_arg( 'oauth_verifier', $url ); } - $query = ''; + $query = ''; $parsed = parse_url( $request_url ); - if ( !empty( $parsed['query'] ) && 'POST' == $method ) { + if ( ! empty( $parsed['query'] ) && 'POST' === $method ) { $request_url = str_replace( '?' . $parsed['query'], '', $request_url ); - $query = $parsed['query']; + $query = $parsed['query']; } Keyring_Util::debug( "OAuth1 Request URL: $request_url" ); switch ( $method ) { - case 'GET': - Keyring_Util::debug( 'OAuth1 GET ' . $request_url ); - $res = wp_remote_get( $request_url, $params ); - break; - - case 'POST': - $params = array_merge( array( 'body' => $query, 'sslverify' => false ), $params ); - Keyring_Util::debug( 'OAuth1 POST ' . $request_url ); - Keyring_Util::debug( $params ); - $res = wp_remote_post( $request_url, $params ); - break; - - case 'PUT': - $params = array_merge( array( 'method' => 'PUT' ), $params ); - $res = wp_remote_request( $request_url, $params ); - break; - - default: - Keyring::error( __( 'Unsupported method specified.', 'keyring' ) ); - exit; + case 'GET': + Keyring_Util::debug( 'OAuth1 GET ' . $request_url ); + $res = wp_remote_get( $request_url, $params ); + break; + + case 'POST': + $params = array_merge( + array( + 'body' => $query, + 'sslverify' => false, + ), + $params + ); + Keyring_Util::debug( 'OAuth1 POST ' . $request_url ); + Keyring_Util::debug( $params ); + $res = wp_remote_post( $request_url, $params ); + break; + + case 'PUT': + $params = array_merge( array( 'method' => 'PUT' ), $params ); + $res = wp_remote_request( $request_url, $params ); + break; + + default: + Keyring::error( __( 'Unsupported method specified.', 'keyring' ) ); + exit; } Keyring_Util::debug( $res ); $this->set_request_response_code( wp_remote_retrieve_response_code( $res ) ); // Accept all 2xx response codes - if ( '2' == substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { - if ( $raw_response ) + if ( '2' === substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { + if ( $raw_response ) { return wp_remote_retrieve_body( $res ); - else + } else { return $this->parse_response( wp_remote_retrieve_body( $res ) ); + } } else { return new Keyring_Error( 'keyring-request-error', $res ); } diff --git a/includes/services/core/oauth2.php b/includes/services/core/oauth2.php index 0af92e002f5..25baa9d501b 100644 --- a/includes/services/core/oauth2.php +++ b/includes/services/core/oauth2.php @@ -27,30 +27,30 @@ class Keyring_Service_OAuth2 extends Keyring_Service_OAuth1 { function request_token() { Keyring_Util::debug( 'Keyring_Service_OAuth2::request_token()' ); - if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-request-' . $this->get_name() ) ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-request-' . $this->get_name() ) ) { Keyring::error( __( 'Invalid/missing request nonce.', 'keyring' ) ); exit; } // Need to create a request token now, so that we have a state to pass - $request_token = new Keyring_Request_Token( + $request_token = new Keyring_Request_Token( $this->get_name(), array(), apply_filters( 'keyring_request_token_meta', array( - 'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false + 'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false, ), $this->get_name(), array(), // no token $this ) ); - $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); - $request_token_id = $this->store_token( $request_token ); + $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); + $request_token_id = $this->store_token( $request_token ); $url = $this->authorize_url; - if ( !stristr( $url, '?' ) ) { + if ( ! stristr( $url, '?' ) ) { $url .= '?'; } $params = array( @@ -73,13 +73,14 @@ function request_token() { function verify_token() { Keyring_Util::debug( 'Keyring_Service_OAuth2::verify_token()' ); - if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-verify-' . $this->get_name() ) ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-verify-' . $this->get_name() ) ) { Keyring::error( __( 'Invalid/missing verification nonce.', 'keyring' ) ); exit; } - if ( !isset( $_GET['code'] ) || !isset( $_GET['state'] ) ) { + if ( ! isset( $_GET['code'] ) || ! isset( $_GET['state'] ) ) { Keyring::error( + /* translators: service name */ sprintf( __( 'There was a problem communicating with %s. Please try again in a moment.', 'keyring' ), $this->get_label() ) ); return false; @@ -87,13 +88,19 @@ function verify_token() { // Load up the request token that got us here and globalize it global $keyring_request_token; - $state = preg_replace( '/[^\x20-\x7E]/', '', $_GET['state'] ); - $keyring_request_token = $this->store->get_token( array( 'id' => $state, 'type' => 'request' ) ); + $state = preg_replace( '/[^\x20-\x7E]/', '', $_GET['state'] ); + $keyring_request_token = $this->store->get_token( + array( + 'id' => $state, + 'type' => 'request', + ) + ); Keyring_Util::debug( 'OAuth2 Loaded Request Token ' . $state ); Keyring_Util::debug( $keyring_request_token ); - if ( !$keyring_request_token ) { + if ( ! $keyring_request_token ) { Keyring::error( + /* translators: service name */ sprintf( __( 'Failed to load your request token while connecting to %s. Please try again in a moment.', 'keyring' ), $this->get_label() ) ); return false; @@ -101,18 +108,23 @@ function verify_token() { $error_debug_info = array(); - if ( !empty( $keyring_request_token->meta['blog_id'] ) && !empty( $keyring_request_token->meta['user_id'] ) ) { + if ( ! empty( $keyring_request_token->meta['blog_id'] ) && ! empty( $keyring_request_token->meta['user_id'] ) ) { $error_debug_info = array( 'blog_id' => $keyring_request_token->meta['blog_id'], - 'user_id' => $keyring_request_token->meta['user_id'] + 'user_id' => $keyring_request_token->meta['user_id'], ); } // Remove request token, don't need it any more. - $this->store->delete( array( 'id' => $state, 'type' => 'request' ) ); + $this->store->delete( + array( + 'id' => $state, + 'type' => 'request', + ) + ); $url = $this->access_token_url; - if ( !stristr( $url, '?' ) ) { + if ( ! stristr( $url, '?' ) ) { $url .= '?'; } $params = array( @@ -125,22 +137,22 @@ function verify_token() { $params = apply_filters( 'keyring_' . $this->get_name() . '_verify_token_params', $params ); Keyring_Util::debug( 'OAuth2 Access Token URL: ' . $url . http_build_query( $params ) ); switch ( strtoupper( $this->access_token_method ) ) { - case 'GET': - $res = wp_remote_get( $url . http_build_query( $params ) ); - break; - case 'POST': - $res = wp_remote_post( $url, apply_filters( 'keyring_' . $this->get_name() . '_verify_token_post_params', array( 'body' => $params ) ) ); - break; + case 'GET': + $res = wp_remote_get( $url . http_build_query( $params ) ); + break; + case 'POST': + $res = wp_remote_post( $url, apply_filters( 'keyring_' . $this->get_name() . '_verify_token_post_params', array( 'body' => $params ) ) ); + break; } Keyring_Util::debug( 'OAuth2 Response' ); Keyring_Util::debug( $res ); // Accept all 2xx response codes - if ( '2' == substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { + if ( '2' === substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { $token = wp_remote_retrieve_body( $res ); Keyring_Util::debug( $token ); - $token = $this->parse_access_token( $token ); + $token = $this->parse_access_token( $token ); $access_token = new Keyring_Access_Token( $this->get_name(), $token['access_token'], @@ -155,6 +167,7 @@ function verify_token() { exit; } Keyring::error( + /* translators: service name */ sprintf( __( 'There was a problem authorizing with %s. Please try again in a moment.', 'keyring' ), $this->get_label() ), $error_debug_info ); @@ -177,12 +190,13 @@ function parse_access_token( $token ) { function request( $url, array $params = array() ) { Keyring_Util::debug( $url ); - if ( $this->requires_token() && empty( $this->token ) ) - return new Keyring_Error( 'keyring-request-error', __( 'No token' ) ); + if ( $this->requires_token() && empty( $this->token ) ) { + return new Keyring_Error( 'keyring-request-error', __( 'No token', 'keyring' ) ); + } $token = $this->token ? $this->token : null; - if ( !is_null( $token ) ) { + if ( ! is_null( $token ) ) { if ( $this->authorization_header ) { // type can be OAuth, Bearer, ... $params['headers']['Authorization'] = $this->authorization_header . ' ' . (string) $token; @@ -213,26 +227,34 @@ function request( $url, array $params = array() ) { Keyring_Util::debug( $params ); switch ( strtoupper( $method ) ) { - case 'GET': - $res = wp_remote_get( $url, $params ); - break; - - case 'POST': - $params = array_merge( array( 'sslverify' => false ), $params ); - $res = wp_remote_post( $url, $params ); - break; - - case 'PUT': - // Make sure PUT requests have a Content-Length header set otherwise the server might return an error - // See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html - if ( ! isset( $params['body'] ) && ! isset( $params['headers']['Content-Length'] ) ) { - $params['headers']['Content-Length'] = '0'; - } - - default: - $params = array_merge( array( 'method' => $method, 'sslverify' => false ), $params ); - $res = wp_remote_request( $url, $params ); - break; + case 'GET': + $res = wp_remote_get( $url, $params ); + break; + + case 'POST': + $params = array_merge( array( 'sslverify' => false ), $params ); + $res = wp_remote_post( $url, $params ); + break; + + case 'PUT': + // Make sure PUT requests have a Content-Length header set otherwise the server might return an error + // See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html + if ( ! isset( $params['body'] ) && ! isset( $params['headers']['Content-Length'] ) ) { + $params['headers']['Content-Length'] = '0'; + } + + // Intentional fall-through. + + default: + $params = array_merge( + array( + 'method' => $method, + 'sslverify' => false, + ), + $params + ); + $res = wp_remote_request( $url, $params ); + break; } Keyring_Util::debug( 'OAuth2 Response' ); @@ -240,10 +262,10 @@ function request( $url, array $params = array() ) { $this->set_request_response_code( wp_remote_retrieve_response_code( $res ) ); // Accept all 2xx response codes - if ( '2' == substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { + if ( '2' === substr( wp_remote_retrieve_response_code( $res ), 0, 1 ) ) { if ( $full_response ) { return $res; - } else if ( $raw_response ) { + } elseif ( $raw_response ) { return wp_remote_retrieve_body( $res ); } else { return $this->parse_response( wp_remote_retrieve_body( $res ) ); diff --git a/includes/services/extended/0-google-base.php b/includes/services/extended/0-google-base.php index fc1febe3922..97a9062366a 100644 --- a/includes/services/extended/0-google-base.php +++ b/includes/services/extended/0-google-base.php @@ -12,7 +12,7 @@ */ class Keyring_Service_GoogleBase extends Keyring_Service_OAuth2 { - var $api_label = ''; + var $api_label = ''; function __construct() { parent::__construct(); @@ -23,12 +23,12 @@ function __construct() { add_filter( 'keyring_' . $this->get_name() . '_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://accounts.google.com/o/oauth2/v2/auth', 'GET' ); - $this->set_endpoint( 'access_token', 'https://www.googleapis.com/oauth2/v4/token', 'POST' ); - $this->set_endpoint( 'refresh', 'https://www.googleapis.com/oauth2/v4/token', 'POST' ); - $this->set_endpoint( 'userinfo', 'https://www.googleapis.com/oauth2/v3/userinfo', 'GET' ); + $this->set_endpoint( 'authorize', 'https://accounts.google.com/o/oauth2/v2/auth', 'GET' ); + $this->set_endpoint( 'access_token', 'https://www.googleapis.com/oauth2/v4/token', 'POST' ); + $this->set_endpoint( 'refresh', 'https://www.googleapis.com/oauth2/v4/token', 'POST' ); + $this->set_endpoint( 'userinfo', 'https://www.googleapis.com/oauth2/v3/userinfo', 'GET' ); - $creds = $this->get_credentials(); + $creds = $this->get_credentials(); $this->redirect_uri = $creds['redirect_uri']; $this->key = $creds['key']; $this->secret = $creds['secret']; @@ -37,7 +37,7 @@ function __construct() { $this->authorization_parameter = false; // Need to reset the callback because Google is very strict about where it sends people - if ( !empty( $creds['redirect_uri'] ) ) { + if ( ! empty( $creds['redirect_uri'] ) ) { $this->callback_url = $creds['redirect_uri']; // Allow user to manually enter a redirect URI } else { $this->callback_url = remove_query_arg( array( 'nonce', 'kr_nonce' ), $this->callback_url ); // At least strip nonces, since you can't save them in your app config @@ -50,26 +50,27 @@ function __construct() { function basic_ui_intro() { $class = get_called_class(); + /* translators: url */ echo '

' . sprintf( __( "Google controls access to all of their APIs through their API Console. Go to the Library page in the console and click the Select a project dropdown next to the logo in the upper left of the screen. Click the plus icon to create a new project. Enter a name and then click Create.", 'keyring' ), 'https://console.developers.google.com/apis/library' ) . '

'; /* translators: %s: The name of the service being connected */ - echo '

' . sprintf( __( "Now you need to enable the %s and setup your OAuth credentials.", 'keyring' ), $class::LABEL ) . '

'; + echo '

' . sprintf( __( 'Now you need to enable the %s and setup your OAuth credentials.', 'keyring' ), $class::LABEL ) . '

'; echo '
    '; - echo '
  1. ' . __( "Select your project from the project dropdown.", 'keyring' ) . '
  2. '; - echo '
  3. ' . __( "Click Library in the menu on the left.", 'keyring' ) . '
  4. '; + echo '
  5. ' . __( 'Select your project from the project dropdown.', 'keyring' ) . '
  6. '; + echo '
  7. ' . __( 'Click Library in the menu on the left.', 'keyring' ) . '
  8. '; /* translators: %s: The name of the service being connected */ - echo '
  9. ' . sprintf( __( "Find and click %s.", 'keyring' ), $class::LABEL ) . '
  10. '; - echo '
  11. ' . __( "Next to the heading, click Enable.", 'keyring' ) . '
  12. '; - echo '
  13. ' . __( "Click the blue button labelled Create credentials.", 'keyring' ) . '
  14. '; - echo '
  15. ' . __( "Click Credential in the menu on the left.", 'keyring' ) . '
  16. '; - echo '
  17. ' . __( "Click the OAuth consent screen menu item.", 'keyring' ) . '
  18. '; - echo '
  19. ' . __( "You must enter a Product name, but you can skip the logo and home page URL.", 'keyring' ) . '
  20. '; - echo '
  21. ' . __( "Click Save.", 'keyring' ) . '
  22. '; - echo '
  23. ' . __( "Click the Create credentials button and select OAuth client ID.", 'keyring' ) . '
  24. '; - echo '
  25. ' . __( "Select Web application and enter a relevant name or just use the default.", 'keyring' ) . '
  26. '; + echo '
  27. ' . sprintf( __( 'Find and click %s.', 'keyring' ), $class::LABEL ) . '
  28. '; + echo '
  29. ' . __( 'Next to the heading, click Enable.', 'keyring' ) . '
  30. '; + echo '
  31. ' . __( 'Click the blue button labelled Create credentials.', 'keyring' ) . '
  32. '; + echo '
  33. ' . __( 'Click Credential in the menu on the left.', 'keyring' ) . '
  34. '; + echo '
  35. ' . __( 'Click the OAuth consent screen menu item.', 'keyring' ) . '
  36. '; + echo '
  37. ' . __( 'You must enter a Product name, but you can skip the logo and home page URL.', 'keyring' ) . '
  38. '; + echo '
  39. ' . __( 'Click Save.', 'keyring' ) . '
  40. '; + echo '
  41. ' . __( 'Click the Create credentials button and select OAuth client ID.', 'keyring' ) . '
  42. '; + echo '
  43. ' . __( 'Select Web application and enter a relevant name or just use the default.', 'keyring' ) . '
  44. '; /* translators: %s: The user's domain name */ - echo '
  45. ' . sprintf( __( "For the Authorized JavaScript Origins, enter the URL of your domain, e.g. %s.", 'keyring' ), ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] ) . '
  46. '; + echo '
  47. ' . sprintf( __( 'For the Authorized JavaScript Origins, enter the URL of your domain, e.g. %s.', 'keyring' ), ( is_ssl() ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'] ) . '
  48. '; /* translators: %s: The redirect URL to verify the connection */ - echo '
  49. ' . sprintf( __( "In the Authorized Redirect URIs box, enter the URL %s.", 'keyring' ), Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify' ) ) ) . '
  50. '; + echo '
  51. ' . sprintf( __( 'In the Authorized Redirect URIs box, enter the URL %s.', 'keyring' ), Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify' ) ) ) . '
  52. '; echo '
  53. ' . __( "Click Create when you're done.", 'keyring' ) . '
  54. '; echo '
'; echo '

' . __( "Once you've saved your details, copy the Client ID into the Client ID field below, and the Client secret value into Client Secret. The Redirect URI box should fill itself out for you.", 'keyring' ) . '

'; @@ -77,7 +78,7 @@ function basic_ui_intro() { } function request_token_params( $params ) { - $class = get_called_class(); + $class = get_called_class(); $params['prompt'] = 'consent'; // Always prompt, and get a refresh token for offline access $params['scope'] = $class::SCOPE; $params['access_type'] = $class::ACCESS_TYPE; @@ -150,21 +151,24 @@ function maybe_refresh_token() { return; } - $response = wp_remote_post( $this->refresh_url, array( - 'method' => $this->refresh_method, - 'body' => array( - 'client_id' => $this->key, - 'client_secret' => $this->secret, - 'refresh_token' => $meta['refresh_token'], - 'grant_type' => 'refresh_token', - ), - ) ); + $response = wp_remote_post( + $this->refresh_url, + array( + 'method' => $this->refresh_method, + 'body' => array( + 'client_id' => $this->key, + 'client_secret' => $this->secret, + 'refresh_token' => $meta['refresh_token'], + 'grant_type' => 'refresh_token', + ), + ) + ); if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return false; } - $return = json_decode( wp_remote_retrieve_body( $response ) ); + $return = json_decode( wp_remote_retrieve_body( $response ) ); $meta['expires'] = time() + $return->expires_in; // Build access token @@ -177,7 +181,7 @@ function maybe_refresh_token() { // Store the updated access token $access_token = apply_filters( 'keyring_access_token', $access_token, (array) $return ); - $id = $this->store->update( $access_token ); + $id = $this->store->update( $access_token ); // And switch to using it $this->set_token( $access_token ); @@ -200,16 +204,22 @@ function basic_ui() { // Handle actually saving credentials if ( isset( $_POST['api_key'] ) && isset( $_POST['api_secret'] ) ) { // Store credentials against this service - $this->update_credentials( array( - 'key' => stripslashes( trim( $_POST['api_key'] ) ), - 'secret' => stripslashes( trim( $_POST['api_secret'] ) ), - 'redirect_uri' => stripslashes( $_POST['redirect_uri'] ), - ) ); + $this->update_credentials( + array( + 'key' => stripslashes( trim( $_POST['api_key'] ) ), + 'secret' => stripslashes( trim( $_POST['api_secret'] ) ), + 'redirect_uri' => stripslashes( $_POST['redirect_uri'] ), + ) + ); echo '

' . __( 'Credentials saved.', 'keyring' ) . '

'; } - $api_key = $api_secret = $redirect_uri = ''; - if ( $creds = $this->get_credentials() ) { + $api_key = ''; + $api_secret = ''; + $redirect_uri = ''; + + $creds = $this->get_credentials(); + if ( $creds ) { $api_key = $creds['key']; $api_secret = $creds['secret']; $redirect_uri = $creds['redirect_uri']; diff --git a/includes/services/extended/500px.php b/includes/services/extended/500px.php index 09717b8f480..3f299b7f137 100644 --- a/includes/services/extended/500px.php +++ b/includes/services/extended/500px.php @@ -18,26 +18,27 @@ function __construct() { } $this->authorization_header = true; - $this->authorization_realm = 'api.500px.com'; + $this->authorization_realm = 'api.500px.com'; $this->set_endpoint( 'request_token', 'https://api.500px.com/v1/oauth/request_token', 'GET' ); - $this->set_endpoint( 'authorize', 'https://api.500px.com/v1/oauth/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://api.500px.com/v1/oauth/access_token', 'GET' ); - $this->set_endpoint( 'authenticate', 'https://api.500px.com/v1/oauth/authorize', 'GET' ); - $this->set_endpoint( 'users', 'https://api.500px.com/v1/users', 'GET' ); + $this->set_endpoint( 'authorize', 'https://api.500px.com/v1/oauth/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://api.500px.com/v1/oauth/access_token', 'GET' ); + $this->set_endpoint( 'authenticate', 'https://api.500px.com/v1/oauth/authorize', 'GET' ); + $this->set_endpoint( 'users', 'https://api.500px.com/v1/users', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); + $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; $this->requires_token( true ); } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'To connect to 500px, you\'ll need to create an application at 500px.com.', 'keyring' ), 'https://500px.com/settings/applications' ) . '

'; echo '

' . __( "Once you've created your app, enter the Consumer Key and Consumer Secret below.", 'keyring' ) . '

'; } @@ -58,7 +59,7 @@ function build_token_meta( $token ) { ) ); - $response = $this->request( "https://api.500px.com/v1/users", array( 'method' => 'GET' ) ); + $response = $this->request( 'https://api.500px.com/v1/users', array( 'method' => 'GET' ) ); if ( Keyring_Util::is_error( $response ) ) { $meta = array(); diff --git a/includes/services/extended/eventbrite.php b/includes/services/extended/eventbrite.php index b429e8814d7..1bd69c80131 100644 --- a/includes/services/extended/eventbrite.php +++ b/includes/services/extended/eventbrite.php @@ -6,17 +6,17 @@ */ class Keyring_Service_Eventbrite extends Keyring_Service_OAuth2 { - const NAME = 'eventbrite'; - const LABEL = 'Eventbrite'; - const API_BASE = 'https://www.eventbriteapi.com/v3/'; + const NAME = 'eventbrite'; + const LABEL = 'Eventbrite'; + const API_BASE = 'https://www.eventbriteapi.com/v3/'; const OAUTH_BASE = 'https://www.eventbrite.com/oauth/'; function __construct() { parent::__construct(); - $this->set_endpoint( 'authorize', self::OAUTH_BASE . 'authorize', 'GET' ); - $this->set_endpoint( 'access_token', self::OAUTH_BASE . 'token', 'POST' ); - $this->set_endpoint( 'self', self::API_BASE . 'users/me/', 'GET' ); + $this->set_endpoint( 'authorize', self::OAUTH_BASE . 'authorize', 'GET' ); + $this->set_endpoint( 'access_token', self::OAUTH_BASE . 'token', 'POST' ); + $this->set_endpoint( 'self', self::API_BASE . 'users/me/', 'GET' ); // Enable "basic" UI for entering key/secret if ( ! KEYRING__HEADLESS_MODE ) { @@ -24,17 +24,18 @@ function __construct() { add_filter( 'keyring_eventbrite_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $this->authorization_header = 'Bearer'; $this->authorization_parameter = false; } function basic_ui_intro() { - echo '

' . sprintf( __( "To get started, register an OAuth client on Eventbrite. The most important setting is the OAuth redirect_uri, which should be set to %s. You can set the other values to whatever you like.", 'keyring' ), esc_url( Keyring_Util::admin_url( 'eventbrite', array( 'action' => 'verify' ) ) ) ) . '

'; + /* translators: url */ + echo '

' . sprintf( __( "To get started, register an OAuth client on Eventbrite. The most important setting is the OAuth redirect_uri, which should be set to %s. You can set the other values to whatever you like.", 'keyring' ), esc_url( Keyring_Util::admin_url( 'eventbrite', array( 'action' => 'verify' ) ) ) ) . '

'; echo '

' . __( "Once you've saved those changes, copy the APPLICATION KEY value into the API Key field, then click the 'Show' link next to the OAuth client secret, copy the value into the API Secret field and click save (you don't need an App ID value for Eventbrite).", 'keyring' ) . '

'; } @@ -47,7 +48,7 @@ function build_token_meta( $token ) { ) ); $response = $this->request( $this->self_url, array( 'method' => $this->self_method ) ); - $meta = array(); + $meta = array(); if ( ! Keyring_Util::is_error( $response ) ) { if ( isset( $response->emails->email ) ) { $meta['username'] = $response->emails->email; diff --git a/includes/services/extended/facebook.php b/includes/services/extended/facebook.php index 9d49d6d32c6..a8f8ff96580 100644 --- a/includes/services/extended/facebook.php +++ b/includes/services/extended/facebook.php @@ -17,19 +17,26 @@ function __construct() { add_filter( 'keyring_facebook_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://www.facebook.com/v2.9/dialog/oauth', 'GET' ); + $this->set_endpoint( 'authorize', 'https://www.facebook.com/v2.9/dialog/oauth', 'GET' ); $this->set_endpoint( 'access_token', 'https://graph.facebook.com/v2.9/oauth/access_token', 'GET' ); - $this->set_endpoint( 'self', 'https://graph.facebook.com/v2.9/me', 'GET' ); - $this->set_endpoint( 'profile_pic', 'https://graph.facebook.com/v2.9/me/picture/?redirect=false&width=150&height=150', 'GET' ); + $this->set_endpoint( 'self', 'https://graph.facebook.com/v2.9/me', 'GET' ); + $this->set_endpoint( 'profile_pic', 'https://graph.facebook.com/v2.9/me/picture/?redirect=false&width=150&height=150', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $kr_nonce = wp_create_nonce( 'keyring-verify' ); $nonce = wp_create_nonce( 'keyring-verify-facebook' ); - $this->redirect_uri = Keyring_Util::admin_url( self::NAME, array( 'action' => 'verify', 'kr_nonce' => $kr_nonce, 'nonce' => $nonce, ) ); + $this->redirect_uri = Keyring_Util::admin_url( + self::NAME, + array( + 'action' => 'verify', + 'kr_nonce' => $kr_nonce, + 'nonce' => $nonce, + ) + ); $this->requires_token( true ); @@ -39,12 +46,15 @@ function __construct() { function basic_ui_intro() { echo '

' . __( "If you haven't already, you'll need to set up an app on Facebook:", 'keyring' ) . '

'; echo '
    '; + /* translators: url */ echo '
  1. ' . sprintf( __( "Click + Create New App at the top-right of this page", 'keyring' ), 'https://developers.facebook.com/apps' ) . '
  2. '; - echo '
  3. ' . __( "Enter a name for your app (maybe the name of your website?) and a Category, click Continue (you can skip optional things)", 'keyring' ) . '
  4. '; - echo '
  5. ' . __( "Enter whatever is in the CAPTCHA and click Continue", 'keyring' ) . '
  6. '; - echo '
  7. ' . sprintf( __( "Click Settings on the left and then Advanced at the top of that page. Under Valid OAuth redirect URIs, enter your domain name. That value is probably %s", 'keyring' ), $_SERVER['HTTP_HOST'] ) . '
  8. '; - echo '
  9. ' . sprintf( __( "Click the Website with Facebook Login box and enter the URL to your website, which is probably %s", 'keyring' ), get_bloginfo( 'url' ) ) . '
  10. '; - echo '
  11. ' . __( "Click Save Changes", 'keyring' ) . '
  12. '; + echo '
  13. ' . __( 'Enter a name for your app (maybe the name of your website?) and a Category, click Continue (you can skip optional things)', 'keyring' ) . '
  14. '; + echo '
  15. ' . __( 'Enter whatever is in the CAPTCHA and click Continue', 'keyring' ) . '
  16. '; + /* translators: url */ + echo '
  17. ' . sprintf( __( 'Click Settings on the left and then Advanced at the top of that page. Under Valid OAuth redirect URIs, enter your domain name. That value is probably %s', 'keyring' ), $_SERVER['HTTP_HOST'] ) . '
  18. '; + /* translators: url */ + echo '
  19. ' . sprintf( __( 'Click the Website with Facebook Login box and enter the URL to your website, which is probably %s', 'keyring' ), get_bloginfo( 'url' ) ) . '
  20. '; + echo '
  21. ' . __( 'Click Save Changes', 'keyring' ) . '
  22. '; echo '
'; echo '

' . __( "Once you're done configuring your app, copy and paste your App ID and App Secret (in the top section of your app's Basic details) into the appropriate fields below. Leave the App Key field blank.", 'keyring' ) . '

'; } @@ -62,8 +72,8 @@ function _get_credentials() { ); } else { $all = apply_filters( 'keyring_credentials', get_option( 'keyring_credentials' ) ); - if ( !empty( $all['facebook'] ) ) { - $creds = $all['facebook']; + if ( ! empty( $all['facebook'] ) ) { + $creds = $all['facebook']; $creds['key'] = $creds['app_id']; return $creds; } @@ -75,7 +85,7 @@ function _get_credentials() { function is_configured() { $credentials = $this->get_credentials(); - return !empty( $credentials['app_id'] ) && !empty( $credentials['secret'] ); + return ! empty( $credentials['app_id'] ) && ! empty( $credentials['secret'] ); } /** @@ -84,8 +94,10 @@ function is_configured() { * @return Array containing originals, plus the scope parameter */ function filter_request_token( $params ) { - if ( $scope = implode( ',', apply_filters( 'keyring_facebook_scope', array() ) ) ) + $scope = implode( ',', apply_filters( 'keyring_facebook_scope', array() ) ); + if ( $scope ) { $params['scope'] = $scope; + } return $params; } @@ -102,9 +114,9 @@ function build_token_meta( $token ) { $meta = array(); } else { $meta = array( - 'user_id' => $response->id, - 'name' => $response->name, - 'picture' => "https://graph.facebook.com/v2.9/{$response->id}/picture?type=large", + 'user_id' => $response->id, + 'name' => $response->name, + 'picture' => "https://graph.facebook.com/v2.9/{$response->id}/picture?type=large", ); } @@ -135,7 +147,7 @@ function get_fb_pages( $connection = false ) { } $additional_external_users = array(); - $fb_accounts = $this->request( 'https://graph.facebook.com/v2.9/me/accounts/' ); + $fb_accounts = $this->request( 'https://graph.facebook.com/v2.9/me/accounts/' ); if ( ! empty( $fb_accounts ) && ! is_wp_error( $fb_accounts ) ) { foreach ( $fb_accounts->data as $fb_account ) { if ( empty( $fb_account->access_token ) ) { @@ -145,11 +157,14 @@ function get_fb_pages( $connection = false ) { // Must request page with access token from /me/accounts, // otherwise can_post returns as voice of user, not page $fb_account_url = 'https://graph.facebook.com/v2.9/' . urlencode( $fb_account->id ); - $fb_account_url = add_query_arg( array( - 'access_token' => $fb_account->access_token, - 'fields' => 'is_published,can_post,id,name,category,picture', - ), $fb_account_url ); - $fb_page = $this->request( $fb_account_url ); + $fb_account_url = add_query_arg( + array( + 'access_token' => $fb_account->access_token, + 'fields' => 'is_published,can_post,id,name,category,picture', + ), + $fb_account_url + ); + $fb_page = $this->request( $fb_account_url ); // only continue with this account as a viable option if we can post content to it if ( ! $fb_page->is_published || ! $fb_page->can_post ) { @@ -172,7 +187,6 @@ function get_fb_pages( $connection = false ) { } } - return $additional_external_users; } diff --git a/includes/services/extended/fitbit.php b/includes/services/extended/fitbit.php index 554e8245ed1..aa41578e63d 100644 --- a/includes/services/extended/fitbit.php +++ b/includes/services/extended/fitbit.php @@ -19,15 +19,15 @@ function __construct() { add_filter( 'keyring_fitbit_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://www.fitbit.com/oauth2/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://api.fitbit.com/oauth2/token', 'POST' ); - $this->set_endpoint( 'refresh', 'https://api.fitbit.com/oauth2/token', 'POST' ); - $this->set_endpoint( 'profile', 'https://api.fitbit.com/1/user/-/profile.json', 'GET' ); + $this->set_endpoint( 'authorize', 'https://www.fitbit.com/oauth2/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://api.fitbit.com/oauth2/token', 'POST' ); + $this->set_endpoint( 'refresh', 'https://api.fitbit.com/oauth2/token', 'POST' ); + $this->set_endpoint( 'profile', 'https://api.fitbit.com/1/user/-/profile.json', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; // Fitbit requires an exact match on Redirect URI, which means we can't send any nonces $this->callback_url = remove_query_arg( array( 'nonce', 'kr_nonce' ), $this->callback_url ); @@ -44,9 +44,10 @@ function __construct() { } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'Go to Fitbit and create a new application, which allows Keyring to talk to Fitbit.', 'keyring' ), 'https://dev.fitbit.com/apps/new' ) . '

'; /* translators: %s: the redirect URL to verify the connection */ - echo '

' . sprintf( __( "You can use anything for the name/description details etc. Make sure you set the OAuth 2.0 Application Type to Personal (grants you some extra access) and set your Callback URL to %s. You only need Read-Only access if you are syncing data, but Read & Write will let you update details as well.", 'keyring' ), Keyring_Util::admin_url( self::NAME, array( 'action' => 'verify' ) ) ) . '

'; + echo '

' . sprintf( __( 'You can use anything for the name/description details etc. Make sure you set the OAuth 2.0 Application Type to Personal (grants you some extra access) and set your Callback URL to %s. You only need Read-Only access if you are syncing data, but Read & Write will let you update details as well.', 'keyring' ), Keyring_Util::admin_url( self::NAME, array( 'action' => 'verify' ) ) ) . '

'; } function request_token_params( $params ) { @@ -145,24 +146,27 @@ function refresh_token() { } // Refresh our access token - $response = wp_remote_post( $this->refresh_url, array( - 'method' => $this->refresh_method, - 'headers' => $this->get_basic_auth(), - 'body' => array( - 'grant_type' => 'refresh_token', - 'refresh_token' => $meta['refresh_token'], + $response = wp_remote_post( + $this->refresh_url, + array( + 'method' => $this->refresh_method, + 'headers' => $this->get_basic_auth(), + 'body' => array( + 'grant_type' => 'refresh_token', + 'refresh_token' => $meta['refresh_token'], + ), ) - ) ); + ); Keyring_Util::debug( $response ); if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return false; } - $return = json_decode( wp_remote_retrieve_body( $response ) ); + $return = json_decode( wp_remote_retrieve_body( $response ) ); $meta['refresh_token'] = $return->refresh_token; - $meta['expires'] = time() + $return->expires_in; - $access_token = new Keyring_Access_Token( + $meta['expires'] = time() + $return->expires_in; + $access_token = new Keyring_Access_Token( $this->get_name(), $return->access_token, $meta, diff --git a/includes/services/extended/flickr.php b/includes/services/extended/flickr.php index 9d622e5ab8c..9d8910feb42 100644 --- a/includes/services/extended/flickr.php +++ b/includes/services/extended/flickr.php @@ -18,21 +18,22 @@ function __construct() { } $this->set_endpoint( 'request_token', 'https://www.flickr.com/services/oauth/request_token', 'GET' ); - $this->set_endpoint( 'authorize', 'https://www.flickr.com/services/oauth/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://www.flickr.com/services/oauth/access_token', 'GET' ); + $this->set_endpoint( 'authorize', 'https://www.flickr.com/services/oauth/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://www.flickr.com/services/oauth/access_token', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); + $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; $this->requires_token( true ); } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'To connect to Flickr, you\'ll need to create an application at Flickr.com. If this is a personal website then you can use a non-commercial key (which will be approved automatically).', 'keyring' ), 'http://www.flickr.com/services/apps/create/apply/?' ) . '

'; echo '

' . __( "Once you've created your app, enter the API Key and Secret below (App ID is not required for Flickr apps).", 'keyring' ) . '

'; } @@ -48,13 +49,13 @@ function build_token_meta( $token ) { ) ) ); - $url = 'https://api.flickr.com/services/rest/?'; + $url = 'https://api.flickr.com/services/rest/?'; $params = array( 'method' => 'flickr.people.getInfo', 'api_key' => $this->key, 'user_id' => $token['user_nsid'], ); - $url .= http_build_query( $params ); + $url .= http_build_query( $params ); $response = $this->request( $url, array( 'method' => 'GET' ) ); if ( Keyring_Util::is_error( $response ) ) { @@ -73,10 +74,10 @@ function build_token_meta( $token ) { function get_display( Keyring_Access_Token $token ) { $return = ''; - $meta = $token->get_meta(); + $meta = $token->get_meta(); if ( ! empty( $meta['name'] ) ) { return $meta['name']; - } else if ( ! empty( $meta['username'] ) ) { + } elseif ( ! empty( $meta['username'] ) ) { return $meta['username']; } } @@ -95,7 +96,8 @@ function request( $url, array $params = array() ) { 'format' => 'json', // Always return JSON 'nojsoncallback' => 1, // Don't wrap it in a callback ), - $url ); + $url + ); return parent::request( $url, $params ); } @@ -110,12 +112,12 @@ function parse_response( $response ) { } function test_connection() { - $url = "https://api.flickr.com/services/rest/?"; + $url = 'https://api.flickr.com/services/rest/?'; $params = array( 'method' => 'flickr.test.login', 'api_key' => $this->key, ); - $url .= http_build_query( $params ); + $url .= http_build_query( $params ); $response = $this->request( $url ); if ( ! Keyring_Util::is_error( $response ) ) { diff --git a/includes/services/extended/foursquare.php b/includes/services/extended/foursquare.php index 1a601035280..d608cdd4b85 100644 --- a/includes/services/extended/foursquare.php +++ b/includes/services/extended/foursquare.php @@ -21,25 +21,26 @@ function __construct() { add_filter( 'keyring_foursquare_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://foursquare.com/oauth2/authenticate', 'GET' ); + $this->set_endpoint( 'authorize', 'https://foursquare.com/oauth2/authenticate', 'GET' ); $this->set_endpoint( 'access_token', 'https://foursquare.com/oauth2/access_token', 'GET' ); - $this->set_endpoint( 'self', 'https://api.foursquare.com/v2/users/self', 'GET' ); + $this->set_endpoint( 'self', 'https://api.foursquare.com/v2/users/self', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'If you haven\'t already, you\'ll need to create a new app at Foursquare. You should only need to worry about these settings:', 'keyring' ), 'https://foursquare.com/developers/register' ) . '

'; echo '
    '; echo '
  1. ' . __( "Your app name: enter whatever you like, maybe your website's name?", 'keyring' ) . '
  2. '; /* translators: %s: the blog URL */ echo '
  3. ' . sprintf( __( "Download / welcome page url: just enter your website's URL, %s", 'keyring' ), get_bloginfo( 'url' ) ) . '
  4. '; /* translators: %s: the redirect URL to verify the connection */ - echo '
  5. ' . sprintf( __( "Redirect URI(s): Copy-paste this, %s", 'keyring' ), Keyring_Util::admin_url( 'foursquare', array( 'action' => 'verify' ) ) ) . '
  6. '; - echo '
  7. ' . __( "New users can connect via the web: check the box", 'keyring' ) . '
  8. '; + echo '
  9. ' . sprintf( __( 'Redirect URI(s): Copy-paste this, %s', 'keyring' ), Keyring_Util::admin_url( 'foursquare', array( 'action' => 'verify' ) ) ) . '
  10. '; + echo '
  11. ' . __( 'New users can connect via the web: check the box', 'keyring' ) . '
  12. '; echo '
'; echo '

' . __( "Once you've saved those changes, copy the Client id value into the API Key field, and the Client secret value into the API Secret field and click save (you don't need an App ID value for Foursquare).", 'keyring' ) . '

'; } diff --git a/includes/services/extended/github.php b/includes/services/extended/github.php index 99298b880ca..6b303f5a22a 100644 --- a/includes/services/extended/github.php +++ b/includes/services/extended/github.php @@ -6,7 +6,7 @@ */ class Keyring_Service_Github extends Keyring_Service_OAuth2 { - const NAME = 'github'; + const NAME = 'github'; const LABEL = 'GitHub'; const SCOPE = ''; @@ -19,17 +19,16 @@ function __construct() { add_filter( 'keyring_github_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://github.com/login/oauth/authorize', 'GET' ); + $this->set_endpoint( 'authorize', 'https://github.com/login/oauth/authorize', 'GET' ); $this->set_endpoint( 'access_token', 'https://github.com/login/oauth/access_token', 'POST' ); - $this->set_endpoint( 'self', 'https://api.github.com/user', 'GET' ); + $this->set_endpoint( 'self', 'https://api.github.com/user', 'GET' ); $creds = $this->get_credentials(); - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - - $this->authorization_header = 'token'; + $this->authorization_header = 'token'; $this->authorization_parameter = false; add_filter( 'keyring_github_request_token_params', array( $this, 'request_token_params' ) ); @@ -44,12 +43,13 @@ function __construct() { function verify_token_post_params( $params ) { $params['headers'] = array( - 'Accept' => 'application/json' + 'Accept' => 'application/json', ); return $params; } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'To get started, register an OAuth client on GitHub. The most important setting is the OAuth redirect_uri, which should be set to %2$s. You can set the other values to whatever you like.', 'keyring' ), 'https://developer.github.com/apps/building-oauth-apps/creating-an-oauth-app/', Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify' ) ) ) . '

'; echo '

' . __( "Once you've saved those changes, copy the CLIENT ID value into the API Key field, and the CLIENT SECRET value into the API Secret field and click save (you don't need an App ID value for GitHub).", 'keyring' ) . '

'; } @@ -60,19 +60,19 @@ function request_token_params( $params ) { } function redirect_incoming_verify( $request ) { - if ( !isset( $request['kr_nonce'] ) ) { + if ( ! isset( $request['kr_nonce'] ) ) { // First request, from GitHub. Nonce it and move on. $kr_nonce = wp_create_nonce( 'keyring-verify' ); - $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); + $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); wp_safe_redirect( Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify', 'kr_nonce' => $kr_nonce, - 'nonce' => $nonce, - 'state' => $request['state'], - 'code' => $request['code'], // Auth code from successful response (maybe) + 'nonce' => $nonce, + 'state' => $request['state'], + 'code' => $request['code'], // Auth code from successful response (maybe) ) ) ); @@ -96,8 +96,8 @@ function build_token_meta( $token ) { $meta = array( 'user_id' => $response->id, 'username' => $response->login, - 'name' => $response->name, - 'picture' => $response->avatar_url + 'name' => $response->name, + 'picture' => $response->avatar_url, ); } diff --git a/includes/services/extended/google-drive.php b/includes/services/extended/google-drive.php index 1c24fd996e7..4258fdd710e 100644 --- a/includes/services/extended/google-drive.php +++ b/includes/services/extended/google-drive.php @@ -37,7 +37,7 @@ function _get_credentials() { */ function fetch_profile_picture() { $image = false; - $res = $this->request( $this->userinfo_url, array( 'method' => $this->self_method ) ); + $res = $this->request( $this->userinfo_url, array( 'method' => $this->self_method ) ); if ( ! Keyring_Util::is_error( $res ) ) { $image = str_replace( 's64', 's256', $res->picture ); diff --git a/includes/services/extended/instagram.php b/includes/services/extended/instagram.php index 3c65896cdb7..cdfad985b2a 100644 --- a/includes/services/extended/instagram.php +++ b/includes/services/extended/instagram.php @@ -18,20 +18,21 @@ function __construct() { add_filter( 'keyring_instagram_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://api.instagram.com/oauth/authorize/', 'GET' ); + $this->set_endpoint( 'authorize', 'https://api.instagram.com/oauth/authorize/', 'GET' ); $this->set_endpoint( 'access_token', 'https://api.instagram.com/oauth/access_token', 'POST' ); - $this->set_endpoint( 'self', 'https://api.instagram.com/v1/users/self/', 'GET' ); + $this->set_endpoint( 'self', 'https://api.instagram.com/v1/users/self/', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $this->authorization_header = false; // Send in querystring $this->authorization_parameter = 'access_token'; } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'To get started, register an OAuth client on Instagram. The most important setting is the OAuth redirect_uri, which should be set to %2$s. You can set the other values to whatever you like.', 'keyring' ), 'http://instagram.com/developer/clients/register/', Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify' ) ) ) . '

'; echo '

' . __( "Once you've saved those changes, copy the CLIENT ID value into the API Key field, and the CLIENT SECRET value into the API Secret field and click save (you don't need an App ID value for Instagram).", 'keyring' ) . '

'; } diff --git a/includes/services/extended/instapaper.php b/includes/services/extended/instapaper.php index 57ef1b36068..9c561e1cb9d 100644 --- a/includes/services/extended/instapaper.php +++ b/includes/services/extended/instapaper.php @@ -20,21 +20,22 @@ function __construct() { $this->authorization_header = true; - $this->set_endpoint( 'access_token', 'https://www.instapaper.com/api/1/oauth/access_token', 'POST' ); - $this->set_endpoint( 'verify', 'https://www.instapaper.com/api/1/account/verify_credentials', 'POST' ); + $this->set_endpoint( 'access_token', 'https://www.instapaper.com/api/1/oauth/access_token', 'POST' ); + $this->set_endpoint( 'verify', 'https://www.instapaper.com/api/1/account/verify_credentials', 'POST' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); + $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; $this->requires_token( true ); } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'To use the Instapaper API, you need to get manually approved. Apply here, then wait for a reply email.', 'keyring' ), 'http://www.instapaper.com/main/request_oauth_consumer_token' ) . '

'; echo '

' . __( "Once you get approved, you'll get an email back with your details. Copy the OAuth consumer key value into the API Key field, and the OAuth consumer secret value into the API Secret field and click save (you don't need an App ID value for Instapaper).", 'keyring' ) . '

'; } @@ -51,32 +52,32 @@ function request_ui() { if ( isset( $_GET['error'] ) ) { echo '
    '; switch ( $_GET['error'] ) { - case '401': - echo '
  • ' . __( 'Your account details could not be confirmed, please try again.', 'keyring' ) . '
  • '; - break; - case 'empty': - echo '
  • ' . __( 'Please make sure you enter a username and password.', 'keyring' ) . '
  • '; - break; + case '401': + echo '
  • ' . __( 'Your account details could not be confirmed, please try again.', 'keyring' ) . '
  • '; + break; + case 'empty': + echo '
  • ' . __( 'Please make sure you enter a username and password.', 'keyring' ) . '
  • '; + break; } echo '
'; } // Even though it doesn't make too much sense, we support request tokens in HTTP Basic // to ensure consistency with other services - $request_token = new Keyring_Request_Token( + $request_token = new Keyring_Request_Token( $this->get_name(), array(), apply_filters( 'keyring_request_token_meta', array( - 'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false + 'for' => isset( $_REQUEST['for'] ) ? (string) $_REQUEST['for'] : false, ), $this->get_name(), array() // no token ) ); - $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); - $request_token_id = $this->store_token( $request_token ); + $request_token = apply_filters( 'keyring_request_token', $request_token, $this ); + $request_token_id = $this->store_token( $request_token ); Keyring_Util::debug( 'xAuth/Instapaper Stored Request token ' . $request_token_id ); echo apply_filters( 'keyring_' . $this->get_name() . '_request_ui_intro', '' ); @@ -106,13 +107,14 @@ function request_ui() { jQuery( document ).ready( function() { jQuery( '#username' ).focus(); } ); - + get_name() ) ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-verify-' . $this->get_name() ) ) { Keyring::error( __( 'Invalid/missing verification nonce.', 'keyring' ) ); exit; } @@ -120,22 +122,32 @@ function verify_token() { // Load up the request token that got us here and globalize it if ( $_REQUEST['state'] ) { global $keyring_request_token; - $state = (int) $_REQUEST['state']; - $keyring_request_token = $this->store->get_token( array( 'id' => $state, 'type' => 'request' ) ); + $state = (int) $_REQUEST['state']; + $keyring_request_token = $this->store->get_token( + array( + 'id' => $state, + 'type' => 'request', + ) + ); Keyring_Util::debug( 'xAuth/Instapaper Loaded Request Token ' . $_REQUEST['state'] ); Keyring_Util::debug( $keyring_request_token ); // Remove request token, don't need it any more. - $this->store->delete( array( 'id' => $state, 'type' => 'request' ) ); + $this->store->delete( + array( + 'id' => $state, + 'type' => 'request', + ) + ); } - if ( !strlen( $_POST['username'] ) ) { + if ( ! strlen( $_POST['username'] ) ) { $url = Keyring_Util::admin_url( $this->get_name(), array( - 'action' => 'request', - 'error' => 'empty', - 'kr_nonce' => wp_create_nonce( 'keyring-request' ) + 'action' => 'request', + 'error' => 'empty', + 'kr_nonce' => wp_create_nonce( 'keyring-request' ), ) ); Keyring_Util::debug( $url ); @@ -150,7 +162,14 @@ function verify_token() { ); ksort( $body ); $this->set_token( new Keyring_Access_Token( $this->get_name(), null, array() ) ); - $res = $this->request( $this->access_token_url, array( 'method' => $this->access_token_method, 'raw_response' => true, 'body' => $body ) ); + $res = $this->request( + $this->access_token_url, + array( + 'method' => $this->access_token_method, + 'raw_response' => true, + 'body' => $body, + ) + ); Keyring_Util::debug( 'OAuth1 Access Token Response' ); Keyring_Util::debug( $res ); @@ -162,7 +181,7 @@ function verify_token() { array( 'action' => 'request', 'error' => '401', - 'kr_nonce' => wp_create_nonce( 'keyring-request' ) + 'kr_nonce' => wp_create_nonce( 'keyring-request' ), ) ); Keyring_Util::debug( $url ); @@ -207,9 +226,9 @@ function build_token_meta( $token ) { $meta = array(); } else { $meta = array( - 'user_id' => $response[0]->user_id, - 'username' => $response[0]->username, - 'name' => $response[0]->username, + 'user_id' => $response[0]->user_id, + 'username' => $response[0]->username, + 'name' => $response[0]->username, ); } @@ -221,12 +240,12 @@ function get_display( Keyring_Access_Token $token ) { } function test_connection() { - $response = $this->request( $this->verify_url, array( 'method' => $this->verify_method ) ); - if ( ! Keyring_Util::is_error( $response ) ) { - return true; - } + $response = $this->request( $this->verify_url, array( 'method' => $this->verify_method ) ); + if ( ! Keyring_Util::is_error( $response ) ) { + return true; + } - return $response; + return $response; } } diff --git a/includes/services/extended/jetpack.php b/includes/services/extended/jetpack.php index 9c6e68bd558..7657d583b26 100644 --- a/includes/services/extended/jetpack.php +++ b/includes/services/extended/jetpack.php @@ -19,14 +19,14 @@ function __construct() { add_filter( 'keyring_jetpack_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://public-api.wordpress.com/oauth2/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://public-api.wordpress.com/oauth2/token', 'POST' ); - $this->set_endpoint( 'self', 'https://public-api.wordpress.com/rest/v1/me/', 'GET' ); + $this->set_endpoint( 'authorize', 'https://public-api.wordpress.com/oauth2/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://public-api.wordpress.com/oauth2/token', 'POST' ); + $this->set_endpoint( 'self', 'https://public-api.wordpress.com/rest/v1/me/', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $this->authorization_header = 'Bearer'; @@ -35,7 +35,7 @@ function __construct() { add_action( 'pre_keyring_jetpack_verify', array( $this, 'redirect_incoming_verify' ) ); // Need to reset the callback because Google is very strict about where it sends people - if ( !empty( $creds['redirect_uri'] ) ) { + if ( ! empty( $creds['redirect_uri'] ) ) { $this->callback_url = $creds['redirect_uri']; // Allow user to manually enter a redirect URI } else { $this->callback_url = remove_query_arg( array( 'nonce', 'kr_nonce' ), $this->callback_url ); // At least strip nonces, since you can't save them in your app config @@ -60,7 +60,7 @@ function redirect_incoming_verify( $request ) { if ( ! isset( $request['kr_nonce'] ) ) { // First request, from WP.com. Nonce it and move on. $kr_nonce = wp_create_nonce( 'keyring-verify' ); - $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); + $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); wp_safe_redirect( Keyring_Util::admin_url( $this->get_name(), diff --git a/includes/services/extended/linkedin.php b/includes/services/extended/linkedin.php index 4f18294d890..c591746e6f2 100644 --- a/includes/services/extended/linkedin.php +++ b/includes/services/extended/linkedin.php @@ -19,15 +19,15 @@ function __construct() { add_filter( 'keyring_linkedin_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://www.linkedin.com/oauth/v2/authorization', 'GET' ); - $this->set_endpoint( 'access_token', 'https://www.linkedin.com/oauth/v2/accessToken', 'POST' ); - $this->set_endpoint( 'self', 'https://api.linkedin.com/v2/me', 'GET' ); - $this->set_endpoint( 'profile_pic', 'https://api.linkedin.com/v2/me/picture-urls::(original)/', 'GET' ); + $this->set_endpoint( 'authorize', 'https://www.linkedin.com/oauth/v2/authorization', 'GET' ); + $this->set_endpoint( 'access_token', 'https://www.linkedin.com/oauth/v2/accessToken', 'POST' ); + $this->set_endpoint( 'self', 'https://api.linkedin.com/v2/me', 'GET' ); + $this->set_endpoint( 'profile_pic', 'https://api.linkedin.com/v2/me/picture-urls::(original)/', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; @@ -37,9 +37,11 @@ function __construct() { } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( "To connect to LinkedIn, you'll first need to create an app. A lot of the details are required, but they're not actually important to the operation of your app, since Keyring will override any important settings.", 'keyring' ), 'https://www.linkedin.com/secure/developer?newapp=' ) . '

'; echo '

' . __( "Once you've created your app, go down to the Auth section and copy the Client ID value into the API Key field below, and the Client Secret value into the API Secret field", 'keyring' ) . '

'; - echo '

' . sprintf( __( "In the LinkedIn Redirect URLs: box, enter the URL %s.", 'keyring' ), Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify' ) ) ) . '

'; + /* translators: url */ + echo '

' . sprintf( __( 'In the LinkedIn Redirect URLs: box, enter the URL %s.', 'keyring' ), Keyring_Util::admin_url( $this->get_name(), array( 'action' => 'verify' ) ) ) . '

'; echo '

' . __( "Then click save (you don't need an App ID value for LinkedIn).", 'keyring' ) . '

'; } @@ -95,17 +97,17 @@ function build_token_meta( $token ) { } else { $this->person = $response; - $firstName = $this->person->firstName; - $lastName = $this->person->lastName; - $lfirst = "{$firstName->preferredLocale->language}_{$firstName->preferredLocale->country}"; - $llast = "{$lastName->preferredLocale->language}_{$lastName->preferredLocale->country}"; + $first_name = $this->person->firstName; + $last_name = $this->person->lastName; + $lfirst = "{$first_name->preferredLocale->language}_{$first_name->preferredLocale->country}"; + $llast = "{$last_name->preferredLocale->language}_{$last_name->preferredLocale->country}"; - $profilePicture = $this->person->profilePicture; + $profile_picture = $this->person->profilePicture; $meta = array( 'user_id' => $this->person->id, - 'name' => $firstName->localized->{$lfirst} . ' ' . $lastName->localized->{$llast}, - 'picture' => $profilePicture->{'displayImage~'}->elements[0]->identifiers[0]->identifier, + 'name' => $first_name->localized->{$lfirst} . ' ' . $last_name->localized->{$llast}, + 'picture' => $profile_picture->{'displayImage~'}->elements[0]->identifiers[0]->identifier, ); } @@ -121,7 +123,7 @@ function get_display( Keyring_Access_Token $token ) { * * @return string|mixed */ - function fetch_profile_picture () { + function fetch_profile_picture() { $response = $this->request( $this->self_url . '?projection=(profilePicture(displayImage~:playableStreams))', array( 'method' => $this->self_method ) @@ -131,6 +133,7 @@ function fetch_profile_picture () { return new WP_Error( 'missing-profile_picture', __( 'Could not find profile picture.', 'keyring' ) ); } + // phpcs:ignore WordPress.NamingConventions.ValidVariableName return $response->profilePicture->{'displayImage~'}->elements[0]->identifiers[0]->identifier; } diff --git a/includes/services/extended/moves.php b/includes/services/extended/moves.php index edf24380ef9..fefd69f2e79 100644 --- a/includes/services/extended/moves.php +++ b/includes/services/extended/moves.php @@ -35,15 +35,15 @@ function __construct() { add_filter( 'keyring_moves_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://api.moves-app.com/oauth/v1/authorize', 'GET' ); + $this->set_endpoint( 'authorize', 'https://api.moves-app.com/oauth/v1/authorize', 'GET' ); $this->set_endpoint( 'access_token', 'https://api.moves-app.com/oauth/v1/access_token', 'POST' ); - $this->set_endpoint( 'verify_token', 'https://api.moves-app.com/oauth/v1/tokeninfo', 'GET' ); - $this->set_endpoint( 'profile', 'https://api.moves-app.com/api/1.1/user/profile', 'GET' ); + $this->set_endpoint( 'verify_token', 'https://api.moves-app.com/oauth/v1/tokeninfo', 'GET' ); + $this->set_endpoint( 'profile', 'https://api.moves-app.com/api/1.1/user/profile', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; // Moves requires an exact match on Redirect URI, which means we can't send any nonces $this->callback_url = remove_query_arg( array( 'nonce', 'kr_nonce' ), $this->callback_url ); @@ -56,6 +56,7 @@ function __construct() { } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'Head over and create a new application on Moves-app which you\'ll use to connect.', 'keyring' ), 'https://dev.moves-app.com/apps/new' ) . '

'; /* translators: %s: the redirect URL to verify the connection */ echo '

' . sprintf( __( "Once it's created, click the Development tab. Your App ID and API Key are both shown on that page as Client ID. Enter your Client secret in the API Secret box. On that tab there is also a Redirect URI box, which you should set to %s.", 'keyring' ), Keyring_Util::admin_url( self::NAME, array( 'action' => 'verify' ) ) ) . '

'; @@ -67,7 +68,7 @@ function request_token_params( $params ) { } function redirect_incoming_verify( $request ) { - if ( !isset( $request['kr_nonce'] ) ) { + if ( ! isset( $request['kr_nonce'] ) ) { $kr_nonce = wp_create_nonce( 'keyring-verify' ); $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); wp_safe_redirect( diff --git a/includes/services/extended/nest.php b/includes/services/extended/nest.php index fcff588933c..7189dc52aa1 100644 --- a/includes/services/extended/nest.php +++ b/includes/services/extended/nest.php @@ -18,14 +18,14 @@ function __construct() { add_filter( 'keyring_nest_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://home.nest.com/login/oauth2', 'GET' ); + $this->set_endpoint( 'authorize', 'https://home.nest.com/login/oauth2', 'GET' ); $this->set_endpoint( 'access_token', 'https://api.home.nest.com/oauth2/access_token', 'POST' ); - $this->set_endpoint( 'self', 'https://developer-api.nest.com/', 'GET' ); + $this->set_endpoint( 'self', 'https://developer-api.nest.com/', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $this->authorization_header = 'Bearer'; @@ -41,7 +41,7 @@ function redirect_incoming_verify( $request ) { if ( ! isset( $request['kr_nonce'] ) ) { // First request, from Nest. Nonce it and move on. $kr_nonce = wp_create_nonce( 'keyring-verify' ); - $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); + $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); wp_safe_redirect( Keyring_Util::admin_url( $this->get_name(), diff --git a/includes/services/extended/pinterest.php b/includes/services/extended/pinterest.php index 004aac36559..332922dcffd 100644 --- a/includes/services/extended/pinterest.php +++ b/includes/services/extended/pinterest.php @@ -19,14 +19,14 @@ function __construct() { add_filter( 'keyring_pinterest_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://api.pinterest.com/oauth/', 'GET' ); - $this->set_endpoint( 'access_token', 'https://api.pinterest.com/v1/oauth/token', 'POST' ); - $this->set_endpoint( 'self', 'https://api.pinterest.com/v1/me/?fields=first_name,last_name,username,image', 'GET' ); // undocumented, but required to get the `image` in a single request + $this->set_endpoint( 'authorize', 'https://api.pinterest.com/oauth/', 'GET' ); + $this->set_endpoint( 'access_token', 'https://api.pinterest.com/v1/oauth/token', 'POST' ); + $this->set_endpoint( 'self', 'https://api.pinterest.com/v1/me/?fields=first_name,last_name,username,image', 'GET' ); // undocumented, but required to get the `image` in a single request - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; // Send auth token in query string $this->authorization_header = false; @@ -48,10 +48,10 @@ function request_token_params( $params ) { } function redirect_incoming_verify( $request ) { - if ( !isset( $request['kr_nonce'] ) ) { + if ( ! isset( $request['kr_nonce'] ) ) { // First request, from Pinterest. Nonce it and move on. $kr_nonce = wp_create_nonce( 'keyring-verify' ); - $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); + $nonce = wp_create_nonce( 'keyring-verify-' . $this->get_name() ); wp_safe_redirect( Keyring_Util::admin_url( $this->get_name(), @@ -90,7 +90,7 @@ function build_token_meta( $token ) { 'user_id' => $response->data->id, 'username' => $response->data->username, 'name' => $response->data->first_name . ' ' . $response->data->last_name, - 'picture' => $response->data->image->{'60x60'}->url // Pinterest violate their own docs that this should be 'small' + 'picture' => $response->data->image->{'60x60'}->url, // Pinterest violate their own docs that this should be 'small' ); } diff --git a/includes/services/extended/pocket.php b/includes/services/extended/pocket.php index 632f92e83ac..61e9deb67ea 100644 --- a/includes/services/extended/pocket.php +++ b/includes/services/extended/pocket.php @@ -13,15 +13,22 @@ function __construct() { add_filter( 'keyring_pocket_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'request_token', 'https://getpocket.com/v3/oauth/request', 'POST' ); - $this->set_endpoint( 'authorize', 'https://getpocket.com/auth/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://getpocket.com/v3/oauth/authorize', 'POST' ); + $this->set_endpoint( 'request_token', 'https://getpocket.com/v3/oauth/request', 'POST' ); + $this->set_endpoint( 'authorize', 'https://getpocket.com/auth/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://getpocket.com/v3/oauth/authorize', 'POST' ); $creds = $this->get_credentials(); $this->key = $creds['key']; $kr_nonce = wp_create_nonce( 'keyring-verify' ); $nonce = wp_create_nonce( 'keyring-verify-pocket' ); - $this->redirect_uri = Keyring_Util::admin_url( self::NAME, array( 'action' => 'verify', 'kr_nonce' => $kr_nonce, 'nonce' => $nonce, ) ); + $this->redirect_uri = Keyring_Util::admin_url( + self::NAME, + array( + 'action' => 'verify', + 'kr_nonce' => $kr_nonce, + 'nonce' => $nonce, + ) + ); add_filter( 'keyring_request_token', array( $this, 'obtain_request_token' ), 10, 1 ); add_filter( 'keyring_pocket_request_token_params', array( $this, 'request_token_params' ), 10, 1 ); @@ -31,10 +38,11 @@ function __construct() { function basic_ui_intro() { echo '

' . __( "If you haven't already, you'll need to set up an app on Pocket:", 'keyring' ) . '

'; echo '
    '; + /* translators: url */ echo '
  1. ' . sprintf( __( "Head over to this page", 'keyring' ), 'https://getpocket.com/developer/apps/new' ) . '
  2. '; - echo '
  3. ' . __( "Enter a name for your app (maybe the name of your website?) and a brief description.", 'keyring' ) . '
  4. '; - echo '
  5. ' . __( "Select Retrieve for Permissions, and Web for Platforms.", 'keyring' ) . '
  6. '; - echo '
  7. ' . __( "Accept Terms of Service and click CREATE APPLICATION", 'keyring' ) . '
  8. '; + echo '
  9. ' . __( 'Enter a name for your app (maybe the name of your website?) and a brief description.', 'keyring' ) . '
  10. '; + echo '
  11. ' . __( 'Select Retrieve for Permissions, and Web for Platforms.', 'keyring' ) . '
  12. '; + echo '
  13. ' . __( 'Accept Terms of Service and click CREATE APPLICATION', 'keyring' ) . '
  14. '; echo '
'; echo '

' . __( "Once you're done configuring your app, copy and paste your Consumer Key into API Key field. Leave the rest of the fields blank.", 'keyring' ) . '

'; } @@ -47,9 +55,9 @@ function is_configured() { function obtain_request_token( $token ) { if ( 'pocket' === $token->name ) { $this->requires_token = false; - $params = array( + $params = array( 'method' => $this->request_token_method, - 'body' => array( + 'body' => array( 'consumer_key' => $this->key, 'redirect_uri' => $this->callback_url, ), @@ -64,11 +72,22 @@ function obtain_request_token( $token ) { } function request_token_params( $params ) { - $token = $this->store->get_token( array( 'id' => $params['state'], 'type' => 'request' ) ); + $token = $this->store->get_token( + array( + 'id' => $params['state'], + 'type' => 'request', + ) + ); if ( isset( $token->token ) && ! empty( $token->token ) ) { $params = array( - 'redirect_uri' => add_query_arg( array( 'state' => $params['state'], 'code' => $token->token ), $params['redirect_uri'] ), + 'redirect_uri' => add_query_arg( + array( + 'state' => $params['state'], + 'code' => $token->token, + ), + $params['redirect_uri'] + ), 'request_token' => $token->token, ); } @@ -79,7 +98,7 @@ function request_token_params( $params ) { function verify_token_params( $params ) { return array( 'consumer_key' => $params['client_id'], - 'code' => $params['code'] + 'code' => $params['code'], ); } @@ -87,9 +106,9 @@ function verify_token_post_params( $params ) { return array( 'headers' => array( 'Content-Type' => 'application/json; charset=UTF-8', - 'X-Accept' => 'application/json' + 'X-Accept' => 'application/json', ), - 'body' => $params['body'] + 'body' => $params['body'], ); } @@ -100,12 +119,13 @@ function parse_access_token( $token ) { } function request( $url, array $params = array() ) { - $params['body']['consumer_key'] = $this->key; + $params['body']['consumer_key'] = $this->key; $params['headers']['Content-Type'] = 'application/json; charset=UTF-8'; $params['headers']['X-Accept'] = 'application/json'; - if ( $token = $this->get_token() ) { + $token = $this->get_token(); + if ( $token ) { $params['body']['access_token'] = $token->token; } @@ -119,8 +139,8 @@ function build_token_meta( $token ) { $meta = array(); } else { $meta = array( - 'user_id' => $token['username'], - 'name' => $token['username'], + 'user_id' => $token['username'], + 'name' => $token['username'], ); } @@ -137,8 +157,8 @@ function test_connection() { array( 'method' => 'POST', 'body' => array( - 'count' => 1 - ) + 'count' => 1, + ), ) ); if ( ! Keyring_Util::is_error( $response ) ) { diff --git a/includes/services/extended/runkeeper.php b/includes/services/extended/runkeeper.php index f50707efde1..c35ee2d8c37 100644 --- a/includes/services/extended/runkeeper.php +++ b/includes/services/extended/runkeeper.php @@ -18,22 +18,23 @@ function __construct() { add_filter( 'keyring_runkeeper_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://runkeeper.com/apps/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://runkeeper.com/apps/token', 'POST' ); - $this->set_endpoint( 'deauthorize', 'https://runkeeper.com/apps/de-authorize', 'POST' ); - $this->set_endpoint( 'user', 'https://api.runkeeper.com/user', 'GET' ); - $this->set_endpoint( 'profile', 'https://api.runkeeper.com/profile', 'GET' ); + $this->set_endpoint( 'authorize', 'https://runkeeper.com/apps/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://runkeeper.com/apps/token', 'POST' ); + $this->set_endpoint( 'deauthorize', 'https://runkeeper.com/apps/de-authorize', 'POST' ); + $this->set_endpoint( 'user', 'https://api.runkeeper.com/user', 'GET' ); + $this->set_endpoint( 'profile', 'https://api.runkeeper.com/profile', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $this->authorization_header = 'Bearer'; $this->authorization_parameter = false; } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'You\'ll need to register a new application on RunKeeper so that you can connect. Be sure to check the Read Health Information option under Permissions Requests (and explain why you want to read that data). You will also be required to set an Estimated Date of Publication.', 'keyring' ), 'http://runkeeper.com/partner/applications/register' ) . '

'; echo '

' . __( "Once you've registered your application, click the Application Keys and URLs next to it, and copy the Client ID into the API Key field below, and the Client Secret value into API Secret.", 'keyring' ) . '

'; } @@ -55,7 +56,7 @@ function build_token_meta( $token ) { // Now get the rest of their profile $profile = $this->request( $this->profile_url, array( 'method' => $this->profile_method ) ); - if ( !Keyring_Util::is_error( $profile ) ) { + if ( ! Keyring_Util::is_error( $profile ) ) { $meta['username'] = substr( $profile->profile, strrpos( $profile->profile, '/' ) + 1 ); $meta['name'] = $profile->name; $meta['picture'] = $profile->large_picture; @@ -67,7 +68,8 @@ function build_token_meta( $token ) { } function get_display( Keyring_Access_Token $token ) { - return $token->get_meta( 'name' );; + return $token->get_meta( 'name' ); + } function test_connection() { diff --git a/includes/services/extended/strava.php b/includes/services/extended/strava.php index 846e56a6348..13e07547675 100644 --- a/includes/services/extended/strava.php +++ b/includes/services/extended/strava.php @@ -18,16 +18,16 @@ function __construct() { add_filter( 'keyring_strava_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'authorize', 'https://www.strava.com/oauth/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://www.strava.com/oauth/token', 'POST' ); - $this->set_endpoint( 'refresh', 'https://www.strava.com/oauth/token', 'POST' ); - $this->set_endpoint( 'deauthorize', 'https://www.strava.com/oauth/deauthorize', 'POST' ); - $this->set_endpoint( 'user', 'https://www.strava.com/api/v3/athlete', 'GET' ); + $this->set_endpoint( 'authorize', 'https://www.strava.com/oauth/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://www.strava.com/oauth/token', 'POST' ); + $this->set_endpoint( 'refresh', 'https://www.strava.com/oauth/token', 'POST' ); + $this->set_endpoint( 'deauthorize', 'https://www.strava.com/oauth/deauthorize', 'POST' ); + $this->set_endpoint( 'user', 'https://www.strava.com/api/v3/athlete', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; $this->authorization_header = 'Bearer'; $this->authorization_parameter = false; @@ -36,6 +36,7 @@ function __construct() { } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'You\'ll need to create a new application on Strava so that you can connect.', 'keyring' ), 'https://www.strava.com/settings/api' ) . '

'; echo '

' . __( "Once you've registered your application, copy the Application Name into the App ID, the Client ID and the Client Secret into the API Key and API Secret fields below,.", 'keyring' ) . '

'; } @@ -87,10 +88,10 @@ function build_token_meta( $token ) { } else { $meta['name'] = $profile->username; } - $meta['first_name'] = $profile->firstname; - $meta['last_name'] = $profile->lastname; - $meta['picture'] = $profile->profile; - $meta['first_date'] = $profile->created_at; // Capture the athlete's profile creation date for later use, eg: in keyring-social-importers + $meta['first_name'] = $profile->firstname; + $meta['last_name'] = $profile->lastname; + $meta['picture'] = $profile->profile; + $meta['first_date'] = $profile->created_at; // Capture the athlete's profile creation date for later use, eg: in keyring-social-importers } return apply_filters( 'keyring_access_token_meta', $meta, $this->get_name(), $token, $profile, $this ); @@ -99,7 +100,8 @@ function build_token_meta( $token ) { } function get_display( Keyring_Access_Token $token ) { - return $token->get_meta( 'name' );; + return $token->get_meta( 'name' ); + } function maybe_refresh_token() { @@ -115,21 +117,24 @@ function maybe_refresh_token() { return; } - $response = wp_remote_post( $this->refresh_url, array( - 'method' => $this->refresh_method, - 'body' => array( - 'client_id' => $this->key, - 'client_secret' => $this->secret, - 'refresh_token' => $meta['refresh_token'], - 'grant_type' => 'refresh_token', - ), - ) ); + $response = wp_remote_post( + $this->refresh_url, + array( + 'method' => $this->refresh_method, + 'body' => array( + 'client_id' => $this->key, + 'client_secret' => $this->secret, + 'refresh_token' => $meta['refresh_token'], + 'grant_type' => 'refresh_token', + ), + ) + ); if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { return false; } - $return = json_decode( wp_remote_retrieve_body( $response ) ); + $return = json_decode( wp_remote_retrieve_body( $response ) ); $meta['expires'] = $return->expires_at; // Build access token @@ -142,7 +147,7 @@ function maybe_refresh_token() { // Store the updated access token $access_token = apply_filters( 'keyring_access_token', $access_token, (array) $return ); - $id = $this->store->update( $access_token ); + $id = $this->store->update( $access_token ); // And switch to using it $this->set_token( $access_token ); diff --git a/includes/services/extended/tripit.php b/includes/services/extended/tripit.php index 6c9d083bc54..f5fa5f5ee89 100644 --- a/includes/services/extended/tripit.php +++ b/includes/services/extended/tripit.php @@ -20,23 +20,24 @@ function __construct() { $this->authorization_header = true; $this->authorization_realm = false; - $this->set_endpoint( 'request_token', 'https://api.tripit.com/oauth/request_token', 'POST' ); - $this->set_endpoint( 'authorize', 'https://www.tripit.com/oauth/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://api.tripit.com/oauth/access_token', 'POST' ); - $this->set_endpoint( 'verify', 'https://api.tripit.com/v1/get/profile/id/me', 'GET' ); + $this->set_endpoint( 'request_token', 'https://api.tripit.com/oauth/request_token', 'POST' ); + $this->set_endpoint( 'authorize', 'https://www.tripit.com/oauth/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://api.tripit.com/oauth/access_token', 'POST' ); + $this->set_endpoint( 'verify', 'https://api.tripit.com/v1/get/profile/id/me', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); + $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; $this->requires_token( true ); } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'If you haven\'t created an app on TripIt yet, create one now. Make sure you select \'Full API Application\' and \'Web application or widget\'; other than that the settings are all up to you.', 'keyring' ), 'https://www.tripit.com/developer/create' ) . '

'; echo '

' . __( "Once you've created your app, you will see a yellow box at the top of the page, where you can get your API Key and API Secret, to enter below (you don't need an App ID value for TripIt).", 'keyring' ) . '

'; } @@ -62,10 +63,10 @@ function build_token_meta( $token ) { $meta = array(); } else { $meta = array( - 'user_id' => $response->Profile->{'@attributes'}->ref, - 'username' => $response->Profile->screen_name, - 'name' => $response->Profile->public_display_name, - 'picture' => $response->Profile->photo_url, + 'user_id' => $response->Profile->{'@attributes'}->ref, + 'username' => $response->Profile->screen_name, + 'name' => $response->Profile->public_display_name, + 'picture' => $response->Profile->photo_url, ); } diff --git a/includes/services/extended/tumblr.php b/includes/services/extended/tumblr.php index 53c7d09aaaf..585436550ad 100644 --- a/includes/services/extended/tumblr.php +++ b/includes/services/extended/tumblr.php @@ -18,23 +18,24 @@ function __construct() { } $this->set_endpoint( 'request_token', 'https://www.tumblr.com/oauth/request_token', 'POST' ); - $this->set_endpoint( 'authorize', 'https://www.tumblr.com/oauth/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://www.tumblr.com/oauth/access_token', 'POST' ); - $this->set_endpoint( 'self', 'https://api.tumblr.com/v2/user/info', 'GET' ); + $this->set_endpoint( 'authorize', 'https://www.tumblr.com/oauth/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://www.tumblr.com/oauth/access_token', 'POST' ); + $this->set_endpoint( 'self', 'https://api.tumblr.com/v2/user/info', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); + $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; $this->authorization_header = true; // Send OAuth token in the header, not querystring - $this->authorization_realm = 'tumblr.com'; + $this->authorization_realm = 'tumblr.com'; } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'To get started, register an application with Tumblr. The Default callback URL should be set to %2$s, and you can enter whatever you like in the other fields.', 'keyring' ), 'https://www.tumblr.com/oauth/register', Keyring_Util::admin_url( 'tumblr', array( 'action' => 'verify' ) ) ) . '

'; echo '

' . __( "Once you've created your app, copy the OAuth Consumer Key into the API Key field below. Click the Show secret key link, and then copy the Secret Key value into the API Secret field below. You don't need an App ID value for Tumblr.", 'keyring' ) . '

'; } @@ -61,7 +62,7 @@ function build_token_meta( $token ) { $meta = array(); } else { $this->person = $response->response->user; - $meta = array( + $meta = array( 'name' => $this->person->name, ); } @@ -75,9 +76,9 @@ function get_display( Keyring_Access_Token $token ) { function test_connection() { $res = $this->request( $this->self_url, array( 'method' => $this->self_method ) ); - if ( ! Keyring_Util::is_error( $res ) ) { - return true; - } + if ( ! Keyring_Util::is_error( $res ) ) { + return true; + } return $res; } @@ -89,7 +90,7 @@ function fetch_profile_picture() { } foreach ( $res->response->user->blogs as $blog ) { - if ( !$blog->primary ) { + if ( ! $blog->primary ) { continue; } return $this->fetch_profile_picture_for_blog( $blog ); @@ -105,19 +106,20 @@ function fetch_primary_blog() { } foreach ( $res->response->user->blogs as $blog ) { - if ( ! $blog->primary ) + if ( ! $blog->primary ) { continue; + } $blog_basename = parse_url( $blog->url, PHP_URL_HOST ); $primary_tumblr_blog = array( - 'id' => $blog_basename, - 'name' => $blog->title, + 'id' => $blog_basename, + 'name' => $blog->title, 'category' => $blog->type, - 'url' => $blog->url, - 'picture' => $this->fetch_profile_picture_for_blog( $blog ), + 'url' => $blog->url, + 'picture' => $this->fetch_profile_picture_for_blog( $blog ), ); - + return (object) $primary_tumblr_blog; } @@ -133,17 +135,18 @@ function fetch_additional_external_users() { $additional_external_users = array(); foreach ( $res->response->user->blogs as $blog ) { - if ( $blog->primary ) + if ( $blog->primary ) { continue; + } $blog_basename = parse_url( $blog->url, PHP_URL_HOST ); $this_tumblr_blog = array( - 'id' => $blog_basename, - 'name' => $blog->title, + 'id' => $blog_basename, + 'name' => $blog->title, 'category' => $blog->type, - 'url' => $blog->url, - 'picture' => $this->fetch_profile_picture_for_blog( $blog ), + 'url' => $blog->url, + 'picture' => $this->fetch_profile_picture_for_blog( $blog ), ); $additional_external_users[] = (object) $this_tumblr_blog; diff --git a/includes/services/extended/twitter.php b/includes/services/extended/twitter.php index 9e311366286..6bff878a89c 100644 --- a/includes/services/extended/twitter.php +++ b/includes/services/extended/twitter.php @@ -18,26 +18,27 @@ function __construct() { } $this->authorization_header = true; - $this->authorization_realm = "twitter.com"; + $this->authorization_realm = 'twitter.com'; $this->set_endpoint( 'request_token', 'https://twitter.com/oauth/request_token', 'POST' ); - $this->set_endpoint( 'authorize', 'https://twitter.com/oauth/authorize', 'GET' ); - $this->set_endpoint( 'access_token', 'https://twitter.com/oauth/access_token', 'POST' ); - $this->set_endpoint( 'verify', 'https://api.twitter.com/1.1/account/verify_credentials.json', 'GET' ); + $this->set_endpoint( 'authorize', 'https://twitter.com/oauth/authorize', 'GET' ); + $this->set_endpoint( 'access_token', 'https://twitter.com/oauth/access_token', 'POST' ); + $this->set_endpoint( 'verify', 'https://api.twitter.com/1.1/account/verify_credentials.json', 'GET' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); + $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; $this->requires_token( true ); } function basic_ui_intro() { - echo '

' . sprintf( __( 'If you haven\'t already, you\'ll need to create an app on Twitter (log in using your normal Twitter account). The Callback URL is %2$s.', 'keyring' ), 'https://apps.twitter.com/app/new', self_admin_url( 'tools.php' ) ) . '

'; + /* translators: url */ + echo '

' . sprintf( __( 'If you haven\'t already, you\'ll need to create an app on Twitter (log in using your normal Twitter account). The Callback URL is %2$s.', 'keyring' ), 'https://apps.twitter.com/app/new', self_admin_url( 'tools.php' ) ) . '

'; echo '

' . __( "Once you've created an app, copy and paste your Consumer key and Consumer secret (from under the OAuth settings section of your app's details) into the boxes below. You don't need an App ID for Twitter.", 'keyring' ) . '

'; } @@ -62,10 +63,10 @@ function build_token_meta( $token ) { $meta = array(); } else { $meta = array( - 'user_id' => $token['user_id'], - 'username' => $token['screen_name'], - 'name' => $response->name, - 'picture' => str_replace( '_normal.', '.', $response->profile_image_url ), + 'user_id' => $token['user_id'], + 'username' => $token['screen_name'], + 'name' => $response->name, + 'picture' => str_replace( '_normal.', '.', $response->profile_image_url ), ); } @@ -77,21 +78,21 @@ function get_display( Keyring_Access_Token $token ) { } function test_connection() { - $res = $this->request( 'https://api.twitter.com/1.1/account/verify_credentials.json' ); - if ( ! Keyring_Util::is_error( $res ) ) { - return true; - } + $res = $this->request( 'https://api.twitter.com/1.1/account/verify_credentials.json' ); + if ( ! Keyring_Util::is_error( $res ) ) { + return true; + } - // Twitter may return a rate limiting error if the user accesses the sharing settings or post - // page frequently. If so, ignore that error, things are likely aaaa-okay... - $keyring_error_message = $res->get_error_message(); - if ( is_array( $keyring_error_message ) && isset( $keyring_error_message['response']['code'] ) ) { - if ( 429 == absint( $keyring_error_message['response']['code'] ) ) { - return true; - } + // Twitter may return a rate limiting error if the user accesses the sharing settings or post + // page frequently. If so, ignore that error, things are likely aaaa-okay... + $keyring_error_message = $res->get_error_message(); + if ( is_array( $keyring_error_message ) && isset( $keyring_error_message['response']['code'] ) ) { + if ( 429 === absint( $keyring_error_message['response']['code'] ) ) { + return true; } + } - return $res; + return $res; } } diff --git a/includes/services/extended/yahoo.php b/includes/services/extended/yahoo.php index 5e4d53556f5..a7291150ba1 100644 --- a/includes/services/extended/yahoo.php +++ b/includes/services/extended/yahoo.php @@ -17,20 +17,21 @@ function __construct() { add_filter( 'keyring_yahoo_basic_ui_intro', array( $this, 'basic_ui_intro' ) ); } - $this->set_endpoint( 'request_token', 'https://api.login.yahoo.com/oauth/v2/get_request_token', 'GET' ); - $this->set_endpoint( 'authorize', 'https://api.login.yahoo.com/oauth/v2/request_auth', 'GET' ); - $this->set_endpoint( 'access_token', 'https://api.login.yahoo.com/oauth/v2/get_token', 'POST' ); + $this->set_endpoint( 'request_token', 'https://api.login.yahoo.com/oauth/v2/get_request_token', 'GET' ); + $this->set_endpoint( 'authorize', 'https://api.login.yahoo.com/oauth/v2/request_auth', 'GET' ); + $this->set_endpoint( 'access_token', 'https://api.login.yahoo.com/oauth/v2/get_token', 'POST' ); - $creds = $this->get_credentials(); - $this->app_id = $creds['app_id']; - $this->key = $creds['key']; - $this->secret = $creds['secret']; + $creds = $this->get_credentials(); + $this->app_id = $creds['app_id']; + $this->key = $creds['key']; + $this->secret = $creds['secret']; - $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); + $this->consumer = new OAuthConsumer( $this->key, $this->secret, $this->callback_url ); $this->signature_method = new OAuthSignatureMethod_HMAC_SHA1; } function basic_ui_intro() { + /* translators: url */ echo '

' . sprintf( __( 'To connect to Yahoo!, you need to Create a new project. Make sure you set the Access Scope to This app requires access to private user data. When you select that, you will be asked for an Application Domain, which should probably be set to http://%2$s. Which APIs you request access for will depend on how Keyring will be used on this site. Common ones will be Contacts, Social Directory, Status, and Updates.', 'keyring' ), 'https://developer.apps.yahoo.com/dashboard/createKey.html', $_SERVER['HTTP_HOST'] ) . '

'; echo '

' . __( "Once you've created your project, copy and paste your Consumer key and Consumer secret (from under the Authentication Information: OAuth section of your app's details) into the boxes below. You don't need an App ID for Yahoo!.", 'keyring' ) . '

'; } @@ -59,7 +60,7 @@ function build_token_meta( $token ) { $meta = array(); } else { $this->person = $response->profile; - $meta = array( + $meta = array( 'user_id' => $token['xoauth_yahoo_guid'], 'name' => $this->person->nickname, 'picture' => $this->person->image->imageUrl, @@ -69,7 +70,7 @@ function build_token_meta( $token ) { return apply_filters( 'keyring_access_token_meta', $meta, $this->get_name(), $token, $response, $this ); } - function get_display( Keyring_Access_Token$token ) { + function get_display( Keyring_Access_Token $token ) { return $token->get_meta( 'name' ); } @@ -97,10 +98,13 @@ function maybe_refresh_token() { $api_url = 'https://api.login.yahoo.com/oauth/v2/get_token'; $api_url .= '?oauth_session_handle=' . $this->token->token->sessionHandle; - $refresh = $this->request( $api_url, array( - 'method' => 'GET', - 'raw_response' => true, - ) ); + $refresh = $this->request( + $api_url, + array( + 'method' => 'GET', + 'raw_response' => true, + ) + ); if ( ! Keyring_Util::is_error( $refresh ) ) { $token = $this->parse_access_token( $refresh ); @@ -125,7 +129,7 @@ function maybe_refresh_token() { // Store the updated access token $access_token = apply_filters( 'keyring_access_token', $access_token, $token ); - $id = $this->store->update( $access_token ); + $id = $this->store->update( $access_token ); // And switch to using it $this->set_token( $access_token ); diff --git a/includes/stores/singlestore.php b/includes/stores/singlestore.php index 63d718a6082..a458ec84e88 100644 --- a/includes/stores/singlestore.php +++ b/includes/stores/singlestore.php @@ -13,18 +13,24 @@ class Keyring_SingleStore extends Keyring_Store { static function &init() { static $instance = false; - if ( !$instance ) { - register_post_type( 'kr_request_token', array( - 'label' => __( 'Keyring Request Token', 'keyring' ), - 'description' => __( 'Token or authentication details stored by Keyring. Request tokens are used during the authorization flow.', 'keyring' ), - 'public' => false, - ) ); - - register_post_type( 'kr_access_token', array( - 'label' => __( 'Keyring Access Token', 'keyring' ), - 'description' => __( 'Token or authentication details stored by Keyring. Access tokens are used to make secure requests.', 'keyring' ), - 'public' => false, - ) ); + if ( ! $instance ) { + register_post_type( + 'kr_request_token', + array( + 'label' => __( 'Keyring Request Token', 'keyring' ), + 'description' => __( 'Token or authentication details stored by Keyring. Request tokens are used during the authorization flow.', 'keyring' ), + 'public' => false, + ) + ); + + register_post_type( + 'kr_access_token', + array( + 'label' => __( 'Keyring Access Token', 'keyring' ), + 'description' => __( 'Token or authentication details stored by Keyring. Access tokens are used to make secure requests.', 'keyring' ), + 'public' => false, + ) + ); $instance = new Keyring_SingleStore; } @@ -34,16 +40,18 @@ static function &init() { function insert( $token ) { // Avoid duplicates by checking to see if this exists already - $found = get_posts( array( - 'posts_per_page' => 1, - 'post_type' => 'kr_' . $token->type() . '_token', - 'meta_key' => 'service', - 'meta_value' => $token->get_name(), - 'author' => get_current_user_id(), - 's' => serialize( $token->token ), // Search the post content for this token - 'exact' => true, // Require exact content match - 'sentence' => true, // Require to search by phrase, otherwise string is split by regex - ) ); + $found = get_posts( + array( + 'posts_per_page' => 1, + 'post_type' => 'kr_' . $token->type() . '_token', + 'meta_key' => 'service', + 'meta_value' => $token->get_name(), + 'author' => get_current_user_id(), + 's' => serialize( $token->token ), // Search the post content for this token + 'exact' => true, // Require exact content match + 'sentence' => true, // Require to search by phrase, otherwise string is split by regex + ) + ); if ( $found ) { $token->unique_id = $found[0]->ID; @@ -55,7 +63,7 @@ function insert( $token ) { 'post_status' => 'publish', 'post_content' => serialize( $token->token ), ); - $id = wp_insert_post( add_magic_quotes( $post ) ); + $id = wp_insert_post( add_magic_quotes( $post ) ); if ( $id ) { // Always record what service this token is for update_post_meta( $id, 'service', $token->get_name() ); @@ -70,13 +78,15 @@ function insert( $token ) { } function update( $token ) { - if ( !$token->unique_id ) + if ( ! $token->unique_id ) { return false; + } - $id = $token->unique_id; + $id = $token->unique_id; $post = get_post( $id ); - if ( !$post ) + if ( ! $post ) { return false; + } $post->post_content = serialize( $token->token ); wp_update_post( $post ); @@ -89,8 +99,9 @@ function update( $token ) { } function delete( $args = array() ) { - if ( !$args['id'] ) + if ( ! $args['id'] ) { return false; + } return wp_delete_post( $args['id'] ); } @@ -101,7 +112,7 @@ function get_tokens( $args = array() ) { 'user_id' => get_current_user_id(), 'blog_id' => get_current_blog_id(), ); - $args = wp_parse_args( $args, $defaults ); + $args = wp_parse_args( $args, $defaults ); $query = array( 'numberposts' => -1, // all @@ -115,14 +126,14 @@ function get_tokens( $args = array() ) { $query['meta_value'] = $args['service']; } - $token_type = 'request' == $args['type'] ? 'Keyring_Request_Token' : 'Keyring_Access_Token'; - $tokens = array(); - $posts = get_posts( $query ); + $token_type = 'request' === $args['type'] ? 'Keyring_Request_Token' : 'Keyring_Access_Token'; + $tokens = array(); + $posts = get_posts( $query ); if ( count( $posts ) ) { foreach ( $posts as $post ) { $meta = get_post_meta( $post->ID ); foreach ( $meta as $mid => $met ) { - $meta[$mid] = $met[0]; + $meta[ $mid ] = $met[0]; } $tokens[] = new $token_type( @@ -149,19 +160,20 @@ function get_token( $args = array() ) { 'user_id' => get_current_user_id(), 'blog_id' => get_current_blog_id(), ); - $args = wp_parse_args( $args, $defaults ); + $args = wp_parse_args( $args, $defaults ); - if ( !$args['id'] && !$args['service'] ) + if ( ! $args['id'] && ! $args['service'] ) { return false; + } $post = get_post( $args['id'] ); if ( $post ) { $meta = get_post_meta( $post->ID ); foreach ( $meta as $mid => $met ) { - $meta[$mid] = $met[0]; + $meta[ $mid ] = $met[0]; } - $token_type = 'kr_request_token' == $post->post_type ? 'Keyring_Request_Token' : 'Keyring_Access_Token'; + $token_type = 'kr_request_token' === $post->post_type ? 'Keyring_Request_Token' : 'Keyring_Access_Token'; return new $token_type( get_post_meta( $post->ID, diff --git a/keyring.php b/keyring.php index dfeb117cf3f..eadc0d6ea56 100644 --- a/keyring.php +++ b/keyring.php @@ -22,8 +22,8 @@ // Debug/messaging levels. Don't mess with these define( 'KEYRING__DEBUG_NOTICE', 1 ); -define( 'KEYRING__DEBUG_WARN', 2 ); -define( 'KEYRING__DEBUG_ERROR', 3 ); +define( 'KEYRING__DEBUG_WARN', 2 ); +define( 'KEYRING__DEBUG_ERROR', 3 ); // Indicates Keyring is installed/active so that other plugins can detect it define( 'KEYRING__VERSION', '2.0' ); @@ -47,10 +47,15 @@ function __construct() { require_once dirname( __FILE__ ) . '/admin-ui.php'; Keyring_Admin_UI::init(); - add_filter( 'keyring_admin_url', function( $url, $params ) { - $url = admin_url( 'tools.php?page=' . Keyring::init()->admin_page ); - return add_query_arg( $params, $url ); - }, 10, 2 ); + add_filter( + 'keyring_admin_url', + function( $url, $params ) { + $url = admin_url( 'tools.php?page=' . Keyring::init()->admin_page ); + return add_query_arg( $params, $url ); + }, + 10, + 2 + ); } // This is used internally to create URLs, and also to know when to @@ -61,9 +66,10 @@ function __construct() { static function &init( $force_load = false ) { static $instance = false; - if ( !$instance ) { - if ( ! KEYRING__HEADLESS_MODE ) + if ( ! $instance ) { + if ( ! KEYRING__HEADLESS_MODE ) { load_plugin_textdomain( 'keyring', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); + } $instance = new Keyring; // Keyring is being loaded 'late', so we need to do some extra set-up @@ -85,10 +91,12 @@ static function plugins_loaded() { // Load stores early so we can confirm they're loaded correctly require_once dirname( __FILE__ ) . '/store.php'; do_action( 'keyring_load_token_stores' ); - $keyring = Keyring::init(); + $keyring = Keyring::init(); $keyring->token_store = apply_filters( 'keyring_token_store', defined( 'KEYRING__TOKEN_STORE' ) ? KEYRING__TOKEN_STORE : false ); - if ( !class_exists( $keyring->token_store ) || !in_array( 'Keyring_Store', class_parents( $keyring->token_store ) ) ) + if ( ! class_exists( $keyring->token_store ) || ! in_array( 'Keyring_Store', class_parents( $keyring->token_store ), true ) ) { + /* translators: file name */ wp_die( sprintf( __( 'Invalid KEYRING__TOKEN_STORE specified. Please make sure KEYRING__TOKEN_STORE is set to a valid classname for handling token storage in %s (or wp-config.php)', 'keyring' ), __FILE__ ) ); + } // Load base token and service definitions + core services require_once dirname( __FILE__ ) . '/token.php'; @@ -98,9 +106,13 @@ static function plugins_loaded() { add_action( 'init', array( 'Keyring', 'init' ), 1 ); // Load external Services (plugins etc should hook to this to define new ones/extensions) - add_action( 'init', function() { - do_action( 'keyring_load_services' ); - }, 2 ); + add_action( + 'init', + function() { + do_action( 'keyring_load_services' ); + }, + 2 + ); /** * And trigger request handlers, which plugins and extended Services use to handle UI, @@ -118,26 +130,26 @@ static function plugins_loaded() { static function request_handlers() { global $current_user; - if ( defined( 'KEYRING__FORCE_USER' ) && KEYRING__FORCE_USER && in_array( $_REQUEST['action'], array( 'request', 'verify' ) ) ) { + if ( defined( 'KEYRING__FORCE_USER' ) && KEYRING__FORCE_USER && in_array( $_REQUEST['action'], array( 'request', 'verify' ), true ) ) { global $current_user; $real_user = $current_user->ID; wp_set_current_user( KEYRING__FORCE_USER ); } if ( - !empty( $_REQUEST['action'] ) + ! empty( $_REQUEST['action'] ) && - in_array( $_REQUEST['action'], apply_filters( 'keyring_core_actions', array( 'request', 'verify', 'created', 'delete', 'manage' ) ) ) + in_array( $_REQUEST['action'], apply_filters( 'keyring_core_actions', array( 'request', 'verify', 'created', 'delete', 'manage' ) ), true ) && - !empty( $_REQUEST['service'] ) + ! empty( $_REQUEST['service'] ) && - in_array( $_REQUEST['service'], array_keys( Keyring::get_registered_services() ) ) + in_array( $_REQUEST['service'], array_keys( Keyring::get_registered_services() ), true ) ) { // We have an action here to allow us to do things pre-authorization, just in case do_action( "pre_keyring_{$_REQUEST['service']}_{$_REQUEST['action']}", $_REQUEST ); // Core nonce check required for everything. "keyring-ACTION" is the kr_nonce format - if ( !isset( $_REQUEST['kr_nonce'] ) || !wp_verify_nonce( $_REQUEST['kr_nonce'], 'keyring-' . $_REQUEST['action'] ) ) { + if ( ! isset( $_REQUEST['kr_nonce'] ) || ! wp_verify_nonce( $_REQUEST['kr_nonce'], 'keyring-' . $_REQUEST['action'] ) ) { Keyring::error( __( 'Invalid/missing Keyring core nonce. All core actions require a valid nonce.', 'keyring' ) ); exit; } @@ -146,12 +158,14 @@ static function request_handlers() { Keyring_Util::debug( $_GET ); do_action( "keyring_{$_REQUEST['service']}_{$_REQUEST['action']}", $_REQUEST ); - if ( 'delete' == $_REQUEST['action'] ) - do_action( "keyring_connection_deleted", $_REQUEST['service'], $_REQUEST ); + if ( 'delete' === $_REQUEST['action'] ) { + do_action( 'keyring_connection_deleted', $_REQUEST['service'], $_REQUEST ); + } } - if ( defined( 'KEYRING__FORCE_USER' ) && KEYRING__FORCE_USER && in_array( $_REQUEST['action'], array( 'request', 'verify' ) ) ) + if ( defined( 'KEYRING__FORCE_USER' ) && KEYRING__FORCE_USER && in_array( $_REQUEST['action'], array( 'request', 'verify' ), true ) ) { wp_set_current_user( $real_user ); + } } static function register_service( Keyring_Service $service ) { @@ -168,8 +182,9 @@ static function get_registered_services() { static function get_service_by_name( $name ) { $keyring = Keyring::init(); - if ( !isset( $keyring->registered_services[ $name ] ) ) + if ( ! isset( $keyring->registered_services[ $name ] ) ) { return null; + } return $keyring->registered_services[ $name ]; } @@ -177,25 +192,26 @@ static function get_service_by_name( $name ) { static function get_token_store() { $keyring = Keyring::init(); - if ( !$keyring->store ) + if ( ! $keyring->store ) { $keyring->store = call_user_func( array( $keyring->token_store, 'init' ) ); + } return $keyring->store; } static function message( $str ) { - $keyring = Keyring::init(); + $keyring = Keyring::init(); $keyring->messages[] = $str; } /** * Generic error handler/trigger. - * @param String $str Informational message (user-readable) + * @param String $str Informational message (user-readable) * @param array $info Additional information relating to the error. * @param boolean $die If we should immediately die (default) or continue */ static function error( $str, $info = array(), $die = true ) { - $keyring = Keyring::init(); + $keyring = Keyring::init(); $keyring->errors[] = $str; do_action( 'keyring_error', $str, $info ); if ( $die ) { @@ -223,27 +239,30 @@ function get_errors() { class Keyring_Util { static function debug( $str, $level = KEYRING__DEBUG_NOTICE ) { - if ( !KEYRING__DEBUG_MODE ) + if ( ! KEYRING__DEBUG_MODE ) { return; + } - if ( is_object( $str ) || is_array( $str ) ) + if ( is_object( $str ) || is_array( $str ) ) { $str = print_r( $str, true ); + } switch ( $level ) { - case KEYRING__DEBUG_WARN : - echo "
Keyring Warning: $str
"; - break; - case KEYRING__DEBUG_ERROR : - wp_die( '

Keyring Error:

' . '

' . $str . '

' ); - exit; + case KEYRING__DEBUG_WARN: + echo "
Keyring Warning: $str
"; + break; + case KEYRING__DEBUG_ERROR: + wp_die( '

Keyring Error:

' . '

' . $str . '

' ); + exit; } error_log( "Keyring: $str" ); } static function is_service( $service ) { - if ( is_object( $service ) && is_subclass_of( $service, 'Keyring_Service' ) ) + if ( is_object( $service ) && is_subclass_of( $service, 'Keyring_Service' ) ) { return true; + } return false; } @@ -275,7 +294,7 @@ static function admin_url( $service = false, $params = array() ) { static function connect_to( $service, $for ) { Keyring_Util::debug( 'Connect to: ' . $service ); // Redirect into Keyring's auth handler if a valid service is provided - $kr_nonce = wp_create_nonce( 'keyring-request' ); + $kr_nonce = wp_create_nonce( 'keyring-request' ); $request_nonce = wp_create_nonce( 'keyring-request-' . $service ); wp_safe_redirect( Keyring_Util::admin_url( @@ -284,7 +303,7 @@ static function connect_to( $service, $for ) { 'action' => 'request', 'kr_nonce' => $kr_nonce, 'nonce' => $request_nonce, - 'for' => $for + 'for' => $for, ) ) ); @@ -299,7 +318,8 @@ static function token_select_box( $tokens, $name, $create = false ) { - + + + Apply WordPress Coding Standards to all files + + + + + + + + + + + + + + + + + + + + . + + + + + + + warning + + + warning + + + warning + + + warning + + + warning + + + + + + + + + + + + + + + + + + + + + + + + /vendor/* + /includes/oauth-php/* + + + + * + + + + + * + + diff --git a/service.php b/service.php index 5145efc593b..0db834a6007 100644 --- a/service.php +++ b/service.php @@ -8,8 +8,8 @@ * @package Keyring */ abstract class Keyring_Service { - const NAME = ''; - const LABEL = ''; + const NAME = ''; + const LABEL = ''; protected $token = false; protected $requires_token = true; protected $store = false; @@ -72,10 +72,10 @@ function __construct() { static function &init() { static $instance = false; - if ( !$instance ) { - $class = get_called_class(); + if ( ! $instance ) { + $class = get_called_class(); $services = Keyring::get_registered_services(); - if ( in_array( $class::NAME, array_keys( $services ) ) ) { + if ( in_array( $class::NAME, array_keys( $services ), true ) ) { $instance = $services[ $class::NAME ]; } else { $instance = new $class; @@ -95,29 +95,32 @@ static function &init() { * then set requirement to true/false as specified. */ function requires_token( $does_it = null ) { - if ( is_null( $does_it ) ) + if ( is_null( $does_it ) ) { return $this->requires_token; + } - $requires = $this->requires_token; + $requires = $this->requires_token; $this->requires_token = $does_it; return $requires; } function get_name() { $c = get_called_class(); - if ( '' != $c::NAME ) + if ( '' !== $c::NAME ) { $name = $c::NAME; - else + } else { $name = strtolower( $c ); + } return $name; } function get_label() { $c = get_called_class(); - if ( '' != $c::LABEL ) + if ( '' !== $c::LABEL ) { $label = $c::LABEL; - else + } else { $label = $this->get_name(); + } return $label; } @@ -136,7 +139,7 @@ function set_request_response_code( $code ) { } function basic_ui() { - if ( !isset( $_REQUEST['nonce'] ) || !wp_verify_nonce( $_REQUEST['nonce'], 'keyring-manage-' . $this->get_name() ) ) { + if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'keyring-manage-' . $this->get_name() ) ) { Keyring::error( __( 'Invalid/missing management nonce.', 'keyring' ) ); exit; } @@ -145,21 +148,31 @@ function basic_ui() { echo '
'; echo '

' . __( 'Keyring Service Management', 'keyring' ) . '

'; echo '

' . __( '← Back', 'keyring' ) . '

'; + /* translators: service name */ echo '

' . sprintf( __( '%s API Credentials', 'keyring' ), esc_html( $this->get_label() ) ) . '

'; // Handle actually saving credentials if ( isset( $_POST['api_key'] ) && isset( $_POST['api_secret'] ) ) { // Store credentials against this service - $this->update_credentials( apply_filters( 'keyring_' . $this->get_name() . '_basic_ui_save_credentials', array( - 'app_id' => ( ! empty( $_POST['app_id'] ) ? stripslashes( trim( $_POST['app_id'] ) ) : '' ), - 'key' => ( ! empty( $_POST['api_key'] ) ? stripslashes( trim( $_POST['api_key'] ) ) : '' ), - 'secret' => ( ! empty( $_POST['api_secret'] ) ? stripslashes( trim( $_POST['api_secret'] ) ) : '' ) - ) ) ); + $this->update_credentials( + apply_filters( + 'keyring_' . $this->get_name() . '_basic_ui_save_credentials', + array( + 'app_id' => ( ! empty( $_POST['app_id'] ) ? stripslashes( trim( $_POST['app_id'] ) ) : '' ), + 'key' => ( ! empty( $_POST['api_key'] ) ? stripslashes( trim( $_POST['api_key'] ) ) : '' ), + 'secret' => ( ! empty( $_POST['api_secret'] ) ? stripslashes( trim( $_POST['api_secret'] ) ) : '' ), + ) + ) + ); echo '

' . __( 'Credentials saved.', 'keyring' ) . '

'; } - $app_id = $api_key = $api_secret = ''; - if ( $creds = $this->get_credentials() ) { + $app_id = ''; + $api_key = ''; + $api_secret = ''; + + $creds = $this->get_credentials(); + if ( $creds ) { $app_id = $creds['app_id']; $api_key = $creds['key']; $api_secret = $creds['secret']; @@ -175,17 +188,17 @@ function basic_ui() { wp_nonce_field( 'keyring-manage-' . $this->get_name(), 'nonce', false ); echo ''; - $ui_app_id = ''; + $ui_app_id = ''; $ui_app_id .= ''; echo apply_filters( 'keyring_' . $this->get_name() . '_basic_ui_app_id', $ui_app_id ); - $ui_api_key = ''; + $ui_api_key = ''; $ui_api_key .= ''; echo apply_filters( 'keyring_' . $this->get_name() . '_basic_ui_api_key', $ui_api_key ); - $ui_api_secret = ''; + $ui_api_secret = ''; $ui_api_secret .= ''; echo apply_filters( 'keyring_' . $this->get_name() . '_basic_ui_api_secret', $ui_api_secret ); @@ -200,7 +213,8 @@ function basic_ui() { jQuery( document ).ready( function() { jQuery( '#app_id' ).focus(); } ); - + '; } @@ -216,8 +230,9 @@ function get_credentials() { if ( method_exists( $this, '_get_credentials' ) ) { $creds = $this->_get_credentials(); - if ( !is_null( $creds ) ) + if ( ! is_null( $creds ) ) { return apply_filters( 'keyring_service_credentials', $creds, $this->get_name() ); + } } // Then check for generic constants @@ -241,7 +256,7 @@ function get_credentials() { // Last check in the database for a shared store of credentials $creds = false; $all = apply_filters( 'keyring_credentials', get_option( 'keyring_credentials' ) ); - if ( !empty( $all[ $this->get_name() ] ) ) { + if ( ! empty( $all[ $this->get_name() ] ) ) { $creds = $all[ $this->get_name() ]; } @@ -255,7 +270,7 @@ function get_credentials() { * @param array $credentials */ function update_credentials( array $credentials ) { - $all = apply_filters( 'keyring_credentials', get_option( 'keyring_credentials' ) ); + $all = apply_filters( 'keyring_credentials', get_option( 'keyring_credentials' ) ); $all[ $this->get_name() ] = $credentials; return update_option( 'keyring_credentials', $all ); } @@ -279,7 +294,18 @@ function verified( $id, $request_token = null ) { // Back to Keyring admin, with ?service=SERVICE&created=UNIQUE_ID&kr_nonce=NONCE $kr_nonce = wp_create_nonce( 'keyring-created' ); - $url = apply_filters( 'keyring_verified_redirect', Keyring_Util::admin_url( $c::NAME, array( 'action' => 'created', 'id' => $id, 'kr_nonce' => $kr_nonce ) ), $c::NAME ); + $url = apply_filters( + 'keyring_verified_redirect', + Keyring_Util::admin_url( + $c::NAME, + array( + 'action' => 'created', + 'id' => $id, + 'kr_nonce' => $kr_nonce, + ) + ), + $c::NAME + ); Keyring_Util::debug( 'Verified connection, redirect to ' . $url ); wp_safe_redirect( $url ); exit; @@ -292,7 +318,7 @@ function is_connected() { function store_token( $token ) { $token->meta['_classname'] = get_called_class(); - $id = $this->store->insert( $token ); + $id = $this->store->insert( $token ); return $id; } @@ -310,7 +336,12 @@ function get_token() { function get_tokens( $id = false ) { $c = get_called_class(); - return $this->store->get_tokens( array( 'service' => $c::NAME, 'type' => 'access' ) ); + return $this->store->get_tokens( + array( + 'service' => $c::NAME, + 'type' => 'access', + ) + ); } function token_select_box( $name, $create = false ) { @@ -321,9 +352,10 @@ function token_select_box( $name, $create = false ) { // Load all packaged services in the ./includes/services/ directory by including all PHP files, first in core, then in extended // Remove a Service (prevent it from loading at all) by filtering on 'keyring_services' -$keyring_services = glob( dirname( __FILE__ ) . "/includes/services/core/*.php" ); -$keyring_services = array_merge( $keyring_services, glob( dirname( __FILE__ ) . "/includes/services/extended/*.php" ) ); +$keyring_services = glob( dirname( __FILE__ ) . '/includes/services/core/*.php' ); +$keyring_services = array_merge( $keyring_services, glob( dirname( __FILE__ ) . '/includes/services/extended/*.php' ) ); $keyring_services = apply_filters( 'keyring_services', $keyring_services ); -foreach ( $keyring_services as $keyring_service ) +foreach ( $keyring_services as $keyring_service ) { require_once $keyring_service; +} unset( $keyring_services, $keyring_service ); diff --git a/store.php b/store.php index ff88e006b6e..3603473ac6d 100644 --- a/store.php +++ b/store.php @@ -49,8 +49,9 @@ abstract function count( $args = array() ); // Load all packaged token store engines in the ./includes/stores/ directory by including all PHP files // Remove a Token Store (prevent it from loading at all) by filtering on 'keyring_token_stores' -$keyring_stores = glob( dirname( __FILE__ ) . "/includes/stores/*.php" ); +$keyring_stores = glob( dirname( __FILE__ ) . '/includes/stores/*.php' ); $keyring_stores = apply_filters( 'keyring_token_stores', $keyring_stores ); -foreach ( $keyring_stores as $keyring_store ) +foreach ( $keyring_stores as $keyring_store ) { require $keyring_store; +} unset( $keyring_stores, $keyring_store ); diff --git a/token.php b/token.php index bf79e3cf49d..29761bc5dc6 100644 --- a/token.php +++ b/token.php @@ -37,19 +37,22 @@ function __toString() { } function get_uniq_id() { - if ( isset( $this->unique_id ) ) + if ( isset( $this->unique_id ) ) { return $this->unique_id; + } return null; } function get_display() { - if ( $service = $this->get_service() ) + $service = $this->get_service(); + if ( $service ) { return $service->get_display( $this ); + } return $this->name; } function get_service() { - if ( !$this->service ) { + if ( ! $this->service ) { $class = $this->get_meta( '_classname', true ); if ( $class && class_exists( $class ) ) { $this->service = call_user_func( array( $class, 'init' ) ); @@ -74,14 +77,14 @@ function get_name() { function get_meta( $name = false, $allow_hidden = false ) { $return = null; if ( $name ) { - if ( '_' != substr( $name, 0, 1 ) || $allow_hidden ) { + if ( '_' !== substr( $name, 0, 1 ) || $allow_hidden ) { if ( isset( $this->meta[ $name ] ) ) { $return = $this->meta[ $name ]; } } } else { foreach ( (array) $this->meta as $key => $val ) { - if ( '_' != substr( $key, 0, 1 ) || $allow_hidden ) { + if ( '_' !== substr( $key, 0, 1 ) || $allow_hidden ) { $return[ $key ] = $val; } } @@ -91,13 +94,15 @@ function get_meta( $name = false, $allow_hidden = false ) { } /** - * Check if a token has expired, or will expire in the next $window seconds - **/ + * Check if a token has expired, or will expire in the next $window seconds + */ function is_expired( $window = 0 ) { - if ( ! $expires = $this->get_meta( 'expires' ) ) { + $expires = $this->get_meta( 'expires' ); + if ( ! $expires ) { return false; // No expires value, assume it's a permanent token } + // phpcs:ignore WordPress.PHP.StrictComparisons if ( '0000-00-00 00:00:00' == $expires ) { return false; // Doesn't expire }
' . __( 'App ID', 'keyring' ) . '
' . __( 'App ID', 'keyring' ) . '
' . __( 'API Key', 'keyring' ) . '
' . __( 'API Key', 'keyring' ) . '
' . __( 'API Secret', 'keyring' ) . '
' . __( 'API Secret', 'keyring' ) . '