-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathUtility.php
More file actions
471 lines (425 loc) · 14.5 KB
/
Copy pathUtility.php
File metadata and controls
471 lines (425 loc) · 14.5 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
// +----------------------------------------------------------------------+
// | Copyright 2013 Madpixels (email : [email protected]) |
// +----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License, version 2, as |
// | published by the Free Software Foundation. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
// | MA 02110-1301 USA |
// +----------------------------------------------------------------------+
// | Author: Eugene Manuilov <[email protected]> |
// +----------------------------------------------------------------------+
/**
* Utilities module class.
*
* @category Visualizer
* @package Module
*
* @since 3.3.0
*/
class Visualizer_Module_Utility extends Visualizer_Module {
const NAME = __CLASS__;
/**
* Some default distinct colors.
*
* @since 3.3.0
*
* @access private
* @var string[]
*/
private static $_CHART_COLORS = array(
'#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080',
);
/**
* Constructor.
*
* @since 3.3.0
*
* @access public
*
* @param Visualizer_Plugin $plugin The instance of the plugin.
*/
public function __construct( Visualizer_Plugin $plugin ) {
parent::__construct( $plugin );
$this->_addFilter( Visualizer_Plugin::FILTER_GET_CHART_SETTINGS, 'apply_global_style_settings', 999, 3 );
}
/**
* Convert hexdec color string to rgb(a) string.
*
* Props to https://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/
*
* @since 3.3.0
*
* @access private
*/
private static function hex2rgba( $color, $opacity = false ) {
$default = 'rgb(0,0,0)';
// Return default if no color provided
if ( empty( $color ) ) {
return $default;
}
// Sanitize $color if "#" is provided
if ( strpos( $color, '#' ) === 0 ) {
$color = substr( $color, 1 );
}
// Check if color has 6 or 3 characters and get values
if ( strlen( $color ) === 6 ) {
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
} elseif ( strlen( $color ) === 3 ) {
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
} else {
return $default;
}
// Convert hexadec to rgb
$rgb = array_map( 'hexdec', $hex );
// Check if opacity is set(rgba or rgb)
if ( $opacity ) {
if ( abs( $opacity ) > 1 ) {
$opacity = 1.0;
}
$output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')';
} else {
$output = 'rgb(' . implode( ',', $rgb ) . ')';
}
// Return rgb(a) color string
return $output;
}
/**
* Returns a color at a specific index from the palette, with its transparent equivalent.
*
* @return array{string, string}
* @access private
*/
private static function get_color_at( int $index ): array {
$colors = self::get_color_palette();
$color = $colors[ $index % count( $colors ) ];
return array( self::hex2rgba( $color, 0.5 ), $color );
}
/**
* Mixes a hex color toward white by the given factor (0 = original, 1 = white).
*
* @access private
*/
private static function tint_color( string $hex, float $factor ): string {
$hex = ltrim( $hex, '#' );
if ( strlen( $hex ) === 3 ) {
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2];
}
$r = (int) round( hexdec( substr( $hex, 0, 2 ) ) + ( 255 - hexdec( substr( $hex, 0, 2 ) ) ) * $factor );
$g = (int) round( hexdec( substr( $hex, 2, 2 ) ) + ( 255 - hexdec( substr( $hex, 2, 2 ) ) ) * $factor );
$b = (int) round( hexdec( substr( $hex, 4, 2 ) ) + ( 255 - hexdec( substr( $hex, 4, 2 ) ) ) * $factor );
return sprintf( '#%02x%02x%02x', $r, $g, $b );
}
/**
* Returns the color palette to use for charts.
*
* When global primary/secondary colors are configured, generates a palette of
* alternating tints: [primary, secondary, primary@25%, secondary@25%, ...].
* Falls back to the built-in colors otherwise.
*
* @return string[]
* @access private
*/
private static function get_color_palette(): array {
$global = self::get_global_style_defaults();
$primary = $global['color_primary'];
$secondary = $global['color_secondary'];
if ( empty( $primary ) && empty( $secondary ) ) {
return self::$_CHART_COLORS;
}
$bases = array_filter( array( $primary, $secondary ) );
$factors = array( 0, 0.25, 0.5, 0.75 );
$palette = array();
foreach ( $factors as $factor ) {
foreach ( $bases as $base ) {
$palette[] = $factor === 0 ? $base : self::tint_color( $base, $factor );
}
}
return $palette;
}
/**
* Returns the global style defaults stored in the plugin settings.
*
* @return array<string, string>
* @access public
* @static
*/
public static function get_global_style_defaults(): array {
$option = get_option( Visualizer_Module_Admin::OPTION_GLOBAL_SETTINGS, array() );
return wp_parse_args(
$option,
array(
'color_primary' => '',
'color_secondary' => '',
'apply_existing' => '0',
)
);
}
/**
* Applies global styles to existing charts at render time.
*
* @param array<string, mixed>|mixed $settings Chart settings.
* @param int $chart_id Chart ID.
* @param string $type Chart type.
* @return array<string, mixed>
* @access public
*/
public function apply_global_style_settings( $settings, $chart_id, $type ): array {
$settings = is_array( $settings ) ? $settings : array();
$global = self::get_global_style_defaults();
if ( empty( $global['apply_existing'] ) ) {
return $settings;
}
if ( empty( $global['color_primary'] ) && empty( $global['color_secondary'] ) ) {
return $settings;
}
if ( empty( $chart_id ) ) {
return $settings;
}
$library = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_LIBRARY, true );
$library = is_string( $library ) ? strtolower( $library ) : '';
if ( empty( $type ) ) {
$type = get_post_meta( $chart_id, Visualizer_Plugin::CF_CHART_TYPE, true );
}
$type = is_string( $type ) ? strtolower( $type ) : '';
if ( empty( $library ) ) {
$library = in_array( $type, array( 'datatable', 'tabular' ), true ) ? 'datatable' : 'googlecharts';
}
if ( 'googlecharts' === $library || 'google' === $library ) {
if ( 'geo' !== $type ) {
$has_explicit = false;
if ( ! empty( $settings['colors'] ) ) {
$has_explicit = true;
}
if ( ! $has_explicit && isset( $settings['series'] ) && is_array( $settings['series'] ) ) {
foreach ( $settings['series'] as $series_settings ) {
if ( ! empty( $series_settings['color'] ) ) {
$has_explicit = true;
break;
}
}
}
if ( ! $has_explicit && isset( $settings['slices'] ) && is_array( $settings['slices'] ) ) {
foreach ( $settings['slices'] as $slice_settings ) {
if ( ! empty( $slice_settings['color'] ) ) {
$has_explicit = true;
break;
}
}
}
if ( ! $has_explicit ) {
$settings['colors'] = self::get_color_palette();
}
}
return $settings;
}
if ( 'chartjs' === $library ) {
$has_explicit = false;
if ( isset( $settings['series'] ) && is_array( $settings['series'] ) ) {
foreach ( $settings['series'] as $series_settings ) {
if ( ! empty( $series_settings['backgroundColor'] ) || ! empty( $series_settings['borderColor'] ) || ! empty( $series_settings['hoverBackgroundColor'] ) ) {
$has_explicit = true;
break;
}
}
}
if ( ! $has_explicit && isset( $settings['slices'] ) && is_array( $settings['slices'] ) ) {
foreach ( $settings['slices'] as $slice_settings ) {
if ( ! empty( $slice_settings['backgroundColor'] ) || ! empty( $slice_settings['borderColor'] ) || ! empty( $slice_settings['hoverBackgroundColor'] ) ) {
$has_explicit = true;
break;
}
}
}
if ( $has_explicit ) {
return $settings;
}
$series = get_post_meta( $chart_id, Visualizer_Plugin::CF_SERIES, true );
if ( is_array( $series ) ) {
$settings = self::apply_chartjs_palette( $settings, $type, $series, $chart_id );
}
}
return $settings;
}
/**
* Applies the global palette to ChartJS settings for a chart.
*
* @param array<string, mixed> $settings Current chart settings.
* @param string $type Chart type.
* @param array<int, mixed> $series Series definitions.
* @param int $chart_id Chart ID.
* @return array<string, mixed>
* @access private
* @static
*/
private static function apply_chartjs_palette( array $settings, string $type, array $series, int $chart_id ): array {
$attributes = array();
$name = 'series';
$count = count( $series );
$max = $count - 1;
switch ( $type ) {
case 'polarArea':
// fall through.
case 'pie':
$chart = get_post( $chart_id );
$data = $chart instanceof WP_Post ? maybe_unserialize( $chart->post_content ) : array();
$name = 'slices';
$max = is_array( $data ) ? count( $data ) : $count;
// fall through.
case 'column':
// fall through.
case 'bar':
for ( $i = 0; $i < $max; $i++ ) {
$colors = self::get_color_at( $i );
$attributes[] = array( 'backgroundColor' => $colors[0], 'hoverBackgroundColor' => $colors[1] );
}
break;
case 'radar':
// fall through.
case 'line':
// fall through.
case 'area':
for ( $i = 0; $i < $max; $i++ ) {
$colors = self::get_color_at( $i );
$attributes[] = array( 'borderColor' => $colors[0] );
}
break;
}
if ( $attributes ) {
$settings[ $name ] = $attributes;
}
return $settings;
}
/**
* Sets some defaults in the chart.
*
* @since 3.3.0
*
* @access public
*/
public static function set_defaults( $chart, $post_status = 'auto-draft' ) {
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
$library = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_LIBRARY, true );
// if post_status is null, operate on the chart irrespective of the post_status
if ( ( ! is_null( $post_status ) && $chart->post_status !== $post_status ) ) {
return;
}
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
if ( ! $series || ! is_array( $series ) ) {
return;
}
switch ( $library ) {
case 'ChartJS':
self::set_defaults_chartjs( $chart, $post_status );
break;
case 'GoogleCharts':
self::set_defaults_google( $chart, $post_status );
break;
}
}
/**
* Sets some defaults in the chart for Google charts.
*
* @since 3.3.0
*
* @access private
*/
private static function set_defaults_google( $chart, $post_status ) {
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
$attributes = array();
if ( $post_status === 'auto-draft' ) {
switch ( $type ) {
case 'combo':
// chart type 'bars' and randomly choose the type of each series.
$types = array( 'area', 'line', 'steppedArea', 'bar' );
$attributes['seriesType'] = 'bars';
for ( $x = 0; $x < count( $series ); $x++ ) {
$attributes['series'][ $x ]['type'] = $types[ rand( 0, count( $types ) - 1 ) ];
}
break;
case 'candlestick':
// add stroke and fill color so that behavior is consistent.
$attributes['candlestick']['fallingColor']['stroke'] = '#3366cc';
$attributes['candlestick']['fallingColor']['fill'] = '#fff';
$attributes['candlestick']['risingColor']['stroke'] = '#3366cc';
$attributes['candlestick']['risingColor']['fill'] = '#3366cc';
break;
}
// Apply global color defaults to new Google Charts (not Geo — it uses colorAxis).
if ( 'geo' !== $type ) {
$global = self::get_global_style_defaults();
if ( ! empty( $global['color_primary'] ) || ! empty( $global['color_secondary'] ) ) {
$attributes['colors'] = self::get_color_palette();
}
}
}
if ( $attributes ) {
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, array_merge( $settings, $attributes ) );
}
}
/**
* Sets some defaults in the chart for ChartJS charts. Only during first creation.
*
* @since 3.3.0
*
* @access private
*/
private static function set_defaults_chartjs( $chart, $post_status ) {
if ( $post_status !== 'auto-draft' ) {
return;
}
$type = get_post_meta( $chart->ID, Visualizer_Plugin::CF_CHART_TYPE, true );
$series = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SERIES, true );
$settings = get_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, true );
$attributes = array();
$name = 'series';
$count = count( $series );
$max = $count - 1;
switch ( $type ) {
case 'polarArea':
// fall through.
case 'pie':
$data = unserialize( $chart->post_content );
$name = 'slices';
$max = count( $data );
// fall through.
case 'column':
// fall through.
case 'bar':
for ( $i = 0; $i < $max; $i++ ) {
$colors = self::get_color_at( $i );
$attributes[] = array( 'backgroundColor' => $colors[0], 'hoverBackgroundColor' => $colors[1] );
}
break;
case 'radar':
// fall through.
case 'line':
// fall through.
case 'area':
for ( $i = 0; $i < $max; $i++ ) {
$colors = self::get_color_at( $i );
$attributes[] = array( 'borderColor' => $colors[0] );
}
break;
}
if ( $post_status === 'auto-draft' ) {
// the charts are huge in size so let's always get them down to 50%.
$settings['width'] = $settings['height'] = '50%';
}
if ( $attributes ) {
$settings[ $name ] = $attributes;
update_post_meta( $chart->ID, Visualizer_Plugin::CF_SETTINGS, $settings );
}
}
}