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