PluginProbe
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 2.17.2
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v2.17.2
4.7.2 4.7.1 4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 All 281 releases
surecart / app / src / BlockLibrary / BlockService.php
BlockService.php
60 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SureCart\BlockLibrary;
4
5 use SureCartCore\Application\Application;
6
7 /**
8 * Provide general block-related functionality.
9 */
10 class BlockService {
11 /**
12 * View engine.
13 *
14 * @var Application
15 */
16 protected $app = null;
17
18 /**
19 * Constructor.
20 *
21 * @param Application $app Application Instance.
22 */
23 public function __construct( Application $app ) {
24 $this->app = $app;
25 }
26
27 /**
28 * Render a block using a template
29 *
30 * @param string|string[] $views A view or array of views.
31 * @param array<string, mixed> $context Context to send.
32 * @return string View html output.
33 */
34 public function render( $views, $context = [] ) {
35 return apply_filters( 'surecart_block_output', $this->app->views()->make( $views )->with( $context )->toString() );
36 }
37
38 /**
39 * Find all blocks and nested blocks by name.
40 *
41 * @param string $type Block item to filter by.
42 * @param string $name Block name.
43 * @param array $blocks Array of blocks.
44 * @return array
45 */
46 public function filterBy( $type, $name, $blocks ) {
47 $found_blocks = [];
48 $blocks = (array) $blocks;
49 foreach ( $blocks as $block ) {
50 if ( $name === $block[ $type ] ) {
51 $found_blocks[] = $block;
52 }
53 if ( ! empty( $block['innerBlocks'] ) ) {
54 $found_blocks = array_merge( $found_blocks, $this->filterBy( $type, $name, $block['innerBlocks'] ) );
55 }
56 }
57 return $found_blocks;
58 }
59 }
60