| @@ -2,13 +2,12 @@ | ||
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | 4 | * Automatic YouTube Gallery Gutenberg Block. |
| 5 | 5 | * |
| 6 | - * @link https://plugins360.com | |
| 7 | - * @since 1.0.0 | |
| 6 | + * @link https://plugins360.com | |
| 7 | + * @since 1.0.0 | |
| 8 | 8 | * |
| 9 | - * @package Automatic_YouTube_Gallery | |
| 10 | - * @subpackage Automatic_YouTube_Gallery/block | |
| 9 | + * @package Automatic_YouTube_Gallery | |
| 11 | 10 | */ |
| 12 | 11 | |
| 13 | 12 | // Exit if accessed directly |
| 14 | 13 | if ( ! defined( 'WPINC' ) ) { |
| @@ -28,10 +27,9 @@ | ||
| 28 | 27 | * @since 1.0.0 |
| 29 | 28 | * @param array $categories Default block categories. |
| 30 | 29 | * @return array Modified block categories. |
| 31 | 30 | */ |
| 32 | - public function block_categories( $categories ) { | |
| 33 | - | |
| 31 | + public function block_categories( $categories ) { | |
| 34 | 32 | return array_merge( |
| 35 | 33 | $categories, |
| 36 | 34 | array( |
| 37 | 35 | array( |
| @@ -38,10 +36,9 @@ | ||
| 38 | 36 | 'slug' => 'automatic-youtube-gallery', |
| 39 | 37 | 'title' => __( 'Automatic YouTube Gallery', 'automatic-youtube-gallery' ), |
| 40 | 38 | ), |
| 41 | 39 | ) |
| 42 | - ); | |
| 43 | - | |
| 40 | + ); | |
| 44 | 41 | } |
| 45 | 42 | |
| 46 | 43 | /** |
| 47 | 44 | * Enqueue block assets for backend editor. |
| @@ -48,9 +45,8 @@ | ||
| 48 | 45 | * |
| 49 | 46 | * @since 1.0.0 |
| 50 | 47 | */ |
| 51 | 48 | public function enqueue_block_editor_assets() { |
| 52 | - | |
| 53 | 49 | $fields = ayg_get_editor_fields(); |
| 54 | 50 | |
| 55 | 51 | foreach ( $fields as $key => $section ) { |
| 56 | 52 | foreach ( $section['fields'] as $_key => $field ) { |
| @@ -59,84 +55,20 @@ | ||
| 59 | 55 | } |
| 60 | 56 | } |
| 61 | 57 | } |
| 62 | 58 | |
| 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 | 59 | wp_localize_script( |
| 73 | - 'ayg-block-js', | |
| 74 | - 'ayg_block', | |
| 60 | + 'wp-block-editor', | |
| 61 | + 'ayg_block', | |
| 75 | 62 | array( |
| 76 | 63 | 'options' => $fields, |
| 77 | 64 | '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' ) | |
| 65 | + 'selected_color' => __( 'Selected Color', 'automatic-youtube-gallery' ) | |
| 82 | 66 | ) |
| 83 | - ) | |
| 84 | - ); | |
| 85 | - | |
| 67 | + ) | |
| 68 | + ); | |
| 86 | 69 | } |
| 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 | 70 | |
| 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 | 71 | /** |
| 140 | 72 | * Register our custom block. |
| 141 | 73 | * |
| 142 | 74 | * @since 1.0.0 |
| @@ -141,47 +73,43 @@ | ||
| 141 | 73 | * |
| 142 | 74 | * @since 1.0.0 |
| 143 | 75 | */ |
| 144 | 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 | + ); | |
| 145 | 89 | |
| 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 | - ); | |
| 90 | + $fields = ayg_get_editor_fields(); | |
| 154 | 91 | |
| 155 | - $fields = ayg_get_editor_fields(); | |
| 92 | + foreach ( $fields as $key => $section ) { | |
| 93 | + foreach ( $section['fields'] as $field ) { | |
| 94 | + $type = 'string'; | |
| 156 | 95 | |
| 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 | - | |
| 96 | + if ( 'number' == $field['type'] ) { | |
| 97 | + $type = 'number'; | |
| 98 | + } elseif ( 'checkbox' == $field['type'] ) { | |
| 99 | + $type = 'boolean'; | |
| 173 | 100 | } |
| 174 | 101 | |
| 102 | + $attributes[ $field['name'] ] = array( | |
| 103 | + 'type' => $type | |
| 104 | + ); | |
| 175 | 105 | } |
| 176 | - | |
| 177 | - register_block_type( 'automatic-youtube-gallery/block', array( | |
| 178 | - 'attributes' => $attributes, | |
| 179 | - 'render_callback' => array( $this, 'render_block' ), | |
| 180 | - ) ); | |
| 181 | - | |
| 182 | 106 | } |
| 183 | 107 | |
| 108 | + register_block_type( __DIR__ . '/build', array( | |
| 109 | + 'attributes' => $attributes, | |
| 110 | + 'render_callback' => array( $this, 'render_block' ), | |
| 111 | + ) ); | |
| 184 | 112 | } |
| 185 | 113 | |
| 186 | 114 | /** |
| 187 | 115 | * Render the block frontend. |
| @@ -190,23 +118,31 @@ | ||
| 190 | 118 | * @param array $atts An associative array of attributes. |
| 191 | 119 | * @return string HTML output. |
| 192 | 120 | */ |
| 193 | 121 | public function render_block( $atts ) { |
| 122 | + if ( ! empty( $atts['is_admin'] ) ) { | |
| 123 | + $atts['autoplay'] = false; | |
| 124 | + } | |
| 194 | 125 | |
| 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; | |
| 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'] ); | |
| 198 | 130 | } |
| 199 | 131 | |
| 200 | - if ( ! empty( $atts['is_admin'] ) ) { | |
| 201 | - | |
| 202 | - $atts['autoplay'] = false; | |
| 203 | - $atts['autoadvance'] = false; | |
| 204 | - | |
| 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 | + } | |
| 205 | 137 | } |
| 206 | 138 | |
| 207 | - return ayg_build_gallery( $this->clean_attributes( $atts ) ); | |
| 139 | + // Output | |
| 140 | + $output = '<div ' . get_block_wrapper_attributes() . '>'; | |
| 141 | + $output .= ayg_build_gallery( $this->clean_attributes( $atts ) ); | |
| 142 | + $output .= '</div>'; | |
| 208 | 143 | |
| 144 | + return $output; | |
| 209 | 145 | } |
| 210 | 146 | |
| 211 | 147 | /** |
| 212 | 148 | * Clean attributes array. |
| @@ -216,13 +152,11 @@ | ||
| 216 | 152 | * @param array $atts Array of attributes. |
| 217 | 153 | * @return array Cleaned attributes array. |
| 218 | 154 | */ |
| 219 | 155 | private function clean_attributes( $atts ) { |
| 220 | - | |
| 221 | 156 | $attributes = array(); |
| 222 | 157 | |
| 223 | 158 | foreach ( $atts as $key => $value ) { |
| 224 | - | |
| 225 | 159 | if ( is_null( $value ) ) { |
| 226 | 160 | continue; |
| 227 | 161 | } |
| 228 | 162 | |
| @@ -230,12 +164,10 @@ | ||
| 230 | 164 | $value = ( true === $value ) ? 1 : 0; |
| 231 | 165 | } |
| 232 | 166 | |
| 233 | 167 | $attributes[ $key ] = $value; |
| 234 | - | |
| 235 | 168 | } |
| 236 | 169 | |
| 237 | 170 | return $attributes; |
| 238 | - | |
| 239 | 171 | } |
| 240 | 172 | |
| 241 | -} | |
| 173 | +} | |