| 1 |
<?php |
| 2 |
/** |
| 3 |
* Setup moment events. |
| 4 |
* |
| 5 |
* @package WpVr\Tracking |
| 6 |
* @since 8.5.37 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Wpvr\Admin\Tracking\Events; |
| 10 |
|
| 11 |
// Exit if accessed directly. |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
use Wpvr\Admin\Tracking\AbstractEvent; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class SetupEvents |
| 20 |
* |
| 21 |
* Tracks setup-related events. |
| 22 |
* |
| 23 |
* @package WpVr\Tracking\Events |
| 24 |
* @since 8.5.37 |
| 25 |
*/ |
| 26 |
class SetupEvents extends AbstractEvent { |
| 27 |
|
| 28 |
/** |
| 29 |
* Register WordPress hooks for this event. |
| 30 |
* |
| 31 |
* @since 8.5.37 |
| 32 |
*/ |
| 33 |
public function register_hooks() { |
| 34 |
add_action( 'wpvr_tour_status', array( $this, 'track_tour_status' ), 10, 2 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Track tour status changes. |
| 39 |
* |
| 40 |
* @param int $tour_id The ID of the tour post. |
| 41 |
* @param string $status The new status of the tour post. |
| 42 |
* @since 8.5.37 |
| 43 |
*/ |
| 44 |
public function track_tour_status( $tour_id, $status ) { |
| 45 |
$tour = get_post( $tour_id ); |
| 46 |
|
| 47 |
if ( ! $tour || $tour->post_type !== 'wpvr_item' ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$post_type = $tour->post_type; |
| 52 |
$post_type_object = get_post_type_object( $post_type ); |
| 53 |
$post_type_name = $post_type_object ? strtolower( $post_type_object->labels->singular_name ) : 'post'; |
| 54 |
|
| 55 |
$event_type = ''; |
| 56 |
$previous_status = get_post_meta( $tour_id, '_previous_status', true ); |
| 57 |
|
| 58 |
switch ( $status ) { |
| 59 |
case 'publish': |
| 60 |
if ( $previous_status === 'draft' || $previous_status === 'auto-draft' || empty( $previous_status ) ) { |
| 61 |
$event_type = 'first_' . $post_type_name . '_created'; |
| 62 |
$existing_posts = get_posts( array( |
| 63 |
'post_type' => $post_type, |
| 64 |
'post_status' => 'publish', |
| 65 |
'author' => $tour->post_author, |
| 66 |
'posts_per_page' => 1, |
| 67 |
'exclude' => array( $tour_id ), |
| 68 |
'fields' => 'ids' |
| 69 |
) ); |
| 70 |
|
| 71 |
if ( ! empty( $existing_posts ) ) { |
| 72 |
$event_type = $post_type_name . '_updated'; |
| 73 |
} |
| 74 |
} else { |
| 75 |
$event_type = $post_type_name . '_updated'; |
| 76 |
} |
| 77 |
break; |
| 78 |
|
| 79 |
case 'draft': |
| 80 |
if ( empty( $previous_status ) || $previous_status === 'auto-draft' ) { |
| 81 |
$event_type = $post_type_name . '_published'; |
| 82 |
} else { |
| 83 |
$event_type = $post_type_name . '_updated'; |
| 84 |
} |
| 85 |
break; |
| 86 |
|
| 87 |
default: |
| 88 |
$event_type = $post_type_name . '_updated'; |
| 89 |
break; |
| 90 |
} |
| 91 |
|
| 92 |
if ( ! empty( $event_type ) ) { |
| 93 |
$this->track_setup_moment( $event_type, array( |
| 94 |
'post_title' => $tour->post_title, |
| 95 |
'post_type' => $post_type, |
| 96 |
'creation_time' => current_time( 'c' ), |
| 97 |
) ); |
| 98 |
} |
| 99 |
update_post_meta( $tour_id, '_previous_status', $status ); |
| 100 |
} |
| 101 |
|
| 102 |
} |
| 103 |
|