Refactor open-graph-tags.php for correctness and standards compliance
- Early-return instead of deeply nested if blocks - Fix $the_outcome used outside the foreach loop (undefined variable risk) - Remove unused variables ($results, $publish_date, $i, $result_string, $title_string, $labels) - Remove no-op $description = $description assignment - Normalize indentation to tabs throughout - Use strict comparison (===) consistently - Guard $venue_terms with is_wp_error() check Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -13,6 +13,12 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||||||
|
|
||||||
add_action( 'wp_head', 'custom_open_graph_tags_with_sportspress_integration' );
|
add_action( 'wp_head', 'custom_open_graph_tags_with_sportspress_integration' );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the head-to-head matchup image URL for an event.
|
||||||
|
*
|
||||||
|
* @param int|WP_Post $post Post ID or object.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
function asc_sp_event_matchup_image_url( $post ) {
|
function asc_sp_event_matchup_image_url( $post ) {
|
||||||
if ( is_numeric( $post ) ) {
|
if ( is_numeric( $post ) ) {
|
||||||
$post = get_post( $post );
|
$post = get_post( $post );
|
||||||
@@ -25,11 +31,17 @@ function asc_sp_event_matchup_image_url( $post ) {
|
|||||||
return get_site_url() . '/head-to-head?post=' . $post->ID;
|
return get_site_url() . '/head-to-head?post=' . $post->ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a display title for an sp_event using team short names.
|
||||||
|
*
|
||||||
|
* @param int|WP_Post $post Post ID or object.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
function asc_generate_sp_event_title( $post ) {
|
function asc_generate_sp_event_title( $post ) {
|
||||||
// See https://github.com/ThemeBoy/SportsPress/blob/770fa8c6654d7d6648791e877709c2428677635b/includes/admin/post-types/class-sp-admin-cpt-event.php#L99C40-L99C55
|
|
||||||
if ( is_numeric( $post ) ) {
|
if ( is_numeric( $post ) ) {
|
||||||
$post = get_post( $post );
|
$post = get_post( $post );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! $post || $post->post_type !== 'sp_event' ) {
|
if ( ! $post || $post->post_type !== 'sp_event' ) {
|
||||||
return get_the_title();
|
return get_the_title();
|
||||||
}
|
}
|
||||||
@@ -58,6 +70,13 @@ function asc_generate_sp_event_title( $post ) {
|
|||||||
return implode( $delimiter, $team_names );
|
return implode( $delimiter, $team_names );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format a short date string for an event.
|
||||||
|
*
|
||||||
|
* @param int|WP_Post $post Post ID or object.
|
||||||
|
* @param bool $withTime Whether to include the time.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
function asc_generate_short_date( $post, $withTime = true ) {
|
function asc_generate_short_date( $post, $withTime = true ) {
|
||||||
$formatted_date = get_the_date( 'D n/j/y', $post );
|
$formatted_date = get_the_date( 'D n/j/y', $post );
|
||||||
|
|
||||||
@@ -65,140 +84,136 @@ function asc_generate_short_date( $post, $withTime = true ) {
|
|||||||
return $formatted_date;
|
return $formatted_date;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( get_the_date('i', $post) == "00") {
|
if ( get_the_date( 'i', $post ) === '00' ) {
|
||||||
$formatted_time = get_the_date( 'gA', $post );
|
$formatted_time = get_the_date( 'gA', $post );
|
||||||
} else {
|
} else {
|
||||||
$formatted_time = get_the_date( 'g:iA', $post );
|
$formatted_time = get_the_date( 'g:iA', $post );
|
||||||
}
|
}
|
||||||
return $formatted_date . " " . $formatted_time ;
|
|
||||||
|
|
||||||
|
return $formatted_date . ' ' . $formatted_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output Open Graph meta tags for sp_event single pages.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
function custom_open_graph_tags_with_sportspress_integration() {
|
function custom_open_graph_tags_with_sportspress_integration() {
|
||||||
if (is_single()) {
|
if ( ! is_single() ) {
|
||||||
global $post;
|
return;
|
||||||
if ($post->post_type === 'sp_event') {
|
}
|
||||||
// Instantiate SP_Event object
|
|
||||||
$event = new SP_Event($post->ID);
|
|
||||||
|
|
||||||
// Fetch details using SP_Event methods
|
global $post;
|
||||||
$publish_date = get_the_date('F j, Y', $post);
|
|
||||||
|
if ( ! $post || $post->post_type !== 'sp_event' ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$event = new SP_Event( $post->ID );
|
||||||
$venue_terms = get_the_terms( $post->ID, 'sp_venue' );
|
$venue_terms = get_the_terms( $post->ID, 'sp_venue' );
|
||||||
$venue_name = $venue_terms ? $venue_terms[0]->name : 'Venue TBD';
|
$venue_name = ( $venue_terms && ! is_wp_error( $venue_terms ) ) ? $venue_terms[0]->name : 'Venue TBD';
|
||||||
$results = $event->results(); // Using SP_Event method
|
|
||||||
$title = asc_generate_sp_event_title( $post );
|
$title = asc_generate_sp_event_title( $post );
|
||||||
$sp_status = get_post_meta( $post->ID, 'sp_status', true );
|
$sp_status = get_post_meta( $post->ID, 'sp_status', true );
|
||||||
$status = $event->status(); // Using SP_Event method
|
$status = $event->status();
|
||||||
$publish_date_and_time = get_the_date( 'F j, Y g:i A', $post );
|
$publish_date_and_time = get_the_date( 'F j, Y g:i A', $post );
|
||||||
$description = "{$publish_date_and_time} at {$venue_name}.";
|
$description = "{$publish_date_and_time} at {$venue_name}.";
|
||||||
|
|
||||||
if ( 'postponed' == $sp_status || 'cancelled' == $sp_status || 'tbd' == $sp_status) {
|
if ( in_array( $sp_status, array( 'postponed', 'cancelled', 'tbd' ), true ) ) {
|
||||||
$description = strtoupper($sp_status) . " — " . $description;
|
$label = strtoupper( $sp_status );
|
||||||
$title = strtoupper($sp_status) . " — " . $title . " — " . asc_generate_short_date($post) . " — " . $venue_name;
|
$description = "{$label} — {$description}";
|
||||||
}
|
$title = "{$label} — {$title} — " . asc_generate_short_date( $post ) . " — {$venue_name}";
|
||||||
|
} elseif ( 'future' === $status ) {
|
||||||
if ( 'future' == $status ) {
|
$title = $title . ' — ' . asc_generate_short_date( $post ) . ' — ' . $venue_name;
|
||||||
$description = $description;
|
} elseif ( 'results' === $status ) {
|
||||||
$title = $title . " — " . asc_generate_short_date($post) . " — " . $venue_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( 'results' == $status ) { // checks if there is a final score
|
|
||||||
// Get event result data
|
|
||||||
$data = $event->results();
|
$data = $event->results();
|
||||||
|
|
||||||
// The first row should be column labels
|
// First row is column labels; remove it.
|
||||||
$labels = $data[0];
|
|
||||||
|
|
||||||
// Remove the first row to leave us with the actual data
|
|
||||||
unset( $data[0] );
|
unset( $data[0] );
|
||||||
|
|
||||||
$data = array_filter( $data );
|
$data = array_filter( $data );
|
||||||
|
|
||||||
if ( empty( $data ) ) {
|
if ( ! empty( $data ) ) {
|
||||||
return false;
|
if ( get_option( 'sportspress_event_reverse_teams', 'no' ) === 'yes' ) {
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize
|
|
||||||
$i = 0;
|
|
||||||
$result_string = '';
|
|
||||||
$title_string = '';
|
|
||||||
|
|
||||||
// Reverse teams order if the option "Events > Teams > Order > Reverse order" is enabled.
|
|
||||||
$reverse_teams = get_option( 'sportspress_event_reverse_teams', 'no' ) === 'yes' ? true : false;
|
|
||||||
if ( $reverse_teams ) {
|
|
||||||
$data = array_reverse( $data, true );
|
$data = array_reverse( $data, true );
|
||||||
}
|
}
|
||||||
|
|
||||||
$teams_result_array = [];
|
$teams_result_array = array();
|
||||||
|
|
||||||
foreach ( $data as $team_id => $result ) :
|
foreach ( $data as $team_id => $result ) {
|
||||||
$outcomes = array();
|
|
||||||
$result_outcome = sp_array_value( $result, 'outcome' );
|
$result_outcome = sp_array_value( $result, 'outcome' );
|
||||||
if ( ! is_array( $result_outcome ) ) :
|
$the_outcome = null;
|
||||||
$outcomes = array( '—' );
|
|
||||||
else :
|
if ( is_array( $result_outcome ) ) {
|
||||||
foreach ( $result_outcome as $outcome ) :
|
foreach ( $result_outcome as $outcome_slug ) {
|
||||||
$the_outcome = get_page_by_path( $outcome, OBJECT, 'sp_outcome' );
|
$found = get_page_by_path( $outcome_slug, OBJECT, 'sp_outcome' );
|
||||||
if ( is_object( $the_outcome ) ) :
|
if ( is_object( $found ) ) {
|
||||||
$outcomes[] = $the_outcome->post_title;
|
$the_outcome = $found;
|
||||||
endif;
|
break;
|
||||||
endforeach;
|
}
|
||||||
endif;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
unset( $result['outcome'] );
|
unset( $result['outcome'] );
|
||||||
|
|
||||||
$team_name = sp_team_short_name( $team_id );
|
$outcome_title = $the_outcome ? $the_outcome->post_title : '';
|
||||||
$team_abbreviation = sp_team_abbreviation( $team_id );
|
$outcome_abbreviation = '';
|
||||||
|
if ( $the_outcome ) {
|
||||||
$outcome_abbreviation = get_post_meta( $the_outcome->ID, 'sp_abbreviation', true );
|
$outcome_abbreviation = get_post_meta( $the_outcome->ID, 'sp_abbreviation', true );
|
||||||
if ( ! $outcome_abbreviation ) {
|
if ( ! $outcome_abbreviation ) {
|
||||||
$outcome_abbreviation = sp_substr( $the_outcome->post_title, 0, 1 );
|
$outcome_abbreviation = sp_substr( $the_outcome->post_title, 0, 1 );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
array_push($teams_result_array, [
|
$teams_result_array[] = array(
|
||||||
"result" => $result,
|
'result' => $result,
|
||||||
"outcome" => $the_outcome->post_title,
|
'outcome' => $outcome_title,
|
||||||
"outcome_abbreviation" => $outcome_abbreviation,
|
'outcome_abbreviation' => $outcome_abbreviation,
|
||||||
"team_name" => $team_name,
|
'team_name' => sp_team_short_name( $team_id ),
|
||||||
"team_abbreviation" => $team_abbreviation
|
'team_abbreviation' => sp_team_abbreviation( $team_id ),
|
||||||
]
|
|
||||||
);
|
);
|
||||||
$i++;
|
}
|
||||||
endforeach;
|
|
||||||
$publish_date = asc_generate_short_date($post, false);
|
|
||||||
|
|
||||||
$special_result_suffix_abbreviation = '';
|
if ( count( $teams_result_array ) >= 2 ) {
|
||||||
$special_result_suffix= '';
|
$special_abbreviation = '';
|
||||||
|
$special_label = '';
|
||||||
|
|
||||||
foreach ( $teams_result_array as $team ) {
|
foreach ( $teams_result_array as $team ) {
|
||||||
$outcome_abbreviation = strtoupper( $team['outcome_abbreviation'] ); // Normalize case
|
$abbr = strtoupper( $team['outcome_abbreviation'] );
|
||||||
|
|
||||||
if ( $outcome_abbreviation === 'TF-W' ) {
|
if ( 'TF-W' === $abbr ) {
|
||||||
$special_result_suffix_abbreviation = 'TF-W';
|
$special_abbreviation = 'TF-W';
|
||||||
$special_result_suffix = 'Technical Forfeit Win';
|
$special_label = 'Technical Forfeit Win';
|
||||||
break;
|
break;
|
||||||
} elseif ( $outcome_abbreviation === 'TF-L' ) {
|
} elseif ( 'TF-L' === $abbr ) {
|
||||||
$special_result_suffix_abbreviation = 'TF';
|
$special_abbreviation = 'TF';
|
||||||
$special_result_suffix = 'Technical Forfeit';
|
$special_label = 'Technical Forfeit';
|
||||||
break;
|
break;
|
||||||
} elseif ( $outcome_abbreviation === 'F-W' || $outcome_abbreviation === 'F-L' ) {
|
} elseif ( 'F-W' === $abbr || 'F-L' === $abbr ) {
|
||||||
$special_result_suffix_abbreviation = 'Forfeit';
|
$special_abbreviation = 'Forfeit';
|
||||||
$special_result_suffix = 'Forfeit';
|
$special_label = 'Forfeit';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$title = "{$teams_result_array[0]['team_name']} {$teams_result_array[0]['result']['r']}-{$teams_result_array[1]['result']['r']} {$teams_result_array[1]['team_name']} — {$publish_date}" . ($special_result_suffix ? "({$special_result_suffix_abbreviation})" : "");
|
$short_date = asc_generate_short_date( $post, false );
|
||||||
$description .= " " . "{$teams_result_array[0]['team_name']} ({$teams_result_array[0]['outcome']}), {$teams_result_array[1]['team_name']} ({$teams_result_array[1]['outcome']})." ;
|
$t0 = $teams_result_array[0];
|
||||||
|
$t1 = $teams_result_array[1];
|
||||||
|
$score = isset( $t0['result']['r'] ) && isset( $t1['result']['r'] )
|
||||||
|
? "{$t0['result']['r']}-{$t1['result']['r']}"
|
||||||
|
: '';
|
||||||
|
$suffix = $special_label ? " ({$special_abbreviation})" : '';
|
||||||
|
|
||||||
|
$title = "{$t0['team_name']} {$score} {$t1['team_name']} — {$short_date}{$suffix}";
|
||||||
|
$description .= " {$t0['team_name']} ({$t0['outcome']}), {$t1['team_name']} ({$t1['outcome']}).";
|
||||||
}
|
}
|
||||||
$description .= " " . $post->post_content;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$description .= ' ' . $post->post_content;
|
||||||
$image = asc_sp_event_matchup_image_url( $post );
|
$image = asc_sp_event_matchup_image_url( $post );
|
||||||
|
|
||||||
echo '<meta property="og:type" content="article" />' . "\n";
|
echo '<meta property="og:type" content="article" />' . "\n";
|
||||||
echo '<meta property="og:image" content="' . esc_url( $image ) . '" />' . "\n";
|
echo '<meta property="og:image" content="' . esc_url( $image ) . '" />' . "\n";
|
||||||
echo '<meta property="og:title" content="' . esc_attr( $title ) . '" />' . "\n";
|
echo '<meta property="og:title" content="' . esc_attr( $title ) . '" />' . "\n";
|
||||||
echo '<meta property="og:description" content="' . esc_attr( $description ) . '" />' . "\n";
|
echo '<meta property="og:description" content="' . esc_attr( $description ) . '" />' . "\n";
|
||||||
echo '<meta property="og:url" content="' . esc_url( get_permalink() ) . '" />' . "\n";
|
echo '<meta property="og:url" content="' . esc_url( get_permalink() ) . '" />' . "\n";
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
|
|||||||
Reference in New Issue
Block a user