* @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
', 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
', 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