| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dev4Press\Plugin\SweepPress\Basic; |
| 4 |
|
| 5 |
use Dev4Press\v39\Core\Plugins\Settings as BaseSettings; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class Settings extends BaseSettings { |
| 12 |
public $base = 'sweeppress'; |
| 13 |
|
| 14 |
public $settings = array( |
| 15 |
'core' => array( |
| 16 |
'activated' => 0 |
| 17 |
), |
| 18 |
'statistics' => array( |
| 19 |
'months' => array(), |
| 20 |
'total' => array() |
| 21 |
), |
| 22 |
'settings' => array( |
| 23 |
'expand_cli' => false, |
| 24 |
'expand_rest' => false, |
| 25 |
'hide_backup_notices' => false |
| 26 |
), |
| 27 |
'sweepers' => array( |
| 28 |
'keep_days_posts-auto-draft' => 7, |
| 29 |
'keep_days_posts-spam' => 7, |
| 30 |
'keep_days_posts-trash' => 14, |
| 31 |
'keep_days_posts-revisions' => 14, |
| 32 |
'keep_days_comments-spam' => 7, |
| 33 |
'keep_days_comments-trash' => 7, |
| 34 |
'keep_days_comments-unapproved' => 28, |
| 35 |
'keep_days_comments-pingback' => 14, |
| 36 |
'keep_days_comments-trackback' => 14, |
| 37 |
'keep_days_comments-ua' => 14, |
| 38 |
'keep_days_comments-akismet' => 14, |
| 39 |
'keep_days_signups-inactive' => 90, |
| 40 |
'db_table_optimize_threshold' => 40, |
| 41 |
'db_table_optimize_min_size' => 6, |
| 42 |
'db_table_optimize_method' => 'optimize', |
| 43 |
'last_used_timestamp' => array() |
| 44 |
) |
| 45 |
); |
| 46 |
|
| 47 |
public function log_statistics( $input = array() ) { |
| 48 |
$default = array( |
| 49 |
'source' => '', |
| 50 |
'records' => 0, |
| 51 |
'size' => 0, |
| 52 |
'time' => 0, |
| 53 |
'jobs' => 0, |
| 54 |
'tasks' => 0, |
| 55 |
'sweepers' => array() |
| 56 |
); |
| 57 |
|
| 58 |
$input = wp_parse_args( $input, $default ); |
| 59 |
|
| 60 |
if ( in_array( $input['source'], array( 'quick', 'scheduler', 'panel', 'auto', 'cli', 'rest' ) ) ) { |
| 61 |
$month = date( 'Y-m' ); |
| 62 |
|
| 63 |
if ( ! isset( $this->current['statistics']['months'][ $month ] ) ) { |
| 64 |
$this->current['statistics']['months'][ $month ] = $this->_statistics_empty(); |
| 65 |
} |
| 66 |
|
| 67 |
$this->current['statistics']['months'][ $month ][ $input['source'] ] ++; |
| 68 |
$this->current['statistics']['months'][ $month ]['records'] += $input['records'] ?? 0; |
| 69 |
$this->current['statistics']['months'][ $month ]['size'] += $input['size'] ?? 0; |
| 70 |
$this->current['statistics']['months'][ $month ]['time'] += $input['time'] ?? 0; |
| 71 |
$this->current['statistics']['months'][ $month ]['jobs'] += $input['jobs'] ?? 0; |
| 72 |
$this->current['statistics']['months'][ $month ]['tasks'] += $input['tasks'] ?? 0; |
| 73 |
|
| 74 |
foreach ( $input['sweepers'] as $sweep => $data ) { |
| 75 |
if ( ! isset( $this->current['statistics']['months'][ $month ]['sweepers'][ $sweep ] ) ) { |
| 76 |
$this->current['statistics']['months'][ $month ]['sweepers'][ $sweep ] = array( |
| 77 |
'records' => 0, |
| 78 |
'size' => 0, |
| 79 |
'time' => 0, |
| 80 |
'counts' => 0 |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
$this->current['statistics']['months'][ $month ]['sweepers'][ $sweep ]['records'] += $data['records'] ?? 0; |
| 85 |
$this->current['statistics']['months'][ $month ]['sweepers'][ $sweep ]['size'] += $data['size'] ?? 0; |
| 86 |
$this->current['statistics']['months'][ $month ]['sweepers'][ $sweep ]['time'] += $data['time'] ?? 0; |
| 87 |
$this->current['statistics']['months'][ $month ]['sweepers'][ $sweep ]['counts'] ++; |
| 88 |
|
| 89 |
$this->current['sweepers']['last_used_timestamp'][ $sweep ] = time(); |
| 90 |
} |
| 91 |
|
| 92 |
if ( empty( $this->current['statistics']['total'] ) ) { |
| 93 |
$this->current['statistics']['total'] = $this->_statistics_empty(); |
| 94 |
} |
| 95 |
|
| 96 |
$this->current['statistics']['total'][ $input['source'] ] ++; |
| 97 |
$this->current['statistics']['total']['records'] += $input['records'] ?? 0; |
| 98 |
$this->current['statistics']['total']['size'] += $input['size'] ?? 0; |
| 99 |
$this->current['statistics']['total']['time'] += $input['time'] ?? 0; |
| 100 |
$this->current['statistics']['total']['jobs'] += $input['jobs'] ?? 0; |
| 101 |
$this->current['statistics']['total']['tasks'] += $input['tasks'] ?? 0; |
| 102 |
|
| 103 |
foreach ( $input['sweepers'] as $sweep => $data ) { |
| 104 |
if ( ! isset( $this->current['statistics']['total']['sweepers'][ $sweep ] ) ) { |
| 105 |
$this->current['statistics']['total']['sweepers'][ $sweep ] = array( |
| 106 |
'records' => 0, |
| 107 |
'size' => 0, |
| 108 |
'time' => 0, |
| 109 |
'counts' => 0 |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
$this->current['statistics']['total']['sweepers'][ $sweep ]['records'] += $data['records'] ?? 0; |
| 114 |
$this->current['statistics']['total']['sweepers'][ $sweep ]['size'] += $data['size'] ?? 0; |
| 115 |
$this->current['statistics']['total']['sweepers'][ $sweep ]['time'] += $data['time'] ?? 0; |
| 116 |
$this->current['statistics']['total']['sweepers'][ $sweep ]['counts'] ++; |
| 117 |
} |
| 118 |
|
| 119 |
$this->save( 'statistics' ); |
| 120 |
$this->save( 'sweepers' ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public function get_statistics( string $month = '' ) : array { |
| 125 |
if ( empty( $month ) || ! isset( $this->current['statistics']['months'][ $month ] ) ) { |
| 126 |
$value = $this->current['statistics']['total']; |
| 127 |
$value['label'] = isset( $value['started'] ) ? sprintf( __( "Since: %s", "sweeppress" ), $value['started'] ) : __( "All Time", "sweeppress" ); |
| 128 |
} else { |
| 129 |
$value = $this->current['statistics']['months'][ $month ]; |
| 130 |
$value['label'] = sprintf( __( "For Month: %s", "sweeppress" ), date_create_from_format( 'Y-m', $month )->format( 'Y F' ) ); |
| 131 |
} |
| 132 |
|
| 133 |
$value['size_total'] = $value['size']; |
| 134 |
|
| 135 |
if ( isset( $value['sweepers']['optimize-tables']['size'] ) ) { |
| 136 |
$value['size'] = $value['size'] - $value['sweepers']['optimize-tables']['size']; |
| 137 |
} |
| 138 |
|
| 139 |
return $value; |
| 140 |
} |
| 141 |
|
| 142 |
public function list_statistics() : array { |
| 143 |
$list = array( |
| 144 |
'' => __( "All Time Statistics", "sweeppress" ) |
| 145 |
); |
| 146 |
|
| 147 |
foreach ( array_keys( $this->current['statistics']['months'] ) as $month ) { |
| 148 |
$list[ $month ] = date_create_from_format( 'Y-m', $month )->format( 'Y F' ); |
| 149 |
} |
| 150 |
|
| 151 |
return $list; |
| 152 |
} |
| 153 |
|
| 154 |
public function show_notice( string $name = 'backup' ) : bool { |
| 155 |
switch ( $name ) { |
| 156 |
default: |
| 157 |
case 'backup': |
| 158 |
return ! $this->get( 'hide_backup_notices' ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public function get_sweeper_last_used_timestamp( string $sweeper ) : int { |
| 163 |
return isset( $this->current['sweepers']['last_used_timestamp'][ $sweeper ] ) ? intval( $this->current['sweepers']['last_used_timestamp'][ $sweeper ] ) : 0; |
| 164 |
} |
| 165 |
|
| 166 |
protected function constructor() { |
| 167 |
$this->info = new Information(); |
| 168 |
|
| 169 |
add_action( 'sweeppress_load_settings', array( $this, 'init' ) ); |
| 170 |
} |
| 171 |
|
| 172 |
protected function _statistics_empty() : array { |
| 173 |
return array( |
| 174 |
'started' => date( 'c' ), |
| 175 |
'quick' => 0, |
| 176 |
'scheduler' => 0, |
| 177 |
'panel' => 0, |
| 178 |
'auto' => 0, |
| 179 |
'cli' => 0, |
| 180 |
'rest' => 0, |
| 181 |
'records' => 0, |
| 182 |
'size' => 0, |
| 183 |
'jobs' => 0, |
| 184 |
'tasks' => 0, |
| 185 |
'time' => 0, |
| 186 |
'sweepers' => array() |
| 187 |
); |
| 188 |
} |
| 189 |
} |
| 190 |
|