PluginProbe
Float menu – awesome floating side menu / 7.2.1
Float menu – awesome floating side menu v7.2.1
7.2.5 trunk 2.1 2.2 3.0.1 3.1 3.2.2 3.3.1 3.5 3.5.1 3.5.2 3.5.3. 3.5.4 4.0 4.1 4.1.1 4.2 4.3 4.3.1 4.3.2 5.0 5.0.1 5.0.2 5.0.3 5.1 All 57 releases
float-menu / classes / Admin / ImporterExporter.php

ImporterExporter.php in Float menu – awesome floating side menu 7.2.1, at classes/Admin/ImporterExporter.php

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