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 ); $status = $affiliates_options->get_option( 'referrals_status', null ); $search = $affiliates_options->get_option( 'referrals_search', null ); $search_description = $affiliates_options->get_option( 'referrals_search_description', 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 ); $expanded_items = $affiliates_options->get_option( 'referrals_expanded_items', null ); $show_inoperative = $affiliates_options->get_option( 'referrals_show_inoperative', null ); if ( !isset( $_POST['action'] ) && 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_status' ); $affiliates_options->delete_option( 'referrals_search' ); $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_expanded_items' ); $affiliates_options->delete_option( 'referrals_show_inoperative' ); $from_date = null; $thru_date = null; $affiliate_id = null; $status = null; $search = null; $search_description = null; $expanded = null; $expanded_data = null; $expanded_description = null; $expanded_items = null; $show_inoperative = null; } else if ( !isset( $_POST['action'] ) && 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( 'referrals_from_date', $from_date ); } else { $from_date = null; $affiliates_options->delete_option( 'referrals_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 ); } else { $thru_date = null; $affiliates_options->delete_option( 'referrals_thru_date' ); } if ( $from_date && $thru_date ) { if ( strtotime( $from_date ) > strtotime( $thru_date ) ) { $thru_date = null; $affiliates_options->delete_option( 'referrals_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( 'referrals_affiliate_id', $affiliate_id ); } } else if ( isset( $_POST['affiliate_id'] ) ) { // empty && isset => '' => all $affiliate_id = null; $affiliates_options->delete_option( 'referrals_affiliate_id' ); } if ( !empty( $_POST['status'] ) ) { if ( $status = Affiliates_Utility::verify_referral_status_transition( $_POST['status'], $_POST['status'] ) ) { $affiliates_options->update_option( 'referrals_status', $status ); } else { $status = null; $affiliates_options->delete_option( 'referrals_status' ); } } else { $status = null; $affiliates_options->delete_option( 'referrals_status' ); } if ( !empty( $_POST['search'] ) ) { $search = $_POST['search']; $affiliates_options->update_option( 'referrals_search', $_POST['search'] ); } else { $search = null; $affiliates_options->delete_option( 'referrals_search' ); } if ( !empty( $_POST['search_description'] ) ) { $search_description = true; $affiliates_options->update_option( 'referrals_search_description', true ); } else { $search_description = false; $affiliates_options->delete_option( 'referrals_search_description' ); } // expanded details? 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 ( class_exists( 'Affiliates_Referral_Item' ) ) { if ( !empty( $_POST['expanded_items'] ) ) { $expanded_items = true; $affiliates_options->update_option( 'referrals_expanded_items', true ); } else { $expanded_items = false; $affiliates_options->delete_option( 'referrals_expanded_items' ); } } 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], 'admin' ) ) { wp_die( __( 'Access denied.', 'affiliates' ) ); } } if ( isset( $_POST['paged'] ) ) { if ( !wp_verify_nonce( $_POST[AFFILIATES_ADMIN_HITS_NONCE_2], 'admin' ) ) { wp_die( __( 'Access denied.', 'affiliates' ) ); } } $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $current_url = remove_query_arg( 'paged', $current_url ); $output .= '
' . '

' . __( 'Referrals', 'affiliates' ) . '

' . '
'; $output .= '
'; $output .= sprintf( '%s%s', __( 'Click to add a referral manually', 'affiliates' ), esc_url( add_query_arg( 'action', 'edit', $current_url ) ), __( 'Add', 'affiliates' ), AFFILIATES_PLUGIN_URL .'images/add.png', __( 'Add', 'affiliates') ); $output .= '
'; $output .= apply_filters( 'affiliates_admin_referrals_secondary_actions', '' ); $output .= '
'; // floating right $output .= '
'; $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 ); } // 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 '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'; } $filters = " WHERE 1=%d "; $filter_params = array( 1 ); // We 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 ); } if ( $from_date && $thru_date ) { $filters .= " AND datetime >= %s AND datetime < %s "; $filter_params[] = $from_datetime; $filter_params[] = $thru_datetime; } else if ( $from_date ) { $filters .= " AND datetime >= %s "; $filter_params[] = $from_datetime; } else if ( $thru_date ) { $filters .= " AND datetime < %s "; $filter_params[] = $thru_datetime; } if ( $affiliate_id ) { $filters .= " AND r.affiliate_id = %d "; $filter_params[] = $affiliate_id; } if ( $status ) { $filters .= " AND r.status = %s "; $filter_params[] = $status; } if ( $search ) { if ( $search_description ) { $filters .= " AND ( r.data LIKE '%%%s%%' OR r.description LIKE '%%%s%%' ) "; $filter_params[] = $search; $filter_params[] = $search; } else { $filters .= " AND r.data LIKE '%%%s%%' "; $filter_params[] = $search; } } do { $repeat = false; $offset = ( $paged - 1 ) * $row_count; $query = $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS 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 ); $results = $wpdb->get_results( $query, OBJECT ); $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( 'datetime' => __( 'Date', 'affiliates' ), 'post_title' => __( 'Post', 'affiliates' ), 'name' => __( 'Affiliate', 'affiliates' ), 'amount' => __( 'Amount', 'affiliates' ), 'currency_id' => __( 'Currency', 'affiliates' ), 'status' => __( 'Status', 'affiliates' ), 'edit' => '', 'remove' => '', ); $column_count = count( $column_display_names ); $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 ) ); $status_descriptions = array( AFFILIATES_REFERRAL_STATUS_ACCEPTED => __( 'Accepted', 'affiliates' ), AFFILIATES_REFERRAL_STATUS_CLOSED => __( 'Closed', 'affiliates' ), AFFILIATES_REFERRAL_STATUS_PENDING => __( 'Pending', 'affiliates' ), AFFILIATES_REFERRAL_STATUS_REJECTED => __( 'Rejected', 'affiliates' ), ); $status_icons = array( AFFILIATES_REFERRAL_STATUS_ACCEPTED => "" . __( ", AFFILIATES_REFERRAL_STATUS_CLOSED => "" . __( ", AFFILIATES_REFERRAL_STATUS_PENDING => "" . __( ", AFFILIATES_REFERRAL_STATUS_REJECTED => "" . __( ", ); $status_select = ''; $output .= '
' . '' . '
' . '
' . $affiliates_select . Affiliates_UI_Elements::render_select( 'select.affiliate-id-filter' ) . '
' . '
' . $status_select . ' ' . ' ' . ' ' . sprintf( '' . '
' . '
' . '' . ' ' . '' . '
' . '
' . '' . ' ' . '' . ' ' . '' . ' ' . ( class_exists( 'Affiliates_Referral_Item' ) ? '' : '' ) . ' ' . '' . '
' . '
' . wp_nonce_field( 'admin', AFFILIATES_ADMIN_HITS_FILTER_NONCE, true, false ) . '' . '' . '' . '
' . '
' . '
'; $output .= '
' . ' ' . wp_nonce_field( 'admin', AFFILIATES_ADMIN_HITS_NONCE_1, true, false ) . '
'; if ( $paginate ) { require_once( AFFILIATES_CORE_LIB . '/class-affiliates-pagination.php' ); $pagination = new Affiliates_Pagination($count, null, $row_count); $output .= '
'; $output .= '
'; $output .= wp_nonce_field( 'admin', 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 .= ' '; 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 .= sprintf( '', esc_attr( $result->amount ), stripslashes( wp_filter_nohtml_kses( affiliates_format_referral_amount( $result->amount, 'display' ) ) ) ); $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 ( $expanded ) { $output .= apply_filters( 'affiliates_admin_referrals_expanded_items', '', array( 'referral_id' => $result->referral_id, 'column_count' => $column_count, 'expanded_items' => $expanded_items, 'i' => $i ) ); } 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 .= sprintf( "", $i, $i % 2 == 0 ? 'even' : 'odd' ) . '' . ''; } } } else { $output .= ''; } $output .= ''; $output .= '
$column_display_name
' . DateHelper::s2u( $result->datetime ) . '' . wp_filter_nohtml_kses( $title ) . '" . stripslashes( wp_filter_nohtml_kses( $result->name ) ) . "%s" . stripslashes( wp_filter_nohtml_kses( $result->currency_id ) ) . ""; $output .= isset( $status_icons[$result->status] ) ? $status_icons[$result->status] : ''; $output .= ' '; $output .= isset( $status_descriptions[$result->status] ) ? $status_descriptions[$result->status] : ''; // $output .= "
"; // $output .= "
"; // $output .= ""; // $output .= ''; // $output .= ''; // $output .= ''; // $output .= ''; // $output .= ''; // $output .= wp_nonce_field( 'admin', AFFILIATES_ADMIN_HITS_FILTER_NONCE, true, false ); // $output .= "
"; // $output .= "
"; $output .= "
'; $edit_url = add_query_arg( 'referral_id', $result->referral_id, add_query_arg( 'action', 'edit', $current_url ) ); $output .= sprintf( '', esc_url( add_query_arg( 'paged', $paged, $edit_url ) ) ); $output .= sprintf( '%s', AFFILIATES_PLUGIN_URL . 'images/edit.png', __( 'Edit', 'affiliates' ) ); $output .= ''; $output .= ''; $remove_url = add_query_arg( 'referral_id', $result->referral_id, add_query_arg( 'action', 'remove', $current_url ) ); $output .= sprintf( '', esc_url( add_query_arg( 'paged', $paged, $remove_url ) ) ); $output .= sprintf( '%s', AFFILIATES_PLUGIN_URL . 'images/remove.png', __( 'Remove', 'affiliates' ) ); $output .= ''; $output .= '
"; $output .= '
'; $output .= "
$expander
"; $output .= '
' . __( 'Data', 'affiliates' ) . '
'; $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 .= stripslashes( wp_filter_nohtml_kses( $title ) ); $output .= ''; $output .= stripslashes( wp_filter_kses( $value ) ); $output .= '
'; $output .= __( 'Data', 'affiliates' ); $output .= ''; if ( is_string( $data ) ) { $output .= stripslashes( wp_filter_kses( $data ) ); } $output .= '
'; $output .= '
'; // .view $output .= '
'; // .view-toggle $output .= '
' . '
' . "
$expander
" . '
' . __('Description', 'affiliates' ) . '
' . "
" . wp_filter_kses( addslashes( $result->description ) ) . '
' . '
' . '
' . __('There are no results.', 'affiliates' ) . '
'; 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 .= '
'; } $output .= '
'; // .referrals-overview echo $output; affiliates_footer(); } // function affiliates_admin_referrals()