| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Csv_File extends Red_FileIO { |
| 4 |
const CSV_SOURCE = 0; |
| 5 |
const CSV_TARGET = 1; |
| 6 |
const CSV_REGEX = 2; |
| 7 |
const CSV_CODE = 3; |
| 8 |
|
| 9 |
public function force_download() { |
| 10 |
parent::force_download(); |
| 11 |
|
| 12 |
$filename = 'redirection-'.date_i18n( get_option( 'date_format' ) ).'.csv'; |
| 13 |
|
| 14 |
header( 'Content-Type: text/csv' ); |
| 15 |
header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); |
| 16 |
} |
| 17 |
|
| 18 |
public function get_data( array $items, array $groups ) { |
| 19 |
$lines[] = implode( ',', array( 'source', 'target', 'regex', 'type', 'code', 'match', 'hits', 'title' ) ); |
| 20 |
|
| 21 |
foreach ( $items as $line ) { |
| 22 |
$lines[] = $this->item_as_csv( $line ); |
| 23 |
} |
| 24 |
|
| 25 |
return implode( PHP_EOL, $lines ).PHP_EOL; |
| 26 |
} |
| 27 |
|
| 28 |
public function item_as_csv( $item ) { |
| 29 |
$data = $item->match->get_data(); |
| 30 |
$data = isset( $data['url'] ) ? $data = $data['url'] : '*'; |
| 31 |
|
| 32 |
$csv = array( |
| 33 |
$item->get_url(), |
| 34 |
$data, |
| 35 |
$item->is_regex() ? 1 : 0, |
| 36 |
$item->get_action_type(), |
| 37 |
$item->get_action_code(), |
| 38 |
$item->get_action_type(), |
| 39 |
$item->get_hits(), |
| 40 |
$item->get_title(), |
| 41 |
); |
| 42 |
|
| 43 |
$csv = array_map( array( $this, 'escape_csv' ), $csv ); |
| 44 |
return join( $csv, ',' ); |
| 45 |
} |
| 46 |
|
| 47 |
public function escape_csv( $item ) { |
| 48 |
return '"'.str_replace( '"', '""', $item ).'"'; |
| 49 |
} |
| 50 |
|
| 51 |
public function load( $group, $filename, $data ) { |
| 52 |
ini_set( 'auto_detect_line_endings', true ); |
| 53 |
|
| 54 |
$file = fopen( $filename, 'r' ); |
| 55 |
|
| 56 |
ini_set( 'auto_detect_line_endings', false ); |
| 57 |
|
| 58 |
if ( $file ) { |
| 59 |
return $this->load_from_file( $group, $file ); |
| 60 |
} |
| 61 |
|
| 62 |
return 0; |
| 63 |
} |
| 64 |
|
| 65 |
public function load_from_file( $group_id, $file ) { |
| 66 |
$count = 0; |
| 67 |
|
| 68 |
while ( ( $csv = fgetcsv( $file, 5000, ',' ) ) ) { |
| 69 |
$item = $this->csv_as_item( $csv, $group_id ); |
| 70 |
|
| 71 |
if ( $item ) { |
| 72 |
$created = Red_Item::create( $item ); |
| 73 |
|
| 74 |
if ( ! is_wp_error( $created ) ) { |
| 75 |
$count++; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $count; |
| 81 |
} |
| 82 |
|
| 83 |
private function get_valid_code( $code ) { |
| 84 |
if ( get_status_header_desc( $code ) !== '' ) { |
| 85 |
return intval( $code, 10 ); |
| 86 |
} |
| 87 |
|
| 88 |
return 301; |
| 89 |
} |
| 90 |
|
| 91 |
public function csv_as_item( $csv, $group ) { |
| 92 |
if ( count( $csv ) > 1 && $csv[ self::CSV_SOURCE ] !== 'source' && $csv[ self::CSV_TARGET ] !== 'target' ) { |
| 93 |
return array( |
| 94 |
'url' => trim( $csv[ self::CSV_SOURCE ] ), |
| 95 |
'action_data' => array( 'url' => trim( $csv[ self::CSV_TARGET ] ) ), |
| 96 |
'regex' => isset( $csv[ self::CSV_REGEX ] ) ? $this->parse_regex( $csv[ self::CSV_REGEX ] ) : $this->is_regex( $csv[ self::CSV_SOURCE ] ), |
| 97 |
'group_id' => $group, |
| 98 |
'match_type' => 'url', |
| 99 |
'action_type' => 'url', |
| 100 |
'action_code' => isset( $csv[ self::CSV_CODE ] ) ? $this->get_valid_code( $csv[ self::CSV_CODE ] ) : 301, |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
private function parse_regex( $value ) { |
| 108 |
return intval( $value, 10 ) === 1 ? true : false; |
| 109 |
} |
| 110 |
|
| 111 |
private function is_regex( $url ) { |
| 112 |
$regex = '()[]$^*'; |
| 113 |
|
| 114 |
if ( strpbrk( $url, $regex ) === false ) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
|