PluginProbe
Bubble Menu – Floating Button Menu with Sticky Navigation / 4.0.7
Bubble Menu – Floating Button Menu with Sticky Navigation v4.0.7
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.7, at classes/Admin/ImporterExporter.php

208 lines 5.3 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 ( ! isset( $_FILES['import_file'] ) || empty( $_FILES['import_file']['name'] ) ) {
70 wp_die( esc_attr__( 'Please select a file to import', 'bubble-menu' ),
71 esc_attr__( 'Error', 'bubble-menu' ),
72 [ 'response' => 400 ] );
73 }
74
75 if ( self::get_file_extension( sanitize_text_field( $_FILES['import_file']['name'] ) ) !== 'json' ) {
76 wp_die(
77 esc_html__( 'Please upload a valid .json file', 'bubble-menu' ),
78 esc_html__( 'Error', 'bubble-menu' ),
79 [ 'response' => 400 ] );
80 }
81
82 if ( empty( $_FILES['import_file']['tmp_name'] ) ) {
83 wp_die( esc_attr__( 'Please select a file to import', 'bubble-menu' ),
84 esc_attr__( 'Error', 'bubble-menu' ),
85 [ 'response' => 400 ] );
86 }
87
88 $import_file = sanitize_text_field( $_FILES['import_file']['tmp_name'] );
89 $settings = wp_json_file_decode( $import_file );
90
91 $columns = DBManager::get_columns();
92
93 $update = ! empty( $_POST['wpie_import_update'] ) ? '1' : '';
94
95 foreach ( $settings as $key => $val ) {
96 $data = [];
97 $formats = [];
98
99 foreach ( $columns as $column ) {
100 $name = $column->Field;
101 $data[ $name ] = ! empty( $val->$name ) ? $val->$name : '';
102 if ( $name === 'id' || $name === 'status' || $name === 'mode' ) {
103 $formats[] = '%d';
104 } else {
105 $formats[] = '%s';
106 }
107 }
108
109 $check_row = DBManager::check_row( $data['id'] );
110
111 if ( ! empty( $update ) && ! empty( $check_row ) ) {
112 $where = [
113 'id' => absint( $data['id'] ),
114 ];
115 $index = array_search( 'id', array_keys( $data ), true );
116 unset( $data['id'], $formats[ $index ] );
117
118 DBManager::update( $data, $where, $formats );
119 } elseif ( ! empty( $check_row ) ) {
120 $index = array_search( 'id', array_keys( $data ), true );
121 unset( $data['id'], $formats[ $index ] );
122
123 DBManager::insert( $data, $formats );
124 } else {
125 DBManager::insert( $data, $formats );
126 }
127 }
128
129 $redirect_link = add_query_arg( [
130 'page' => WOWP_Plugin::SLUG,
131 ], admin_url( 'admin.php' ) );
132
133 wp_safe_redirect( $redirect_link );
134 exit;
135 }
136
137 private static function get_file_extension( $str ) {
138 $parts = explode( '.', $str );
139
140 return end( $parts );
141 }
142
143 /**
144 * @throws \JsonException
145 */
146 public static function export_item( $id = 0, $action = '' ): bool {
147 $verify = AdminActions::verify(WOWP_Plugin::PREFIX . '_export_item');
148
149 if ( ! $verify ) {
150 return false;
151 }
152
153 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
154 $action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : $action;
155 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
156
157 if ( ( $page !== WOWP_Plugin::SLUG ) || ( $action !== 'export' ) || empty( $id ) ) {
158 return false;
159 }
160
161 $data = DBManager::get_data_by_id( $id );
162 if ( ! $data ) {
163 return false;
164 }
165
166 $name = trim( $data->title );
167 $name = str_replace( ' ', '-', $name );
168 $file_name = $name . '-database-' . gmdate( 'm-d-Y' ) . '.json';
169 self::export( $file_name, [ $data ] );
170
171 return true;
172 }
173
174 /**
175 * @throws \JsonException
176 */
177 public static function export_data(): bool {
178 $verify = AdminActions::verify(WOWP_Plugin::PREFIX . '_export_data');
179
180 if ( ! $verify ) {
181 return false;
182 }
183
184 $file_name = WOWP_Plugin::SHORTCODE . '-database-' . gmdate( 'm-d-Y' ) . '.json';
185 $data = DBManager::get_all_data();
186 if ( empty( $data ) ) {
187 return false;
188 }
189 self::export( $file_name, $data );
190
191 return true;
192 }
193
194 /**
195 * @throws \JsonException
196 */
197 private static function export( $file_name, $data ): void {
198 ignore_user_abort( true );
199 nocache_headers();
200 header( 'Content-Type: application/json; charset=utf-8' );
201 header( 'Content-Disposition: attachment; filename=' . $file_name );
202 header( "Expires: 0" );
203
204 echo wp_json_encode( $data );
205 exit;
206 }
207
208 }