Admin.php
1 month ago
AdminEnqueue.php
1 month ago
AdminHelp.php
1 month ago
AdminMenu.php
1 month ago
PluginActionLinks.php
3 months ago
SupportExport.php
2 months ago
Admin.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OnlineOptimisation\EmailEncoderBundle\Admin; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | use OnlineOptimisation\EmailEncoderBundle\Traits\PluginHelper; |
| 8 | |
| 9 | class Admin |
| 10 | { |
| 11 | use PluginHelper; |
| 12 | |
| 13 | /** @var array< string > $display_notices */ |
| 14 | public static array $display_notices = []; |
| 15 | |
| 16 | |
| 17 | public function boot(): void |
| 18 | { |
| 19 | ( new AdminEnqueue() )->boot(); |
| 20 | ( new AdminMenu() )->boot(); // AdminHelp is added here |
| 21 | ( new PluginActionLinks() )->boot(); |
| 22 | |
| 23 | add_action( 'init', [ $this, 'register_hooks' ] ); |
| 24 | } |
| 25 | |
| 26 | # ADMIN METHODS ============================================================ |
| 27 | |
| 28 | public function register_hooks(): void |
| 29 | { |
| 30 | add_action( 'admin_init', [ $this, 'save_settings_admin' ] ); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | public function save_settings_admin(): void |
| 38 | { |
| 39 | // $this->log( __METHOD__ ); |
| 40 | if ( !isset( $_POST[ $this->getPageName() . '_nonce' ] ) ) { |
| 41 | return; |
| 42 | }; |
| 43 | |
| 44 | if ( ! wp_verify_nonce( $_POST[ $this->getPageName() . '_nonce' ], $this->getPageName() ) ) { |
| 45 | wp_die( esc_html__( 'You don\'t have permission to update these settings.', 'email-encoder-bundle' ) ); |
| 46 | } |
| 47 | |
| 48 | if ( ! current_user_can( $this->getAdminCap( 'admin-update-settings' ) ) ) { |
| 49 | wp_die( esc_html__( 'You don\'t have permission to update these settings.', 'email-encoder-bundle' ) ); |
| 50 | } |
| 51 | |
| 52 | $raw = wp_unslash( $_POST ); |
| 53 | |
| 54 | if ( isset( $raw[ $this->getSettingsKey() ] ) && is_array( $raw[ $this->getSettingsKey() ] ) ) { |
| 55 | |
| 56 | //Strip duplicate slashes before saving |
| 57 | foreach ( $raw[ $this->getSettingsKey() ] as $k => $v ) { |
| 58 | if ( is_string( $v ) ) { |
| 59 | $raw[ $this->getSettingsKey() ][ $k ] = $this->sanitise( $v, $k ); |
| 60 | // $this->log( $raw[ $this->getSettingsKey() ][ $k ] ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // $this->log( $this->getSettingsKey() ); |
| 65 | $check = update_option( $this->getSettingsKey(), $raw[ $this->getSettingsKey() ] ); |
| 66 | |
| 67 | if ( $check ) { |
| 68 | $this->reloadSettings(); |
| 69 | $update_notice = $this->helper()->create_admin_notice( 'Settings successfully saved.', 'success', true ); |
| 70 | } else { |
| 71 | $update_notice = $this->helper()->create_admin_notice( 'No changes were made to your settings with your last save.', 'info', true ); |
| 72 | } |
| 73 | |
| 74 | // PRG redirect: toggling `own_admin_menu` moves the page between |
| 75 | // `options-general.php` and `admin.php`, so the POST URL can no |
| 76 | // longer match a registered menu — leaving WP's `$title` null and |
| 77 | // tripping a strip_tags() deprecation in admin-header.php. Stash |
| 78 | // the notice in a per-user transient and bounce to the canonical URL. |
| 79 | set_transient( $this->getNoticeTransientKey(), $update_notice, 30 ); |
| 80 | wp_safe_redirect( $this->getCanonicalAdminPageUrl() ); |
| 81 | exit; |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public function maybe_consume_redirect_notice(): void |
| 88 | { |
| 89 | $key = $this->getNoticeTransientKey(); |
| 90 | $notice = get_transient( $key ); |
| 91 | if ( $notice ) { |
| 92 | delete_transient( $key ); |
| 93 | self::$display_notices[] = (string) $notice; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | private function getNoticeTransientKey(): string |
| 99 | { |
| 100 | return 'eeb_admin_notice_' . get_current_user_id(); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | private function getCanonicalAdminPageUrl(): string |
| 105 | { |
| 106 | $is_top_level = (string) $this->getSetting( 'own_admin_menu', true ) === '1'; |
| 107 | $base = $is_top_level ? 'admin.php' : 'options-general.php'; |
| 108 | return admin_url( $base . '?page=' . $this->getPageName() ); |
| 109 | } |
| 110 | |
| 111 | protected function sanitise( string $value, ?string $key = null ): string |
| 112 | { |
| 113 | // if ( $key == 'protection_text' ) { |
| 114 | // $this->log( [ |
| 115 | // 'k' => $key, |
| 116 | // 'v' => $value, |
| 117 | // // 'config' => $this->getSetting( $key ), |
| 118 | // ] ); |
| 119 | // } |
| 120 | |
| 121 | return sanitize_text_field( $value ); |
| 122 | } |
| 123 | |
| 124 | } |
| 125 |