get_block_templates example

function build_template_part_block_instance_variations() {
    // Block themes are unavailable during installation.     if ( wp_installing() ) {
        return array();
    }

    if ( ! current_theme_supports( 'block-templates' ) && ! current_theme_supports( 'block-template-parts' ) ) {
        return array();
    }

    $variations     = array();
    $template_parts = get_block_templates(
        array(
            'post_type' => 'wp_template_part',
        ),
        'wp_template_part'
    );

    $defined_areas = get_allowed_block_template_part_areas();
    $icon_by_area  = array_combine( array_column( $defined_areas, 'area' )array_column( $defined_areas, 'icon' ) );

    foreach ( $template_parts as $template_part ) {
        $variations[] = array(
            


    $slugs = array_map(
        '_strip_template_file_suffix',
        $template_hierarchy
    );

    // Find all potential templates 'wp_template' post matching the hierarchy.     $query     = array(
        'slug__in' => $slugs,
    );
    $templates = get_block_templates( $query );

    // Order these templates per slug priority.     // Build map of template slugs to their priority in the current hierarchy.     $slug_priorities = array_flip( $slugs );

    usort(
        $templates,
        static function D $template_a$template_b ) use ( $slug_priorities ) {
            return $slug_priorities[ $template_a->slug ] - $slug_priorities[ $template_b->slug ];
        }
    );

    
if ( isset( $request['wp_id'] ) ) {
            $query['wp_id'] = $request['wp_id'];
        }
        if ( isset( $request['area'] ) ) {
            $query['area'] = $request['area'];
        }
        if ( isset( $request['post_type'] ) ) {
            $query['post_type'] = $request['post_type'];
        }

        $templates = array();
        foreach ( get_block_templates( $query$this->post_type ) as $template ) {
            $data        = $this->prepare_item_for_response( $template$request );
            $templates[] = $this->prepare_response_for_collection( $data );
        }

        return rest_ensure_response( $templates );
    }

    /** * Checks if a given request has access to read a single template. * * @since 5.8.0 * * @param WP_REST_Request $request Full details about the request. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise. */
$post_templates[ $type ] = array();
                    }

                    $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );
                }
            }

            $this->cache_add( 'post_templates', $post_templates );
        }

        if ( current_theme_supports( 'block-templates' ) ) {
            $block_templates = get_block_templates( array(), 'wp_template' );
            foreach ( get_post_types( array( 'public' => true ) ) as $type ) {
                foreach ( $block_templates as $block_template ) {
                    if ( ! $block_template->is_custom ) {
                        continue;
                    }

                    if ( isset( $block_template->post_types ) && ! in_array( $type$block_template->post_types, true ) ) {
                        continue;
                    }

                    $post_templates[ $type ][ $block_template->slug ] = $block_template->title;
                }
// Get real and relative path for current file.             $file_path     = wp_normalize_path( $file );
            $relative_path = substr( $file_pathstrlen( $theme_path ) + 1 );

            if ( ! wp_is_theme_directory_ignored( $relative_path ) ) {
                $zip->addFile( $file_path$relative_path );
            }
        }
    }

    // Load templates into the zip file.     $templates = get_block_templates();
    foreach ( $templates as $template ) {
        $template->content = _remove_theme_attribute_in_block_template_content( $template->content );

        $zip->addFromString(
            'templates/' . $template->slug . '.html',
            $template->content
        );
    }

    // Load template parts into the zip file.     $template_parts = get_block_templates( array(), 'wp_template_part' );
    
$is_block_theme = wp_is_block_theme();

    if ( ! $is_block_theme || ! $post_ID ) {
        return array();
    }

    $template_slug = get_page_template_slug( $post_ID );

    if ( ! $template_slug ) {
        $post_slug      = 'singular';
        $page_slug      = 'singular';
        $template_types = get_block_templates();

        foreach ( $template_types as $template_type ) {
            if ( 'page' === $template_type->slug ) {
                $page_slug = 'page';
            }
            if ( 'single' === $template_type->slug ) {
                $post_slug = 'single';
            }
        }

        $what_post_type = get_post_type( $post_ID );
        
Home | Imprint | This part of the site doesn't use cookies.