| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin-specific notice handlers |
| 4 |
* |
| 5 |
* Registers built-in admin notices using the WINP_Notices system. |
| 6 |
* |
| 7 |
* @package Woody_Code_Snippets |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WINP_Admin_Notices |
| 17 |
*/ |
| 18 |
class WINP_Admin_Notices { |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
add_action( 'admin_init', [ $this, 'register_notices' ] ); |
| 25 |
add_action( 'admin_init', [ $this, 'register_pending_error_notices' ] ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register pending error notices from frontend fatal errors |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public function register_pending_error_notices() { |
| 34 |
$pending_notices = get_option( 'winp_pending_error_notices', [] ); |
| 35 |
|
| 36 |
if ( ! is_array( $pending_notices ) || empty( $pending_notices ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// Get dismissed notices. |
| 41 |
$dismissed_notices = get_option( 'winp_dismissed_notices', [] ); |
| 42 |
if ( ! is_array( $dismissed_notices ) ) { |
| 43 |
$dismissed_notices = []; |
| 44 |
} |
| 45 |
|
| 46 |
// Register each pending notice that hasn't been dismissed. |
| 47 |
$updated = false; |
| 48 |
foreach ( $pending_notices as $notice_id => $notice_data ) { |
| 49 |
// Check if this notice was dismissed (full ID with prefix). |
| 50 |
$full_notice_id = 'winp_notice_' . $notice_id; |
| 51 |
|
| 52 |
// Check if dismissed and not expired. |
| 53 |
$is_dismissed = false; |
| 54 |
if ( isset( $dismissed_notices[ $full_notice_id ] ) ) { |
| 55 |
$expires = (int) $dismissed_notices[ $full_notice_id ]; |
| 56 |
$is_dismissed = 0 === $expires || $expires > time(); |
| 57 |
} |
| 58 |
|
| 59 |
if ( $is_dismissed ) { |
| 60 |
// Notice was dismissed, remove it from pending list. |
| 61 |
unset( $pending_notices[ $notice_id ] ); |
| 62 |
$updated = true; |
| 63 |
} else { |
| 64 |
// Not dismissed yet, register it. |
| 65 |
WINP_Notices::add_notice( |
| 66 |
$notice_id, |
| 67 |
$notice_data['message'], |
| 68 |
$notice_data['type'], |
| 69 |
true, |
| 70 |
0 |
| 71 |
); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Update option if any notices were removed. |
| 76 |
if ( $updated ) { |
| 77 |
if ( empty( $pending_notices ) ) { |
| 78 |
delete_option( 'winp_pending_error_notices' ); |
| 79 |
} else { |
| 80 |
update_option( 'winp_pending_error_notices', $pending_notices, false ); |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Register built-in admin notices |
| 87 |
* |
| 88 |
* @return void |
| 89 |
*/ |
| 90 |
public function register_notices() { |
| 91 |
// Security warning for PHP snippets. |
| 92 |
WINP_Notices::add_notice( |
| 93 |
'warning_security_notice', |
| 94 |
$this->get_warning_notice(), |
| 95 |
'warning', |
| 96 |
true, |
| 97 |
0, |
| 98 |
[ |
| 99 |
'post', |
| 100 |
'post-new', |
| 101 |
'edit', |
| 102 |
] |
| 103 |
); |
| 104 |
|
| 105 |
// Safe mode notice. |
| 106 |
WINP_Notices::add_notice( |
| 107 |
'safe_mode', |
| 108 |
$this->get_safe_mode_notice(), |
| 109 |
'warning', |
| 110 |
false |
| 111 |
); |
| 112 |
|
| 113 |
// Snippet error notice. |
| 114 |
WINP_Notices::add_notice( |
| 115 |
'result_error', |
| 116 |
$this->get_throw_php_error_notice(), |
| 117 |
'error', |
| 118 |
false, |
| 119 |
0, |
| 120 |
[ |
| 121 |
'post', |
| 122 |
'post-new', |
| 123 |
'edit', |
| 124 |
] |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Security warning for PHP snippet editing |
| 130 |
* |
| 131 |
* @return string|null |
| 132 |
*/ |
| 133 |
private function get_warning_notice() { |
| 134 |
$current_screen = get_current_screen(); |
| 135 |
|
| 136 |
if ( ! $current_screen || 'post' !== $current_screen->base || ( WINP_SNIPPETS_POST_TYPE !== $current_screen->post_type ) ) { |
| 137 |
return null; |
| 138 |
} |
| 139 |
|
| 140 |
$snippet_type = WINP_Helper::get_snippet_type(); |
| 141 |
|
| 142 |
if ( WINP_SNIPPET_TYPE_PHP !== $snippet_type ) { |
| 143 |
return null; |
| 144 |
} |
| 145 |
|
| 146 |
$notice = '<b>' . __( 'Woody Code Snippets', 'insert-php' ) . '</b>: ' . __( 'Custom PHP snippets run on your site. Test thoroughly before activating. Consider using Safe Mode to preview changes.', 'insert-php' ); |
| 147 |
return '<p>' . $notice . '</p>'; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Error notification after saving snippet |
| 152 |
* |
| 153 |
* @return string|null |
| 154 |
*/ |
| 155 |
private function get_throw_php_error_notice() { |
| 156 |
$save_snippet_result = WINP_HTTP::get( 'wbcr_inp_save_snippet_result' ); |
| 157 |
$post_id = WINP_HTTP::get( 'post' ); |
| 158 |
|
| 159 |
if ( ! empty( $save_snippet_result ) && 'code-error' == $save_snippet_result ) { |
| 160 |
$post_id = ! empty( $post_id ) ? intval( $post_id ) : null; |
| 161 |
|
| 162 |
if ( $post_id ) { |
| 163 |
$error = WINP_Plugin::app()->get_execute_object()->getSnippetError( $post_id ); |
| 164 |
|
| 165 |
if ( is_array( $error ) ) { |
| 166 |
// translators: 1: line number, 2: error message. |
| 167 |
return sprintf( '<p>%s</p><p><strong>%s</strong></p>', sprintf( __( 'The snippet has been deactivated due to an error on line %d:', 'insert-php' ), $error['line'] ), $error['message'] ); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return null; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Safe mode reminder notification |
| 177 |
* |
| 178 |
* @return string|null |
| 179 |
*/ |
| 180 |
private function get_safe_mode_notice() { |
| 181 |
if ( ! WINP_Helper::is_safe_mode() ) { |
| 182 |
return null; |
| 183 |
} |
| 184 |
|
| 185 |
$disable_safe_mode_url = esc_url( add_query_arg( [ 'wbcr-php-snippets-disable-safe-mode' => 1 ] ) ); |
| 186 |
|
| 187 |
$notice = __( 'Woody Code Snippets', 'insert-php' ) . ': ' . __( 'Safe Mode is active. All snippets are temporarily disabled. You can re-enable them after fixing any issues.', 'insert-php' ); |
| 188 |
$notice .= ' <a href="' . $disable_safe_mode_url . '" class="button button-default">' . __( 'Disable Safe Mode', 'insert-php' ) . '</a>'; |
| 189 |
|
| 190 |
return '<p>' . $notice . '</p>'; |
| 191 |
} |
| 192 |
} |
| 193 |
|