get_option( 'hits_from_date', null );
$thru_date = $affiliates_options->get_option( 'hits_thru_date', null );
$affiliate_id = $affiliates_options->get_option( 'hits_affiliate_id', null );
$expanded = $affiliates_options->get_option( 'hits_expanded', null ); // @todo input ist not shown, eventually remove unless ...
$expanded_referrals = $affiliates_options->get_option( 'hits_expanded_referrals', null );
$expanded_hits = $affiliates_options->get_option( 'hits_expanded_hits', null );
$show_inoperative = $affiliates_options->get_option( 'hits_show_inoperative', null );
if ( isset( $_POST['clear_filters'] ) ) {
$affiliates_options->delete_option( 'hits_from_date' );
$affiliates_options->delete_option( 'hits_thru_date' );
$affiliates_options->delete_option( 'hits_affiliate_id' );
$affiliates_options->delete_option( 'hits_expanded' );
$affiliates_options->delete_option( 'hits_expanded_referrals' );
$affiliates_options->delete_option( 'hits_expanded_hits' );
$affiliates_options->delete_option( 'hits_show_inoperative' );
$from_date = null;
$thru_date = null;
$affiliate_id = null;
$expanded = null;
$expanded_hits = null;
$expanded_referrals = null;
$show_inoperative = null;
} else if ( isset( $_POST['submitted'] ) ) {
// filter by date(s)
if ( !empty( $_POST['from_date'] ) ) {
$from_date = date( 'Y-m-d', strtotime( $_POST['from_date'] ) );
$affiliates_options->update_option( 'hits_from_date', $from_date );
} else {
$from_date = null;
$affiliates_options->delete_option( 'hits_from_date' );
}
if ( !empty( $_POST['thru_date'] ) ) {
$thru_date = date( 'Y-m-d', strtotime( $_POST['thru_date'] ) );
$affiliates_options->update_option( 'hits_thru_date', $thru_date );
} else {
$thru_date = null;
$affiliates_options->delete_option( 'hits_thru_date' );
}
if ( $from_date && $thru_date ) {
if ( strtotime( $from_date ) > strtotime( $thru_date ) ) {
$thru_date = null;
$affiliates_options->delete_option( 'hits_thru_date' );
}
}
// 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_id', $affiliate_id );
}
} else if ( isset( $_POST['affiliate_id'] ) ) { // empty && isset => '' => all
$affiliate_id = null;
$affiliates_options->delete_option( 'hits_affiliate_id' );
}
// expanded details?
if ( !empty( $_POST['expanded'] ) ) {
$expanded = true;
$affiliates_options->update_option( 'hits_expanded', true );
} else {
$expanded = false;
$affiliates_options->delete_option( 'hits_expanded' );
}
if ( !empty( $_POST['expanded_hits'] ) ) {
$expanded_hits = true;
$affiliates_options->update_option( 'hits_expanded_hits', true );
} else {
$expanded_hits = false;
$affiliates_options->delete_option( 'hits_expanded_hits' );
}
if ( !empty( $_POST['expanded_referrals'] ) ) {
$expanded_referrals = true;
$affiliates_options->update_option( 'hits_expanded_referrals', true );
} else {
$expanded_referrals = false;
$affiliates_options->delete_option( 'hits_expanded_referrals' );
}
if ( !empty( $_POST['show_inoperative'] ) ) {
$show_inoperative = true;
$affiliates_options->update_option( 'hits_show_inoperative', true );
} else {
$show_inoperative = false;
$affiliates_options->delete_option( 'hits_show_inoperative' );
}
}
if ( isset( $_POST['row_count'] ) ) {
if ( !affiliates_verify_post_nonce( AFFILIATES_ADMIN_HITS_NONCE_1, 'admin' ) ) {
wp_die( esc_html__( 'Access denied.', 'affiliates' ) );
}
}
if ( isset( $_POST['paged'] ) ) {
if ( !affiliates_verify_post_nonce( AFFILIATES_ADMIN_HITS_NONCE_2, 'admin' ) ) {
wp_die( esc_html__( 'Access denied.', 'affiliates' ) );
}
}
$current_url = affiliates_get_current_url();
$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' );
$output .= '
';
$output .= __( 'Visits & Referrals', 'affiliates' );
$output .= ' ';
$row_count = isset( $_POST['row_count'] ) ? intval( $_POST['row_count'] ) : 0;
if ($row_count <= 0) {
$row_count = $affiliates_options->get_option( 'affiliates_hits_per_page', AFFILIATES_HITS_PER_PAGE );
} else {
$affiliates_options->update_option('affiliates_hits_per_page', $row_count );
}
// current page
$paged = isset( $_REQUEST['paged'] ) ? intval( $_REQUEST['paged'] ) : 1;
if ( $paged < 1 ) {
$paged = 1;
}
$orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null;
switch ( $orderby ) {
case 'date' :
case 'visits' :
case 'hits' :
case 'referrals' :
case 'ratio' :
break;
default:
$orderby = 'date';
}
$order = isset( $_GET['order'] ) ? $_GET['order'] : null;
switch ( $order ) {
case 'asc' :
case 'ASC' :
$switch_order = 'DESC';
break;
case 'desc' :
case 'DESC' :
$switch_order = 'ASC';
break;
default:
$order = 'DESC';
$switch_order = 'ASC';
}
$filters = " WHERE 1=%d ";
$filter_params = array( 1 );
// 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:
$u2s_from_date = $from_date ? date( 'Y-m-d', strtotime( DateHelper::u2s( $from_date ) ) ) : null;
$u2s_thru_date = $thru_date ? date( 'Y-m-d', strtotime( DateHelper::u2s( $thru_date ) ) ) : null;
if ( $u2s_from_date && $u2s_thru_date ) {
$filters .= " AND date >= %s AND date <= %s ";
$filter_params[] = $u2s_from_date;
$filter_params[] = $u2s_thru_date;
} else if ( $u2s_from_date ) {
$filters .= " AND date >= %s ";
$filter_params[] = $u2s_from_date;
} else if ( $u2s_thru_date ) {
$filters .= " AND date <= %s ";
$filter_params[] = $u2s_thru_date;
}
if ( $affiliate_id ) {
$filters .= " AND affiliate_id = %d ";
$filter_params[] = $affiliate_id;
}
// @since 4.10.0 correct referrals count requires filters to be taken into account
$referrals_subquery_where = '';
$referrals_subquery_conditions = array();
$referrals_subquery_params = array();
if ( $affiliate_id ) {
$referrals_subquery_conditions[] = "affiliate_id = %d";
$referrals_subquery_params[] = $affiliate_id;
}
if ( $u2s_from_date && $u2s_thru_date ) {
$referrals_subquery_conditions[] = " datetime >= %s AND datetime <= %s ";
$referrals_subquery_params[] = $u2s_from_date . ' 00:00:00';
$referrals_subquery_params[] = $u2s_thru_date . ' 23:59:59';
} else if ( $u2s_from_date ) {
$referrals_subquery_conditions[] = " datetime >= %s ";
$referrals_subquery_params[] = $u2s_from_date . ' 00:00:00';
} else if ( $u2s_thru_date ) {
$referrals_subquery_conditions[] = " datetime <= %s ";
$referrals_subquery_params[] = $u2s_thru_date . ' 23:59:59';
}
if ( count( $referrals_subquery_conditions ) > 0 ) {
$referrals_subquery_where = ' WHERE ' . implode( ' AND ', $referrals_subquery_conditions );
$referrals_subquery_where = $wpdb->prepare( $referrals_subquery_where, $referrals_subquery_params ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
}
do {
$repeat = false;
$offset = ( $paged - 1 ) * $row_count;
// Get the summarized results, these are grouped by date.
// If there were any referral on a date without a hit, it would not be included:
// Example conditions:
// - 2011-02-01 23:59:59 hit recorded
// - 2011-02-02 00:10:05 referral recorded
// - no hits recorded on 2011-02-02
// =>
// - the referral will not show up
// So, for ratio calculation, only the date with actual visits and referrals will show up.
// Referrals on dates without visits would give an infinite ratio (x referrals / 0 visits).
// We have a separate page which shows all referrals.
$query = $wpdb->prepare(
"SELECT SQL_CALC_FOUND_ROWS " .
"hits.date, " .
"hits.hits, " .
"hits.visits, " .
"IF ( referrals.count IS NOT NULL, referrals.count, 0 ) AS referrals, " .
"IF ( referrals.count IS NOT NULL AND hits.visits > 0, referrals.count / hits.visits, 0 ) AS ratio " .
"FROM " .
"( SELECT date, COUNT(DISTINCT ip) AS visits, COUNT(*) AS hits FROM $hits_table $filters GROUP BY date ) AS hits " .
"LEFT JOIN ( SELECT COUNT(*) AS count, date(datetime) AS date FROM $referrals_table $referrals_subquery_where GROUP BY date(datetime) ) AS referrals ON hits.date = referrals.date " .
"ORDER BY $orderby $order " .
"LIMIT $row_count OFFSET $offset",
$filter_params
);
$results = $wpdb->get_results( $query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$count = intval( $wpdb->get_var( "SELECT FOUND_ROWS()" ) );
if ( $count > $row_count ) {
$paginate = true;
} else {
$paginate = false;
}
$pages = max( array( 1, ceil( $count / $row_count ) ) );
if ( $paged > $pages ) {
$paged = $pages;
$repeat = true;
}
} while ( $repeat );
$column_display_names = array(
'date' => __( 'Date', 'affiliates' ) . '*',
'visits' => __( 'Visits', 'affiliates' ),
'hits' => __( 'Hits', 'affiliates' ),
'referrals' => __( 'Referrals', 'affiliates' ),
'ratio' => __( 'Ratio', 'affiliates' )
);
$output .= '';
$affiliates_select = Affiliates_UI_Elements::affiliates_select(
array(
'name' => 'affiliate_id',
'class' => 'affiliate-id-filter',
'label-class' => 'affiliate-id-filter',
'affiliate_id' => $affiliate_id,
'show_inoperative' => $show_inoperative
)
);
$output .=
'
' .
'
' . __( 'Filters', 'affiliates' ) . ' ' .
'
' .
'
';
$output .= '
';
if ( $paginate ) {
require_once( AFFILIATES_CORE_LIB . '/class-affiliates-pagination.php' );
$pagination = new Affiliates_Pagination( $count, null, $row_count );
$output .= '
';
}
$output .= '
';
$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 = sprintf(
'%2$s ' .
'' .
' '.
' '.
' ' .
' ',
esc_url( add_query_arg( $options, $current_url ) ),
esc_html( $column_display_name )
);
$output .= "$column_display_name ";
}
$output .= ' ';
$output .= ' ';
$output .= '';
if ( count( $results ) > 0 ) {
for ( $i = 0; $i < count( $results ); $i++ ) {
$result = $results[$i];
$output .= '';
$output .= ''. esc_html( $result->date ) . ' ';
$output .= '' . esc_html( $result->visits ) . ' ';
$output .= '' . esc_html( $result->hits ) . ' ';
$output .= '' . esc_html( $result->referrals ) . ' ';
$output .= '' . esc_html( $result->ratio ) . ' ';
$output .= ' ';
if ( $expanded || $expanded_referrals || $expanded_hits ) {
//
// expanded : referrals ----------------------------------------
//
if ( $expanded_referrals ) {
$referrals_filters = " WHERE date(datetime) = %s ";
$referrals_filter_params = array( $result->date );
if ( $affiliate_id ) {
$referrals_filters .= " AND r.affiliate_id = %d ";
$referrals_filter_params[] = $affiliate_id;
}
$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 ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
if ( count( $referrals) > 0 ) {
$output .= '';
$output .= '';
$output .= '';
$output .= '
' . esc_html__( 'Referrals', 'affiliates' ) . '
';
$output .= '
';
$output .= '';
$output .= '';
$output .= '' . esc_html__( 'Date', 'affiliates' ) . ' ';
$output .= '' . esc_html__( 'Post', 'affiliates' ) . ' ';
$output .= '' . esc_html__( 'Affiliate', 'affiliates' ) . ' ';
$output .= ' ';
$output .= ' ';
$output .= '';
foreach ( $referrals as $referral ) {
$output .= '';
$output .= '' . esc_html( $referral->datetime ) . ' ';
$link = affiliates_get_referral_post_permalink( $referral );
$title = affiliates_get_referral_post_title( $referral );
$output .= '';
if ( !empty( $link ) ) {
$output .= sprintf(
'%s ',
esc_url( $link ),
stripslashes( wp_filter_nohtml_kses( $title ) )
);
} else {
$output .= stripslashes( wp_filter_nohtml_kses( $title ) );
}
$output .= ' ';
$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";
$details_filters = " WHERE h.date = %s ";
$details_filter_params = array( $result->date );
if ( $affiliate_id ) {
$details_filters .= " AND h.affiliate_id = %d ";
$details_filter_params[] = $affiliate_id;
}
$user_agents_table = _affiliates_get_tablename( 'user_agents' );
$uris_table = _affiliates_get_tablename( 'uris' );
$details_query = $wpdb->prepare(
"SELECT h.*, a.*, ua.*, src.uri src_uri, dest.uri dest_uri
FROM $hits_table h
LEFT JOIN $affiliates_table a ON h.affiliate_id = a.affiliate_id
LEFT JOIN $user_agents_table ua ON h.user_agent_id = ua.user_agent_id
LEFT JOIN $uris_table src ON h.src_uri_id = src.uri_id
LEFT JOIN $uris_table dest ON h.dest_uri_id = dest.uri_id
$details_filters
ORDER BY $details_orderby
",
$details_filter_params
);
$hits = $wpdb->get_results( $details_query, OBJECT ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$output .= '';
$output .= '';
$output .= '';
$output .= '
' . esc_html__( 'Hits', 'affiliates' ) . '
';
$output .= '
';
$output .= '';
$output .= '';
$output .= '' . esc_html__( 'Date', 'affiliates' ) . ' ';
$output .= '' . esc_html__( 'IP', 'affiliates' ) . ' ';
$output .= '' . esc_html__( 'Affiliate', 'affiliates' ) . ' ';
$output .= '' . esc_html__( 'Source URI', 'affiliates' ) . ' ';
$output .= '' . esc_html__( 'Landing URI', 'affiliates' ) . ' ';
$output .= '' . esc_html__( 'User Agent', 'affiliates' ) . ' ';
$output .= ' ';
$output .= ' ';
$output .= '';
foreach ( $hits as $hit ) {
$output .= '';
$output .= '' . esc_html( $hit->datetime ) . ' ';
$output .= "" . long2ip( $hit->ip ) . " ";
$output .= "" . stripslashes( wp_filter_nohtml_kses( $hit->name ) ) . " ";
$output .= "" . esc_html( $hit->src_uri ) . " ";
$output .= "" . esc_html( $hit->dest_uri ) . " ";
$output .= "" . esc_html( $hit->user_agent ) . " ";
$output .= ' ';
}
$output .= '
';
$output .= '
'; // .details-hits
$output .= ' ';
} // if $expanded_hits
} // expanded
}
} else {
$output .= '' . esc_html__( 'There are no results.', 'affiliates' ) . ' ';
}
$output .= ' ';
$output .= '
';
if ( $paginate ) {
require_once( AFFILIATES_CORE_LIB . '/class-affiliates-pagination.php' );
$pagination = new Affiliates_Pagination( $count, null, $row_count );
$output .= '
';
$output .= $pagination->pagination( 'bottom' );
$output .= '
';
}
$server_dtz = DateHelper::getServerDateTimeZone();
$output .= '
';
$output .= sprintf(
/* translators: 1 time zone, 2 offset */
esc_html__( '* Date is given for the server\'s time zone : %1$s, which has an offset of %2$s hours with respect to GMT.', 'affiliates' ),
$server_dtz->getName(),
$server_dtz->getOffset( new DateTime() ) / 3600.0
);
$output .= '
';
$output .= '
'; // .visits-overview
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
affiliates_footer();
} // function affiliates_admin_hits()