| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Promotions; |
| 4 |
|
| 5 |
use Elementor\Core\Base\Document; |
| 6 |
use Elementor\User; |
| 7 |
use Elementor\Utils; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Conversion_Banner { |
| 14 |
|
| 15 |
const DEFAULT_SELECTOR = '.wrap h1, .wrap h2'; |
| 16 |
const SCRIPT_HANDLE = 'e-conversion-banner'; |
| 17 |
const STYLE_HANDLE = 'e-conversion-banner'; |
| 18 |
const NONCE_ACTION = 'e_conversion_banner_nonce'; |
| 19 |
const OBJECT_NAME = 'eConversionBanner'; |
| 20 |
const DISMISS_KEY = 'conversion_banner_go_pro'; |
| 21 |
const AJAX_ACTION = 'elementor_dismiss_conversion_banner'; |
| 22 |
const CONTAINER_ID = 'e-conversion-banner'; |
| 23 |
const UPGRADE_URL = 'https://go.elementor.com/go-pro-wp-admin-upgrade-notice/'; |
| 24 |
const BIRTHDAY_PROMOTION_URL = 'https://go.elementor.com/go-pro-wp-admin-upgrad-notice/'; |
| 25 |
|
| 26 |
const HELLO_THEME_CONFIG_FILTER = 'hello-plus-theme/rest/admin-config'; |
| 27 |
const THEME_SLUGS = [ 'hello-elementor', 'hello-biz', 'hello-commerce' ]; |
| 28 |
const THEME_SETTINGS_PAGE_SUFFIX = '-settings'; |
| 29 |
const GO_PRO_TITLE_PREFIX = 'Go Pro'; |
| 30 |
const MIN_ELEMENTOR_PAGES_TO_TRIGGER = 2; |
| 31 |
const PENDING_TRANSIENT_KEY = 'elementor_conversion_banner_pages_pending'; |
| 32 |
const PENDING_TTL = DAY_IN_SECONDS; |
| 33 |
const UNLOCK_OPTION_KEY = 'elementor_conversion_banner_unlocked'; |
| 34 |
const UNLOCK_OPTION_VALUE = '1'; |
| 35 |
|
| 36 |
public function __construct() { |
| 37 |
add_action( 'wp_ajax_' . self::AJAX_ACTION, [ $this, 'ajax_dismiss_banner' ] ); |
| 38 |
|
| 39 |
add_filter( self::HELLO_THEME_CONFIG_FILTER, [ $this, 'suppress_hello_theme_banner' ] ); |
| 40 |
|
| 41 |
add_action( 'current_screen', [ $this, 'maybe_register_banner_hooks' ] ); |
| 42 |
} |
| 43 |
|
| 44 |
public static function register_cache_invalidation_hooks(): void { |
| 45 |
static $hooks_registered = false; |
| 46 |
|
| 47 |
if ( $hooks_registered ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$hooks_registered = true; |
| 52 |
|
| 53 |
add_action( 'added_post_meta', [ self::class, 'maybe_invalidate_pending_cache' ], 10, 4 ); |
| 54 |
add_action( 'updated_post_meta', [ self::class, 'maybe_invalidate_pending_cache' ], 10, 4 ); |
| 55 |
} |
| 56 |
|
| 57 |
public static function maybe_invalidate_pending_cache( $meta_id, $post_id, $meta_key, $meta_value ): void { |
| 58 |
if ( Document::BUILT_WITH_ELEMENTOR_META_KEY !== $meta_key ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if ( self::is_unlocked() ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
delete_transient( self::PENDING_TRANSIENT_KEY ); |
| 67 |
} |
| 68 |
|
| 69 |
public function maybe_register_banner_hooks(): void { |
| 70 |
$placement = $this->get_active_placement(); |
| 71 |
|
| 72 |
if ( empty( $placement ) || ! self::has_min_elementor_pages() ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
add_action( 'in_admin_header', [ $this, 'render_banner_container' ], 11 ); |
| 77 |
add_action( 'admin_enqueue_scripts', function () use ( $placement ) { |
| 78 |
$this->enqueue_assets( $placement ); |
| 79 |
} ); |
| 80 |
} |
| 81 |
|
| 82 |
public function render_banner_container(): void { |
| 83 |
?> |
| 84 |
<div id="<?php echo esc_attr( self::CONTAINER_ID ); ?>"> |
| 85 |
<?php $this->print_banner_markup( true ); ?> |
| 86 |
</div> |
| 87 |
<?php |
| 88 |
} |
| 89 |
|
| 90 |
public function suppress_hello_theme_banner( $config ) { |
| 91 |
if ( $this->is_request_from_theme_admin_page() |
| 92 |
|| ! ( is_array( $config ) && isset( $config['welcome'] ) ) |
| 93 |
|| Utils::has_pro() |
| 94 |
) { |
| 95 |
return $config; |
| 96 |
} |
| 97 |
|
| 98 |
$title = $config['welcome']['title'] ?? ''; |
| 99 |
|
| 100 |
if ( is_string( $title ) && substr( $title, 0, strlen( self::GO_PRO_TITLE_PREFIX ) ) === self::GO_PRO_TITLE_PREFIX ) { |
| 101 |
$config['welcome'] = []; |
| 102 |
} |
| 103 |
|
| 104 |
return $config; |
| 105 |
} |
| 106 |
|
| 107 |
private function is_request_from_theme_admin_page(): bool { |
| 108 |
$referer = wp_get_referer(); |
| 109 |
|
| 110 |
if ( ! $referer ) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
parse_str( (string) wp_parse_url( $referer, PHP_URL_QUERY ), $query_args ); |
| 115 |
|
| 116 |
$page = $query_args['page'] ?? ''; |
| 117 |
|
| 118 |
foreach ( self::THEME_SLUGS as $theme_slug ) { |
| 119 |
if ( $page === $theme_slug || $page === $theme_slug . self::THEME_SETTINGS_PAGE_SUFFIX ) { |
| 120 |
return true; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
public function ajax_dismiss_banner(): void { |
| 128 |
try { |
| 129 |
check_ajax_referer( self::NONCE_ACTION, 'nonce' ); |
| 130 |
|
| 131 |
if ( ! $this->is_user_allowed() ) { |
| 132 |
wp_send_json_error( 'Permission denied', 403 ); |
| 133 |
} |
| 134 |
|
| 135 |
User::set_introduction_viewed( [ 'introductionKey' => self::DISMISS_KEY ] ); |
| 136 |
|
| 137 |
wp_send_json_success(); |
| 138 |
|
| 139 |
} catch ( \Exception $e ) { |
| 140 |
wp_send_json_error( 'Failed to dismiss banner', 500 ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
private function enqueue_assets( array $placement ): void { |
| 145 |
$min_suffix = Utils::is_script_debug() ? '' : '.min'; |
| 146 |
|
| 147 |
wp_enqueue_script( |
| 148 |
self::SCRIPT_HANDLE, |
| 149 |
ELEMENTOR_ASSETS_URL . 'js/' . self::SCRIPT_HANDLE . $min_suffix . '.js', |
| 150 |
[ 'wp-util', 'elementor-common' ], |
| 151 |
ELEMENTOR_VERSION, |
| 152 |
true |
| 153 |
); |
| 154 |
|
| 155 |
wp_set_script_translations( self::SCRIPT_HANDLE, 'elementor' ); |
| 156 |
|
| 157 |
wp_localize_script( |
| 158 |
self::SCRIPT_HANDLE, |
| 159 |
self::OBJECT_NAME, |
| 160 |
[ |
| 161 |
'nonce' => wp_create_nonce( self::NONCE_ACTION ), |
| 162 |
'action' => self::AJAX_ACTION, |
| 163 |
'placement' => $placement, |
| 164 |
] |
| 165 |
); |
| 166 |
|
| 167 |
$this->enqueue_styles(); |
| 168 |
} |
| 169 |
|
| 170 |
private function enqueue_styles(): void { |
| 171 |
wp_enqueue_style( |
| 172 |
self::STYLE_HANDLE, |
| 173 |
ELEMENTOR_ASSETS_URL . 'css/modules/promotions/conversion-banner.css', |
| 174 |
[], |
| 175 |
ELEMENTOR_VERSION |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
private function print_banner_markup( bool $dismissable ): void { |
| 180 |
$banner = $this->get_banner_config(); |
| 181 |
|
| 182 |
?> |
| 183 |
<div class="e-conversion-banner__paper"> |
| 184 |
<?php if ( $dismissable ) : ?> |
| 185 |
<button type="button" class="e-conversion-banner__dismiss notice-dismiss"> |
| 186 |
<span class="screen-reader-text"><?php echo esc_html__( 'Dismiss this notice.', 'elementor' ); ?></span> |
| 187 |
</button> |
| 188 |
<?php endif; ?> |
| 189 |
<div class="e-conversion-banner__content"> |
| 190 |
<h2 class="e-conversion-banner__title"><?php echo esc_html( $banner['title'] ); ?></h2> |
| 191 |
<p class="e-conversion-banner__text"><?php echo esc_html( $banner['text'] ); ?></p> |
| 192 |
<div class="e-conversion-banner__actions"> |
| 193 |
<?php foreach ( $banner['buttons'] as $button ) : ?> |
| 194 |
<a |
| 195 |
class="e-conversion-banner__button button button-primary" |
| 196 |
href="<?php echo esc_url( $button['link'] ); ?>" |
| 197 |
target="<?php echo esc_attr( $button['target'] ?? '_self' ); ?>" |
| 198 |
><?php echo esc_html( $button['text'] ); ?></a> |
| 199 |
<?php endforeach; ?> |
| 200 |
</div> |
| 201 |
</div> |
| 202 |
<?php if ( ! empty( $banner['image']['src'] ) ) : ?> |
| 203 |
<img |
| 204 |
class="e-conversion-banner__image" |
| 205 |
src="<?php echo esc_url( $banner['image']['src'] ); ?>" |
| 206 |
alt="<?php echo esc_attr( $banner['image']['alt'] ); ?>" |
| 207 |
/> |
| 208 |
<?php endif; ?> |
| 209 |
</div> |
| 210 |
<?php |
| 211 |
} |
| 212 |
|
| 213 |
private function get_banner_config(): array { |
| 214 |
if ( Utils::is_sale_time() ) { |
| 215 |
return $this->get_birthday_banner_config(); |
| 216 |
} |
| 217 |
|
| 218 |
return [ |
| 219 |
'title' => esc_html__( 'Build more with Elementor Pro', 'elementor' ), |
| 220 |
'text' => esc_html__( 'Add the theme builder, popup builder, and 85+ advanced widgets to your Elementor Editor.', 'elementor' ), |
| 221 |
'buttons' => [ |
| 222 |
[ |
| 223 |
'text' => esc_html__( 'Upgrade now', 'elementor' ), |
| 224 |
'link' => self::UPGRADE_URL, |
| 225 |
'target' => '_blank', |
| 226 |
], |
| 227 |
], |
| 228 |
'image' => [ |
| 229 |
'src' => '', |
| 230 |
'alt' => esc_html__( 'Upgrade to Elementor Pro', 'elementor' ), |
| 231 |
], |
| 232 |
]; |
| 233 |
} |
| 234 |
|
| 235 |
private function get_birthday_banner_config(): array { |
| 236 |
return [ |
| 237 |
'title' => esc_html__( 'Celebrate 10 years of Elementor', 'elementor' ), |
| 238 |
'text' => esc_html__( 'Upgrade your workflow with more capabilities for less. Offer ends June 17.', 'elementor' ), |
| 239 |
'buttons' => [ |
| 240 |
[ |
| 241 |
'text' => esc_html__( 'Get Discounts', 'elementor' ), |
| 242 |
'link' => self::BIRTHDAY_PROMOTION_URL, |
| 243 |
'target' => '_blank', |
| 244 |
], |
| 245 |
], |
| 246 |
'image' => [ |
| 247 |
'src' => ELEMENTOR_ASSETS_URL . 'images/decade-birthday.png', |
| 248 |
'alt' => esc_html__( 'Celebrate 10 years of Elementor', 'elementor' ), |
| 249 |
], |
| 250 |
]; |
| 251 |
} |
| 252 |
|
| 253 |
public static function should_display_banner(): bool { |
| 254 |
return self::is_user_allowed() && self::should_display(); |
| 255 |
} |
| 256 |
|
| 257 |
private function get_active_placement(): array { |
| 258 |
$current_screen = get_current_screen(); |
| 259 |
|
| 260 |
if ( ! $current_screen ) { |
| 261 |
return []; |
| 262 |
} |
| 263 |
|
| 264 |
$allowed_pages = $this->get_allowed_admin_pages(); |
| 265 |
|
| 266 |
return $allowed_pages[ $current_screen->id ] ?? []; |
| 267 |
} |
| 268 |
|
| 269 |
private static function should_display(): bool { |
| 270 |
return ! Utils::has_pro() && ! self::is_dismissed() && self::has_min_elementor_pages(); |
| 271 |
} |
| 272 |
|
| 273 |
private static function is_unlocked(): bool { |
| 274 |
return self::UNLOCK_OPTION_VALUE === get_option( self::UNLOCK_OPTION_KEY ); |
| 275 |
} |
| 276 |
|
| 277 |
private static function has_min_elementor_pages(): bool { |
| 278 |
if ( self::is_unlocked() ) { |
| 279 |
return true; |
| 280 |
} |
| 281 |
|
| 282 |
if ( get_transient( self::PENDING_TRANSIENT_KEY ) ) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
$query = new \WP_Query( [ |
| 287 |
'fields' => 'ids', |
| 288 |
'meta_key' => Document::BUILT_WITH_ELEMENTOR_META_KEY, |
| 289 |
'meta_value' => 'builder', |
| 290 |
'no_found_rows' => true, |
| 291 |
'post_status' => 'any', |
| 292 |
'post_type' => 'any', |
| 293 |
'posts_per_page' => self::MIN_ELEMENTOR_PAGES_TO_TRIGGER, |
| 294 |
'update_post_meta_cache' => false, |
| 295 |
'update_post_term_cache' => false, |
| 296 |
] ); |
| 297 |
|
| 298 |
if ( count( $query->posts ) >= self::MIN_ELEMENTOR_PAGES_TO_TRIGGER ) { |
| 299 |
update_option( self::UNLOCK_OPTION_KEY, self::UNLOCK_OPTION_VALUE, true ); |
| 300 |
delete_transient( self::PENDING_TRANSIENT_KEY ); |
| 301 |
|
| 302 |
return true; |
| 303 |
} |
| 304 |
|
| 305 |
set_transient( self::PENDING_TRANSIENT_KEY, 1, self::PENDING_TTL ); |
| 306 |
|
| 307 |
return false; |
| 308 |
} |
| 309 |
|
| 310 |
private static function is_user_allowed(): bool { |
| 311 |
return current_user_can( 'manage_options' ); |
| 312 |
} |
| 313 |
|
| 314 |
private static function is_dismissed(): bool { |
| 315 |
return (bool) User::get_introduction_meta( self::DISMISS_KEY ); |
| 316 |
} |
| 317 |
|
| 318 |
private function get_allowed_admin_pages(): array { |
| 319 |
$default = [ 'selector' => self::DEFAULT_SELECTOR ]; |
| 320 |
$plugin_pages = [ 'selector' => '.wrap hr.wp-header-end' ]; |
| 321 |
|
| 322 |
return [ |
| 323 |
'dashboard' => [ 'selector' => '#wpbody #wpbody-content .wrap h1' ], |
| 324 |
'toplevel_page_elementor' => [ |
| 325 |
'selector' => '#e-home-screen', |
| 326 |
'before' => true, |
| 327 |
], |
| 328 |
'elementor_page_elementor-settings' => $default, |
| 329 |
'elementor_page_elementor-tools' => $default, |
| 330 |
'elementor_page_elementor-role-manager' => $default, |
| 331 |
'elementor_page_elementor-element-manager' => [ |
| 332 |
'selector' => '.wrap h1, .wrap h3.wp-heading-inline', |
| 333 |
], |
| 334 |
'elementor_page_elementor-system-info' => [ |
| 335 |
'selector' => '#wpbody #wpbody-content #elementor-system-info .elementor-system-info-header', |
| 336 |
'before' => true, |
| 337 |
], |
| 338 |
'elementor_library_page_e-floating-buttons' => [ |
| 339 |
'selector' => '#wpbody-content .e-landing-pages-empty, .wrap h2', |
| 340 |
'before' => true, |
| 341 |
], |
| 342 |
'edit-e-floating-buttons' => $default, |
| 343 |
'edit-elementor_library' => [ |
| 344 |
'selector' => self::DEFAULT_SELECTOR, |
| 345 |
'before' => true, |
| 346 |
], |
| 347 |
'edit-elementor_library_category' => $default, |
| 348 |
'themes' => $default, |
| 349 |
'nav-menus' => $default, |
| 350 |
'theme-editor' => $default, |
| 351 |
'plugins' => $plugin_pages, |
| 352 |
'plugin-install' => $plugin_pages, |
| 353 |
'plugin-editor' => $plugin_pages, |
| 354 |
]; |
| 355 |
} |
| 356 |
} |
| 357 |
|