module.php
179 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor\Modules\Announcements; |
| 4 | |
| 5 | use Elementor\Core\Base\App as BaseApp; |
| 6 | use Elementor\Modules\Announcements\Classes\Announcement; |
| 7 | use Elementor\Settings as ElementorSettings; |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly. |
| 11 | } |
| 12 | |
| 13 | class Module extends BaseApp { |
| 14 | |
| 15 | /** |
| 16 | * @return bool |
| 17 | */ |
| 18 | public static function is_active(): bool { |
| 19 | return is_admin(); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @return string |
| 24 | */ |
| 25 | public function get_name(): string { |
| 26 | return 'announcements'; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Render wrapper for the app to load. |
| 31 | */ |
| 32 | private function render_app_wrapper() { |
| 33 | ?> |
| 34 | <div id="e-announcements-root"></div> |
| 35 | <?php |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Enqueue app scripts. |
| 40 | */ |
| 41 | private function enqueue_scripts() { |
| 42 | wp_enqueue_script( |
| 43 | 'announcements-app', |
| 44 | $this->get_js_assets_url( 'announcements-app' ), |
| 45 | [], |
| 46 | ELEMENTOR_VERSION, |
| 47 | true |
| 48 | ); |
| 49 | |
| 50 | $this->print_config( 'announcements-app' ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get initialization settings to use in frontend. |
| 55 | * |
| 56 | * @return array[] |
| 57 | */ |
| 58 | protected function get_init_settings(): array { |
| 59 | $active_announcements = $this->get_active_announcements(); |
| 60 | $additional_settings = []; |
| 61 | |
| 62 | foreach ( $active_announcements as $announcement ) { |
| 63 | $additional_settings[] = $announcement->get_prepared_data(); |
| 64 | //@TODO - replace with ajax request from the front after actually triggered |
| 65 | $announcement->after_triggered(); |
| 66 | } |
| 67 | |
| 68 | return [ |
| 69 | 'announcements' => $additional_settings, |
| 70 | ]; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Enqueue the module styles. |
| 75 | */ |
| 76 | public function enqueue_styles() { |
| 77 | wp_enqueue_style( |
| 78 | 'announcements-app', |
| 79 | $this->get_css_assets_url( 'modules/announcements/announcements' ), |
| 80 | [], |
| 81 | ELEMENTOR_VERSION |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Retrieve all announcement in raw format ( array ). |
| 87 | * |
| 88 | * @return array[] |
| 89 | */ |
| 90 | private function get_raw_announcements(): array { |
| 91 | $raw_announcements = [ |
| 92 | [ |
| 93 | 'title' => 'Unlock the Power of Elementor AI', |
| 94 | 'description' => '<p>Design professional websites with natively integrated AI tools.</p> |
| 95 | <ul> |
| 96 | <li>Let AI write or edit your text, adjust its length and tone of voice. Also generate custom code that seamlessly integrates into your website.</li> |
| 97 | <li>Create one-of-a kind images, add, or erase content in existing images or expand them beyond their original size and aspect ratio.</li> |
| 98 | <li>Access and repurpose previously-generated prompts with Elementor’s AI History Panel to streamline your workflow and ensure consistency.</li> |
| 99 | </ul>', |
| 100 | 'media' => [ |
| 101 | 'type' => 'image', |
| 102 | 'src' => ELEMENTOR_ASSETS_URL . 'images/announcement.png?' . ELEMENTOR_VERSION, |
| 103 | ], |
| 104 | 'cta' => [ |
| 105 | [ |
| 106 | 'label' => 'Continue', |
| 107 | 'variant' => 'primary', |
| 108 | 'target' => '_blank', |
| 109 | ], |
| 110 | [ |
| 111 | 'label' => 'Learn More', |
| 112 | 'target' => '_blank', |
| 113 | 'url' => 'https://go.elementor.com/whats-new-popup-learn-elementor-ai/', |
| 114 | ], |
| 115 | ], |
| 116 | 'triggers' => [ |
| 117 | [ |
| 118 | 'action' => 'aiStared', |
| 119 | ], |
| 120 | ], |
| 121 | ], |
| 122 | ]; |
| 123 | |
| 124 | // DO NOT USE THIS FILTER |
| 125 | return apply_filters( 'elementor/announcements/raw_announcements', $raw_announcements ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Retrieve all announcement objects. |
| 130 | * |
| 131 | * @return array |
| 132 | */ |
| 133 | private function get_announcements(): array { |
| 134 | $announcements = []; |
| 135 | foreach ( $this->get_raw_announcements() as $announcement_data ) { |
| 136 | $announcements[] = new Announcement( $announcement_data ); |
| 137 | } |
| 138 | |
| 139 | return $announcements; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Retrieve all active announcement objects. |
| 144 | * |
| 145 | * @return array |
| 146 | */ |
| 147 | private function get_active_announcements(): array { |
| 148 | $active_announcements = []; |
| 149 | foreach ( $this->get_announcements() as $announcement ) { |
| 150 | if ( $announcement->is_active() ) { |
| 151 | $active_announcements[] = $announcement; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $active_announcements; |
| 156 | } |
| 157 | |
| 158 | public function __construct() { |
| 159 | parent::__construct(); |
| 160 | |
| 161 | add_action( 'elementor/init', [ $this, 'on_elementor_init' ] ); |
| 162 | } |
| 163 | |
| 164 | public function on_elementor_init() { |
| 165 | if ( empty( $this->get_active_announcements() ) ) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | add_action( 'elementor/editor/footer', function () { |
| 170 | $this->render_app_wrapper(); |
| 171 | } ); |
| 172 | |
| 173 | add_action( 'elementor/editor/after_enqueue_scripts', function () { |
| 174 | $this->enqueue_scripts(); |
| 175 | $this->enqueue_styles(); |
| 176 | } ); |
| 177 | } |
| 178 | } |
| 179 |