| @@ -27,9 +27,9 @@ | ||
| 27 | 27 | * @since 1.0.0 |
| 28 | 28 | */ |
| 29 | 29 | public function __construct() { |
| 30 | 30 | add_shortcode( 'automatic_youtube_gallery', array( $this, 'shortcode_automatic_youtube_gallery' ) ); |
| 31 | - } | |
| 31 | + } | |
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * Enqueue styles for the public-facing side of the site. |
| 35 | 35 | * |
| @@ -35,12 +35,27 @@ | ||
| 35 | 35 | * |
| 36 | 36 | * @since 1.0.0 |
| 37 | 37 | */ |
| 38 | 38 | public function register_styles() { |
| 39 | + $player_settings = get_option( 'ayg_player_settings' ); | |
| 40 | + | |
| 41 | + $deps = array(); | |
| 42 | + if ( isset( $player_settings['player_type'] ) && 'custom' == $player_settings['player_type'] ) { | |
| 43 | + $deps[] = AYG_SLUG . '-plyr'; | |
| 44 | + } | |
| 45 | + | |
| 39 | 46 | wp_register_style( |
| 47 | + AYG_SLUG . '-plyr', | |
| 48 | + AYG_URL . 'vendor/plyr/plyr.css', | |
| 49 | + array(), | |
| 50 | + '3.7.8', | |
| 51 | + 'all' | |
| 52 | + ); | |
| 53 | + | |
| 54 | + wp_register_style( | |
| 40 | 55 | AYG_SLUG . '-public', |
| 41 | - AYG_URL . 'public/assets/css/public.css', | |
| 42 | - array(), | |
| 56 | + AYG_URL . 'public/assets/css/public.min.css', | |
| 57 | + $deps, | |
| 43 | 58 | AYG_VERSION, |
| 44 | 59 | 'all' |
| 45 | 60 | ); |
| 46 | 61 | } |
| @@ -50,33 +65,84 @@ | ||
| 50 | 65 | * |
| 51 | 66 | * @since 1.0.0 |
| 52 | 67 | */ |
| 53 | 68 | public function register_scripts() { |
| 69 | + $strings_settings = get_option( 'ayg_strings_settings' ); | |
| 70 | + $gallery_settings = get_option( 'ayg_gallery_settings' ); | |
| 71 | + $player_settings = get_option( 'ayg_player_settings' ); | |
| 72 | + $privacy_settings = get_option( 'ayg_privacy_settings' ); | |
| 73 | + | |
| 74 | + $deps = array( 'jquery' ); | |
| 75 | + if ( isset( $player_settings['player_type'] ) && 'custom' == $player_settings['player_type'] ) { | |
| 76 | + $deps[] = AYG_SLUG . '-plyr'; | |
| 77 | + } | |
| 78 | + | |
| 54 | 79 | wp_register_script( |
| 80 | + AYG_SLUG . '-plyr', | |
| 81 | + AYG_URL . 'vendor/plyr/plyr.polyfilled.js', | |
| 82 | + array(), | |
| 83 | + '3.7.8', | |
| 84 | + array( 'strategy' => 'defer' ) | |
| 85 | + ); | |
| 86 | + | |
| 87 | + wp_register_script( | |
| 55 | 88 | AYG_SLUG . '-public', |
| 56 | - AYG_URL . 'public/assets/js/public.js', | |
| 57 | - array( 'jquery' ), | |
| 89 | + AYG_URL . 'public/assets/js/public.min.js', | |
| 90 | + $deps, | |
| 58 | 91 | AYG_VERSION, |
| 59 | - false | |
| 92 | + array( 'strategy' => 'defer' ) | |
| 60 | 93 | ); |
| 61 | 94 | |
| 62 | - $top_offset = apply_filters( 'ayg_gallery_scrolltop_offset', 10 ); | |
| 95 | + $scroll_top_offset = ( isset( $gallery_settings['scroll_top_offset'] ) && ! empty( $gallery_settings['scroll_top_offset'] ) ) ? (int) $gallery_settings['scroll_top_offset'] : 10; | |
| 96 | + $scroll_top_offset = apply_filters( 'ayg_gallery_scrolltop_offset', $scroll_top_offset ); // Backward compatibility to 2.4.3 | |
| 97 | + $scroll_top_offset = apply_filters( 'ayg_gallery_scroll_top_offset', $scroll_top_offset ); | |
| 63 | 98 | |
| 99 | + $show_more_label = ! empty( $strings_settings['show_more_label'] ) ? sanitize_text_field( $strings_settings['show_more_label'] ) : __( 'Show More', 'automatic-youtube-gallery' ); | |
| 100 | + $show_less_label = ! empty( $strings_settings['show_less_label'] ) ? sanitize_text_field( $strings_settings['show_less_label'] ) : __( 'Show Less', 'automatic-youtube-gallery' ); | |
| 101 | + | |
| 64 | 102 | $script_args = array( |
| 65 | - 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 66 | - 'i18n' => array( | |
| 67 | - 'show_more' => '[+] ' . __( 'Show More', 'automatic-youtube-gallery' ), | |
| 68 | - 'show_less' => '[-] ' . __( 'Show Less', 'automatic-youtube-gallery' ) | |
| 69 | - ), | |
| 70 | - 'top_offset' => $top_offset, | |
| 71 | - 'players' => array() | |
| 103 | + 'ajax_url' => admin_url( 'admin-ajax.php' ), | |
| 104 | + 'ajax_nonce' => wp_create_nonce( 'ayg_ajax_nonce' ), | |
| 105 | + 'current_page_url' => get_permalink(), | |
| 106 | + 'current_gallery_id' => get_query_var( 'ayg_gallery_id' ), | |
| 107 | + 'player_type' => isset( $player_settings['player_type'] ) ? sanitize_text_field( $player_settings['player_type'] ) : 'youtube', | |
| 108 | + 'player_color' => isset( $player_settings['player_color'] ) ? sanitize_text_field( $player_settings['player_color'] ) : '#00b3ff', | |
| 109 | + 'privacy_enhanced_mode' => isset( $player_settings['privacy_enhanced_mode'] ) ? (int) $player_settings['privacy_enhanced_mode'] : 0, | |
| 110 | + 'origin' => '', | |
| 111 | + 'cookieconsent' => 0, | |
| 112 | + 'top_offset' => $scroll_top_offset, | |
| 113 | + 'i18n' => array( | |
| 114 | + 'show_more' => $show_more_label, | |
| 115 | + 'show_less' => $show_less_label | |
| 116 | + ) | |
| 72 | 117 | ); |
| 73 | 118 | |
| 119 | + if ( isset( $player_settings['origin'] ) && ! empty( $player_settings['origin'] ) ) { | |
| 120 | + $url_parts = parse_url( site_url() ); | |
| 121 | + $script_args['origin'] = $url_parts['scheme'] . '://' . $url_parts['host']; | |
| 122 | + } | |
| 123 | + | |
| 124 | + if ( ! isset( $_COOKIE['ayg_gdpr_consent'] ) ) { | |
| 125 | + if ( ! empty( $privacy_settings['cookie_consent'] ) && ! empty( $privacy_settings['consent_message'] ) && ! empty( $privacy_settings['button_label'] ) ) { | |
| 126 | + $script_args['cookieconsent'] = 1; | |
| 127 | + $script_args['cookieconsent_message'] = wp_kses_post( trim( $privacy_settings['consent_message'] ) ); | |
| 128 | + $script_args['cookieconsent_button_label'] = esc_html( $privacy_settings['button_label'] ); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 74 | 132 | wp_localize_script( |
| 75 | 133 | AYG_SLUG . '-public', |
| 76 | - 'ayg_public', | |
| 134 | + 'ayg_config', | |
| 77 | 135 | $script_args |
| 78 | 136 | ); |
| 137 | + | |
| 138 | + wp_register_script( | |
| 139 | + AYG_SLUG . '-theme-classic', | |
| 140 | + AYG_URL . 'public/assets/js/theme-classic.min.js', | |
| 141 | + array( 'jquery' ), | |
| 142 | + AYG_VERSION, | |
| 143 | + array( 'strategy' => 'defer' ) | |
| 144 | + ); | |
| 79 | 145 | } |
| 80 | 146 | |
| 81 | 147 | /** |
| 82 | 148 | * Enqueue Gutenberg block assets for backend editor. |
| @@ -89,9 +155,11 @@ | ||
| 89 | 155 | wp_enqueue_style( AYG_SLUG . '-public' ); |
| 90 | 156 | |
| 91 | 157 | // Scripts |
| 92 | 158 | $this->register_scripts(); |
| 159 | + | |
| 93 | 160 | wp_enqueue_script( AYG_SLUG . '-public' ); |
| 161 | + wp_enqueue_script( AYG_SLUG . '-theme-classic' ); | |
| 94 | 162 | } |
| 95 | 163 | |
| 96 | 164 | /** |
| 97 | 165 | * Process the shortcode [automatic_youtube_gallery]. |
| @@ -96,13 +164,18 @@ | ||
| 96 | 164 | /** |
| 97 | 165 | * Process the shortcode [automatic_youtube_gallery]. |
| 98 | 166 | * |
| 99 | 167 | * @since 1.0.0 |
| 100 | - * @param array $atts An associative array of attributes. | |
| 101 | - * @return string Shortcode HTML output. | |
| 168 | + * @param array $attributes An associative array of attributes. | |
| 169 | + * @param string $content Enclosing content. | |
| 170 | + * @return string Shortcode HTML output. | |
| 102 | 171 | */ |
| 103 | - public function shortcode_automatic_youtube_gallery( $atts ) { | |
| 104 | - return ayg_build_gallery( $atts ); | |
| 172 | + public function shortcode_automatic_youtube_gallery( $attributes, $content = null ) { | |
| 173 | + if ( ! empty( $content ) ) { | |
| 174 | + $attributes['content'] = $content; | |
| 175 | + } | |
| 176 | + | |
| 177 | + return ayg_build_gallery( $attributes ); | |
| 105 | 178 | } |
| 106 | 179 | |
| 107 | 180 | /** |
| 108 | 181 | * Load more videos. |
| @@ -108,39 +181,62 @@ | ||
| 108 | 181 | * Load more videos. |
| 109 | 182 | * |
| 110 | 183 | * @since 1.0.0 |
| 111 | 184 | */ |
| 112 | - public function ajax_callback_load_more_videos() { | |
| 185 | + public function ajax_callback_load_videos() { | |
| 113 | 186 | // Security check |
| 114 | - check_ajax_referer( 'ayg_pagination_nonce', 'nonce' ); | |
| 187 | + check_ajax_referer( 'ayg_ajax_nonce', 'security' ); | |
| 115 | 188 | |
| 116 | 189 | // Proceed safe |
| 117 | 190 | $json = array(); |
| 118 | - $attributes = $_POST; | |
| 119 | - $source_type = sanitize_text_field( $attributes['type'] ); | |
| 191 | + $attributes = array_map( 'sanitize_text_field', $_POST ); | |
| 192 | + $source_type = $attributes['type']; | |
| 120 | 193 | |
| 121 | 194 | $api_params = array( |
| 195 | + 'uid' => $attributes['uid'], | |
| 122 | 196 | 'type' => $source_type, |
| 123 | - 'id' => sanitize_text_field( $attributes['id'] ), | |
| 124 | - 'order' => sanitize_text_field( $attributes['order'] ), // works only when type=search | |
| 197 | + 'src' => $attributes['src'], | |
| 198 | + 'order' => $attributes['order'], // works only when type=search | |
| 199 | + 'limit' => (int) $attributes['limit'], | |
| 125 | 200 | 'maxResults' => (int) $attributes['per_page'], |
| 126 | 201 | 'cache' => (int) $attributes['cache'], |
| 127 | - 'pageToken' => sanitize_text_field( $attributes['pageToken'] ) | |
| 202 | + 'pageToken' => $attributes['pageToken'] | |
| 128 | 203 | ); |
| 129 | 204 | |
| 205 | + if ( ! empty( $attributes['searchTerm'] ) ) { | |
| 206 | + $api_params['searchTerm'] = $attributes['searchTerm']; | |
| 207 | + } | |
| 208 | + | |
| 130 | 209 | $youtube_api = new AYG_YouTube_API(); |
| 131 | - $response = $youtube_api->get_videos( $api_params ); | |
| 210 | + $response = $youtube_api->query( $api_params ); | |
| 132 | 211 | |
| 133 | 212 | if ( ! isset( $response->error ) ) { |
| 134 | 213 | if ( isset( $response->page_info ) ) { |
| 135 | 214 | $json = $response->page_info; |
| 215 | + $json['message'] = sprintf( | |
| 216 | + _n( '%s video found matching your query.', '%s videos found matching your query.', $json['videos_found'], 'automatic-youtube-gallery' ), | |
| 217 | + number_format_i18n( $json['videos_found'] ) | |
| 218 | + ); | |
| 136 | 219 | } |
| 137 | 220 | |
| 138 | 221 | if ( isset( $response->videos ) ) { |
| 139 | 222 | $videos = $response->videos; |
| 223 | + $columns = (int) $attributes['columns']; | |
| 140 | 224 | |
| 141 | 225 | ob_start(); |
| 142 | - the_ayg_gallery( $videos, $attributes ); | |
| 226 | + foreach ( $videos as $index => $video ) { | |
| 227 | + $classes = array(); | |
| 228 | + $classes[] = 'ayg-video'; | |
| 229 | + $classes[] = 'ayg-video-' . $video->id; | |
| 230 | + $classes[] = 'ayg-col'; | |
| 231 | + $classes[] = 'ayg-col-' . $columns; | |
| 232 | + if ( $columns > 3 ) $classes[] = 'ayg-col-sm-3'; | |
| 233 | + if ( $columns > 2 ) $classes[] = 'ayg-col-xs-2'; | |
| 234 | + | |
| 235 | + echo'<div class="' . implode( ' ', $classes ) . '">'; | |
| 236 | + the_ayg_gallery_thumbnail( $video, $attributes ); | |
| 237 | + echo '</div>'; | |
| 238 | + } | |
| 143 | 239 | $json['html'] = ob_get_clean(); |
| 144 | 240 | } |
| 145 | 241 | |
| 146 | 242 | wp_send_json_success( $json ); |
| @@ -147,8 +243,22 @@ | ||
| 147 | 243 | } else { |
| 148 | 244 | $json['message'] = $response->error_message; |
| 149 | 245 | wp_send_json_error( $json ); |
| 150 | 246 | } |
| 247 | + } | |
| 248 | + | |
| 249 | + /** | |
| 250 | + * Set cookie for accepting the privacy consent. | |
| 251 | + * | |
| 252 | + * @since 2.0.0 | |
| 253 | + */ | |
| 254 | + public function set_gdpr_cookie() { | |
| 255 | + // Security check | |
| 256 | + check_ajax_referer( 'ayg_ajax_nonce', 'security' ); | |
| 257 | + | |
| 258 | + // Proceed safe | |
| 259 | + setcookie( 'ayg_gdpr_consent', 1, time() + ( 30 * 24 * 60 * 60 ), COOKIEPATH, COOKIE_DOMAIN ); | |
| 260 | + wp_send_json_success(); | |
| 151 | 261 | } |
| 152 | 262 | |
| 153 | 263 | /** |
| 154 | 264 | * [SMUSH] Skip YouTube iframes from lazy loading. |