Admin.php
5 months ago
AdminEnqueue.php
5 months ago
AdminHelp.php
5 months ago
AdminMenu.php
5 months ago
AdminMetaBox.php
5 months ago
PluginActionLinks.php
5 months ago
Admin.php
90 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OnlineOptimisation\EmailEncoderBundle\Admin; |
| 4 | |
| 5 | use OnlineOptimisation\EmailEncoderBundle\Traits\PluginHelper; |
| 6 | |
| 7 | class Admin |
| 8 | { |
| 9 | use PluginHelper; |
| 10 | |
| 11 | public static array $display_notices = []; |
| 12 | |
| 13 | |
| 14 | public function boot(): void |
| 15 | { |
| 16 | ( new AdminEnqueue() )->boot(); |
| 17 | ( new AdminMenu() )->boot(); // AdminMetaBox & AdminHelp are added here |
| 18 | ( new PluginActionLinks() )->boot(); |
| 19 | |
| 20 | add_action( 'init', [ $this, 'register_hooks' ] ); |
| 21 | } |
| 22 | |
| 23 | # ADMIN METHODS ============================================================ |
| 24 | |
| 25 | public function register_hooks() |
| 26 | { |
| 27 | add_action( 'admin_init', [ $this, 'save_settings_admin' ] ); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | public function save_settings_admin() |
| 35 | { |
| 36 | // $this->log( __METHOD__ ); |
| 37 | if ( !isset( $_POST[ $this->getPageName() . '_nonce' ] ) ) { |
| 38 | return; |
| 39 | }; |
| 40 | |
| 41 | if ( ! wp_verify_nonce( $_POST[ $this->getPageName() . '_nonce' ], $this->getPageName() ) ) { |
| 42 | wp_die( __( 'You don\'t have permission to update these settings.', 'email-encoder-bundle' ) ); |
| 43 | } |
| 44 | |
| 45 | if ( ! current_user_can( $this->getAdminCap( 'admin-update-settings' ) ) ) { |
| 46 | wp_die( __( 'You don\'t have permission to update these settings.', 'email-encoder-bundle' ) ); |
| 47 | } |
| 48 | |
| 49 | $raw = wp_unslash( $_POST ); |
| 50 | |
| 51 | if ( isset( $raw[ $this->getSettingsKey() ] ) && is_array( $raw[ $this->getSettingsKey() ] ) ) { |
| 52 | |
| 53 | //Strip duplicate slashes before saving |
| 54 | foreach ( $raw[ $this->getSettingsKey() ] as $k => $v ) { |
| 55 | if ( is_string( $v ) ) { |
| 56 | $raw[ $this->getSettingsKey() ][ $k ] = $this->sanitise( $v, $k ); |
| 57 | // $this->log( $raw[ $this->getSettingsKey() ][ $k ] ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // $this->log( $this->getSettingsKey() ); |
| 62 | $check = update_option( $this->getSettingsKey(), $raw[ $this->getSettingsKey() ] ); |
| 63 | |
| 64 | if ( $check ) { |
| 65 | $this->reloadSettings(); |
| 66 | $update_notice = $this->helper()->create_admin_notice( 'Settings successfully saved.', 'success', true ); |
| 67 | self::$display_notices[] = $update_notice; |
| 68 | } else { |
| 69 | $update_notice = $this->helper()->create_admin_notice( 'No changes were made to your settings with your last save.', 'info', true ); |
| 70 | self::$display_notices[] = $update_notice; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | protected function sanitise( string $value, ?string $key = null ): string |
| 77 | { |
| 78 | // if ( $key == 'protection_text' ) { |
| 79 | // $this->log( [ |
| 80 | // 'k' => $key, |
| 81 | // 'v' => $value, |
| 82 | // // 'config' => $this->getSetting( $key ), |
| 83 | // ] ); |
| 84 | // } |
| 85 | |
| 86 | return sanitize_text_field( $value ); |
| 87 | } |
| 88 | |
| 89 | } |
| 90 |