ConnectionNotice.php
191 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Admin\Notices; |
| 4 | |
| 5 | use Hostinger\Reach\Admin\Menus; |
| 6 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 7 | use Hostinger\Reach\Functions; |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | class ConnectionNotice { |
| 12 | |
| 13 | public const NOTICE_DISMISS_TRANSIENT = self::NOTICE_NAME . '_dismissed'; |
| 14 | private const NOTICE_NAME = 'hostinger_reach_connection_notice'; |
| 15 | private const NOTICE_ACTION = self::NOTICE_NAME . '_action'; |
| 16 | |
| 17 | |
| 18 | private ReachApiHandler $reach_api_handler; |
| 19 | private Functions $functions; |
| 20 | |
| 21 | public function __construct( ReachApiHandler $reach_api_handler, Functions $functions ) { |
| 22 | $this->reach_api_handler = $reach_api_handler; |
| 23 | $this->functions = $functions; |
| 24 | } |
| 25 | |
| 26 | public function init(): void { |
| 27 | add_action( 'admin_notices', array( $this, 'display_notice' ) ); |
| 28 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); |
| 29 | add_action( 'wp_ajax_' . self::NOTICE_ACTION, array( $this, 'handle_ajax_action' ) ); |
| 30 | } |
| 31 | |
| 32 | public function display_notice(): void { |
| 33 | if ( $this->should_render() ) { |
| 34 | $this->render(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public function enqueue_admin_assets(): void { |
| 39 | if ( $this->should_render() ) { |
| 40 | $this->enqueue_connection_notice_assets(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | public function handle_ajax_action(): void { |
| 45 | if ( ! current_user_can( 'manage_options' ) ) { |
| 46 | wp_die( -1, 403 ); |
| 47 | } |
| 48 | |
| 49 | check_ajax_referer( self::NOTICE_ACTION, 'nonce' ); |
| 50 | $choice = sanitize_text_field( $_POST['choice'] ); |
| 51 | |
| 52 | switch ( $choice ) { |
| 53 | case 'connect': |
| 54 | $this->handle_connect(); |
| 55 | |
| 56 | break; |
| 57 | case 'dismiss': |
| 58 | $this->handle_dismiss(); |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private function handle_dismiss(): void { |
| 64 | set_transient( self::NOTICE_DISMISS_TRANSIENT, true, WEEK_IN_SECONDS ); |
| 65 | wp_send_json_success(); |
| 66 | } |
| 67 | |
| 68 | private function handle_connect(): void { |
| 69 | $response = $this->reach_api_handler->post_generate_auth_url(); |
| 70 | $data = $response->get_data(); |
| 71 | $auth_url = $data['auth_url'] ?? ''; |
| 72 | |
| 73 | if ( empty( $auth_url ) ) { |
| 74 | wp_send_json_error( |
| 75 | array( |
| 76 | 'message' => __( 'Could not generate auth URL', 'hostinger-reach' ), |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | wp_send_json_success( |
| 82 | array( |
| 83 | 'redirect_url' => $auth_url, |
| 84 | ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | private function render(): void { |
| 89 | ?> |
| 90 | <div style="background-image: url('<?php echo esc_url( $this->functions->get_asset_url( 'images/notices/notice-bg.png' ) ); ?>');" id="hostinger-reach-connection-notice" class="notice notice-info is-dismissible hostinger-reach-notice"> |
| 91 | <div data-action="dismiss" class="hostinger-reach-action-button hostinger-reach-notice-close"></div> |
| 92 | <div class="hostinger-reach-notice-wrap"> |
| 93 | <div class="hostinger-reach-notice-main"> |
| 94 | <div class="hostinger-reach-notice-content"> |
| 95 | <h3><?php esc_html_e( 'Connect Hostinger Reach to start Email Marketing', 'hostinger-reach' ); ?></h3> |
| 96 | <p><?php esc_html_e( 'Send newsletters with professionally designed templates, automate campaigns, and build AI-powered segments. With Hostinger Reach, growing your audience is easy.', 'hostinger-reach' ); ?></p> |
| 97 | </div> |
| 98 | <div class="hostinger-reach-notice-actions"> |
| 99 | <button data-action="connect" class="hostinger-reach-button hostinger-reach-action-button button button-primary"> |
| 100 | <?php esc_html_e( 'Connect', 'hostinger-reach' ); ?> |
| 101 | </button> |
| 102 | <a href="<?php echo esc_url( Menus::get_reach_admin_url() ); ?>" class="button button-secondary hostinger-reach-button hostinger-reach-learn-more"> |
| 103 | <?php esc_html_e( 'Learn more', 'hostinger-reach' ); ?> |
| 104 | </a> |
| 105 | </div> |
| 106 | </div> |
| 107 | <div class="hostinger-reach-notice-aside"> |
| 108 | <img |
| 109 | width="255" |
| 110 | alt="<?php esc_html_e( 'Hostinger Reach', 'hostinger-reach' ); ?>" |
| 111 | class="hostinger-reach-notice-image" |
| 112 | src="<?php echo esc_url( $this->functions->get_asset_url( 'images/notices/connection-notice.png' ) ); ?>" |
| 113 | /> |
| 114 | </div> |
| 115 | </div> |
| 116 | </div> |
| 117 | <?php |
| 118 | } |
| 119 | |
| 120 | private function should_render(): bool { |
| 121 | if ( ! current_user_can( 'manage_options' ) ) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | $screen = get_current_screen(); |
| 126 | |
| 127 | if ( ! $screen || ! in_array( |
| 128 | $screen->id, |
| 129 | array( |
| 130 | 'dashboard', |
| 131 | 'pages', |
| 132 | 'posts', |
| 133 | 'edit-page', |
| 134 | 'edit-post', |
| 135 | ), |
| 136 | true |
| 137 | ) ) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if ( $this->is_dismissed() || $this->is_connected() ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | private function is_dismissed(): bool { |
| 149 | return (bool) get_transient( self::NOTICE_DISMISS_TRANSIENT ); |
| 150 | } |
| 151 | |
| 152 | private function is_connected(): bool { |
| 153 | return $this->reach_api_handler->is_connected(); |
| 154 | } |
| 155 | |
| 156 | private function enqueue_connection_notice_assets(): void { |
| 157 | $asset_name = 'connection-notice'; |
| 158 | $js_file = $this->functions->get_frontend_dir() . $asset_name . '.js'; |
| 159 | $css_file = $this->functions->get_frontend_dir() . $asset_name . '.css'; |
| 160 | |
| 161 | if ( $js_file ) { |
| 162 | wp_enqueue_script( |
| 163 | $asset_name, |
| 164 | $this->functions->get_frontend_url() . $asset_name . '.js', |
| 165 | array(), |
| 166 | filemtime( $this->functions->get_frontend_dir() . $asset_name . '.js' ), |
| 167 | true |
| 168 | ); |
| 169 | |
| 170 | wp_localize_script( |
| 171 | $asset_name, |
| 172 | self::NOTICE_NAME . '_data', |
| 173 | array( |
| 174 | 'action' => self::NOTICE_ACTION, |
| 175 | 'nonce' => wp_create_nonce( self::NOTICE_ACTION ), |
| 176 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 177 | ) |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | if ( $css_file ) { |
| 182 | wp_enqueue_style( |
| 183 | $asset_name, |
| 184 | $this->functions->get_frontend_url() . $asset_name . '.css', |
| 185 | array(), |
| 186 | filemtime( $this->functions->get_frontend_dir() . $asset_name . '.css' ), |
| 187 | ); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 |