-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlogPosts.php
64 lines (49 loc) · 1.73 KB
/
BlogPosts.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
<?php
class BlogPosts {
/**
* Get posts from a remote WordPress API
*
* @param Int $page
* @param Int $limit
* @return array|bool
*/
public static function getPosts( Int $page, Int $limit ) {
global $wgBlogPostsConfig;
$data = [
'page' => $page ? $page : 1,
'per_page' => $limit ? $limit : $wgBlogPostsConfig['postsPerPage']
];
$data = http_build_query( $data );
$curl = curl_init();
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $curl, CURLOPT_URL, $wgBlogPostsConfig['blogURL'] . '&_embed&' . $data );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec( $curl );
$result = json_decode( $result, true );
if ( !$result || array_key_exists( 'code', $result ) ) {
return false;
}
return array_map( function( $post ) {
return [
'image' => html_entity_decode( $post['_embedded']['wp:featuredmedia'][0]['link'] ),
'title' => html_entity_decode( $post['title']['rendered'] ),
'url' => html_entity_decode( $post['link'] )
];
}, $result );
}
public static function createBlogPostsSection( $input, array $args, Parser $parser, PPFrame $frame ) {
global $wgBlogPostsConfig;
$parser->getOutput()->addModules( 'ext.BlogPosts' );
$templateParser = new TemplateParser( __DIR__ . '/templates' );
$initialPage = $wgBlogPostsConfig['initialPage'];
$postsPerPage = $wgBlogPostsConfig['postsPerPage'];
$data = self::getPosts( $initialPage, $postsPerPage );
$html = $templateParser->processTemplate( 'blog-posts', [
'titleText' => wfMessage( 'blog-posts-title' ),
'moreText' => wfMessage( 'blog-posts-more' ),
'loadingText' => wfMessage( 'blog-posts-loading' ),
'posts' => $data
] );
return [ $html, 'markerType' => 'nowiki' ];
}
}