PluginProbe
Bubble Menu – Floating Button Menu with Sticky Navigation / 4.0
Bubble Menu – Floating Button Menu with Sticky Navigation v4.0
trunk 1.3 2.0 2.2 2.2.1 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.1 4.1.1
bubble-menu / classes / Admin / ImporterExporter.php

ImporterExporter.php in Bubble Menu – Floating Button Menu with Sticky Navigation 4.0, at classes/Admin/ImporterExporter.php

197 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Display the export/import form.
4 */
5
6 namespace BubbleMenu\Admin;
7
8 defined( 'ABSPATH' ) || exit;
9
10 use BubbleMenu\WOWP_Plugin;
11
12 /**
13 * Class ImporterExporter
14 *
15 * This class provides functionality for exporting and importing data.
16 */
17 class ImporterExporter {
18
19 public static function form_export(): void {
20 ?>
21 <form method="post">
22 <p></p>
23 <p>
24 <?php
25 submit_button( __( 'Export All Data', 'bubble-menu' ), 'secondary', 'submit', false ); ?><?php
26 wp_nonce_field( WOWP_Plugin::PREFIX . '_nonce', WOWP_Plugin::PREFIX . '_export_data' ); ?>
27 </p>
28 </form>
29
30 <?php
31 }
32
33 public static function form_import(): void {
34 ?>
35 <form method="post" enctype="multipart/form-data" action="">
36 <p>
37 <span class="wpie-file">
38 <input type="file" name="import_file" accept="*.json"/>
39 </span>
40 </p>
41 <p>
42 <label>
43 <input type="checkbox" name="wpie_import_update" value="1">
44 <?php esc_html_e( 'Update item if item already exists.', 'bubble-menu' ); ?>
45 </label>
46
47 </p>
48
49 <p>
50 <?php
51 submit_button( __( 'Import', 'bubble-menu' ), 'secondary', 'submit', false ); ?><?php
52 wp_nonce_field( WOWP_Plugin::PREFIX . '_nonce', WOWP_Plugin::PREFIX . '_import_data' ); ?>
53 </p>
54 </form>
55
56 <?php
57 }
58
59 /**
60 * @throws \JsonException
61 */
62 public static function import_data(): void {
63 $verify = AdminActions::verify(WOWP_Plugin::PREFIX . '_import_data');
64
65 if ( ! $verify ) {
66 return;
67 }
68
69 if ( self::get_file_extension( sanitize_text_field( $_FILES['import_file']['name'] ) ) !== 'json' ) {
70 wp_die(
71 esc_html__( 'Please upload a valid .json file', 'bubble-menu' ),
72 esc_html__( 'Error', 'bubble-menu' ),
73 [ 'response' => 400 ] );
74 }
75
76
77 $import_file = sanitize_text_field( $_FILES['import_file']['tmp_name'] );
78 $settings = wp_json_file_decode( $import_file );
79
80 $columns = DBManager::get_columns();
81
82 $update = ! empty( $_POST['wpie_import_update'] ) ? '1' : '';
83
84 foreach ( $settings as $key => $val ) {
85 $data = [];
86 $formats = [];
87
88 foreach ( $columns as $column ) {
89 $name = $column->Field;
90 $data[ $name ] = ! empty( $val->$name ) ? $val->$name : '';
91 if ( $name === 'id' || $name === 'status' || $name === 'mode' ) {
92 $formats[] = '%d';
93 } else {
94 $formats[] = '%s';
95 }
96 }
97
98 $check_row = DBManager::check_row( $data['id'] );
99
100 if ( ! empty( $update ) && ! empty( $check_row ) ) {
101 $where = [
102 'id' => absint( $data['id'] ),
103 ];
104 $index = array_search( 'id', array_keys( $data ), true );
105 unset( $data['id'], $formats[ $index ] );
106
107 DBManager::update( $data, $where, $formats );
108 } elseif ( ! empty( $check_row ) ) {
109 $index = array_search( 'id', array_keys( $data ), true );
110 unset( $data['id'], $formats[ $index ] );
111
112 DBManager::insert( $data, $formats );
113 } else {
114 DBManager::insert( $data, $formats );
115 }
116 }
117
118 $redirect_link = add_query_arg( [
119 'page' => WOWP_Plugin::SLUG,
120 ], admin_url( 'admin.php' ) );
121
122 wp_safe_redirect( $redirect_link );
123 exit;
124 }
125
126 private static function get_file_extension( $str ) {
127 $parts = explode( '.', $str );
128
129 return end( $parts );
130 }
131
132 /**
133 * @throws \JsonException
134 */
135 public static function export_item( $id = 0, $action = '' ): bool {
136 $verify = AdminActions::verify(WOWP_Plugin::PREFIX . '_export_item');
137
138 if ( ! $verify ) {
139 return false;
140 }
141
142 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
143 $action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : $action;
144 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
145
146 if ( ( $page !== WOWP_Plugin::SLUG ) || ( $action !== 'export' ) || empty( $id ) ) {
147 return false;
148 }
149
150 $data = DBManager::get_data_by_id( $id );
151 if ( ! $data ) {
152 return false;
153 }
154
155 $name = trim( $data->title );
156 $name = str_replace( ' ', '-', $name );
157 $file_name = $name . '-database-' . gmdate( 'm-d-Y' ) . '.json';
158 self::export( $file_name, [ $data ] );
159
160 return true;
161 }
162
163 /**
164 * @throws \JsonException
165 */
166 public static function export_data(): bool {
167 $verify = AdminActions::verify(WOWP_Plugin::PREFIX . '_export_data');
168
169 if ( ! $verify ) {
170 return false;
171 }
172
173 $file_name = WOWP_Plugin::SHORTCODE . '-database-' . gmdate( 'm-d-Y' ) . '.json';
174 $data = DBManager::get_all_data();
175 if ( empty( $data ) ) {
176 return false;
177 }
178 self::export( $file_name, $data );
179
180 return true;
181 }
182
183 /**
184 * @throws \JsonException
185 */
186 private static function export( $file_name, $data ): void {
187 ignore_user_abort( true );
188 nocache_headers();
189 header( 'Content-Type: application/json; charset=utf-8' );
190 header( 'Content-Disposition: attachment; filename=' . $file_name );
191 header( "Expires: 0" );
192
193 echo wp_json_encode( $data );
194 exit;
195 }
196
197 }