Skip to content

Commit

Permalink
Translate log messages; Fixed use of Nunil_Exception class
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Foti <foti.giuseppe@gmail.com>
  • Loading branch information
MocioF committed Apr 29, 2024
1 parent f745b06 commit 63f7c29
Show file tree
Hide file tree
Showing 20 changed files with 157 additions and 116 deletions.
62 changes: 29 additions & 33 deletions admin/class-no-unsafe-inline-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use NUNIL\Nunil_Lib_Db as DB;
use NUNIL\Nunil_Lib_Utils as Utils;
use NUNIL\Nunil_Lib_Log as Log;
use NUNIL\Nunil_Exception;

/**
* The admin-specific functionality of the plugin.
Expand Down Expand Up @@ -471,11 +473,15 @@ public function nunil_set_screen(): void {
* @return void
*/
public function register_options(): void {
register_setting(
'no-unsafe-inline_group',
'no-unsafe-inline',
array( $this, 'sanitize_options' )
);
try {
register_setting(
'no-unsafe-inline_group',
'no-unsafe-inline',
array( $this, 'sanitize_options' )
);
} catch ( Nunil_Exception $ex ) {
$ex->logexception();
}

add_settings_section(
'no-unsafe-inline_fetch_directives_settings',
Expand Down Expand Up @@ -885,7 +891,7 @@ public function register_base_rule(): void {
/**
* Sanitize the settings
*
* @throws \Exception Main option is not an array.
* @throws \NUNIL\Nunil_Exception Main option is not an array.
* @param array<string|array<string>> $input Contains the settings.
* @return array<mixed>
*/
Expand All @@ -897,7 +903,7 @@ public function sanitize_options( $input ) {

$options = (array) get_option( 'no-unsafe-inline' );
if ( empty( $options ) ) {
throw( new Exception( 'Option no-unsafe-inline is not an array' ) );
throw new Nunil_Exception( esc_html__( 'The wp-option no-unsafe-inline is not an array', 'no-unsafe-inline' ) );
}

// Checkboxes.
Expand Down Expand Up @@ -927,7 +933,7 @@ public function sanitize_options( $input ) {
foreach ( $check_box_options as $option_name ) {
if ( ! isset( $input[ $option_name ] ) ) {
$new_input[ $option_name ] = 0;
} elseif ( ! in_array( $input[ $option_name ], $admitted_values ) ) {
} elseif ( ! in_array( $input[ $option_name ], $admitted_values, true ) ) {
$new_input[ $option_name ] = 0;
} elseif ( 'on' === $input[ $option_name ] ) {
$new_input[ $option_name ] = 1;
Expand Down Expand Up @@ -1020,7 +1026,7 @@ public function sanitize_options( $input ) {
'errorlog',
);
if ( isset( $input['log_driver'] ) && is_string( $input['log_driver'] ) ) {
if ( in_array( $input['log_driver'], $admitted_log_drivers ) ) {
if ( in_array( $input['log_driver'], $admitted_log_drivers, true ) ) {
$new_input['log_driver'] = sanitize_text_field( $input['log_driver'] );
}
}
Expand All @@ -1032,7 +1038,7 @@ public function sanitize_options( $input ) {
'debug',
);
if ( isset( $input['log_level'] ) && is_string( $input['log_level'] ) ) {
if ( in_array( $input['log_level'], $admitted_log_levels ) ) {
if ( in_array( $input['log_level'], $admitted_log_levels, true ) ) {
$new_input['log_level'] = sanitize_text_field( $input['log_level'] );
}
}
Expand Down Expand Up @@ -1084,15 +1090,15 @@ public function sanitize_tools( $input ) {
}

foreach ( $input as $key => $value ) {
if ( ! in_array( $key, $options_checked ) ) {
if ( ! in_array( $key, $options_checked, true ) ) {
unset( $input[ $key ] );
}
}
foreach ( $options_checked as $option_name ) {
if ( ! isset( $input[ $option_name ] ) ) {
$input[ $option_name ] = 0;
}
if ( ! in_array( $input[ $option_name ], $admitted_values ) ) {
if ( ! in_array( $input[ $option_name ], $admitted_values, true ) ) {
$input[ $option_name ] = 0;
}
if ( 'on' === $input[ $option_name ] ) {
Expand Down Expand Up @@ -1659,7 +1665,7 @@ public function print_log_driver(): void {
$options = (array) get_option( 'no-unsafe-inline' );
$value = ( isset( $options['log_driver'] ) && is_string( $options['log_driver'] ) ) ? esc_attr( $options['log_driver'] ) : 'errorlog';

$print_selected = function( $val ) use ( $value ) {
$print_selected = function ( $val ) use ( $value ) {
return $val === $value ? 'selected' : '';
};
printf(
Expand All @@ -1680,7 +1686,7 @@ public function print_log_level(): void {
$options = (array) get_option( 'no-unsafe-inline' );
$value = ( isset( $options['log_level'] ) && is_string( $options['log_level'] ) ) ? esc_attr( $options['log_level'] ) : 'error';

$print_selected = function( $val ) use ( $value ) {
$print_selected = function ( $val ) use ( $value ) {
return $val === $value ? 'selected' : '';
};
printf(
Expand Down Expand Up @@ -2248,10 +2254,8 @@ public function trigger_clustering(): void {

if ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) ) === 'xmlhttprequest' ) {
echo wp_json_encode( $result );
} else {
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
} elseif ( isset( $_SERVER['HTTP_REFERER'] ) ) {
header( 'Location: ' . esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) );
}
}

wp_die();
Expand Down Expand Up @@ -2291,8 +2295,8 @@ public function clean_database(): void {
}
$result_string = $result_string . '</ul>';

\NUNIL\Nunil_Lib_Utils::show_message( '<strong>No unsafe-inline</strong> ' . esc_html__( 'cleaned up the database at the user\'s request', 'no-unsafe-inline' ), 'success' );
\NUNIL\Nunil_Lib_Log::info( 'cleaned up the database at the user\'s request' );
Utils::show_message( '<strong>No unsafe-inline</strong> ' . esc_html__( 'Eliminated the content of the database at the user\'s request', 'no-unsafe-inline' ), 'success' );
Log::info( esc_html__( 'Eliminated the content of the database at the user\'s request', 'no-unsafe-inline' ) );

$result = array(
'type' => 'success',
Expand All @@ -2301,10 +2305,8 @@ public function clean_database(): void {

if ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) ) === 'xmlhttprequest' ) {
echo wp_json_encode( $result );
} else {
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
} elseif ( isset( $_SERVER['HTTP_REFERER'] ) ) {
header( 'Location: ' . esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) );
}
}

wp_die();
Expand Down Expand Up @@ -2332,8 +2334,8 @@ public function prune_database(): void {
$result_string = $result_string . $prune->prune_external_assets();
$result_string = $result_string . $prune->prune_old_occurences();

\NUNIL\Nunil_Lib_Utils::show_message( '<strong>No unsafe-inline</strong> ' . esc_html__( 'pruned the database at the user\'s request', 'no-unsafe-inline' ), 'success' );
\NUNIL\Nunil_Lib_Log::info( 'pruned the database at the user\'s request' );
Utils::show_message( '<strong>No unsafe-inline</strong> ' . esc_html__( 'Pruned the database at the user\'s request', 'no-unsafe-inline' ), 'success' );
Log::info( esc_html__( 'Pruned the database at the user\'s request', 'no-unsafe-inline' ) );

$result = array(
'type' => 'success',
Expand All @@ -2342,10 +2344,8 @@ public function prune_database(): void {

if ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) ) === 'xmlhttprequest' ) {
echo wp_json_encode( $result );
} else {
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
} elseif ( isset( $_SERVER['HTTP_REFERER'] ) ) {
header( 'Location: ' . esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) );
}
}

wp_die();
Expand All @@ -2368,10 +2368,8 @@ public function update_summary_tables(): void {

if ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) ) === 'xmlhttprequest' ) {
echo wp_json_encode( $result );
} else {
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
} elseif ( isset( $_SERVER['HTTP_REFERER'] ) ) {
header( 'Location: ' . esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) );
}
}

wp_die();
Expand Down Expand Up @@ -2562,10 +2560,8 @@ public function test_classifier(): void {
);
if ( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ) ) === 'xmlhttprequest' ) {
echo wp_json_encode( $result );
} else {
if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
} elseif ( isset( $_SERVER['HTTP_REFERER'] ) ) {
header( 'Location: ' . esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) );
}
}

wp_die();
Expand Down
8 changes: 6 additions & 2 deletions admin/partials/class-no-unsafe-inline-admin-logs-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ public function prepare_items() {
$orderby = ( isset( $_REQUEST['orderby'] ) && in_array( $_REQUEST['orderby'], array_keys( $this->get_sortable_columns() ), true ) ) ? sanitize_text_field( wp_unslash( $_REQUEST['orderby'] ) ) : 'created_at';
$order = ( isset( $_REQUEST['order'] ) && in_array( $_REQUEST['order'], array( 'asc', 'desc' ) ) ) ? sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) : 'desc';

$logs = NUNIL\Nunil_Lib_Db::get_logs( $paged * $per_page, $per_page, $orderby, $order, ARRAY_A );
$this->items = (array) $logs;
try {
$logs = NUNIL\Nunil_Lib_Db::get_logs( $paged * $per_page, $per_page, $orderby, $order, ARRAY_A );
$this->items = (array) $logs;
} catch ( NUNIL\Nunil_Exception $ex ) {
$ex->logexception();
}

$this->set_pagination_args(
array(
Expand Down
6 changes: 5 additions & 1 deletion admin/partials/class-no-unsafe-inline-base-rule-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ public function prepare_items() {
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array( $columns, $hidden, $sortable );
$this->items = self::get_sources();
try {
$this->items = self::get_sources();
} catch ( NUNIL\Nunil_Exception $ex ) {
$ex->logexception();
}
}
}
10 changes: 8 additions & 2 deletions admin/partials/class-no-unsafe-inline-events-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function process_bulk_action() {
public function column_script( $item ) {
$admin_page_url = admin_url( 'options-general.php' );

$hl = new \Highlight\Highlighter();
$hl = new Highlighter();
$hl->setAutodetectLanguages( array( 'javascript', 'css', 'json', 'wasm' ) );
$highlighted = $hl->highlightAuto( $item['script'] );
$code = '<div class="nunil-code-wrapper-' . $item['ID'] . '"><div class="code-accordion-' . $item['ID'] . '"><h4>' . esc_html__( 'View code', 'no-unsafe-inline' ) . '</h4>';
Expand Down Expand Up @@ -317,7 +317,13 @@ public function column_default( $item, $column_name ) {
case 'occurences':
return $item[ $column_name ];
default:
Log::debug( 'Error in column_default( $item, $column_name ) ' . print_r( $item, true ) );
Log::debug(
sprintf(
// translators: %s is a dumped variable content.
esc_html__( 'Error in column_default( $item, $column_name ). $item is:%s ', 'no-unsafe-inline' ),
'<pre><code>' . esc_html( print_r( $item, true ) ) . '</code></pre>'
)
);
return;
}
}
Expand Down
8 changes: 7 additions & 1 deletion admin/partials/class-no-unsafe-inline-external-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,13 @@ public function column_default( $item, $column_name ) {
case 'sha512':
return $item[ $column_name ];
default:
Log::debug( 'Error in column_default( $item, $column_name ) ' . print_r( $item, true ) );
Log::debug(
sprintf(
// translators: %s is a dumped variable content.
esc_html__( 'Error in column_default( $item, $column_name ). $item is: %s', 'no-unsafe-inline' ),
'<pre><code>' . esc_html( print_r( $item, true ) ) . '</code></pre>'
)
);
return;
}
}
Expand Down
12 changes: 9 additions & 3 deletions admin/partials/class-no-unsafe-inline-inline-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public function process_bulk_action() {
public function column_script( $item ) {
$admin_page_url = admin_url( 'options-general.php' );

$hl = new \Highlight\Highlighter();
$hl = new Highlighter();
$hl->setAutodetectLanguages( array( 'javascript', 'css', 'json', 'wasm' ) );
$highlighted = $hl->highlightAuto( $item['script'] );
$code = '<div class="nunil-code-wrapper-' . $item['ID'] . '"><div class="code-accordion-' . $item['ID'] . '"><h4>' . esc_html__( 'View code', 'no-unsafe-inline' ) . '</h4>';
Expand Down Expand Up @@ -281,7 +281,7 @@ public function column_clustername( $item ) {
* @return string
*/
public function column_pages( $item ) {
$hl = new \Highlight\Highlighter();
$hl = new Highlighter();
$highlighted = $hl->highlightAuto( $item['pages'] );
$code = '<div class="pages-wrapper-' . $item['ID'] . '"><div class="pages-accordion-' . $item['ID'] . '"><h5>' . esc_html__( 'View pages', 'no-unsafe-inline' ) . '</h5>';
$code .= "<div><pre class=\"nunil-pages-code\"><code class=\"hljs {$highlighted->language}\">";
Expand Down Expand Up @@ -309,7 +309,13 @@ public function column_default( $item, $column_name ) {
case 'occurences':
return $item[ $column_name ];
default:
Log::debug( 'Error in column_default( $item, $column_name ) ' . print_r( $item, true ) );
Log::debug(
sprintf(
// translators: %s is a dumped variable content.
esc_html__( 'Error in column_default( $item, $column_name ). $item is: %s', 'no-unsafe-inline' ),
'<pre><code>' . esc_html( print_r( $item, true ) ) . '</code></pre>'
)
);
return;
}
}
Expand Down
16 changes: 8 additions & 8 deletions includes/class-no-unsafe-inline-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use NUNIL\Nunil_Manage_Muplugin;
use NUNIL\Nunil_Lib_Log as Log;
use NUNIL\Nunil_Lib_Db as DB;
use NUNIL\Nunil_Exception;

/**
* Fired during plugin activation.
Expand Down Expand Up @@ -144,15 +145,14 @@ public static function single_activate( $network_wide = false ): void {
self::disable_all_tools();
self::set_default_options();

try {
if ( ! Nunil_Manage_Muplugin::is_nunil_muplugin_installed() ) {
if ( ! Nunil_Manage_Muplugin::is_nunil_muplugin_installed() ) {
try {
Nunil_Manage_Muplugin::toggle_nunil_muplugin_installation();
} catch ( Nunil_Exception $ex ) {
$ex->logexception();
require_once __DIR__ . '/class-no-unsafe-inline-deactivator.php';
\No_Unsafe_Inline_Deactivator::deactivate( $network_wide );
}
} catch ( Exception $ex ) {
Log::error( 'Impossible to install mu-plugin: ' . $ex->getMessage() . ', ' . $ex->getTraceAsString() );
Log::error( 'no-unsafe-inline cannot work without mu-plugin. Deactivate.' );
require_once __DIR__ . '/class-no-unsafe-inline-deactivator.php';
\No_Unsafe_Inline_Deactivator::deactivate( $network_wide );
}
Log::info( 'Activated plugin.' );
}
Expand Down Expand Up @@ -365,7 +365,7 @@ public static function check_php_required_extensions() {
if ( '' !== $needed && 'cli' === php_sapi_name() ) {
echo PHP_VERSION . PHP_EOL;
echo 'EXTENSIONS REQUIRED: ' . PHP_EOL;
echo $needed;
echo esc_html( $needed );
}
return $needed;
}
Expand Down
21 changes: 11 additions & 10 deletions includes/class-no-unsafe-inline-deactivator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use NUNIL\Nunil_Manage_Muplugin;
use NUNIL\Nunil_Lib_Log as Log;
use NUNIL\Nunil_Lib_Db as DB;
use NUNIL\Nunil_Exception;

/**
* Fired during plugin deactivation.
Expand Down Expand Up @@ -58,19 +59,19 @@ public static function deactivate( $network_wide ) {
if ( in_array( NO_UNSAFE_INLINE_PLUGIN_BASENAME, (array) get_option( 'active_plugins', array() ), true ) ) {
$remove_mu_plugin = false;
}
Log::info( 'Deactivated plugin.' );
Log::info( esc_html__( 'Deactivated plugin.', 'no-unsafe-inline' ) );
restore_current_blog();
}
// On network wide deactivation, remove mu-plugin.
try {
if ( true === $remove_mu_plugin && Nunil_Manage_Muplugin::is_nunil_muplugin_installed() ) {
if ( true === $remove_mu_plugin && Nunil_Manage_Muplugin::is_nunil_muplugin_installed() ) {
try {
Nunil_Manage_Muplugin::toggle_nunil_muplugin_installation();
} catch ( Nunil_Exception $ex ) {
$ex->logexception();
}
} catch ( Exception $ex ) {
Log::error( 'Impossible to uninstall mu-plugin: ' . $ex->getMessage() . ', ' . $ex->getTraceAsString() );
}
} else {
Log::error( 'Impossible to perform network deactivation on older wp installation with more than 10000 sites' );
Log::error( esc_html__( 'Impossible to perform network deactivation on older wp installation with more than 10000 sites', 'no-unsafe-inline' ) );
}
} else {
// On single site deactivation, remove tables and settings, if set in options.
Expand All @@ -96,12 +97,12 @@ public static function deactivate( $network_wide ) {
}
}
// Remove mu-plugin only if is not a multisite enviroment.
try {
if ( false === is_multisite() && Nunil_Manage_Muplugin::is_nunil_muplugin_installed() ) {
if ( false === is_multisite() && Nunil_Manage_Muplugin::is_nunil_muplugin_installed() ) {
try {
Nunil_Manage_Muplugin::toggle_nunil_muplugin_installation();
} catch ( Nunil_Exception $ex ) {
$ex->logexception();
}
} catch ( Exception $ex ) {
Log::error( 'Impossible to uninstall mu-plugin: ' . $ex->getMessage() . ', ' . $ex->getTraceAsString() );
}
}
}
Expand Down
Loading

0 comments on commit 63f7c29

Please sign in to comment.