PluginProbe
Core Framework / trunk
Core Framework vtrunk
2.0.2 2.0.1 2.0.0 1.10.4 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.3.10 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.1.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.8.0 All 32 releases
core-framework / wp / Common / Utils / Errors.php

Errors.php in Core Framework trunk, at wp/Common/Utils/Errors.php

173 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * CoreFramework
4 *
5 * @package CoreFramework
6 * @author Core Framework <hello@coreframework.com>
7 * @copyright 2023 Core Framework
8 * @license MIT
9 * @link https://coreframework.com
10 */
11
12 declare (strict_types = 1);
13
14 namespace CoreFramework\Common\Utils;
15
16 use CoreFramework\Config\Plugin;
17
18 /**
19 * Utility to show prettified wp_die errors, write debug logs as
20 * string or array and to deactivate plugin and print a notice
21 *
22 * @package CoreFramework\Common\Utils
23 * @since 0.0.0
24 */
25 class Errors {
26
27 /**
28 * Get the plugin data in static form
29 *
30 * @since 0.0.0
31 */
32 public static function getPluginData(): array {
33 return Plugin::init()->data();
34 }
35
36 /**
37 * Prettified wp_die error function
38 *
39 * @param $message : The error message
40 * @param string $subtitle : Specified title of the error
41 * @param string $source : File source of the error
42 * @param string $title : General title of the error
43 * @param string $exception
44 * @since 0.0.0
45 */
46 public static function wpDie( $message = '', $subtitle = '', $source = '', $exception = '', $title = '' ): void {
47 if ( $message ) {
48 $plugin = self::getPluginData();
49 $title = $title ?: $plugin['name'] .
50 ' ' .
51 $plugin['version'] .
52 ' ' .
53 \__( '› Fatal Error', 'core-framework' );
54 self::writeLog(
55 array(
56 'title' => $title . ' - ' . $subtitle,
57 'message' => $message,
58 'source' => $source,
59 'exception' => $exception,
60 )
61 );
62 $source = $source
63 ? sprintf(
64 '<code>%s</code><br><br>',
65 esc_html( sprintf( /* translators: %s: file path */ \__( 'Error source: %s', 'core-framework' ), $source ) )
66 )
67 : '';
68 \wp_die(
69 \wp_kses_post(
70 sprintf(
71 '<h1>%s<br><small>%s</small></h1>%s<hr><p>%s</p>',
72 esc_html( $title ),
73 esc_html( $subtitle ),
74 $source,
75 esc_html( $message )
76 )
77 ),
78 esc_html( $title )
79 );
80 } else {
81 \wp_die();
82 }
83 }
84
85 /**
86 * De-activates the plugin and shows notice error in back-end
87 *
88 * @param $message : The error message
89 * @param string $subtitle : Specified title of the error
90 * @param string $source : File source of the error
91 * @param string $title : General title of the error
92 * @param string $exception
93 * @since 0.0.0
94 */
95 public static function pluginDie(
96 $message = '',
97 $subtitle = '',
98 $source = '',
99 $exception = '',
100 $title = ''
101 ): void {
102 if ( $message ) {
103 $plugin = self::getPluginData();
104 $title = $title ?: $plugin['name'] .
105 ' ' .
106 $plugin['version'] .
107 ' ' .
108 \__( '› Plugin Disabled', 'core-framework' );
109 self::writeLog(
110 array(
111 'title' => $title . ' - ' . $subtitle,
112 'message' => $message,
113 'source' => $source,
114 'exception' => $exception,
115 )
116 );
117 $source = $source
118 ? '<small>' . esc_html( sprintf( /* translators: %s: file path */ \__( 'Error source: %s', 'core-framework' ), $source ) ) . '</small> - '
119 : '';
120 $footer = $source . '<a href="' . esc_url( $plugin['uri'] ) . '"><small>' . esc_html( $plugin['uri'] ) . '</small></a>';
121 $error = sprintf(
122 '<strong><h3>%s</h3>%s</strong><p>%s</p><hr><p>%s</p>',
123 esc_html( $title ),
124 esc_html( $subtitle ),
125 esc_html( $message ),
126 $footer
127 );
128 global $core_framework_die_notice;
129 $core_framework_die_notice = $error;
130 \add_action(
131 'admin_notices',
132 static function (): void {
133 global $core_framework_die_notice;
134 echo \wp_kses_post( sprintf( '<div class="notice notice-error"><p>%s</p></div>', $core_framework_die_notice ) );
135 }
136 );
137 }
138
139 \add_action(
140 'admin_init',
141 static function (): void {
142 \deactivate_plugins( CORE_FRAMEWORK_MAIN_FILE );
143 // This only removes WordPress's activation flag after deactivation; no input is consumed.
144 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
145 if ( isset( $_GET['activate'] ) ) {
146 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
147 unset( $_GET['activate'] );
148 }
149 }
150 );
151 }
152
153 /**
154 * Writes a log if wp_debug is enables
155 *
156 * @param $log
157 * @since 0.0.0
158 */
159 public static function writeLog( $log ): void {
160 if ( true === WP_DEBUG ) {
161 if ( is_array( $log ) || is_object( $log ) ) {
162 $log = wp_json_encode( $log );
163 }
164
165 if ( is_string( $log ) ) {
166 // This method exists only for explicit diagnostics when WP_DEBUG is enabled.
167 // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
168 error_log( $log );
169 }
170 }
171 }
172 }
173