PluginProbe
Redirection / 5.5.2
Redirection v5.5.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 / fileio / json.php

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

85 lines 2.2 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 header( 'Content-Type: application/json' );
8 header( 'Content-Disposition: attachment; filename="' . $this->export_filename( 'json' ) . '"' );
9 }
10
11 public function get_data( array $items, array $groups ) {
12 $version = red_get_plugin_data( dirname( dirname( __FILE__ ) ) . '/redirection.php' );
13
14 $items = array(
15 'plugin' => array(
16 'version' => trim( $version['Version'] ),
17 'date' => date( 'r' ),
18 ),
19 'groups' => $groups,
20 'redirects' => array_map( function( $item ) {
21 return $item->to_json();
22 }, $items ),
23 );
24
25 return wp_json_encode( $items, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ) . PHP_EOL;
26 }
27
28 public function load( $group, $filename, $data ) {
29 global $wpdb;
30
31 $count = 0;
32 $json = @json_decode( $data, true );
33 if ( $json === false ) {
34 return 0;
35 }
36
37 // Import groups
38 $group_map = array();
39
40 if ( isset( $json['groups'] ) ) {
41 foreach ( $json['groups'] as $group ) {
42 $old_group_id = $group['id'];
43 unset( $group['id'] );
44
45 $group = Red_Group::create( $group['name'], $group['module_id'], $group['enabled'] ? true : false );
46 if ( $group ) {
47 $group_map[ $old_group_id ] = $group->get_id();
48 }
49 }
50 }
51
52 unset( $json['groups'] );
53
54 // Import redirects
55 if ( isset( $json['redirects'] ) ) {
56 foreach ( $json['redirects'] as $pos => $redirect ) {
57 unset( $redirect['id'] );
58
59 if ( ! isset( $group_map[ $redirect['group_id'] ] ) ) {
60 $new_group = Red_Group::create( 'Group', 1 );
61 $group_map[ $redirect['group_id'] ] = $new_group->get_id();
62 }
63
64 if ( $redirect['match_type'] === 'url' && isset( $redirect['action_data'] ) && ! is_array( $redirect['action_data'] ) ) {
65 $redirect['action_data'] = array( 'url' => $redirect['action_data'] );
66 }
67
68 $redirect['group_id'] = $group_map[ $redirect['group_id'] ];
69 $created = Red_Item::create( $redirect );
70
71 if ( $created instanceof Red_Item ) {
72 $count++;
73 }
74
75 // Helps reduce memory usage
76 unset( $json['redirects'][ $pos ] );
77 $wpdb->queries = array();
78 $wpdb->num_queries = 0;
79 }
80 }
81
82 return $count;
83 }
84 }
85