| 1 |
<?php |
| 2 |
/** |
| 3 |
* Visit Site |
| 4 |
* |
| 5 |
* @package AutomatorWP\Integrations\WordPress\Triggers\Visit_Site |
| 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_WordPress_Visit_Site extends AutomatorWP_Integration_Trigger { |
| 13 |
|
| 14 |
/** |
| 15 |
* Initialize the trigger |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
public function __construct( $integration ) { |
| 20 |
|
| 21 |
$this->integration = $integration; |
| 22 |
$this->trigger = $integration . '_visit_site'; |
| 23 |
|
| 24 |
parent::__construct(); |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the trigger |
| 30 |
* |
| 31 |
* @since 1.0.0 |
| 32 |
*/ |
| 33 |
public function register() { |
| 34 |
|
| 35 |
automatorwp_register_trigger( $this->trigger, array( |
| 36 |
'integration' => $this->integration, |
| 37 |
'label' => __( 'User daily visits the site', 'automatorwp' ), |
| 38 |
'select_option' => __( 'User <strong>daily visits</strong> the site', 'automatorwp' ), |
| 39 |
/* translators: %1$s: Number of times. */ |
| 40 |
'edit_label' => sprintf( __( 'User daily visits the site %1$s time(s)', 'automatorwp' ), '{times}' ), |
| 41 |
'log_label' => __( 'User daily visits the site', 'automatorwp' ), |
| 42 |
'action' => 'init', |
| 43 |
'function' => array( $this, 'listener' ), |
| 44 |
'priority' => 10, |
| 45 |
'accepted_args' => 1, |
| 46 |
'options' => array( |
| 47 |
'times' => automatorwp_utilities_times_option() |
| 48 |
), |
| 49 |
'tags' => array( |
| 50 |
'times' => automatorwp_utilities_times_tag( true ) |
| 51 |
) |
| 52 |
) ); |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Trigger listener |
| 58 |
* |
| 59 |
* @since 1.0.0 |
| 60 |
*/ |
| 61 |
public function listener() { |
| 62 |
|
| 63 |
$user_id = get_current_user_id(); |
| 64 |
|
| 65 |
// Bail if user is not logged in |
| 66 |
if( $user_id === 0 ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
// Get the user last visit |
| 71 |
$last_visit = get_user_meta( $user_id, '_automatorwp_last_visit', true ); |
| 72 |
|
| 73 |
if( ! empty( $last_visit ) ) { |
| 74 |
|
| 75 |
$today = date( 'Y-m-d', current_time( 'timestamp' ) ); |
| 76 |
$last_visit = date( 'Y-m-d', strtotime( $last_visit ) ); |
| 77 |
|
| 78 |
// Bail if user has visited the site today |
| 79 |
if( $last_visit === $today ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
// Update the user last visit for next check |
| 86 |
update_user_meta( $user_id, '_automatorwp_last_visit', date( 'Y-m-d 00:00:00', current_time( 'timestamp' ) ) ); |
| 87 |
|
| 88 |
automatorwp_trigger_event( array( |
| 89 |
'trigger' => $this->trigger, |
| 90 |
'user_id' => $user_id, |
| 91 |
) ); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
new AutomatorWP_WordPress_Visit_Site( 'wordpress' ); |
| 98 |
new AutomatorWP_WordPress_Visit_Site( 'users' ); |