Skip to content

Commit

Permalink
Add clear log action; combine same errors in log; increase error log …
Browse files Browse the repository at this point in the history
…limit; fix offline assets/styles
  • Loading branch information
joshcanhelp committed Sep 3, 2018
1 parent ccec093 commit 731079f
Show file tree
Hide file tree
Showing 12 changed files with 504 additions and 97 deletions.
6 changes: 5 additions & 1 deletion WP_Auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public function init() {
$auth0_admin->init();

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

$configure_jwt_auth = new WP_Auth0_Configure_JWTAUTH( $this->a0_options );
$configure_jwt_auth->init();
Expand Down Expand Up @@ -450,12 +451,15 @@ public function install() {
public function deactivate() {
flush_rewrite_rules();
}

public static function uninstall() {
$a0_options = WP_Auth0_Options::Instance();
$a0_options->delete();

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

delete_option( 'auth0_db_version' );
delete_option( 'auth0_error_log' );

delete_option( 'widget_wp_auth0_popup_widget' );
delete_option( 'widget_wp_auth0_widget' );
Expand Down
2 changes: 1 addition & 1 deletion assets/css/initial-setup.css

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions assets/css/initial-setup/main.styl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
background-color: bgColor;

.a0-wrap {
font-family:'avenir roman';
font-family:'avenir roman', "Open Sans", sans-serif;
margin-left:-10px;
padding-left: 2%;
padding-right: 2%;
Expand Down Expand Up @@ -903,8 +903,8 @@
}

.a0-table {
margin:30px;
width: initial;
margin: 2em 2%;
width: 96%;
}

#manuallySetToken {
Expand Down
Binary file added assets/img/auth0-logo-blue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 17 additions & 1 deletion assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,23 @@ jQuery(document).ready(function($) {
media_frame.open();
});

// Show/hide field for specific switches
/*
Generic form confirm stop
*/
$('form.js-a0-confirm-submit').submit(function (e) {
var message = $(this).attr('data-confirm-msg');
if ( !message || !message.length ) {
message = wpa0.form_confirm_submit_msg;
}

if ( ! window.confirm(message) ) {
e.preventDefault();
}
});

/*
Show/hide field for specific switches
*/
$('[data-expand][data-expand!=""]').each( function() {
var $thisSwitch = $( this );
var $showFieldRow = $( '#' + $thisSwitch.attr( 'data-expand' ).trim() ).closest( 'tr' );
Expand Down
138 changes: 132 additions & 6 deletions lib/WP_Auth0_ErrorLog.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,155 @@
<?php
/**
* Contains the WP_Auth0_ErrorLog class.
*
* @package WP-Auth0
* @since 2.0.0
*/

/**
* Class WP_Auth0_ErrorLog.
* Handles error log CRUD actions and hooks.
*/
class WP_Auth0_ErrorLog {

/**
* Option name used to store the error log.
*/
const OPTION_NAME = 'auth0_error_log';

/**
* Limit of the error logs that can be stored
*/
const ERROR_LOG_ENTRY_LIMIT = 30;

/**
* Add actions and filters for the error log settings section.
*
* @deprecated 3.6.0 - Not needed, handled in WP_Auth0_Admin::admin_enqueue()
* @link https://developer.wordpress.org/reference/hooks/admin_action__requestaction/
*/
public function init() {
// phpcs:ignore
trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );
add_action( 'admin_action_wpauth0_clear_error_log', 'wp_auth0_errorlog_clear_error_log' );
}

/**
* Render the settings page.
*
* @see WP_Auth0_Settings_Section::init_menu()
*/
public function render_settings_page() {
include WPA0_PLUGIN_DIR . 'templates/a0-error-log.php';
}

/**
* Get the error log.
*
* @return array
*/
public function get() {
$log = get_option( self::OPTION_NAME );

if ( empty( $log ) ) {
$log = array();
}

return $log;
}

/**
* Add a new log entry, checking for previous duplicates and limit.
*
* @param array $new_entry - New log entry to add.
*
* @return bool
*/
public function add( array $new_entry ) {
$log = $this->get();

// Prepare the last error log entry to compare with the new one.
$last_entry = null;
if ( ! empty( $log ) ) {
// Get the last error logged.
$last_entry = $log[0];

// Remove date and count fields so it can be compared with the new error.
$last_entry = array_diff_key( $last_entry, array_flip( array( 'date', 'count' ) ) );
}

if ( serialize( $last_entry ) === serialize( $new_entry ) ) {
// New error and last error are the same so set the current time and increment the counter.
$log[0]['date'] = time();
$log[0]['count'] = isset( $log[0]['count'] ) ? intval( $log[0]['count'] ) + 1 : 2;
} else {
// New error is not a repeat to set required fields.
$new_entry['date'] = time();
$new_entry['count'] = 1;
array_unshift( $log, $new_entry );
}

return $this->update( $log );
}

/**
* Clear out the error log.
*
* @return bool
*/
public function clear() {
return update_option( self::OPTION_NAME, array() );
}

/**
* Delete the error log option.
*
* @return bool
*/
public function delete() {
return delete_option( self::OPTION_NAME );
}

/**
* Update the error log with an array and enforcing the length limit.
*
* @param array $log - Log array to update.
*
* @return bool
*/
private function update( array $log ) {
if ( count( $log ) > self::ERROR_LOG_ENTRY_LIMIT ) {
array_pop( $log );
}
return update_option( self::OPTION_NAME, $log );
}

/**
* Enqueue scripts and styles.
*
* @deprecated 3.6.0 - Not needed, handled in WP_Auth0_Admin::admin_enqueue()
*/
public function admin_enqueue() {
// phpcs:ignore
trigger_error( sprintf( __( 'Method %s is deprecated.', 'wp-auth0' ), __METHOD__ ), E_USER_DEPRECATED );
}
}

public function render_settings_page() {
/**
* Function to call the method that clears out the error log.
*
* @hook admin_action_wpauth0_clear_error_log
*/
function wp_auth0_errorlog_clear_error_log() {

$data = get_option( 'auth0_error_log', array() );
if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'clear_error_log' ) ) {
wp_die( __( 'Not allowed.', 'wp-auth0' ) );
}

include WPA0_PLUGIN_DIR . 'templates/a0-error-log.php';
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'Not authorized.', 'wp-auth0' ) );
}

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

wp_safe_redirect( admin_url( 'admin.php?page=wpa0-errors&cleared=1' ) );
exit;
}
60 changes: 27 additions & 33 deletions lib/WP_Auth0_ErrorManager.php
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
<?php
/**
* Contains the WP_Auth0_ErrorManager class.
*
* @package WP-Auth0
*/

/**
* Class WP_Auth0_ErrorManager
* Class WP_Auth0_ErrorManager.
* Handles creating a new error log entry.
*/
class WP_Auth0_ErrorManager {

/**
* Create a row in the error log, up to 20 entries
* Create a row in the error log.
*
* @param string $section - Portion of the codebase that generated the error.
* @param mixed $error - Error message string or discoverable error type.
*
* @param string $section - portion of the codebase that generated the error
* @param string|WP_Error|Exception $error - error message string or discoverable error type
* @return bool
*/
public static function insert_auth0_error( $section, $error ) {
$code = 'unknown_code';
$message = __( 'Unknown error message', 'wp-auth0' );

$new_entry = array(
'section' => $section,
'code' => 'unknown_code',
'message' => __( 'Unknown error message', 'wp-auth0' ),
);

if ( $error instanceof WP_Error ) {
$code = $error->get_error_code();
$message = $error->get_error_message();
$new_entry['code'] = $error->get_error_code();
$new_entry['message'] = $error->get_error_message();
} elseif ( $error instanceof Exception ) {
$code = $error->getCode();
$message = $error->getMessage();
$new_entry['code'] = $error->getCode();
$new_entry['message'] = $error->getMessage();
} elseif ( is_array( $error ) && ! empty( $error['response'] ) ) {
if ( ! empty( $error['response']['code'] ) ) {
$code = sanitize_text_field( $error['response']['code'] );
$new_entry['code'] = sanitize_text_field( $error['response']['code'] );
}
if ( ! empty( $error['response']['message'] ) ) {
$message = sanitize_text_field( $error['response']['message'] );
$new_entry['message'] = sanitize_text_field( $error['response']['message'] );
}
} else {
$message = is_object( $error ) || is_array( $error ) ? serialize( $error ) : $error;
}

$log = get_option( 'auth0_error_log' );

if ( empty( $log ) ) {
$log = array();
}

array_unshift(
$log, array(
'section' => $section,
'code' => $code,
'message' => $message,
'date' => time(),
)
);

if ( count( $log ) > 20 ) {
array_pop( $log );
$new_entry['message'] = is_object( $error ) || is_array( $error ) ? serialize( $error ) : $error;
}

update_option( 'auth0_error_log', $log );
$error_log = new WP_Auth0_ErrorLog();
return $error_log->add( $new_entry );
}
}
26 changes: 12 additions & 14 deletions lib/WP_Auth0_Settings_Section.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,21 @@ public function init_menu() {
exit;
}

$client_id = $this->a0_options->get( 'client_id' );
$client_secret = $this->a0_options->get( 'client_secret' );
$domain = $this->a0_options->get( 'domain' );

$show_initial_setup = ( ( ! $client_id ) || ( ! $client_secret ) || ( ! $domain ) );

$main_menu = 'wpa0';

if ( $show_initial_setup ) {
$main_menu = 'wpa0-setup';
}
$main_menu = ! WP_Auth0::ready() ? 'wpa0-setup' : 'wpa0';

add_menu_page(
__( 'Auth0', 'wp-auth0' ), __( 'Auth0', 'wp-auth0' ), 'manage_options', $main_menu,
( $show_initial_setup ? array( $this->initial_setup, 'render_setup_page' ) : array( $this->auth0_admin, 'render_settings_page' ) ), WPA0_PLUGIN_IMG_URL . 'a0icon.png', 85.55
__( 'Auth0', 'wp-auth0' ),
__( 'Auth0', 'wp-auth0' ),
'manage_options',
$main_menu,
! WP_Auth0::ready() ?
array( $this->initial_setup, 'render_setup_page' ) :
array( $this->auth0_admin, 'render_settings_page' ),
WPA0_PLUGIN_IMG_URL . 'a0icon.png',
85.55
);

if ( $show_initial_setup ) {
if ( ! WP_Auth0::ready() ) {
add_submenu_page( $main_menu, __( 'Auth0 for WordPress - Setup Wizard', 'wp-auth0' ), __( 'Setup Wizard', 'wp-auth0' ), 'manage_options', 'wpa0-setup', array( $this->initial_setup, 'render_setup_page' ) );
add_submenu_page( $main_menu, __( 'Settings', 'wp-auth0' ), __( 'Settings', 'wp-auth0' ), 'manage_options', 'wpa0', array( $this->auth0_admin, 'render_settings_page' ) );
} else {
Expand All @@ -68,6 +65,7 @@ public function init_menu() {
}
}

// TODO: deprecate
public function redirect_to_help() {

}
Expand Down
15 changes: 8 additions & 7 deletions lib/admin/WP_Auth0_Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public function admin_enqueue() {
wp_register_script( 'wpa0_admin', WPA0_PLUGIN_JS_URL . 'admin.js', array( 'wpa0_bootstrap' ), WPA0_VERSION );
wp_localize_script(
'wpa0_admin', 'wpa0', array(
'media_title' => __( 'Choose your icon', 'wp-auth0' ),
'media_button' => __( 'Choose icon', 'wp-auth0' ),
'clear_cache_working' => __( 'Working ...', 'wp-auth0' ),
'clear_cache_done' => __( 'Done!', 'wp-auth0' ),
'clear_cache_nonce' => wp_create_nonce( 'auth0_delete_cache_transient' ),
'ajax_url' => admin_url( 'admin-ajax.php' ),
'media_title' => __( 'Choose your icon', 'wp-auth0' ),
'media_button' => __( 'Choose icon', 'wp-auth0' ),
'clear_cache_working' => __( 'Working ...', 'wp-auth0' ),
'clear_cache_done' => __( 'Done!', 'wp-auth0' ),
'clear_cache_nonce' => wp_create_nonce( 'auth0_delete_cache_transient' ),
'form_confirm_submit_msg' => __( 'Are you sure?', 'wp-auth0' ),
'ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
wp_register_script( 'wpa0_async', WPA0_PLUGIN_LIB_URL . 'async.min.js', false, WPA0_VERSION );
Expand All @@ -51,7 +52,7 @@ public function admin_enqueue() {
add_action( 'admin_notices', array( $this, 'create_account_message' ) );
}

if ( in_array( $wpa0_curr_page, array( 'wpa0', 'wpa0-setup', 'wpa0-import-settings' ) ) ) {
if ( in_array( $wpa0_curr_page, array( 'wpa0', 'wpa0-setup', 'wpa0-import-settings', 'wpa0-errors' ) ) ) {
wp_enqueue_script( 'wpa0_admin' );
wp_enqueue_script( 'wpa0_async' );
}
Expand Down
Loading

0 comments on commit 731079f

Please sign in to comment.