* @copyright 2023 Core Framework * @license MIT * @link https://coreframework.com */ declare (strict_types = 1); namespace CoreFramework\Common\Utils; use CoreFramework\Config\Plugin; /** * Utility to show prettified wp_die errors, write debug logs as * string or array and to deactivate plugin and print a notice * * @package CoreFramework\Common\Utils * @since 0.0.0 */ class Errors { /** * Get the plugin data in static form * * @since 0.0.0 */ public static function getPluginData(): array { return Plugin::init()->data(); } /** * Prettified wp_die error function * * @param $message : The error message * @param string $subtitle : Specified title of the error * @param string $source : File source of the error * @param string $title : General title of the error * @param string $exception * @since 0.0.0 */ public static function wpDie( $message = '', $subtitle = '', $source = '', $exception = '', $title = '' ): void { if ( $message ) { $plugin = self::getPluginData(); $title = $title ?: $plugin['name'] . ' ' . $plugin['version'] . ' ' . \__( '› Fatal Error', 'core-framework' ); self::writeLog( array( 'title' => $title . ' - ' . $subtitle, 'message' => $message, 'source' => $source, 'exception' => $exception, ) ); $source = $source ? sprintf( '%s

', esc_html( sprintf( /* translators: %s: file path */ \__( 'Error source: %s', 'core-framework' ), $source ) ) ) : ''; \wp_die( \wp_kses_post( sprintf( '

%s
%s

%s

%s

', esc_html( $title ), esc_html( $subtitle ), $source, esc_html( $message ) ) ), esc_html( $title ) ); } else { \wp_die(); } } /** * De-activates the plugin and shows notice error in back-end * * @param $message : The error message * @param string $subtitle : Specified title of the error * @param string $source : File source of the error * @param string $title : General title of the error * @param string $exception * @since 0.0.0 */ public static function pluginDie( $message = '', $subtitle = '', $source = '', $exception = '', $title = '' ): void { if ( $message ) { $plugin = self::getPluginData(); $title = $title ?: $plugin['name'] . ' ' . $plugin['version'] . ' ' . \__( '› Plugin Disabled', 'core-framework' ); self::writeLog( array( 'title' => $title . ' - ' . $subtitle, 'message' => $message, 'source' => $source, 'exception' => $exception, ) ); $source = $source ? '' . esc_html( sprintf( /* translators: %s: file path */ \__( 'Error source: %s', 'core-framework' ), $source ) ) . ' - ' : ''; $footer = $source . '' . esc_html( $plugin['uri'] ) . ''; $error = sprintf( '

%s

%s

%s


%s

', esc_html( $title ), esc_html( $subtitle ), esc_html( $message ), $footer ); global $core_framework_die_notice; $core_framework_die_notice = $error; \add_action( 'admin_notices', static function (): void { global $core_framework_die_notice; echo \wp_kses_post( sprintf( '

%s

', $core_framework_die_notice ) ); } ); } \add_action( 'admin_init', static function (): void { \deactivate_plugins( CORE_FRAMEWORK_MAIN_FILE ); // This only removes WordPress's activation flag after deactivation; no input is consumed. // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( isset( $_GET['activate'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended unset( $_GET['activate'] ); } } ); } /** * Writes a log if wp_debug is enables * * @param $log * @since 0.0.0 */ public static function writeLog( $log ): void { if ( true === WP_DEBUG ) { if ( is_array( $log ) || is_object( $log ) ) { $log = wp_json_encode( $log ); } if ( is_string( $log ) ) { // This method exists only for explicit diagnostics when WP_DEBUG is enabled. // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log error_log( $log ); } } } }