ajax.php
3 weeks ago
install-notice.php
3 weeks ago
install-tracker.php
3 weeks ago
onboard-status.php
3 weeks ago
plugin-data-sender.php
1 year ago
plugin-installer.php
3 weeks ago
plugin-skin.php
1 year ago
plugin-status.php
4 years ago
utils.php
1 year ago
install-notice.php
220 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ElementsKit_Lite\Libs\Framework\Classes; |
| 4 | |
| 5 | defined( 'ABSPATH' ) || exit; |
| 6 | |
| 7 | /** |
| 8 | * Displays and processes the consolidated WPMet plugin install notice. |
| 9 | * |
| 10 | * @since 3.9.5 |
| 11 | */ |
| 12 | class Install_Notice { |
| 13 | |
| 14 | /** |
| 15 | * ElementsKit plugin basename. |
| 16 | * |
| 17 | * @since 3.9.5 |
| 18 | * @var string |
| 19 | */ |
| 20 | const PLUGIN_FILE = 'elementskit-lite/elementskit-lite.php'; |
| 21 | |
| 22 | /** |
| 23 | * Register notice, asset, and AJAX hooks. |
| 24 | * |
| 25 | * @since 3.9.5 |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function init(): void { |
| 30 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); |
| 31 | add_action( 'admin_notices', array( $this, 'render' ) ); |
| 32 | add_action( 'wp_ajax_elementskit_dismiss_auto_install_notice', array( $this, 'handle_dismiss' ) ); |
| 33 | add_action( 'wp_ajax_elementskit_confirm_auto_install_notice', array( $this, 'handle_confirm' ) ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Determine whether the install notice should be displayed. |
| 38 | * |
| 39 | * @since 3.9.5 |
| 40 | * |
| 41 | * @return bool True when the notice should be displayed. |
| 42 | */ |
| 43 | private function should_render(): bool { |
| 44 | if ( ! current_user_can( 'manage_options' ) ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | if ( Install_Tracker::is_notice_dismissed( self::PLUGIN_FILE ) ) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | if ( Utils::instance()->get_settings( 'ekit_user_consent_for_banner', 'yes' ) !== 'yes' ) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // Show when ElementsKit was auto-installed or completed its own onboarding without giving email. |
| 57 | $registry = Install_Tracker::get_registry(); |
| 58 | $auto_installed = isset( $registry['elementskit-lite/elementskit-lite.php'] ); |
| 59 | $own_onboarded = (bool) get_option( 'elements_kit_onboard_status' ); |
| 60 | if ( ! $auto_installed && ! $own_onboarded ) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | // If not auto-installed, suppress when email was already collected during own onboarding. |
| 65 | if ( ! $auto_installed && Install_Tracker::has_collected_email() ) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // Only show on the ElementsKit dashboard and Get Help pages. |
| 70 | $current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 71 | return in_array( $current_page, array( 'elementskit', 'elementskit-lite_get_help' ), true ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Enqueue the install notice styles and scripts. |
| 76 | * |
| 77 | * @since 3.9.5 |
| 78 | * |
| 79 | * @return void |
| 80 | */ |
| 81 | public function enqueue_assets(): void { |
| 82 | if ( ! $this->should_render() ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $asset_url = \ElementsKit_Lite::lib_url() . 'framework/assets/'; |
| 87 | $version = \ElementsKit_Lite::version(); |
| 88 | |
| 89 | wp_enqueue_style( 'elementskit-install-notice', $asset_url . 'css/install-notice.css', array(), $version ); |
| 90 | wp_enqueue_script( 'elementskit-install-notice', $asset_url . 'js/install-notice.js', array( 'jquery' ), $version, true ); |
| 91 | wp_localize_script( |
| 92 | 'elementskit-install-notice', |
| 93 | 'elementskitInstallNotice', |
| 94 | array( |
| 95 | 'ajaxUrl' => admin_url( 'admin-ajax.php' ), |
| 96 | 'nonce' => wp_create_nonce( 'elementskit_auto_install_notice_nonce' ), |
| 97 | 'applying' => __( 'Applying…', 'elementskit-lite' ), |
| 98 | ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Render the consolidated plugin install notice. |
| 104 | * |
| 105 | * @since 3.9.5 |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function render(): void { |
| 110 | if ( ! $this->should_render() ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | ?> |
| 115 | <div class="notice notice-info wpmet-elementskit-auto-install-notice"> |
| 116 | <div class="wpmet-elementskit-notice-content"> |
| 117 | <h3> |
| 118 | <?php esc_html_e( 'Get more from your new plugins 🚀', 'elementskit-lite' ); ?> |
| 119 | </h3> |
| 120 | <p> |
| 121 | <?php esc_html_e( 'Allow us to use non-sensitive data from your site to improve our products and get personalized performance tips.', 'elementskit-lite' ); ?> |
| 122 | </p> |
| 123 | </div> |
| 124 | <div class="wpmet-elementskit-notice-actions"> |
| 125 | <button class="button wpmet-elementskit-notice-btn-skip" id="wpmet-elementskit-notice-skip"> |
| 126 | <?php esc_html_e( 'Skip for Now', 'elementskit-lite' ); ?> |
| 127 | </button> |
| 128 | <button class="button button-primary wpmet-elementskit-notice-btn-confirm" id="wpmet-elementskit-notice-confirm"> |
| 129 | <?php esc_html_e( 'Confirm & Apply', 'elementskit-lite' ); ?> |
| 130 | </button> |
| 131 | </div> |
| 132 | </div> |
| 133 | <?php |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Handle the notice dismissal AJAX request. |
| 138 | * |
| 139 | * @since 3.9.5 |
| 140 | * |
| 141 | * @return void |
| 142 | */ |
| 143 | public function handle_dismiss(): void { |
| 144 | check_ajax_referer( 'elementskit_auto_install_notice_nonce', 'nonce' ); |
| 145 | if ( ! current_user_can( 'manage_options' ) ) { |
| 146 | wp_send_json_error(); |
| 147 | } |
| 148 | Install_Tracker::dismiss_notice( self::PLUGIN_FILE ); |
| 149 | wp_send_json_success(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Handle the notice confirmation AJAX request. |
| 154 | * |
| 155 | * Sends subscription data for ElementsKit and registered plugins before |
| 156 | * resolving the current install notice. |
| 157 | * |
| 158 | * @since 3.9.5 |
| 159 | * |
| 160 | * @return void |
| 161 | */ |
| 162 | public function handle_confirm(): void { |
| 163 | check_ajax_referer( 'elementskit_auto_install_notice_nonce', 'nonce' ); |
| 164 | if ( ! current_user_can( 'manage_options' ) ) { |
| 165 | wp_send_json_error(); |
| 166 | } |
| 167 | |
| 168 | $registry = Install_Tracker::get_registry(); |
| 169 | $email = Install_Tracker::get_collected_email(); |
| 170 | $email = $email ? $email : $this->get_user_email(); |
| 171 | $email = $email ? $email : sanitize_email( get_option( 'admin_email' ) ); |
| 172 | |
| 173 | Plugin_Data_Sender::instance()->sendEmailSubscribeData( |
| 174 | 'plugin-subscribe', |
| 175 | array( |
| 176 | 'email' => $email, |
| 177 | 'slug' => 'elementskit', |
| 178 | ) |
| 179 | ); |
| 180 | |
| 181 | // Also send for auto-installed plugins that do not have their own notice banner. |
| 182 | $plugin_map = Install_Tracker::get_onboard_status_map(); |
| 183 | foreach ( array_keys( $registry ) as $plugin_file ) { |
| 184 | if ( |
| 185 | self::PLUGIN_FILE !== $plugin_file |
| 186 | && ! empty( $plugin_map[ $plugin_file ]['crm_slug'] ) |
| 187 | ) { |
| 188 | Plugin_Data_Sender::instance()->sendEmailSubscribeData( |
| 189 | 'plugin-subscribe', |
| 190 | array( |
| 191 | 'email' => $email, |
| 192 | 'slug' => sanitize_key( $plugin_map[ $plugin_file ]['crm_slug'] ), |
| 193 | ) |
| 194 | ); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | Install_Tracker::store_collected_email( $email ); |
| 199 | Install_Tracker::dismiss_notice( self::PLUGIN_FILE ); |
| 200 | wp_send_json_success(); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Return the admin email address stored in plugin options, if available. |
| 205 | * |
| 206 | * @since 3.9.5 |
| 207 | * |
| 208 | * @return string A sanitized email address, or an empty string when not set. |
| 209 | */ |
| 210 | private function get_user_email(): string { |
| 211 | $options = get_option( 'elementskit_options', array() ); |
| 212 | |
| 213 | if ( empty( $options['settings']['newsletter_email'] ) ) { |
| 214 | return ''; |
| 215 | } |
| 216 | |
| 217 | return sanitize_email( $options['settings']['newsletter_email'] ); |
| 218 | } |
| 219 | } |
| 220 |