block-based-templates
1 month ago
styles
4 years ago
customizer-options.php
1 year ago
editor-hooks.php
1 year ago
fallback-compatibility.php
4 years ago
front-page.html
2 years ago
frontend-hooks.php
1 year ago
preview.php
4 years ago
templates-importer.php
1 year ago
templates.php
1 year ago
third-party-themes.php
1 year ago
templates.php
164 lines
| 1 | <?php |
| 2 | |
| 3 | use IlluminateAgnostic\Arr\Support\Arr; |
| 4 | |
| 5 | |
| 6 | |
| 7 | function kubio_get_classic_theme_primary_templates() { |
| 8 | |
| 9 | $slugs = array( |
| 10 | 'index', |
| 11 | 'singular', |
| 12 | 'archive', |
| 13 | 'single', |
| 14 | 'page', |
| 15 | 'home', |
| 16 | '404', |
| 17 | 'search', |
| 18 | ); |
| 19 | |
| 20 | $existing = array(); |
| 21 | |
| 22 | foreach ( $slugs as $slug ) { |
| 23 | $existing[ $slug ] = file_exists( wp_get_theme()->get_file_path( "$slug.php" ) ); |
| 24 | } |
| 25 | |
| 26 | return $existing; |
| 27 | } |
| 28 | |
| 29 | function kubio_get_classic_theme_templates( $template_type = 'page' ) { |
| 30 | $theme = wp_get_theme(); |
| 31 | $files = (array) $theme->get_files( 'php', 1, true ); |
| 32 | |
| 33 | $post_templates = array(); |
| 34 | |
| 35 | foreach ( $files as $file => $full_path ) { |
| 36 | if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) { |
| 37 | |
| 38 | if ( $file === 'page.php' && $template_type === 'page' ) { |
| 39 | $post_templates['page']['page.php'] = __( 'Page', 'kubio' ); |
| 40 | } |
| 41 | |
| 42 | if ( $file === 'single.php' && $template_type === 'post' ) { |
| 43 | $post_templates['post']['single.php'] = __( 'Single', 'kubio' ); |
| 44 | } |
| 45 | |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | $types = array( 'page' ); |
| 50 | if ( preg_match( '|Template Post Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) { |
| 51 | $types = explode( ',', _cleanup_header_comment( $type[1] ) ); |
| 52 | } |
| 53 | |
| 54 | foreach ( $types as $type ) { |
| 55 | $type = sanitize_key( $type ); |
| 56 | if ( ! isset( $post_templates[ $type ] ) ) { |
| 57 | $post_templates[ $type ] = array(); |
| 58 | } |
| 59 | |
| 60 | $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return Arr::get( $post_templates, $template_type, array() ); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | function kubio_third_party_theme_has_front_page_template() { |
| 69 | $theme = wp_get_theme(); |
| 70 | $files = (array) $theme->get_files( 'php', 1, true ); |
| 71 | return array_key_exists( 'front-page.php', $files ); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | function kubio_third_party_themes_default_block_templates( $templates ) { |
| 76 | |
| 77 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 78 | $kubio_default_templates = glob( KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . '/templates/*.html' ); |
| 79 | |
| 80 | foreach ( $kubio_default_templates as $template ) { |
| 81 | $slug = preg_replace( '#(.*)/templates/(.*).html#', '$2', wp_normalize_path( $template ) ); |
| 82 | $templates[ $slug ] = $template; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return $templates; |
| 87 | } |
| 88 | |
| 89 | add_filter( 'kubio/importer/available_templates', 'kubio_third_party_themes_default_block_templates', 20 ); |
| 90 | |
| 91 | |
| 92 | function kubio_third_party_themes_default_block_template_parts( $templates ) { |
| 93 | |
| 94 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 95 | $kubio_default_templates = glob( KUBIO_3RD_PARTY_DEFAULT_TEMPLATES_PATH . '/parts/*.html' ); |
| 96 | |
| 97 | foreach ( $kubio_default_templates as $template ) { |
| 98 | $slug = preg_replace( '#(.*)/parts/(.*).html#', '$2', wp_normalize_path( $template ) ); |
| 99 | $templates[ $slug ] = $template; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return $templates; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | add_filter( 'kubio/importer/available_template_parts', 'kubio_third_party_themes_default_block_template_parts', 20 ); |
| 108 | |
| 109 | |
| 110 | function kubio_third_party_themes_is_importing_kubio_template( $current_value, $template ) { |
| 111 | return ( $current_value || strpos( $template, 'kubio-' ) === 0 ); |
| 112 | } |
| 113 | |
| 114 | add_filter( 'kubio/template/is_importing_kubio_template', 'kubio_third_party_themes_is_importing_kubio_template', 10, 2 ); |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | function kubio_retrieve_template_source( $object ) { |
| 120 | if ( is_object( $object ) ) { |
| 121 | $post_id = $object->wp_id; |
| 122 | } elseif ( isset( $object['wp_id'] ) ) { |
| 123 | $post_id = $object['wp_id']; |
| 124 | } elseif ( isset( $object['id'] ) ) { |
| 125 | $post_id = $object['id']; |
| 126 | } |
| 127 | |
| 128 | $source = get_post_meta( $post_id, '_kubio_template_source', true ); |
| 129 | if ( $source === false ) { |
| 130 | $source = 'custom'; |
| 131 | } |
| 132 | return $source; |
| 133 | } |
| 134 | |
| 135 | function kubio_update_template_source( $value, $object ) { |
| 136 | $post_id = $object->wp_id; |
| 137 | $original = get_post_meta( $post_id, '_kubio_template_source', true ); |
| 138 | update_post_meta( $post_id, '_kubio_template_source', $value, $original ); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | function kubio_register_template_source_rest_field() { |
| 143 | |
| 144 | register_rest_field( |
| 145 | 'wp_template', |
| 146 | 'kubio_template_source', |
| 147 | array( |
| 148 | 'get_callback' => 'kubio_retrieve_template_source', |
| 149 | 'update_callback' => 'kubio_update_template_source', |
| 150 | ) |
| 151 | ); |
| 152 | |
| 153 | register_rest_field( |
| 154 | 'wp_template_part', |
| 155 | 'kubio_template_source', |
| 156 | array( |
| 157 | 'get_callback' => 'kubio_retrieve_template_source', |
| 158 | 'update_callback' => 'kubio_update_template_source', |
| 159 | ) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | add_action( 'rest_api_init', 'kubio_register_template_source_rest_field' ); |
| 164 |