| @@ -3,15 +3,18 @@ | ||
| 3 | 3 | class Red_Csv_File extends Red_FileIO { |
| 4 | 4 | const CSV_SOURCE = 0; |
| 5 | 5 | const CSV_TARGET = 1; |
| 6 | 6 | const CSV_REGEX = 2; |
| 7 | - const CSV_CODE = 3; | |
| 7 | + const CSV_TYPE = 3; | |
| 8 | + const CSV_CODE = 4; | |
| 8 | 9 | |
| 9 | 10 | public function force_download() { |
| 10 | 11 | parent::force_download(); |
| 11 | 12 | |
| 13 | + $filename = 'redirection-'.date_i18n( get_option( 'date_format' ) ).'.csv'; | |
| 14 | + | |
| 12 | 15 | header( 'Content-Type: text/csv' ); |
| 13 | - header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'csv' ) . '"' ); | |
| 16 | + header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); | |
| 14 | 17 | } |
| 15 | 18 | |
| 16 | 19 | public function get_data( array $items, array $groups ) { |
| 17 | 20 | $lines[] = implode( ',', array( 'source', 'target', 'regex', 'type', 'code', 'match', 'hits', 'title' ) ); |
| @@ -19,14 +22,16 @@ | ||
| 19 | 22 | foreach ( $items as $line ) { |
| 20 | 23 | $lines[] = $this->item_as_csv( $line ); |
| 21 | 24 | } |
| 22 | 25 | |
| 23 | - return implode( PHP_EOL, $lines ) . PHP_EOL; | |
| 26 | + return implode( PHP_EOL, $lines ).PHP_EOL; | |
| 24 | 27 | } |
| 25 | 28 | |
| 26 | 29 | public function item_as_csv( $item ) { |
| 27 | - $data = $item->match->get_data(); | |
| 28 | - $data = isset( $data['url'] ) ? $data = $data['url'] : '*'; | |
| 30 | + $data = $item->get_action_data(); | |
| 31 | + if ( is_array( maybe_unserialize( $data ) ) ) { | |
| 32 | + $data = '*'; | |
| 33 | + } | |
| 29 | 34 | |
| 30 | 35 | $csv = array( |
| 31 | 36 | $item->get_url(), |
| 32 | 37 | $data, |
| @@ -42,9 +47,9 @@ | ||
| 42 | 47 | return join( $csv, ',' ); |
| 43 | 48 | } |
| 44 | 49 | |
| 45 | 50 | public function escape_csv( $item ) { |
| 46 | - return '"' . str_replace( '"', '""', $item ) . '"'; | |
| 51 | + return '"'.str_replace( '"', '""', $item ).'"'; | |
| 47 | 52 | } |
| 48 | 53 | |
| 49 | 54 | public function load( $group, $filename, $data ) { |
| 50 | 55 | ini_set( 'auto_detect_line_endings', true ); |
| @@ -52,25 +57,19 @@ | ||
| 52 | 57 | $file = fopen( $filename, 'r' ); |
| 53 | 58 | |
| 54 | 59 | ini_set( 'auto_detect_line_endings', false ); |
| 55 | 60 | |
| 56 | - $count = 0; | |
| 57 | 61 | if ( $file ) { |
| 58 | - $count = $this->load_from_file( $group, $file, ',' ); | |
| 59 | - | |
| 60 | - // Try again with semicolons - Excel often exports CSV with semicolons | |
| 61 | - if ( $count === 0 ) { | |
| 62 | - $count = $this->load_from_file( $group, $file, ';' ); | |
| 63 | - } | |
| 62 | + return $this->load_from_file( $group, $file ); | |
| 64 | 63 | } |
| 65 | 64 | |
| 66 | - return $count; | |
| 65 | + return 0; | |
| 67 | 66 | } |
| 68 | 67 | |
| 69 | - public function load_from_file( $group_id, $file, $separator ) { | |
| 68 | + public function load_from_file( $group_id, $file ) { | |
| 70 | 69 | $count = 0; |
| 71 | 70 | |
| 72 | - while ( ( $csv = fgetcsv( $file, 5000, $separator ) ) ) { | |
| 71 | + while ( ( $csv = fgetcsv( $file, 5000, ',' ) ) ) { | |
| 73 | 72 | $item = $this->csv_as_item( $csv, $group_id ); |
| 74 | 73 | |
| 75 | 74 | if ( $item ) { |
| 76 | 75 | $created = Red_Item::create( $item ); |
| @@ -92,12 +91,12 @@ | ||
| 92 | 91 | return 301; |
| 93 | 92 | } |
| 94 | 93 | |
| 95 | 94 | public function csv_as_item( $csv, $group ) { |
| 96 | - if ( count( $csv ) > 1 && $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' ) { | |
| 95 | + if ( $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' && count( $csv ) > 1 ) { | |
| 97 | 96 | return array( |
| 98 | - 'url' => trim( $csv[ self::CSV_SOURCE ] ), | |
| 99 | - 'action_data' => array( 'url' => trim( $csv[ self::CSV_TARGET ] ) ), | |
| 97 | + 'url' => trim( $csv[ self ::CSV_SOURCE ] ), | |
| 98 | + 'action_data' => trim( $csv[ self ::CSV_TARGET ] ), | |
| 100 | 99 | 'regex' => isset( $csv[ self::CSV_REGEX ] ) ? $this->parse_regex( $csv[ self::CSV_REGEX ] ) : $this->is_regex( $csv[ self::CSV_SOURCE ] ), |
| 101 | 100 | 'group_id' => $group, |
| 102 | 101 | 'match_type' => 'url', |
| 103 | 102 | 'action_type' => 'url', |