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