getOption( 'an_option_choice' );
$anOptionCookie = $an_option->getOption( 'an_option_cookie' );
$anOptionCookieLife = $an_option->getOption( 'an_option_cookie_life' );
$anModalTitle = $an_option->getOption( 'an_modal_title' );
$anModalText = $an_option->getOption( 'an_modal_text' );
$anPageRedirect = $an_option->getOption( 'an_page_redirect' );
$anPageNojsActivation = $an_option->getOption( 'an_page_nojs_activation' );
$anPageNojsRedirect = $an_option->getOption( 'an_page_nojs_redirect' );
//Modal Options
$anOptionModalEffect = $an_option->getOption( 'an_option_modal_effect' );
$anOptionModalSpeed = $an_option->getOption( 'an_option_modal_speed' );
$anOptionModalClose = $an_option->getOption( 'an_option_modal_close' );
$anOptionModalBgcolor = $an_option->getOption( 'an_option_modal_bgcolor' );
$anOptionModalBgopacity = $an_option->getOption( 'an_option_modal_bgopacity' );
$anOptionModalBxcolor = $an_option->getOption( 'an_option_modal_bxcolor' );
$anOptionModalBxtitle = $an_option->getOption( 'an_option_modal_bxtitle' );
$anOptionModalBxtext = $an_option->getOption( 'an_option_modal_bxtext' );
$anOptionModalCustomCSS = $an_option->getOption( 'an_option_modal_custom_css' );
//Modal Options
$anAlternativeActivation = $an_option->getOption( 'an_alternative_activation' );
$anAlternativeElement = $an_option->getOption( 'an_alternative_elements' );
$anAlternativeText = $an_option->getOption( 'an_alternative_text' );
$anAlternativeClone = $an_option->getOption( 'an_alternative_clone' );
$anAlternativeProperties = $an_option->getOption( 'an_alternative_properties' );
$anAlternativeCss = $an_option->getOption( 'an_alternative_custom_css' );
//remove cookie if deactivate
an_remove_cookie(AN_COOKIE, $anOptionCookie);
//redirect URL with JS
$anPermalink = an_url_redirect($anPageRedirect);
//Modal box effect
$anOptionModalEffect = an_modal_parameter($anOptionModalEffect);
//Modal box close
$anOptionModalClose = an_modal_close($anOptionModalClose);
//Style construct
//Overlay RGA color
$anOptionModalOverlay = hex2rgba($anOptionModalBgcolor, $anOptionModalBgopacity/100);
//DOM and Json
$output .= '
$anOptionChoice,
'anOptionCookie' => $anOptionCookie,
'anOptionCookieLife' => $anOptionCookieLife,
'anModalTitle' => $anModalTitle,
'anModalText' => do_shortcode($anModalText),
'anPageRedirect' => $anPageRedirect,
'anPermalink' => $anPermalink,
'anOptionModalEffect' => $anOptionModalEffect,
'anOptionModalspeed' => $anOptionModalSpeed,
'anOptionModalclose' => $anOptionModalClose,
'anOptionModalOverlay' => $anOptionModalOverlay,
'anOptionModalBxtitle' => $anOptionModalBxtitle,
'anAlternativeActivation' => $anAlternativeActivation,
'anAlternativeElement' => $anAlternativeElement,
'anAlternativeText' => do_shortcode($anAlternativeText),
'anAlternativeClone' => $anAlternativeClone,
'anAlternativeProperties' => $anAlternativeProperties,
'anGetUserIpAdress'=> $anGetUserIpAdress,
));
$output .= '/* ]]> */';
$output .= '';
//NO JS Redirect
if(!empty($anPageNojsActivation) && !$_COOKIE[AN_COOKIE]) {
//redirect URL with NO JS
$anNojsPermalink = an_url_redirect($anPageNojsRedirect);
if($anNojsPermalink != "undefined"){
//Set new cookie value
an_nojs_cookie($expiration);
$output .= '
';
}
}
echo $output;
}
add_action('wp_footer', 'an_prepare');
/***************************************************************
* Deregister custom stylesheet if option is empty
***************************************************************/
function an_deregister_styles() {
$anBlockerNotify = unserialize(get_option( 'adblocker_notify_options'));
if(empty($anBlockerNotify['an_option_modal_custom_css']) && empty($anBlockerNotify['an_alternative_custom_css'])){
wp_deregister_style( 'tf-compiled-options-adblocker_notify' );
}
}
add_action( 'wp_print_styles', 'an_deregister_styles', 100);
/***************************************************************
* Append fake ad to Wordpress content
***************************************************************/
function an_fake_an_content($content) {
$content .= '';
return $content;
}
add_filter( 'the_content', 'an_fake_an_content' );
/***************************************************************
* Generate redirection URL with page ID
***************************************************************/
function an_url_redirect($pageId){
if ( is_main_query() ) {
$currentPage = get_queried_object_id();
} else {
global $wp;
$current_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
$currentPage = url_to_postid($current_url);
}
if(!empty($pageId) && $pageId != $currentPage){
$anPermalink = get_permalink( $pageId );
} else {
$anPermalink = "undefined";
}
return $anPermalink;
}
/***************************************************************
* Remove cookie when option is disabled
***************************************************************/
function an_remove_cookie($cookieName, $anOptionCookie){
if (isset($_COOKIE[$cookieName]) && $anOptionCookie != 1) {
unset($_COOKIE[$cookieName]);
setcookie($cookieName, null, -1, '/');
}
}
/***************************************************************
* Restart cookie on every options save.
***************************************************************/
function an_restart_cookie(){
if (isset($_COOKIE[AN_COOKIE])) {
unset($_COOKIE[AN_COOKIE]);
setcookie(AN_COOKIE, null, -1, '/');
}
}
add_action('tf_admin_options_saved_adblocker_notify', 'an_restart_cookie');
/***************************************************************
* Set cookie for No JS redirection.
***************************************************************/
function an_nojs_cookie($expiration){
$expiration = time()+($expiration*24*60*60);
if (!isset($_COOKIE[AN_COOKIE])) {
setcookie(AN_COOKIE, true, $expiration, '/');
}
}
/***************************************************************
* Modal Box effect parameter
***************************************************************/
function an_modal_parameter($key){
switch ($key) {
case '':
case 1:
$key = 'fadeAndPop';
break;
case 2:
$key = 'fade';
break;
case 3:
$key = 'none';
break;
default :
$key = 'fadeAndPop';
break;
}
return $key;
}
/***************************************************************
* Modal Boxe closing option
***************************************************************/
function an_modal_close($key){
switch ($key) {
case '':
case 1:
$key = true;
break;
case 2:
$key = false;
break;
default :
$key = true;
break;
}
return $key;
}
/***************************************************************
* Convert hexdec color string to rgb(a) string
* Src: http://mekshq.com/how-to-convert-hexadecimal-color-code-to-rgb-or-rgba-using-php/
***************************************************************/
function hex2rgba($color, $opacity = false) {
$default = 'rgb(0,0,0)';
//Return default if no color provided
if(empty($color))
return $default;
//Sanitize $color if "#" is provided
if ($color[0] == '#' ) {
$color = substr( $color, 1 );
}
//Check if color has 6 or 3 characters and get values
if (strlen($color) == 6) {
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
} elseif ( strlen( $color ) == 3 ) {
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
} else {
return $default;
}
//Convert hexadec to rgb
$rgb = array_map('hexdec', $hex);
//Check if opacity is set(rgba or rgb)
if($opacity){
if(abs($opacity) > 1)
$opacity = 1.0;
$output = 'rgba('.implode(",",$rgb).','.$opacity.')';
} else {
$output = 'rgb('.implode(",",$rgb).')';
}
//Return rgb(a) color string
return $output;
}
/***************************************************************
* Page views & page blocked counter
***************************************************************/
function an_adblock_counter() {
if ( current_user_can( 'manage_options' ) || empty ($_POST['an_state']))
return;
$anCount = get_option('adblocker_notify_counter');
if(empty($anCount)){
add_option('adblocker_notify_counter', array('total' => 0, 'blocked' => 0, 'deactivated' => 0, 'history' => array()));
$anCount = get_option('adblocker_notify_counter');
}
$an_state = $_POST['an_state'];
//update option with new values
$anCount[$an_state]++;
//update db
update_option('adblocker_notify_counter', $anCount);
//then update history
an_history_counter($an_state);
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($val=null) {
$anCount = get_option( 'adblocker_notify_counter' );
$anToday = date( 'Y-m-d', current_time( 'timestamp', 0 ) );
//$anToday = date( 'Y-m-d', strtotime( '3 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']);
update_option('adblocker_notify_counter', $anCount);
} 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)
$anRemove = array_pop($anCount['history']);
}
}
}
update_option('adblocker_notify_counter', $anCount);
}
}
/***************************************************************
* Data history extraction & order revert for chart
***************************************************************/
function an_data_histoty($val=null){
$anCount = get_option( 'adblocker_notify_counter' );
if(empty($anCount['history'][0]))
return;
foreach($anCount['history'] as $row){
$anOutput .= $row[$val].',';
}
$anOutput = trim($anOutput, ",");
return $anOutput;
}
/***************************************************************
* Display the Dashboard Widget
***************************************************************/
function an_get_counters() {
$anCount = get_option('adblocker_notify_counter');
//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['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'];
if($anCount['total'] != 0){
$average = round(($anCount['blocked']/$anCount['total'])*100,2);
} else {
$average = 0;
}
$totalNoBlockerToday = $anCount['history'][0]['total'] - $anCount['history'][0]['blocked'];
if($anCount['total'] != 0){
$averageToday = round(($anCount['history'][0]['blocked']/$anCount['history'][0]['total'])*100,2);
} else {
$averageToday = 0;
}
$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' ) .'
'. __( 'You may probably increase this number by improving your custom messages', 'an-translate' ) .'
'. __( 'Settings', 'an-translate' ) .'
   
'. __( 'Reset Stats', 'an-translate' ) .'
'. __( 'Admins are excluded from this statistics.', 'an-translate' ) .'
';
echo $output;
}
/***************************************************************
* Register the Dashboard Widget display function
***************************************************************/
function an_dashboard_widgets() {
global $wp_meta_boxes;
wp_add_dashboard_widget('an_dashboard_widgets', 'Adblock Notify Stats', 'an_get_counters');
}
add_action('wp_dashboard_setup', 'an_dashboard_widgets');
/***************************************************************
* Reset plugin options
***************************************************************/
function an_stats_notice() {
echo '
Adblock Notify stats have been successfully cleared.
';
}
function an_reset_stats() {
$screen = get_current_screen();
if ( $screen->id != 'toplevel_page_'. AN_ID )
return;
if($_GET['an-reset'] == 'true'){
delete_option( 'adblocker_notify_counter' );
add_action( 'admin_notices', 'an_stats_notice' );
}
}
add_filter('admin_head', 'an_reset_stats' );