Route single-projects template through provider interface

This commit is contained in:
Keith Solomon
2026-08-09 23:21:06 -05:00
parent d8ce719433
commit 9803b497ab
+25 -74
View File
@@ -12,73 +12,24 @@ if ( $is_fse_theme ) {
$settings = projects_portfolio_settings(); $settings = projects_portfolio_settings();
$project_id = get_the_ID(); $project_id = get_the_ID();
$download_count = (int) get_post_meta( $project_id, '_projects_portfolio_download_count', true ); $download_count = (int) get_post_meta( $project_id, '_projects_portfolio_download_count', true );
$github_url = get_post_meta( $project_id, '_projects_portfolio_github_url', true );
$version = 'Unknown';
$github_data = projects_portfolio_get_github_data( $github_url );
// Extract specific GitHub data. $repo_url = get_post_meta( $project_id, '_projects_portfolio_repo_url', true );
$owner_avatar = $github_data['owner']['avatar_url'] ?? ''; if ( '' === $repo_url ) {
$owner_name = $github_data['owner']['login'] ?? ''; $repo_url = get_post_meta( $project_id, '_projects_portfolio_github_url', true );
$owner_url = $github_data['owner']['html_url'] ?? '';
$last_updated = $github_data['updated_at'] ?? '';
$language = $github_data['language'] ?? '';
$license = $github_data['license']['name'] ?? 'None';
$owner = projects_portfolio_github_owner( $owner_name );
// Fetch the version number from the GitHub API if the URL is set.
if ( $github_url ) {
// Retrieve the GitHub API token from the plugin settings.
$github_token = get_option( 'projects_portfolio_github_api_token', '' );
// Prepare the API URL.
$api_url = str_replace( 'https://github.com/', 'https://api.github.com/repos/', rtrim( $github_url, '/' ) ) . '/releases/latest';
// Check if the response is cached.
$transient_key = 'github_api_response_' . md5( $api_url );
$response = get_transient( $transient_key );
if ( false === $response ) {
// Prepare the request arguments.
$args = [];
if ( ! empty( $github_token ) ) {
$args['headers'] = [
'Authorization' => 'token ' . $github_token,
];
} }
// Make the API request. $repo_data = projects_portfolio_get_repo_data( $project_id );
$response = wp_remote_get( $api_url, $args ); $version = projects_portfolio_get_version( $project_id );
$owner = projects_portfolio_get_owner( $project_id );
// Cache the response for 1 hour if the request is successful. $owner_avatar = $repo_data['owner']['avatar_url'] ?? '';
if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) { $owner_name = $repo_data['owner']['login'] ?? '';
set_transient( $transient_key, $response, HOUR_IN_SECONDS ); $owner_url = $repo_data['owner']['html_url'] ?? '';
} $last_updated = $repo_data['updated_at'] ?? '';
} $language = $repo_data['language'] ?? '';
$license = $repo_data['license']['name'] ?? 'None';
$browse_url = $repo_url ? projects_portfolio_get_repo_browse_url( $project_id ) : '';
// Handle the response.
if ( is_wp_error( $response ) ) {
error_log( 'GitHub API error: ' . $response->get_error_message() );
} else {
$response_code = wp_remote_retrieve_response_code( $response );
$response_body = wp_remote_retrieve_body( $response );
error_log( 'GitHub API response code: ' . $response_code );
error_log( 'GitHub API response body: ' . $response_body );
if ( $response_code === 200 ) {
$data = json_decode( $response_body, true );
// Check if tag_name exists in the response.
if ( isset( $data['tag_name'] ) ) {
$version = esc_html( $data['tag_name'] );
}
} else {
error_log( "GitHub API error: HTTP $response_code for URL $api_url" );
}
}
}
// Format last updated date.
if ( $last_updated ) { if ( $last_updated ) {
$last_updated = date_i18n( get_option( 'date_format' ), strtotime( $last_updated ) ); $last_updated = date_i18n( get_option( 'date_format' ), strtotime( $last_updated ) );
} }
@@ -117,12 +68,12 @@ if ( $last_updated ) {
<a href="<?php echo esc_url( site_url( '/download/' . $project_id ) ); ?>" class="button project-download-button"> <a href="<?php echo esc_url( site_url( '/download/' . $project_id ) ); ?>" class="button project-download-button">
<?php esc_html_e( 'Download Now', 'projects-wp' ); ?> <?php esc_html_e( 'Download Now', 'projects-wp' ); ?>
</a> </a>
<?php if ( $github_url ) : ?> <?php if ( $repo_url ) : ?>
<a href="<?php echo esc_url( $github_url ); ?>" class="button project-github-button" target="_blank" rel="noopener noreferrer"> <a href="<?php echo esc_url( $browse_url ); ?>" class="button project-github-button" target="_blank" rel="noopener noreferrer">
<?php esc_html_e( 'View on GitHub', 'projects-wp' ); ?> <?php esc_html_e( 'View Repo', 'projects-wp' ); ?>
</a> </a>
<?php else : ?> <?php else : ?>
<p><?php esc_html_e( 'GitHub Repository:', 'projects-wp' ); ?> <?php esc_html_e( 'No repository linked.', 'projects-wp' ); ?></p> <p><?php esc_html_e( 'Repository:', 'projects-wp' ); ?> <?php esc_html_e( 'No repository linked.', 'projects-wp' ); ?></p>
<?php endif; ?> <?php endif; ?>
</div> </div>
@@ -163,23 +114,23 @@ if ( $last_updated ) {
</tr> </tr>
<?php endif; ?> <?php endif; ?>
<?php <?php
if ( ! empty( $github_data ) ) : if ( ! empty( $repo_data ) ) :
if ( isset( $github_data['stargazers_count'] ) && $settings['templates']['stargazers_count'] ) : ?> if ( isset( $repo_data['stargazers_count'] ) && $settings['templates']['stargazers_count'] ) : ?>
<tr> <tr>
<th><?php esc_html_e( 'Stars:', 'projects-wp' ); ?></th> <th><?php esc_html_e( 'Stars:', 'projects-wp' ); ?></th>
<td><?php esc_html_e( $github_data['stargazers_count'] ); ?></td> <td><?php esc_html_e( $repo_data['stargazers_count'] ); ?></td>
</tr> </tr>
<?php endif; <?php endif;
if ( isset( $github_data['forks_count'] ) && $settings['templates']['forks'] ) : ?> if ( isset( $repo_data['forks_count'] ) && $settings['templates']['forks'] ) : ?>
<tr> <tr>
<th><?php esc_html_e( 'Forks:', 'projects-wp' ); ?></th> <th><?php esc_html_e( 'Forks:', 'projects-wp' ); ?></th>
<td><?php esc_html_e( $github_data['forks_count'] ); ?></td> <td><?php esc_html_e( $repo_data['forks_count'] ); ?></td>
</tr> </tr>
<?php endif; <?php endif;
if ( isset( $github_data['open_issues_count'] ) && $settings['templates']['open_issues_count'] ) : ?> if ( isset( $repo_data['open_issues_count'] ) && $settings['templates']['open_issues_count'] ) : ?>
<tr> <tr>
<th><?php esc_html_e( 'Issues:', 'projects-wp' ); ?></th> <th><?php esc_html_e( 'Issues:', 'projects-wp' ); ?></th>
<td><?php esc_html_e( $github_data['open_issues_count'] ); ?></td> <td><?php esc_html_e( $repo_data['open_issues_count'] ); ?></td>
</tr> </tr>
<?php endif; <?php endif;
endif; endif;
@@ -189,7 +140,7 @@ if ( $last_updated ) {
</div> </div>
<?php if ( $settings['templates']['github_owner'] && $owner_avatar && $owner_name ) : ?> <?php if ( $settings['templates']['github_owner'] && $owner_avatar && $owner_name ) : ?>
<a href="<?php esc_html_e( $github_url ); ?>"> <a href="<?php echo esc_url( $browse_url ); ?>">
<div class="project-owner"> <div class="project-owner">
<span> <span>
<img src="<?php echo esc_url( $owner_avatar ); ?>" alt="<?php esc_attr_e( $owner_name ); ?>" class="owner-avatar" style="width: 50px; height: 50px; border-radius: 50%;" /> <img src="<?php echo esc_url( $owner_avatar ); ?>" alt="<?php esc_attr_e( $owner_name ); ?>" class="owner-avatar" style="width: 50px; height: 50px; border-radius: 50%;" />