PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 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 All 47 releases
← All changes | integrations/youtube/includes/triggers.php +160 -0 7.8.9 → 8.0.5 View file →
@@ -1,0 +1,160 @@
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 + $log = ct_get_object( $log_id );
100 + $trigger = $log->trigger_type;
101 +
102 + if( $type !== 'event_trigger' ) {
103 + return $fields;
104 + }
105 +
106 + switch( $trigger ) {
107 + case 'gamipress_youtube_watch_video':
108 + case 'gamipress_youtube_watch_specific_video':
109 +
110 + $video_id = ct_get_object_meta( $log_id, $prefix . 'video_id', true );
111 +
112 + $url = esc_url( 'https://www.youtube.com/embed/' . rawurlencode( $video_id ) );
113 +
114 + $fields[] = array(
115 + 'name' => __( 'Video', 'gamipress' ),
116 + 'desc' => __( 'Video watched by the user.', 'gamipress' ),
117 + 'id' => $prefix . 'video_id',
118 + 'type' => 'html',
119 + 'content' => '<iframe id="youtube-player" type="text/html" src="' . esc_attr( $url ) . '" width="640" height="360" frameborder="0"></iframe>'
120 + );
121 + break;
122 + }
123 +
124 + return $fields;
125 +
126 +}
127 +add_filter( 'gamipress_log_extra_data_fields', 'gamipress_youtube_log_extra_data_fields', 10, 3 );
128 +
129 +/**
130 + * Override the meta data to filter the logs count
131 + *
132 + * @since 1.0.4
133 + *
134 + * @param array $log_meta The meta data to filter the logs count
135 + * @param int $user_id The given user's ID
136 + * @param string $trigger The given trigger we're checking
137 + * @param int $since The since timestamp where retrieve the logs
138 + * @param int $site_id The desired Site ID to check
139 + * @param array $args The triggered args or requirement object
140 + *
141 + * @return array The meta data to filter the logs count
142 + */
143 +function gamipress_youtube_get_user_trigger_count_log_meta( $log_meta, $user_id, $trigger, $since, $site_id, $args ) {
144 +
145 + switch( $trigger ) {
146 + case 'gamipress_youtube_watch_specific_video':
147 + // Add the video ID
148 + if( isset( $args[0] ))
149 + $log_meta['video_id'] = $args[0];
150 +
151 + // $args could be a requirement object
152 + if( isset( $args['youtube_video_id'] ) )
153 + $log_meta['video_id'] = $args['youtube_video_id'];
154 + break;
155 + }
156 +
157 + return $log_meta;
158 +
159 +}
160 +add_filter( 'gamipress_get_user_trigger_count_log_meta', 'gamipress_youtube_get_user_trigger_count_log_meta', 10, 6 );