PluginProbe
Redirection / 2.10
Redirection v2.10
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / fileio / csv.php

csv.php in Redirection 2.10, at fileio/csv.php

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