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