| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The public-facing functionality of the plugin. |
| 5 |
* |
| 6 |
* @link https://plugins360.com |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package Automatic_YouTube_Gallery |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if ( ! defined( 'WPINC' ) ) { |
| 14 |
die; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* AYG_Public class. |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class AYG_Public { |
| 23 |
|
| 24 |
/** |
| 25 |
* Get things started. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
add_shortcode( 'automatic_youtube_gallery', array( $this, 'shortcode_automatic_youtube_gallery' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Enqueue styles for the public-facing side of the site. |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 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 |
|
| 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( |
| 55 |
AYG_SLUG . '-public', |
| 56 |
AYG_URL . 'public/assets/css/public.min.css', |
| 57 |
$deps, |
| 58 |
AYG_VERSION, |
| 59 |
'all' |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Enqueue scripts for the public-facing side of the site. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
*/ |
| 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 |
|
| 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( |
| 88 |
AYG_SLUG . '-public', |
| 89 |
AYG_URL . 'public/assets/js/public.min.js', |
| 90 |
$deps, |
| 91 |
AYG_VERSION, |
| 92 |
array( 'strategy' => 'defer' ) |
| 93 |
); |
| 94 |
|
| 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 ); |
| 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 |
|
| 102 |
$script_args = 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 |
) |
| 117 |
); |
| 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 |
|
| 132 |
wp_localize_script( |
| 133 |
AYG_SLUG . '-public', |
| 134 |
'ayg_config', |
| 135 |
$script_args |
| 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 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Enqueue Gutenberg block assets for backend editor. |
| 149 |
* |
| 150 |
* @since 1.6.1 |
| 151 |
*/ |
| 152 |
public function enqueue_block_editor_assets() { |
| 153 |
// Styles |
| 154 |
$this->register_styles(); |
| 155 |
wp_enqueue_style( AYG_SLUG . '-public' ); |
| 156 |
|
| 157 |
// Scripts |
| 158 |
$this->register_scripts(); |
| 159 |
|
| 160 |
wp_enqueue_script( AYG_SLUG . '-public' ); |
| 161 |
wp_enqueue_script( AYG_SLUG . '-theme-classic' ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Process the shortcode [automatic_youtube_gallery]. |
| 166 |
* |
| 167 |
* @since 1.0.0 |
| 168 |
* @param array $attributes An associative array of attributes. |
| 169 |
* @param string $content Enclosing content. |
| 170 |
* @return string Shortcode HTML output. |
| 171 |
*/ |
| 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 ); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Load more videos. |
| 182 |
* |
| 183 |
* @since 1.0.0 |
| 184 |
*/ |
| 185 |
public function ajax_callback_load_videos() { |
| 186 |
// Security check |
| 187 |
check_ajax_referer( 'ayg_ajax_nonce', 'security' ); |
| 188 |
|
| 189 |
// Proceed safe |
| 190 |
$json = array(); |
| 191 |
$attributes = array_map( 'sanitize_text_field', $_POST ); |
| 192 |
$source_type = $attributes['type']; |
| 193 |
|
| 194 |
$api_params = array( |
| 195 |
'uid' => $attributes['uid'], |
| 196 |
'type' => $source_type, |
| 197 |
'src' => $attributes['src'], |
| 198 |
'order' => $attributes['order'], // works only when type=search |
| 199 |
'limit' => (int) $attributes['limit'], |
| 200 |
'maxResults' => (int) $attributes['per_page'], |
| 201 |
'cache' => (int) $attributes['cache'], |
| 202 |
'pageToken' => $attributes['pageToken'] |
| 203 |
); |
| 204 |
|
| 205 |
if ( ! empty( $attributes['searchTerm'] ) ) { |
| 206 |
$api_params['searchTerm'] = $attributes['searchTerm']; |
| 207 |
} |
| 208 |
|
| 209 |
$youtube_api = new AYG_YouTube_API(); |
| 210 |
$response = $youtube_api->query( $api_params ); |
| 211 |
|
| 212 |
if ( ! isset( $response->error ) ) { |
| 213 |
if ( isset( $response->page_info ) ) { |
| 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 |
); |
| 219 |
} |
| 220 |
|
| 221 |
if ( isset( $response->videos ) ) { |
| 222 |
$videos = $response->videos; |
| 223 |
$columns = (int) $attributes['columns']; |
| 224 |
|
| 225 |
ob_start(); |
| 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 |
} |
| 239 |
$json['html'] = ob_get_clean(); |
| 240 |
} |
| 241 |
|
| 242 |
wp_send_json_success( $json ); |
| 243 |
} else { |
| 244 |
$json['message'] = $response->error_message; |
| 245 |
wp_send_json_error( $json ); |
| 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(); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* [SMUSH] Skip YouTube iframes from lazy loading. |
| 265 |
* |
| 266 |
* @since 1.5.0 |
| 267 |
* @param bool $skip Should skip? Default: false. |
| 268 |
* @param string $src Iframe url. |
| 269 |
* @return bool |
| 270 |
*/ |
| 271 |
public function smush( $skip, $src ) { |
| 272 |
return false !== strpos( $src, 'youtube' ); |
| 273 |
} |
| 274 |
|
| 275 |
} |
| 276 |
|