announcement.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor\Modules\Announcements\Classes; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | class Announcement { |
| 10 | |
| 11 | /** |
| 12 | * @var array |
| 13 | */ |
| 14 | protected $raw_data; |
| 15 | /** |
| 16 | * @var array |
| 17 | */ |
| 18 | protected $triggers; |
| 19 | |
| 20 | public function __construct( array $data ) { |
| 21 | $this->raw_data = $data; |
| 22 | $this->set_triggers(); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * @return array |
| 27 | */ |
| 28 | protected function get_triggers(): array { |
| 29 | return $this->triggers; |
| 30 | } |
| 31 | |
| 32 | protected function set_triggers() { |
| 33 | $triggers = $this->raw_data['triggers'] ?? []; |
| 34 | foreach ( $triggers as $trigger ) { |
| 35 | $this->triggers[] = Utils::get_trigger_object( $trigger ); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * is_active |
| 41 | * @return bool |
| 42 | */ |
| 43 | public function is_active(): bool { |
| 44 | $triggers = $this->get_triggers(); |
| 45 | |
| 46 | if ( empty( $triggers ) ) { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | foreach ( $triggers as $trigger ) { |
| 51 | if ( ! $trigger->is_active() ) { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | public function after_triggered() { |
| 60 | foreach ( $this->get_triggers() as $trigger ) { |
| 61 | if ( $trigger->is_active() ) { |
| 62 | $trigger->after_triggered(); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @return array |
| 69 | */ |
| 70 | public function get_prepared_data(): array { |
| 71 | $raw_data = $this->raw_data; |
| 72 | unset( $raw_data['triggers'] ); |
| 73 | |
| 74 | return $raw_data; |
| 75 | } |
| 76 | } |
| 77 |