Skip to content

Commit

Permalink
FIX CS & phpstan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Aug 10, 2023
1 parent 0ef89bb commit 25e4dcc
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 48 deletions.
6 changes: 3 additions & 3 deletions inc/blog-management/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ function load() :void {
* @package [package]
* @since 2.10
*
* @param array $tables [description]
* @param string[] $tables [description]
* @param int $site_id [description]
*
* @return array Tables to delete (in addition to the WP default).
* @return string[] Tables to delete (in addition to the WP default).
*/
function wpmu_drop_tables( array $tables, int $site_id ) : array {
function wpmu_drop_tables( array $tables, int $site_id ) :array {

global $wpdb;

Expand Down
88 changes: 65 additions & 23 deletions inc/dashboard-widget/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,36 @@
use function balanceTags;
use function current_user_can;
use function wp_add_dashboard_widget;
use WP_DEBUG_LOG;

// const VIEW = ( defined( 'WP_DEBUG' ) ) ? 'debug' : 'error';
const VIEW = 'error';
// const VIEW = 'debugORerror';
const FILE = '/logs/php.' . VIEW . '.log';
/**
* Get name of log file to show, depending on WP_DEBUG set or not.
*
* @return string
*/
function get_logfile_type() :string {
return ( defined( 'WP_DEBUG' ) ) ? 'debug' : 'error';
}

// const LOG = constant( 'WP_CONTENT_DIR' ) . FILE;
const LOG = \WP_DEBUG_LOG;
/**
* Get location of logfile relative to "wp-content" directory.
*
* @return string
*/
function get_logfile_location() :string {
return \sprintf(
'/logs/php.%s.log',
get_logfile_type()
);
}

/**
* Get full absolute path of relevant logfile.
*
* @return string
*/
function get_logfile_path() :string {
return \WP_CONTENT_DIR . get_logfile_location();
}

/**
* Bootstrap module, when enabled.
Expand All @@ -33,7 +54,12 @@ function bootstrap() :void {
add_action( 'wp_network_dashboard_setup', __NAMESPACE__ . '\\add_widget' );
}

function add_widget() {
/**
* Adds a new dashboard widget with the currently relevant log file.
*
* @return void
*/
function add_widget() :void {

if ( ! current_user_can( 'manage_sites' ) ) {
return;
Expand All @@ -48,10 +74,10 @@ function add_widget() {
*/
wp_add_dashboard_widget(
'cbstdsys-php-errorlog',
sprintf(
\sprintf(
'<span>%s Log (%s)</span>',
ucfirst( VIEW ),
'<abbr title="' . LOG . '">...' . FILE . '</abbr>'
\ucfirst( get_logfile_type() ),
'<abbr title="' . get_logfile_path() . '">...' . get_logfile_location() . '</abbr>'
),
__NAMESPACE__ . '\\render_widget'
);
Expand All @@ -64,11 +90,13 @@ function add_widget() {
* Reads and displays the first x (1000 by default) lines from php_error.log
* or debug.log if WP_DEBUG is enabled.
*
* @todo clean this up even more. This is still a mess from 1995.
* @todo clean this up even more. This is still a mess from 1995.
*
* @since 0.0.1
*
* @since 0.0.1
* @return void
*/
function render_widget() {
function render_widget() :void {

// The maximum number of errors to display in the widget.
$error_display_limit = 1000;
Expand All @@ -78,18 +106,28 @@ function render_widget() {

$file_cleared = false;

// Clear file?
$wp_debug_log = get_logfile_path();

if ( empty( $wp_debug_log ) ) {
return;
}

// Clear file.
// TODO #39 Fix "Processing form data without nonce verification." (WordPress.Security.NonceVerification.Recommended) !
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['cbstdsys-php-errorlog'] ) && $_GET['cbstdsys-php-errorlog'] === 'clear' ) {
$handle = fopen( LOG, 'w' );
fclose( $handle );
$file_cleared = true;
$handle = fopen( $wp_debug_log, 'w' );
if ( false !== $handle ) {
fclose( $handle );
$file_cleared = true;
}
}
// Read from file.
if ( ! file_exists( LOG ) ) {
if ( ! file_exists( $wp_debug_log ) ) {
echo '<p><em>There was a problem reading the error log file.</em></p>';
}

$errors = file( LOG );
$errors = (array) file( $wp_debug_log );
$errors = array_reverse( $errors );

if ( $file_cleared ) {
Expand All @@ -101,9 +139,10 @@ function render_widget() {
}

echo '<p>' . count( $errors ) . ' error';
if ( $errors !== 1 ) {
if ( count( $errors ) > 1 ) {
echo 's';
}

echo '.';

echo ' [ <b><a href="?cbstdsys-php-errorlog=clear" onclick="return;">CLEAR LOG FILE</a></b> ]';
Expand All @@ -114,14 +153,16 @@ function render_widget() {

$i = 0;
foreach ( $errors as $error ) {
$error = esc_html( $error );
$error = esc_html( (string) $error );
echo '<li style="padding:2px 4px 6px;border-bottom:1px solid #ececec;">';
$error_output = preg_replace( '/\[([^\]]+)\]/', '<b>[$1]</b>', $error, 1 );

$error_output = (string) preg_replace( '/\[([^\]]+)\]/', '<b>[$1]</b>', $error, 1 );
if ( strlen( $error_output ) > $error_length_limit ) {
$error_output = substr( $error_output, 0, $error_length_limit ) . ' [&hellip;]';
}
echo esc_html( balanceTags( $error_output, true ) );
echo '</li>';

$i++;
if ( $i > $error_display_limit ) {
echo esc_html( '<li class="howto">More than ' . $error_display_limit . ' errors in log...</li>' );
Expand All @@ -130,3 +171,4 @@ function render_widget() {
}
echo '</ol></div>';
}

6 changes: 3 additions & 3 deletions inc/mode/error-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$request_uri = getenv( 'REQUEST_URI' );

// Information protocol of incoming request.
if ( ! isset( $server_protocol ) ) {
if ( empty( $server_protocol ) ) {
$server_protocol = 'HTTP/1.1';
}
defined( 'FT_ERROR_STATUS' ) || define( 'FT_ERROR_STATUS', '503 Service Temporarily Unavailable' );
Expand Down Expand Up @@ -67,7 +67,7 @@
}

// Server name.
if ( isset( $server_name ) ) {
if ( ! empty( $server_name ) ) {
$server_name = filter_var(
stripslashes(
$server_name
Expand All @@ -79,7 +79,7 @@
}

// Request URI.
if ( isset( $request_uri ) ) {
if ( ! empty( $request_uri ) ) {
$request_uri = filter_var(
stripslashes(
$request_uri
Expand Down
16 changes: 7 additions & 9 deletions inc/mode/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Figuren_Theater\Maintenance\Mode;

use FT_MAINTENANCE_MODE;

use function add_action;
use function add_filter;
use function current_user_can;
Expand All @@ -25,7 +23,7 @@
*/
function bootstrap() :void {

if ( defined( 'FT_MAINTENANCE_MODE' ) && FT_MAINTENANCE_MODE ) {
if ( defined( 'FT_MAINTENANCE_MODE' ) && \constant( 'FT_MAINTENANCE_MODE' ) ) {
add_action( 'set_current_user', __NAMESPACE__ . '\\load', -1000 );
}

Expand Down Expand Up @@ -67,14 +65,14 @@ function load_plugins() {
/**
* Add note to [maintenance, php-error, db-error].php on dropins list
*
* @param array $meta Meta links
* @param string $file Plugin filename (sunrise.php for sunrise)
* @param array $data Data from the plugin header
* @param string $status Status of the plugin
* @param string[] $meta Meta links
* @param string $file Plugin filename (sunrise.php for sunrise)
* @param string[] $data Data from the plugin header
* @param string $status Status of the plugin
*
* @return array Modified meta links
* @return string[] Modified meta links
*/
function output_dropin_note( $meta, $file, $data, $status ) {
function output_dropin_note( array $meta, string $file, array $data, string $status ) :array {
if ( 'dropins' !== $status ) {
return $meta;
}
Expand Down
53 changes: 43 additions & 10 deletions inc/wp-db-backup/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ function( string $tablename ) : string {
/**
* Shedule WP_Cron for DB backups
*
* Saves the scheduled time for a database backup cron job.
*
* Because none of the normal Admins is
* (per design of the plugin)
* allowed to do backups manually.
Expand All @@ -180,24 +182,55 @@ function( string $tablename ) : string {
* Mainly cloned from the inside of
* wpdbBackup->save_backup_time()
* located at ...plugins\wp-db-backup\wp-db-backup.php
*
* This function unschedules the previous cron job for database backup and then schedules
* a new cron job for the next day based on the registered datetime of the site. The recurrence
* interval for the cron job is retrieved from the 'wp_cron_backup_schedule' option. If scheduling
* the new cron job is successful, the function returns true. If an exception occurs during the
* scheduling process, an error action is triggered and false is returned.
*
* @return void
*/
function save_backup_time() {
// Unschedule the previous cron.
function save_backup_time() :void {
// Unschedule the previous cron job for database backup.
wp_clear_scheduled_hook( 'wp_db_backup_cron' );

$tomorrow_date = date( 'Y-m-d', strtotime( 'tomorrow' ) );
$registered_datetime = get_blog_details( null, false )->registered;
// Get the site details using get_blog_details.
$site_details = get_blog_details( null, false );

$registered_datetime = explode( ' ', $registered_datetime );
// Check if the site_details is a WP_Site object,
// otherwise exit early.
if ( ! $site_details instanceof \WP_Site ) {
return;
}

$timestamp = strtotime( $tomorrow_date . $registered_datetime[1] );
$recurrence = get_option( 'wp_cron_backup_schedule' );
// Get the registered datetime of the site as a string.
$registered_datetime_string = $site_details->registered;

try {
return wp_schedule_event( $timestamp, $recurrence, 'wp_db_backup_cron' );
// Create a DateTime object from the registered datetime string.
$registered_datetime = new \DateTime( $registered_datetime_string );

// Get tomorrow's date and time for the scheduled backup.
$tomorrow = new \DateTime( 'tomorrow' );

// Set the time component of registered datetime to tomorrow's time.
$timestamp = $tomorrow->setTime(
(int) $registered_datetime->format( 'H' ),
(int) $registered_datetime->format( 'i' )
);

// Calculate the timestamp for the next scheduled backup.
$timestamp = $timestamp->getTimestamp();

// Get the recurrence interval for the cron job.
$recurrence = (string) get_option( 'wp_cron_backup_schedule' );

try {
// Schedule a new cron job for the specified time and recurrence.
wp_schedule_event( $timestamp, $recurrence, 'wp_db_backup_cron' );
} catch ( Exception $wp_error ) {
// If an exception occurs, trigger an error action and return false.
do_action( 'qm/error', $wp_error ); // See https://querymonitor.com/docs/logging-variables/ for more examples.
}

}

0 comments on commit 25e4dcc

Please sign in to comment.