array( 'nonce' => wp_create_nonce( 'wp_rest' ), 'baseUrl' => esc_url_raw( rest_url( 'stacklar/v1' ) ), ), 'i18n' => array( 'sending' => __( 'Sending...', 'stacklar-forms' ), 'success' => __( 'Submitted successfully.', 'stacklar-forms' ), 'error' => __( 'Submission failed.', 'stacklar-forms' ), ), ); $editor_config = apply_filters( 'stacklar_editor_config', $editor_config ); wp_localize_script( 'stacklar-blocks', 'StacklarCfg', is_array( $editor_config ) ? $editor_config : array() ); $editor_style_deps = array(); $style_path = STACKLAR_PATH . 'build/style-core.css'; $style_url = STACKLAR_URL . 'build/style-core.css'; if ( file_exists( $style_path ) ) { wp_register_style( 'stacklar-style', $style_url, array(), filemtime( $style_path ) ); $editor_style_deps[] = 'stacklar-style'; } $advanced_style_path = STACKLAR_PATH . 'build/style-advanced.css'; $advanced_style_url = STACKLAR_URL . 'build/style-advanced.css'; if ( file_exists( $advanced_style_path ) ) { wp_register_style( 'stacklar-style-advanced', $advanced_style_url, array( 'stacklar-style' ), filemtime( $advanced_style_path ) ); $editor_style_deps[] = 'stacklar-style-advanced'; } $editor_path = STACKLAR_PATH . 'build/editor.css'; $editor_url = STACKLAR_URL . 'build/editor.css'; if ( ! file_exists( $editor_path ) ) { $editor_path = STACKLAR_PATH . 'build/index.css'; $editor_url = STACKLAR_URL . 'build/index.css'; } if ( file_exists( $editor_path ) ) { wp_register_style( 'stacklar-editor', $editor_url, $editor_style_deps, filemtime( $editor_path ) ); } require_once STACKLAR_PATH . 'includes/blocks/base.php'; // Block slug => render class; each class lives in includes/blocks/.php. $block_classes = array( 'submit' => 'Stacklar_Block_Submit', 'name' => 'Stacklar_Block_Name', 'text' => 'Stacklar_Block_Text', 'checkbox' => 'Stacklar_Block_Checkbox', 'tel' => 'Stacklar_Block_Tel', 'email' => 'Stacklar_Block_Email', 'textarea' => 'Stacklar_Block_Textarea', 'select' => 'Stacklar_Block_Select', 'hidden' => 'Stacklar_Block_Hidden', 'acceptance' => 'Stacklar_Block_Acceptance', 'radio' => 'Stacklar_Block_Radio', 'number' => 'Stacklar_Block_Number', 'url' => 'Stacklar_Block_Url', 'section' => 'Stacklar_Block_Section', 'html' => 'Stacklar_Block_HTML', ); foreach ( $block_classes as $slug => $class ) { require_once STACKLAR_PATH . 'includes/blocks/' . $slug . '.php'; $this->stacklar_register_block_from_metadata( $slug, new $class() ); } } private function stacklar_register_block_from_metadata( $slug, $block = null ) { $slug = sanitize_key( (string) $slug ); if ( '' === $slug ) { return; } $metadata_path = trailingslashit( STACKLAR_PATH . 'blocks/' . $slug ); $metadata_file = $metadata_path . 'block.json'; if ( ! file_exists( $metadata_file ) ) { if ( $block && method_exists( $block, 'register' ) ) { $block->register(); } return; } $args = array(); if ( $block && method_exists( $block, 'render' ) ) { $args['render_callback'] = array( $block, 'render' ); } if ( function_exists( 'register_block_type_from_metadata' ) ) { register_block_type_from_metadata( $metadata_path, $args ); return; } register_block_type( $metadata_path, $args ); } public function register_block_category( $categories, $ctx ) { $categories = array_values( array_filter( $categories, static function ( $category ) { return ! isset( $category['slug'] ) || 'stacklar' !== $category['slug']; } ) ); array_unshift( $categories, array( 'slug' => 'stacklar', 'title' => __( 'Stacklar', 'stacklar-forms' ), 'icon' => 'feedback', ) ); return $categories; } }