<?php
/**
 * Pattern housekeeping: hide WordPress's generic patterns, and make the
 * Patterns screen reachable.
 *
 * THE PATTERNS THEMSELVES LIVE IN THE DATABASE, NOT IN THIS THEME.
 * They are ordinary `wp_block` posts, edited in wp-admin like any other post
 * (Appearance → Patterns). That is deliberate: the point of patterns is that
 * whoever builds pages can add a new one whenever a new standard row appears,
 * without a developer. Patterns defined in theme files can only be changed by
 * editing code, which kills that loop. This file therefore registers no
 * patterns — it only removes the ones that would confuse, and adds the menu
 * link WordPress leaves out.
 *
 * SYNCED vs UNSYNCED — the one thing worth knowing.
 * A pattern should be UNSYNCED: inserting it drops an independent copy into the
 * page, so editing or deleting the pattern later cannot touch pages already
 * built from it. A SYNCED block ("reusable block") is the opposite — pages hold
 * a live link to it, so deleting one silently removes that section from every
 * page using it, with no warning and nothing in the trash. That happened here
 * on 2026-07-24: two unused-looking "Admissions Block" reusable blocks were
 * deleted and took a section off the Home and Inquiry pages until they were
 * restored. Create patterns unsynced unless a live link is genuinely wanted.
 *
 * WHY CORE PATTERNS ARE TURNED OFF
 * WordPress ships ~11 generic patterns and can fetch more from the pattern
 * directory. None match this site, so an editor inserting one gets a section
 * that looks nothing like Brentwood. With them gone, everything in the Patterns
 * tab is safe to use.
 *
 * @package Kadence-Child
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Drop WordPress's bundled patterns and don't fetch remote ones. Runs late on
 * after_setup_theme so it applies after the parent theme's own setup.
 */
add_action(
	'after_setup_theme',
	function () {
		remove_theme_support( 'core-block-patterns' );
	},
	11
);
add_filter( 'should_load_remote_block_patterns', '__return_false' );

/**
 * WordPress registers the `wp_block` post type with `show_in_menu = false`, so
 * the screen that manages patterns exists but is linked from nowhere. Add it
 * under Appearance, where an editor would look for it.
 *
 * `edit_posts` matches the capability the Patterns screen itself requires, so
 * this exposes the menu item to exactly the people who could already use it.
 */
add_action(
	'admin_menu',
	function () {
		add_submenu_page(
			'themes.php',
			__( 'Patterns', 'kadence-child' ),
			__( 'Patterns', 'kadence-child' ),
			'edit_posts',
			'edit.php?post_type=wp_block'
		);
	}
);
