| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Events Block |
| 4 |
* Description: Events Gutenberg Block to Create Events Grid In Block Editor. |
| 5 |
* Author: Cool Plugins |
| 6 |
* Author URI: https://coolplugins.net/ |
| 7 |
* Version: 1.0.5 |
| 8 |
* License: GPLv2 or later |
| 9 |
* Text Domain: events-block |
| 10 |
*/ |
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
define( 'EVENTS_BLOCK_VERSION', '1.0.5' ); |
| 13 |
define( 'EVENTS_BLOCK_FILE', __FILE__ ); |
| 14 |
define( 'EVENTS_BLOCK_PATH', plugin_dir_path( __FILE__ ) ); |
| 15 |
define( 'EVENTS_BLOCK_URL', plugin_dir_url( __FILE__ ) ); |
| 16 |
|
| 17 |
final class EVTB_Events_Block { |
| 18 |
private static $instance = null; |
| 19 |
public static function get_instance() { |
| 20 |
if ( ! isset( self::$instance ) ) self::$instance = new self(); |
| 21 |
return self::$instance; |
| 22 |
} |
| 23 |
private function __construct() { |
| 24 |
|
| 25 |
register_activation_hook( EVENTS_BLOCK_FILE, array( $this, 'evtb_plugin_activate' ) ); |
| 26 |
add_action( 'init', array( $this, 'init' ) ); |
| 27 |
add_action( 'enqueue_block_editor_assets', array( $this, 'editor_assets' ), 5 ); |
| 28 |
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_assets' ) ); |
| 29 |
} |
| 30 |
/** |
| 31 |
* Plugin Activation Hook |
| 32 |
* Saves plugin version and install dates |
| 33 |
*/ |
| 34 |
public function evtb_plugin_activate() { |
| 35 |
|
| 36 |
update_option( 'evtb_version', EVENTS_BLOCK_VERSION ); |
| 37 |
update_option( 'evtb_activation_time', gmdate( 'Y-m-d h:i:s' ) ); |
| 38 |
|
| 39 |
if (!get_option( 'evtb_initial_save_version' ) ) { |
| 40 |
add_option( 'evtb_initial_save_version', EVENTS_BLOCK_VERSION ); |
| 41 |
} |
| 42 |
if(!get_option( 'evtb_install_date' ) ) { |
| 43 |
add_option( 'evtb_install_date', gmdate('Y-m-d h:i:s') ); |
| 44 |
} |
| 45 |
} |
| 46 |
public function init() { |
| 47 |
// Register assets first (required for block.json) |
| 48 |
$asset = file_exists( EVENTS_BLOCK_PATH . 'build/index.asset.php' ) ? require EVENTS_BLOCK_PATH . 'build/index.asset.php' : array( 'dependencies' => array(), 'version' => EVENTS_BLOCK_VERSION ); |
| 49 |
|
| 50 |
// Block Directory installations may have timing issues, so we register first |
| 51 |
wp_register_script( |
| 52 |
'evtb-events-blocks', |
| 53 |
EVENTS_BLOCK_URL . 'build/index.js', |
| 54 |
$asset['dependencies'], |
| 55 |
EVENTS_BLOCK_VERSION, |
| 56 |
true // Load in footer (after dependencies), but we'll enqueue early |
| 57 |
); |
| 58 |
wp_localize_script( 'evtb-events-blocks', 'evtbPluginData', array( |
| 59 |
'pluginUrl' => EVENTS_BLOCK_URL, |
| 60 |
'images' => array( |
| 61 |
'crazyDJ' => EVENTS_BLOCK_URL . 'assets/images/crazy-DJ-experience-santa-cruz.webp', |
| 62 |
'rockBand' => EVENTS_BLOCK_URL . 'assets/images/cute-girls-rock-band-performance.webp', |
| 63 |
'foodDistribution' => EVENTS_BLOCK_URL . 'assets/images/free-food-distribution-at-mumbai.webp' |
| 64 |
) |
| 65 |
) ); |
| 66 |
|
| 67 |
// Register styles |
| 68 |
if ( file_exists( EVENTS_BLOCK_PATH . 'editor.css' ) ) { |
| 69 |
wp_register_style( 'evtb-events-editor', EVENTS_BLOCK_URL . 'editor.css', array(), EVENTS_BLOCK_VERSION ); |
| 70 |
} |
| 71 |
if ( file_exists( EVENTS_BLOCK_PATH . 'style.css' ) ) { |
| 72 |
wp_register_style( 'evtb-events-style', EVENTS_BLOCK_URL . 'style.css', array(), EVENTS_BLOCK_VERSION ); |
| 73 |
} |
| 74 |
|
| 75 |
// Register icon font (for block.json to use) |
| 76 |
if ( file_exists( EVENTS_BLOCK_PATH . 'assets/css/evtb-icons.css' ) ) { |
| 77 |
wp_register_style( 'evtb-icons', EVENTS_BLOCK_URL . 'assets/css/evtb-icons.css', array(), EVENTS_BLOCK_VERSION ); |
| 78 |
} |
| 79 |
|
| 80 |
$blocks_path = EVENTS_BLOCK_PATH . 'blocks/'; |
| 81 |
|
| 82 |
// IMPORTANT: Register parent block FIRST, then child blocks |
| 83 |
// This ensures Block Directory recognizes the block hierarchy correctly |
| 84 |
if ( is_dir( $blocks_path . 'events-grid' ) && file_exists( $blocks_path . 'events-grid/block.json' ) ) { |
| 85 |
register_block_type( $blocks_path . 'events-grid' ); |
| 86 |
} |
| 87 |
|
| 88 |
// Register child blocks after parent |
| 89 |
if ( is_dir( $blocks_path . 'event-item' ) && file_exists( $blocks_path . 'event-item/block.json' ) ) { |
| 90 |
register_block_type( $blocks_path . 'event-item' ); |
| 91 |
} |
| 92 |
|
| 93 |
if ( is_dir( $blocks_path . 'event-date-badge' ) && file_exists( $blocks_path . 'event-date-badge/block.json' ) ) { |
| 94 |
register_block_type( $blocks_path . 'event-date-badge' ); |
| 95 |
} |
| 96 |
} |
| 97 |
public function editor_assets() { |
| 98 |
// CRITICAL: Enqueue script with HIGH priority to ensure it loads BEFORE editor initializes |
| 99 |
// This fixes the Block Directory installation timing issue |
| 100 |
wp_enqueue_script( 'evtb-events-blocks' ); |
| 101 |
|
| 102 |
// Add inline script to verify blocks are registered (for debugging) |
| 103 |
// This helps identify if blocks registered successfully |
| 104 |
wp_add_inline_script( 'evtb-events-blocks', ' |
| 105 |
// Verify blocks are registered after script loads |
| 106 |
if ( typeof wp !== "undefined" && wp.blocks ) { |
| 107 |
wp.domReady( function() { |
| 108 |
const registeredBlocks = [ "evtb/events-grid", "evtb/event-item", "evtb/event-date-badge" ]; |
| 109 |
registeredBlocks.forEach( function( blockName ) { |
| 110 |
if ( ! wp.blocks.getBlockType( blockName ) ) { |
| 111 |
console.warn( "EVTB: Block " + blockName + " not registered yet" ); |
| 112 |
} |
| 113 |
} ); |
| 114 |
} ); |
| 115 |
} |
| 116 |
', 'after' ); |
| 117 |
|
| 118 |
// Icon font will be loaded via block.json, but enqueue here as fallback for editor |
| 119 |
if ( file_exists( EVENTS_BLOCK_PATH . 'assets/css/evtb-icons.css' ) ) { |
| 120 |
wp_enqueue_style( 'evtb-icons', EVENTS_BLOCK_URL . 'assets/css/evtb-icons.css', array(), EVENTS_BLOCK_VERSION ); |
| 121 |
} |
| 122 |
} |
| 123 |
public function frontend_assets() { |
| 124 |
// Icon font will be loaded via block.json, but enqueue here as fallback for frontend |
| 125 |
if ( file_exists( EVENTS_BLOCK_PATH . 'assets/css/evtb-icons.css' ) ) { |
| 126 |
wp_enqueue_style( 'evtb-icons', EVENTS_BLOCK_URL . 'assets/css/evtb-icons.css', array(), EVENTS_BLOCK_VERSION ); |
| 127 |
} |
| 128 |
if ( file_exists( EVENTS_BLOCK_PATH . 'assets/js/frontend.js' ) ) { |
| 129 |
wp_enqueue_script( 'evtb-frontend', EVENTS_BLOCK_URL . 'assets/js/frontend.js', array(), EVENTS_BLOCK_VERSION, true ); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
EVTB_Events_Block::get_instance(); |
| 134 |
|
| 135 |
|