-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcode.php
78 lines (69 loc) · 2.13 KB
/
shortcode.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
<?php
// Create Shortcode source-data
// Use the shortcode: [source-data source_id=""]
function create_sourcedata_shortcode($atts) {
// Attributes
$atts = shortcode_atts(
array(
'source_id' => '',
),
$atts,
'source-data'
);
// Attributes in var
$source_id = $atts['source_id'];
$source_data = get_source_data($source_id);
echo $source_data;
}
add_shortcode( 'source-data', 'create_sourcedata_shortcode' );
function get_source_data($source_id){
$source_url = get_post_meta($source_id, 'sourceurl_18744',true);
$source_data = get_transient('source_data'.$source_id);
if($source_data !== false){
return get_data_formated($source_data); // Format and return
}else{
$rest_data = get_data_from_rest($source_url); // fetch data
if($rest_data){
set_transient( 'source_data'.$source_id, $rest_data, 60*60*3 ); // save in transient
return get_data_formated($rest_data); // format and return
}else{
return "Unfortunately we could not fetch data for this Blog, Please Try Again.";
}
}
}
function get_data_from_rest($url){
$url = rtrim($url,"/");
$url = $url."/wp-json/wp/v2/posts";
$request = wp_remote_get($url);
if( is_wp_error( $request ) ) {
return false; // Bail early
}else{
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body );
if(!empty($data)){
foreach ($data as $blogpost) {
$source['title'] = $blogpost->title->rendered;
$source['excerpt'] = $blogpost->excerpt->rendered;
$source['link'] = $blogpost->link;
$source_data[]=$source;
}
}
return $source_data;
}
}
function get_data_formated($data){
$tmp_str = "";
foreach ($data as $blogpost){
$formated_post ="";
$post_link = $blogpost['link'];
$post_title = $blogpost['title'];
$link_alt = strip_tags($blogpost['excerpt']);
$excerpt = $blogpost['excerpt'];
// Just titles
$formated_post = '<h3><a href="'.$post_link.'" title="'.$link_alt.'" target="_blank">'.$post_title.'</a></h3>';
// Blog Format
//$formated_post = '<h3><a href="'.$post_link.'" title="'.$link_alt.'">'.$post_title.'</a></h3><br /><div class="excerpt">'.$excerpt.'</div>';
$tmp_str.=$formated_post;
}
return $tmp_str;
}