neric.Arrays.DisallowLongArraySyntax.Found, Generic.Metrics.CyclomaticComplexity.MaxExceeded, Generic.Metrics.NestingLevel.MaxExceeded // Check current page. if ( empty( $_POST['action'] ) || 'aawp_clicks_export' !== $_POST['action'] ) { return; } // Capability check. if ( ! aawp_is_user_editor() ) { return; } // Nonce check. if ( empty( $_POST['data'] ) || empty( $_POST['tracking_id'] ) || empty( $_REQUEST['nonce'] ) || ! wp_verify_nonce( wp_unslash( $_REQUEST['nonce'] ), 'aawp-clicks-export-nonce' ) ) { return; } $data = array_map( 'wp_unslash', $_POST['data'] ); $data = array_map( 'sanitize_text_field', $_POST['data'] ); $tracking_ids = array_map( 'wp_unslash', $_POST['tracking_id'] ); $tracking_ids = array_map( 'sanitize_text_field', $_POST['tracking_id'] ); $start_date = ! empty( $_POST['start_date'] ) ? sanitize_text_field( wp_unslash( $_POST['start_date'] ) ) : ''; $end_date = ! empty( $_POST['end_date'] ) ? sanitize_text_field( wp_unslash( $_POST['end_date'] ) ) : ''; $db_results = $this->db_results( $tracking_ids, $start_date, $end_date ); $columns = $listtable->get_columns(); $columns['is_widget'] = esc_html__( 'Is Widget?', 'aawp' ); $csv_columns = []; foreach ( $columns as $key => $column ) { if ( in_array( $key, $data, true ) ) { $csv_columns[] = $column; } } /** * Rows Format. * * $rows = array( * array('John', 'Doe', 'john@example.com'), * array('Jane', 'Smith', 'jane@example.com'), * // More rows... * ); */ ignore_user_abort( true ); // Set time limit. if ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved @set_time_limit(0); // @codingStandardsIgnoreLine } if ( ob_get_contents() ) { ob_clean(); } $filename = 'aawp-clicks-' . date( 'Y-m-d' ) . '.csv'; // Force download. header( 'Content-Type: application/force-download' ); header( 'Content-Type: application/octet-stream' ); header( 'Content-Type: application/download' ); // Disposition / Encoding on response body. header( "Content-Disposition: attachment;filename=\"{$filename}\";charset=utf-8" ); header( 'Content-Transfer-Encoding: binary' ); $output = fopen( 'php://output', 'w' ); // Handle UTF-8 chars conversion for CSV. fprintf( $output, chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) ); fputcsv( $output, $csv_columns ); foreach ( $db_results as $result ) { $product = aawp_get_product( $result->product_id ); $row_data = []; foreach ( $data as $column ) { if ( property_exists( $result, $column ) ) { $row_data[] = $result->$column; } else { switch ( $column ) { case 'asin': $row_data[] = ! empty( $product['asin'] ) ? $product['asin'] : '-'; break; case 'title': $row_data[] = ! empty( $product['title'] ) ? $product['title'] : '-'; break; case 'link': $url = ! empty( $product['url'] ) ? aawp_replace_url_tracking_id_placeholder( $product['url'], $result->tracking_id, false ) : ''; $row_data[] = esc_url( $url ); break; case 'source': $source = $listtable->get_source( $result->source_type, absint( $result->source_id ) ); $row_data[] = ! empty( $source['title'] ) ? $source['title'] : ''; break; default: $row_data[] = ''; } }//end if }//end foreach fputcsv( $output, $row_data ); }//end foreach fclose( $output ); exit; } /** * Get db results by tracking id and dates. * * @param array $tracking_ids An array of tracking ids. * @param string $start_date Filter db results by startdate. * @param string $end_date Filter db results by enddate. * * @since 3.31.0 * * @return array The db results */ public function db_results( $tracking_ids, $start_date, $end_date ) { global $wpdb; $tracking_ids = "'" . implode( "', '", $tracking_ids ) . "'"; $where = ''; if ( ! empty( $start_date ) ) { $where .= $wpdb->prepare( ' AND DATE( created_at ) >= %s', $start_date ); } if ( ! empty( $end_date ) ) { $where .= $wpdb->prepare( ' AND DATE( created_at ) <= %s', $end_date ); } $query_items = "SELECT * FROM {$wpdb->prefix}aawp_clicks WHERE tracking_id IN ($tracking_ids) $where"; //phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery return $wpdb->get_results( $query_items ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery } }