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' );
|
||||
|
||||
/**
|
||||
* 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 ) {
|
||||
if ( is_numeric( $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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ) {
|
||||
// See https://github.com/ThemeBoy/SportsPress/blob/770fa8c6654d7d6648791e877709c2428677635b/includes/admin/post-types/class-sp-admin-cpt-event.php#L99C40-L99C55
|
||||
if ( is_numeric( $post ) ) {
|
||||
$post = get_post( $post );
|
||||
}
|
||||
|
||||
if ( ! $post || $post->post_type !== 'sp_event' ) {
|
||||
return get_the_title();
|
||||
}
|
||||
@@ -58,147 +70,150 @@ function asc_generate_sp_event_title( $post ) {
|
||||
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 ) {
|
||||
$formatted_date = get_the_date('D n/j/y', $post);
|
||||
$formatted_date = get_the_date( 'D n/j/y', $post );
|
||||
|
||||
if (!$withTime){
|
||||
return $formatted_date;
|
||||
}
|
||||
if ( ! $withTime ) {
|
||||
return $formatted_date;
|
||||
}
|
||||
|
||||
if ( get_the_date('i', $post) == "00") {
|
||||
$formatted_time = get_the_date('gA', $post);
|
||||
} else {
|
||||
$formatted_time = get_the_date('g:iA', $post);
|
||||
}
|
||||
return $formatted_date . " " . $formatted_time ;
|
||||
if ( get_the_date( 'i', $post ) === '00' ) {
|
||||
$formatted_time = get_the_date( 'gA', $post );
|
||||
} else {
|
||||
$formatted_time = get_the_date( 'g:iA', $post );
|
||||
}
|
||||
|
||||
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() {
|
||||
if (is_single()) {
|
||||
global $post;
|
||||
if ($post->post_type === 'sp_event') {
|
||||
// Instantiate SP_Event object
|
||||
$event = new SP_Event($post->ID);
|
||||
if ( ! is_single() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fetch details using SP_Event methods
|
||||
$publish_date = get_the_date('F j, Y', $post);
|
||||
$venue_terms = get_the_terms($post->ID, 'sp_venue');
|
||||
$venue_name = $venue_terms ? $venue_terms[0]->name : 'Venue TBD';
|
||||
$results = $event->results(); // Using SP_Event method
|
||||
$title = asc_generate_sp_event_title($post);
|
||||
$sp_status = get_post_meta( $post->ID, 'sp_status', true );
|
||||
$status = $event->status(); // Using SP_Event method
|
||||
$publish_date_and_time = get_the_date('F j, Y g:i A', $post);
|
||||
$description = "{$publish_date_and_time} at {$venue_name}.";
|
||||
|
||||
if ( 'postponed' == $sp_status || 'cancelled' == $sp_status || 'tbd' == $sp_status) {
|
||||
$description = strtoupper($sp_status) . " — " . $description;
|
||||
$title = strtoupper($sp_status) . " — " . $title . " — " . asc_generate_short_date($post) . " — " . $venue_name;
|
||||
}
|
||||
global $post;
|
||||
|
||||
if ( 'future' == $status ) {
|
||||
$description = $description;
|
||||
$title = $title . " — " . asc_generate_short_date($post) . " — " . $venue_name;
|
||||
}
|
||||
if ( ! $post || $post->post_type !== 'sp_event' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( 'results' == $status ) { // checks if there is a final score
|
||||
// Get event result data
|
||||
$data = $event->results();
|
||||
$event = new SP_Event( $post->ID );
|
||||
$venue_terms = get_the_terms( $post->ID, 'sp_venue' );
|
||||
$venue_name = ( $venue_terms && ! is_wp_error( $venue_terms ) ) ? $venue_terms[0]->name : 'Venue TBD';
|
||||
$title = asc_generate_sp_event_title( $post );
|
||||
$sp_status = get_post_meta( $post->ID, 'sp_status', true );
|
||||
$status = $event->status();
|
||||
$publish_date_and_time = get_the_date( 'F j, Y g:i A', $post );
|
||||
$description = "{$publish_date_and_time} at {$venue_name}.";
|
||||
|
||||
// The first row should be column labels
|
||||
$labels = $data[0];
|
||||
if ( in_array( $sp_status, array( 'postponed', 'cancelled', 'tbd' ), true ) ) {
|
||||
$label = strtoupper( $sp_status );
|
||||
$description = "{$label} — {$description}";
|
||||
$title = "{$label} — {$title} — " . asc_generate_short_date( $post ) . " — {$venue_name}";
|
||||
} elseif ( 'future' === $status ) {
|
||||
$title = $title . ' — ' . asc_generate_short_date( $post ) . ' — ' . $venue_name;
|
||||
} elseif ( 'results' === $status ) {
|
||||
$data = $event->results();
|
||||
|
||||
// Remove the first row to leave us with the actual data
|
||||
unset( $data[0] );
|
||||
// First row is column labels; remove it.
|
||||
unset( $data[0] );
|
||||
$data = array_filter( $data );
|
||||
|
||||
$data = array_filter( $data );
|
||||
if ( ! empty( $data ) ) {
|
||||
if ( get_option( 'sportspress_event_reverse_teams', 'no' ) === 'yes' ) {
|
||||
$data = array_reverse( $data, true );
|
||||
}
|
||||
|
||||
if ( empty( $data ) ) {
|
||||
return false;
|
||||
}
|
||||
$teams_result_array = array();
|
||||
|
||||
// Initialize
|
||||
$i = 0;
|
||||
$result_string = '';
|
||||
$title_string = '';
|
||||
foreach ( $data as $team_id => $result ) {
|
||||
$result_outcome = sp_array_value( $result, 'outcome' );
|
||||
$the_outcome = null;
|
||||
|
||||
// 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 );
|
||||
}
|
||||
if ( is_array( $result_outcome ) ) {
|
||||
foreach ( $result_outcome as $outcome_slug ) {
|
||||
$found = get_page_by_path( $outcome_slug, OBJECT, 'sp_outcome' );
|
||||
if ( is_object( $found ) ) {
|
||||
$the_outcome = $found;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$teams_result_array = [];
|
||||
unset( $result['outcome'] );
|
||||
|
||||
foreach ( $data as $team_id => $result ) :
|
||||
$outcomes = array();
|
||||
$result_outcome = sp_array_value( $result, 'outcome' );
|
||||
if ( ! is_array( $result_outcome ) ) :
|
||||
$outcomes = array( '—' );
|
||||
else :
|
||||
foreach ( $result_outcome as $outcome ) :
|
||||
$the_outcome = get_page_by_path( $outcome, OBJECT, 'sp_outcome' );
|
||||
if ( is_object( $the_outcome ) ) :
|
||||
$outcomes[] = $the_outcome->post_title;
|
||||
endif;
|
||||
endforeach;
|
||||
endif;
|
||||
|
||||
unset( $result['outcome'] );
|
||||
|
||||
$team_name = sp_team_short_name( $team_id );
|
||||
$team_abbreviation = sp_team_abbreviation( $team_id );
|
||||
$outcome_title = $the_outcome ? $the_outcome->post_title : '';
|
||||
$outcome_abbreviation = '';
|
||||
if ( $the_outcome ) {
|
||||
$outcome_abbreviation = get_post_meta( $the_outcome->ID, 'sp_abbreviation', true );
|
||||
if ( ! $outcome_abbreviation ) {
|
||||
$outcome_abbreviation = sp_substr( $the_outcome->post_title, 0, 1 );
|
||||
}
|
||||
}
|
||||
|
||||
$outcome_abbreviation = get_post_meta( $the_outcome->ID, 'sp_abbreviation', true );
|
||||
if ( ! $outcome_abbreviation ) {
|
||||
$outcome_abbreviation = sp_substr( $the_outcome->post_title, 0, 1 );
|
||||
}
|
||||
|
||||
array_push($teams_result_array, [
|
||||
"result" => $result,
|
||||
"outcome" => $the_outcome->post_title,
|
||||
"outcome_abbreviation" => $outcome_abbreviation,
|
||||
"team_name" => $team_name,
|
||||
"team_abbreviation" => $team_abbreviation
|
||||
]
|
||||
);
|
||||
$i++;
|
||||
endforeach;
|
||||
$publish_date = asc_generate_short_date($post, false);
|
||||
$teams_result_array[] = array(
|
||||
'result' => $result,
|
||||
'outcome' => $outcome_title,
|
||||
'outcome_abbreviation' => $outcome_abbreviation,
|
||||
'team_name' => sp_team_short_name( $team_id ),
|
||||
'team_abbreviation' => sp_team_abbreviation( $team_id ),
|
||||
);
|
||||
}
|
||||
|
||||
$special_result_suffix_abbreviation = '';
|
||||
$special_result_suffix= '';
|
||||
if ( count( $teams_result_array ) >= 2 ) {
|
||||
$special_abbreviation = '';
|
||||
$special_label = '';
|
||||
|
||||
foreach ( $teams_result_array as $team ) {
|
||||
$outcome_abbreviation = strtoupper( $team['outcome_abbreviation'] ); // Normalize case
|
||||
foreach ( $teams_result_array as $team ) {
|
||||
$abbr = strtoupper( $team['outcome_abbreviation'] );
|
||||
|
||||
if ( $outcome_abbreviation === 'TF-W' ) {
|
||||
$special_result_suffix_abbreviation = 'TF-W';
|
||||
$special_result_suffix = 'Technical Forfeit Win';
|
||||
break;
|
||||
} elseif ( $outcome_abbreviation === 'TF-L' ) {
|
||||
$special_result_suffix_abbreviation = 'TF';
|
||||
$special_result_suffix = 'Technical Forfeit';
|
||||
break;
|
||||
} elseif ( $outcome_abbreviation === 'F-W' || $outcome_abbreviation === 'F-L' ) {
|
||||
$special_result_suffix_abbreviation = 'Forfeit';
|
||||
$special_result_suffix = 'Forfeit';
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( 'TF-W' === $abbr ) {
|
||||
$special_abbreviation = 'TF-W';
|
||||
$special_label = 'Technical Forfeit Win';
|
||||
break;
|
||||
} elseif ( 'TF-L' === $abbr ) {
|
||||
$special_abbreviation = 'TF';
|
||||
$special_label = 'Technical Forfeit';
|
||||
break;
|
||||
} elseif ( 'F-W' === $abbr || 'F-L' === $abbr ) {
|
||||
$special_abbreviation = 'Forfeit';
|
||||
$special_label = 'Forfeit';
|
||||
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})" : "");
|
||||
$description .= " " . "{$teams_result_array[0]['team_name']} ({$teams_result_array[0]['outcome']}), {$teams_result_array[1]['team_name']} ({$teams_result_array[1]['outcome']})." ;
|
||||
}
|
||||
$description .= " " . $post->post_content;
|
||||
$image = asc_sp_event_matchup_image_url( $post );
|
||||
echo '<meta property="og:type" content="article" />' . "\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:description" content="' . esc_attr( $description ) . '" />' . "\n";
|
||||
echo '<meta property="og:url" content="' . esc_url( get_permalink() ) . '" />' . "\n";
|
||||
}
|
||||
}
|
||||
$short_date = asc_generate_short_date( $post, false );
|
||||
$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;
|
||||
$image = asc_sp_event_matchup_image_url( $post );
|
||||
|
||||
echo '<meta property="og:type" content="article" />' . "\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:description" content="' . esc_attr( $description ) . '" />' . "\n";
|
||||
echo '<meta property="og:url" content="' . esc_url( get_permalink() ) . '" />' . "\n";
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user