-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add clear log action; combine same errors in log; increase error log …
…limit; fix offline assets/styles
- Loading branch information
1 parent
ccec093
commit 731079f
Showing
12 changed files
with
504 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.