| 1 |
<?php |
| 2 |
/** |
| 3 |
* Extensions for blocks |
| 4 |
* |
| 5 |
* @package ghostkit |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class GhostKit_Extensions |
| 14 |
*/ |
| 15 |
class GhostKit_Extensions { |
| 16 |
/** |
| 17 |
* Registered extensions. |
| 18 |
* |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
private static $extensions = array(); |
| 22 |
|
| 23 |
/** |
| 24 |
* The list of default settings to extend Core blocks. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private static $default_supports = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* The list of unsupported Core blocks to add extension. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
private static $unsupported_blocks = array( |
| 36 |
'core/shortcode', |
| 37 |
'core/block', |
| 38 |
'core/legacy-widget', |
| 39 |
'core/freeform', |
| 40 |
'core/html', |
| 41 |
); |
| 42 |
|
| 43 |
/** |
| 44 |
* GhostKit_Extensions constructor. |
| 45 |
*/ |
| 46 |
public static function init() { |
| 47 |
add_action( |
| 48 |
'init', |
| 49 |
function () { |
| 50 |
// Register the block support. |
| 51 |
WP_Block_Supports::get_instance()->register( |
| 52 |
'ghostkit', |
| 53 |
array( |
| 54 |
'register_attribute' => 'GhostKit_Extensions::register_attribute', |
| 55 |
) |
| 56 |
); |
| 57 |
|
| 58 |
add_filter( 'render_block', 'GhostKit_Extensions::render_block', 10, 2 ); |
| 59 |
} |
| 60 |
); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register extension. |
| 65 |
* |
| 66 |
* @param string $extension_name - new extension name. |
| 67 |
* @param array $extension_data - new extension data. |
| 68 |
*/ |
| 69 |
public static function register( $extension_name, $extension_data ) { |
| 70 |
self::$extensions[ $extension_name ] = array_merge( |
| 71 |
$extension_data, |
| 72 |
array( 'name' => $extension_name ) |
| 73 |
); |
| 74 |
|
| 75 |
self::$default_supports = array_merge( |
| 76 |
self::$default_supports, |
| 77 |
$extension_data['default_supports'] ?? array() |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Registers the `ghostkit` block attribute for block types that support it. |
| 83 |
* |
| 84 |
* @param WP_Block_Type $block_type Block Type. |
| 85 |
*/ |
| 86 |
public static function register_attribute( $block_type ) { |
| 87 |
$has_ghostkit_support = block_has_support( $block_type, array( 'ghostkit' ), false ); |
| 88 |
|
| 89 |
// Add ghostkit extensions support to all Ghost Kit and Core blocks. |
| 90 |
if ( |
| 91 |
! $has_ghostkit_support && |
| 92 |
$block_type->name && |
| 93 |
( |
| 94 |
substr( $block_type->name, 0, 5 ) === 'core/' || |
| 95 |
substr( $block_type->name, 0, 9 ) === 'ghostkit/' |
| 96 |
) && |
| 97 |
! in_array( $block_type->name, self::$unsupported_blocks, true ) |
| 98 |
) { |
| 99 |
$has_ghostkit_support = true; |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! $has_ghostkit_support ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
// Add supports. |
| 107 |
if ( ! $block_type->supports ) { |
| 108 |
$block_type->supports = array(); |
| 109 |
} |
| 110 |
if ( ! array_key_exists( 'ghostkit', $block_type->supports ) ) { |
| 111 |
$block_type->supports['ghostkit'] = self::$default_supports; |
| 112 |
} else { |
| 113 |
$new_supports = self::$default_supports; |
| 114 |
|
| 115 |
// Change defaults to user custom configs. |
| 116 |
if ( is_array( $block_type->supports['ghostkit'] ) ) { |
| 117 |
foreach ( $block_type->supports['ghostkit'] as $k => $val ) { |
| 118 |
if ( true !== $val ) { |
| 119 |
$new_supports[ $k ] = $val; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
$block_type->supports['ghostkit'] = $new_supports; |
| 125 |
} |
| 126 |
|
| 127 |
// Add attribute. |
| 128 |
if ( ! $block_type->attributes ) { |
| 129 |
$block_type->attributes = array(); |
| 130 |
} |
| 131 |
if ( ! array_key_exists( 'ghostkit', $block_type->attributes ) ) { |
| 132 |
$block_type->attributes['ghostkit'] = array( |
| 133 |
'type' => 'object', |
| 134 |
); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Renders block with extensions. |
| 140 |
* |
| 141 |
* @param string $block_content Rendered block content. |
| 142 |
* @param array $block Block object. |
| 143 |
* |
| 144 |
* @return string Filtered block content. |
| 145 |
*/ |
| 146 |
public static function render_block( $block_content, $block ) { |
| 147 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); |
| 148 |
|
| 149 |
foreach ( self::$extensions as $block_extension_config ) { |
| 150 |
$has_extension_support = block_has_support( $block_type, array( 'ghostkit', $block_extension_config['name'] ), false ); |
| 151 |
|
| 152 |
if ( ! $has_extension_support || ! isset( $block_extension_config['render_block'] ) ) { |
| 153 |
continue; |
| 154 |
} |
| 155 |
|
| 156 |
$block_content = call_user_func( |
| 157 |
$block_extension_config['render_block'], |
| 158 |
$block_content, |
| 159 |
$block, |
| 160 |
$block_type |
| 161 |
); |
| 162 |
|
| 163 |
$block_content = apply_filters( 'gkt_extension_' . $block_extension_config['name'] . '_render_block', $block_content, $block, $block_type ); |
| 164 |
} |
| 165 |
|
| 166 |
return $block_content; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/deprecated/index.php'; |
| 171 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/attributes/index.php'; |
| 172 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/styles/index.php'; |
| 173 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/position/index.php'; |
| 174 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/spacings/index.php'; |
| 175 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/frame/index.php'; |
| 176 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/transform/index.php'; |
| 177 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/custom-css/index.php'; |
| 178 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/display/index.php'; |
| 179 |
require_once ghostkit()->plugin_path . 'gutenberg/extend/effects/index.php'; |
| 180 |
|
| 181 |
GhostKit_Extensions::init(); |
| 182 |
|