block-templates.php
1 year ago
class-templates-list-table.php
2 years ago
default-template-types.php
7 months ago
full-site-editing.php
1 year ago
get-block-templates.php
1 year ago
template-loader.php
1 year ago
template-parts.php
1 month ago
templates.php
1 month ago
full-site-editing.php
124 lines
| 1 | <?php |
| 2 | |
| 3 | function kubio_maybe_transform_slug_to_title( $slug ) { |
| 4 | |
| 5 | $slug_parts = explode( |
| 6 | ' ', |
| 7 | trim( |
| 8 | preg_replace( |
| 9 | '/\s\s+/', |
| 10 | ' ', |
| 11 | str_replace( array( '-', ' ' ), ' ', $slug ) |
| 12 | ) |
| 13 | ) |
| 14 | ); |
| 15 | $title = implode( |
| 16 | ' ', |
| 17 | array_map( |
| 18 | function ( $item ) { |
| 19 | return ucfirst( $item ); |
| 20 | }, |
| 21 | $slug_parts |
| 22 | ) |
| 23 | ); |
| 24 | |
| 25 | if ( empty( trim( $title ) ) ) { |
| 26 | $title = $slug; |
| 27 | } |
| 28 | |
| 29 | return $title; |
| 30 | } |
| 31 | |
| 32 | function kubio_get_template_hierarchy( $template_type ) { |
| 33 | if ( ! in_array( $template_type, kubio_get_template_type_slugs(), true ) ) { |
| 34 | return array(); |
| 35 | } |
| 36 | |
| 37 | $get_template_function = 'get_' . str_replace( '-', '_', $template_type ) . '_template'; // front-page -> get_front_page_template. |
| 38 | $template_hierarchy_filter = str_replace( '-', '', $template_type ) . '_template_hierarchy'; // front-page -> frontpage_template_hierarchy. |
| 39 | |
| 40 | $result = array(); |
| 41 | $template_hierarchy_filter_function = function ( $templates ) use ( &$result ) { |
| 42 | $result = $templates; |
| 43 | |
| 44 | return $templates; |
| 45 | }; |
| 46 | |
| 47 | add_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20, 1 ); |
| 48 | call_user_func( $get_template_function ); // This invokes template_hierarchy_filter. |
| 49 | remove_filter( $template_hierarchy_filter, $template_hierarchy_filter_function, 20 ); |
| 50 | |
| 51 | return $result; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | function kubio_get_template_paths() { |
| 56 | |
| 57 | $template_parts_rels = array( |
| 58 | '/full-site-editing/block-templates/*.html', |
| 59 | '/block-templates/*.html', |
| 60 | ); |
| 61 | $block_template_files = array(); |
| 62 | |
| 63 | foreach ( $template_parts_rels as $template_parts_rel ) { |
| 64 | $parent_block_template_files = glob( get_stylesheet_directory() . $template_parts_rel ); |
| 65 | $block_template_files = is_array( $block_template_files ) ? array_merge( $block_template_files, $parent_block_template_files ) : array(); |
| 66 | |
| 67 | } |
| 68 | |
| 69 | if ( is_child_theme() ) { |
| 70 | foreach ( $template_parts_rels as $template_parts_rel ) { |
| 71 | $child_block_template_files = glob( get_template_directory() . $template_parts_rel ); |
| 72 | $child_block_template_files = is_array( $child_block_template_files ) ? $child_block_template_files : array(); |
| 73 | $block_template_files = array_merge( $block_template_files, $child_block_template_files ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return $block_template_files; |
| 78 | } |
| 79 | |
| 80 | function kubio_get_template_part_paths( $base_directory ) { |
| 81 | $path_list = array(); |
| 82 | if ( file_exists( $base_directory . '/full-site-editing/block-template-parts' ) ) { |
| 83 | $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory . '/full-site-editing/block-template-parts' ) ); |
| 84 | $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); |
| 85 | foreach ( $nested_html_files as $path => $file ) { |
| 86 | $path_list[] = $path; |
| 87 | } |
| 88 | } else { |
| 89 | if ( file_exists( $base_directory . '/block-template-parts' ) ) { |
| 90 | $nested_files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $base_directory . '/block-template-parts' ) ); |
| 91 | $nested_html_files = new RegexIterator( $nested_files, '/^.+\.html$/i', RecursiveRegexIterator::GET_MATCH ); |
| 92 | foreach ( $nested_html_files as $path => $file ) { |
| 93 | $path_list[] = $path; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $path_list; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | function kubio_add_page_top_div() { |
| 103 | ?> |
| 104 | <div id="page-top" tabindex="-1"></div> |
| 105 | <?php |
| 106 | } |
| 107 | |
| 108 | add_action( 'wp_body_open', 'kubio_add_page_top_div' ); |
| 109 | |
| 110 | |
| 111 | add_filter( |
| 112 | 'wp_list_table_class_name', |
| 113 | function ( $class ) { |
| 114 | global $post_type; |
| 115 | |
| 116 | if ( $post_type === 'wp_template_part' || $post_type === 'wp_template' ) { |
| 117 | require_once __DIR__ . '/class-templates-list-table.php'; |
| 118 | $class = 'KubioWPTemplateListTable'; |
| 119 | } |
| 120 | |
| 121 | return $class; |
| 122 | } |
| 123 | ); |
| 124 |