class-progress.php
133 lines
| 1 | <?php |
| 2 | defined('ABSPATH') or die(); |
| 3 | class cmplz_progress { |
| 4 | private static $_this; |
| 5 | |
| 6 | function __construct() { |
| 7 | if ( isset( self::$_this ) ) |
| 8 | wp_die( sprintf( '%s is a singleton class and you cannot create a second instance.', get_class( $this ) ) ); |
| 9 | self::$_this = $this; |
| 10 | |
| 11 | add_filter("cmplz_do_action", array($this, 'progress_actions'), 10, 3); |
| 12 | } |
| 13 | |
| 14 | static function this() { |
| 15 | return self::$_this; |
| 16 | } |
| 17 | |
| 18 | public function progress_actions($data, $action, $request) { |
| 19 | if ( $action === 'dismiss_task' ) { |
| 20 | $data = $this->dismiss_task( $request['id'] ); |
| 21 | } |
| 22 | if ( $action === 'get_notices' ) { |
| 23 | $data = [ |
| 24 | 'notices' => $this->notices(), |
| 25 | 'show_cookiebanner' => cmplz_cookiebanner_should_load(true), |
| 26 | ]; |
| 27 | } |
| 28 | |
| 29 | return $data; |
| 30 | } |
| 31 | |
| 32 | public function notices(): array { |
| 33 | $notices = COMPLIANZ::$admin->get_warnings(array( 'status' => 'all' )); |
| 34 | $out = []; |
| 35 | foreach ($notices as $id => $notice ) { |
| 36 | $notice['id'] = $id; |
| 37 | $out[] = $notice; |
| 38 | } |
| 39 | return $out; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Calculate the percentage completed in the dashboard progress section |
| 44 | * Determine max score by adding $notice['score'] to the $max_score variable |
| 45 | * Determine actual score by adding $notice['score'] of each item with a 'success' output to $actual_score |
| 46 | * @return int |
| 47 | * |
| 48 | * @since 4.0 |
| 49 | * |
| 50 | */ |
| 51 | |
| 52 | private function percentage(): int { |
| 53 | if ( ! cmplz_user_can_manage() ) { |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | $max_score = 0; |
| 58 | $actual_score = 0; |
| 59 | $notices = COMPLIANZ::$admin->get_warnings(array( |
| 60 | 'status' => 'all', |
| 61 | )); |
| 62 | foreach ( $notices as $id => $notice ) { |
| 63 | if (isset( $notice['score'] )) { |
| 64 | // Only items matching condition will show in the dashboard. Only use these to determine max count. |
| 65 | $max_score += (int) $notice['score']; |
| 66 | $success = isset( $notice['icon'] ) && ( $notice['status'] === 'success' ); |
| 67 | if ( $success ) { |
| 68 | // If the output is success, task is completed. Add to actual count. |
| 69 | $actual_score += (int) $notice['score']; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | $score = $max_score>0 ? $actual_score / $max_score :0; |
| 74 | return (int) round( $score * 100 ); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /** |
| 79 | * Count number of premium notices we have in the list. |
| 80 | * @return int |
| 81 | */ |
| 82 | public function get_lowest_possible_task_count(): int { |
| 83 | $premium_notices = COMPLIANZ::$admin->get_warnings(array('premium_only'=>true)); |
| 84 | return count($premium_notices) ; |
| 85 | } |
| 86 | |
| 87 | public function dismiss_from_admin_notice(){ |
| 88 | if ( !cmplz_user_can_manage() ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | if (isset($_GET['dismiss_notice'])) { |
| 93 | $id = sanitize_title($_GET['dismiss_notice']); |
| 94 | $this->dismiss_task($id); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Process the react dismissal of a task |
| 100 | * |
| 101 | * Since 3.1 |
| 102 | * |
| 103 | * @access public |
| 104 | * |
| 105 | */ |
| 106 | |
| 107 | public function dismiss_task($id): array { |
| 108 | if ( !empty($id) ) { |
| 109 | $id = sanitize_title( $id ); |
| 110 | $dismissed_warnings = get_option( 'cmplz_dismissed_warnings', array() ); |
| 111 | if ( ! in_array( $id, $dismissed_warnings, true ) ) { |
| 112 | $dismissed_warnings[] = $id; |
| 113 | update_option( 'cmplz_dismissed_warnings', $dismissed_warnings, false ); |
| 114 | } |
| 115 | $count = get_transient( 'cmplz_plusone_count' ); |
| 116 | if (is_numeric($count) && $count>0) { |
| 117 | $count--; |
| 118 | } |
| 119 | set_transient('cmplz_plusone_count', $count, WEEK_IN_SECONDS); |
| 120 | //remove this notice from the admin notices list |
| 121 | $notices = get_transient( 'cmplz_admin_notices' ); |
| 122 | if (isset($notices[$id])) { |
| 123 | unset($notices[$id]); |
| 124 | } |
| 125 | set_transient('cmplz_admin_notices', $notices, DAY_IN_SECONDS); |
| 126 | } |
| 127 | |
| 128 | return []; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | } |
| 133 |