PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.5.0
AlphaListing v4.5.0
4.5.0 trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / GutenBlock.php
alphalisting / src Last commit date
Shortcode 2 weeks ago Alphabet.php 2 weeks ago Extension.php 2 weeks ago Grouping.php 2 weeks ago GutenBlock.php 2 weeks ago Indices.php 2 weeks ago Numbers.php 2 weeks ago Query.php 2 weeks ago Shortcode.php 2 weeks ago Singleton.php 2 weeks ago Strings.php 2 weeks ago
GutenBlock.php
113 lines
1 <?php
2 /**
3 * Server-side rendering of the `alphalisting` block.
4 *
5 * @package alphalisting
6 */
7
8 declare(strict_types=1);
9
10 namespace eslin87\AlphaListing;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Server-side rendering of the `alphalisting` block implementation class.
18 *
19 * @package alphalisting
20 */
21 class GutenBlock extends Singleton {
22 /**
23 * Render the block.
24 *
25 * @since 4.0.0
26 * @param array $attributes The block configured attributes.
27 * @return string The block content.
28 */
29 public function render( $attributes ) {
30 global $shortcode_tags;
31 if ( empty( $shortcode_tags ) || ! is_array( $shortcode_tags ) || ! array_key_exists( 'alphalisting', $shortcode_tags ) ) {
32 return esc_html__( 'The AlphaListing plugin has been disabled.', 'alphalisting' );
33 }
34
35 return call_user_func( $shortcode_tags['alphalisting'], $attributes );
36 }
37
38 /**
39 * Register and initialize the block.
40 *
41 * @since 4.0.0
42 * @return void
43 * @throws \Error When the plugin has not been correctly built.
44 */
45 final public function initialize() {
46 $script_asset_path = dirname( ALPHALISTING_PLUGIN_FILE ) . '/build/index.asset.php';
47 if ( ! file_exists( $script_asset_path ) ) {
48 throw new \Error(
49 'You need to run `npm start` or `npm run build` for the "alphalisting/block" block first.'
50 );
51 }
52 $index_js = 'build/index.js';
53 $script_asset = require $script_asset_path;
54 wp_register_script(
55 'alphalisting-block-editor',
56 plugins_url( $index_js, ALPHALISTING_PLUGIN_FILE ),
57 $script_asset['dependencies'],
58 ALPHALISTING_VERSION,
59 true
60 );
61
62 $editor_css = 'css/editor.css';
63 wp_register_style(
64 'alphalisting-block-editor',
65 plugins_url( $editor_css, ALPHALISTING_PLUGIN_FILE ),
66 array(),
67 ALPHALISTING_VERSION
68 );
69
70 $style_css = 'css/alphalisting-default.css';
71 wp_register_style(
72 'alphalisting-block',
73 plugins_url( $style_css, ALPHALISTING_PLUGIN_FILE ),
74 array(),
75 ALPHALISTING_VERSION
76 );
77
78 /*
79 * `scripts/` is build source, but this one file is read at runtime and
80 * must be present in the distributed package. Degrade rather than
81 * fatal if it is missing: an unguarded json_decode( false ) throws a
82 * TypeError on PHP 8 and takes the whole site down on every request.
83 */
84 $attributes_path = dirname( ALPHALISTING_PLUGIN_FILE ) . '/scripts/blocks/attributes.json';
85 $attributes = array();
86 if ( file_exists( $attributes_path ) ) {
87 $attributes_json = file_get_contents( $attributes_path ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
88 if ( is_string( $attributes_json ) ) {
89 $attributes = json_decode( $attributes_json, true );
90 }
91 }
92 if ( ! is_array( $attributes ) ) {
93 if ( defined( 'ALPHALISTING_LOG' ) && ALPHALISTING_LOG ) {
94 do_action( 'alphalisting_log', 'AlphaListing: could not read block attributes', $attributes_path );
95 }
96 $attributes = array();
97 }
98
99 $attributes = apply_filters( 'alphalisting_get_gutenberg_attributes', $attributes );
100
101 register_block_type(
102 'alphalisting/block',
103 array(
104 'editor_script' => 'alphalisting-block-editor',
105 'editor_style' => 'alphalisting-block-editor',
106 'style' => 'alphalisting-block',
107 'render_callback' => array( $this, 'render' ),
108 'attributes' => $attributes,
109 )
110 );
111 }
112 }
113