get_option( 'hits_affiliate_from_date', null );
$thru_date = $affiliates_options->get_option( 'hits_affiliate_thru_date', null );
$affiliate_id = $affiliates_options->get_option( 'hits_affiliate_affiliate_id', null );
$expanded = $affiliates_options->get_option( 'hits_affiliate_expanded', null ); // @todo input ist not shown, eventually remove unless ...
$expanded_referrals = $affiliates_options->get_option( 'hits_affiliate_expanded_referrals', null );
$expanded_hits = $affiliates_options->get_option( 'hits_affiliate_expanded_hits', null );
$show_inoperative = $affiliates_options->get_option( 'hits_affiliate_show_inoperative', null );
if ( isset( $_POST['clear_filters'] ) ) {
$affiliates_options->delete_option( 'hits_affiliate_from_date' );
$affiliates_options->delete_option( 'hits_affiliate_thru_date' );
$affiliates_options->delete_option( 'hits_affiliate_affiliate_id' );
$affiliates_options->delete_option( 'hits_affiliate_expanded' );
$affiliates_options->delete_option( 'hits_affiliate_expanded_referrals' );
$affiliates_options->delete_option( 'hits_affiliate_expanded_hits' );
$affiliates_options->delete_option( 'hits_affiliate_show_inoperative' );
$from_date = null;
$thru_date = null;
$affiliate_id = null;
$expanded = null;
$expanded_hits = null;
$expanded_referrals = null;
$show_inoperative = null;
} else {
// filter by date(s)
if ( !empty( $_POST['from_date'] ) ) {
$from_date = date( 'Y-m-d', strtotime( $_POST['from_date'] ) );
$affiliates_options->update_option( 'hits_affiliate_from_date', $from_date );
}
if ( !empty( $_POST['thru_date'] ) ) {
$thru_date = date( 'Y-m-d', strtotime( $_POST['thru_date'] ) );
$affiliates_options->update_option( 'hits_affiliate_thru_date', $thru_date );
}
if ( $from_date && $thru_date ) {
if ( strtotime( $from_date ) > strtotime( $thru_date ) ) {
$thru_date = null;
$affiliates_options->delete_option( 'hits_affiliate_thru_date' );
}
}
// We now have the desired dates from the user's point of view, i.e. in her timezone.
// If supported, adjust the dates for the site's timezone:
if ( $from_date ) {
$from_datetime = DateHelper::u2s( $from_date );
}
if ( $thru_date ) {
$thru_datetime = DateHelper::u2s( $thru_date, 24*3600 );
}
// filter by affiliate id
if ( !empty( $_POST['affiliate_id'] ) ) {
$affiliate_id = affiliates_check_affiliate_id( $_POST['affiliate_id'] );
if ( $affiliate_id ) {
$affiliates_options->update_option( 'hits_affiliate_affiliate_id', $affiliate_id );
}
} else if ( isset( $_POST['affiliate_id'] ) ) { // empty && isset => '' => all
$affiliate_id = null;
$affiliates_options->delete_option( 'hits_affiliate_affiliate_id' );
}
// expanded details?
if ( !empty( $_POST['submitted'] ) ) {
if ( !empty( $_POST['expanded'] ) ) {
$expanded = true;
$affiliates_options->update_option( 'hits_affiliate_expanded', true );
} else {
$expanded = false;
$affiliates_options->delete_option( 'hits_affiliate_expanded' );
}
if ( !empty( $_POST['expanded_hits'] ) ) {
$expanded_hits = true;
$affiliates_options->update_option( 'hits_affiliate_expanded_hits', true );
} else {
$expanded_hits = false;
$affiliates_options->delete_option( 'hits_affiliate_expanded_hits' );
}
if ( !empty( $_POST['expanded_referrals'] ) ) {
$expanded_referrals = true;
$affiliates_options->update_option( 'hits_affiliate_expanded_referrals', true );
} else {
$expanded_referrals = false;
$affiliates_options->delete_option( 'hits_affiliate_expanded_referrals' );
}
if ( !empty( $_POST['show_inoperative'] ) ) {
$show_inoperative = true;
$affiliates_options->update_option( 'hits_affiliate_show_inoperative', true );
} else {
$show_inoperative = false;
$affiliates_options->delete_option( 'hits_affiliate_show_inoperative' );
}
}
}
if ( isset( $_POST['row_count'] ) ) {
if ( !wp_verify_nonce( $_POST[AFFILIATES_ADMIN_HITS_AFF_NONCE_1], plugin_basename( __FILE__ ) ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
}
if ( isset( $_POST['paged'] ) ) {
if ( !wp_verify_nonce( $_POST[AFFILIATES_ADMIN_HITS_AFF_NONCE_2], plugin_basename( __FILE__ ) ) ) {
wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) );
}
}
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$current_url = remove_query_arg( 'paged', $current_url );
$affiliates_table = _affiliates_get_tablename( 'affiliates' );
$referrals_table = _affiliates_get_tablename( 'referrals' );
$hits_table = _affiliates_get_tablename( 'hits' );
echo
'
' .
'
' .
__( 'Affiliates & Referrals', AFFILIATES_PLUGIN_DOMAIN ) .
'
' .
'';
$row_count = intval( $_POST['row_count'] );
if ($row_count <= 0) {
$row_count = $affiliates_options->get_option( 'hits_affiliate_per_page', AFFILIATES_HITS_AFFILIATE_PER_PAGE );
} else {
$affiliates_options->update_option('hits_affiliate_per_page', $row_count );
}
$offset = intval( $_GET['offset'] );
if ( $offset < 0 ) {
$offset = 0;
}
$paged = intval( $_GET['paged'] );
if ( $paged < 0 ) {
$paged = 0;
}
$orderby = $_GET['orderby'];
switch ( $orderby ) {
case 'date' :
case 'visits' :
case 'hits' :
case 'referrals' :
case 'ratio' :
case 'name' :
break;
default:
$orderby = 'name';
}
$order = $_GET['order'];
switch ( $order ) {
case 'asc' :
case 'ASC' :
$switch_order = 'DESC';
break;
case 'desc' :
case 'DESC' :
$switch_order = 'ASC';
break;
default:
$order = 'ASC';
$switch_order = 'DESC';
}
if ( $from_date || $thru_date || $affiliate_id ) {
$filters = " WHERE ";
} else {
$filters = '';
}
$filter_params = array();
if ( $from_date && $thru_date ) {
$filters .= " datetime >= %s AND datetime < %s ";
$filter_params[] = $from_datetime;
$filter_params[] = $thru_datetime;
} else if ( $from_date ) {
$filters .= " datetime >= %s ";
$filter_params[] = $from_datetime;
} else if ( $thru_date ) {
$filters .= " datetime < %s ";
$filter_params[] = $thru_datetime;
}
if ( $affiliate_id ) {
if ( $from_date || $thru_date ) {
$filters .= " AND ";
}
$filters .= " h.affiliate_id = %d ";
$filter_params[] = $affiliate_id;
}
// how many are there ?
$count_query = $wpdb->prepare(
"SELECT affiliate_id FROM $hits_table h
$filters
GROUP BY affiliate_id
",
$filter_params
);
$wpdb->query( $count_query );
$count = $wpdb->num_rows;
if ( $count > $row_count ) {
$paginate = true;
} else {
$paginate = false;
}
$pages = ceil ( $count / $row_count );
if ( $paged > $pages ) {
$paged = $pages;
}
if ( $paged != 0 ) {
$offset = ( $paged - 1 ) * $row_count;
}
// Get the summarized results, these are grouped by date.
// Note: Referrals on dates without a hit will not be included.
// @see notes about this in affiliates_admin_hits()
$query = $wpdb->prepare("
SELECT
*,
count(distinct ip) visits,
sum(count) hits,
(select count(*) from wp_aff_referrals where affiliate_id = h.affiliate_id ) referrals,
((select count(*) from wp_aff_referrals where affiliate_id = h.affiliate_id )/count(distinct ip)) ratio
FROM $hits_table h
LEFT JOIN $affiliates_table a ON h.affiliate_id = a.affiliate_id
$filters
GROUP BY h.affiliate_id
ORDER BY $orderby $order
LIMIT $row_count OFFSET $offset
",
$filter_params
);
$results = $wpdb->get_results( $query, OBJECT );
$column_display_names = array(
'name' => __( 'Affiliate', AFFILIATES_PLUGIN_DOMAIN ),
'visits' => __( 'Visitors', AFFILIATES_PLUGIN_DOMAIN ),
'hits' => __( 'Hits', AFFILIATES_PLUGIN_DOMAIN ),
'referrals' => __( 'Referrals', AFFILIATES_PLUGIN_DOMAIN ),
'ratio' => __( 'Ratio', AFFILIATES_PLUGIN_DOMAIN )
);
$output .= '';
$affiliates = affiliates_get_affiliates( true, !$show_inoperative );
if ( !empty( $affiliates ) ) {
$affiliates_select .= '
';
$affiliates_select .= '
';
}
$output .=
'
' .
'
' .
'
' .
'
';
$output .= '
';
if ( $paginate ) {
require_once(dirname( __FILE__ ) . '/class-affiliates-pagination.php' );
$pagination = new Affiliates_Pagination($count, $paged, $row_count);
$output .= '
';
}
$output .= '
';
foreach ( $column_display_names as $key => $column_display_name ) {
$options = array(
'orderby' => $key,
'order' => $switch_order
);
$class = "";
if ( strcmp( $key, $orderby ) == 0 ) {
$lorder = strtolower( $order );
$class = "$key manage-column sorted $lorder";
} else {
$class = "$key manage-column sortable";
}
$column_display_name = '' . $column_display_name . '';
$output .= "| $column_display_name | ";
}
$output .= '
';
if ( count( $results ) > 0 ) {
for ( $i = 0; $i < count( $results ); $i++ ) {
$result = $results[$i];
$output .= '';
$affiliate = affiliates_get_affiliate( $result->affiliate_id );
$output .= "| " . stripslashes( wp_filter_nohtml_kses( $affiliate['name'] ) ) . " | ";
$output .= "$result->visits | ";
$output .= "$result->hits | ";
$output .= "$result->referrals | ";
$output .= "$result->ratio | ";
$output .= '
';
if ( $expanded || $expanded_referrals || $expanded_hits ) {
//
// expanded : referrals ----------------------------------------
//
if ( $expanded_referrals ) {
// get the detailed results for referrals
$referrals_filters = " WHERE r.affiliate_id = %d ";
$referrals_filter_params = array( $result->affiliate_id );
if ( $from_date && $thru_date ) {
$referrals_filters .= " AND datetime >= %s AND datetime < %s ";
$referrals_filter_params[] = $from_datetime;
$referrals_filter_params[] = $thru_datetime;
} else if ( $from_date ) {
$referrals_filters .= " AND datetime >= %s ";
$referrals_filter_params[] = $from_datetime;
} else if ( $thru_date ) {
$referrals_filters .= " datetime < %s ";
$referrals_filter_params[] = $thru_datetime;
}
$referrals_orderby = "datetime $order";
$referrals_query = $wpdb->prepare(
"SELECT *
FROM $referrals_table r
LEFT JOIN $affiliates_table a ON r.affiliate_id = a.affiliate_id
$referrals_filters
ORDER BY $referrals_orderby
",
$referrals_filter_params
);
$referrals = $wpdb->get_results( $referrals_query, OBJECT );
if ( count($referrals) > 0 ) {
$output .= '';
$output .= '';
$output .= '';
$output .= ' ' . __( 'Referrals', AFFILIATES_PLUGIN_DOMAIN ) . ' ';
$output .= '
| ' . __( 'Time', AFFILIATES_PLUGIN_DOMAIN ) . ' |
' . __( 'Post', AFFILIATES_PLUGIN_DOMAIN ) . ' |
' . __( 'Affiliate', AFFILIATES_PLUGIN_DOMAIN ) . ' |
';
foreach ( $referrals as $referral ) {
$output .= '';
$output .= "| " . DateHelper::s2u( $referral->datetime ) . " | ";
$link = get_permalink( $referral->post_id );
$title = get_the_title( $referral->post_id );
$output .= '' . stripslashes( wp_filter_nohtml_kses( $title ) ) . ' | ';
$output .= "" . stripslashes( wp_filter_nohtml_kses( $referral->name ) ) . " | ";
$output .= ' ';
}
$output .= ' ';
$output .= ' '; // .details-referrals
$output .= ' |
';
}
} // if $expanded_referrals
//
// expanded : hits ----------------------------------------
//
if ( $expanded_hits ) {
// get the detailed results for hits
$details_orderby = "date $order, time $order";
$details_filters = " WHERE h.affiliate_id = %d ";
$details_filter_params = array( $result->affiliate_id );
if ( $from_date && $thru_date ) {
$details_filters .= " AND datetime >= %s AND datetime < %s ";
$details_filter_params[] = $from_datetime;
$details_filter_params[] = $thru_datetime;
} else if ( $from_date ) {
$details_filters .= " AND datetime >= %s ";
$details_filter_params[] = $from_datetime;
} else if ( $thru_date ) {
$details_filters .= " datetime < %s ";
$details_filter_params[] = $thru_datetime;
}
$details_query = $wpdb->prepare(
"SELECT *
FROM $hits_table h
LEFT JOIN $affiliates_table a ON h.affiliate_id = a.affiliate_id
$details_filters
ORDER BY $details_orderby
",
$details_filter_params
);
$hits = $wpdb->get_results( $details_query, OBJECT );
$output .= '';
$output .= '';
$output .= '';
$output .= ' ' . __( 'Hits', AFFILIATES_PLUGIN_DOMAIN ) . ' ';
$output .= '
| ' . __( 'Date', AFFILIATES_PLUGIN_DOMAIN ) . ' |
' . __( 'Time', AFFILIATES_PLUGIN_DOMAIN ) . ' |
' . __( 'IP', AFFILIATES_PLUGIN_DOMAIN ) . ' |
' . __( 'Count', AFFILIATES_PLUGIN_DOMAIN ) . ' |
' . __( 'Affiliate', AFFILIATES_PLUGIN_DOMAIN ) . ' |
';
foreach ( $hits as $hit ) {
$output .= '';
// $output .= "| $hit->date | ";
$output .= '' . DateHelper::formatDate( DateHelper::s2u( $hit->datetime ) ) . ' | ';
// $output .= "$hit->time | ";
$output .= '' . DateHelper::formatTime( DateHelper::s2u( $hit->datetime ) ) . ' | ';
$output .= "" . long2ip( $hit->ip ) . " | ";
$output .= "$hit->count | ";
$output .= "" . stripslashes( wp_filter_nohtml_kses( $hit->name ) ) . " | ";
$output .= ' ';
}
$output .= ' ';
$output .= ' '; // .details-hits
$output .= ' |
';
} // if $expanded_hits
} // expanded
}
} else {
$output .= '| ' . __('There are no results.', AFFILIATES_PLUGIN_DOMAIN ) . ' |
';
}
$output .= '';
$output .= '
';
if ( $paginate ) {
require_once(dirname( __FILE__ ) . '/class-affiliates-pagination.php' );
$pagination = new Affiliates_Pagination($count, null, $row_count);
$output .= '
';
$output .= $pagination->pagination( 'bottom' );
$output .= '
';
}
$output .= '
'; // .visits-overview
echo $output;
affiliates_footer();
} // function affiliates_admin_hits()
?>