PluginProbe
Redirection / 2.9
Redirection v2.9
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 / json.php

json.php in Redirection 2.9, at fileio/json.php

84 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Red_Json_File extends Red_FileIO {
4 public function force_download() {
5 parent::force_download();
6
7 $filename = 'redirection-'.date_i18n( get_option( 'date_format' ) ).'.json';
8
9 header( 'Content-Type: application/json' );
10 header( 'Content-Disposition: attachment; filename="'.$filename.'"' );
11 }
12
13 public function get_data( array $items, array $groups ) {
14 $version = get_plugin_data( dirname( dirname( __FILE__ ) ).'/redirection.php' );
15
16 $items = array(
17 'plugin' => array(
18 'version' => trim( $version['Version'] ),
19 'date' => date( 'r' ),
20 ),
21 'groups' => $groups,
22 'redirects' => array_map( function( $item ) {
23 return $item->to_json();
24 }, $items ),
25 );
26
27 return json_encode( $items, JSON_PRETTY_PRINT ).PHP_EOL;
28 }
29
30 public function load( $group, $filename, $data ) {
31 global $wpdb;
32
33 $count = 0;
34 $json = @json_decode( $data, true );
35 if ( $json === false ) {
36 return 0;
37 }
38
39 // Import groups
40 $groups = array();
41 $group_map = array();
42
43 if ( isset( $json['groups'] ) ) {
44 foreach ( $json['groups'] as $group ) {
45 $old_group_id = $group['id'];
46 unset( $group['id'] );
47
48 $group = Red_Group::create( $group['name'], $group['module_id'] );
49 if ( $group ) {
50 $group_map[ $old_group_id ] = $group->get_id();
51 }
52 }
53 }
54
55 unset( $json['groups'] );
56
57 // Import redirects
58 if ( isset( $json['redirects'] ) ) {
59 foreach ( $json['redirects'] as $pos => $redirect ) {
60 unset( $redirect['id'] );
61
62 if ( ! isset( $group_map[ $redirect['group_id'] ] ) ) {
63 $group_map[ $redirect['group_id'] ] = Red_Group::create( 'Group', 1 );
64 }
65
66 if ( $redirect['match_type'] === 'url' && isset( $redirect['action_data'] ) && ! is_array( $redirect['action_data'] ) ) {
67 $redirect['action_data'] = array( 'url' => $redirect['action_data'] );
68 }
69
70 $redirect['group_id'] = $group_map[ $redirect['group_id'] ];
71 Red_Item::create( $redirect );
72 $count++;
73
74 // Helps reduce memory usage
75 unset( $json['redirects'][$pos] );
76 $wpdb->queries = array();
77 $wpdb->num_queries = 0;
78 }
79 }
80
81 return $count;
82 }
83 }
84