$plugin_data ) { if ( strpos( $plugin_path, 'brandy-blocks' ) !== false ) { $is_installed = true; $plugin_basename = $plugin_path; break; } } wp_enqueue_script( 'brandy-patterns-library', BRANDY_TEMPLATE_URL . '/assets/lib/patterns-library.js', array( 'jquery' ), BRANDY_VERSION, true ); wp_localize_script( 'brandy-patterns-library', 'brandyPatternsLibrary', array( 'site_url' => site_url(), 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'updates' ), 'patterns_nonce' => wp_create_nonce( 'brandy_get_patterns' ), 'is_installed' => $is_installed, 'plugin_basename' => $plugin_basename, ) ); } /** * AJAX handler to get patterns after plugin activation */ public function get_patterns_after_activation() { // Verify nonce (false = don't die on failure, just return false) $nonce_check = check_ajax_referer( 'brandy_get_patterns', 'nonce', false ); if ( false === $nonce_check ) { wp_send_json_error( array( 'message' => 'Nonce verification failed' ) ); return; } // Check user capabilities if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( array( 'message' => 'Insufficient permissions' ) ); return; } try { // Force WordPress to recognize the newly activated plugin // This is necessary because the plugin was just activated via AJAX if ( ! defined( 'BRANDY_BLOCKS_VERSION' ) ) { // Try to find the plugin file $possible_paths = array( WP_PLUGIN_DIR . '/brandy-blocks/brandy-blocks.php', WP_PLUGIN_DIR . '/Brandy-Blocks/brandy-blocks.php', WP_PLUGIN_DIR . '/Brandy-Blocks-1/brandy-blocks.php', ); foreach ( $possible_paths as $plugin_file ) { if ( file_exists( $plugin_file ) ) { include_once $plugin_file; // Initialize the plugin if there's an init class if ( class_exists( 'BrandyBlocks\Initialize' ) ) { \BrandyBlocks\Initialize::get_instance(); } break; } } } // Get all registered patterns $patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered(); $categories = \WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered(); // Filter only Brandy patterns $brandy_patterns = array_filter( $patterns, function ( $pattern ) { return isset( $pattern['categories'] ) && is_array( $pattern['categories'] ) && count( array_filter( $pattern['categories'], function ( $cat ) { return stripos( $cat, 'brandy' ) !== false; } ) ) > 0; } ); $brandy_categories = array(); foreach ( $categories as $category ) { if ( stripos( $category['name'], 'brandy' ) !== false ) { $brandy_categories[] = $category; } } wp_send_json_success( array( 'patterns' => array_values( $brandy_patterns ), 'categories' => $brandy_categories, ) ); } catch ( \Exception $e ) { wp_send_json_error( array( 'message' => 'Error loading patterns: ' . $e->getMessage(), ) ); } } } PatternsLibrary::get_instance();