| @@ -1,0 +1,106 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Scripts | |
| 4 | + * | |
| 5 | + * @package GamiPress\Link\Scripts | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * Register frontend scripts | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + * @return void | |
| 16 | + */ | |
| 17 | +function gamipress_youtube_register_scripts() { | |
| 18 | + | |
| 19 | + // Use minified libraries if SCRIPT_DEBUG is turned off | |
| 20 | + $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; | |
| 21 | + | |
| 22 | + // Scripts | |
| 23 | + wp_register_script( 'gamipress-youtube-api-js', 'https://www.youtube.com/iframe_api', array( 'jquery' ), GAMIPRESS_YOUTUBE_VER, true ); | |
| 24 | + wp_register_script( 'gamipress-youtube-js', GAMIPRESS_YOUTUBE_URL . 'assets/js/gamipress-youtube' . $suffix . '.js', array( 'jquery', 'gamipress-youtube-api-js' ), GAMIPRESS_YOUTUBE_VER, true ); | |
| 25 | + | |
| 26 | +} | |
| 27 | +add_action( 'init', 'gamipress_youtube_register_scripts' ); | |
| 28 | + | |
| 29 | +/** | |
| 30 | + * Enqueue frontend scripts | |
| 31 | + * | |
| 32 | + * @since 1.0.0 | |
| 33 | + * @return void | |
| 34 | + */ | |
| 35 | +function gamipress_youtube_enqueue_scripts( $hook = null ) { | |
| 36 | + | |
| 37 | + if( wp_script_is( 'gamipress-youtube-js', 'enqueued' ) ) { | |
| 38 | + return; | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Allowed delay in seconds to allow set the video as watched | |
| 43 | + * This delay is added as extra watched seconds to avoid delay issues caused, for example, by the browser or javascript slowdowns | |
| 44 | + * A delay of 1 sec will make event get triggered if users see 9 secs of a 10 secs video | |
| 45 | + * | |
| 46 | + * @since 1.0.3 | |
| 47 | + * | |
| 48 | + * @param int $allowed_delay Allowed delay in seconds, by default 1 | |
| 49 | + * | |
| 50 | + * @return int Allowed delay in seconds | |
| 51 | + */ | |
| 52 | + $allowed_delay = apply_filters( 'gamipress_youtube_allowed_delay', 1 ); | |
| 53 | + | |
| 54 | + // Scripts | |
| 55 | + wp_localize_script( 'gamipress-youtube-js', 'gamipress_youtube', array( | |
| 56 | + 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php', 'relative' ) ), | |
| 57 | + 'nonce' => gamipress_get_nonce(), | |
| 58 | + 'user_id' => get_current_user_id(), | |
| 59 | + 'post_id' => get_the_ID(), | |
| 60 | + 'debug_mode' => gamipress_is_debug_mode(), | |
| 61 | + 'allowed_delay' => $allowed_delay | |
| 62 | + ) ); | |
| 63 | + | |
| 64 | + wp_enqueue_script( 'gamipress-youtube-api-js' ); | |
| 65 | + wp_enqueue_script( 'gamipress-youtube-js' ); | |
| 66 | + | |
| 67 | +} | |
| 68 | +//add_action( 'wp_enqueue_scripts', 'gamipress_youtube_enqueue_scripts', 100 ); | |
| 69 | + | |
| 70 | +/** | |
| 71 | + * Register admin scripts | |
| 72 | + * | |
| 73 | + * @since 1.0.0 | |
| 74 | + * @return void | |
| 75 | + */ | |
| 76 | +function gamipress_youtube_admin_register_scripts() { | |
| 77 | + // Use minified libraries if SCRIPT_DEBUG is turned off | |
| 78 | + $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; | |
| 79 | + | |
| 80 | + // Scripts | |
| 81 | + wp_register_script( 'gamipress-youtube-requirements-ui-js', GAMIPRESS_YOUTUBE_URL . 'assets/js/gamipress-youtube-requirements-ui' . $suffix . '.js', array( 'jquery' ), GAMIPRESS_YOUTUBE_VER, true ); | |
| 82 | + | |
| 83 | +} | |
| 84 | +add_action( 'admin_init', 'gamipress_youtube_admin_register_scripts' ); | |
| 85 | + | |
| 86 | +/** | |
| 87 | + * Enqueue admin scripts | |
| 88 | + * | |
| 89 | + * @since 1.0.0 | |
| 90 | + * @return void | |
| 91 | + */ | |
| 92 | +function gamipress_youtube_admin_enqueue_scripts( $hook ) { | |
| 93 | + | |
| 94 | + global $post_type; | |
| 95 | + | |
| 96 | + // Scripts | |
| 97 | + | |
| 98 | + $allowed_post_types = array_merge( gamipress_get_achievement_types_slugs(), gamipress_get_rank_types_slugs() ); | |
| 99 | + | |
| 100 | + // Requirements ui script | |
| 101 | + if ( $post_type === 'points-type' || in_array( $post_type, $allowed_post_types ) ) { | |
| 102 | + wp_enqueue_script( 'gamipress-youtube-requirements-ui-js' ); | |
| 103 | + } | |
| 104 | + | |
| 105 | +} | |
| 106 | +add_action( 'admin_enqueue_scripts', 'gamipress_youtube_admin_enqueue_scripts', 100 ); | |