prefix . 'posts'; // actions if ( isset( $_POST['affiliate_id'] ) && isset( $_POST['post_id'] ) && isset( $_POST['datetime'] ) && isset( $_POST['action'] ) ) { if ( isset( $_POST['status'] ) ) { $referral = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $referrals_table WHERE affiliate_id = %d AND post_id = %d AND datetime = %s", intval( $_POST['affiliate_id'] ), intval( $_POST['post_id'] ), $_POST['datetime'] ) ); if ( $referral ) { if ( Affiliates_Utility::verify_referral_status_transition( $referral->status, $_POST['status'] ) ) { $wpdb->query( $wpdb->prepare( "UPDATE $referrals_table SET status = %s WHERE affiliate_id = %d AND post_id = %d AND datetime = %s AND status = %s", $_POST['status'], intval( $referral->affiliate_id ), intval( $referral->post_id ), $referral->datetime, $referral->status ) ); } } } } // filters $from_date = $affiliates_options->get_option( 'referrals_from_date', null ); $thru_date = $affiliates_options->get_option( 'referrals_thru_date', null ); $affiliate_id = $affiliates_options->get_option( 'referrals_affiliate_id', null ); $expanded = $affiliates_options->get_option( 'referrals_expanded', null ); $expanded_description = $affiliates_options->get_option( 'referrals_expanded_description', null ); $expanded_data = $affiliates_options->get_option( 'referrals_expanded_data', null ); $show_inoperative = $affiliates_options->get_option( 'referrals_show_inoperative', null ); if ( isset( $_POST['clear_filters'] ) ) { $affiliates_options->delete_option( 'referrals_from_date' ); $affiliates_options->delete_option( 'referrals_thru_date' ); $affiliates_options->delete_option( 'referrals_affiliate_id' ); $affiliates_options->delete_option( 'referrals_expanded' ); $affiliates_options->delete_option( 'referrals_expanded_description' ); $affiliates_options->delete_option( 'referrals_expanded_data' ); $affiliates_options->delete_option( 'referrals_show_inoperative' ); $from_date = null; $thru_date = null; $affiliate_id = null; $expanded = null; $expanded_data = null; $expanded_description = 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( 'referrals_from_date', $from_date ); } if ( !empty( $_POST['thru_date'] ) ) { $thru_date = date( 'Y-m-d', strtotime( $_POST['thru_date'] ) ); $affiliates_options->update_option( 'referrals_thru_date', $thru_date ); } if ( $from_date && $thru_date ) { if ( strtotime( $from_date ) > strtotime( $thru_date ) ) { $thru_date = null; $affiliates_options->delete_option( 'referrals_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( 'referrals_affiliate_id', $affiliate_id ); } } else if ( isset( $_POST['affiliate_id'] ) ) { // empty && isset => '' => all $affiliate_id = null; $affiliates_options->delete_option( 'referrals_affiliate_id' ); } // expanded details? if ( !empty( $_POST['submitted'] ) ) { if ( !empty( $_POST['expanded'] ) ) { $expanded = true; $affiliates_options->update_option( 'referrals_expanded', true ); } else { $expanded = false; $affiliates_options->delete_option( 'referrals_expanded' ); } if ( !empty( $_POST['expanded_data'] ) ) { $expanded_data = true; $affiliates_options->update_option( 'referrals_expanded_data', true ); } else { $expanded_data = false; $affiliates_options->delete_option( 'referrals_expanded_data' ); } if ( !empty( $_POST['expanded_description'] ) ) { $expanded_description = true; $affiliates_options->update_option( 'referrals_expanded_description', true ); } else { $expanded_description = false; $affiliates_options->delete_option( 'referrals_expanded_description' ); } if ( !empty( $_POST['show_inoperative'] ) ) { $show_inoperative = true; $affiliates_options->update_option( 'referrals_show_inoperative', true ); } else { $show_inoperative = false; $affiliates_options->delete_option( 'referrals_show_inoperative' ); } } } if ( isset( $_POST['row_count'] ) ) { if ( !wp_verify_nonce( $_POST[AFFILIATES_ADMIN_HITS_NONCE_1], plugin_basename( __FILE__ ) ) ) { wp_die( __( 'Access denied.', AFFILIATES_PLUGIN_DOMAIN ) ); } } if ( isset( $_POST['paged'] ) ) { if ( !wp_verify_nonce( $_POST[AFFILIATES_ADMIN_HITS_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 ); $output .= '
' . '

' . __( 'Referrals', AFFILIATES_PLUGIN_DOMAIN ) . '

' . '
'; $row_count = isset( $_POST['row_count'] ) ? intval( $_POST['row_count'] ) : 0; if ($row_count <= 0) { $row_count = $affiliates_options->get_option( 'referrals_per_page', AFFILIATES_HITS_PER_PAGE ); } else { $affiliates_options->update_option('referrals_per_page', $row_count ); } $offset = isset( $_GET['offset'] ) ? intval( $_GET['offset'] ) : 0; if ( $offset < 0 ) { $offset = 0; } $paged = isset( $_GET['paged'] ) ? intval( $_GET['paged'] ) : 0; if ( $paged < 0 ) { $paged = 0; } $orderby = isset( $_GET['orderby'] ) ? $_GET['orderby'] : null; switch ( $orderby ) { case 'datetime' : case 'name' : case 'post_title' : case 'amount' : case 'currency_id' : case 'status' : break; default : $orderby = 'datetime'; } $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'; } 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 .= " r.affiliate_id = %d "; $filter_params[] = $affiliate_id; } // how many are there ? $count_query = $wpdb->prepare( "SELECT count(*) FROM $referrals_table r $filters ", $filter_params ); $count = $wpdb->get_var( $count_query ); 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; } $query = $wpdb->prepare(" SELECT r.*, a.affiliate_id, a.name FROM $referrals_table r LEFT JOIN $affiliates_table a ON r.affiliate_id = a.affiliate_id LEFT JOIN $posts_table p ON r.post_id = p.ID $filters ORDER BY $orderby $order LIMIT $row_count OFFSET $offset ", $filter_params + $filter_params ); $results = $wpdb->get_results( $query, OBJECT ); $column_display_names = array( 'datetime' => __( 'Date', AFFILIATES_PLUGIN_DOMAIN ), 'post_title' => __( 'Post', AFFILIATES_PLUGIN_DOMAIN ), 'name' => __( 'Affiliate', AFFILIATES_PLUGIN_DOMAIN ), 'amount' => __( 'Amount', AFFILIATES_PLUGIN_DOMAIN ), 'currency_id' => __( 'Currency', AFFILIATES_PLUGIN_DOMAIN ), 'status' => __( 'Status', AFFILIATES_PLUGIN_DOMAIN ) ); $column_count = count( $column_display_names ); $output .= '
'; $affiliates = affiliates_get_affiliates( true, !$show_inoperative ); $affiliates_select = ''; if ( !empty( $affiliates ) ) { $affiliates_select .= ''; $affiliates_select .= ''; } $output .= '
' . '' . '
' . '

' . $affiliates_select . '

' . '' . ''. '' . ''. '

' . wp_nonce_field( plugin_basename( __FILE__ ), AFFILIATES_ADMIN_HITS_FILTER_NONCE, true, false ) . '' . '' . '' . '' . '' . '' . '' . '' . '' . '' . '' . '

' . '
' . '
'; $output .= '
' . ' ' . wp_nonce_field( plugin_basename( __FILE__ ), AFFILIATES_ADMIN_HITS_NONCE_1, true, false ) . '
'; if ( $paginate ) { require_once(dirname( __FILE__ ) . '/class-affiliates-pagination.php' ); $pagination = new Affiliates_Pagination($count, null, $row_count); $output .= '
'; $output .= '
'; $output .= wp_nonce_field( plugin_basename( __FILE__ ), AFFILIATES_ADMIN_HITS_NONCE_2, true, false ); $output .= '
'; $output .= '
'; $output .= $pagination->pagination( 'top' ); $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 = '' . $column_display_name . ''; $output .= ""; } $output .= ' '; $status_descriptions = array( AFFILIATES_REFERRAL_STATUS_ACCEPTED => __( 'Accepted', AFFILIATES_PLUGIN_DOMAIN ), AFFILIATES_REFERRAL_STATUS_CLOSED => __( 'Closed', AFFILIATES_PLUGIN_DOMAIN ), AFFILIATES_REFERRAL_STATUS_PENDING => __( 'Pending', AFFILIATES_PLUGIN_DOMAIN ), AFFILIATES_REFERRAL_STATUS_REJECTED => __( 'Rejected', AFFILIATES_PLUGIN_DOMAIN ), ); if ( count( $results ) > 0 ) { for ( $i = 0; $i < count( $results ); $i++ ) { $result = $results[$i]; $output .= ''; $output .= ''; $link = get_permalink( $result->post_id ); $title = get_the_title( $result->post_id ); $output .= ''; $output .= ""; $output .= ""; $output .= ""; $output .= ""; $output .= ''; $data = $result->data; if ( !empty( $data ) && $expanded ) { if ( $expanded_data ) { $data_view_style = ''; $expander = AFFILIATES_EXPANDER_RETRACT; } else { $data_view_style = ' style="display:none;" '; $expander = AFFILIATES_EXPANDER_EXPAND; } $data = unserialize( $data ); if ( $data ) { $output .= ''; $output .= "'; $output .= ''; } } if ( !empty( $result->description ) && $expanded ) { if ( $expanded_description ) { $description_view_style = ''; $expander = AFFILIATES_EXPANDER_RETRACT; } else { $description_view_style = ' style="display:none;" '; $expander = AFFILIATES_EXPANDER_EXPAND; } $output .= "" . '' . ''; } } } else { $output .= ''; } $output .= ''; $output .= '
$column_display_name
' . DateHelper::s2u( $result->datetime ) . '' . wp_filter_nohtml_kses( $title ) . '" . stripslashes( wp_filter_nohtml_kses( $result->name ) ) . "" . stripslashes( wp_filter_nohtml_kses( $result->amount ) ) . "" . stripslashes( wp_filter_nohtml_kses( $result->currency_id ) ) . ""; $output .= "
"; $output .= "
"; $output .= ""; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= ''; $output .= wp_nonce_field( plugin_basename( __FILE__ ), AFFILIATES_ADMIN_HITS_FILTER_NONCE, true, false ); $output .= "
"; $output .= "
"; $output .= "
"; $output .= '
'; $output .= "
$expander
"; $output .= '
' . __( 'Data', AFFILIATES_PLUGIN_DOMAIN ) . '
'; $output .= "
"; $output .= ''; if ( is_array( $data ) ) { foreach ( $data as $key => $info ) { $title = __( $info['title'], $info['domain'] ); $value = $info['value']; $output .= ""; $output .= ''; $output .= ''; $output .= ''; } } else { $output .= ""; $output .= ''; $output .= ''; $output .= ''; } $output .= '
'; $output .= wp_filter_nohtml_kses( $title ); $output .= ''; // @todo revise // $output .= wp_filter_nohtml_kses( $value ); $output .= stripslashes( $value ); $output .= '
'; $output .= __( 'Data', AFFILIATES_PLUGIN_DOMAIN ); $output .= ''; // @todo revise //$output .= wp_filter_nohtml_kses( $data ); $output .= stripslashes( $value ); $output .= '
'; $output .= '
'; // .view $output .= '
'; // .view-toggle $output .= '
' . '
' . "
$expander
" . '
' . __('Description', AFFILIATES_PLUGIN_DOMAIN ) . '
' . "
" . wp_filter_kses( addslashes( $result->description ) ) . '
' . '
' . '
' . __('There are no results.', AFFILIATES_PLUGIN_DOMAIN ) . '
'; 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() ?>