build
1 month ago
src
1 month ago
BlockAPI.php
2 years ago
FilterBlocks.php
5 months ago
Integration.php
1 month ago
RegisterBlocks.php
2 years ago
editor.css
2 years ago
editor.js
2 years ago
FilterBlocks.php
233 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Filter block. |
| 4 | * |
| 5 | * @package ShopPress |
| 6 | */ |
| 7 | |
| 8 | namespace ShopPress\BlockEditor; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use WP_Block_Type_Registry; |
| 13 | |
| 14 | class FilterBlocks { |
| 15 | /** |
| 16 | * Init integration blocks. |
| 17 | * |
| 18 | * @since 1.4.0 |
| 19 | */ |
| 20 | public static function init() { |
| 21 | add_filter( 'allowed_block_types_all', array( __CLASS__, 'filter_blocks' ), 10, 2 ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Filters blocks. |
| 26 | * |
| 27 | * @since 1.4.0 |
| 28 | */ |
| 29 | public static function filter_blocks( $allowed_block_types, $editor_context ) { |
| 30 | // In some editor contexts, there may be no post object; bail early in that case. |
| 31 | if ( empty( $editor_context ) || empty( $editor_context->post ) ) { |
| 32 | return $allowed_block_types; |
| 33 | } |
| 34 | |
| 35 | $all_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered(); |
| 36 | $custom_type = get_post_meta( $editor_context->post->ID, 'custom_type', true ); |
| 37 | $post_type = $editor_context->post->post_type; |
| 38 | |
| 39 | if ( ! $custom_type ) { |
| 40 | return $allowed_block_types; |
| 41 | } |
| 42 | |
| 43 | $core_blocks = array_filter( |
| 44 | array_keys( $all_blocks ), |
| 45 | function ( $block ) { |
| 46 | return strpos( $block, 'shop-press/' ) !== 0; |
| 47 | } |
| 48 | ); |
| 49 | |
| 50 | $single_blocks = array( |
| 51 | 'shop-press/product-title', |
| 52 | 'shop-press/product-price', |
| 53 | 'shop-press/product-add-to-cart', |
| 54 | 'shop-press/product-attributes', |
| 55 | 'shop-press/product-breadcrumb', |
| 56 | // 'shop-press/product-call-for-price', |
| 57 | 'shop-press/product-categories', |
| 58 | 'shop-press/product-compare', |
| 59 | 'shop-press/product-description', |
| 60 | 'shop-press/product-content', |
| 61 | 'shop-press/product-discount', |
| 62 | // 'shop-press/product-flash-sale-countdown', |
| 63 | 'shop-press/product-images', |
| 64 | 'shop-press/product-navigation', |
| 65 | 'shop-press/product-rating', |
| 66 | 'shop-press/product-review', |
| 67 | 'shop-press/product-sale-badge', |
| 68 | // 'shop-press/product-sharing', |
| 69 | 'shop-press/product-size-chart', |
| 70 | 'shop-press/product-sku', |
| 71 | 'shop-press/product-stock', |
| 72 | // 'shop-press/product-stock-progress-bar', |
| 73 | // 'shop-press/product-suggest-price', |
| 74 | 'shop-press/product-tags', |
| 75 | 'shop-press/product-weight', |
| 76 | 'shop-press/product-wishlist', |
| 77 | 'shop-press/product-tabs', |
| 78 | ); |
| 79 | |
| 80 | $cart_blocks = array( |
| 81 | 'shop-press/cart-coupon', |
| 82 | 'shop-press/cart-table', |
| 83 | 'shop-press/cart-totals', |
| 84 | ); |
| 85 | |
| 86 | $empty_cart = array( |
| 87 | 'shop-press/cart-empty-message', |
| 88 | 'shop-press/cart-return-to-shop', |
| 89 | ); |
| 90 | |
| 91 | $checkout_blocks = array( |
| 92 | 'shop-press/checkout-additional-fields', |
| 93 | 'shop-press/checkout-form-billing', |
| 94 | 'shop-press/checkout-form-coupon', |
| 95 | 'shop-press/checkout-order-review', |
| 96 | 'shop-press/checkout-payment', |
| 97 | 'shop-press/checkout-login-form', |
| 98 | 'shop-press/checkout-shipping-form', |
| 99 | ); |
| 100 | |
| 101 | $thankyou_blocks = array( |
| 102 | 'shop-press/thankyou-order-details', |
| 103 | 'shop-press/thankyou-details-customer', |
| 104 | 'shop-press/thankyou-order-review', |
| 105 | ); |
| 106 | |
| 107 | $my_account_blocks = array( |
| 108 | 'shop-press/my-account', |
| 109 | ); |
| 110 | |
| 111 | $wishlist_blocks = array( |
| 112 | 'shop-press/my-wishlist', |
| 113 | ); |
| 114 | |
| 115 | $compare_blocks = array( |
| 116 | 'shop-press/compare', |
| 117 | ); |
| 118 | |
| 119 | $shop_blocks = array( |
| 120 | 'shop-press/shop-product', |
| 121 | 'shop-press/shop-filters', |
| 122 | ); |
| 123 | |
| 124 | $dashboard_blocks = array( |
| 125 | 'shop-press/my-account-dashboard', |
| 126 | ); |
| 127 | |
| 128 | $orders_blocks = array( |
| 129 | 'shop-press/my-account-orders', |
| 130 | ); |
| 131 | |
| 132 | $downloads_blocks = array( |
| 133 | 'shop-press/my-account-downloads', |
| 134 | ); |
| 135 | |
| 136 | $addresses_blocks = array( |
| 137 | 'shop-press/my-account-addresses', |
| 138 | ); |
| 139 | |
| 140 | $account_details_blocks = array( |
| 141 | 'shop-press/my-account-edit-account', |
| 142 | ); |
| 143 | |
| 144 | $account_login_blocks = array( |
| 145 | 'shop-press/my-account-login', |
| 146 | ); |
| 147 | |
| 148 | $my_account_blocks = array( |
| 149 | 'shop-press/my-account', |
| 150 | ); |
| 151 | |
| 152 | $my_account_wishlist_blocks = array( |
| 153 | 'shop-press/wishlist_blocks', |
| 154 | ); |
| 155 | |
| 156 | $my_account_notifications_blocks = array( |
| 157 | 'shop-press/my-account-notifications', |
| 158 | ); |
| 159 | |
| 160 | $loop_blocks = array( |
| 161 | 'shop-press/loop-add-to-cart', |
| 162 | 'shop-press/loop-categories', |
| 163 | 'shop-press/loop-compare', |
| 164 | 'shop-press/loop-description', |
| 165 | 'shop-press/loop-discount', |
| 166 | // 'shop-press/loop-flash-sale-countdown', |
| 167 | 'shop-press/loop-price', |
| 168 | 'shop-press/loop-quick-view', |
| 169 | 'shop-press/loop-rating', |
| 170 | 'shop-press/loop-review', |
| 171 | 'shop-press/loop-sale-flash', |
| 172 | 'shop-press/loop-sku', |
| 173 | 'shop-press/loop-stock', |
| 174 | 'shop-press/loop-title', |
| 175 | 'shop-press/loop-tags', |
| 176 | 'shop-press/loop-thumbnail', |
| 177 | 'shop-press/loop-wishlist', |
| 178 | ); |
| 179 | |
| 180 | if ( $post_type === 'shoppress_pages' ) { |
| 181 | |
| 182 | if ( $custom_type === 'single' || $custom_type === 'quick_view' ) { |
| 183 | $allowed_block_types = array_merge( $core_blocks, $single_blocks ); |
| 184 | } elseif ( $custom_type === 'cart' ) { |
| 185 | $allowed_block_types = array_merge( $core_blocks, $cart_blocks ); |
| 186 | } elseif ( $custom_type === 'checkout' ) { |
| 187 | $allowed_block_types = array_merge( $core_blocks, $checkout_blocks ); |
| 188 | } elseif ( $custom_type === 'shop' || $custom_type === 'archive' ) { |
| 189 | $allowed_block_types = array_merge( $core_blocks, $shop_blocks ); |
| 190 | } elseif ( $custom_type === 'empty_cart' ) { |
| 191 | $allowed_block_types = array_merge( $core_blocks, $empty_cart ); |
| 192 | } elseif ( $custom_type === 'wishlist' ) { |
| 193 | $allowed_block_types = array_merge( $core_blocks, $wishlist_blocks ); |
| 194 | } elseif ( $custom_type === 'compare' ) { |
| 195 | $allowed_block_types = array_merge( $core_blocks, $compare_blocks ); |
| 196 | } elseif ( $custom_type === 'thank_you' ) { |
| 197 | $allowed_block_types = array_merge( $core_blocks, $thankyou_blocks ); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if ( $post_type === 'shoppress_myaccount' ) { |
| 202 | |
| 203 | if ( $custom_type === 'dashboard' ) { |
| 204 | $allowed_block_types = array_merge( $core_blocks, $dashboard_blocks ); |
| 205 | } elseif ( $custom_type === 'orders' ) { |
| 206 | $allowed_block_types = array_merge( $core_blocks, $orders_blocks ); |
| 207 | } elseif ( $custom_type === 'downloads' ) { |
| 208 | $allowed_block_types = array_merge( $core_blocks, $downloads_blocks ); |
| 209 | } elseif ( $custom_type === 'addresses' ) { |
| 210 | $allowed_block_types = array_merge( $core_blocks, $addresses_blocks ); |
| 211 | } elseif ( $custom_type === 'account_details' ) { |
| 212 | $allowed_block_types = array_merge( $core_blocks, $account_details_blocks ); |
| 213 | } elseif ( $custom_type === 'my_account' ) { |
| 214 | $allowed_block_types = array_merge( $core_blocks, $my_account_blocks ); |
| 215 | } elseif ( $custom_type === 'my_account_wishlist' ) { |
| 216 | $allowed_block_types = array_merge( $core_blocks, $my_account_wishlist_blocks ); |
| 217 | } elseif ( $custom_type === 'my_account_notifications' ) { |
| 218 | $allowed_block_types = array_merge( $core_blocks, $my_account_notifications_blocks ); |
| 219 | } elseif ( $custom_type === 'my_account_wishlist' ) { |
| 220 | $allowed_block_types = array_merge( $core_blocks, $my_account_wishlist_blocks ); |
| 221 | } elseif ( $custom_type === 'login_register_form' ) { |
| 222 | $allowed_block_types = array_merge( $core_blocks, $account_login_blocks ); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if ( $post_type === 'shoppress_loop' ) { |
| 227 | $allowed_block_types = array_merge( $core_blocks, $loop_blocks ); |
| 228 | } |
| 229 | |
| 230 | return $allowed_block_types; |
| 231 | } |
| 232 | } |
| 233 |