Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 93 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| Admin | |
0.00% |
0 / 93 |
|
0.00% |
0 / 7 |
650 | |
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 | |||
| register_assets | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
6 | |||
| 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_editor_assets', array( __CLASS__, 'register_assets' ) ); |
| 40 | self::register_block_patterns(); |
| 41 | \add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_class' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Register assets. |
| 46 | */ |
| 47 | public static function register_assets() { |
| 48 | $asset_file = NFD_WONDER_BLOCKS_BUILD_DIR . '/wonder-blocks.asset.php'; |
| 49 | |
| 50 | if ( is_readable( $asset_file ) ) { |
| 51 | $asset = include_once $asset_file; |
| 52 | |
| 53 | \wp_register_script( |
| 54 | 'nfd-wonder-blocks', |
| 55 | NFD_WONDER_BLOCKS_BUILD_URL . '/wonder-blocks.js', |
| 56 | array_merge( $asset['dependencies'], array() ), |
| 57 | $asset['version'], |
| 58 | true |
| 59 | ); |
| 60 | |
| 61 | \wp_register_style( |
| 62 | 'nfd-wonder-blocks', |
| 63 | NFD_WONDER_BLOCKS_BUILD_URL . '/wonder-blocks.css', |
| 64 | array(), |
| 65 | $asset['version'] |
| 66 | ); |
| 67 | |
| 68 | \wp_localize_script( |
| 69 | 'nfd-wonder-blocks', |
| 70 | 'nfdWonderBlocks', |
| 71 | array( |
| 72 | 'nonce' => \wp_create_nonce( 'wp_rest' ), |
| 73 | 'nfdRestURL' => \get_home_url() . '/index.php?rest_route=/nfd-wonder-blocks/v1', |
| 74 | 'assets' => \esc_url( NFD_WONDER_BLOCKS_URL . '/assets' ), |
| 75 | 'wpVer' => \esc_html( get_bloginfo( 'version' ) ), |
| 76 | 'nfdWBVersion' => \esc_html( NFD_WONDER_BLOCKS_VERSION ), |
| 77 | 'brand' => Brands::get_current_brand(), |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | \wp_set_script_translations( |
| 82 | 'nfd-wonder-blocks', |
| 83 | 'nfd-wonder-blocks', |
| 84 | NFD_WONDER_BLOCKS_DIR . '/languages' |
| 85 | ); |
| 86 | |
| 87 | \wp_enqueue_script( 'nfd-wonder-blocks' ); |
| 88 | \wp_enqueue_style( 'nfd-wonder-blocks' ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Filter default WP script translations file to load the correct one |
| 94 | * |
| 95 | * @param string $file The translations file. |
| 96 | * @param string $handle Script handle. |
| 97 | * @param string $domain The strings textdomain. |
| 98 | * @return string |
| 99 | */ |
| 100 | public static function load_script_translation_file( $file, $handle, $domain ) { |
| 101 | |
| 102 | if ( 'nfd-wonder-blocks' === $handle ) { |
| 103 | $locale = determine_locale(); |
| 104 | $key = md5( 'build/' . NFD_WONDER_BLOCKS_VERSION . '/wonder-blocks.js' ); |
| 105 | $file = NFD_WONDER_BLOCKS_DIR . "/languages/{$domain}-{$locale}-{$key}.json"; |
| 106 | } |
| 107 | |
| 108 | return $file; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Register Block Patterns |
| 113 | */ |
| 114 | public static function register_block_patterns() { |
| 115 | |
| 116 | // Disable opening default WP Patterns modal on empty pages. |
| 117 | $patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered(); |
| 118 | |
| 119 | foreach ( $patterns as $pattern ) { |
| 120 | if ( ! empty( $pattern['blockTypes'] ) && in_array( 'core/post-content', $pattern['blockTypes'], true ) ) { |
| 121 | \unregister_block_pattern( $pattern['name'] ); |
| 122 | $pattern['blockTypes'] = array_diff( $pattern['blockTypes'], array( 'core/post-content' ) ); |
| 123 | \register_block_pattern( $pattern['name'], $pattern ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Add WonderBlocks patterns. |
| 128 | $wb_patterns = Items::get( 'patterns' ); |
| 129 | |
| 130 | if ( is_array( $wb_patterns ) && ! empty( $wb_patterns ) ) { |
| 131 | |
| 132 | $wb_pattern_categories = Categories::get( 'patterns' ); |
| 133 | |
| 134 | // Register WonderBlocks pattern categories. |
| 135 | if ( is_array( $wb_pattern_categories ) && ! empty( $wb_pattern_categories ) ) { |
| 136 | foreach ( $wb_pattern_categories as $category ) { |
| 137 | register_block_pattern_category( |
| 138 | 'wonder-blocks-' . $category['title'], |
| 139 | array( 'label' => 'WonderBlocks - ' . $category['label'] ) |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | foreach ( $wb_patterns as $pattern ) { |
| 145 | |
| 146 | $categories = array(); |
| 147 | |
| 148 | // Build categories array. |
| 149 | if ( is_array( $pattern['categories'] ) && ! empty( $pattern['categories'] ) ) { |
| 150 | foreach ( $pattern['categories'] as $category ) { |
| 151 | $categories[] = 'wonder-blocks-' . $category; |
| 152 | } |
| 153 | } elseif ( is_string( $pattern['categories'] ) ) { |
| 154 | $categories[] = 'wonder-blocks-' . $pattern['categories']; |
| 155 | } |
| 156 | |
| 157 | \register_block_pattern( |
| 158 | 'wonder-blocks/' . $pattern['title'], |
| 159 | array( |
| 160 | 'title' => $pattern['title'], |
| 161 | 'content' => $pattern['content'], |
| 162 | 'description' => $pattern['title'], |
| 163 | 'categories' => $categories, |
| 164 | ) |
| 165 | ); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Add custom admin class on block editor pages. |
| 172 | * |
| 173 | * @param string $classes Body classes. |
| 174 | * @return string |
| 175 | */ |
| 176 | public static function add_admin_body_class( $classes ) { |
| 177 | $current_screen = get_current_screen(); |
| 178 | |
| 179 | if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
| 180 | $classes .= ' nfd-wb--hide-theme-patterns'; |
| 181 | } |
| 182 | |
| 183 | return $classes; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Load text domain for Module |
| 188 | * |
| 189 | * @return void |
| 190 | */ |
| 191 | public static function load_text_domain() { |
| 192 | |
| 193 | \load_plugin_textdomain( |
| 194 | 'nfd-wonder-blocks', |
| 195 | false, |
| 196 | NFD_WONDER_BLOCKS_DIR . '/languages' |
| 197 | ); |
| 198 | |
| 199 | \load_script_textdomain( |
| 200 | 'nfd-wonder-blocks', |
| 201 | 'nfd-wonder-blocks', |
| 202 | NFD_WONDER_BLOCKS_DIR . '/languages' |
| 203 | ); |
| 204 | } |
| 205 | } |