| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create Topic |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\Asgaros_Forum\Triggers\Create_Topic |
| 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_Asgaros_Forum_Create_Topic extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'asgaros_forum'; |
| 15 |
public $trigger = 'asgaros_forum_create_topic'; |
| 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 creates a topic on a forum', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User <strong>creates a topic</strong> on a forum', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Forum title. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User creates a topic on %1$s %2$s time(s)', 'automatorwp' ), '{forum}', '{times}' ), |
| 30 |
/* translators: %1$s: Forum title. */ |
| 31 |
'log_label' => sprintf( __( 'User creates a topic on %1$s', 'automatorwp' ), '{forum}' ), |
| 32 |
'action' => 'asgarosforum_after_add_topic_submit', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 6, |
| 36 |
'options' => array( |
| 37 |
'forum' => automatorwp_utilities_ajax_selector_option( array( |
| 38 |
'field' => 'forum', |
| 39 |
'name' => __( 'Forum:', 'automatorwp' ), |
| 40 |
'option_none_value' => 'any', |
| 41 |
'option_none_label' => __( 'any forum', 'automatorwp' ), |
| 42 |
'action_cb' => 'automatorwp_asgaros_forum_get_forums', |
| 43 |
'options_cb' => 'automatorwp_asgaros_forum_options_cb_forum', |
| 44 |
'default' => 'any' |
| 45 |
) ), |
| 46 |
'times' => automatorwp_utilities_times_option(), |
| 47 |
), |
| 48 |
'tags' => array_merge( |
| 49 |
automatorwp_asgaros_forum_get_general_tags(), |
| 50 |
automatorwp_asgaros_forum_get_topic_tags(), |
| 51 |
automatorwp_utilities_times_tag() |
| 52 |
), |
| 53 |
|
| 54 |
) ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Trigger listener |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* |
| 62 |
* @param int $post_id |
| 63 |
* @param int $topic_id |
| 64 |
* @param array $post_data |
| 65 |
* |
| 66 |
*/ |
| 67 |
public function listener( $post_id, $topic_id, $subject, $content, $link, $author_id ) { |
| 68 |
|
| 69 |
$user_id = get_current_user_id(); |
| 70 |
|
| 71 |
// Login is required |
| 72 |
if ( $user_id === 0 ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// To get topic parent |
| 77 |
$forum_id = automatorwp_asgaros_forum_get_forum_by_topic( $topic_id ); |
| 78 |
|
| 79 |
automatorwp_trigger_event( array( |
| 80 |
'trigger' => $this->trigger, |
| 81 |
'user_id' => $author_id, |
| 82 |
'forum_id' => $forum_id, |
| 83 |
'topic_id' => $topic_id, |
| 84 |
'topic_title' => $subject, |
| 85 |
'topic_content' => $content, |
| 86 |
'topic_link' => $link, |
| 87 |
) ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* User deserves check |
| 92 |
* |
| 93 |
* @since 1.0.0 |
| 94 |
* |
| 95 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 96 |
* @param stdClass $trigger The trigger object |
| 97 |
* @param int $user_id The user ID |
| 98 |
* @param array $event Event inpostation |
| 99 |
* @param array $trigger_options The trigger's stored options |
| 100 |
* @param stdClass $automation The trigger's automation object |
| 101 |
* |
| 102 |
* @return bool True if user deserves trigger, false otherwise |
| 103 |
*/ |
| 104 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 105 |
|
| 106 |
// Don't deserve if post is not received |
| 107 |
if( ! isset( $event['forum_id'] ) ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
// Don't deserve if post doesn't match with the trigger option |
| 112 |
if( $trigger_options['forum'] !== 'any' && absint( $trigger_options['forum'] ) !== absint( $event['forum_id'] ) ) { |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
return $deserves_trigger; |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Register the required hooks |
| 122 |
* |
| 123 |
* @since 1.0.0 |
| 124 |
*/ |
| 125 |
public function hooks() { |
| 126 |
|
| 127 |
// Log meta data |
| 128 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 129 |
|
| 130 |
parent::hooks(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Trigger custom log meta |
| 135 |
* |
| 136 |
* @since 1.0.0 |
| 137 |
* |
| 138 |
* @param array $log_meta Log meta data |
| 139 |
* @param stdClass $trigger The trigger object |
| 140 |
* @param int $user_id The user ID |
| 141 |
* @param array $event Event inpostation |
| 142 |
* @param array $trigger_options The trigger's stored options |
| 143 |
* @param stdClass $automation The trigger's automation object |
| 144 |
* |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 148 |
|
| 149 |
// Bail if action type don't match this action |
| 150 |
if( $trigger->type !== $this->trigger ) { |
| 151 |
return $log_meta; |
| 152 |
} |
| 153 |
|
| 154 |
$log_meta['forum_id'] = isset( $event['forum_id'] ) ? $event['forum_id'] : ''; |
| 155 |
$log_meta['topic_id'] = isset( $event['topic_id'] ) ? $event['topic_id'] : ''; |
| 156 |
$log_meta['topic_title'] = isset( $event['topic_title'] ) ? $event['topic_title'] : ''; |
| 157 |
$log_meta['topic_content'] = isset( $event['topic_content'] ) ? $event['topic_content'] : ''; |
| 158 |
$log_meta['topic_link'] = isset( $event['topic_link'] ) ? $event['topic_link'] : ''; |
| 159 |
|
| 160 |
return $log_meta; |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
new AutomatorWP_Asgaros_Forum_Create_Topic(); |