| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\Modules\Announcements; |
| 4 |
|
| 5 |
use Elementor\Core\Base\App as BaseApp; |
| 6 |
use Elementor\Modules\Ai\Preferences; |
| 7 |
use Elementor\Modules\Announcements\Classes\Announcement; |
| 8 |
use Elementor\Settings as ElementorSettings; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly. |
| 12 |
} |
| 13 |
|
| 14 |
class Module extends BaseApp { |
| 15 |
|
| 16 |
const AI_ASSETS_BASE_URL = 'https://assets.elementor.com/ai/v1/'; |
| 17 |
|
| 18 |
/** |
| 19 |
* @return bool |
| 20 |
*/ |
| 21 |
public static function is_active(): bool { |
| 22 |
return is_admin(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public function get_name(): string { |
| 29 |
return 'announcements'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Render wrapper for the app to load. |
| 34 |
*/ |
| 35 |
private function render_app_wrapper() { |
| 36 |
?> |
| 37 |
<div id="e-announcements-root"></div> |
| 38 |
<?php |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Enqueue app scripts. |
| 43 |
*/ |
| 44 |
private function enqueue_scripts() { |
| 45 |
wp_enqueue_script( |
| 46 |
'announcements-app', |
| 47 |
$this->get_js_assets_url( 'announcements-app' ), |
| 48 |
[ |
| 49 |
'wp-i18n', |
| 50 |
], |
| 51 |
ELEMENTOR_VERSION, |
| 52 |
true |
| 53 |
); |
| 54 |
|
| 55 |
wp_set_script_translations( 'announcements-app', 'elementor' ); |
| 56 |
|
| 57 |
$this->print_config( 'announcements-app' ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get initialization settings to use in frontend. |
| 62 |
* |
| 63 |
* @return array[] |
| 64 |
*/ |
| 65 |
protected function get_init_settings(): array { |
| 66 |
$active_announcements = $this->get_active_announcements(); |
| 67 |
$additional_settings = []; |
| 68 |
|
| 69 |
foreach ( $active_announcements as $announcement ) { |
| 70 |
$additional_settings[] = $announcement->get_prepared_data(); |
| 71 |
// @TODO - replace with ajax request from the front after actually triggered |
| 72 |
$announcement->after_triggered(); |
| 73 |
} |
| 74 |
|
| 75 |
return [ |
| 76 |
'announcements' => $additional_settings, |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Enqueue the module styles. |
| 82 |
*/ |
| 83 |
public function enqueue_styles() { |
| 84 |
wp_enqueue_style( |
| 85 |
'announcements-app', |
| 86 |
$this->get_css_assets_url( 'modules/announcements/announcements' ), |
| 87 |
[], |
| 88 |
ELEMENTOR_VERSION |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Retrieve all announcement in raw format ( array ). |
| 94 |
* |
| 95 |
* @return array[] |
| 96 |
*/ |
| 97 |
private function get_raw_announcements(): array { |
| 98 |
$raw_announcements = []; |
| 99 |
|
| 100 |
if ( Preferences::is_ai_enabled( get_current_user_id() ) ) { |
| 101 |
$raw_announcements[] = $this->get_ai_announcement_data(); |
| 102 |
} |
| 103 |
|
| 104 |
// DO NOT USE THIS FILTER |
| 105 |
return apply_filters( 'elementor/announcements/raw_announcements', $raw_announcements ); |
| 106 |
} |
| 107 |
|
| 108 |
private function get_ai_announcement_data(): array { |
| 109 |
return [ |
| 110 |
'title' => __( 'Discover your new superpowers ', 'elementor' ), |
| 111 |
'description' => __( '<p>With AI for text, code, image generation and editing, you can bring your vision to life faster than ever. Start your free trial now - <b>no credit card required!</b></p>', 'elementor' ), |
| 112 |
'media' => [ |
| 113 |
'type' => 'image', |
| 114 |
'src' => self::AI_ASSETS_BASE_URL . 'images/ai-social-hd.gif', |
| 115 |
], |
| 116 |
'cta' => [ |
| 117 |
[ |
| 118 |
'label' => __( 'Let\'s do it', 'elementor' ), |
| 119 |
'variant' => 'primary', |
| 120 |
'target' => '_top', |
| 121 |
'url' => '#welcome-ai', |
| 122 |
], |
| 123 |
[ |
| 124 |
'label' => __( 'Skip', 'elementor' ), |
| 125 |
'variant' => 'secondary', |
| 126 |
], |
| 127 |
], |
| 128 |
'triggers' => [ |
| 129 |
[ |
| 130 |
'action' => 'aiStarted', |
| 131 |
], |
| 132 |
], |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Retrieve all announcement objects. |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
private function get_announcements(): array { |
| 142 |
$announcements = []; |
| 143 |
foreach ( $this->get_raw_announcements() as $announcement_data ) { |
| 144 |
$announcements[] = new Announcement( $announcement_data ); |
| 145 |
} |
| 146 |
|
| 147 |
return $announcements; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Retrieve all active announcement objects. |
| 152 |
* |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
private function get_active_announcements(): array { |
| 156 |
$active_announcements = []; |
| 157 |
foreach ( $this->get_announcements() as $announcement ) { |
| 158 |
if ( $announcement->is_active() ) { |
| 159 |
$active_announcements[] = $announcement; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return $active_announcements; |
| 164 |
} |
| 165 |
|
| 166 |
public function __construct() { |
| 167 |
parent::__construct(); |
| 168 |
|
| 169 |
add_action( 'elementor/init', [ $this, 'on_elementor_init' ] ); |
| 170 |
} |
| 171 |
|
| 172 |
public function on_elementor_init() { |
| 173 |
if ( empty( $this->get_active_announcements() ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
add_action( 'elementor/editor/footer', function () { |
| 178 |
$this->render_app_wrapper(); |
| 179 |
} ); |
| 180 |
|
| 181 |
add_action( 'elementor/editor/after_enqueue_scripts', function () { |
| 182 |
$this->enqueue_scripts(); |
| 183 |
$this->enqueue_styles(); |
| 184 |
} ); |
| 185 |
} |
| 186 |
} |
| 187 |
|