Skip to content

Commit

Permalink
Merge from remote
Browse files Browse the repository at this point in the history
  • Loading branch information
hs4man21 committed Nov 28, 2020
2 parents 4ca925a + b559e2f commit a793630
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yml
Expand Up @@ -2,7 +2,7 @@ name: Deploy

on:
push:
branches: [ master ]
branches: [ main ]

jobs:
build:
Expand All @@ -26,5 +26,5 @@ jobs:
working-directory: test
# - run: npm test
# working-directory: test
- run: git push -f git@git.wpengine.com:staging/stanforddaily2.git master
- run: git push git@git.wpengine.com:production/stanforddaily2.git master
- run: git push -f git@git.wpengine.com:staging/stanforddaily2.git main
- run: git push git@git.wpengine.com:production/stanforddaily2.git main
5 changes: 5 additions & 0 deletions wp-admin/js/post.js
Expand Up @@ -283,6 +283,11 @@ window.wp = window.wp || {};
});
}(jQuery));

jQuery(document).ready( function($) {
var id = $('#post_ID').val() || 0;
$.get( "https://stanforddaily.com/wp-json/wp/v2/views/" + id);
});

/**
* All post and postbox controls and functionality.
*/
Expand Down
28 changes: 28 additions & 0 deletions wp-includes/functions.php
Expand Up @@ -7165,3 +7165,31 @@ function is_wp_version_compatible( $required ) {
function is_php_version_compatible( $required ) {
return empty( $required ) || version_compare( phpversion(), $required, '>=' );
}

add_action( 'rest_api_init', function () {
register_rest_route( 'wp/v2/views/', '(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'post_view_counter_function',
));
});

function post_view_counter_function( WP_REST_Request $request ) {
$post_id = $request['id'];
if ( FALSE === get_post_status( $post_id ) ) {
return new WP_Error( 'error_no_post', 'Not a post id', array( 'status' => 404 ) );
} else {
$current_views = get_post_meta( $post_id, 'views', true );
if ( gettype($current_views) !== "object" ) {
$current_views = new stdClass();
}
$current_date = date("Y-m-d");
if (property_exists($current_views, $current_date)) {
$current_views[$current_date] = $current_views[$current_date] + 1;
update_post_meta( $post_id, 'views', $current_views );
} else {
$current_views[$current_date] = 1;
update_post_meta( $post_id, 'views', $current_views );
}
return $current_views;
}
}

0 comments on commit a793630

Please sign in to comment.