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
automatorwp / integrations / wordpress / triggers / view-page.php

view-page.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.3, at integrations/wordpress/triggers/view-page.php

135 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * View Page
4 *
5 * @package AutomatorWP\Integrations\WordPress\Triggers\View_Page
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_Page 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_page';
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 page', 'automatorwp' ),
38 'select_option' => __( 'User views <strong>a page</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( array(
49 'name' => __( 'Page:', 'automatorwp' ),
50 'option_none_label' => __( 'any page', 'automatorwp' ),
51 'post_type' => 'page'
52 ) ),
53 'times' => automatorwp_utilities_times_option(),
54 ),
55 'tags' => array_merge(
56 automatorwp_utilities_post_tags( __( 'Page', 'automatorwp' ) ),
57 automatorwp_utilities_times_tag()
58 )
59 ) );
60
61 }
62
63 /**
64 * Trigger listener
65 *
66 * @since 1.0.0
67 */
68 public function listener() {
69
70 global $post;
71
72 // Bail if in admin area
73 if( is_admin() ) {
74 return;
75 }
76
77 // Bail if not post instanced
78 if( ! $post instanceof WP_Post ) {
79 return;
80 }
81
82 // Bail if post type is not a page
83 if( $post->post_type !== 'page' ) {
84 return;
85 }
86
87 $user_id = get_current_user_id();
88
89 // Bail if user is not logged in
90 if( $user_id === 0 ) {
91 return;
92 }
93
94 automatorwp_trigger_event( array(
95 'trigger' => $this->trigger,
96 'user_id' => $user_id,
97 'post_id' => $post->ID,
98 ) );
99
100 }
101
102 /**
103 * User deserves check
104 *
105 * @since 1.0.0
106 *
107 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
108 * @param stdClass $trigger The trigger object
109 * @param int $user_id The user ID
110 * @param array $event Event information
111 * @param array $trigger_options The trigger's stored options
112 * @param stdClass $automation The trigger's automation object
113 *
114 * @return bool True if user deserves trigger, false otherwise
115 */
116 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
117
118 // Don't deserve if post is not received
119 if( ! isset( $event['post_id'] ) ) {
120 return false;
121 }
122
123 // Don't deserve if post doesn't match with the trigger option
124 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
125 return false;
126 }
127
128 return $deserves_trigger;
129
130 }
131
132 }
133
134 new AutomatorWP_WordPress_View_Page( 'wordpress' );
135 new AutomatorWP_WordPress_View_Page( 'posts' );