← All changes
|
integrations/fluentcommunity/includes/triggers/join-space.php
+116
-0
5.7.9.2
→
6.0.3
View file →
| @@ -1,0 +1,116 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Join Space | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\FluentCommunity\Triggers\Join_Space | |
| 6 | + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.0.0 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +class AutomatorWP_FluentCommunity_Join_Space extends AutomatorWP_Integration_Trigger { | |
| 13 | + | |
| 14 | + public $integration = 'fluentcommunity'; | |
| 15 | + public $trigger = 'fluentcommunity_join_space'; | |
| 16 | + | |
| 17 | + /** | |
| 18 | + * Register the trigger | |
| 19 | + * | |
| 20 | + * @since 1.0.0 | |
| 21 | + */ | |
| 22 | + public function register() { | |
| 23 | + | |
| 24 | + automatorwp_register_trigger($this->trigger, array( | |
| 25 | + 'integration' => $this->integration, | |
| 26 | + 'label' => __( 'User joins a space', 'automatorwp' ), | |
| 27 | + 'select_option' => __( 'User joins a <strong>space</strong>', 'automatorwp' ), | |
| 28 | + /* translators: %1$s: Space title. %2$s: Number of times. */ | |
| 29 | + 'edit_label' => sprintf( __( 'User joins %1$s %2$s time(s)', 'automatorwp' ), '{space}','{times}' ), | |
| 30 | + /* translators: %1$s: Space title. */ | |
| 31 | + 'log_label' => sprintf( __( 'User joins %1$s', 'automatorwp' ), '{space}','{times}' ), | |
| 32 | + 'action' => 'fluent_community/space/joined', | |
| 33 | + 'function' => array($this, 'listener'), | |
| 34 | + 'priority' => 10, | |
| 35 | + 'accepted_args' => 3, | |
| 36 | + 'options' => array( | |
| 37 | + 'space' => automatorwp_utilities_ajax_selector_option( array( | |
| 38 | + 'field' => 'space', | |
| 39 | + 'name' => __( 'Space:', 'automatorwp' ), | |
| 40 | + 'option_none_value' => 'any', | |
| 41 | + 'option_none_label' => __( 'any space', 'automatorwp' ), | |
| 42 | + 'action_cb' => 'automatorwp_fluentcommunity_get_spaces', | |
| 43 | + 'options_cb' => 'automatorwp_fluentcommunity_options_cb_space', | |
| 44 | + 'default' => 'any' | |
| 45 | + ) ), | |
| 46 | + 'times' => automatorwp_utilities_times_option(), | |
| 47 | + ), | |
| 48 | + 'tags' => array_merge( | |
| 49 | + automatorwp_utilities_times_tag() | |
| 50 | + ) | |
| 51 | + )); | |
| 52 | + } | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Trigger listener | |
| 56 | + * | |
| 57 | + * @since 1.0.0 | |
| 58 | + * | |
| 59 | + * @param object $space | |
| 60 | + * @param int $userId | |
| 61 | + * @param string $by Self, by_admin, by_automation | |
| 62 | + */ | |
| 63 | + public function listener( $space, $userId, $by ) { | |
| 64 | + | |
| 65 | + $user_id = get_current_user_id(); | |
| 66 | + | |
| 67 | + // Login is required | |
| 68 | + if ( $user_id === 0 ) { | |
| 69 | + return; | |
| 70 | + } | |
| 71 | + | |
| 72 | + $space_data = $space->getOriginal(); | |
| 73 | + | |
| 74 | + // Trigger event when user performs a comment action | |
| 75 | + automatorwp_trigger_event(array( | |
| 76 | + 'trigger' => $this->trigger, | |
| 77 | + 'user_id' => $user_id, | |
| 78 | + 'space_id' => $space_data['id'], | |
| 79 | + )); | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * User deserves check | |
| 84 | + * | |
| 85 | + * @since 1.0.0 | |
| 86 | + * | |
| 87 | + * @param bool $deserves_trigger True if user deserves trigger, false otherwise | |
| 88 | + * @param stdClass $trigger The trigger object | |
| 89 | + * @param int $user_id The user ID | |
| 90 | + * @param array $event Event information | |
| 91 | + * @param array $trigger_options The trigger's stored options | |
| 92 | + * @param stdClass $automation The trigger's automation object | |
| 93 | + * | |
| 94 | + * @return bool True if user deserves trigger, false otherwise | |
| 95 | + */ | |
| 96 | + | |
| 97 | + | |
| 98 | + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { | |
| 99 | + | |
| 100 | + // Don't deserve if post is not received | |
| 101 | + if( ! isset( $event['space_id'] ) ) { | |
| 102 | + return false; | |
| 103 | + } | |
| 104 | + | |
| 105 | + // Don't deserve if post doesn't match with the trigger option | |
| 106 | + if( $trigger_options['space'] !== 'any' && absint( $trigger_options['space'] ) !== absint( $event['space_id'] ) ) { | |
| 107 | + return false; | |
| 108 | + } | |
| 109 | + | |
| 110 | + return $deserves_trigger; | |
| 111 | + | |
| 112 | + } | |
| 113 | + | |
| 114 | +} | |
| 115 | + | |
| 116 | +new AutomatorWP_FluentCommunity_Join_Space(); | |