-
Notifications
You must be signed in to change notification settings - Fork 2
/
shortcode-statistics.php
51 lines (41 loc) · 1.54 KB
/
shortcode-statistics.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
<?php
/**
* Shortcode to echo statistics information
*
* Usage: [statistics attribute="groups"]
*/
namespace WechangeCollection;
function create_statistics_shortcode($atts) {
$atts = shortcode_atts(
array(
'attribute' => '',
),
$atts,
'wechange-statistics'
);
$attribute = $atts['attribute'];
if ( empty( $attribute ) ) {
return '<span style="color: red;">' . __('Please define attribute to return statistic', 'wechange-collection') . '</span>';
}
if ( false === ( $statistics_output = get_transient( 'wechance-collection-statistic-' . $attribute ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$scope = '';
if ( defined( 'WECHANGE_SCOPE' ) && ! empty( trim( 'WECHANGE_SCOPE' ) ) ) {
$scope = 'managed_tag/' . trim( WECHANGE_SCOPE ) . '/';
}
$api_url = trailingslashit( WECHANGE_BASE_URL ). 'api/v1/statistics/general/' . $scope;
$api_response = file_get_contents( $api_url );
$statistic = json_decode($api_response);
$statistics_output = $statistic->{$atts["attribute"]};
if ( ! isset( $statistics_output ) ) {
$statistics_output = '<span style="color: red;">' . __('Please check attribute. Attribute doesn\'t seem to exist', 'wechange-collection') . '</span>';
}
set_transient(
'wechance-collection-statistic-' . $attribute,
$statistics_output,
apply_filters( 'wechange_collection_cache_time_statistics', wechange_collection_cache_time() )
);
}
return $statistics_output;
}
add_shortcode( 'wechange-statistics', __NAMESPACE__ . '\create_statistics_shortcode' );