/** * Returns an array that contains data about all available templates. * @function wpb_block_template_infos * @since 0.1.0 */ function wpb_block_template_infos() { $block_template_infos = array(); foreach (wpb_block_template_paths() as $path) { foreach (glob($path . '/*', GLOB_ONLYDIR) as $path) { $type = str_replace(WP_CONTENT_DIR, '', $path); $data = wpb_read_json($path . '/block.json'); $data['buid'] = $type; $data['path'] = $path; $block_template_infos[] = $data; } } return apply_filters('wpb/block_template_infos', $block_template_infos); }
register_post_type('block', array('labels' => $labels, 'description' => '', 'public' => false, 'publicly_queryable' => false, 'show_ui' => true, 'show_in_menu' => false, 'query_var' => false, 'rewrite' => false, 'capability_type' => 'post', 'has_archive' => false, 'hierarchical' => false, 'menu_position' => null, 'supports' => false)); //------------------------------------------------------------------------------ // UI //------------------------------------------------------------------------------ /** * @action admin_init * @since 0.1.0 */ add_action('admin_init', function () { /** * Adds the block list metabox on the page. This metabox is hidden. * @since 0.1.0 */ add_meta_box('wpb_block_list', 'Blocks', function () { $block_template_infos = wpb_block_template_infos(); $block_template_paths = wpb_block_template_paths(); $filter = function ($page_block) { return wpb_block_template_by_buid($page_block['buid']); }; $page_blocks = get_post_meta(get_the_id(), '_page_blocks', true); if ($page_blocks) { $page_blocks = array_filter($page_blocks, $filter); } $data = Timber::get_context(); $data['page_blocks'] = $page_blocks; $data['block_template_infos'] = $block_template_infos; $data['block_template_paths'] = $block_template_paths; Timber::render('block-list.twig', $data); }, 'page', 'normal', 'high'); /** * Adds a metabox on the block edit page used to store the block id and page
/** * @function wpb_block_metabox * @since 1.0.0 */ function wpb_block_metabox() { foreach (wpb_block_context() as $post_type) { add_meta_box('wpb_block_metabox', wpb_block_metabox_title($post_type), function () { $block_template_infos = wpb_block_template_infos(); $block_template_paths = wpb_block_template_paths(); $filter = function ($page_block) { return wpb_block_template_by_buid($page_block['buid']); }; $page_blocks = wpb_get_blocks(get_the_id()); if ($page_blocks) { $page_blocks = array_filter($page_blocks, $filter); } $categories = array(); foreach ($block_template_infos as $block_template_info) { $categories[$block_template_info['category']] = array(); } foreach ($block_template_infos as $block_template_info) { $categories[$block_template_info['category']][] = $block_template_info; } ksort($categories); $pages = wp_list_pages(array('title_li' => '', 'echo' => false, 'walker' => new WPB_Walker_Page())); $data = Timber::get_context(); $data['pages'] = $pages; $data['categories'] = $categories; $data['page_blocks'] = $page_blocks; $data['block_template_infos'] = $block_template_infos; $data['block_template_paths'] = $block_template_paths; Timber::render('block-metabox.twig', $data); }, $post_type, 'normal', wpb_block_metabox_priority($post_type)); add_filter('postbox_classes_' . $post_type . '_wpb_block_metabox', function ($classes = array()) { $classes[] = 'wpb-postbox'; $classes[] = 'seamless'; return $classes; }); } }