597 lines
26 KiB
PHP
597 lines
26 KiB
PHP
<?php
|
|
|
|
class Swi_Foot_Blocks
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
// Register blocks immediately when this object is constructed.
|
|
// The plugin constructs this class during the plugin init sequence,
|
|
// so calling register_blocks() now ensures scripts/styles are registered
|
|
// before `enqueue_block_editor_assets` runs later in the request.
|
|
$this->register_blocks();
|
|
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
|
|
// Editor assets enqueued below; data endpoints are provided via REST (see includes/class-swi-foot-rest.php)
|
|
add_action('enqueue_block_editor_assets', array($this, 'enqueue_editor_assets'));
|
|
}
|
|
|
|
public function register_blocks()
|
|
{
|
|
// Register custom block category for Swiss Football blocks
|
|
add_filter('block_categories_all', function($categories) {
|
|
// Check if category already exists
|
|
foreach ($categories as $cat) {
|
|
if ($cat['slug'] === 'swi-football') {
|
|
return $categories; // Already registered
|
|
}
|
|
}
|
|
// Add custom category with translated label
|
|
$categories[] = array(
|
|
'slug' => 'swi-football',
|
|
'title' => __('Swiss Football', 'swi_foot_matchdata'),
|
|
'icon' => 'soccer'
|
|
);
|
|
return $categories;
|
|
});
|
|
|
|
// Register blocks from metadata (block.json) and provide server-side render callbacks
|
|
$base = dirname(__DIR__) . '/blocks';
|
|
|
|
// Register editor script and styles from webpack build
|
|
$editor_js_url = SWI_FOOT_PLUGIN_URL . 'assets/build/editor-blocks.js';
|
|
|
|
wp_register_script(
|
|
'swi-foot-editor-blocks',
|
|
$editor_js_url,
|
|
array('wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n', 'wp-data', 'wp-rich-text'),
|
|
SWI_FOOT_PLUGIN_VERSION
|
|
);
|
|
|
|
$editor_css = SWI_FOOT_PLUGIN_URL . 'assets/blocks.css';
|
|
wp_register_style('swi-foot-editor-styles', $editor_css, array(), SWI_FOOT_PLUGIN_VERSION);
|
|
|
|
if ( file_exists( $base . '/standings/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/standings', array(
|
|
'render_callback' => array( $this, 'render_standings_block' ),
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles'
|
|
) );
|
|
}
|
|
|
|
if ( file_exists( $base . '/context/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/context', array(
|
|
'render_callback' => function($attributes, $content, $block) {
|
|
// Provide a transparent wrapper but push provider context to a global stack
|
|
// so inner shortcodes and blocks can read it during rendering.
|
|
$provided = is_array($attributes['swi_foot_context'] ?? null) ? $attributes['swi_foot_context'] : array();
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $provided);
|
|
|
|
global $swi_foot_context_stack;
|
|
if (!isset($swi_foot_context_stack) || !is_array($swi_foot_context_stack)) {
|
|
$swi_foot_context_stack = array();
|
|
}
|
|
$swi_foot_context_stack[] = $ctx;
|
|
|
|
// Render inner blocks (so shortcodes executed within this scope can read the global)
|
|
$rendered = do_blocks($content);
|
|
|
|
// Pop stack
|
|
array_pop($swi_foot_context_stack);
|
|
|
|
return $rendered;
|
|
},
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles'
|
|
) );
|
|
}
|
|
|
|
if ( file_exists( $base . '/schedule/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/schedule', array(
|
|
'render_callback' => array( $this, 'render_schedule_block' ),
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles'
|
|
) );
|
|
}
|
|
|
|
/**
|
|
* Deprecated shortcode-inserter block
|
|
* Kept for backwards compatibility only
|
|
*/
|
|
if ( file_exists( $base . '/shortcode-inserter/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/shortcode-inserter', array(
|
|
'render_callback' => array( $this, 'render_shortcode_inserter_block' ),
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles',
|
|
'deprecated' => true,
|
|
) );
|
|
}
|
|
|
|
if ( file_exists( $base . '/match-roster/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/match-roster', array(
|
|
'render_callback' => array( $this, 'render_match_roster_block' ),
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles'
|
|
) );
|
|
}
|
|
|
|
if ( file_exists( $base . '/match-events/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/match-events', array(
|
|
'render_callback' => array( $this, 'render_match_events_block' ),
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles'
|
|
) );
|
|
}
|
|
|
|
if ( file_exists( $base . '/match-bench/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/match-bench', array(
|
|
'render_callback' => array( $this, 'render_match_bench_block' ),
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles'
|
|
) );
|
|
}
|
|
|
|
if ( file_exists( $base . '/match-referees/block.json' ) ) {
|
|
register_block_type_from_metadata( $base . '/match-referees', array(
|
|
'render_callback' => array( $this, 'render_match_referees_block' ),
|
|
'editor_script' => 'swi-foot-editor-blocks',
|
|
'editor_style' => 'swi-foot-editor-styles'
|
|
) );
|
|
}
|
|
}
|
|
|
|
public function enqueue_scripts()
|
|
{
|
|
wp_enqueue_script('swi-foot-blocks', SWI_FOOT_PLUGIN_URL . 'assets/blocks.js', array('jquery'), SWI_FOOT_PLUGIN_VERSION, true);
|
|
wp_enqueue_style('swi-foot-blocks', SWI_FOOT_PLUGIN_URL . 'assets/blocks.css', array(), SWI_FOOT_PLUGIN_VERSION);
|
|
|
|
// Localize REST API data for frontend event refresh polling
|
|
wp_localize_script('swi-foot-blocks', 'swiFootRest', array(
|
|
'rest_url' => rest_url('swi-foot/v1/'),
|
|
'rest_nonce' => wp_create_nonce('wp_rest')
|
|
));
|
|
}
|
|
|
|
/*
|
|
public function enqueue_editor_assets()
|
|
{
|
|
wp_enqueue_script(
|
|
'swi-foot-editor-blocks',
|
|
SWI_FOOT_PLUGIN_URL . 'assets/editor-blocks.js',
|
|
array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n'),
|
|
SWI_FOOT_PLUGIN_VERSION
|
|
);
|
|
|
|
wp_localize_script('swi-foot-editor-blocks', 'swiFootData', array(
|
|
'teams' => $this->get_teams_for_editor(),
|
|
'shortcodes' => array(
|
|
'match' => __('Full Match Display', 'swi_foot_matchdata'),
|
|
'match_home_team' => __('Home Team', 'swi_foot_matchdata'),
|
|
'match_away_team' => __('Away Team', 'swi_foot_matchdata'),
|
|
'match_date' => __('Match Date', 'swi_foot_matchdata'),
|
|
'match_time' => __('Match Time', 'swi_foot_matchdata'),
|
|
'match_venue' => __('Venue', 'swi_foot_matchdata'),
|
|
'match_score' => __('Score', 'swi_foot_matchdata'),
|
|
'match_status' => __('Status', 'swi_foot_matchdata'),
|
|
'match_league' => __('League', 'swi_foot_matchdata'),
|
|
'match_round' => __('Round', 'swi_foot_matchdata')
|
|
)
|
|
));
|
|
}
|
|
*/
|
|
|
|
public function enqueue_editor_assets()
|
|
{
|
|
// Enqueue the registered script handle (register_blocks() registers it on init)
|
|
if ( ! wp_script_is( 'swi-foot-editor-blocks', 'enqueued' ) ) {
|
|
wp_enqueue_script( 'swi-foot-editor-blocks' );
|
|
}
|
|
|
|
// Enqueue editor styles globally to ensure block wrapper styling applies
|
|
wp_enqueue_style( 'swi-foot-editor-styles' );
|
|
|
|
wp_localize_script('swi-foot-editor-blocks', 'swiFootEditorData', array(
|
|
'rest_url' => esc_url_raw(rest_url('swi-foot/v1')),
|
|
'rest_nonce' => wp_create_nonce('wp_rest'),
|
|
));
|
|
}
|
|
|
|
|
|
|
|
private function get_teams_for_editor()
|
|
{
|
|
$api = new Swi_Foot_API();
|
|
$teams = $api->get_teams();
|
|
|
|
if (is_wp_error($teams) || empty($teams)) {
|
|
return array();
|
|
}
|
|
|
|
$team_options = array();
|
|
foreach ($teams as $team) {
|
|
$team_name = $team['teamName'] ?? '';
|
|
$league = $team['teamLeagueName'] ?? '';
|
|
|
|
// If league name is available, append in parentheses
|
|
if (!empty($league)) {
|
|
$team_name .= ' (' . $league . ')';
|
|
}
|
|
|
|
$team_options[] = array(
|
|
'value' => $team['teamId'] ?? '',
|
|
'label' => $team_name
|
|
);
|
|
}
|
|
|
|
return $team_options;
|
|
}
|
|
|
|
public function render_standings_block($attributes, $content = '', $block = null)
|
|
{
|
|
// Resolve context: block context (from provider) overrides post meta defaults
|
|
$provided = is_object($block) && !empty($block->context['swi-foot/context']) ? $block->context['swi-foot/context'] : null;
|
|
$ctx = array();
|
|
if (is_array($provided)) $ctx = $provided;
|
|
// Merge with post-level defaults (post meta -> plugin options)
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $ctx);
|
|
|
|
// Team must come from context only
|
|
$team_id = $ctx['team_id'] ?? null;
|
|
|
|
if (empty($team_id)) {
|
|
return '<div class="swi-foot-error">' . __('Standings: No team provided in container context.', 'swi_foot_matchdata') . '</div>';
|
|
}
|
|
|
|
$api = new Swi_Foot_API();
|
|
$standings = $api->get_standings($team_id);
|
|
|
|
if (is_wp_error($standings)) {
|
|
return '<div class="swi-foot-error">' .
|
|
sprintf(
|
|
/* translators: %s is the error message from the API */
|
|
__('Error loading standings: %s', 'swi_foot_matchdata'),
|
|
$standings->get_error_message()
|
|
) .
|
|
'</div>';
|
|
}
|
|
|
|
if (empty($standings)) {
|
|
return '<div class="swi-foot-notice">' . __('No standings data available.', 'swi_foot_matchdata') . '</div>';
|
|
}
|
|
|
|
ob_start();
|
|
?>
|
|
<div class="swi-foot-standings">
|
|
<h3><?php _e('Current Standings', 'swi_foot_matchdata'); ?></h3>
|
|
<table class="swi-foot-table">
|
|
<thead>
|
|
<tr>
|
|
<th><?php _e('Pos', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('Team', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('P', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('W', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('D', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('L', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('GF', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('GA', 'swi_foot_matchdata'); ?></th>
|
|
<th><?php _e('Pts', 'swi_foot_matchdata'); ?></th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($standings as $team): ?>
|
|
<tr<?php echo ($team['teamId'] == $team_id) ? ' class="highlight"' : ''; ?>>
|
|
<td><?php echo esc_html($team['position']); ?></td>
|
|
<td><?php echo esc_html($team['teamName']); ?></td>
|
|
<td><?php echo esc_html($team['matches']); ?></td>
|
|
<td><?php echo esc_html($team['wins']); ?></td>
|
|
<td><?php echo esc_html($team['draws']); ?></td>
|
|
<td><?php echo esc_html($team['losses']); ?></td>
|
|
<td><?php echo esc_html($team['goalsFor']); ?></td>
|
|
<td><?php echo esc_html($team['goalsAgainst']); ?></td>
|
|
<td><strong><?php echo esc_html($team['points']); ?></strong></td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
public function render_schedule_block($attributes, $content = '', $block = null)
|
|
{
|
|
$provided = is_object($block) && !empty($block->context['swi-foot/context']) ? $block->context['swi-foot/context'] : null;
|
|
$ctx = array();
|
|
if (is_array($provided)) $ctx = $provided;
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $ctx);
|
|
|
|
// Team must come from context only
|
|
$team_id = $ctx['team_id'] ?? null;
|
|
$limit = isset($attributes['limit']) ? $attributes['limit'] : 5;
|
|
$match_filter = isset($attributes['matchFilter']) ? $attributes['matchFilter'] : 'all';
|
|
|
|
if (empty($team_id)) {
|
|
return '<div class="swi-foot-error">' . __('Schedule: No team provided in container context.', 'swi_foot_matchdata') . '</div>';
|
|
}
|
|
|
|
$api = new Swi_Foot_API();
|
|
$events = $api->get_schedule($team_id);
|
|
|
|
if (is_wp_error($events)) {
|
|
return '<div class="swi-foot-error">' .
|
|
sprintf(
|
|
/* translators: %s is the error message from the API */
|
|
__('Error loading schedule: %s', 'swi_foot_matchdata'),
|
|
$events->get_error_message()
|
|
) .
|
|
'</div>';
|
|
}
|
|
|
|
// Filter upcoming events by match type (home/away/all) and limit results
|
|
$upcoming_events = array();
|
|
if (is_array($events)) {
|
|
foreach ($events as $event) {
|
|
// Check if match is in the future
|
|
if (strtotime($event['matchDate']) >= time()) {
|
|
// Apply match filter (home/away/all)
|
|
$match_should_be_included = true;
|
|
|
|
if ($match_filter !== 'all') {
|
|
// Determine if this is a home or away match
|
|
// teamAId is home team, teamBId is away team
|
|
$is_home_match = isset($event['teamAId']) && $event['teamAId'] == $team_id;
|
|
$is_away_match = isset($event['teamBId']) && $event['teamBId'] == $team_id;
|
|
|
|
// If team IDs aren't available, try to infer from team names
|
|
if (!isset($event['teamAId']) || !isset($event['teamBId'])) {
|
|
// This is a fallback - not ideal but works if IDs aren't present
|
|
$is_home_match = true; // Default to home
|
|
}
|
|
|
|
if ($match_filter === 'home' && !$is_home_match) {
|
|
$match_should_be_included = false;
|
|
} elseif ($match_filter === 'away' && !$is_away_match) {
|
|
$match_should_be_included = false;
|
|
}
|
|
}
|
|
|
|
if ($match_should_be_included) {
|
|
$upcoming_events[] = $event;
|
|
if (count($upcoming_events) >= $limit) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
ob_start();
|
|
?>
|
|
<div class="swi-foot-schedule">
|
|
<h3><?php _e('Upcoming Matches', 'swi_foot_matchdata'); ?></h3>
|
|
<?php if (empty($upcoming_events)): ?>
|
|
<p><?php _e('No upcoming matches scheduled.', 'swi_foot_matchdata'); ?></p>
|
|
<?php else: ?>
|
|
<div class="swi-foot-events">
|
|
<?php foreach ($upcoming_events as $event): ?>
|
|
<div class="swi-foot-event">
|
|
<div class="event-date">
|
|
<?php echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($event['matchDate'])); ?>
|
|
</div>
|
|
<div class="event-teams">
|
|
<?php echo esc_html($event['teamNameA'] ?? ''); ?> vs <?php echo esc_html($event['teamNameB'] ?? ''); ?>
|
|
</div>
|
|
<?php if (!empty($event['stadiumPlaygroundName'])): ?>
|
|
<div class="event-location"><?php echo esc_html($event['stadiumPlaygroundName']); ?></div>
|
|
<?php endif; ?>
|
|
<?php if (!empty($event['leagueName'])): ?>
|
|
<div class="event-league"><?php echo esc_html($event['leagueName']); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
public function render_shortcode_inserter_block($attributes, $content = '', $block = null)
|
|
{
|
|
$provided = is_object($block) && !empty($block->context['swi-foot/context']) ? $block->context['swi-foot/context'] : null;
|
|
$ctx = array();
|
|
if (is_array($provided)) $ctx = $provided;
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $ctx);
|
|
|
|
$shortcode_type = $attributes['shortcodeType'] ?? 'match';
|
|
$match_id = $attributes['matchId'] ?? ($ctx['match_id'] ?? '');
|
|
$team_id = $attributes['teamId'] ?? ($ctx['team_id'] ?? '');
|
|
$show_next = !empty($attributes['showNext']);
|
|
$format = $attributes['format'] ?? '';
|
|
$separator = $attributes['separator'] ?? ':';
|
|
|
|
// Build shortcode based on attributes
|
|
$shortcode_parts = array();
|
|
|
|
if (!empty($match_id)) {
|
|
$shortcode_parts[] = 'match_id="' . esc_attr($match_id) . '"';
|
|
}
|
|
|
|
if (!empty($team_id)) {
|
|
$shortcode_parts[] = 'team_id="' . esc_attr($team_id) . '"';
|
|
}
|
|
|
|
if ($show_next) {
|
|
$shortcode_parts[] = 'show_next="true"';
|
|
}
|
|
|
|
if (!empty($format) && in_array($shortcode_type, array('match_date', 'match_time'))) {
|
|
$shortcode_parts[] = 'format="' . esc_attr($format) . '"';
|
|
}
|
|
|
|
if (!empty($separator) && $separator !== ':' && $shortcode_type === 'match_score') {
|
|
$shortcode_parts[] = 'separator="' . esc_attr($separator) . '"';
|
|
}
|
|
|
|
$shortcode = '[swi_foot_' . $shortcode_type . (!empty($shortcode_parts) ? ' ' . implode(' ', $shortcode_parts) : '') . ']';
|
|
|
|
// Execute the shortcode and return inline wrapper so editors can insert it inside text
|
|
$rendered = do_shortcode($shortcode);
|
|
return '<span class="swi-foot-inline shortcode-inserter">' . $rendered . '</span>';
|
|
}
|
|
|
|
/**
|
|
* Render match roster block
|
|
* Gets match from container context, uses side attribute to determine which team to show
|
|
*/
|
|
public function render_match_roster_block($attributes, $content = '', $block = null)
|
|
{
|
|
// Get context from parent block container
|
|
$provided = is_object($block) && !empty($block->context['swi-foot/context']) ? $block->context['swi-foot/context'] : null;
|
|
$ctx = array();
|
|
if (is_array($provided)) $ctx = $provided;
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $ctx);
|
|
|
|
// Match must come from context
|
|
$match_id = $ctx['match_id'] ?? '';
|
|
$side = $attributes['side'] ?? 'home';
|
|
$show_starting_squad = isset($attributes['showStartingSquad']) ? $attributes['showStartingSquad'] : true;
|
|
$show_bench = isset($attributes['showBench']) ? $attributes['showBench'] : false;
|
|
|
|
if (empty($match_id)) {
|
|
return '<div class="swi-foot-error">' . __('Match Roster: No match provided in container context.', 'swi_foot_matchdata') . '</div>';
|
|
}
|
|
|
|
// Build shortcode using match from context and side attribute
|
|
$shortcode = '[swi_foot_roster match_id="' . esc_attr($match_id) . '" side="' . esc_attr($side) . '"';
|
|
if ($show_starting_squad) {
|
|
$shortcode .= ' show_starting="true"';
|
|
}
|
|
if ($show_bench) {
|
|
$shortcode .= ' show_bench="true"';
|
|
}
|
|
$shortcode .= ']';
|
|
|
|
// Process and return the shortcode
|
|
return do_shortcode($shortcode);
|
|
}
|
|
|
|
/**
|
|
* Render match events block
|
|
* Gets match from container context, displays live events for that match
|
|
*/
|
|
public function render_match_events_block($attributes, $content = '', $block = null)
|
|
{
|
|
// Get context from parent block container
|
|
$provided = is_object($block) && !empty($block->context['swi-foot/context']) ? $block->context['swi-foot/context'] : null;
|
|
$ctx = array();
|
|
if (is_array($provided)) $ctx = $provided;
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $ctx);
|
|
|
|
// Match must come from context
|
|
$match_id = $ctx['match_id'] ?? '';
|
|
$refresh_interval = $attributes['refreshInterval'] ?? 30;
|
|
$event_order = $attributes['eventOrder'] ?? 'dynamic';
|
|
|
|
if (empty($match_id)) {
|
|
return '<div class="swi-foot-error">' . __('Match Events: No match provided in container context.', 'swi_foot_matchdata') . '</div>';
|
|
}
|
|
|
|
// Build shortcode using match from context
|
|
$shortcode = '[swi_foot_events match_id="' . esc_attr($match_id) . '" refresh_interval="' . esc_attr($refresh_interval) . '" event_order="' . esc_attr($event_order) . '"]';
|
|
|
|
// Process and return the shortcode
|
|
return do_shortcode($shortcode);
|
|
}
|
|
|
|
/**
|
|
* Render match bench block
|
|
* Gets match from container context, displays team staff/substitutes
|
|
*/
|
|
public function render_match_bench_block($attributes, $content = '', $block = null)
|
|
{
|
|
// Get context from parent block container
|
|
$provided = is_object($block) && !empty($block->context['swi-foot/context']) ? $block->context['swi-foot/context'] : null;
|
|
$ctx = array();
|
|
if (is_array($provided)) $ctx = $provided;
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $ctx);
|
|
|
|
// Match must come from context
|
|
$match_id = $ctx['match_id'] ?? '';
|
|
$side = $attributes['side'] ?? 'home';
|
|
|
|
if (empty($match_id)) {
|
|
return '<div class="swi-foot-error">' . __('Match Bench: No match provided in container context.', 'swi_foot_matchdata') . '</div>';
|
|
}
|
|
|
|
// Build shortcode using match from context and side attribute
|
|
$shortcode = '[swi_foot_bench match_id="' . esc_attr($match_id) . '" side="' . esc_attr($side) . '"]';
|
|
|
|
// Process and return the shortcode
|
|
return do_shortcode($shortcode);
|
|
}
|
|
|
|
/**
|
|
* Render match referees block
|
|
* Gets match from container context, displays match officials/referees
|
|
*/
|
|
public function render_match_referees_block($attributes, $content = '', $block = null)
|
|
{
|
|
// Get context from parent block container
|
|
$provided = is_object($block) && !empty($block->context['swi-foot/context']) ? $block->context['swi-foot/context'] : null;
|
|
$ctx = array();
|
|
if (is_array($provided)) $ctx = $provided;
|
|
$post_defaults = function_exists('swi_foot_resolve_context') ? swi_foot_resolve_context(get_the_ID()) : array();
|
|
$ctx = array_merge($post_defaults, $ctx);
|
|
|
|
// Match must come from context
|
|
$match_id = $ctx['match_id'] ?? '';
|
|
|
|
if (empty($match_id)) {
|
|
return '<div class="swi-foot-error">' . __('Match Referees: No match provided in container context.', 'swi_foot_matchdata') . '</div>';
|
|
}
|
|
|
|
// Build shortcode using match from context
|
|
$shortcode = '[swi_foot_referees match_id="' . esc_attr($match_id) . '"]';
|
|
|
|
// Process and return the shortcode
|
|
return do_shortcode($shortcode);
|
|
}
|
|
|
|
private function render_team_selector($block_type)
|
|
{
|
|
$api = new Swi_Foot_API();
|
|
$teams = $api->get_teams();
|
|
|
|
if (is_wp_error($teams) || empty($teams)) {
|
|
return '<div class="swi-foot-error">' .
|
|
__('Please configure your API settings and ensure teams are available.', 'swi_foot_matchdata') .
|
|
'</div>';
|
|
}
|
|
|
|
ob_start();
|
|
?>
|
|
<div class="swi-foot-team-selector">
|
|
<p><?php printf(__('Please select a team to display %s:', 'swi_foot_matchdata'), $block_type); ?></p>
|
|
<select onchange="swiFootSelectTeam(this.value, '<?php echo esc_attr($block_type); ?>')">
|
|
<option value=""><?php _e('Select a team...', 'swi_foot_matchdata'); ?></option>
|
|
<?php foreach ($teams as $team): ?>
|
|
<option value="<?php echo esc_attr($team['teamId'] ?? ''); ?>">
|
|
<?php echo esc_html($team['teamName'] ?? 'Unknown Team'); ?>
|
|
</option>
|
|
<?php endforeach; ?>
|
|
</select>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
|
|
}
|
|
?>
|