blocks
3 years ago
class-do-css.php
3 years ago
class-dynamic-content.php
3 years ago
class-enqueue-css.php
3 years ago
class-legacy-attributes.php
4 years ago
class-plugin-update.php
5 years ago
class-query-loop.php
3 years ago
class-render-blocks.php
3 years ago
class-rest.php
3 years ago
class-settings.php
4 years ago
dashboard.php
3 years ago
defaults.php
3 years ago
functions.php
3 years ago
general.php
3 years ago
class-render-blocks.php
171 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file handles the dynamic parts of our blocks. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Render the dynamic aspects of our blocks. |
| 14 | * |
| 15 | * @since 1.2.0 |
| 16 | */ |
| 17 | class GenerateBlocks_Render_Block { |
| 18 | /** |
| 19 | * Instance. |
| 20 | * |
| 21 | * @access private |
| 22 | * @var object Instance |
| 23 | * @since 1.2.0 |
| 24 | */ |
| 25 | private static $instance; |
| 26 | |
| 27 | /** |
| 28 | * Initiator. |
| 29 | * |
| 30 | * @since 1.2.0 |
| 31 | * @return object initialized object of class. |
| 32 | */ |
| 33 | public static function get_instance() { |
| 34 | if ( ! isset( self::$instance ) ) { |
| 35 | self::$instance = new self(); |
| 36 | } |
| 37 | |
| 38 | return self::$instance; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructor. |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | add_action( 'init', array( $this, 'register_blocks' ) ); |
| 46 | |
| 47 | if ( version_compare( $GLOBALS['wp_version'], '5.9', '>' ) ) { |
| 48 | add_filter( 'render_block', array( $this, 'filter_rendered_blocks' ), 10, 3 ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Register our dynamic blocks. |
| 54 | * |
| 55 | * @since 1.2.0 |
| 56 | */ |
| 57 | public function register_blocks() { |
| 58 | $container_args = [ |
| 59 | 'title' => esc_html__( 'Container', 'generateblocks' ), |
| 60 | 'render_callback' => [ 'GenerateBlocks_Block_Container', 'render_block' ], |
| 61 | ]; |
| 62 | |
| 63 | if ( version_compare( $GLOBALS['wp_version'], '6.1.0', '<' ) ) { |
| 64 | $container_args['editor_script'] = 'generateblocks'; |
| 65 | $container_args['editor_style'] = 'generateblocks'; |
| 66 | } else { |
| 67 | $container_args['editor_script_handles'] = [ 'generateblocks' ]; |
| 68 | $container_args['editor_style_handles'] = [ 'generateblocks' ]; |
| 69 | } |
| 70 | |
| 71 | register_block_type( |
| 72 | 'generateblocks/container', |
| 73 | $container_args |
| 74 | ); |
| 75 | |
| 76 | register_block_type( |
| 77 | 'generateblocks/grid', |
| 78 | array( |
| 79 | 'title' => esc_html__( 'Grid', 'generateblocks' ), |
| 80 | 'render_callback' => [ 'GenerateBlocks_Block_Grid', 'render_block' ], |
| 81 | 'uses_context' => array( |
| 82 | 'generateblocks/query', |
| 83 | 'generateblocks/queryId', |
| 84 | 'generateblocks/inheritQuery', |
| 85 | ), |
| 86 | ) |
| 87 | ); |
| 88 | |
| 89 | register_block_type( |
| 90 | 'generateblocks/query-loop', |
| 91 | array( |
| 92 | 'title' => esc_html__( 'Query loop', 'generateblocks' ), |
| 93 | 'render_callback' => [ 'GenerateBlocks_Block_Grid', 'render_block' ], |
| 94 | 'provides_context' => array( |
| 95 | 'generateblocks/query' => 'query', |
| 96 | 'generateblocks/queryId' => 'uniqueId', |
| 97 | 'generateblocks/inheritQuery' => 'inheritQuery', |
| 98 | ), |
| 99 | ) |
| 100 | ); |
| 101 | |
| 102 | register_block_type( |
| 103 | 'generateblocks/button-container', |
| 104 | array( |
| 105 | 'title' => esc_html__( 'Buttons', 'generateblocks' ), |
| 106 | 'render_callback' => [ 'GenerateBlocks_Block_Button_Container', 'render_block' ], |
| 107 | ) |
| 108 | ); |
| 109 | |
| 110 | register_block_type( |
| 111 | 'generateblocks/headline', |
| 112 | array( |
| 113 | 'title' => esc_html__( 'Headline', 'generateblocks' ), |
| 114 | 'render_callback' => [ 'GenerateBlocks_Block_Headline', 'render_block' ], |
| 115 | ) |
| 116 | ); |
| 117 | |
| 118 | register_block_type( |
| 119 | 'generateblocks/button', |
| 120 | array( |
| 121 | 'title' => esc_html__( 'Button', 'generateblocks' ), |
| 122 | 'render_callback' => [ 'GenerateBlocks_Block_Button', 'render_block' ], |
| 123 | 'uses_context' => array( |
| 124 | 'generateblocks/query', |
| 125 | 'generateblocks/queryId', |
| 126 | 'generateblocks/inheritQuery', |
| 127 | ), |
| 128 | ) |
| 129 | ); |
| 130 | |
| 131 | register_block_type( |
| 132 | GENERATEBLOCKS_DIR . 'dist/blocks/image', |
| 133 | array( |
| 134 | 'title' => esc_html__( 'Image', 'generateblocks' ), |
| 135 | 'render_callback' => [ 'GenerateBlocks_Block_Image', 'render_block' ], |
| 136 | 'uses_context' => array( |
| 137 | 'generateblocks/query', |
| 138 | 'generateblocks/queryId', |
| 139 | 'postType', |
| 140 | 'postId', |
| 141 | ), |
| 142 | ) |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Filter existing rendered blocks. |
| 148 | * |
| 149 | * @since 1.5.0 |
| 150 | * @param string $block_content The block content. |
| 151 | * @param array $block The block data. |
| 152 | * @param WP_Block $instance Block instance. |
| 153 | */ |
| 154 | public function filter_rendered_blocks( $block_content, $block, $instance ) { |
| 155 | $attributes = isset( $block['attrs'] ) ? $block['attrs'] : null; |
| 156 | |
| 157 | // Don't output if no dynamic link exists. |
| 158 | if ( isset( $attributes ) && ! empty( $attributes['dynamicLinkType'] ) && ! empty( $attributes['dynamicLinkRemoveIfEmpty'] ) ) { |
| 159 | $dynamic_link = GenerateBlocks_Dynamic_Content::get_dynamic_url( $attributes, $instance ); |
| 160 | |
| 161 | if ( ! $dynamic_link ) { |
| 162 | return ''; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return $block_content; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | GenerateBlocks_Render_Block::get_instance(); |
| 171 |