getOption( 'an_option_stats' ) != 2 ) { wp_add_dashboard_widget( 'an_dashboard_widgets', '  ' . __( 'Adblock Notify Stats', 'an-translate' ), 'an_get_counters' ); //Chart JS wp_enqueue_script( 'an_chart_js', AN_URL . 'lib/chart-js/Chart.min.js', array( 'jquery' ), NULL ); //CSS & JS add_action( 'admin_enqueue_scripts', 'an_register_admin_scripts' ); } } add_action( 'wp_dashboard_setup', 'an_dashboard_widgets' ); /*************************************************************** * Page views & page blocked counter ***************************************************************/ function an_adblock_counter() { if (current_user_can( 'manage_options' ) || empty( $_POST[ 'an_state' ] ) ) return; $an_states = $_POST[ 'an_state' ]; $anCount = get_option( 'adblocker_notify_counter' ); foreach ( $an_states as $an_state ) { if ( empty( $anCount ) ) { $anCount = array( 'total' => 0, 'blocked' => 0, 'deactivated' => 0, 'history' => array() ); add_option( 'adblocker_notify_counter', $anCount ); } //update option with new values $anCount[$an_state] ++; //then update history $anCount = an_history_counter( $anCount, $an_state ); } //update db update_option( 'adblocker_notify_counter', $anCount ); exit; } add_action( 'wp_ajax_call_an_adblock_counter', 'an_adblock_counter' ); add_action( 'wp_ajax_nopriv_call_an_adblock_counter', 'an_adblock_counter' ); /*************************************************************** * Calcul date diff ***************************************************************/ function an_date_diff( $toDay, $toCheck ) { $todayObj = new DateTime( $toDay ); $expiredObj = new DateTime( $toCheck ); $dateDiff = $todayObj->diff( $expiredObj ); return $dateDiff->days; } /*************************************************************** * Page history counter ***************************************************************/ function an_history_counter( $anCount, $val = null ) { $anToday = date( 'Y-m-d', current_time( 'timestamp', 0 ) ); //$anToday = date( 'Y-m-d', strtotime( '1 day', strtotime( date( 'Y-m-d', current_time( 'timestamp', 0 ) ) ) ) ); if (empty($anCount[ 'history' ][0])) { $anCount[ 'history' ][0] = array( 'date' => $anToday, 'total' => $anCount[ 'total' ], 'blocked' => $anCount[ 'blocked' ] ); } else { $anDate = $anCount[ 'history' ][0][ 'date' ]; $anDiff = an_date_diff( $anToday, $anDate ); if ( $anDate == $anToday ) { //increase current date if ( $val == 'total' ) { $anCount[ 'history' ][0][ 'total' ] = $anCount[ 'history' ][0][ 'total' ] + 1; } elseif ($val == 'blocked' ) { $anCount[ 'history' ][0][ 'blocked' ] = $anCount[ 'history' ][0][ 'blocked' ] + 1; } } else if ( $anDiff > 0 ) { //remove last + add new one if ( $val == 'total' ) { $anNew = array( 'date' => $anToday, 'total' => 1, 'blocked' => 0 ); } elseif ( $val == 'blocked' ) { $anNew = array( 'date' => $anToday, 'total' => 1, 'blocked' => 1 ); } $anCount[ 'history' ] = array_merge( array( $anNew ), $anCount[ 'history' ] ); if (count($anCount[ 'history' ] == 8)) { $anOld = an_date_diff( $anToday, $anCount[ 'history' ][7][ 'date' ] ); if ( $anOld == 7 && count( $anCount[ 'history' ] == 8 ) ) { //remove last + add new one ($anRemove is a rubbish var) array_pop( $anCount[ 'history' ] ); } } } } return $anCount; } /*************************************************************** * Data history extraction & order revert for chart ***************************************************************/ function an_widget_data_histoty( $anCount, $val = null ) { if ( empty( $anCount[ 'history' ][0] ) ) return; foreach ( $anCount[ 'history' ] as $row ) { $anOutput[] = $row[$val]; } return $anOutput; } /*************************************************************** * Display the Dashboard Widget ***************************************************************/ function an_get_counters() { $anCount = get_option( 'adblocker_notify_counter' ); if ( empty( $anCount ) ) { echo '

No data

'; return; } //prevent plugin's counter to be higher than the page counter if page is refreshed during the ajax call or if wordpress caching systeme in not badly configured if ( ( $anCount[ 'blocked' ] > $anCount[ 'total' ]) || ($anCount[ 'history' ][0][ 'blocked' ] > $anCount[ 'history' ][0][ 'total' ] ) ) { if ( $anCount[ 'blocked' ] > $anCount[ 'total' ] ) { $anCount[ 'total' ] = $anCount[ 'blocked' ]; } if ( $anCount[ 'history' ][0][ 'blocked' ] > $anCount[ 'history' ][0][ 'total' ] ) { $anCount[ 'history' ][0][ 'total' ] = $anCount[ 'history' ][0][ 'blocked' ]; } //update db update_option( 'adblocker_notify_counter', $anCount ); } if ( empty($anCount[ 'total' ] ) ) $anCount[ 'total' ] = 0; if ( empty($anCount[ 'history' ][0][ 'total' ] ) ) $anCount[ 'history' ][0][ 'total' ] = 0; if ( empty($anCount[ 'blocked' ] ) ) $anCount[ 'blocked' ] = 0; if ( empty($anCount[ 'history' ][0][ 'blocked' ] ) ) $anCount[ 'history' ][0][ 'blocked' ] = 0; if ( empty($anCount[ 'deactivated' ] ) ) $anCount[ 'deactivated' ] = 0; $totalNoBlocker = $anCount[ 'total' ] - $anCount[ 'blocked' ]; $average = 0; if ( $anCount[ 'total' ] != 0 ) { $average = round( ( $anCount[ 'blocked' ] / $anCount[ 'total' ] ) * 100, 2 ); } $totalNoBlockerToday = $anCount[ 'history' ][0][ 'total' ] - $anCount[ 'history' ][0][ 'blocked' ]; $averageToday = 0; if ( $anCount[ 'total' ] != 0 ) { $averageToday = round( ( $anCount[ 'history' ][0][ 'blocked' ] / $anCount[ 'history' ][0][ 'total' ] ) * 100, 2 ); } $output = '
' . __( 'Total', 'an-translate' ) . ' ' . __( 'Today', 'an-translate' ) . '
' . __( 'Pages Views', 'an-translate' ) . ' ' . $anCount[ 'total' ] . ' ' . $anCount[ 'history' ][0][ 'total' ] . '
' . __( 'Pages with Adblock', 'an-translate' ) . ' ' . $anCount[ 'blocked' ] . ' ' . $anCount[ 'history' ][0][ 'blocked' ] . '
' . __( 'Total', 'an-translate' ) . '' . $average . '%' . __( 'Ads blocked', 'an-translate' ) . '
' . __( 'Today', 'an-translate' ) . '' . $averageToday . '%' . __( 'Ads blocked', 'an-translate' ) . '

' . $anCount[ 'deactivated' ] . ' ' . __( 'Ad Blocker software deactivated', 'an-translate' ) . '

'; $output .= ''; echo $output; }