PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.5.2
WP Coder – Insert & Manage Code Snippets v4.5.2
4.5.2 4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 All 40 releases
← All changes | classes/Dashboard/ImporterExporter.php +84 -16 3.0 → 4.5.2 View file →
@@ -3,21 +3,45 @@
3 3 namespace WPCoder\Dashboard;
4 4
5 5 defined( 'ABSPATH' ) || exit;
6 6
7 -use WPCoder\WOW_Plugin;
7 +use WPCoder\WPCoder;
8 8
9 9 class ImporterExporter {
10 10 private $settings;
11 11
12 +
13 +
12 14 public static function form_export(): void {
13 15 ?>
14 16 <form method="post">
15 - <p></p>
16 - <p>
17 - <?php submit_button( __( 'Export All Data', 'wpcoder' ), 'secondary', 'submit', false ); ?>
18 - <?php wp_nonce_field( WOW_Plugin::PREFIX . '_nonce', WOW_Plugin::PREFIX . '_export_data' ); ?>
19 - </p>
17 + <p/>
18 + <?php
19 + $tags = DBManager::get_tags_from_table();
20 +
21 + $tag_search = ( ! empty( $_REQUEST['tag'] ) ) ? sanitize_text_field( $_REQUEST ['tag'] ) : '';
22 + $tag_search = ( $tag_search === 'all' ) ? '' : $tag_search;
23 +
24 + echo '<div class="actions"><label for="filter-by-tag" class="screen-reader-text">' . esc_attr__( 'Filter by tag',
25 + 'wp-coder' ) . '</label>';
26 + echo '<select name="tag" id="filter-by-tag">';
27 + echo '<option value="all"' . selected( 'all', $tag_search, false ) . '>' . esc_attr__( 'All',
28 + 'wp-coder' ) . '</option>';
29 +
30 + if ( ! empty( $tags ) ) {
31 + foreach ( $tags as $tag ) {
32 + if ( empty( $tag['tag'] ) ) {
33 + continue;
34 + }
35 + echo '<option value="' . esc_attr( trim( $tag['tag'] ) ) . '"' . selected( $tag['tag'], $tag_search,
36 + false ) . '>' . esc_html( $tag['tag'] ) . '</option>';
37 + }
38 + }
39 + echo '</select>';
40 + submit_button( __( 'Export Data', 'wp-coder' ), 'secondary', 'submit', false );
41 + echo '</div>';
42 + wp_nonce_field( WPCoder::PREFIX . '_nonce', WPCoder::PREFIX . '_export_data' );
43 + ?>
20 44 </form>
21 45
22 46 <?php
23 47 }
@@ -28,20 +52,22 @@
28 52 <p>
29 53 <span class="wowp-file">
30 54 <input type="file" name="import_file" accept="*.json"/>
31 55 </span>
56 + <br>
57 + Upload a valid WP Coder Pro .json file exported from another site.
32 58 </p>
33 59 <p>
34 60 <label>
35 61 <input type="checkbox" name="wowp_import_update" value="1">
36 - <?php esc_attr_e( 'Update item if item already exists.' . 'wpcoder' ); ?>
62 + <?php esc_attr_e( 'Update item if item already exists.', 'wp-coder' ); ?>
37 63 </label>
38 64
39 65 </p>
40 66
41 67 <p>
42 - <?php submit_button( __( 'Import', 'wpcoder' ), 'secondary', 'submit', false ); ?>
43 - <?php wp_nonce_field( WOW_Plugin::PREFIX . '_nonce', WOW_Plugin::PREFIX . '_import_data' ); ?>
68 + <?php submit_button( __( 'Import Settings', 'wp-coder' ), 'secondary', 'submit', false ); ?>
69 + <?php wp_nonce_field( WPCoder::PREFIX . '_nonce', WPCoder::PREFIX . '_import_data' ); ?>
44 70 </p>
45 71 </form>
46 72
47 73 <?php
@@ -49,9 +75,9 @@
49 75
50 76 public static function import_data(): void {
51 77
52 78 if ( self::get_file_extension( $_FILES['import_file']['name'] ) != 'json' ) {
53 - wp_die( __( 'Please upload a valid .json file', 'wpcoder' ), __( 'Error', 'wpcoder' ),
79 + wp_die( esc_attr__( 'Please upload a valid .json file', 'wp-coder' ), esc_attr__( 'Error', 'wp-coder' ),
54 80 [ 'response' => 400 ] );
55 81 }
56 82
57 83 $import_file = $_FILES['import_file']['tmp_name'];
@@ -97,9 +123,9 @@
97 123 }
98 124 }
99 125
100 126 $redirect_link = add_query_arg( [
101 - 'page' => WOW_Plugin::SLUG,
127 + 'page' => WPCoder::SLUG,
102 128 ], admin_url( 'admin.php' ) );
103 129
104 130 wp_safe_redirect( $redirect_link );
105 131 exit;
@@ -111,15 +137,15 @@
111 137
112 138 return end( $parts );
113 139 }
114 140
115 - public static function export_item($id = 0, $action = '') {
141 + public static function export_item( $id = 0, $action = '' ): bool {
116 142
117 143 $page = isset( $_GET['page'] ) ? sanitize_text_field( $_GET['page'] ) : '';
118 144 $action = isset( $_GET['action'] ) ? sanitize_text_field( $_GET['action'] ) : $action;
119 145 $id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : $id;
120 146
121 - if ( ( $page !== WOW_Plugin::SLUG ) || ( $action !== 'export' ) || empty( $id ) ) {
147 + if ( ( $page !== WPCoder::SLUG ) || ( $action !== 'export' ) || empty( $id ) ) {
122 148 return false;
123 149 }
124 150
125 151 $data = DBManager::get_data_by_id( $id );
@@ -128,17 +154,59 @@
128 154 }
129 155
130 156 $name = trim( $data->title );
131 157 $name = str_replace( ' ', '-', $name );
132 - $file_name = $name . '-database-' . date( 'm-d-Y' ) . '.json';
158 + $file_name = WPCoder::info( 'data' ) . '-' . $name . '-id-' . $id . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
133 159 self::export( $file_name, [ $data ] );
134 160
135 161 return true;
136 162 }
137 163
164 + public static function export_items(): bool {
165 +
166 + if ( isset( $_REQUEST['filter_action'] ) && ! empty( $_REQUEST['filter_action'] ) ) {
167 + return false;
168 + }
169 +
170 + if ( !isset( $_REQUEST['action'] ) || 'export' !== $_REQUEST['action'] ) {
171 + return false;
172 + }
173 +
174 + $name = WPCoder::PREFIX . '_list_action';
175 + $nonce_action = WPCoder::PREFIX . '_nonce';
176 +
177 + if( ! isset( $_POST[ $name ] ) || ! wp_verify_nonce( $_POST[ $name ], $nonce_action ) || ! WPCoder::user_can_manage() ) {
178 + return false;
179 + }
180 +
181 + $ids = isset( $_POST['ID'] ) ? ( map_deep( $_POST['ID'], 'absint' ) ) : false;
182 +
183 +
184 + if ( empty( $ids ) || ! is_array( $ids ) ) {
185 + return false;
186 + }
187 +
188 + $data = DBManager::get_data_by_ids( $ids );
189 + if ( ! $data ) {
190 + return false;
191 + }
192 +
193 + $file_name = WPCoder::info( 'data' ) . '-items-' . implode( '-', array_slice( $ids, 0, 3 ) ) . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
194 + self::export( $file_name, $data );
195 +
196 + return true;
197 + }
198 +
138 199 public static function export_data(): bool {
139 - $file_name = WOW_Plugin::SHORTCODE . '-database-' . date( 'm-d-Y' ) . '.json';
140 - $data = DBManager::get_all_data();
200 + $tag = isset( $_POST['tag'] ) ? sanitize_text_field( wp_unslash( $_POST['tag'] ) ) : '';
201 + if ( ! empty( $tag ) ) {
202 + $file_name = WPCoder::info( 'data' ) . '-tag-' . esc_attr( $tag ) . '-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
203 + $data = DBManager::get_data_by_tag( $tag );
204 + } else {
205 + $file_name = WPCoder::info( 'data' ) . '-database-' . gmdate( 'Y-m-d-H-i-s' ) . '.json';
206 + $data = DBManager::get_all_data();
207 + }
208 +
141 209 if ( empty( $data ) ) {
142 210 return false;
143 211 }
144 212 self::export( $file_name, $data );