AddFormNotice.php
171 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Admin\Notices; |
| 4 | |
| 5 | use Hostinger\Reach\Api\Handlers\ReachApiHandler; |
| 6 | use Hostinger\Reach\Functions; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | class AddFormNotice { |
| 11 | |
| 12 | public const NOTICE_DISMISS_TRANSIENT = self::NOTICE_NAME . '_dismissed'; |
| 13 | private const NOTICE_NAME = 'hostinger_reach_add_form_notice'; |
| 14 | private const NOTICE_ACTION = self::NOTICE_NAME . '_action'; |
| 15 | |
| 16 | |
| 17 | private ReachApiHandler $reach_api_handler; |
| 18 | private Functions $functions; |
| 19 | |
| 20 | public function __construct( ReachApiHandler $reach_api_handler, Functions $functions ) { |
| 21 | $this->reach_api_handler = $reach_api_handler; |
| 22 | $this->functions = $functions; |
| 23 | } |
| 24 | |
| 25 | public function init(): void { |
| 26 | add_action( 'admin_notices', array( $this, 'display_notice' ) ); |
| 27 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); |
| 28 | add_action( 'wp_ajax_' . self::NOTICE_ACTION, array( $this, 'handle_ajax_action' ) ); |
| 29 | } |
| 30 | |
| 31 | public function display_notice(): void { |
| 32 | if ( $this->should_render() ) { |
| 33 | $this->render(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | public function enqueue_admin_assets(): void { |
| 38 | if ( $this->should_render() ) { |
| 39 | $this->enqueue_notice_assets(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function handle_ajax_action(): void { |
| 44 | check_ajax_referer( self::NOTICE_ACTION, 'nonce' ); |
| 45 | $choice = sanitize_text_field( $_POST['choice'] ); |
| 46 | |
| 47 | switch ( $choice ) { |
| 48 | case 'success': |
| 49 | $this->handle_success(); |
| 50 | |
| 51 | break; |
| 52 | case 'dismiss': |
| 53 | $this->handle_dismiss(); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private function handle_dismiss(): void { |
| 59 | set_transient( self::NOTICE_DISMISS_TRANSIENT, true, WEEK_IN_SECONDS ); |
| 60 | wp_send_json_success(); |
| 61 | } |
| 62 | |
| 63 | private function handle_success(): void { |
| 64 | wp_send_json_success( |
| 65 | array( |
| 66 | 'redirect_url' => 'admin.php?page=hostinger-reach&addForm=1', |
| 67 | ) |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | private function render(): void { |
| 72 | ?> |
| 73 | <div style="background-image: url('<?php echo esc_url( $this->functions->get_asset_url( 'images/notices/notice-bg.png' ) ); ?>');" id="hostinger-reach-add-form-notice" class="notice notice-info is-dismissible hostinger-reach-notice"> |
| 74 | <div data-action="dismiss" class="hostinger-reach-action-button hostinger-reach-notice-close"></div> |
| 75 | <div class="hostinger-reach-notice-wrap"> |
| 76 | <div class="hostinger-reach-notice-main"> |
| 77 | <div class="hostinger-reach-notice-content"> |
| 78 | <h3><?php esc_html_e( 'Add form to start sending campaigns with Hostinger Reach', 'hostinger-reach' ); ?></h3> |
| 79 | <p><?php esc_html_e( 'Easily add or connect existing forms, automate WooCommerce, and sync contacts to start building campaigns and grow your business.', 'hostinger-reach' ); ?></p> |
| 80 | </div> |
| 81 | <div class="hostinger-reach-notice-actions"> |
| 82 | <button data-action="success" class="hostinger-reach-button hostinger-reach-action-button button button-primary"> |
| 83 | <?php esc_html_e( 'Add form', 'hostinger-reach' ); ?> |
| 84 | </button> |
| 85 | </div> |
| 86 | </div> |
| 87 | <div class="hostinger-reach-notice-aside"> |
| 88 | <img |
| 89 | width="255" |
| 90 | alt="<?php esc_html_e( 'Hostinger Reach', 'hostinger-reach' ); ?>" |
| 91 | class="hostinger-reach-notice-image" |
| 92 | src="<?php echo esc_url( $this->functions->get_asset_url( 'images/notices/add-form-notice.png' ) ); ?>" |
| 93 | /> |
| 94 | </div> |
| 95 | </div> |
| 96 | </div> |
| 97 | <?php |
| 98 | } |
| 99 | |
| 100 | private function should_render(): bool { |
| 101 | $screen = get_current_screen(); |
| 102 | |
| 103 | if ( ! $screen || ! in_array( |
| 104 | $screen->id, |
| 105 | array( |
| 106 | 'dashboard', |
| 107 | 'pages', |
| 108 | 'posts', |
| 109 | 'edit-page', |
| 110 | 'edit-post', |
| 111 | ), |
| 112 | true |
| 113 | ) ) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | if ( $this->is_dismissed() || ! $this->is_connected() || $this->has_added_forms() ) { |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | return true; |
| 122 | } |
| 123 | |
| 124 | private function has_added_forms(): bool { |
| 125 | return (bool) get_option( Functions::HOSTINGER_REACH_HAS_FORMS, false ); |
| 126 | } |
| 127 | |
| 128 | private function is_dismissed(): bool { |
| 129 | return (bool) get_transient( self::NOTICE_DISMISS_TRANSIENT ); |
| 130 | } |
| 131 | |
| 132 | private function is_connected(): bool { |
| 133 | return $this->reach_api_handler->is_connected(); |
| 134 | } |
| 135 | |
| 136 | private function enqueue_notice_assets(): void { |
| 137 | $asset_name = 'add-form-notice'; |
| 138 | $js_file = $this->functions->get_frontend_dir() . $asset_name . '.js'; |
| 139 | $css_file = $this->functions->get_frontend_dir() . $asset_name . '.css'; |
| 140 | |
| 141 | if ( $js_file ) { |
| 142 | wp_enqueue_script( |
| 143 | $asset_name, |
| 144 | $this->functions->get_frontend_url() . $asset_name . '.js', |
| 145 | array(), |
| 146 | filemtime( $this->functions->get_frontend_dir() . $asset_name . '.js' ), |
| 147 | true |
| 148 | ); |
| 149 | |
| 150 | wp_localize_script( |
| 151 | $asset_name, |
| 152 | self::NOTICE_NAME . '_data', |
| 153 | array( |
| 154 | 'action' => self::NOTICE_ACTION, |
| 155 | 'nonce' => wp_create_nonce( self::NOTICE_ACTION ), |
| 156 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 157 | ) |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | if ( $css_file ) { |
| 162 | wp_enqueue_style( |
| 163 | $asset_name, |
| 164 | $this->functions->get_frontend_url() . $asset_name . '.css', |
| 165 | array(), |
| 166 | filemtime( $this->functions->get_frontend_dir() . $asset_name . '.css' ), |
| 167 | ); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 |