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 / presto-player / includes / triggers / watch-video.php

watch-video.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.3, at integrations/presto-player/includes/triggers/watch-video.php

171 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Watch Video
4 *
5 * @package AutomatorWP\Integrations\Presto_Player\Triggers\Watch_Video
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_Presto_Player_Watch_Video extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'presto_player';
15 public $trigger = 'presto_player_watch_video';
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 fully watches a video', 'automatorwp' ),
27 'select_option' => __( 'User fully watches a <strong>video</strong>', 'automatorwp' ),
28 /* translators: %1$s: Post Title. %2$s: Number of times. */
29 'edit_label' => sprintf( __( 'User fully watches %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
30 /* translators: %1$s: Post Title. */
31 'log_label' => sprintf( __( 'User fully watches %1$s', 'automatorwp' ), '{post}' ),
32 'action' => 'presto_player_progress',
33 'function' => array( $this, 'listener' ),
34 'priority' => 10,
35 'accepted_args' => 2,
36 'options' => array(
37 'post' => automatorwp_utilities_post_option( array(
38 'name' => __( 'Video:', 'automatorwp' ),
39 'option_none_label' => __( 'any video', 'automatorwp' ),
40 'post_type' => 'pp_video_block'
41 ) ),
42 'times' => automatorwp_utilities_times_option(),
43 ),
44 'tags' => array_merge(
45 automatorwp_presto_player_video_tags(),
46 automatorwp_utilities_times_tag()
47 )
48 ) );
49
50 }
51
52 /**
53 * Trigger listener
54 *
55 * @since 1.0.0
56 *
57 * @param int $video_id
58 * @param int $percent
59 */
60 public function listener( $video_id, $percent ) {
61
62 $user_id = get_current_user_id();
63
64 // Bail if user is not logged in
65 if( $user_id === 0 ) {
66 return;
67 }
68
69 // Bail if user not watched the video entirely
70 if( $percent < 100 ) {
71 return;
72 }
73
74 automatorwp_trigger_event( array(
75 'trigger' => $this->trigger,
76 'user_id' => $user_id,
77 'video_id' => $video_id,
78 ) );
79
80 }
81
82 /**
83 * User deserves check
84 *
85 * @since 1.0.0
86 *
87 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
88 * @param stdClass $trigger The trigger object
89 * @param int $user_id The user ID
90 * @param array $event Event information
91 * @param array $trigger_options The trigger's stored options
92 * @param stdClass $automation The trigger's automation object
93 *
94 * @return bool True if user deserves trigger, false otherwise
95 */
96 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
97
98 // Don't deserve if video is not received
99 if( ! isset( $event['video_id'] ) ) {
100 return false;
101 }
102
103 $video_id = absint( $event['video_id'] );
104
105 // Don't deserve if video doesn't exists
106 if( $video_id === 0 ) {
107 return false;
108 }
109
110 $model = new \PrestoPlayer\Models\Video();
111
112 if( ! $model ) {
113 return '';
114 }
115
116 $video = $model->get( $video_id );
117
118 $video_post_id = $video->__get( 'post_id' );
119
120 // Don't deserve if video doesn't match with the trigger option
121 if( $trigger_options['post'] !== 'any' && absint( $video_post_id ) !== absint( $trigger_options['post'] ) ) {
122 return false;
123 }
124
125 return $deserves_trigger;
126
127 }
128
129 /**
130 * Register the required hooks
131 *
132 * @since 1.0.0
133 */
134 public function hooks() {
135
136 // Log meta data
137 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
138
139 parent::hooks();
140 }
141
142 /**
143 * Trigger custom log meta
144 *
145 * @since 1.0.0
146 *
147 * @param array $log_meta Log meta data
148 * @param stdClass $trigger The trigger object
149 * @param int $user_id The user ID
150 * @param array $event Event information
151 * @param array $trigger_options The trigger's stored options
152 * @param stdClass $automation The trigger's automation object
153 *
154 * @return array
155 */
156 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
157
158 // Bail if action type don't match this action
159 if( $trigger->type !== $this->trigger ) {
160 return $log_meta;
161 }
162
163 $log_meta['video_id'] = ( isset( $event['video_id'] ) ? $event['video_id'] : 0 );
164
165 return $log_meta;
166
167 }
168
169 }
170
171 new AutomatorWP_Presto_Player_Watch_Video();