| 1 |
<?php |
| 2 |
/** |
| 3 |
* Send Message |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WPAdverts\Triggers\Send_Message |
| 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_WPAdverts_Send_Message extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
public $integration = 'wpadverts'; |
| 15 |
public $trigger = 'wpadverts_send_message'; |
| 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 sends a message to an advert author', 'automatorwp' ), |
| 27 |
'select_option' => __( 'User <strong>sends a message</strong> to an advert author', 'automatorwp' ), |
| 28 |
/* translators: %1$s: Post Title. %2$s: Number of times. */ |
| 29 |
'edit_label' => sprintf( __( 'User sends a message to %1$s author time(s)', 'automatorwp' ), '{post}', '{times}' ), |
| 30 |
/* translators: %1$s: Post Title. */ |
| 31 |
'log_label' => sprintf( __( 'User sends a message to %1$s author', 'automatorwp' ), '{post}' ), |
| 32 |
'action' => 'adext_contact_form_send', |
| 33 |
'function' => array( $this, 'listener' ), |
| 34 |
'priority' => 10, |
| 35 |
'accepted_args' => 2, |
| 36 |
'options' => array( |
| 37 |
'post' => automatorwp_utilities_post_option( array( |
| 38 |
'name' => __( 'Advert:', 'automatorwp' ), |
| 39 |
'option_none_label' => __( 'any advert', 'automatorwp' ), |
| 40 |
'post_type' => 'advert' |
| 41 |
) ), |
| 42 |
'times' => automatorwp_utilities_times_option(), |
| 43 |
), |
| 44 |
'tags' => array_merge( |
| 45 |
automatorwp_utilities_post_tags( __( 'Advert', 'automatorwp' ) ), |
| 46 |
automatorwp_utilities_times_tag() |
| 47 |
) |
| 48 |
) ); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Trigger listener |
| 54 |
* |
| 55 |
* @since 1.0.0 |
| 56 |
* |
| 57 |
* @param int $post_id |
| 58 |
* @param Adverts_Form $form |
| 59 |
*/ |
| 60 |
public function listener( $post_id, $form ) { |
| 61 |
|
| 62 |
$post = get_post( $post_id ); |
| 63 |
|
| 64 |
// Bail if post not exists |
| 65 |
if( ! $post ) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Bail if not is an advert |
| 70 |
if( $post->post_type !== 'advert' ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// First try to get the user from the message email |
| 75 |
$email = $form->get_value( 'message_email' ); |
| 76 |
$user = get_user_by_email( $email ); |
| 77 |
|
| 78 |
if( $user ) { |
| 79 |
$user_id = $user->ID; |
| 80 |
} |
| 81 |
|
| 82 |
if( $user_id === 0 ) { |
| 83 |
$user_id = get_current_user_id(); |
| 84 |
} |
| 85 |
|
| 86 |
// Bail if can't find the user ID |
| 87 |
if( $user_id === 0 ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
automatorwp_trigger_event( array( |
| 92 |
'trigger' => $this->trigger, |
| 93 |
'user_id' => $user_id, |
| 94 |
'post_id' => $post->ID, |
| 95 |
) ); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* User deserves check |
| 101 |
* |
| 102 |
* @since 1.0.0 |
| 103 |
* |
| 104 |
* @param bool $deserves_trigger True if user deserves trigger, false otherwise |
| 105 |
* @param stdClass $trigger The trigger object |
| 106 |
* @param int $user_id The user ID |
| 107 |
* @param array $event Event information |
| 108 |
* @param array $trigger_options The trigger's stored options |
| 109 |
* @param stdClass $automation The trigger's automation object |
| 110 |
* |
| 111 |
* @return bool True if user deserves trigger, false otherwise |
| 112 |
*/ |
| 113 |
public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) { |
| 114 |
|
| 115 |
// Don't deserve if post is not received |
| 116 |
if( ! isset( $event['post_id'] ) ) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
// Don't deserve if post doesn't match with the trigger option |
| 121 |
if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
|
| 125 |
return $deserves_trigger; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
new AutomatorWP_WPAdverts_Send_Message(); |