PluginProbe
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels / 2.1.0
Automatic YouTube Gallery – Embed Auto-Updating YouTube Video Galleries, Feeds, Playlists & Channels v2.1.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 2.1.0, at block/block.php

177 lines 3.9 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 * Register our custom block.
85 *
86 * @since 1.0.0
87 */
88 public function register_block_type() {
89 // Hook the post rendering to the block
90 if ( function_exists( 'register_block_type' ) ) {
91 $attributes = array(
92 'is_admin' => array(
93 'type' => 'boolean'
94 ),
95 'uid' => array(
96 'type' => 'string'
97 )
98 );
99
100 $fields = ayg_get_editor_fields();
101
102 foreach ( $fields as $key => $section ) {
103 foreach ( $section['fields'] as $field ) {
104 $type = 'string';
105
106 if ( 'number' == $field['type'] ) {
107 $type = 'number';
108 } elseif ( 'checkbox' == $field['type'] ) {
109 $type = 'boolean';
110 }
111
112 $attributes[ $field['name'] ] = array(
113 'type' => $type
114 );
115 }
116 }
117
118 register_block_type( 'automatic-youtube-gallery/block', array(
119 'attributes' => $attributes,
120 'render_callback' => array( $this, 'render_block' ),
121 ));
122 }
123 }
124
125 /**
126 * Render the block frontend.
127 *
128 * @since 1.0.0
129 * @param array $atts An associative array of attributes.
130 * @return string HTML output.
131 */
132 public function render_block( $atts ) {
133 // If this is an autosave, our form has not been submitted, so we don't want to do anything
134 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
135 return;
136 }
137
138 if ( ! empty( $atts['is_admin'] ) ) {
139 $atts['autoplay'] = false;
140 $atts['autoadvance'] = false;
141 }
142
143 if ( ! empty( $atts['uid'] ) ) {
144 $atts['uid'] = md5( $atts['uid'] );
145 }
146
147 return ayg_build_gallery( $this->clean_attributes( $atts ) );
148 }
149
150 /**
151 * Clean attributes array.
152 *
153 * @since 1.0.0
154 * @access private
155 * @param array $atts Array of attributes.
156 * @return array Cleaned attributes array.
157 */
158 private function clean_attributes( $atts ) {
159 $attributes = array();
160
161 foreach ( $atts as $key => $value ) {
162 if ( is_null( $value ) ) {
163 continue;
164 }
165
166 if ( is_bool( $value ) ) {
167 $value = ( true === $value ) ? 1 : 0;
168 }
169
170 $attributes[ $key ] = $value;
171 }
172
173 return $attributes;
174 }
175
176 }
177