PluginProbe
Redirection / 2.8.1
Redirection v2.8.1
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.8.1, at fileio/json.php

71 lines 1.7 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 $count = 0;
32 $json = @json_decode( $data );
33 if ( $json === false ) {
34 return 0;
35 }
36
37 // Import groups
38 $groups = array();
39 $group_map = array();
40
41 if ( isset( $json->groups ) ) {
42 foreach ( $json->groups as $group ) {
43 $old_group_id = $group->id;
44 unset( $group->id );
45
46 $group = Red_Group::create( $group->name, $group->module_id );
47 if ( $group ) {
48 $group_map[ $old_group_id ] = $group->get_id();
49 }
50 }
51 }
52
53 // Import redirects
54 if ( isset( $json->redirects ) ) {
55 foreach ( $json->redirects as $redirect ) {
56 unset( $redirect->id );
57
58 if ( ! isset( $group_map[ $redirect->group_id ] ) ) {
59 $group_map[ $redirect->group_id ] = Red_Group::create( 'Group', 1 );
60 }
61
62 $redirect->group_id = $group_map[ $redirect->group_id ];
63 $redirect = Red_Item::create( (array)$redirect );
64 $count++;
65 }
66 }
67
68 return $count;
69 }
70 }
71