| 1 |
<?php |
| 2 |
/** |
| 3 |
* Sureforms Blocks Initializer |
| 4 |
* |
| 5 |
* Enqueue CSS/JS of all the blocks. |
| 6 |
* |
| 7 |
* @since 0.0.1 |
| 8 |
* @package Sureforms |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Spec_Init_Blocks. |
| 17 |
* |
| 18 |
* @package Sureforms |
| 19 |
*/ |
| 20 |
class Spec_Init_Blocks { |
| 21 |
|
| 22 |
/** |
| 23 |
* Member Variable |
| 24 |
* |
| 25 |
* @var instance |
| 26 |
*/ |
| 27 |
private static $instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Store Json variable |
| 31 |
* |
| 32 |
* @since 0.0.1 |
| 33 |
* @var instance |
| 34 |
*/ |
| 35 |
public static $icon_json; |
| 36 |
|
| 37 |
/** |
| 38 |
* As our svg icon is too long array so we will divide that into number of icon chunks. |
| 39 |
* |
| 40 |
* @var int |
| 41 |
* @since 0.0.1 |
| 42 |
*/ |
| 43 |
public static $number_of_icon_chunks = 4; |
| 44 |
|
| 45 |
/** |
| 46 |
* Initiator |
| 47 |
*/ |
| 48 |
public static function get_instance() { |
| 49 |
if ( ! isset( self::$instance ) ) { |
| 50 |
self::$instance = new self(); |
| 51 |
} |
| 52 |
return self::$instance; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor |
| 57 |
*/ |
| 58 |
public function __construct() { |
| 59 |
|
| 60 |
// Hook: Frontend assets. |
| 61 |
add_action( 'enqueue_block_assets', [ $this, 'block_assets' ] ); |
| 62 |
|
| 63 |
// Hook: Editor assets. |
| 64 |
add_action( 'enqueue_block_editor_assets', [ $this, 'editor_assets' ] ); |
| 65 |
add_action( 'enqueue_block_assets', [ $this, 'editor_assets' ] ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Enqueue Gutenberg block assets for both frontend + backend. |
| 70 |
* |
| 71 |
* @since 0.0.1 |
| 72 |
*/ |
| 73 |
public function block_assets() { |
| 74 |
|
| 75 |
global $post; |
| 76 |
|
| 77 |
if ( empty( $post->post_content ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
// find blocks in post content. |
| 82 |
$blocks = parse_blocks( $post->post_content ); |
| 83 |
|
| 84 |
if ( empty( $blocks ) && ! is_array( $blocks ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$allowed_blocks = [ |
| 89 |
'srfm/form', |
| 90 |
'srfm/advanced-heading', |
| 91 |
'srfm/separator', |
| 92 |
'srfm/image', |
| 93 |
'srfm/icon', |
| 94 |
]; |
| 95 |
|
| 96 |
$has_required_block = false; |
| 97 |
|
| 98 |
foreach ( $blocks as $block ) { |
| 99 |
|
| 100 |
if ( $has_required_block ) { |
| 101 |
break; |
| 102 |
} |
| 103 |
|
| 104 |
if ( in_array( $block['blockName'], $allowed_blocks, true ) ) { |
| 105 |
$has_required_block = true; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
if ( ! $has_required_block ) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// Register block styles for both frontend + backend. |
| 114 |
wp_enqueue_style( |
| 115 |
'srfm-block-style-css', // Handle. |
| 116 |
SRFM_URL . 'modules/gutenberg/build/style-blocks.css', |
| 117 |
is_admin() ? [ 'wp-editor' ] : null, // Dependency to include the CSS after it. |
| 118 |
SRFM_VER // filemtime( plugin_dir_path( __DIR__ ) . 'build/style-blocks.css' ) // Version: File modification time. |
| 119 |
); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Enqueue assets for both backend. |
| 125 |
* |
| 126 |
* @since 0.0.1 |
| 127 |
*/ |
| 128 |
public function editor_assets() { |
| 129 |
|
| 130 |
$post_id = isset( $_GET['post'] ) ? intval( $_GET['post'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 131 |
$post_type = get_post_type( $post_id ); |
| 132 |
|
| 133 |
if ( SRFM_FORMS_POST_TYPE === $post_type ) { |
| 134 |
|
| 135 |
$script_dep_path = SRFM_DIR . 'modules/gutenberg/build/blocks.asset.php'; |
| 136 |
$script_info = file_exists( $script_dep_path ) |
| 137 |
? include $script_dep_path |
| 138 |
: [ |
| 139 |
'dependencies' => [], |
| 140 |
'version' => SRFM_VER, |
| 141 |
]; |
| 142 |
$script_dep = array_merge( $script_info['dependencies'], [ 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-editor' ] ); |
| 143 |
$script_ver = $script_info['version']; |
| 144 |
|
| 145 |
// Register block editor script for backend. |
| 146 |
wp_enqueue_script( |
| 147 |
'srfm-spec-block-js', // Handle. |
| 148 |
SRFM_URL . 'modules/gutenberg/build/blocks.js', |
| 149 |
$script_dep, // Dependencies, defined above. |
| 150 |
$script_ver, // Version: filemtime — Gets file modification time. |
| 151 |
true // Enqueue the script in the footer. |
| 152 |
); |
| 153 |
|
| 154 |
wp_set_script_translations( 'srfm-spec-block-js', 'sureforms' ); |
| 155 |
|
| 156 |
// Register block editor styles for backend. |
| 157 |
wp_enqueue_style( |
| 158 |
'srfm-block-block-editor-css', // Handle. |
| 159 |
SRFM_URL . 'modules/gutenberg/build/blocks.style.css', |
| 160 |
[ 'wp-edit-blocks' ], // Dependency to include the CSS after it. |
| 161 |
SRFM_VER // Version: File modification time. |
| 162 |
); |
| 163 |
|
| 164 |
// Common Editor style. |
| 165 |
wp_enqueue_style( |
| 166 |
'srfm-block-common-editor-css', // Handle. |
| 167 |
SRFM_URL . 'modules/gutenberg/dist/editor.css', |
| 168 |
[ 'wp-edit-blocks' ], // Dependency to include the CSS after it. |
| 169 |
SRFM_VER // Version: File modification time. |
| 170 |
); |
| 171 |
|
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Prepare if class 'Spec_Init_Blocks' exist. |
| 178 |
* Kicking this off by calling 'get_instance()' method |
| 179 |
*/ |
| 180 |
Spec_Init_Blocks::get_instance(); |
| 181 |
|