PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 1.2.0
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v1.2.0
2.9.1 2.9.0 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.4.0 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 2.0.0 2.1.0 2.2.0 2.3.2 2.3.3 2.3.5 2.3.6 2.3.8 2.3.9 2.4.3 All 37 releases
automatic-youtube-gallery / block / block.php

block.php in Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels 1.2.0, at block/block.php

215 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Automatic YouTube Gallery Gutenberg Block.
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_Block class.
19 *
20 * @since 1.0.0
21 */
22 class AYG_Block {
23
24 /**
25 * Register our custom block category.
26 *
27 * @since 1.0.0
28 * @param array $categories Default block categories.
29 * @return array Modified block categories.
30 */
31 public function block_categories( $categories ) {
32 return array_merge(
33 $categories,
34 array(
35 array(
36 'slug' => 'automatic-youtube-gallery',
37 'title' => __( 'Automatic YouTube Gallery', 'automatic-youtube-gallery' ),
38 ),
39 )
40 );
41 }
42
43 /**
44 * Enqueue block assets for backend editor.
45 *
46 * @since 1.0.0
47 */
48 public function enqueue_block_editor_assets() {
49 $fields = ayg_get_editor_fields();
50
51 foreach ( $fields as $key => $section ) {
52 foreach ( $section['fields'] as $_key => $field ) {
53 if ( isset( $field['description'] ) ) {
54 $fields[ $key ]['fields'][ $_key ]['description'] = strip_tags( $field['description'] );
55 }
56 }
57 }
58
59 // Scripts
60 wp_enqueue_script(
61 'ayg-block-js',
62 plugins_url( '/block/dist/blocks.build.js', dirname( __FILE__ ) ),
63 array( 'wp-blocks', 'wp-i18n', 'wp-element' ),
64 filemtime( plugin_dir_path( __DIR__ ) . 'block/dist/blocks.build.js' ),
65 true
66 );
67
68 wp_localize_script(
69 'ayg-block-js',
70 'ayg_block',
71 array(
72 'options' => $fields,
73 'i18n' => array(
74 'block_description' => __( 'Create automated YouTube galleries.', 'automatic-youtube-gallery' ),
75 'block_title' => __( 'Automatic YouTube Gallery', 'automatic-youtube-gallery' ),
76 'selected_color' => __( 'Selected Color', 'automatic-youtube-gallery' ),
77 'spinner_message' => __( 'Waiting to finish your block configuration. This delay is to avoid frequent YouTube API calls.', 'automatic-youtube-gallery' )
78 )
79 )
80 );
81 }
82
83 /**
84 * Enqueue Gutenberg block assets for both frontend + backend.
85 *
86 * @since 1.0.0
87 */
88 public function enqueue_block_assets() {
89 if ( wp_script_is( AYG_SLUG . '-public', 'registered' ) ) {
90 wp_enqueue_style( AYG_SLUG . '-public' );
91 wp_enqueue_script( AYG_SLUG . '-public' );
92 } else {
93 // Styles
94 wp_enqueue_style(
95 AYG_SLUG . '-public',
96 AYG_URL . 'public/assets/css/public.css',
97 array(),
98 AYG_VERSION,
99 'all'
100 );
101
102 // Scripts
103 wp_enqueue_script(
104 AYG_SLUG . '-public',
105 AYG_URL . 'public/assets/js/public.js',
106 array( 'jquery' ),
107 AYG_VERSION,
108 false
109 );
110
111 wp_localize_script(
112 AYG_SLUG . '-public',
113 'ayg_public',
114 array(
115 'ajax_url' => admin_url( 'admin-ajax.php' ),
116 'i18n' => array(
117 'show_more' => __( 'Show More', 'automatic-youtube-gallery' ),
118 'show_less' => __( 'Show Less', 'automatic-youtube-gallery' )
119 ),
120 'players' => array()
121 )
122 );
123 }
124
125 do_action( 'ayg_enqueue_scripts' );
126 }
127
128 /**
129 * Register our custom block.
130 *
131 * @since 1.0.0
132 */
133 public function register_block_type() {
134 // Hook the post rendering to the block
135 if ( function_exists( 'register_block_type' ) ) {
136 $attributes = array(
137 'is_admin' => array(
138 'type' => 'boolean'
139 )
140 );
141
142 $fields = ayg_get_editor_fields();
143
144 foreach ( $fields as $key => $section ) {
145 foreach ( $section['fields'] as $field ) {
146 $type = 'string';
147
148 if ( 'number' == $field['type'] ) {
149 $type = 'number';
150 } elseif ( 'checkbox' == $field['type'] ) {
151 $type = 'boolean';
152 }
153
154 $attributes[ $field['name'] ] = array(
155 'type' => $type
156 );
157 }
158 }
159
160 register_block_type( 'automatic-youtube-gallery/block', array(
161 'attributes' => $attributes,
162 'render_callback' => array( $this, 'render_block' ),
163 ));
164 }
165 }
166
167 /**
168 * Render the block frontend.
169 *
170 * @since 1.0.0
171 * @param array $atts An associative array of attributes.
172 * @return string HTML output.
173 */
174 public function render_block( $atts ) {
175 // If this is an autosave, our form has not been submitted, so we don't want to do anything
176 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
177 return;
178 }
179
180 if ( ! empty( $atts['is_admin'] ) ) {
181 $atts['autoplay'] = false;
182 $atts['autoadvance'] = false;
183 }
184
185 return ayg_build_gallery( $this->clean_attributes( $atts ) );
186 }
187
188 /**
189 * Clean attributes array.
190 *
191 * @since 1.0.0
192 * @access private
193 * @param array $atts Array of attributes.
194 * @return array Cleaned attributes array.
195 */
196 private function clean_attributes( $atts ) {
197 $attributes = array();
198
199 foreach ( $atts as $key => $value ) {
200 if ( is_null( $value ) ) {
201 continue;
202 }
203
204 if ( is_bool( $value ) ) {
205 $value = ( true === $value ) ? 1 : 0;
206 }
207
208 $attributes[ $key ] = $value;
209 }
210
211 return $attributes;
212 }
213
214 }
215