PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | integrations/wordpress/triggers/view-post.php +131 -0 5.7.2 → 6.0.3 View file →
@@ -1,0 +1,131 @@
1 +<?php
2 +/**
3 + * View Post
4 + *
5 + * @package AutomatorWP\Integrations\WordPress\Triggers\View_Post
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_View_Post 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 . '_view_post';
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 views a post', 'automatorwp' ),
38 + 'select_option' => __( 'User views <strong>a post</strong>', 'automatorwp' ),
39 + /* translators: %1$s: Post title. %2$s: Number of times. */
40 + 'edit_label' => sprintf( __( 'User views %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
41 + /* translators: %1$s: Post title. */
42 + 'log_label' => sprintf( __( 'User views %1$s', 'automatorwp' ), '{post}' ),
43 + 'action' => 'template_redirect',
44 + 'function' => array( $this, 'listener' ),
45 + 'priority' => 10,
46 + 'accepted_args' => 1,
47 + 'options' => array(
48 + 'post' => automatorwp_utilities_post_option(),
49 + 'times' => automatorwp_utilities_times_option(),
50 + ),
51 + 'tags' => array_merge(
52 + automatorwp_utilities_post_tags(),
53 + automatorwp_utilities_times_tag()
54 + )
55 + ) );
56 +
57 + }
58 +
59 + /**
60 + * Trigger listener
61 + *
62 + * @since 1.0.0
63 + */
64 + public function listener() {
65 +
66 + global $post;
67 +
68 + // Bail if in admin area
69 + if( is_admin() ) {
70 + return;
71 + }
72 +
73 + // Bail if not post instanced
74 + if( ! $post instanceof WP_Post ) {
75 + return;
76 + }
77 +
78 + // Bail if post type is not a post
79 + if( $post->post_type !== 'post' ) {
80 + return;
81 + }
82 +
83 + $user_id = get_current_user_id();
84 +
85 + // Bail if user is not logged in
86 + if( $user_id === 0 ) {
87 + return;
88 + }
89 +
90 + automatorwp_trigger_event( array(
91 + 'trigger' => $this->trigger,
92 + 'user_id' => $user_id,
93 + 'post_id' => $post->ID,
94 + ) );
95 +
96 + }
97 +
98 + /**
99 + * User deserves check
100 + *
101 + * @since 1.0.0
102 + *
103 + * @param bool $deserves_trigger True if user deserves trigger, false otherwise
104 + * @param stdClass $trigger The trigger object
105 + * @param int $user_id The user ID
106 + * @param array $event Event information
107 + * @param array $trigger_options The trigger's stored options
108 + * @param stdClass $automation The trigger's automation object
109 + *
110 + * @return bool True if user deserves trigger, false otherwise
111 + */
112 + public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
113 +
114 + // Don't deserve if post is not received
115 + if( ! isset( $event['post_id'] ) ) {
116 + return false;
117 + }
118 +
119 + // Don't deserve if post doesn't match with the trigger option
120 + if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
121 + return false;
122 + }
123 +
124 + return $deserves_trigger;
125 +
126 + }
127 +
128 +}
129 +
130 +new AutomatorWP_WordPress_View_Post( 'wordpress' );
131 +new AutomatorWP_WordPress_View_Post( 'posts' );