PluginProbe
Redirection / 4.2
Redirection v4.2
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 / models / file-io.php

file-io.php in Redirection 4.2, at models/file-io.php

102 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class Red_FileIO {
4 public static function create( $type ) {
5 $exporter = false;
6
7 if ( $type === 'rss' ) {
8 include_once dirname( dirname( __FILE__ ) ) . '/fileio/rss.php';
9 $exporter = new Red_Rss_File();
10 } elseif ( $type === 'csv' ) {
11 include_once dirname( dirname( __FILE__ ) ) . '/fileio/csv.php';
12 $exporter = new Red_Csv_File();
13 } elseif ( $type === 'apache' ) {
14 include_once dirname( dirname( __FILE__ ) ) . '/fileio/apache.php';
15 $exporter = new Red_Apache_File();
16 } elseif ( $type === 'nginx' ) {
17 include_once dirname( dirname( __FILE__ ) ) . '/fileio/nginx.php';
18 $exporter = new Red_Nginx_File();
19 } elseif ( $type === 'json' ) {
20 include_once dirname( dirname( __FILE__ ) ) . '/fileio/json.php';
21 $exporter = new Red_Json_File();
22 }
23
24 return $exporter;
25 }
26
27 public static function import( $group_id, $file ) {
28 $parts = pathinfo( $file['name'] );
29 $extension = isset( $parts['extension'] ) ? $parts['extension'] : '';
30 $extension = strtolower( $extension );
31
32 if ( $extension === 'csv' ) {
33 include_once dirname( dirname( __FILE__ ) ) . '/fileio/csv.php';
34 $importer = new Red_Csv_File();
35 $data = '';
36 } elseif ( $extension === 'json' ) {
37 include_once dirname( dirname( __FILE__ ) ) . '/fileio/json.php';
38 $importer = new Red_Json_File();
39 $data = @file_get_contents( $file['tmp_name'] );
40 } else {
41 include_once dirname( dirname( __FILE__ ) ) . '/fileio/apache.php';
42 $importer = new Red_Apache_File();
43 $data = @file_get_contents( $file['tmp_name'] );
44 }
45
46 if ( $extension !== 'json' ) {
47 $group = Red_Group::get( $group_id );
48 if ( ! $group ) {
49 return false;
50 }
51 }
52
53 return $importer->load( $group_id, $file['tmp_name'], $data );
54 }
55
56 public function force_download() {
57 header( 'Cache-Control: no-cache, must-revalidate' );
58 header( 'Expires: Mon, 26 Jul 1997 05:00:00 GMT' );
59 }
60
61 protected function export_filename( $extension ) {
62 $name = wp_parse_url( home_url(), PHP_URL_HOST );
63 $name = str_replace( '.', '-', $name );
64 $date = strtolower( date_i18n( get_option( 'date_format' ) ) );
65 $date = str_replace( [ ',', ' ', '--' ], '-', $date );
66
67 return 'redirection-' . $name . '-' . $date . '.' . $extension;
68 }
69
70 public static function export( $module_name_or_id, $format ) {
71 $groups = false;
72 $items = false;
73
74 if ( $module_name_or_id === 'all' || $module_name_or_id === 0 ) {
75 $groups = Red_Group::get_all();
76 $items = Red_Item::get_all();
77 } else {
78 $module_name_or_id = is_numeric( $module_name_or_id ) ? $module_name_or_id : Red_Module::get_id_for_name( $module_name_or_id );
79 $module = Red_Module::get( intval( $module_name_or_id, 10 ) );
80
81 if ( $module ) {
82 $groups = Red_Group::get_all_for_module( $module->get_id() );
83 $items = Red_Item::get_all_for_module( $module->get_id() );
84 }
85 }
86
87 $exporter = self::create( $format );
88 if ( $exporter && $items !== false && $groups !== false ) {
89 return array(
90 'data' => $exporter->get_data( $items, $groups ),
91 'total' => count( $items ),
92 'exporter' => $exporter,
93 );
94 }
95
96 return false;
97 }
98
99 abstract function get_data( array $items, array $groups );
100 abstract function load( $group, $filename, $data );
101 }
102