Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 100 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Admin | |
0.00% |
0 / 100 |
|
0.00% |
0 / 7 |
1122 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| load_wonder_blocks | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| enqueue_block_assets | |
0.00% |
0 / 42 |
|
0.00% |
0 / 1 |
110 | |||
| load_script_translation_file | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| register_block_patterns | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
210 | |||
| add_admin_body_class | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| load_text_domain | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Patterns\Library; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Patterns\Data\Brands; |
| 6 | use NewfoldLabs\WP\Module\Patterns\Services\PluginService; |
| 7 | |
| 8 | /** |
| 9 | * Admin library class |
| 10 | */ |
| 11 | final class Admin { |
| 12 | /** |
| 13 | * Admin pages that require WonderBlock assets. |
| 14 | * |
| 15 | * @var array |
| 16 | */ |
| 17 | private static $admin_pages = array( 'page', 'post', 'page-new', 'post-new', 'site-editor' ); |
| 18 | |
| 19 | /** |
| 20 | * Constructor. |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | foreach ( self::$admin_pages as $admin_page ) { |
| 24 | \add_action( "load-{$admin_page}.php", array( __CLASS__, 'load_wonder_blocks' ) ); |
| 25 | } |
| 26 | |
| 27 | \add_action( 'init', array( PluginService::class, 'setup' ) ); |
| 28 | \add_action( 'init', array( __CLASS__, 'load_text_domain' ), 100 ); |
| 29 | |
| 30 | \add_filter( 'load_script_translation_file', array( __CLASS__, 'load_script_translation_file' ), 10, 3 ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Load wonder block assets into the respective admin editor page and suppress the core patterns modal. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public static function load_wonder_blocks() { |
| 39 | \add_action( 'enqueue_block_assets', array( __CLASS__, 'enqueue_block_assets' ) ); |
| 40 | self::register_block_patterns(); |
| 41 | \add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_class' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Enqueue block assets for iframe editor compatibility. |
| 46 | * This ensures scripts and styles are loaded in both parent window and iframe. |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public static function enqueue_block_assets() { |
| 51 | // Only enqueue in block editor context |
| 52 | $current_screen = \get_current_screen(); |
| 53 | if ( ! $current_screen || ! method_exists( $current_screen, 'is_block_editor' ) || ! $current_screen->is_block_editor() ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $asset_file = NFD_WONDER_BLOCKS_BUILD_DIR . '/wonder-blocks.asset.php'; |
| 58 | |
| 59 | if ( is_readable( $asset_file ) ) { |
| 60 | $asset = include $asset_file; |
| 61 | |
| 62 | // Validate asset file structure |
| 63 | if ( ! is_array( $asset ) || ! isset( $asset['dependencies'] ) || ! isset( $asset['version'] ) ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // Register script if not already registered |
| 68 | if ( ! \wp_script_is( 'nfd-wonder-blocks', 'registered' ) ) { |
| 69 | \wp_register_script( |
| 70 | 'nfd-wonder-blocks', |
| 71 | NFD_WONDER_BLOCKS_BUILD_URL . '/wonder-blocks.js', |
| 72 | array_merge( $asset['dependencies'], array() ), |
| 73 | $asset['version'], |
| 74 | true |
| 75 | ); |
| 76 | |
| 77 | \wp_localize_script( |
| 78 | 'nfd-wonder-blocks', |
| 79 | 'nfdWonderBlocks', |
| 80 | array( |
| 81 | 'nonce' => \wp_create_nonce( 'wp_rest' ), |
| 82 | 'nfdRestURL' => \get_home_url() . '/index.php?rest_route=/nfd-wonder-blocks/v1', |
| 83 | 'assets' => \esc_url( NFD_WONDER_BLOCKS_URL . '/assets' ), |
| 84 | 'wpVer' => \esc_html( get_bloginfo( 'version' ) ), |
| 85 | 'nfdWBVersion' => \esc_html( NFD_WONDER_BLOCKS_VERSION ), |
| 86 | 'brand' => Brands::get_current_brand(), |
| 87 | ) |
| 88 | ); |
| 89 | |
| 90 | \wp_set_script_translations( |
| 91 | 'nfd-wonder-blocks', |
| 92 | 'nfd-wonder-blocks', |
| 93 | NFD_WONDER_BLOCKS_DIR . '/languages' |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | // Register style if not already registered |
| 98 | if ( ! \wp_style_is( 'nfd-wonder-blocks', 'registered' ) ) { |
| 99 | \wp_register_style( |
| 100 | 'nfd-wonder-blocks', |
| 101 | NFD_WONDER_BLOCKS_BUILD_URL . '/wonder-blocks.css', |
| 102 | array(), |
| 103 | $asset['version'] |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | \wp_enqueue_script( 'nfd-wonder-blocks' ); |
| 108 | \wp_enqueue_style( 'nfd-wonder-blocks' ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Filter default WP script translations file to load the correct one |
| 114 | * |
| 115 | * @param string $file The translations file. |
| 116 | * @param string $handle Script handle. |
| 117 | * @param string $domain The strings textdomain. |
| 118 | * @return string |
| 119 | */ |
| 120 | public static function load_script_translation_file( $file, $handle, $domain ) { |
| 121 | |
| 122 | if ( 'nfd-wonder-blocks' === $handle ) { |
| 123 | $locale = determine_locale(); |
| 124 | $key = md5( 'build/' . NFD_WONDER_BLOCKS_VERSION . '/wonder-blocks.js' ); |
| 125 | $file = NFD_WONDER_BLOCKS_DIR . "/languages/{$domain}-{$locale}-{$key}.json"; |
| 126 | } |
| 127 | |
| 128 | return $file; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Register Block Patterns |
| 133 | */ |
| 134 | public static function register_block_patterns() { |
| 135 | |
| 136 | // Disable opening default WP Patterns modal on empty pages. |
| 137 | $patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered(); |
| 138 | |
| 139 | foreach ( $patterns as $pattern ) { |
| 140 | if ( ! empty( $pattern['blockTypes'] ) && in_array( 'core/post-content', $pattern['blockTypes'], true ) ) { |
| 141 | \unregister_block_pattern( $pattern['name'] ); |
| 142 | $pattern['blockTypes'] = array_diff( $pattern['blockTypes'], array( 'core/post-content' ) ); |
| 143 | \register_block_pattern( $pattern['name'], $pattern ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Add WonderBlocks patterns. |
| 148 | $wb_patterns = Items::get( 'patterns' ); |
| 149 | |
| 150 | if ( is_array( $wb_patterns ) && ! empty( $wb_patterns ) ) { |
| 151 | |
| 152 | $wb_pattern_categories = Categories::get( 'patterns' ); |
| 153 | |
| 154 | // Register WonderBlocks pattern categories. |
| 155 | if ( is_array( $wb_pattern_categories ) && ! empty( $wb_pattern_categories ) ) { |
| 156 | foreach ( $wb_pattern_categories as $category ) { |
| 157 | register_block_pattern_category( |
| 158 | 'wonder-blocks-' . $category['title'], |
| 159 | array( 'label' => 'WonderBlocks - ' . $category['label'] ) |
| 160 | ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | foreach ( $wb_patterns as $pattern ) { |
| 165 | |
| 166 | $categories = array(); |
| 167 | |
| 168 | // Build categories array. |
| 169 | if ( is_array( $pattern['categories'] ) && ! empty( $pattern['categories'] ) ) { |
| 170 | foreach ( $pattern['categories'] as $category ) { |
| 171 | $categories[] = 'wonder-blocks-' . $category; |
| 172 | } |
| 173 | } elseif ( is_string( $pattern['categories'] ) ) { |
| 174 | $categories[] = 'wonder-blocks-' . $pattern['categories']; |
| 175 | } |
| 176 | |
| 177 | \register_block_pattern( |
| 178 | 'wonder-blocks/' . $pattern['slug'], |
| 179 | array( |
| 180 | 'title' => $pattern['title'], |
| 181 | 'content' => $pattern['content'], |
| 182 | 'description' => $pattern['description'] ?? $pattern['title'], |
| 183 | 'categories' => $categories, |
| 184 | ) |
| 185 | ); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Add custom admin class on block editor pages. |
| 192 | * |
| 193 | * @param string $classes Body classes. |
| 194 | * @return string |
| 195 | */ |
| 196 | public static function add_admin_body_class( $classes ) { |
| 197 | $current_screen = get_current_screen(); |
| 198 | |
| 199 | if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
| 200 | $classes .= ' nfd-wb--hide-theme-patterns'; |
| 201 | } |
| 202 | |
| 203 | return $classes; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Load text domain for Module |
| 208 | * |
| 209 | * @return void |
| 210 | */ |
| 211 | public static function load_text_domain() { |
| 212 | |
| 213 | \load_plugin_textdomain( |
| 214 | 'nfd-wonder-blocks', |
| 215 | false, |
| 216 | NFD_WONDER_BLOCKS_DIR . '/languages' |
| 217 | ); |
| 218 | |
| 219 | \load_script_textdomain( |
| 220 | 'nfd-wonder-blocks', |
| 221 | 'nfd-wonder-blocks', |
| 222 | NFD_WONDER_BLOCKS_DIR . '/languages' |
| 223 | ); |
| 224 | } |
| 225 | } |