PluginProbe
Redirection / 5.4
Redirection v5.4
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 5.4, at models/file-io.php

104 lines 3.2 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' || $extension === 'txt' ) {
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
64 $name = sanitize_text_field( $name );
65 $name = str_replace( '.', '-', $name );
66 $date = strtolower( date_i18n( get_option( 'date_format' ) ) );
67 $date = str_replace( [ ',', ' ', '--' ], '-', $date );
68
69 return 'redirection-' . $name . '-' . $date . '.' . sanitize_text_field( $extension );
70 }
71
72 public static function export( $module_name_or_id, $format ) {
73 $groups = false;
74 $items = false;
75
76 if ( $module_name_or_id === 'all' || $module_name_or_id === 0 ) {
77 $groups = Red_Group::get_all();
78 $items = Red_Item::get_all();
79 } else {
80 $module_name_or_id = is_numeric( $module_name_or_id ) ? $module_name_or_id : Red_Module::get_id_for_name( $module_name_or_id );
81 $module = Red_Module::get( intval( $module_name_or_id, 10 ) );
82
83 if ( $module ) {
84 $groups = Red_Group::get_all_for_module( $module->get_id() );
85 $items = Red_Item::get_all_for_module( $module->get_id() );
86 }
87 }
88
89 $exporter = self::create( $format );
90 if ( $exporter && $items !== false && $groups !== false ) {
91 return [
92 'data' => $exporter->get_data( $items, $groups ),
93 'total' => count( $items ),
94 'exporter' => $exporter,
95 ];
96 }
97
98 return false;
99 }
100
101 abstract public function get_data( array $items, array $groups );
102 abstract public function load( $group, $filename, $data );
103 }
104