| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Block Service Provider |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace SureCart\BlockLibrary; |
| 8 |
|
| 9 |
use SureCartBlocks\Blocks\BlockService; |
| 10 |
use SureCartCore\ServiceProviders\ServiceProviderInterface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Block Service Provider Class |
| 14 |
* Registers block service used throughout the plugin |
| 15 |
* |
| 16 |
* @author SureCart <andre@surecart.com> |
| 17 |
* @since 1.0.0 |
| 18 |
* @license GPL |
| 19 |
*/ |
| 20 |
class BlockServiceProvider implements ServiceProviderInterface { |
| 21 |
/** |
| 22 |
* {@inheritDoc} |
| 23 |
* |
| 24 |
* @param \Pimple\Container $container Service Container. |
| 25 |
*/ |
| 26 |
public function register( $container ) { |
| 27 |
$app = $container[ SURECART_APPLICATION_KEY ]; |
| 28 |
|
| 29 |
$container['blocks'] = function () use ( $app ) { |
| 30 |
return new BlockService( $app ); |
| 31 |
}; |
| 32 |
|
| 33 |
$container['blocks.patterns'] = function () use ( $app ) { |
| 34 |
return new BlockPatternsService( $app ); |
| 35 |
}; |
| 36 |
|
| 37 |
$container['blocks.validations'] = function () { |
| 38 |
return new BlockValidationService( |
| 39 |
apply_filters( |
| 40 |
'surecart_block_validators', |
| 41 |
[ |
| 42 |
new \SureCart\BlockValidator\VariantChoice(), |
| 43 |
] |
| 44 |
) |
| 45 |
); |
| 46 |
}; |
| 47 |
|
| 48 |
$app->alias( 'blocks', 'blocks' ); |
| 49 |
|
| 50 |
$app->alias( |
| 51 |
'block', |
| 52 |
function () use ( $app ) { |
| 53 |
return call_user_func_array( [ $app->blocks(), 'render' ], func_get_args() ); |
| 54 |
} |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritDoc} |
| 60 |
* |
| 61 |
* @param \Pimple\Container $container Service Container. |
| 62 |
* |
| 63 |
* @return void |
| 64 |
* |
| 65 |
* phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter |
| 66 |
*/ |
| 67 |
public function bootstrap( $container ) { |
| 68 |
$container['blocks.patterns']->bootstrap(); |
| 69 |
$container['blocks.validations']->bootstrap(); |
| 70 |
|
| 71 |
// allow design tokens in css. |
| 72 |
add_filter( |
| 73 |
'safe_style_css', |
| 74 |
function( $styles ) { |
| 75 |
return array_merge( |
| 76 |
[ |
| 77 |
'--spacing', |
| 78 |
'--font-weight', |
| 79 |
'--line-height', |
| 80 |
'--font-size', |
| 81 |
'--text-align', |
| 82 |
'--color', |
| 83 |
'--sc-input-label-color', |
| 84 |
'--primary-background', |
| 85 |
'--primary-color', |
| 86 |
'--sc-color-primary-text', |
| 87 |
'--sc-color-primary-500', |
| 88 |
'--sc-focus-ring-color-primary', |
| 89 |
'--sc-input-border-color-focus', |
| 90 |
], |
| 91 |
$styles |
| 92 |
); |
| 93 |
} |
| 94 |
); |
| 95 |
// allow our web components in wp_kses contexts. |
| 96 |
add_filter( 'wp_kses_allowed_html', [ $this, 'ksesComponents' ] ); |
| 97 |
// register our blocks. |
| 98 |
add_action( 'init', [ $this, 'registerBlocks' ] ); |
| 99 |
// register our category. |
| 100 |
add_action( 'block_categories_all', [ $this, 'registerBlockCategories' ] ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Register our custom block category. |
| 105 |
* |
| 106 |
* @param array $categories Array of categories. |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function registerBlockCategories( $categories ) { |
| 110 |
return [ |
| 111 |
...[ |
| 112 |
[ |
| 113 |
'slug' => 'surecart', |
| 114 |
'title' => esc_html__( 'SureCart', 'surecart' ), |
| 115 |
], |
| 116 |
], |
| 117 |
...$categories, |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Add iFrame to allowed wp_kses_post tags |
| 123 |
* |
| 124 |
* @param array $tags Allowed tags, attributes, and/or entities. |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function ksesComponents( $tags ) { |
| 129 |
$components = json_decode( file_get_contents( plugin_dir_path( SURECART_PLUGIN_FILE ) . 'app/src/Support/kses.json' ), true ); |
| 130 |
|
| 131 |
// add slot to defaults. |
| 132 |
$tags['span']['slot'] = true; |
| 133 |
$tags['div']['slot'] = true; |
| 134 |
$tags['sc-spinner']['data-*'] = true; |
| 135 |
|
| 136 |
return array_merge( $components, $tags ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Register blocks from config |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function registerBlocks() { |
| 145 |
$service = \SureCart::resolve( SURECART_CONFIG_KEY ); |
| 146 |
if ( ! empty( $service['blocks'] ) ) { |
| 147 |
foreach ( $service['blocks'] as $block ) { |
| 148 |
( new $block() )->register(); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
|