class-progress.php
159 lines
| 1 | <?php |
| 2 | defined('ABSPATH') or die(); |
| 3 | class rsssl_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_action( 'admin_init', array( $this, 'dismiss_from_admin_notice') ); |
| 12 | } |
| 13 | |
| 14 | static function this() { |
| 15 | return self::$_this; |
| 16 | } |
| 17 | |
| 18 | public function get() { |
| 19 | return [ |
| 20 | 'text' => $this->get_text(), |
| 21 | 'percentage' => $this->percentage(), |
| 22 | 'notices' => $this->notices(), |
| 23 | ]; |
| 24 | } |
| 25 | |
| 26 | public function notices(){ |
| 27 | $notices = RSSSL()->admin->get_notices_list(array( 'status' => ['open','warning','completed','premium'] )); |
| 28 | $out = []; |
| 29 | foreach ($notices as $id => $notice ) { |
| 30 | $notice['id'] = $id; |
| 31 | $out[] = $notice; |
| 32 | } |
| 33 | return $out; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Calculate the percentage completed in the dashboard progress section |
| 38 | * Determine max score by adding $notice['score'] to the $max_score variable |
| 39 | * Determine actual score by adding $notice['score'] of each item with a 'success' output to $actual_score |
| 40 | * @return int |
| 41 | * |
| 42 | * @since 4.0 |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | private function percentage() { |
| 47 | if ( ! rsssl_user_can_manage() ) { |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | $max_score = 0; |
| 52 | $actual_score = 0; |
| 53 | $notices = RSSSL()->admin->get_notices_list(array( |
| 54 | 'status' => ['open','warning','completed','premium'], |
| 55 | )); |
| 56 | foreach ( $notices as $id => $notice ) { |
| 57 | if (isset( $notice['score'] )) { |
| 58 | // Only items matching condition will show in the dashboard. Only use these to determine max count. |
| 59 | $max_score += (int) $notice['score']; |
| 60 | $success = isset( $notice['output']['icon'] ) && ( $notice['output']['icon'] === 'success' ); |
| 61 | if ( $success ) { |
| 62 | // If the output is success, task is completed. Add to actual count. |
| 63 | $actual_score += (int) $notice['score']; |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | $score = $max_score>0 ? $actual_score / $max_score :0; |
| 68 | return (int) round( $score * 100 ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get text for progress block |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | private function get_text(){ |
| 77 | if (!rsssl_user_can_manage()) return ''; |
| 78 | ob_start(); |
| 79 | |
| 80 | $open_task_count = count( RSSSL()->admin->get_notices_list( array( 'status' => ['open','warning'] ) )); |
| 81 | if ( rsssl_get_option('ssl_enabled') ) { |
| 82 | if ( $open_task_count !== 0 ) { |
| 83 | echo sprintf( _n( "Security configuration not completed yet. You still have %s task open.", "You still have %s tasks open.", $open_task_count, 'really-simple-ssl' ), $open_task_count ); |
| 84 | } |
| 85 | if ( $open_task_count === 0 && defined('rsssl_pro') ) { |
| 86 | _e("Security configuration completed!", "really-simple-ssl"); |
| 87 | } |
| 88 | |
| 89 | if ( $open_task_count === 0 && ! defined('rsssl_pro') ) { |
| 90 | _e( "Basic security configuration completed!", "really-simple-ssl" ); |
| 91 | } |
| 92 | } else if ( !is_network_admin() ) { |
| 93 | _e( "SSL is not yet enabled on this site.", "really-simple-ssl" ); |
| 94 | } |
| 95 | do_action('rsssl_progress_feedback'); |
| 96 | return ob_get_clean(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Count number of premium notices we have in the list. |
| 101 | * @return int |
| 102 | */ |
| 103 | public function get_lowest_possible_task_count() { |
| 104 | $premium_notices = RSSSL()->admin->get_notices_list(array('premium_only'=>true)); |
| 105 | return count($premium_notices) ; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @return void |
| 110 | */ |
| 111 | public function dismiss_from_admin_notice(){ |
| 112 | if ( ! rsssl_user_can_manage() ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | if ( isset($_GET['dismiss_notice']) ) { |
| 117 | $id = sanitize_title($_GET['dismiss_notice']); |
| 118 | |
| 119 | // Verify nonce |
| 120 | if ( ! isset($_GET['_wpnonce']) || ! wp_verify_nonce($_GET['_wpnonce'], 'rsssl_dismiss_notice_' . $id) ) { |
| 121 | return; // or wp_die('Invalid request'); |
| 122 | } |
| 123 | |
| 124 | $this->dismiss_task($id); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Process the react dismissal of a task |
| 130 | * |
| 131 | * Since 3.1 |
| 132 | * |
| 133 | * @access public |
| 134 | * |
| 135 | */ |
| 136 | |
| 137 | public function dismiss_task($id) |
| 138 | { |
| 139 | if ( !empty($id) ) { |
| 140 | $id = sanitize_title( $id ); |
| 141 | update_option( "rsssl_".$id."_dismissed", true, false ); |
| 142 | $count = get_option( 'rsssl_plusone_count' ); |
| 143 | if (is_numeric($count) && $count>0) { |
| 144 | $count--; |
| 145 | } |
| 146 | update_option('rsssl_plusone_count', $count, WEEK_IN_SECONDS); |
| 147 | //remove this notice from the admin notices list |
| 148 | $notices = get_option( 'rsssl_admin_notices' ); |
| 149 | if (isset($notices[$id])) { |
| 150 | unset($notices[$id]); |
| 151 | } |
| 152 | update_option('rsssl_admin_notices', $notices); |
| 153 | } |
| 154 | |
| 155 | return [ |
| 156 | 'percentage' => $this->percentage(), |
| 157 | ]; |
| 158 | } |
| 159 | } |