<?php


//remove_theme_support('core-block-patterns');
function observer_patterns_reg_from_files() {
    $cache_key = 'observer_patterns';
    // Attempt to get cached patterns.
    $patterns = get_transient( $cache_key );
//    delete_transient('observer_patterns');

    // If no cache exists, scan for patterns.
    if ( false === $patterns ) {
        $patterns = [];
        $current_dir = new WP_Theme('patterns', get_template_directory());
        $files = $current_dir->get_files( array('html'), 100 );

        foreach ($files as $file) {
            $path_parts = pathinfo($file);
            $category = basename($path_parts['dirname']);
            // Ensure the category is registered.
            register_block_pattern_category( $category, array( 'label' => $category ) );

            // Build the pattern details.
            $pattern_slug = 'examiner2/' . $path_parts['filename'];
            $patterns[] = [
                'slug'          => $pattern_slug,
                'title'         => $path_parts['filename'],
                'description'   => $path_parts['filename'],
                'content'       => file_get_contents($file),
                'categories'    => [ $category ],
                'viewportWidth' => 1420,
            ];
        }
        // Cache the patterns for a day (adjust as needed).
        //set_transient( $cache_key, $patterns, DAY_IN_SECONDS );
    }

    // Register the block patterns.
    foreach ( $patterns as $pattern ) {
        if(in_array('query', $pattern['categories'])){
            register_block_pattern($pattern['slug'], [
                'title' => $pattern['title'],
                'description' => $pattern['description'],
                'content' => preg_replace('/"lock":\{[^}]+\},?/', '', $pattern['content']),
                'blockTypes' => array('core/query'),
                'categories' => $pattern['categories'],
                'viewportWidth' => $pattern['viewportWidth'],
            ]);
        }else{
             register_block_pattern($pattern['slug'], [
                'title' => $pattern['title'],
                'description' => $pattern['description'],
                'content' => preg_replace('/"lock":\{[^}]+\},?/', '', $pattern['content']),

                'categories' => $pattern['categories'],
                'viewportWidth' => $pattern['viewportWidth'],
            ]);
        }
    }
}
add_action( 'admin_init', 'observer_patterns_reg_from_files' );
//function observer_patterns_reg_from_files() {
//
//
//// get theme instance of WP_Theme
//$current_dir = new WP_Theme('patterns',get_template_directory());
//
//// get up to 10 subfolders depth
//$files = $current_dir->get_files( array('html') , 100 );
//
////var_dump($files);
//
//foreach ($files as $file){
//
////    echo '<pre>';
////    var_dump($file);
////    echo '</pre>';
//    $path_parts = pathinfo($file);
//
//    $blocks = parse_blocks(file_get_contents($file));
//    $all_blocks = search($blocks, 'blockName');
////    echo '<pre>';
////    var_dump($path_parts['filename'],$blocks);
////    echo '</pre>';
////var_dump(basename($path_parts['dirname']));
//
//    register_block_pattern_category(basename($path_parts['dirname']), array( 'label' => basename($path_parts['dirname']) ) );
//
//
////$get_patterns  = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
////    $pattern_names = array_map(
////        function ( array $pattern ) {
////            return $pattern['name'];
////        },
////        $get_patterns
////    );
////    var_dump($pattern_names);
////       if(in_array('observer2/' .basename($path_parts['dirname']), $pattern_names  )){
////
////       }else{
//            register_block_pattern(
//                'observer2/' . $path_parts['filename'],
//                array(
//                    'title' => $path_parts['filename'],
//                    'description' => $path_parts['filename'],
//                    'content' => file_get_contents($file),
//                    'categories' => array(basename($path_parts['dirname'])),
//                    'viewportWidth' => 1420
//                )
//            );
////        }
//
//
//
//
//}
//}

//add_action( 'admin_init', 'observer_patterns_reg_from_files' );