| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create Topic |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\wpForo\Triggers\Create_Topic |
| 6 |
* @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
class AutomatorWP_wpForo_Create_Topic extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'wpforo'; |
| 15 |
public $trigger = 'wpforo_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 of %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' => 'wpforo_after_add_topic', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 1, |
| 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_wpforo_get_forums', |
| 43 |
'options_cb' => 'automatorwp_wpforo_options_cb_forum', |
| 44 |
'default' => 'any' |
| 45 |
) ), |
| 46 |
'times' => automatorwp_utilities_times_option(), |
| 47 |
), |
| 48 |
'tags' => array_merge( |
| 49 |
automatorwp_wpforo_forum_tags(), |
| 50 |
automatorwp_utilities_times_tag() |
| 51 |
) |
| 52 |
) ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Trigger listener |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
* |
| 61 |
* @param array $topic |
| 62 |
*/ |
| 63 |
public function listener( $topic ) { |
| 64 |
|
| 65 |
$user_id = $topic['userid']; |
| 66 |
|
| 67 |
// Bail if not user provided |
| 68 |
if( $user_id === 0 ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$topic_id = $topic['topicid']; |
| 73 |
$forum_id = strval( $topic['forumid'] ); |
| 74 |
|
| 75 |
// Get the current Board |
| 76 |
$board_id = WPF()->board->get_current( 'boardid' ); |
| 77 |
|
| 78 |
// Boards other than the main one (ID=0) take data from other additional tables in the database |
| 79 |
if ( absint( $board_id ) !== 0 ){ |
| 80 |
$forum_id = $board_id . '-' . $topic['forumid']; |
| 81 |
} |
| 82 |
|
| 83 |
// Trigger the create a reply |
| 84 |
automatorwp_trigger_event( array( |
| 85 |
'trigger' => $this->trigger, |
| 86 |
'user_id' => $user_id, |
| 87 |
'post_id' => $topic_id, |
| 88 |
'forum_id' => $forum_id, |
| 89 |
) ); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* User deserves check |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* |
| 98 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 99 |
* @param stdClass $trigger The trigger object |
| 100 |
* @param int $user_id The user ID |
| 101 |
* @param array $event Event information |
| 102 |
* @param array $trigger_options The trigger's stored options |
| 103 |
* @param stdClass $automation The trigger's automation object |
| 104 |
* |
| 105 |
* @return bool True if user deserves trigger, false otherwise |
| 106 |
*/ |
| 107 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 108 |
|
| 109 |
// Don't deserve if post is not received |
| 110 |
if( ! isset( $event['post_id'] ) && ! isset( $event['forum_id'] ) ) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
// Don't deserve if forum doesn't match with the trigger option |
| 115 |
if( $trigger_options['forum'] !== 'any' && $event['forum_id'] !== $trigger_options['forum'] ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return $deserves_trigger; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Register the required hooks |
| 125 |
* |
| 126 |
* @since 1.0.0 |
| 127 |
*/ |
| 128 |
public function hooks() { |
| 129 |
|
| 130 |
// Log meta data |
| 131 |
add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 ); |
| 132 |
|
| 133 |
parent::hooks(); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Trigger custom log meta |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* |
| 141 |
* @param array $log_meta Log meta data |
| 142 |
* @param stdClass $trigger The trigger object |
| 143 |
* @param int $user_id The user ID |
| 144 |
* @param array $event Event information |
| 145 |
* @param array $trigger_options The trigger's stored options |
| 146 |
* @param stdClass $automation The trigger's automation object |
| 147 |
* |
| 148 |
* @return array |
| 149 |
*/ |
| 150 |
function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 151 |
|
| 152 |
// Bail if action type don't match this action |
| 153 |
if( $trigger->type !== $this->trigger ) { |
| 154 |
return $log_meta; |
| 155 |
} |
| 156 |
|
| 157 |
$log_meta['forum_id'] = ( isset( $event['forum_id'] ) ? $event['forum_id'] : 0 ); |
| 158 |
|
| 159 |
return $log_meta; |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
new AutomatorWP_wpForo_Create_Topic(); |