Skip to content

Commit

Permalink
Releases Archive: Link releases to microsites (#345)
Browse files Browse the repository at this point in the history
* Link the version number column to the microsite if it exists

* Attempt to find microsite in child pages and fix escaping

* Check if $post is null
  • Loading branch information
adamwoodnz authored Nov 12, 2023
1 parent b5c0c6a commit 0ca1621
Showing 1 changed file with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,36 @@ function render_table( $releases ) {
return $table;
}

/**
* Find the microsite url for a given version.
*
* @param string $version The version number to find the microsite url for.
*
* @return string|null Returns the microsite url if found, otherwise null.
*/
function find_microsite_url_for_version( $version ) {
// attempt to find the microsite url only if the version number matches the format major.minor
if ( ! preg_match( '/^\d+\.\d+$/', $version ) ) {
return null;
}

global $post;
if ( ! $post ) {
return null;
}

$child_pages = get_pages( array( 'child_of' => $post->ID ) );
$version_hyphenated = str_replace( '.', '-', $version );

foreach ( $child_pages as $child_page ) {
if ( $version_hyphenated === $child_page->post_name ) {
return get_permalink( $child_page->ID );
}
}

return null;
}

/**
* Render a release row.
*
Expand All @@ -203,8 +233,19 @@ function render_table( $releases ) {
* @return string Returns the row markup.
*/
function render_table_row( $version ) {
$version_number = $version['version'];
$microsite_url = find_microsite_url_for_version( $version_number );

$row = '<tr>';
$row .= '<th class="wp-block-wporg-release-tables__cell-version" scope="row">' . esc_html( $version['version'] ) . '</th>';
$row .= '<th class="wp-block-wporg-release-tables__cell-version" scope="row">';
$row .= isset( $microsite_url )
? sprintf(
'<a href="%1$s">%2$s</a>',
esc_url( $microsite_url ),
esc_html( $version_number ),
)
: esc_html( $version_number );
$row .= '</th>';
$row .= '<td class="wp-block-wporg-release-tables__cell-date">' . esc_html( date_i18n( get_option( 'date_format' ), $version['builton'] ) ) . '</td>';
$row .= sprintf( '<td class="wp-block-wporg-release-tables__cell-zip"><a href="%1$s">zip</a><br /><small>(<a href="%1$s.md5">md5</a> &#183; <a href="%1$s.sha1">sha1</a>)</small></td>', esc_url( $version['zip_url'] ) );

Expand Down

0 comments on commit 0ca1621

Please sign in to comment.