Shortcode
6 months ago
Alphabet.php
6 months ago
Extension.php
6 months ago
Grouping.php
6 months ago
GutenBlock.php
6 months ago
Indices.php
6 months ago
Numbers.php
6 months ago
Query.php
6 months ago
Shortcode.php
6 months ago
Singleton.php
6 months ago
Strings.php
6 months ago
GutenBlock.php
93 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Server-side rendering of the `alphalisting` block. |
| 4 | * |
| 5 | * @package WordPress |
| 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 WordPress |
| 20 | */ |
| 21 | class GutenBlock extends Singleton implements Extension { |
| 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 'The AlphaListing plugin has been disabled.'; |
| 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 | $attributes = json_decode( file_get_contents( dirname( ALPHALISTING_PLUGIN_FILE ) . '/scripts/blocks/attributes.json' ), true ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 79 | $attributes = apply_filters( 'alphalisting_get_gutenberg_attributes', $attributes ); |
| 80 | |
| 81 | register_block_type( |
| 82 | 'alphalisting/block', |
| 83 | array( |
| 84 | 'editor_script' => 'alphalisting-block-editor', |
| 85 | 'editor_style' => 'alphalisting-block-editor', |
| 86 | 'style' => 'alphalisting-block', |
| 87 | 'render_callback' => array( $this, 'render' ), |
| 88 | 'attributes' => $attributes, |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 |