| 1 |
<?php |
| 2 |
/** |
| 3 |
* Centralized manager for WordPress backend functionality. |
| 4 |
* |
| 5 |
* @package MainWP\Dashboard |
| 6 |
* @version 4.6 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace MainWP\Dashboard\Module\Log; |
| 10 |
|
| 11 |
use MainWP\Dashboard\MainWP_Utility; |
| 12 |
|
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class - Log_Stats |
| 17 |
*/ |
| 18 |
class Log_Stats { |
| 19 |
|
| 20 |
/** |
| 21 |
* Class constructor. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Get stats data. |
| 28 |
* |
| 29 |
* @param array $items Logs items array. |
| 30 |
*/ |
| 31 |
public static function get_stats_data( $items ) { //phpcs:ignore -- NOSONAR - complex. |
| 32 |
$data = array(); |
| 33 |
if ( is_array( $items ) ) { |
| 34 |
foreach ( $items as $item ) { |
| 35 |
if ( 'compact' !== $item->connector ) { |
| 36 |
if ( ! isset( $data[ $item->connector ] ) ) { |
| 37 |
$data[ $item->connector ] = array(); |
| 38 |
} |
| 39 |
if ( ! isset( $data[ $item->connector ][ $item->context ] ) ) { |
| 40 |
$data[ $item->connector ][ $item->context ] = array(); |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! isset( $data[ $item->connector ][ $item->context ]['total_events'] ) ) { |
| 44 |
$data[ $item->connector ][ $item->context ]['total_events'] = array( |
| 45 |
'count' => 0, |
| 46 |
'dura_time' => 0, |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
if ( ! isset( $data[ $item->connector ][ $item->context ][ $item->action ] ) ) { |
| 51 |
$data[ $item->connector ][ $item->context ][ $item->action ] = array( |
| 52 |
'count' => 1, |
| 53 |
'dura_time' => $item->duration, |
| 54 |
); |
| 55 |
} else { |
| 56 |
$data[ $item->connector ][ $item->context ][ $item->action ]['count'] += 1; |
| 57 |
$data[ $item->connector ][ $item->context ][ $item->action ]['dura_time'] += $item->duration; |
| 58 |
} |
| 59 |
$data[ $item->connector ][ $item->context ]['total_events']['count'] += 1; |
| 60 |
$data[ $item->connector ][ $item->context ]['total_events']['dura_time'] += $item->duration; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
return $data; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Render stats count. |
| 69 |
* |
| 70 |
* @param array $data Stats data array. |
| 71 |
* @param string $action Action name. |
| 72 |
*/ |
| 73 |
public static function get_stats_count( $data, $action ) { |
| 74 |
return isset( $data[ $action ] ) && ! empty( $data[ $action ]['count'] ) ? $data[ $action ]['count'] : 0; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
/** |
| 79 |
* Render stats duration time. |
| 80 |
* |
| 81 |
* @param array $data Stats data array. |
| 82 |
* @param string $action Action name. |
| 83 |
*/ |
| 84 |
public static function render_stats_duration_time( $data, $action ) { |
| 85 |
$dura_time = isset( $data[ $action ] ) && ! empty( $data[ $action ]['dura_time'] ) ? $data[ $action ]['dura_time'] : 0; |
| 86 |
echo MainWP_Utility::format_duration_time( $dura_time ) ; //phpcs:ignore -- ok. |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Render stats column content. |
| 91 |
* |
| 92 |
* @param array $data Data stats array. |
| 93 |
* @param string $action Action name. |
| 94 |
* @param string $title Action title. |
| 95 |
* @param array $data_prev Data Stats previous array. |
| 96 |
*/ |
| 97 |
public static function render_stats_info( $data, $action, $title, $data_prev ) { |
| 98 |
$count = static::get_stats_count( $data, $action ); |
| 99 |
$prev_count = static::get_stats_count( $data_prev, $action ); |
| 100 |
?> |
| 101 |
<div class="column"> |
| 102 |
<div class="ui small header"> |
| 103 |
<?php static::render_stats_compare( $count, $prev_count ); ?> <?php echo intval( $count ); ?> |
| 104 |
<div class="sub header"> |
| 105 |
<?php echo esc_html( $title ); ?> |
| 106 |
<br /> |
| 107 |
<span class="ui small text"><?php static::render_stats_duration_time( $data, $action ); ?></span> |
| 108 |
</div> |
| 109 |
</div> |
| 110 |
</div> |
| 111 |
<?php |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Render stats compare. |
| 116 |
* |
| 117 |
* @param int $count Count stats. |
| 118 |
* @param int $prev_count Count previous stats. |
| 119 |
*/ |
| 120 |
public static function render_stats_compare( $count, $prev_count ) { |
| 121 |
echo '<span data-tooltip="' . intval( $count ) . ' actions this period, ' . intval( $prev_count ) . ' actions previous period, showing growth or decline." data-position="top left" data-inverted="">'; |
| 122 |
if ( $count === $prev_count ) { |
| 123 |
?> |
| 124 |
<i class="angle right icon" style="float:none;"></i> |
| 125 |
<?php |
| 126 |
} elseif ( $count > $prev_count ) { |
| 127 |
?> |
| 128 |
<i class="angle up green icon"></i> |
| 129 |
<?php |
| 130 |
} else { |
| 131 |
?> |
| 132 |
<i class="angle down red icon"></i> |
| 133 |
<?php |
| 134 |
} |
| 135 |
echo '</span>'; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* Method render_chart_series(). |
| 141 |
* |
| 142 |
* @param array $data Stats data. |
| 143 |
* @param string $action Action. |
| 144 |
* @param string $title Title. |
| 145 |
* @param array $data_prev Stats previous data. |
| 146 |
*/ |
| 147 |
public static function render_chart_series( $data, $action, $title, $data_prev ) { |
| 148 |
$count = static::get_stats_count( $data, $action ); |
| 149 |
$prev_count = static::get_stats_count( $data_prev, $action ); |
| 150 |
echo ' |
| 151 |
{ |
| 152 |
x: "' . esc_html( $title ) . '", |
| 153 |
y: "' . intval( $count ) . '", |
| 154 |
goals: [ |
| 155 |
{ |
| 156 |
name: "Previous", |
| 157 |
value: "' . intval( $prev_count ) . '", |
| 158 |
strokeHeight: 2, |
| 159 |
strokeColor: "#7fb100", |
| 160 |
} |
| 161 |
], |
| 162 |
fillColor: "#18a4e0", |
| 163 |
}, |
| 164 |
|
| 165 |
'; |
| 166 |
} |
| 167 |
} |
| 168 |
|