| 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 |
) |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register our custom block. |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public function register_block_type() { |
| 77 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
$attributes = array( |
| 82 |
'is_admin' => array( |
| 83 |
'type' => 'boolean' |
| 84 |
), |
| 85 |
'uid' => array( |
| 86 |
'type' => 'string' |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
$fields = ayg_get_editor_fields(); |
| 91 |
|
| 92 |
foreach ( $fields as $key => $section ) { |
| 93 |
foreach ( $section['fields'] as $field ) { |
| 94 |
$type = 'string'; |
| 95 |
|
| 96 |
if ( 'number' == $field['type'] ) { |
| 97 |
$type = 'number'; |
| 98 |
} elseif ( 'checkbox' == $field['type'] ) { |
| 99 |
$type = 'boolean'; |
| 100 |
} |
| 101 |
|
| 102 |
$attributes[ $field['name'] ] = array( |
| 103 |
'type' => $type |
| 104 |
); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
register_block_type( __DIR__ . '/build', array( |
| 109 |
'attributes' => $attributes, |
| 110 |
'render_callback' => array( $this, 'render_block' ), |
| 111 |
) ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Render the block frontend. |
| 116 |
* |
| 117 |
* @since 1.0.0 |
| 118 |
* @param array $atts An associative array of attributes. |
| 119 |
* @return string HTML output. |
| 120 |
*/ |
| 121 |
public function render_block( $atts ) { |
| 122 |
if ( ! empty( $atts['is_admin'] ) ) { |
| 123 |
$atts['autoplay'] = false; |
| 124 |
} |
| 125 |
|
| 126 |
// Deprecated since v2.5.8. Retained for backward compatibility. |
| 127 |
if ( ! empty( $atts['uid'] ) ) { |
| 128 |
$atts['deprecated_uid'] = md5( $atts['uid'] ); |
| 129 |
unset( $atts['uid'] ); |
| 130 |
} |
| 131 |
|
| 132 |
// If popup is enabled and type is video, force theme to popup |
| 133 |
if ( isset( $atts['popup'] ) && ! empty( $atts['popup'] ) ) { |
| 134 |
if ( isset( $atts['type'] ) && 'video' == $atts['type'] ) { |
| 135 |
$atts['theme'] = 'popup'; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
// Output |
| 140 |
$output = '<div ' . get_block_wrapper_attributes() . '>'; |
| 141 |
$output .= ayg_build_gallery( $this->clean_attributes( $atts ) ); |
| 142 |
$output .= '</div>'; |
| 143 |
|
| 144 |
return $output; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Clean attributes array. |
| 149 |
* |
| 150 |
* @since 1.0.0 |
| 151 |
* @access private |
| 152 |
* @param array $atts Array of attributes. |
| 153 |
* @return array Cleaned attributes array. |
| 154 |
*/ |
| 155 |
private function clean_attributes( $atts ) { |
| 156 |
$attributes = array(); |
| 157 |
|
| 158 |
foreach ( $atts as $key => $value ) { |
| 159 |
if ( is_null( $value ) ) { |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
if ( is_bool( $value ) ) { |
| 164 |
$value = ( true === $value ) ? 1 : 0; |
| 165 |
} |
| 166 |
|
| 167 |
$attributes[ $key ] = $value; |
| 168 |
} |
| 169 |
|
| 170 |
return $attributes; |
| 171 |
} |
| 172 |
|
| 173 |
} |
| 174 |
|