various improvements to make release 1 complete
This commit is contained in:
@@ -17,6 +17,23 @@ class Swi_Foot_Blocks
|
||||
|
||||
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';
|
||||
|
||||
@@ -90,14 +107,6 @@ class Swi_Foot_Blocks
|
||||
) );
|
||||
}
|
||||
|
||||
// Register additional blocks (editor-only save shortcodes)
|
||||
if ( file_exists( $base . '/team-data/block.json' ) ) {
|
||||
register_block_type_from_metadata( $base . '/team-data', array(
|
||||
'editor_script' => 'swi-foot-editor-blocks',
|
||||
'editor_style' => 'swi-foot-editor-styles'
|
||||
) );
|
||||
}
|
||||
|
||||
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' ),
|
||||
@@ -113,6 +122,22 @@ class Swi_Foot_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()
|
||||
@@ -223,7 +248,11 @@ class Swi_Foot_Blocks
|
||||
|
||||
if (is_wp_error($standings)) {
|
||||
return '<div class="swi-foot-error">' .
|
||||
sprintf(__('Error loading standings: %s', 'swi_foot_matchdata'), $standings->get_error_message()) .
|
||||
sprintf(
|
||||
/* translators: %s is the error message from the API */
|
||||
__('Error loading standings: %s', 'swi_foot_matchdata'),
|
||||
$standings->get_error_message()
|
||||
) .
|
||||
'</div>';
|
||||
}
|
||||
|
||||
@@ -281,6 +310,7 @@ class Swi_Foot_Blocks
|
||||
// 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>';
|
||||
@@ -291,18 +321,47 @@ class Swi_Foot_Blocks
|
||||
|
||||
if (is_wp_error($events)) {
|
||||
return '<div class="swi-foot-error">' .
|
||||
sprintf(__('Error loading schedule: %s', 'swi_foot_matchdata'), $events->get_error_message()) .
|
||||
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 and limit results
|
||||
// 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()) {
|
||||
$upcoming_events[] = $event;
|
||||
if (count($upcoming_events) >= $limit) {
|
||||
break;
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -400,7 +459,8 @@ class Swi_Foot_Blocks
|
||||
// Match must come from context
|
||||
$match_id = $ctx['match_id'] ?? '';
|
||||
$side = $attributes['side'] ?? 'home';
|
||||
$with_bench = $attributes['withBench'] ? 'true' : 'false';
|
||||
$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>';
|
||||
@@ -408,8 +468,11 @@ class Swi_Foot_Blocks
|
||||
|
||||
// Build shortcode using match from context and side attribute
|
||||
$shortcode = '[swi_foot_roster match_id="' . esc_attr($match_id) . '" side="' . esc_attr($side) . '"';
|
||||
if ($with_bench === 'true') {
|
||||
$shortcode .= ' with_bench="true"';
|
||||
if ($show_starting_squad) {
|
||||
$shortcode .= ' show_starting="true"';
|
||||
}
|
||||
if ($show_bench) {
|
||||
$shortcode .= ' show_bench="true"';
|
||||
}
|
||||
$shortcode .= ']';
|
||||
|
||||
@@ -446,6 +509,61 @@ class Swi_Foot_Blocks
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user