| 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 |
wp_register_style( |
| 40 |
AYG_SLUG . '-public', |
| 41 |
AYG_URL . 'public/assets/css/public.min.css', |
| 42 |
array(), |
| 43 |
AYG_VERSION, |
| 44 |
'all' |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Enqueue scripts for the public-facing side of the site. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
*/ |
| 53 |
public function register_scripts() { |
| 54 |
$player_settings = get_option( 'ayg_player_settings' ); |
| 55 |
$privacy_settings = get_option( 'ayg_privacy_settings' ); |
| 56 |
|
| 57 |
wp_register_script( |
| 58 |
AYG_SLUG . '-public', |
| 59 |
AYG_URL . 'public/assets/js/public.min.js', |
| 60 |
array( 'jquery' ), |
| 61 |
AYG_VERSION, |
| 62 |
false |
| 63 |
); |
| 64 |
|
| 65 |
$script_args = array( |
| 66 |
'current_url' => get_permalink(), |
| 67 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 68 |
'ajax_nonce' => wp_create_nonce( 'ayg_ajax_nonce' ), |
| 69 |
'gallery_index' => 0, |
| 70 |
'gallery_id' => get_query_var( 'ayg_gallery_id' ), |
| 71 |
'video_id' => get_query_var( 'ayg_video_id' ), |
| 72 |
'active_player_id' => '', |
| 73 |
'privacy_enhanced_mode' => isset( $player_settings['privacy_enhanced_mode'] ) ? (int) $player_settings['privacy_enhanced_mode'] : 0, |
| 74 |
'origin' => '', |
| 75 |
'cookie_consent' => 0, |
| 76 |
'top_offset' => apply_filters( 'ayg_gallery_scrolltop_offset', 10 ), |
| 77 |
'i18n' => array( |
| 78 |
'show_more' => '[+] ' . __( 'Show More', 'automatic-youtube-gallery' ), |
| 79 |
'show_less' => '[-] ' . __( 'Show Less', 'automatic-youtube-gallery' ) |
| 80 |
) |
| 81 |
); |
| 82 |
|
| 83 |
if ( isset( $player_settings['origin'] ) && ! empty( $player_settings['origin'] ) ) { |
| 84 |
$url_parts = parse_url( site_url() ); |
| 85 |
$script_args['origin'] = $url_parts['scheme'] . '://' . $url_parts['host']; |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! isset( $_COOKIE['ayg_gdpr_consent'] ) ) { |
| 89 |
if ( ! empty( $privacy_settings['cookie_consent'] ) && ! empty( $privacy_settings['consent_message'] ) && ! empty( $privacy_settings['button_label'] ) ) { |
| 90 |
$script_args['cookie_consent'] = 1; |
| 91 |
$script_args['consent_message'] = wp_kses_post( trim( $privacy_settings['consent_message'] ) ); |
| 92 |
$script_args['button_label'] = esc_html( $privacy_settings['button_label'] ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
wp_localize_script( |
| 97 |
AYG_SLUG . '-public', |
| 98 |
'ayg_public', |
| 99 |
$script_args |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Enqueue Gutenberg block assets for backend editor. |
| 105 |
* |
| 106 |
* @since 1.6.1 |
| 107 |
*/ |
| 108 |
public function enqueue_block_editor_assets() { |
| 109 |
// Styles |
| 110 |
$this->register_styles(); |
| 111 |
wp_enqueue_style( AYG_SLUG . '-public' ); |
| 112 |
|
| 113 |
// Scripts |
| 114 |
$this->register_scripts(); |
| 115 |
wp_enqueue_script( AYG_SLUG . '-public' ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Process the shortcode [automatic_youtube_gallery]. |
| 120 |
* |
| 121 |
* @since 1.0.0 |
| 122 |
* @param array $attributes An associative array of attributes. |
| 123 |
* @return string Shortcode HTML output. |
| 124 |
*/ |
| 125 |
public function shortcode_automatic_youtube_gallery( $attributes ) { |
| 126 |
return ayg_build_gallery( $attributes ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Load more videos. |
| 131 |
* |
| 132 |
* @since 1.0.0 |
| 133 |
*/ |
| 134 |
public function ajax_callback_load_more_videos() { |
| 135 |
// Security check |
| 136 |
check_ajax_referer( 'ayg_ajax_nonce', 'security' ); |
| 137 |
|
| 138 |
// Proceed safe |
| 139 |
$json = array(); |
| 140 |
$attributes = array_map( 'sanitize_text_field', $_POST ); |
| 141 |
$source_type = $attributes['type']; |
| 142 |
|
| 143 |
$api_params = array( |
| 144 |
'type' => $source_type, |
| 145 |
'src' => $attributes['src'], |
| 146 |
'order' => $attributes['order'], // works only when type=search |
| 147 |
'maxResults' => (int) $attributes['per_page'], |
| 148 |
'cache' => (int) $attributes['cache'], |
| 149 |
'pageToken' => $attributes['pageToken'] |
| 150 |
); |
| 151 |
|
| 152 |
$youtube_api = new AYG_YouTube_API(); |
| 153 |
$response = $youtube_api->query( $api_params ); |
| 154 |
|
| 155 |
if ( ! isset( $response->error ) ) { |
| 156 |
if ( isset( $response->page_info ) ) { |
| 157 |
$json = $response->page_info; |
| 158 |
} |
| 159 |
|
| 160 |
if ( isset( $response->videos ) ) { |
| 161 |
$videos = $response->videos; |
| 162 |
|
| 163 |
ob_start(); |
| 164 |
foreach ( $videos as $index => $video ) { |
| 165 |
echo'<div class="ayg-item ayg-item-' . $video->id . ' ayg-col ayg-col-' . (int) $attributes['columns'] . '">'; |
| 166 |
the_ayg_gallery_thumbnail( $video, $attributes ); |
| 167 |
echo '</div>'; |
| 168 |
} |
| 169 |
$json['html'] = ob_get_clean(); |
| 170 |
} |
| 171 |
|
| 172 |
wp_send_json_success( $json ); |
| 173 |
} else { |
| 174 |
$json['message'] = $response->error_message; |
| 175 |
wp_send_json_error( $json ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Set cookie for accepting the privacy consent. |
| 181 |
* |
| 182 |
* @since 2.0.0 |
| 183 |
*/ |
| 184 |
public function set_gdpr_cookie() { |
| 185 |
// Security check |
| 186 |
check_ajax_referer( 'ayg_ajax_nonce', 'security' ); |
| 187 |
|
| 188 |
// Proceed safe |
| 189 |
setcookie( 'ayg_gdpr_consent', 1, time() + ( 30 * 24 * 60 * 60 ), COOKIEPATH, COOKIE_DOMAIN ); |
| 190 |
wp_send_json_success(); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* [SMUSH] Skip YouTube iframes from lazy loading. |
| 195 |
* |
| 196 |
* @since 1.5.0 |
| 197 |
* @param bool $skip Should skip? Default: false. |
| 198 |
* @param string $src Iframe url. |
| 199 |
* @return bool |
| 200 |
*/ |
| 201 |
public function smush( $skip, $src ) { |
| 202 |
return false !== strpos( $src, 'youtube' ); |
| 203 |
} |
| 204 |
|
| 205 |
} |
| 206 |
|