Skip to content

Instantly share code, notes, and snippets.

View ViewFromTheBox's full-sized avatar

Jacob Martella ViewFromTheBox

View GitHub Profile
@ViewFromTheBox
ViewFromTheBox / WP REST API Controller Class
Created December 21, 2017 01:47
This is the base WP REST API Controller class
<?php class Sports_Bench_Team_REST_Controller extends WP_REST_Controller {
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
$namespace = 'sportsbench';
$base = 'teams';
register_rest_route( $namespace, '/' . $base, array(
array(
<template>
<li :id="'comment-' + comment.comment_ID" :class="'comment ' + comment-comment.comment_ID + ' ' + comment.comment_parent ? 'child-comment post-parent-' + comment.comment_parent : ''">
<header class="comment-header">
<h4 class="comment-name">{{comment.comment_author}}</h4>
<h5 class="comment-date">Posted at {{ comment.comment_date }} on {{ comment.comment_date }}</h5>
</header>
<div class="comment-content" v-html="comment.comment_content"></div>
<footer class="reply">
<button :id="'reply-' + comment.comment_ID" class="respond-to-comment button" v-on:click="addReply">Reply</button>
</footer>
arrangeComments: function ( commentsList ) {
var maxDepth = 0;
for ( var i = commentsList.length - 1; i >= 0; i-- ) {
if ( commentsList[i].comment_approved != 1 ) {
commentsList.splice( i, 1 );
}
}
for ( i = 0; i < commentsList.length; i += 1 ) {
commentsList[i].comment_children = [];
var date = commentsList[i].comment_date.split(" ").join("T").concat("Z");
getCommentDepth: function ( theComment, comments_list ) {
var depthLevel = 0;
while ( theComment.comment_parent > 0 ) {
theComment = this.getCommentById( theComment.comment_parent, comments_list );
depthLevel++;
}
return depthLevel;
}
getCommentById: function ( commentID, comments_list ) {
for ( var j = 0; j < comments_list.length; j++ ) {
if ( comments_list[j].comment_ID == commentID ) {
return comments_list[j];
}
}
}
add_action( 'rest_api_init', 'theme_slug_extend_rest_post_response' );
function theme_slug_extend_rest_post_response() {
register_rest_field( 'post',
'comments',
array(
'get_callback' => 'theme_slug_get_comments',
'update_callback' => null,
'schema' => null,
) );
}
public function get_team_stats() {
$game_id = $this->game_id;
global $wpdb;
$table = $wpdb->prefix . 'sb_games';
$querystr = "SELECT * FROM $table WHERE game_id = $game_id";
$game_info = $wpdb->get_results( $querystr );
$home_stats = array (
'shots' => $this->get_shots( 'home' ),
'sog' => $this->get_sog( 'home' ),
public function get_red_cards( $team = 'home' ) {
$game_id = $this->game_id;
global $wpdb;
$table = $wpdb->prefix . 'sb_games';
$querystr = "SELECT * FROM $table WHERE game_id = $game_id";
$game_info = $wpdb->get_results( $querystr );
if ( $team == 'away' ) {
return $game_info[0]->game_away_red;
} else {
public function get_stats( $season = false ) {
$stats = array(
'goals' => $this->get_goals( $season ),
'assists' => $this->get_assists( $season ),
'shots_on_goal' => $this->get_sog( $season ),
'fouls' => $this->get_gouls( $season ),
);
return $stats;
}
public function get_goals( $season = false ) {
if ( $season == false ) {
$where = '';
} else {
$where = ' AND game_season = "' . $season . '" ';
}
global $wpdb;
$player_table = $wpdb->prefix . 'sb_players';