| 1 |
<?php |
| 2 |
namespace Elementor\Core\Editor; |
| 3 |
|
| 4 |
use Elementor\Core\Base\Base_Object; |
| 5 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 6 |
use Elementor\Core\Utils\Promotions\Filtered_Promotions_Manager; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Utils; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Notice_Bar extends Base_Object { |
| 15 |
|
| 16 |
protected function get_init_settings() { |
| 17 |
if ( Plugin::$instance->get_install_time() > strtotime( '-1 days' ) ) { |
| 18 |
return []; |
| 19 |
} |
| 20 |
|
| 21 |
$upgrade_url = 'https://go.elementor.com/go-pro-editor-notice-bar/'; |
| 22 |
|
| 23 |
$config = [ |
| 24 |
'description' => $this->get_description(), |
| 25 |
'upgrade_text' => $this->get_upgrade_text(), |
| 26 |
'upgrade_url' => $upgrade_url, |
| 27 |
]; |
| 28 |
|
| 29 |
$config = Filtered_Promotions_Manager::get_filtered_promotion_data( $config, 'elementor/notice-bar/custom_promotion', 'upgrade_url' ); |
| 30 |
|
| 31 |
return [ |
| 32 |
'muted_period' => 14, |
| 33 |
'option_key' => '_elementor_editor_upgrade_notice_dismissed', |
| 34 |
'message' => $config['description'] ?? $this->get_description(), |
| 35 |
'action_title' => $config['upgrade_text'] ?? $this->get_upgrade_text(), |
| 36 |
'action_url' => $config['upgrade_url'] ?? $upgrade_url, |
| 37 |
]; |
| 38 |
} |
| 39 |
|
| 40 |
public function get_upgrade_text() { |
| 41 |
return esc_html__( 'Upgrade Now', 'elementor' ); |
| 42 |
} |
| 43 |
|
| 44 |
public function get_description() { |
| 45 |
return esc_html__( 'Unleash the full power of Elementor\'s features and web creation tools.', 'elementor' ); |
| 46 |
} |
| 47 |
|
| 48 |
final public function get_notice() { |
| 49 |
if ( ! $this->has_access_to_notice() ) { |
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
$settings = $this->get_settings(); |
| 54 |
|
| 55 |
if ( empty( $settings['option_key'] ) ) { |
| 56 |
return null; |
| 57 |
} |
| 58 |
|
| 59 |
$dismissed_time = get_option( $settings['option_key'] ); |
| 60 |
|
| 61 |
if ( $dismissed_time ) { |
| 62 |
if ( $dismissed_time > strtotime( '-' . $settings['muted_period'] . ' days' ) ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$this->set_notice_dismissed(); |
| 67 |
} |
| 68 |
|
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
protected function render_action( $type ) { |
| 73 |
$settings = $this->get_settings(); |
| 74 |
|
| 75 |
// TODO: Make the API better. The bad naming is because of BC. |
| 76 |
$prefix_map = [ |
| 77 |
'primary' => '', |
| 78 |
'secondary' => 'secondary_', |
| 79 |
]; |
| 80 |
|
| 81 |
$prefix = $prefix_map[ $type ]; |
| 82 |
|
| 83 |
$action_title = "{$prefix}action_title"; |
| 84 |
$action_url = "{$prefix}action_url"; |
| 85 |
$action_message = "{$prefix}message"; |
| 86 |
$action_target = "{$prefix}action_target"; |
| 87 |
|
| 88 |
if ( empty( $settings[ $action_title ] ) || empty( $settings[ $action_url ] ) || empty( $settings[ $action_message ] ) ) { |
| 89 |
return; |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
?> |
| 94 |
<div class="e-notice-bar__message <?php echo esc_attr( "e-notice-bar__{$type}_message" ); ?>"> |
| 95 |
<?php Utils::print_unescaped_internal_string( sprintf( $settings[ $action_message ], $settings[ $action_url ] ) ); ?> |
| 96 |
</div> |
| 97 |
|
| 98 |
<div class="e-notice-bar__action <?php echo esc_attr( "e-notice-bar__{$type}_action" ); ?>"> |
| 99 |
<a href="<?php Utils::print_unescaped_internal_string( $settings[ $action_url ] ); ?>" |
| 100 |
target="<?php echo empty( $settings[ $action_target ] ) ? '_blank' : esc_attr( $settings[ $action_target ] ); ?>" |
| 101 |
> |
| 102 |
<?php Utils::print_unescaped_internal_string( $settings[ $action_title ] ); ?> |
| 103 |
</a> |
| 104 |
</div> |
| 105 |
<?php |
| 106 |
} |
| 107 |
|
| 108 |
public function render() { |
| 109 |
$settings = $this->get_settings(); |
| 110 |
|
| 111 |
$icon = empty( $settings['icon'] ) |
| 112 |
? 'eicon-elementor-square' |
| 113 |
: esc_attr( $settings['icon'] ); |
| 114 |
|
| 115 |
?> |
| 116 |
<div id="e-notice-bar" class="e-notice-bar"> |
| 117 |
<i class="e-notice-bar__icon <?php echo esc_attr( $icon ); ?>"></i> |
| 118 |
|
| 119 |
<?php |
| 120 |
$this->render_action( 'primary' ); |
| 121 |
$this->render_action( 'secondary' ); |
| 122 |
?> |
| 123 |
|
| 124 |
<i id="e-notice-bar__close" class="e-notice-bar__close eicon-close"></i> |
| 125 |
</div> |
| 126 |
<?php |
| 127 |
} |
| 128 |
|
| 129 |
public function __construct() { |
| 130 |
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); |
| 131 |
} |
| 132 |
|
| 133 |
public function set_notice_dismissed() { |
| 134 |
if ( ! $this->has_access_to_notice() ) { |
| 135 |
throw new \Exception( 'Access denied' ); |
| 136 |
} |
| 137 |
|
| 138 |
update_option( $this->get_settings( 'option_key' ), time() ); |
| 139 |
} |
| 140 |
|
| 141 |
public function register_ajax_actions( Ajax $ajax ) { |
| 142 |
$ajax->register_ajax_action( 'notice_bar_dismiss', [ $this, 'set_notice_dismissed' ] ); |
| 143 |
} |
| 144 |
|
| 145 |
private function has_access_to_notice() { |
| 146 |
return current_user_can( 'manage_options' ); |
| 147 |
} |
| 148 |
} |
| 149 |
|