| 1 |
<?php |
| 2 |
|
| 3 |
class Red_Csv_File extends Red_FileIO { |
| 4 |
function export( array $items ) { |
| 5 |
$filename = 'redirection-'.date_i18n( get_option( 'date_format' ) ).'.csv'; |
| 6 |
|
| 7 |
header( 'Content-Type: text/csv' ); |
| 8 |
header( 'Cache-Control: no-cache, must-revalidate' ); |
| 9 |
header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' ); |
| 10 |
header( 'Content-Disposition: attachment; filename="'.$filename.'"' ); |
| 11 |
|
| 12 |
$stdout = fopen( 'php://output', 'w' ); |
| 13 |
|
| 14 |
fputcsv( $stdout, array( 'source', 'target', 'regex', 'type', 'code', 'match', 'hits', 'title' ) ); |
| 15 |
|
| 16 |
foreach ( $items AS $line ) { |
| 17 |
$csv = array( |
| 18 |
$line->url, |
| 19 |
$line->action_data, |
| 20 |
$line->regex, |
| 21 |
$line->action_type, |
| 22 |
$line->action->action_code, |
| 23 |
$line->match->action->type, |
| 24 |
$line->last_count, |
| 25 |
$line->title, |
| 26 |
); |
| 27 |
|
| 28 |
fputcsv( $stdout, $csv ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
function load( $group, $data, $filename = '' ) { |
| 33 |
$count = 0; |
| 34 |
$file = fopen( $filename, 'r' ); |
| 35 |
|
| 36 |
if ( $file ) { |
| 37 |
while ( ( $csv = fgetcsv( $file, 1000, ',' ) ) ) { |
| 38 |
if ( $csv[0] != 'source' && $csv[1] != 'target') { |
| 39 |
Red_Item::create( array( |
| 40 |
'source' => trim( $csv[0] ), |
| 41 |
'target' => trim( $csv[1] ), |
| 42 |
'regex' => $this->is_regex( $csv[0] ), |
| 43 |
'group' => $group, |
| 44 |
'match' => 'url', |
| 45 |
'red_action' => 'url' |
| 46 |
) ); |
| 47 |
|
| 48 |
$count++; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $count; |
| 54 |
} |
| 55 |
|
| 56 |
function is_regex( $url ) { |
| 57 |
$regex = '()[]$^*'; |
| 58 |
|
| 59 |
if ( strpbrk( $url, $regex ) === false ) |
| 60 |
return false; |
| 61 |
|
| 62 |
return true; |
| 63 |
} |
| 64 |
} |
| 65 |
|