# automatic-youtube-gallery/1.3.0/public/public.php

Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists &amp; Channels, version 1.3.0. 141 lines.

- Page: https://pluginprobe.com/plugins/automatic-youtube-gallery/1.3.0/code/public/public.php
- Raw: https://pluginprobe.com/plugins/automatic-youtube-gallery/1.3.0/raw/public/public.php
- Modified: 2019-07-09T16:20:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/automatic-youtube-gallery/1.3.0/code/public/public.php#L10-L20`.

```php
<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link    https://plugins360.com
 * @since   1.0.0
 *
 * @package Automatic_YouTube_Gallery
 */

// Exit if accessed directly
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * AYG_Public class.
 *
 * @since 1.0.0
 */
class AYG_Public {

	/**
	 * Get things started.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_shortcode( "automatic_youtube_gallery", array( $this, "shortcode_automatic_youtube_gallery" ) );
	}

	/**
	 * Enqueue styles for the public-facing side of the site.
	 *
	 * @since 1.0.0
	 */
	public function register_styles() {
		wp_register_style( 
			AYG_SLUG . '-public', 
			AYG_URL . 'public/assets/css/public.css', 
			array(), 
			AYG_VERSION, 
			'all' 
		);
	}

	/**
	 * Enqueue scripts for the public-facing side of the site.
	 *
	 * @since 1.0.0
	 */
	public function register_scripts() {
		wp_register_script( 
			AYG_SLUG . '-public', 
			AYG_URL . 'public/assets/js/public.js', 
			array( 'jquery' ), 
			AYG_VERSION, 
			false 
		);

		wp_localize_script( 
			AYG_SLUG . '-public', 
			'ayg_public', 
			array(
				'ajax_url' => admin_url( 'admin-ajax.php' ),
				'i18n'     => array(
					'show_more' => __( 'Show More', 'automatic-youtube-gallery' ),
					'show_less' => __( 'Show Less', 'automatic-youtube-gallery' )
				),
				'players' => array()
			)
		);
	}

	/**
	 * Process the shortcode [automatic_youtube_gallery].
	 *
	 * @since  1.0.0
	 * @param  array  $atts An associative array of attributes.
	 * @return string       Shortcode HTML output.
	 */
	public function shortcode_automatic_youtube_gallery( $atts ) {
		// Enqueue dependencies
		wp_enqueue_style( AYG_SLUG . '-public' );
		wp_enqueue_script( AYG_SLUG . '-public' );

		do_action( 'ayg_enqueue_scripts' );

		// ...
		return ayg_build_gallery( $atts );
	}

	/**
	 * Load more videos.
	 *
	 * @since 1.0.0
	 */
	public function ajax_callback_load_more_videos() {
		// Security check
		check_ajax_referer( 'ayg_pagination_nonce', 'nonce' );

		// Proceed safe
		$json        = array();
		$attributes  = $_POST;
		$source_type = sanitize_text_field( $attributes['type'] );

		$api_params = array(
			'type'       => $source_type,
			'id'         => sanitize_text_field( $attributes['id'] ),
			'order'      => sanitize_text_field( $attributes['order'] ), // works only when type=search
			'maxResults' => (int) $attributes['per_page'],
			'cache'      => (int) $attributes['cache'],
			'pageToken'  => sanitize_text_field( $attributes['pageToken'] )
		);

		$youtube_api = new AYG_YouTube_API();
		$response = $youtube_api->get_videos( $api_params );

		if ( ! isset( $response->error ) ) {
			if ( isset( $response->page_info ) ) {
				$json = $response->page_info;
			}

			if ( isset( $response->videos ) ) {
				$videos = $response->videos;

				ob_start();
				the_ayg_gallery( $videos, $attributes ); 
				$json['html'] = ob_get_clean();
			}	

			wp_send_json_success( $json );			
		} else {
			$json['message'] =  $response->error_message;
			wp_send_json_error( $json );			
		}		
	}

}

```
