Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove deprecated from WP_Auth0_InitialSetup #754

Merged
merged 4 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

define( 'WPA0_LANG', 'wp-auth0' ); // deprecated; do not use for translations

require_once WPA0_PLUGIN_DIR.'vendor/autoload.php';
require_once WPA0_PLUGIN_DIR.'functions.php';
require_once WPA0_PLUGIN_DIR . 'vendor/autoload.php';
require_once WPA0_PLUGIN_DIR . 'functions.php';

/*
* Localization
Expand Down Expand Up @@ -123,9 +123,6 @@ public function init() {

add_filter( 'plugin_action_links_' . $this->basename, [ $this, 'wp_add_plugin_settings_link' ] );

$initial_setup = new WP_Auth0_InitialSetup( $this->a0_options );
$initial_setup->init();

$this->router = new WP_Auth0_Routes( $this->a0_options );
}

Expand Down Expand Up @@ -477,6 +474,55 @@ function wp_auth0_db_check_update() {
* Core WP hooks
*/

function wp_auth0_setup_error_admin_notices() {
if ( empty( $_REQUEST['error'] ) ) {
return false;
}

$initial_setup = new WP_Auth0_InitialSetup( WP_Auth0_Options::Instance() );

switch ( $_REQUEST['error'] ) {

case 'cant_create_client':
$initial_setup->cant_create_client_message();
break;

case 'cant_create_client_grant':
$initial_setup->cant_create_client_grant_message();
break;

case 'cant_exchange_token':
$initial_setup->cant_exchange_token_message();
break;

case 'rejected':
$initial_setup->rejected_message();
break;

case 'access_denied':
$initial_setup->access_denied_message();
break;

default:
$initial_setup->notify_error();
}

return true;
}
add_action( 'admin_notices', 'wp_auth0_setup_error_admin_notices' );

function wp_auth0_setup_callback_step1() {
$setup_conn = new WP_Auth0_InitialSetup_ConnectionProfile( WP_Auth0_Options::Instance() );
$setup_conn->callback();
}
add_action( 'admin_action_wpauth0_callback_step1', 'wp_auth0_setup_callback_step1' );

function wp_auth0_setup_callback_step3_social() {
$setup_admin = new WP_Auth0_InitialSetup_AdminUser( WP_Auth0_Options::Instance() );
$setup_admin->callback();
}
add_action( 'admin_action_wpauth0_callback_step3_social', 'wp_auth0_setup_callback_step3_social' );

/**
* Function to call the method that clears out the error log.
*
Expand Down Expand Up @@ -540,6 +586,25 @@ function wp_auth0_settings_admin_action_error() {
}
add_action( 'admin_notices', 'wp_auth0_settings_admin_action_error' );

function wp_auth0_initial_setup_init() {
if ( 'wpa0-setup' !== ( $_REQUEST['page'] ?? null ) || ! isset( $_REQUEST['callback'] ) ) {
return false;
}

if ( 'rejected' === ( $_REQUEST['error'] ?? null ) ) {
wp_safe_redirect( admin_url( 'admin.php?page=wpa0-setup&error=rejected' ) );
exit;
}

if ( 'access_denied' === ( $_REQUEST['error'] ?? null ) ) {
wp_safe_redirect( admin_url( 'admin.php?page=wpa0-setup&error=access_denied' ) );
exit;
}

(new WP_Auth0_InitialSetup_Consent( WP_Auth0_Options::Instance() ))->callback();
}
add_action( 'init', 'wp_auth0_initial_setup_init', 1 );

function wp_auth0_profile_change_email( $wp_user_id, $old_user_data ) {
$options = WP_Auth0_Options::Instance();
$api_client_creds = new WP_Auth0_Api_Client_Credentials( $options );
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
"phpcbf": "\"vendor/bin/phpcbf\"",
"phpcbf-tests": "\"vendor/bin/phpcbf\" --standard=phpcs-test-ruleset.xml -s ./tests/",
"sniffs": "\"vendor/bin/phpcs\" -e",
"test": "\"vendor/bin/phpunit\" --coverage-text",
"test": "\"vendor/bin/phpunit\"",
"test-cov": "\"vendor/bin/phpunit\" --coverage-text",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this command to check coverage, default one does not (to speed up the checks).

"test-group": "\"vendor/bin/phpunit\" --coverage-text --group",
"test-ci": "\"vendor/bin/phpunit\" --coverage-clover=coverage.xml",
"pre-commit-no-tests": [ "@phpcbf", "@phpcbf-tests", "@phpcs-tests", "@compat", "@phpcs-i18n" ],
Expand Down
105 changes: 22 additions & 83 deletions lib/initial-setup/WP_Auth0_InitialSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class WP_Auth0_InitialSetup {
protected $a0_options;
protected $connection_profile;
protected $enterprise_connection_step;
protected $consent_step;
protected $adminuser_step;
protected $connections_step;
protected $end_step;
Expand All @@ -15,53 +14,13 @@ public function __construct( WP_Auth0_Options $a0_options ) {

$this->connection_profile = new WP_Auth0_InitialSetup_ConnectionProfile( $this->a0_options );
$this->enterprise_connection_step = new WP_Auth0_InitialSetup_EnterpriseConnection( $this->a0_options );
$this->consent_step = new WP_Auth0_InitialSetup_Consent( $this->a0_options );
$this->adminuser_step = new WP_Auth0_InitialSetup_AdminUser( $this->a0_options );
$this->connections_step = new WP_Auth0_InitialSetup_Connections( $this->a0_options );
$this->end_step = new WP_Auth0_InitialSetup_End( $this->a0_options );
}

/**
* @deprecated - 3.10.0, will move add_action calls out of this class in the next major.
*
* @codeCoverageIgnore - Deprecated.
*/
public function init() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logic moved to wp_auth0_setup_error_admin_notices()


add_action( 'init', [ $this, 'init_setup' ], 1 );

add_action( 'admin_action_wpauth0_callback_step1', [ $this->connection_profile, 'callback' ] );
add_action( 'admin_action_wpauth0_callback_step3_social', [ $this->adminuser_step, 'callback' ] );

if ( isset( $_REQUEST['page'] ) && 'wpa0-setup' === $_REQUEST['page'] ) {
if ( isset( $_REQUEST['error'] ) ) {
add_action( 'admin_notices', [ $this, 'notify_error' ] );
}
}

if ( isset( $_REQUEST['error'] ) && 'cant_create_client' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'cant_create_client_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'cant_create_client_grant' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'cant_create_client_grant_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'cant_exchange_token' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'cant_exchange_token_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'rejected' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'rejected_message' ] );
}

if ( isset( $_REQUEST['error'] ) && 'access_denied' == $_REQUEST['error'] ) {
add_action( 'admin_notices', [ $this, 'access_denied' ] );
}
}

public function notify_error() {
printf( '<div class="notice notice-error">%s</div>', strip_tags( $_REQUEST['error'] ) );
printf( '<div class="notice notice-error"><p><strong>%s</strong></p></div>', strip_tags( $_REQUEST['error'] ) );
}

public function render_setup_page() {
Expand Down Expand Up @@ -107,14 +66,13 @@ public function render_setup_page() {

public function cant_create_client_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
jimmyjames marked this conversation as resolved.
Show resolved Hide resolved
<p>
<strong>
<?php echo __( 'There was an error creating the Auth0 App. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php echo __( 'Error log', 'wp-auth0' ); ?></a>
<?php echo __( ' for more information. If the problem persists, please create it manually in the ', 'wp-auth0' ); ?>
<a target="_blank" href="https://manage.auth0.com/#/applications"><?php echo __( 'Auth0 Dashboard', 'wp-auth0' ); ?></a>
<?php echo __( ' and copy the Client ID and Client Secret.', 'wp-auth0' ); ?>
<?php _e( 'There was an error creating the Auth0 App. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php _e( 'error log', 'wp-auth0' ); ?></a>
<?php _e( ' for more information. If the problem persists, please follow the ', 'wp-auth0' ); ?>
<a target="_blank" href="https://auth0.com/docs/cms/wordpress/installation#manual-setup"><?php _e( 'manual setup instructions', 'wp-auth0' ); ?></a>.
</strong>
</p>
</div>
Expand All @@ -123,21 +81,21 @@ public function cant_create_client_message() {

public function cant_create_client_grant_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'There was an error creating the necessary client grants. ', 'wp-auth0' ); ?>
<?php _e( 'There was an error creating the necessary client grants. ', 'wp-auth0' ); ?>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo __() is equivalent to _e()

<?php
echo __(
_e(
'Go to your Auth0 dashboard > APIs > Auth0 Management API > Machine to Machine Applications tab and authorize this Application. ',
'wp-auth0'
);
?>
<?php echo __( 'Make sure to add the following scopes: ', 'wp-auth0' ); ?>
<?php _e( 'Make sure to add the following scopes: ', 'wp-auth0' ); ?>
<code><?php echo implode( '</code>, <code>', WP_Auth0_Api_Client::get_required_scopes() ); ?></code>
<?php echo __( 'You can also check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php echo __( 'Error log', 'wp-auth0' ); ?></a>
<?php echo __( ' for more information.', 'wp-auth0' ); ?>
<?php _e( 'You can also check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php _e( 'Error log', 'wp-auth0' ); ?></a>
<?php _e( ' for more information.', 'wp-auth0' ); ?>
</strong>
</p>
</div>
Expand All @@ -146,13 +104,13 @@ public function cant_create_client_grant_message() {

public function cant_exchange_token_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'There was an error retrieving your Auth0 credentials. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php echo __( 'Error log', 'wp-auth0' ); ?></a>
<?php echo __( ' for more information.', 'wp-auth0' ); ?>
<?php echo __( 'Please check that your server has internet access and can reach ', 'wp-auth0' ); ?>
<?php _e( 'There was an error retrieving your Auth0 credentials. Check the ', 'wp-auth0' ); ?>
<a target="_blank" href="<?php echo admin_url( 'admin.php?page=wpa0-errors' ); ?>"><?php _e( 'Error log', 'wp-auth0' ); ?></a>
<?php _e( ' for more information.', 'wp-auth0' ); ?>
<?php _e( 'Please check that your server has internet access and can reach ', 'wp-auth0' ); ?>
<code>https://<?php echo $this->a0_options->get( 'domain' ); ?></code>
</strong>
</p>
Expand All @@ -162,45 +120,26 @@ public function cant_exchange_token_message() {

public function rejected_message() {
?>
<div id="message" class="error">
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'The required scoped were rejected.', 'wp-auth0' ); ?>
<?php _e( 'The required scopes were rejected.', 'wp-auth0' ); ?>
</strong>
</p>
</div>
<?php
}

public function access_denied() {
public function access_denied_message() {
?>
<div class="notice notice-error">
<p>
<strong>
<?php echo __( 'Please create your Auth0 account first at ', 'wp-auth0' ); ?>
<?php _e( 'Please create your Auth0 account first at ', 'wp-auth0' ); ?>
<a href="https://manage.auth0.com">https://manage.auth0.com</a>
</strong>
</p>
</div>
<?php
}

public function init_setup() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to wp_auth0_initial_setup_init()

if ( ( ! isset( $_REQUEST['page'] ) ) || ( 'wpa0-setup' !== $_REQUEST['page'] ) || ( ! isset( $_REQUEST['callback'] ) ) ) {
return;
}

if ( isset( $_REQUEST['error'] ) && 'rejected' == $_REQUEST['error'] ) {
wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=rejected' ) );
exit;
}

if ( isset( $_REQUEST['error'] ) && 'access_denied' == $_REQUEST['error'] ) {
wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=access_denied' ) );
exit;
}

$this->consent_step->callback();
}

}
4 changes: 2 additions & 2 deletions lib/initial-setup/WP_Auth0_InitialSetup_Consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function consent_callback( $name ) {
$client_response = WP_Auth0_Api_Client::create_client( $domain, $this->access_token, $name );

if ( $client_response === false ) {
wp_redirect( admin_url( 'admin.php?page=wpa0&error=cant_create_client' ) );
wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=cant_create_client' ) );
exit;
}

Expand Down Expand Up @@ -192,7 +192,7 @@ public function consent_callback( $name ) {
$grant_response = WP_Auth0_Api_Client::create_client_grant( $this->access_token, $client_id );

if ( false === $grant_response ) {
wp_redirect( admin_url( 'admin.php?page=wpa0&error=cant_create_client_grant' ) );
wp_redirect( admin_url( 'admin.php?page=wpa0-setup&error=cant_create_client_grant' ) );
exit;
}

Expand Down
2 changes: 1 addition & 1 deletion templates/initial-setup/connection_profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<h4><?php _e( 'Manual Setup', 'wp-auth0' ); ?></h4>
<p><?php _e( 'If you already have an Application or want to use an existing database connection, please follow the steps below.', 'wp-auth0' ); ?></p>
<p><a class="a0-button primary" href="https://auth0.com/docs/cms/wordpress/installation#manual-setup"
target="_blank"><?php _e( 'Manual Setup Instructions', 'wp-auth0' ); ?></a></p>
target="_blank"><?php _e( 'manual Setup Instructions', 'wp-auth0' ); ?></a></p>
<br>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions tests/testErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class TestErrorLog extends WP_Auth0_Test_Case {

use RedirectHelpers;

use UsersHelper;

use WpDieHelper;

/**
Expand Down Expand Up @@ -268,6 +270,7 @@ public function testThatNonAdminStopsProcess() {

public function testThatErrorLogCanBeCleared() {
$this->startRedirectHalting();
$this->setGlobalUser();
$_POST['nonce'] = wp_create_nonce( 'clear_error_log' );
$error_log = new WP_Auth0_ErrorLog();
$error_log::insert_error( uniqid(), uniqid() );
Expand Down
Loading