138 lines
5.0 KiB
PHP
138 lines
5.0 KiB
PHP
<?php
|
|
|
|
// If this file is called directly, abort.
|
|
if ( ! defined( 'WPINC' ) ) {
|
|
die;
|
|
}
|
|
|
|
/**
|
|
* Callback function for the custom projects endpoint.
|
|
*
|
|
* @param WP_REST_Request $request The REST API request object.
|
|
*
|
|
* @since 1.0.0
|
|
* @return WP_REST_Response
|
|
*/
|
|
function projects_portfolio_get_projects_data( $request ) {
|
|
$args = [
|
|
'post_type' => 'projects',
|
|
'posts_per_page' => $request->get_param( 'per_page' ) ?? 10,
|
|
'paged' => $request->get_param( 'page' ) ?? 1,
|
|
];
|
|
|
|
$query = new WP_Query( $args );
|
|
$projects = [];
|
|
|
|
if ( $query->have_posts() ) {
|
|
while ( $query->have_posts() ) {
|
|
$query->the_post();
|
|
|
|
$project_id = get_the_ID();
|
|
$repo_url = get_post_meta( $project_id, '_projects_portfolio_repo_url', true );
|
|
if ( '' === $repo_url ) {
|
|
$repo_url = get_post_meta( $project_id, '_projects_portfolio_github_url', true );
|
|
}
|
|
$repo_data = projects_portfolio_get_repo_data( $project_id );
|
|
|
|
// Fetch project-type taxonomy terms (names).
|
|
$project_types = wp_get_post_terms( $project_id, 'project-type', [
|
|
'fields' => 'names',
|
|
] );
|
|
|
|
$projects[] = [
|
|
'id' => $project_id,
|
|
'title' => get_the_title(),
|
|
'excerpt' => get_the_excerpt(),
|
|
'permalink' => get_permalink(),
|
|
'thumbnail' => get_the_post_thumbnail_url( $project_id, 'large' ),
|
|
'download_count' => (int) get_post_meta( $project_id, '_projects_portfolio_download_count', true ),
|
|
'download_url' => esc_url( site_url( '/download/' . $project_id ) ),
|
|
'github_url' => $repo_url,
|
|
'provider' => get_post_meta( $project_id, '_projects_portfolio_provider', true ) ?: 'github',
|
|
'github_data' => [
|
|
'owner' => [
|
|
'avatar_url' => $repo_data['owner']['avatar_url'] ?? '',
|
|
'name' => $repo_data['owner']['login'] ?? '',
|
|
'profile' => $repo_data['owner']['html_url'] ?? '',
|
|
],
|
|
'last_updated' => ! empty( $repo_data['updated_at'] )
|
|
? date_i18n( get_option( 'date_format' ), strtotime( $repo_data['updated_at'] ) )
|
|
: '',
|
|
'language' => $repo_data['language'] ?? '',
|
|
'license' => $repo_data['license']['name'] ?? 'None',
|
|
'stars' => $repo_data['stargazers_count'] ?? 0,
|
|
'forks' => $repo_data['forks_count'] ?? 0,
|
|
'issues' => $repo_data['open_issues_count'] ?? 0,
|
|
],
|
|
'version' => projects_portfolio_get_version( $project_id ),
|
|
'project_types' => $project_types,
|
|
'content' => get_the_content(),
|
|
];
|
|
}
|
|
wp_reset_postdata();
|
|
}
|
|
|
|
$response = [
|
|
'projects' => $projects,
|
|
'total' => $query->found_posts,
|
|
'pages' => $query->max_num_pages,
|
|
];
|
|
|
|
return rest_ensure_response( $response );
|
|
}
|
|
|
|
/**
|
|
* Register REST API endpoint for popular projects.
|
|
*
|
|
* @since 1.0.0
|
|
* @return VolumeVolumeInfoDimensions
|
|
*/
|
|
function projects_portfolio_register_popular_endpoint() {
|
|
register_rest_route(
|
|
'projects/v1',
|
|
'/popular',
|
|
[
|
|
'methods' => 'GET',
|
|
'callback' => 'projects_portfolio_get_popular_projects',
|
|
'permission_callback' => '__return_true',
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Callback function for popular projects endpoint.
|
|
*
|
|
* @since 1.0.0
|
|
* @return void
|
|
*/
|
|
function projects_portfolio_get_popular_projects( $data ) {
|
|
$args = [
|
|
'post_type' => 'projects',
|
|
'posts_per_page' => $data->get_param( 'per_page' ) ?? 10,
|
|
'meta_key' => '_projects_portfolio_download_count',
|
|
'orderby' => 'meta_value_num',
|
|
'order' => 'DESC',
|
|
];
|
|
|
|
$query = new WP_Query( $args );
|
|
$projects = [];
|
|
|
|
if ( $query->have_posts() ) {
|
|
while ( $query->have_posts() ) {
|
|
$query->the_post();
|
|
$projects[] = [
|
|
'id' => get_the_ID(),
|
|
'title' => get_the_title(),
|
|
'download_count' => (int) get_post_meta( get_the_ID(), '_projects_portfolio_download_count', true ),
|
|
'download_url' => esc_url( site_url( '/download/' . get_the_ID() ) ),
|
|
'github_url' => ( $url = get_post_meta( get_the_ID(), '_projects_portfolio_repo_url', true ) ?: get_post_meta( get_the_ID(), '_projects_portfolio_github_url', true ) ),
|
|
'permalink' => get_permalink(),
|
|
];
|
|
}
|
|
}
|
|
|
|
wp_reset_postdata();
|
|
return rest_ensure_response( $projects );
|
|
}
|
|
add_action( 'rest_api_init', 'projects_portfolio_register_popular_endpoint' );
|