-
Notifications
You must be signed in to change notification settings - Fork 0
/
labs.php
87 lines (59 loc) · 2 KB
/
labs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace andyp\labsstack\REST;
use \WP_REST_Request;
class labs {
public $posts;
public $result;
public $endpoint = "https://parkourlabs.com/wp-json/wp/v2";
public function __construct()
{
$this->REST_call();
$this->render();
}
/**
* https://rudrastyh.com/wordpress/rest-api-get-posts.html
*/
private function REST_call()
{
$transient = \get_transient( 'labsstack' );
if( ! empty( $transient ) ) {
$this->posts = json_decode($transient);
return;
}
$response = \wp_remote_get( add_query_arg( array(
'per_page' => 10,
'labs_category' => 4
), $this->endpoint.'/tutorial?orderby=rand' ) );
if (is_wp_error($response)) {
return;
}
$this->posts = json_decode( $response['body'] ); // our posts are here
\set_transient( 'labsstack', json_encode( $this->posts ), DAY_IN_SECONDS );
}
public function render()
{
if (!isset($this->posts)){
return;
}
shuffle($this->posts);
$output = '<ul class="labs grid grid-cols-2 grid-rows-2 gap-4 iso-2">';
foreach($this->posts as $key => $post )
{
// 1 = 'one', 12 = 'twelve'
$f = new \NumberFormatter("en", \NumberFormatter::SPELLOUT);
$id = $f->format($key);
$zebra = ($key++%2==1) ? 'odd' : 'even';
$output .= '<li class="'.$id.' '.$zebra.' w-full h-40 bg-smoke">';
$output .= '<div class="w-full h-full mb-2">';
$output .= '<img class="lazyload object-cover h-full" src="'.$post->imageURL.'" alt="'.$post->title->rendered.'" width="'.$post->imageWidth.'" height="'.$post->imageHeight.'">';
$output .= '</div>';
$output .= '</li>';
}
$output .= '</ul>';
$this->result = $output;
}
public function out()
{
return $this->result;
}
}