| 1 |
<?php |
| 2 |
/** |
| 3 |
* Create Topic |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\bbPress\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_bbPress_Create_Topic extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'bbpress'; |
| 15 |
public $trigger = 'bbpress_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', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User creates <strong>a topic</strong>', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Forum title. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User creates a topic in %1$s %2$s time(s)', 'automatorwp' ), '{forum}', '{times}' ), |
| 30 |
/* translators: %1$s: Forum title. */ |
| 31 |
'log_label' => sprintf( __( 'User creates a topic in %1$s', 'automatorwp' ), '{forum}' ), |
| 32 |
'action' => 'bbp_new_topic', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 4, |
| 36 |
'options' => array( |
| 37 |
'forum' => array( |
| 38 |
'from' => 'forum', |
| 39 |
'fields' => array( |
| 40 |
'forum' => automatorwp_utilities_post_field( array( |
| 41 |
'name' => __( 'Forum:', 'automatorwp' ), |
| 42 |
'option_none_label' => __( 'any forum', 'automatorwp' ), |
| 43 |
'post_type_cb' => 'bbp_get_forum_post_type' |
| 44 |
) ) |
| 45 |
) |
| 46 |
), |
| 47 |
'times' => automatorwp_utilities_times_option(), |
| 48 |
), |
| 49 |
'tags' => array_merge( |
| 50 |
automatorwp_utilities_post_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 $topic_id |
| 63 |
* @param int $forum_id |
| 64 |
* @param array $anonymous_data |
| 65 |
* @param int $topic_author |
| 66 |
*/ |
| 67 |
public function listener( $topic_id, $forum_id, $anonymous_data, $topic_author ) { |
| 68 |
|
| 69 |
$user_id = $topic_author; |
| 70 |
|
| 71 |
// Bail if not user provided |
| 72 |
if( $user_id === 0 ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
// Trigger the create topic |
| 77 |
automatorwp_trigger_event( array( |
| 78 |
'trigger' => $this->trigger, |
| 79 |
'user_id' => $user_id, |
| 80 |
'post_id' => $topic_id, |
| 81 |
'forum_id' => $forum_id, |
| 82 |
) ); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* User deserves check |
| 88 |
* |
| 89 |
* @since 1.0.0 |
| 90 |
* |
| 91 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 92 |
* @param stdClass $trigger The trigger object |
| 93 |
* @param int $user_id The user ID |
| 94 |
* @param array $event Event information |
| 95 |
* @param array $trigger_options The trigger's stored options |
| 96 |
* @param stdClass $automation The trigger's automation object |
| 97 |
* |
| 98 |
* @return bool True if user deserves trigger, false otherwise |
| 99 |
*/ |
| 100 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 101 |
|
| 102 |
// Don't deserve if post is not received |
| 103 |
if( ! isset( $event['post_id'] ) && ! isset( $event['forum_id'] ) ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
// Don't deserve if forum doesn't match with the trigger option |
| 108 |
if( ! automatorwp_posts_matches( $event['forum_id'], $trigger_options['forum'] ) ) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
return $deserves_trigger; |
| 113 |
|
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
new AutomatorWP_bbPress_Create_Topic(); |