| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post Added |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\FluentCommunity\Triggers\Post_Added |
| 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_Post_Added extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'fluentcommunity'; |
| 15 |
public $trigger = 'fluentcommunity_post_added'; |
| 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 writes a post in a space', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User writes a <strong>post</strong> in a space', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Space title. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User writes a post in %1$s %2$s time(s)', 'automatorwp' ), '{space}', '{times}' ), |
| 30 |
/* translators: %1$s: Space title. */ |
| 31 |
'log_label' => sprintf( __( 'User writes a post in %1$s', 'automatorwp' ), '{space}' ), |
| 32 |
'action' => 'fluent_community/feed/created', |
| 33 |
'function' => array($this, 'listener'), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 1, |
| 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 $feed |
| 60 |
*/ |
| 61 |
public function listener( $feed ) { |
| 62 |
|
| 63 |
$user_id = get_current_user_id(); |
| 64 |
|
| 65 |
// Login is required |
| 66 |
if ( $user_id === 0 ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$feed_data = $feed->getOriginal(); |
| 71 |
|
| 72 |
// Trigger event when user writes a post in a space |
| 73 |
automatorwp_trigger_event(array( |
| 74 |
'trigger' => $this->trigger, |
| 75 |
'user_id' => $user_id, |
| 76 |
'comment' => $feed_data['message'], |
| 77 |
'comment_id' => $feed_data['id'], |
| 78 |
'space_id' => $feed_data['space_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 |
* Register the required hooks |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
*/ |
| 119 |
public function hooks() { |
| 120 |
|
| 121 |
// Log meta data |
| 122 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 123 |
|
| 124 |
// Log fields |
| 125 |
add_filter( 'automatorwp_log_fields', array( $this, 'log_fields' ), 10, 5 ); |
| 126 |
|
| 127 |
parent::hooks(); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Trigger custom log meta |
| 132 |
* |
| 133 |
* @since 1.0.0 |
| 134 |
* |
| 135 |
* @param array $log_meta Log meta data |
| 136 |
* @param stdClass $trigger The trigger object |
| 137 |
* @param int $user_id The user ID |
| 138 |
* @param array $event Event information |
| 139 |
* @param array $trigger_options The trigger's stored options |
| 140 |
* @param stdClass $automation The trigger's automation object |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 145 |
|
| 146 |
// Bail if action type don't match this action |
| 147 |
if( $trigger->type !== $this->trigger ) { |
| 148 |
return $log_meta; |
| 149 |
} |
| 150 |
|
| 151 |
$log_meta['comment'] = ( isset( $event['comment'] ) ? $event['comment'] : array() ); |
| 152 |
|
| 153 |
return $log_meta; |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Action custom log fields |
| 159 |
* |
| 160 |
* @since 1.0.0 |
| 161 |
* |
| 162 |
* @param array $log_fields The log fields |
| 163 |
* @param stdClass $log The log object |
| 164 |
* @param stdClass $object The trigger/action/automation object attached to the log |
| 165 |
* |
| 166 |
* @return array |
| 167 |
*/ |
| 168 |
public function log_fields( $log_fields, $log, $object ) { |
| 169 |
|
| 170 |
// Bail if log is not assigned to an trigger |
| 171 |
if( $log->type !== 'trigger' ) { |
| 172 |
return $log_fields; |
| 173 |
} |
| 174 |
|
| 175 |
// Bail if trigger type don't match this trigger |
| 176 |
if( $object->type !== $this->trigger ) { |
| 177 |
return $log_fields; |
| 178 |
} |
| 179 |
|
| 180 |
$log_fields['comment'] = array( |
| 181 |
'name' => __( 'Post content', 'automatorwp' ), |
| 182 |
'type' => 'text', |
| 183 |
); |
| 184 |
|
| 185 |
return $log_fields; |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
new AutomatorWP_FluentCommunity_Post_Added(); |