-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShortCodes.php
executable file
·198 lines (173 loc) · 9.48 KB
/
ShortCodes.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* Assets ShortCodes
*/
class AssetsShortCodes {
function __construct() {
}
function register_all() {
add_shortcode( 'results', [ $this, 'shortcode_results' ] );
add_shortcode( 'graph', [ $this, 'shortcode_graph' ] );
}
/**
* Shortcode to output results on front-end
*
* @param $attr
* @param $content
*
* @return string|void
*/
function shortcode_results( $attr, $content = null ) {
$attributes = shortcode_atts( [
'from' => '',
'till' => '',
'until' => '',
'footer' => 'false',
], $attr );
if ( empty( $attributes[ 'from' ] ) || empty( $attributes[ 'until' ] ) ) {
if ( current_user_can( 'manage_options' ) ) {
if ( empty( ! $attributes[ 'till' ] ) ) {
return sprintf( '<p>[%s]</p>', esc_html__( 'Shortcode is using old till value', 'b3-assets-tracker' ) ) ;
} else {
return sprintf( '<p>[%s]</p>', esc_html__( 'Shortcode is missing 1 or more attributes', 'b3-assets-tracker' ) ) ;
}
} else {
return '';
}
}
$date_from = gmdate( 'Y-m-d', strtotime( $attributes[ 'from' ] ) );
$date_until = gmdate( 'Y-m-d', strtotime( $attributes[ 'until' ] ) );
$dates = [ $date_from, $date_until ];
$grouped_data = bp_get_results_range( $dates, 'all' );
$show_diff = 1 < count( $grouped_data ) ? true : false;
if ( ! empty( count( $grouped_data ) ) ) {
$show_total = true;
$grouped_data = bp_process_data_for_table( $grouped_data, $show_diff, $show_total );
$types = bp_get_asset_types();
if ( ! is_admin() && is_array( $grouped_data ) ) {
ob_start();
$amount_columns = $grouped_data[ 0 ];
$scroll_class = 15 < $amount_columns ? ' tablescroll' : '';
$shortcode_notice = sprintf( '<div class="shortcode-notice tablescroll">%s</div>', esc_html__( 'Table scrolls horizontally.' ) );
if ( ! is_admin() && 6 < count( $amount_columns ) || is_admin() && 15 < count( $amount_columns ) ) {
printf( '<div class="shortcode-notice tablescroll">%s</div>', esc_html__( 'Table scrolls horizontally.' ) );
}
echo '<div id="data-output"><div id="data">';
include 'admin/includes/data-output.php';
echo '</div></div>';
if ( 'true' == $attributes[ 'footer' ] ) {
$page_id = get_page_by_path( 'assets-tracker' );
if ( $page_id ) {
$message = sprintf( esc_html__( 'This data comes from a WordPress %s I created to track my assets and easily share it within the site.', 'b3-assets-tracker' ), sprintf( '<a href="%s">%s</a>', '/assets-tracker/', 'plugin' ) );
} else {
$post = get_post( get_the_ID() );
$post_ts = gmdate( 'U', strtotime( $post->post_date ) );
if ( 1726701314 <= (int) $post_ts ) {
$message = 'Deze data komt uit een WordPress plugin die ik zelf geschreven heb om mijn assets te kunnen tracken en sharen.';
} else {
$message = esc_html__( 'This data comes from a WordPress plugin I created to track my assets and easily share it within the site.', 'b3-assets-tracker' );
}
}
echo sprintf( '<div class="shortcode-footer">%s</div>', esc_html( $message ) );
}
$result = ob_get_clean();
return $result;
}
} else {
if ( current_user_can( apply_filters( 'b3_assets_role','manage_options' ) ) ) {
return sprintf( '<p>[%s]</p>', esc_html__( 'Not enough data for results.', 'b3-assets-tracker' ) );
}
}
}
/**
* Shortcode to output results on front-end
*
* @param $attr
* @param $content
*
* @return string|void
*/
function shortcode_graph( $attr, $content = null ) {
if ( ! is_admin() ) {
$shortcode_attributes = shortcode_atts( [
'from' => '',
'until' => '',
'dates' => '',
'asset_type' => 'all',
'asset_group' => '',
'title' => '',
'type' => 'line',
'legend' => 'right',
'footer' => 'false',
], $attr );
$validated_shortcode_field = b3_validate_shortcode_fields( $shortcode_attributes );
if ( ! $validated_shortcode_field ) {
if ( current_user_can( 'manage_options' ) ) {
if ( ! empty( $shortcode_attributes[ 'till' ] ) ) {
return sprintf( '<p>[%s]</p>', esc_html__( 'Not enough data for results.', 'b3-assets-tracker' ) );
} else {
return sprintf( '<p>[%s]</p>', esc_html__( 'Shortcode is missing 1 or more attributes', 'b3-assets-tracker' ) );
}
} else {
return '';
}
}
$graph_type = $shortcode_attributes[ 'type' ];
$asset_groups = [];
$asset_types = 'all' != $shortcode_attributes[ 'asset_type' ] ? explode( ',', $shortcode_attributes[ 'asset_type' ] ) : $shortcode_attributes[ 'asset_type' ];
$grouped_data = [];
$show_all = 'all' == $asset_types ? true : false;
if ( 'line' === $graph_type ) {
if ( ! empty( $shortcode_attributes[ 'dates' ] ) ) {
$dates = explode( ',', $shortcode_attributes[ 'dates' ] );
foreach( $dates as $date ) {
$date = gmdate( 'Y-m-d', strtotime( $date ) );
if ( ! array_key_exists( $date, $grouped_data ) ) {
$grouped_data[ $date ] = [];
}
$date_rows = bp_get_data( $date );
if ( ! empty( $date_rows ) ) {
$grouped_data[ $date ] = $date_rows;
}
}
} elseif ( ! empty( $shortcode_attributes[ 'from' ] ) && ! empty( $shortcode_attributes[ 'until' ] ) ) {
$date_from = gmdate( 'Y-m-d', strtotime( $shortcode_attributes[ 'from' ] ) );
$date_until = gmdate( 'Y-m-d', strtotime( $shortcode_attributes[ 'until' ] ) );
$dates = [ $date_from, $date_until ];
$grouped_data = bp_get_results_range( $dates, $asset_types, [], $show_all );
}
} elseif ( in_array( $graph_type, [ 'total_type', 'total_group' ] ) ) {
$date_until = gmdate( 'Y-m-d', strtotime( $shortcode_attributes[ 'until' ] ) );
$dates = [ $date_until ];
$grouped_data = bp_get_results_range( $dates, 'all', [] );
}
$graph_title = bp_get_graph_title( $shortcode_attributes );
if ( 1 < count( $grouped_data ) ) {
$processed_data = bp_process_data_for_chart( $grouped_data, $asset_types, $asset_groups, $graph_type );
$margin_top = apply_filters( 'b3_chart_top_margin', 'auto' );
$chart_args = [
'asset_group' => $asset_groups,
'asset_type' => $asset_types,
'currency' => get_option( 'bp_currency' ),
'graph_title' => $graph_title,
'graph_type' => $graph_type,
'h_axis_title' => esc_html__( 'Date', 'b3-assets-tracker' ),
'v_axis_title' => esc_html__( 'Value', 'b3-assets-tracker' ),
'legend' => $shortcode_attributes[ 'legend' ],
'margin_top' => $margin_top,
'margin_left' => 'auto',
'margin_right' => 'auto',
'data' => $processed_data,
];
wp_localize_script( 'graphs', 'chart_vars', $chart_args );
return bp_get_chart_element();
} else {
if ( current_user_can( 'manage_options' ) ) {
return sprintf( '<p>[%s]</p>', esc_html__( 'Not enough data for to display. Check the shortcode in the content.', 'b3-assets-tracker' ) );
} else {
return sprintf( '<p>%s</p>', esc_html__( 'Something went wrong with the results.', 'b3-assets-tracker' ) );
}
}
}
}
}