2, 'editor_script' => 'tpika-block', 'editor_style' => 'tpika-gallery', 'render_callback' => array( __CLASS__, 'render' ), 'attributes' => array( 'id' => array( 'type' => 'number', 'default' => 0 ), 'columns' => array( 'type' => 'string', 'default' => '' ), 'layout' => array( 'type' => 'string', 'default' => '' ), 'pagination' => array( 'type' => 'string', 'default' => '' ), ), ); register_block_type( 'tpika/gallery', $block ); add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'editor_assets' ) ); } public static function editor_assets() { wp_enqueue_style( 'tpika-gallery' ); wp_enqueue_script( 'tpika-gallery' ); $galleries = array(); foreach ( tpika_gallery_options() as $id => $title ) { $galleries[] = array( 'value' => $id, 'label' => $title . ' (#' . $id . ')', ); } wp_localize_script( 'tpika-block', 'TPIKA_BLOCK', array( 'galleries' => $galleries, 'newUrl' => tpika_admin_url( 'tpika-new' ), 'editUrl' => tpika_admin_url( 'tpika-new', array( 'id' => 0 ) ), ) ); } public static function render( $attrs ) { $id = isset( $attrs['id'] ) ? absint( $attrs['id'] ) : 0; if ( ! $id ) { return ''; } $args = array(); foreach ( array( 'columns', 'layout', 'pagination' ) as $k ) { if ( ! empty( $attrs[ $k ] ) ) { $args[ $k ] = $attrs[ $k ]; } } $html = wp_kses( TPIKA_Renderer::render( $id, $args ), tpika_kses_gallery() ); return $html; } }