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