PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 7.9.8
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v7.9.8
8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 All 46 releases
gamipress / integrations / youtube / includes / triggers.php

triggers.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI 7.9.8, at integrations/youtube/includes/triggers.php

165 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Triggers
4 *
5 * @package GamiPress\Youtube\Triggers
6 * @since 1.0.0
7 */
8
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Register plugin specific triggers
14 *
15 * @param array $triggers
16 * @return mixed
17 */
18 function gamipress_youtube_activity_triggers( $triggers ) {
19
20 $triggers[__( 'Youtube', 'gamipress' )] = array(
21 'gamipress_youtube_watch_video' => __( 'Watch any video', 'gamipress' ),
22 'gamipress_youtube_watch_specific_video' => __( 'Watch a specific video', 'gamipress' ),
23 );
24
25 return $triggers;
26
27 }
28 add_filter( 'gamipress_activity_triggers', 'gamipress_youtube_activity_triggers' );
29
30 /**
31 * Get user for a given trigger action.
32 *
33 * @since 1.0.0
34 *
35 * @param integer $user_id user ID to override.
36 * @param string $trigger Trigger name.
37 * @param array $args Passed trigger args.
38 * @return integer User ID.
39 */
40 function gamipress_youtube_trigger_get_user_id( $user_id, $trigger, $args ) {
41
42 switch ( $trigger ) {
43 case 'gamipress_youtube_watch_video':
44 case 'gamipress_youtube_watch_specific_video':
45 $user_id = $args[1];
46 break;
47 }
48
49 return $user_id;
50
51 }
52 add_filter( 'gamipress_trigger_get_user_id', 'gamipress_youtube_trigger_get_user_id', 10, 3 );
53
54 /**
55 * Extended meta data for event trigger logging
56 *
57 * @since 1.0.0
58 *
59 * @param array $log_meta
60 * @param integer $user_id
61 * @param string $trigger
62 * @param integer $site_id
63 * @param array $args
64 *
65 * @return array
66 */
67 function gamipress_youtube_log_event_trigger_meta_data( $log_meta, $user_id, $trigger, $site_id, $args ) {
68
69 switch ( $trigger ) {
70 case 'gamipress_youtube_watch_video':
71 case 'gamipress_youtube_watch_specific_video':
72 // Add the video ID, seconds, duration and the post ID watched
73 $log_meta['video_id'] = $args[0];
74 $log_meta['seconds'] = $args[2];
75 $log_meta['duration'] = $args[3];
76 $log_meta['post_id'] = $args[4];
77 break;
78 }
79
80 return $log_meta;
81 }
82 add_filter( 'gamipress_log_event_trigger_meta_data', 'gamipress_youtube_log_event_trigger_meta_data', 10, 5 );
83
84 /**
85 * Extra data fields
86 *
87 * @since 1.0.0
88 *
89 * @param array $fields
90 * @param int $log_id
91 * @param string $type
92 *
93 * @return array
94 */
95 function gamipress_youtube_log_extra_data_fields( $fields, $log_id, $type ) {
96
97 $prefix = '_gamipress_';
98
99 // TODO: To remove, added just to backward compatibility
100 if( ! is_gamipress_upgraded_to( '1.4.7' ) ) {
101 $trigger = ct_get_object_meta( $log_id, '_gamipress_trigger_type', true );
102 } else {
103 $log = ct_get_object( $log_id );
104 $trigger = $log->trigger_type;
105 }
106
107 if( $type !== 'event_trigger' ) {
108 return $fields;
109 }
110
111 switch( $trigger ) {
112 case 'gamipress_youtube_watch_video':
113 case 'gamipress_youtube_watch_specific_video':
114
115 $video_id = ct_get_object_meta( $log_id, $prefix . 'video_id', true );
116
117 $url = esc_url( 'https://www.youtube.com/embed/' . rawurlencode( $video_id ) );
118
119 $fields[] = array(
120 'name' => __( 'Video', 'gamipress' ),
121 'desc' => __( 'Video watched by the user.', 'gamipress' ),
122 'id' => $prefix . 'video_id',
123 'type' => 'html',
124 'content' => '<iframe id="youtube-player" type="text/html" src="' . esc_attr( $url ) . '" width="640" height="360" frameborder="0"></iframe>'
125 );
126 break;
127 }
128
129 return $fields;
130
131 }
132 add_filter( 'gamipress_log_extra_data_fields', 'gamipress_youtube_log_extra_data_fields', 10, 3 );
133
134 /**
135 * Override the meta data to filter the logs count
136 *
137 * @since 1.0.4
138 *
139 * @param array $log_meta The meta data to filter the logs count
140 * @param int $user_id The given user's ID
141 * @param string $trigger The given trigger we're checking
142 * @param int $since The since timestamp where retrieve the logs
143 * @param int $site_id The desired Site ID to check
144 * @param array $args The triggered args or requirement object
145 *
146 * @return array The meta data to filter the logs count
147 */
148 function gamipress_youtube_get_user_trigger_count_log_meta( $log_meta, $user_id, $trigger, $since, $site_id, $args ) {
149
150 switch( $trigger ) {
151 case 'gamipress_youtube_watch_specific_video':
152 // Add the video ID
153 if( isset( $args[0] ))
154 $log_meta['video_id'] = $args[0];
155
156 // $args could be a requirement object
157 if( isset( $args['youtube_video_id'] ) )
158 $log_meta['video_id'] = $args['youtube_video_id'];
159 break;
160 }
161
162 return $log_meta;
163
164 }
165 add_filter( 'gamipress_get_user_trigger_count_log_meta', 'gamipress_youtube_get_user_trigger_count_log_meta', 10, 6 );