Skip to content

Commit

Permalink
Move admin actions to functions and remove deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
joshcanhelp committed Dec 13, 2019
1 parent da1a7d9 commit 5923ed8
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 177 deletions.
111 changes: 105 additions & 6 deletions WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,12 @@ public function init() {

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

$auth0_admin = new WP_Auth0_Admin( $this->a0_options, $this->router );
$auth0_admin->init();

$error_log = new WP_Auth0_ErrorLog();
$error_log->init();

$import_settings = new WP_Auth0_Import_Settings( $this->a0_options );
$import_settings->init();

$settings_section = new WP_Auth0_Settings_Section( $this->a0_options, $initial_setup, $error_log, $auth0_admin, $import_settings );
$settings_section->init();

$api_client_creds = new WP_Auth0_Api_Client_Credentials( $this->a0_options );

$api_change_password = new WP_Auth0_Api_Change_Password( $this->a0_options, $api_client_creds );
Expand Down Expand Up @@ -506,6 +500,111 @@ function wp_auth0_db_check_update() {
* Core WP hooks
*/

function wp_auth0_init_admin_menu() {

if ( isset( $_REQUEST['page'] ) && $_REQUEST['page'] === 'wpa0-help' ) {
wp_redirect( admin_url( 'admin.php?page=wpa0#help' ), 301 );
exit;
}

$options = WP_Auth0_Options::Instance();
$initial_setup = new WP_Auth0_InitialSetup( $options );
$routes = new WP_Auth0_Routes( $options );
$admin = new WP_Auth0_Admin( $options, $routes );

$setup_slug = 'wpa0-setup';
$setup_title = __( 'Setup Wizard', 'wp-auth0' );
$setup_func = [ $initial_setup, 'render_setup_page' ];

$settings_slug = 'wpa0';
$settings_title = __( 'Settings', 'wp-auth0' );
$settings_func = [ $admin, 'render_settings_page' ];

$menu_parent = ! WP_Auth0::ready() ? $setup_slug : $settings_slug;
$cap = 'manage_options';

add_menu_page(
'Auth0',
'Auth0',
$cap,
$menu_parent,
! WP_Auth0::ready() ? $setup_func : $settings_func,
WPA0_PLUGIN_IMG_URL . 'a0icon.png',
86
);

if ( ! WP_Auth0::ready() ) {
add_submenu_page( $menu_parent, $setup_title, $setup_title, $cap, $setup_slug, $setup_func );
add_submenu_page( $menu_parent, $settings_title, $settings_title, $cap, $settings_slug, $settings_func );
} else {
add_submenu_page( $menu_parent, $settings_title, $settings_title, $cap, $settings_slug, $settings_func );
add_submenu_page(
$menu_parent,
__( 'Help', 'wp-auth0' ),
__( 'Help', 'wp-auth0' ),
$cap,
'wpa0-help',
'__return_false'
);
add_submenu_page( null, $setup_title, $setup_title, $cap, 'wpa0-setup', $setup_func );
}

add_submenu_page(
$menu_parent,
__( 'Error Log', 'wp-auth0' ),
__( 'Error Log', 'wp-auth0' ),
$cap,
'wpa0-errors',
[ new WP_Auth0_ErrorLog(), 'render_settings_page' ]
);

add_submenu_page(
$menu_parent,
__( 'Import-Export Settings', 'wp-auth0' ),
__( 'Import-Export settings', 'wp-auth0' ),
$cap,
'wpa0-import-settings',
[ new WP_Auth0_Import_Settings( $options ), 'render_import_settings_page' ]
);
}
add_action( 'admin_menu', 'wp_auth0_init_admin_menu', 96, 0 );

function wp_auth0_create_account_message() {
$current_page = $_GET['page'] ?? null;
if ( WP_Auth0::ready() || ! $current_page || 0 !== strpos( $current_page, 'wpa' ) ) {
return false;
}

printf(
'<div class="update-nag">%s<strong><a href="%s">%s</a></strong>%s
<strong><a href="https://auth0.com/docs/cms/wordpress/installation#manual-setup" target="_blank">
%s</a></strong>.</div>',
__( 'Login by Auth0 is not yet configured. Please use the ', 'wp-auth0' ),
admin_url( 'admin.php?page=wpa0-setup' ),
__( 'Setup Wizard', 'wp-auth0' ),
__( ' or follow the ', 'wp-auth0' ),
__( 'Manual setup instructions', 'wp-auth0' )
);
return true;
}
add_action( 'admin_notices', 'wp_auth0_create_account_message' );

function wp_auth0_init_admin() {
$options = WP_Auth0_Options::Instance();
$routes = new WP_Auth0_Routes( $options );
$admin = new WP_Auth0_Admin( $options, $routes );
$admin->init_admin();
}
add_action( 'admin_init', 'wp_auth0_init_admin' );

function wp_auth0_admin_enqueue_scripts() {
$options = WP_Auth0_Options::Instance();
$routes = new WP_Auth0_Routes( $options );
$admin = new WP_Auth0_Admin( $options, $routes );
return $admin->admin_enqueue();
}
add_action( 'admin_enqueue_scripts', 'wp_auth0_admin_enqueue_scripts', 1 );

function wp_auth0_custom_requests( $wp, $return = false ) {
$routes = new WP_Auth0_Routes( WP_Auth0_Options::Instance() );
return $routes->custom_requests( $wp, $return );
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"phpcs-tests": "\"vendor/bin/phpcs\" --standard=phpcs-test-ruleset.xml -s ./tests/",
"phpcbf": "\"vendor/bin/phpcbf\"",
"phpcbf-tests": "\"vendor/bin/phpcbf\" --standard=phpcs-test-ruleset.xml -s ./tests/",
"sniffs": "\"vendor/bin/phpcs\" --standard=phpcs-ruleset.xml -e",
"sniffs": "\"vendor/bin/phpcs\" -e",
"test": "\"vendor/bin/phpunit\" --coverage-text",
"test-group": "\"vendor/bin/phpunit\" --coverage-text --group",
"test-ci": "\"vendor/bin/phpunit\" --coverage-clover=coverage.xml",
Expand Down
12 changes: 6 additions & 6 deletions lib/WP_Auth0_Api_Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public static function get_connect_info( $opt = '' ) {
$a0_options = WP_Auth0_Options::Instance();

self::$connect_info = [
'domain' => $a0_options->get( 'domain' ),
'client_id' => $a0_options->get( 'client_id' ),
'client_secret' => $a0_options->get( 'client_secret' ),
'connection' => $a0_options->get( 'db_connection_name' ),
'app_token' => null,
'audience' => self::get_endpoint( 'api/v2/' ),
'domain' => $a0_options->get( 'domain' ),
'client_id' => $a0_options->get( 'client_id' ),
'client_secret' => $a0_options->get( 'client_secret' ),
'connection' => $a0_options->get( 'db_connection_name' ),
'app_token' => null,
'audience' => self::get_endpoint( 'api/v2/' ),
];
}

Expand Down
65 changes: 0 additions & 65 deletions lib/WP_Auth0_Settings_Section.php

This file was deleted.

59 changes: 9 additions & 50 deletions lib/admin/WP_Auth0_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,16 @@ public function __construct( WP_Auth0_Options $a0_options, WP_Auth0_Routes $rout
$this->router = $router;
}

/**
* @deprecated - 3.10.0, will move add_action calls out of this class in the next major.
*
* @codeCoverageIgnore - Deprecated.
*/
public function init() {
add_action( 'admin_init', [ $this, 'init_admin' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue' ], 1 );
}

/**
* Enqueue scripts for all Auth0 wp-admin pages
*/
public function admin_enqueue() {
// Register admin styles
wp_register_style( 'wpa0_bootstrap', WPA0_PLUGIN_BS_URL . 'css/bootstrap.min.css', false, '3.3.5' );
wp_register_style( 'wpa0_admin_initial_settup', WPA0_PLUGIN_CSS_URL . 'initial-setup.css', false, WPA0_VERSION );
wp_register_style( 'wpa0_admin_initial_setup', WPA0_PLUGIN_CSS_URL . 'initial-setup.css', false, WPA0_VERSION );

// Register admin scripts
wp_register_script( 'wpa0_async', WPA0_PLUGIN_LIB_URL . 'async.min.js', false, WPA0_VERSION );
wp_register_script( 'wpa0_bootstrap', WPA0_PLUGIN_BS_URL . 'js/bootstrap.min.js', [ 'jquery' ], '3.3.6' );
wp_register_script( 'wpa0_admin', WPA0_PLUGIN_JS_URL . 'admin.js', [ 'wpa0_bootstrap' ], WPA0_VERSION );
wp_localize_script(
Expand All @@ -49,42 +40,26 @@ public function admin_enqueue() {
'ajax_url' => admin_url( 'admin-ajax.php' ),
]
);
wp_register_script( 'wpa0_async', WPA0_PLUGIN_LIB_URL . 'async.min.js', false, WPA0_VERSION );

$wpa0_pages = [ 'wpa0', 'wpa0-errors', 'wpa0-import-settings', 'wpa0-setup' ];
$wpa0_curr_page = ! empty( $_REQUEST['page'] ) ? $_REQUEST['page'] : '';
if ( ! in_array( $wpa0_curr_page, $wpa0_pages ) ) {
return;
}

if ( ! WP_Auth0::ready() && 'wpa0-setup' !== $_REQUEST['page'] ) {
add_action( 'admin_notices', [ $this, 'create_account_message' ] );
return false;
}

if ( in_array( $wpa0_curr_page, [ 'wpa0', 'wpa0-setup', 'wpa0-import-settings', 'wpa0-errors' ] ) ) {
wp_enqueue_script( 'wpa0_admin' );
wp_enqueue_script( 'wpa0_async' );
}

wp_enqueue_media();
wp_enqueue_style( 'wpa0_bootstrap' );
wp_enqueue_style( 'wpa0_admin_initial_settup' );

if ( 'wpa0-setup' === $wpa0_curr_page && isset( $_REQUEST['signup'] ) ) {
wp_enqueue_script( 'wpa0_lock', $this->a0_options->get_lock_url(), [ 'jquery' ] );
if ( 'wpa0' === $wpa0_curr_page ) {
wp_enqueue_media();
wp_enqueue_style( 'media' );
}

wp_enqueue_style( 'media' );
}

/**
* @deprecated - 3.6.0, this method displayed an empty auth0_app_token notification, which is not necessary.
*
* @codeCoverageIgnore - Deprecated
*/
public function cant_connect_to_auth0() {
// phpcs:ignore
@trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );
wp_enqueue_style( 'wpa0_bootstrap' );
wp_enqueue_style( 'wpa0_admin_initial_setup' );
return true;
}

public function init_admin() {
Expand Down Expand Up @@ -135,22 +110,6 @@ public function input_validator( array $input ) {
return $input;
}

/**
* Show a message on all Auth0 admin pages when the plugin is not ready to process logins
*/
public function create_account_message() {
printf(
'<div class="update-nag">%s<strong><a href="%s">%s</a></strong>%s
<strong><a href="https://auth0.com/docs/cms/wordpress/installation#manual-setup" target="_blank">
%s</a></strong>.</div>',
__( 'Login by Auth0 is not yet configured. Please use the ', 'wp-auth0' ),
admin_url( 'admin.php?page=wpa0-setup' ),
__( 'Setup Wizard', 'wp-auth0' ),
__( ' or follow the ', 'wp-auth0' ),
__( 'Manual setup instructions', 'wp-auth0' )
);
}

public function render_settings_page() {
include WPA0_PLUGIN_DIR . 'templates/settings.php';
}
Expand Down
42 changes: 0 additions & 42 deletions lib/admin/WP_Auth0_Admin_Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,37 +110,6 @@ protected function add_validation_error( $error, $type = 'error' ) {
);
}

/**
* @deprecated - 3.10.0, no longer used.
*
* @codeCoverageIgnore - Deprecated.
*/
protected function rule_validation( $old_options, $input, $key, $rule_name, $rule_script ) {
// phpcs:ignore
@trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );
$input[ $key ] = ( isset( $input[ $key ] ) ? $input[ $key ] : null );

if ( ( $input[ $key ] !== null && $old_options[ $key ] === null ) || ( $input[ $key ] === null && $old_options[ $key ] !== null ) ) {

try {

$operations = new WP_Auth0_Api_Operations( $this->options );
$input[ $key ] = $operations->toggle_rule(
$this->options->get( 'auth0_app_token' ),
( is_null( $input[ $key ] ) ? $old_options[ $key ] : null ),
$rule_name,
$rule_script
);

} catch ( Exception $e ) {
$this->add_validation_error( $e->getMessage() );
$input[ $key ] = null;
}
}

return $input;
}

/**
* Output a stylized switch on the options page
*
Expand Down Expand Up @@ -306,15 +275,4 @@ protected function get_docs_link( $path, $text = '' ) {
$text = empty( $text ) ? __( 'here', 'wp-auth0' ) : sanitize_text_field( $text );
return sprintf( '<a href="https://auth0.com/docs/%s" target="_blank">%s</a>', $path, $text );
}

/**
* @deprecated - 3.6.0, use WP_Auth0_Admin_Generic::render_switch() instead
*
* @codeCoverageIgnore - Deprecated
*/
protected function render_a0_switch( $id, $name, $value, $checked ) {
// phpcs:ignore
@trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );
$this->render_switch( $id, $name );
}
}
12 changes: 6 additions & 6 deletions tests/testAdminBasicValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public static function setUpBeforeClass() {
parent::setUpBeforeClass();
self::$admin = new WP_Auth0_Admin_Basic( self::$opts );
self::$fields = [
'domain' => 'test.auth0.com',
'custom_domain' => '',
'client_id' => '__test_client_id__',
'client_secret' => '__test_client_secret__',
'client_signing_algorithm' => WP_Auth0_Api_Client::DEFAULT_CLIENT_ALG,
'cache_expiration' => '',
'domain' => 'test.auth0.com',
'custom_domain' => '',
'client_id' => '__test_client_id__',
'client_secret' => '__test_client_secret__',
'client_signing_algorithm' => WP_Auth0_Api_Client::DEFAULT_CLIENT_ALG,
'cache_expiration' => '',
];
}

Expand Down
Loading

0 comments on commit 5923ed8

Please sign in to comment.