Animations.php
4 months ago
BackButton.php
7 months ago
BlockPatterns.php
4 months ago
Carousel.php
6 months ago
ContainerEdgeAlignment.php
7 months ago
Counter.php
4 months ago
Events.php
6 months ago
FluidTypography.php
4 months ago
Gallery.php
8 months ago
GravityFormsInline.php
7 months ago
Headline.php
4 months ago
InsertPost.php
8 months ago
ProductCategories.php
8 months ago
ReadingProgress.php
7 months ago
ReadingTime.php
8 months ago
ShapeAnimations.php
7 months ago
StackedImages.php
4 months ago
StickyColumn.php
8 months ago
Testimonials.php
8 months ago
Events.php
365 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Events module for FrontBlocks. |
| 4 | * |
| 5 | * @package FrontBlocks |
| 6 | * @author Closemarketing |
| 7 | * @copyright 2025 Closemarketing |
| 8 | * @version 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace FrontBlocks\Frontend; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Events class. |
| 17 | * |
| 18 | * @since 1.0.0 |
| 19 | */ |
| 20 | class Events { |
| 21 | /** |
| 22 | * Option key for events feature. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private $option_enable_events = 'enable_events'; |
| 27 | |
| 28 | /** |
| 29 | * Option key for events type (cpt or posts). |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | private $option_events_type = 'events_type'; |
| 34 | |
| 35 | /** |
| 36 | * Constructor. |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | if ( ! $this->is_events_enabled() ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $events_type = $this->get_events_type(); |
| 44 | |
| 45 | if ( 'cpt' === $events_type ) { |
| 46 | // Register CPT and taxonomy. |
| 47 | add_action( 'init', array( $this, 'register_cpt_event' ) ); |
| 48 | add_action( 'init', array( $this, 'register_taxonomy_event_category' ) ); |
| 49 | |
| 50 | // Meta boxes for CPT. |
| 51 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
| 52 | add_action( 'save_post_event', array( $this, 'save_meta' ) ); |
| 53 | } else { |
| 54 | // Meta boxes for posts. |
| 55 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes_posts' ) ); |
| 56 | add_action( 'save_post', array( $this, 'save_meta_posts' ) ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Check if events are enabled. |
| 62 | * |
| 63 | * @return bool |
| 64 | */ |
| 65 | private function is_events_enabled() { |
| 66 | $options = get_option( 'frontblocks_settings', array() ); |
| 67 | return (bool) ( $options[ $this->option_enable_events ] ?? false ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get events type (cpt or posts). |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | private function get_events_type() { |
| 76 | $options = get_option( 'frontblocks_settings', array() ); |
| 77 | return sanitize_text_field( $options[ $this->option_events_type ] ?? 'cpt' ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Register events custom post type. |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | public function register_cpt_event() { |
| 86 | $labels = array( |
| 87 | 'name' => __( 'Eventos', 'frontblocks' ), |
| 88 | 'singular_name' => __( 'Evento', 'frontblocks' ), |
| 89 | 'menu_name' => __( 'Eventos', 'frontblocks' ), |
| 90 | 'all_items' => __( 'Todos los eventos', 'frontblocks' ), |
| 91 | 'add_new' => __( 'Añadir evento', 'frontblocks' ), |
| 92 | 'add_new_item' => __( 'Añadir nuevo evento', 'frontblocks' ), |
| 93 | 'edit_item' => __( 'Editar evento', 'frontblocks' ), |
| 94 | 'new_item' => __( 'Nuevo evento', 'frontblocks' ), |
| 95 | 'view_item' => __( 'Ver evento', 'frontblocks' ), |
| 96 | 'search_items' => __( 'Buscar evento', 'frontblocks' ), |
| 97 | 'not_found' => __( 'No se han encontrado eventos', 'frontblocks' ), |
| 98 | ); |
| 99 | |
| 100 | $args = array( |
| 101 | 'label' => __( 'Eventos', 'frontblocks' ), |
| 102 | 'labels' => $labels, |
| 103 | 'public' => true, |
| 104 | 'show_ui' => true, |
| 105 | 'show_in_rest' => true, |
| 106 | 'has_archive' => true, |
| 107 | 'menu_position' => 6, |
| 108 | 'menu_icon' => 'dashicons-calendar-alt', |
| 109 | 'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ), |
| 110 | 'rewrite' => array( 'slug' => 'evento' ), |
| 111 | 'taxonomies' => array( 'post_tag', 'event_category' ), |
| 112 | ); |
| 113 | |
| 114 | register_post_type( 'event', $args ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Register event category taxonomy. |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function register_taxonomy_event_category() { |
| 123 | $labels = array( |
| 124 | 'name' => __( 'Categorías de eventos', 'frontblocks' ), |
| 125 | 'singular_name' => __( 'Categoría', 'frontblocks' ), |
| 126 | 'menu_name' => __( 'Categorías', 'frontblocks' ), |
| 127 | ); |
| 128 | |
| 129 | $args = array( |
| 130 | 'label' => __( 'Categorías', 'frontblocks' ), |
| 131 | 'labels' => $labels, |
| 132 | 'public' => true, |
| 133 | 'hierarchical' => true, |
| 134 | 'show_ui' => true, |
| 135 | 'show_admin_column' => true, |
| 136 | 'show_in_rest' => true, |
| 137 | 'rewrite' => array( |
| 138 | 'slug' => 'categorias-eventos', |
| 139 | 'with_front' => false, |
| 140 | ), |
| 141 | ); |
| 142 | |
| 143 | register_taxonomy( 'event_category', array( 'event' ), $args ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Add meta boxes for event data (CPT). |
| 148 | * |
| 149 | * @return void |
| 150 | */ |
| 151 | public function add_metaboxes() { |
| 152 | add_meta_box( |
| 153 | 'frbl_event_metabox', |
| 154 | __( 'Datos del Evento', 'frontblocks' ), |
| 155 | array( $this, 'render_metabox' ), |
| 156 | 'event', |
| 157 | 'normal', |
| 158 | 'default' |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Add meta boxes for event data (Posts). |
| 164 | * |
| 165 | * @return void |
| 166 | */ |
| 167 | public function add_metaboxes_posts() { |
| 168 | add_meta_box( |
| 169 | 'frbl_event_metabox', |
| 170 | __( 'Datos del Evento', 'frontblocks' ), |
| 171 | array( $this, 'render_metabox' ), |
| 172 | 'post', |
| 173 | 'normal', |
| 174 | 'default' |
| 175 | ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Render metabox content. |
| 180 | * |
| 181 | * @param WP_Post $post Post object. |
| 182 | * @return void |
| 183 | */ |
| 184 | public function render_metabox( $post ) { |
| 185 | $all_day = get_post_meta( $post->ID, 'all_day', true ); |
| 186 | $start_date = get_post_meta( $post->ID, 'start_date', true ); |
| 187 | $start_time = get_post_meta( $post->ID, 'start_time', true ); |
| 188 | $end_date = get_post_meta( $post->ID, 'end_date', true ); |
| 189 | $end_time = get_post_meta( $post->ID, 'end_time', true ); |
| 190 | $cost = get_post_meta( $post->ID, 'cost', true ); |
| 191 | $web = get_post_meta( $post->ID, 'web', true ); |
| 192 | $poster_evento = get_post_meta( $post->ID, 'poster_evento', true ); |
| 193 | $direccion_evento = get_post_meta( $post->ID, 'direccion_evento', true ); |
| 194 | |
| 195 | wp_nonce_field( 'frbl_event_save_meta', 'frbl_event_meta_nonce' ); |
| 196 | ?> |
| 197 | |
| 198 | <style> |
| 199 | #frbl_event_metabox .frbl-grid { |
| 200 | display: grid; |
| 201 | grid-template-columns: 1fr 1fr; |
| 202 | gap: 25px; |
| 203 | max-width: 1000px; |
| 204 | } |
| 205 | |
| 206 | #frbl_event_metabox .frbl-field { |
| 207 | display: flex; |
| 208 | flex-direction: column; |
| 209 | margin-bottom: 15px; |
| 210 | } |
| 211 | |
| 212 | #frbl_event_metabox label { |
| 213 | font-weight: 600; |
| 214 | margin-bottom: 6px; |
| 215 | } |
| 216 | |
| 217 | #frbl_event_metabox input[type="text"], |
| 218 | #frbl_event_metabox input[type="number"], |
| 219 | #frbl_event_metabox input[type="url"] { |
| 220 | padding: 6px 10px; |
| 221 | font-size: 14px; |
| 222 | width: 100%; |
| 223 | max-width: 380px; |
| 224 | } |
| 225 | |
| 226 | #frbl_event_metabox input[type="date"], |
| 227 | #frbl_event_metabox input[type="time"] { |
| 228 | padding: 4px 6px; |
| 229 | font-size: 13px; |
| 230 | width: 140px; |
| 231 | } |
| 232 | |
| 233 | #frbl_event_metabox .full { |
| 234 | grid-column: span 2; |
| 235 | } |
| 236 | </style> |
| 237 | |
| 238 | <div id="frbl_event_metabox" class="frbl-grid"> |
| 239 | |
| 240 | <div class="frbl-field"> |
| 241 | <label><?php esc_html_e( 'Evento todo el día', 'frontblocks' ); ?></label> |
| 242 | <input type="checkbox" name="all_day" value="1" <?php checked( $all_day, '1' ); ?> /> |
| 243 | </div> |
| 244 | |
| 245 | <div class="frbl-field"> |
| 246 | <label><?php esc_html_e( 'Coste', 'frontblocks' ); ?></label> |
| 247 | <input type="number" step="0.01" name="cost" value="<?php echo esc_attr( $cost ); ?>"> |
| 248 | </div> |
| 249 | |
| 250 | <div class="frbl-field"> |
| 251 | <label><?php esc_html_e( 'Fecha inicio', 'frontblocks' ); ?></label> |
| 252 | <input type="date" name="start_date" value="<?php echo esc_attr( $start_date ); ?>"> |
| 253 | </div> |
| 254 | |
| 255 | <div class="frbl-field"> |
| 256 | <label><?php esc_html_e( 'Hora inicio', 'frontblocks' ); ?></label> |
| 257 | <input type="time" name="start_time" value="<?php echo esc_attr( $start_time ); ?>"> |
| 258 | </div> |
| 259 | |
| 260 | <div class="frbl-field"> |
| 261 | <label><?php esc_html_e( 'Fecha fin', 'frontblocks' ); ?></label> |
| 262 | <input type="date" name="end_date" value="<?php echo esc_attr( $end_date ); ?>"> |
| 263 | </div> |
| 264 | |
| 265 | <div class="frbl-field"> |
| 266 | <label><?php esc_html_e( 'Hora fin', 'frontblocks' ); ?></label> |
| 267 | <input type="time" name="end_time" value="<?php echo esc_attr( $end_time ); ?>"> |
| 268 | </div> |
| 269 | |
| 270 | <div class="frbl-field"> |
| 271 | <label><?php esc_html_e( 'Web del evento', 'frontblocks' ); ?></label> |
| 272 | <input type="url" name="web" value="<?php echo esc_attr( $web ); ?>"> |
| 273 | </div> |
| 274 | |
| 275 | <div class="frbl-field"> |
| 276 | <label><?php esc_html_e( 'Póster del evento (url)', 'frontblocks' ); ?></label> |
| 277 | <input type="url" name="poster_evento" value="<?php echo esc_attr( $poster_evento ); ?>"> |
| 278 | </div> |
| 279 | |
| 280 | <div class="frbl-field full"> |
| 281 | <label><?php esc_html_e( 'Dirección', 'frontblocks' ); ?></label> |
| 282 | <input type="text" name="direccion_evento" value="<?php echo esc_attr( $direccion_evento ); ?>" style="max-width:100%;"> |
| 283 | </div> |
| 284 | |
| 285 | </div> |
| 286 | |
| 287 | <?php |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Save metabox data (CPT). |
| 292 | * |
| 293 | * @param int $post_id Post ID. |
| 294 | * @return void |
| 295 | */ |
| 296 | public function save_meta( $post_id ) { |
| 297 | if ( ! isset( $_POST['frbl_event_meta_nonce'] ) ) { |
| 298 | return; |
| 299 | } |
| 300 | if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['frbl_event_meta_nonce'] ) ), 'frbl_event_save_meta' ) ) { |
| 301 | return; |
| 302 | } |
| 303 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | $fields = array( |
| 308 | 'all_day' => isset( $_POST['all_day'] ) ? '1' : '0', |
| 309 | 'start_date' => isset( $_POST['start_date'] ) ? sanitize_text_field( wp_unslash( $_POST['start_date'] ) ) : '', |
| 310 | 'start_time' => isset( $_POST['start_time'] ) ? sanitize_text_field( wp_unslash( $_POST['start_time'] ) ) : '', |
| 311 | 'end_date' => isset( $_POST['end_date'] ) ? sanitize_text_field( wp_unslash( $_POST['end_date'] ) ) : '', |
| 312 | 'end_time' => isset( $_POST['end_time'] ) ? sanitize_text_field( wp_unslash( $_POST['end_time'] ) ) : '', |
| 313 | 'cost' => isset( $_POST['cost'] ) ? sanitize_text_field( wp_unslash( $_POST['cost'] ) ) : '', |
| 314 | 'web' => isset( $_POST['web'] ) ? esc_url_raw( wp_unslash( $_POST['web'] ) ) : '', |
| 315 | 'poster_evento' => isset( $_POST['poster_evento'] ) ? esc_url_raw( wp_unslash( $_POST['poster_evento'] ) ) : '', |
| 316 | 'direccion_evento' => isset( $_POST['direccion_evento'] ) ? sanitize_text_field( wp_unslash( $_POST['direccion_evento'] ) ) : '', |
| 317 | ); |
| 318 | |
| 319 | foreach ( $fields as $key => $value ) { |
| 320 | update_post_meta( $post_id, $key, $value ); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Save metabox data (Posts). |
| 326 | * |
| 327 | * @param int $post_id Post ID. |
| 328 | * @return void |
| 329 | */ |
| 330 | public function save_meta_posts( $post_id ) { |
| 331 | // Only save if metabox nonce is present (meaning user is editing a post with event data). |
| 332 | if ( ! isset( $_POST['frbl_event_meta_nonce'] ) ) { |
| 333 | return; |
| 334 | } |
| 335 | if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['frbl_event_meta_nonce'] ) ), 'frbl_event_save_meta' ) ) { |
| 336 | return; |
| 337 | } |
| 338 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | // Only save for posts. |
| 343 | if ( 'post' !== get_post_type( $post_id ) ) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | $fields = array( |
| 348 | 'all_day' => isset( $_POST['all_day'] ) ? '1' : '0', |
| 349 | 'start_date' => isset( $_POST['start_date'] ) ? sanitize_text_field( wp_unslash( $_POST['start_date'] ) ) : '', |
| 350 | 'start_time' => isset( $_POST['start_time'] ) ? sanitize_text_field( wp_unslash( $_POST['start_time'] ) ) : '', |
| 351 | 'end_date' => isset( $_POST['end_date'] ) ? sanitize_text_field( wp_unslash( $_POST['end_date'] ) ) : '', |
| 352 | 'end_time' => isset( $_POST['end_time'] ) ? sanitize_text_field( wp_unslash( $_POST['end_time'] ) ) : '', |
| 353 | 'cost' => isset( $_POST['cost'] ) ? sanitize_text_field( wp_unslash( $_POST['cost'] ) ) : '', |
| 354 | 'web' => isset( $_POST['web'] ) ? esc_url_raw( wp_unslash( $_POST['web'] ) ) : '', |
| 355 | 'poster_evento' => isset( $_POST['poster_evento'] ) ? esc_url_raw( wp_unslash( $_POST['poster_evento'] ) ) : '', |
| 356 | 'direccion_evento' => isset( $_POST['direccion_evento'] ) ? sanitize_text_field( wp_unslash( $_POST['direccion_evento'] ) ) : '', |
| 357 | ); |
| 358 | |
| 359 | foreach ( $fields as $key => $value ) { |
| 360 | update_post_meta( $post_id, $key, $value ); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 |