| 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 |
$top_offset = apply_filters( 'ayg_gallery_scrolltop_offset', 10 ); |
| 63 |
|
| 64 |
$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() |
| 72 |
); |
| 73 |
|
| 74 |
wp_localize_script( |
| 75 |
AYG_SLUG . '-public', |
| 76 |
'ayg_public', |
| 77 |
$script_args |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Enqueue Gutenberg block assets for backend editor. |
| 83 |
* |
| 84 |
* @since 1.6.1 |
| 85 |
*/ |
| 86 |
public function enqueue_block_editor_assets() { |
| 87 |
// Styles |
| 88 |
$this->register_styles(); |
| 89 |
wp_enqueue_style( AYG_SLUG . '-public' ); |
| 90 |
|
| 91 |
// Scripts |
| 92 |
$this->register_scripts(); |
| 93 |
wp_enqueue_script( AYG_SLUG . '-public' ); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Process the shortcode [automatic_youtube_gallery]. |
| 98 |
* |
| 99 |
* @since 1.0.0 |
| 100 |
* @param array $atts An associative array of attributes. |
| 101 |
* @return string Shortcode HTML output. |
| 102 |
*/ |
| 103 |
public function shortcode_automatic_youtube_gallery( $atts ) { |
| 104 |
return ayg_build_gallery( $atts ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Load more videos. |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
*/ |
| 112 |
public function ajax_callback_load_more_videos() { |
| 113 |
// Security check |
| 114 |
check_ajax_referer( 'ayg_pagination_nonce', 'nonce' ); |
| 115 |
|
| 116 |
// Proceed safe |
| 117 |
$json = array(); |
| 118 |
$attributes = $_POST; |
| 119 |
$source_type = sanitize_text_field( $attributes['type'] ); |
| 120 |
|
| 121 |
$api_params = array( |
| 122 |
'type' => $source_type, |
| 123 |
'id' => sanitize_text_field( $attributes['id'] ), |
| 124 |
'order' => sanitize_text_field( $attributes['order'] ), // works only when type=search |
| 125 |
'maxResults' => (int) $attributes['per_page'], |
| 126 |
'cache' => (int) $attributes['cache'], |
| 127 |
'pageToken' => sanitize_text_field( $attributes['pageToken'] ) |
| 128 |
); |
| 129 |
|
| 130 |
$youtube_api = new AYG_YouTube_API(); |
| 131 |
$response = $youtube_api->get_videos( $api_params ); |
| 132 |
|
| 133 |
if ( ! isset( $response->error ) ) { |
| 134 |
if ( isset( $response->page_info ) ) { |
| 135 |
$json = $response->page_info; |
| 136 |
} |
| 137 |
|
| 138 |
if ( isset( $response->videos ) ) { |
| 139 |
$videos = $response->videos; |
| 140 |
|
| 141 |
ob_start(); |
| 142 |
the_ayg_gallery( $videos, $attributes ); |
| 143 |
$json['html'] = ob_get_clean(); |
| 144 |
} |
| 145 |
|
| 146 |
wp_send_json_success( $json ); |
| 147 |
} else { |
| 148 |
$json['message'] = $response->error_message; |
| 149 |
wp_send_json_error( $json ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* [SMUSH] Skip YouTube iframes from lazy loading. |
| 155 |
* |
| 156 |
* @since 1.5.0 |
| 157 |
* @param bool $skip Should skip? Default: false. |
| 158 |
* @param string $src Iframe url. |
| 159 |
* @return bool |
| 160 |
*/ |
| 161 |
public function smush( $skip, $src ) { |
| 162 |
return false !== strpos( $src, 'youtube' ); |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
|