event-times-saved.php
4 months ago
user-booking-approved.php
1 year ago
user-register-in-event.php
1 year ago
user-registers-event-with-specific-ticket.php
2 years ago
user-unregister-from-event.php
1 year ago
event-times-saved.php
158 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EventTimesSaved. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category EventTimesSaved |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\EventsManager\Triggers; |
| 15 | |
| 16 | use SureTriggers\Controllers\AutomationController; |
| 17 | use SureTriggers\Integrations\WordPress\WordPress; |
| 18 | use SureTriggers\Traits\SingletonLoader; |
| 19 | |
| 20 | if ( ! class_exists( 'EventTimesSaved' ) ) : |
| 21 | |
| 22 | /** |
| 23 | * EventTimesSaved |
| 24 | * |
| 25 | * @category EventTimesSaved |
| 26 | * @package SureTriggers |
| 27 | * @author BSF <username@example.com> |
| 28 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 29 | * @link https://www.brainstormforce.com/ |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | class EventTimesSaved { |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Integration type. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $integration = 'EventsManager'; |
| 41 | |
| 42 | /** |
| 43 | * Trigger name. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $trigger = 'em_event_times_saved'; |
| 48 | |
| 49 | use SingletonLoader; |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Constructor |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | */ |
| 57 | public function __construct() { |
| 58 | add_filter( 'sure_trigger_register_trigger', [ $this, 'register' ] ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Register action. |
| 63 | * |
| 64 | * @param array $triggers trigger data. |
| 65 | * @return array |
| 66 | */ |
| 67 | public function register( $triggers ) { |
| 68 | |
| 69 | $triggers[ $this->integration ][ $this->trigger ] = [ |
| 70 | 'label' => __( 'Event Times Updated', 'suretriggers' ), |
| 71 | 'action' => $this->trigger, |
| 72 | 'common_action' => 'em_event_save', |
| 73 | 'function' => [ $this, 'trigger_listener' ], |
| 74 | 'priority' => 10, |
| 75 | 'accepted_args' => 2, |
| 76 | ]; |
| 77 | |
| 78 | return $triggers; |
| 79 | |
| 80 | } |
| 81 | |
| 82 | |
| 83 | /** |
| 84 | * Trigger listener |
| 85 | * |
| 86 | * @param bool $result Whether the event was saved successfully. |
| 87 | * @param object $em_event The EM_Event object. |
| 88 | * |
| 89 | * @return bool The original result to preserve the filter chain. |
| 90 | */ |
| 91 | public function trigger_listener( $result, $em_event ) { |
| 92 | if ( ! $result ) { |
| 93 | return $result; |
| 94 | } |
| 95 | |
| 96 | if ( ! is_object( $em_event ) || ! property_exists( $em_event, 'event_id' ) || ! property_exists( $em_event, 'post_id' ) ) { |
| 97 | return $result; |
| 98 | } |
| 99 | |
| 100 | global $wpdb; |
| 101 | |
| 102 | $event = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_events WHERE event_id = %d", $em_event->event_id ) ); |
| 103 | |
| 104 | if ( empty( $event ) ) { |
| 105 | return $result; |
| 106 | } |
| 107 | |
| 108 | $context = (array) json_decode( (string) wp_json_encode( $event ), true ); |
| 109 | |
| 110 | if ( ! empty( $event->location_id ) ) { |
| 111 | $location = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_locations WHERE location_id = %d", $event->location_id ) ); |
| 112 | if ( ! empty( $location ) ) { |
| 113 | $context = array_merge( $context, (array) $location ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | $context['post_id'] = $event->post_id; |
| 118 | |
| 119 | if ( ! empty( $event->event_owner ) ) { |
| 120 | $context = array_merge( |
| 121 | WordPress::get_user_context( (int) $event->event_owner ), |
| 122 | $context |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | $timeranges = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}em_timeranges WHERE timerange_group_id = %s ORDER BY timerange_start ASC", 'event_' . $em_event->event_id ) ); |
| 127 | |
| 128 | if ( ! empty( $timeranges ) ) { |
| 129 | $context['timeranges_count'] = count( $timeranges ); |
| 130 | foreach ( $timeranges as $index => $timerange ) { |
| 131 | $num = $index + 1; |
| 132 | $context[ 'timerange_' . $num . '_start' ] = $timerange->timerange_start; |
| 133 | $context[ 'timerange_' . $num . '_end' ] = $timerange->timerange_end; |
| 134 | $context[ 'timerange_' . $num . '_all_day' ] = $timerange->timerange_all_day; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | AutomationController::sure_trigger_handle_trigger( |
| 139 | [ |
| 140 | 'trigger' => $this->trigger, |
| 141 | 'context' => $context, |
| 142 | ] |
| 143 | ); |
| 144 | |
| 145 | return $result; |
| 146 | } |
| 147 | |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Ignore false positive |
| 152 | * |
| 153 | * @psalm-suppress UndefinedMethod |
| 154 | */ |
| 155 | EventTimesSaved::get_instance(); |
| 156 | |
| 157 | endif; |
| 158 |