getOption( 'an_option_stats' ) != 2 ) { wp_add_dashboard_widget( 'an_dashboard_widgets', '  ' . __( 'Ad Blocker Notify Stats', 'an-translate' ), 'an_get_counters' ); // Chart JS wp_enqueue_script( 'an_chart_js', AN_URL . 'vendor/chart-js/Chart.min.js', array( 'jquery' ), null ); // CSS & JS add_action( 'admin_footer', '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'] ) || an_check_views() ) { return; } $an_states = $_POST['an_state'] ; $anCount = an_get_option( 'adblocker_notify_counter' ); foreach ( $an_states as $an_state ) { $an_state = sanitize_text_field( $an_state ); if ( empty( $anCount ) ) { $anCount = array( 'total' => 0, 'blocked' => 0, 'deactivated' => 0, 'history' => array() ); an_update_option( 'adblocker_notify_counter', $anCount ); } if ( $an_state === 'blocked' ) { $views_counter = an_get_option( 'adblock_notify_global_counter' ); $views_counter = intval( $views_counter ); $views_counter ++ ; an_update_option( 'adblock_notify_global_counter',$views_counter ); } // update option with new values $anCount[ $an_state ] ++; // then update history $anCount = an_history_counter( $anCount, $an_state ); } // update db an_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 ( 'total' == $val ) { $anCount['history'][0]['total'] = $anCount['history'][0]['total'] + 1; } elseif ( 'blocked' == $val ) { $anCount['history'][0]['blocked'] = $anCount['history'][0]['blocked'] + 1; } } elseif ( $anDiff > 0 ) { // remove last + add new one if ( 'total' == $val ) { $anNew = array( 'date' => $anToday, 'total' => 1, 'blocked' => 0 ); } elseif ( 'blocked' == $val ) { $anNew = array( 'date' => $anToday, 'total' => 1, 'blocked' => 1 ); } $anCount['history'] = array_merge( array( $anNew ), $anCount['history'] ); if ( 8 == count( $anCount['history'] ) ) { $anOld = an_date_diff( $anToday, $anCount['history'][7]['date'] ); if ( 7 == $anOld && 8 == count( $anCount['history'] ) ) { // 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 = an_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 an_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 ); } require_once( ABSPATH . 'wp-admin/includes/file.php' ); // Load WP_Filesystem API WP_Filesystem(); global $wp_filesystem; $css = $wp_filesystem->get_contents( AN_PATH . 'css/an-dashboard-widget.css' ); $output = "'; $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; }