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

166 lines 3.3 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 wp_localize_script(
60 'wp-block-editor',
61 'ayg_block',
62 array(
63 'options' => $fields,
64 'i18n' => array(
65 'selected_color' => __( 'Selected Color', 'automatic-youtube-gallery' ),
66 'is_loading' => __( 'Loading...', 'automatic-youtube-gallery' )
67 )
68 )
69 );
70 }
71
72 /**
73 * Register our custom block.
74 *
75 * @since 1.0.0
76 */
77 public function register_block_type() {
78 if ( ! function_exists( 'register_block_type' ) ) {
79 return false;
80 }
81
82 $attributes = array(
83 'is_admin' => array(
84 'type' => 'boolean'
85 ),
86 'uid' => array(
87 'type' => 'string'
88 )
89 );
90
91 $fields = ayg_get_editor_fields();
92
93 foreach ( $fields as $key => $section ) {
94 foreach ( $section['fields'] as $field ) {
95 $type = 'string';
96
97 if ( 'number' == $field['type'] ) {
98 $type = 'number';
99 } elseif ( 'checkbox' == $field['type'] ) {
100 $type = 'boolean';
101 }
102
103 $attributes[ $field['name'] ] = array(
104 'type' => $type
105 );
106 }
107 }
108
109 register_block_type( __DIR__ . '/build', array(
110 'attributes' => $attributes,
111 'render_callback' => array( $this, 'render_block' ),
112 ) );
113 }
114
115 /**
116 * Render the block frontend.
117 *
118 * @since 1.0.0
119 * @param array $atts An associative array of attributes.
120 * @return string HTML output.
121 */
122 public function render_block( $atts ) {
123 if ( ! empty( $atts['is_admin'] ) ) {
124 $atts['autoplay'] = false;
125 }
126
127 if ( ! empty( $atts['uid'] ) ) {
128 $atts['uid'] = md5( $atts['uid'] );
129 }
130
131 // Output
132 $output = '<div ' . get_block_wrapper_attributes() . '>';
133 $output .= ayg_build_gallery( $this->clean_attributes( $atts ) );
134 $output .= '</div>';
135
136 return $output;
137 }
138
139 /**
140 * Clean attributes array.
141 *
142 * @since 1.0.0
143 * @access private
144 * @param array $atts Array of attributes.
145 * @return array Cleaned attributes array.
146 */
147 private function clean_attributes( $atts ) {
148 $attributes = array();
149
150 foreach ( $atts as $key => $value ) {
151 if ( is_null( $value ) ) {
152 continue;
153 }
154
155 if ( is_bool( $value ) ) {
156 $value = ( true === $value ) ? 1 : 0;
157 }
158
159 $attributes[ $key ] = $value;
160 }
161
162 return $attributes;
163 }
164
165 }
166