forked from wp-plugins/wp-gpx-maps
-
Notifications
You must be signed in to change notification settings - Fork 32
/
wp-gpx-maps-admin-tracks.php
255 lines (200 loc) · 7.21 KB
/
wp-gpx-maps-admin-tracks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* Tracks Tab
*
* Content for Tab Tracks and Cache.
*
* @package WP GPX Maps
*/
if ( ! ( is_admin() ) )
return;
$is_admin = current_user_can( 'publish_posts' );
if ( $is_admin != 1 )
return;
$wpgpxmapsUrl = get_admin_url() . 'admin.php?page=WP-GPX-Maps';
$gpxRegEx = '/.gpx$/i';
if ( current_user_can( 'manage_options' ) ) {
$menu_root = 'options-general.php';
} elseif ( current_user_can( 'publish_posts' ) ) {
$menu_root = 'admin.php';
}
/* The First Div (for body) starts in wp-gpx-admin.php */
if ( isset( $_POST['clearcache'] ) ) {
if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'wpgpx_clearcache_nonce' . $entry ) ) {
echo '<div class="notice notice-success"><p>';
esc_html_e( 'Cache is now empty!', 'wp-gpx-maps' );
echo '</p></div>';
wpgpxmaps_recursive_remove_directory( $cacheGpxPath, true );
}
}
if ( is_writable( $realGpxPath ) ) {
?>
<div class="tablenav-top">
<?php
echo '<form enctype="multipart/form-data" method="POST" style="float:left; margin:5px 20px 0 0" action="' . get_bloginfo( 'wpurl' ) . '/wp-admin/' . $menu_root . '?page=WP-GPX-Maps">';
?>
<?php esc_html_e( 'Choose a file to upload:', 'wp-gpx-maps' ); ?> <input name="uploadedfile[]" type="file" onchange="submitgpx(this);" multiple />
<?php
if ( isset( $_FILES['uploadedfile'] ) ) {
$total = count( $_FILES['uploadedfile']['name'] );
for ( $i = 0; $i < $total; $i++ ) {
$uploadingFileName = basename( $_FILES['uploadedfile']['name'][$i] );
$target_path = $realGpxPath . '/' . $uploadingFileName;
if ( preg_match( $gpxRegEx, $target_path ) ) {
if ( move_uploaded_file( $_FILES['uploadedfile']['tmp_name'][$i], $target_path ) ) {
echo '<div class="notice notice-success"><p>';
printf(
/* translators: %1s: GPX file name */
esc_html__( 'The file %1s has been successfully uploaded.', 'wp-gpx-maps' ),
'<span class="code"><strong>' . esc_html( $uploadingFileName ) . '</strong></span>'
);
echo '</p></div>';
} else {
echo '<div class=" notice notice-error"><p>';
esc_html_e( 'There was an error uploading the file, please try again!', 'wp-gpx-maps' );
echo '</p></div>';
}
} else {
echo '<div class="notice notice-warning"><p>';
esc_html_e( 'The file type is not supported!', 'wp-gpx-maps' );
echo '</p></div>';
}
}
}
?>
</form>
<form method="POST" style="float:left; margin:5px 20px 0 0" action="<?php echo $wpgpxmapsUrl; ?>&_wpnonce=<?php echo wp_create_nonce( 'wpgpx_clearcache_nonce' ); ?>" >
<input type="submit" name="clearcache" value="<?php esc_html_e( 'Clear Cache', 'wp-gpx-maps' ); ?>" />
</form>
</div>
<?php
} else {
?>
<br />
<?php
echo '<div class=" notice notice-error"><p>';
printf(
/* translators: %1s: Relative path of the GPX folder */
esc_html__( 'Your folder for GPX files %1s is not writable. Please change the folder permissions.', 'wp-gpx-maps' ),
'<span class="code"><strong>' . esc_html( $relativeGpxPath ) . '</strong></span>'
);
echo '</p></div>';
?>
<br />
<?php
}
$myGpxFileNames = array();
if ( is_readable( $realGpxPath ) && $handle = opendir( $realGpxPath ) ) {
while ( false !== ( $entry = readdir( $handle ) ) ) {
if ( preg_match( $gpxRegEx, $entry ) ) {
if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'wpgpx_deletefile_nonce_' . $entry ) ) {
if ( file_exists( $realGpxPath . '/' . $entry ) ) {
unlink( $realGpxPath . '/' . $entry );
echo '<div class="notice notice-success"><p>';
printf(
/* translators: %1s: GPX file name */
esc_html__( 'The file %1s has been successfully deleted.', 'wp-gpx-maps' ),
'<span class="code"><strong>' . esc_html( $entry ) . '</strong></span>'
);
echo '</p></div>';
} else {
echo '<div class=" notice notice-error"><p>';
printf(
/* translators: %1s: GPX file name */
esc_html__( 'The file %1s could not be deleted.', 'wp-gpx-maps' ),
'<span class="code"><strong>' . esc_html( $entry ) . '</strong></span>'
);
echo '</p></div>';
}
} else {
$myFile = $realGpxPath . '/' . $entry;
$myGpxFileNames[] = array(
'name' => $entry,
'size' => filesize( $myFile ),
'lastedit' => filemtime( $myFile ),
'nonce' => wp_create_nonce( 'wpgpx_deletefile_nonce_' . $entry ),
);
}
}
}
closedir( $handle );
}
if ( is_readable( $realGpxPath ) && $handle = opendir( $realGpxPath ) ) {
while ( false !== ( $entry = readdir( $handle ) ) ) {
if ( preg_match( $gpxRegEx, $entry ) ) {
$filenames[] = $realGpxPath . '/' . $entry;
}
}
closedir( $handle );
}
?>
<table id="table" class="wp-list-table widefat plugins"></table>
<script type="text/javascript">
function submitgpx(el){
var newEl = document.createElement('span');
newEl.innerHTML = '<?php esc_html_e( 'Uploading file...', 'wp-gpx-maps' ); ?>';
el.parentNode.insertBefore(newEl,el.nextSibling);
el.parentNode.submit()
}
jQuery('#table').bootstrapTable({
columns: [{
field: 'name',
title: '<?php _e( 'File', 'wp-gpx-maps' ); ?>',
sortable: true,
formatter: function(value, row, index) {
return [
'<b>' + row.name + '</b><br />',
'<a class="delete_gpx_row" href="<?php echo $wpgpxmapsUrl; ?>&_wpnonce=' + row.nonce + '" ><?php esc_html_e( 'Delete', 'wp-gpx-maps' ); ?></a>',
' | ',
'<a href="<?php echo $relativeGpxPath; ?>' + row.name + '"><?php esc_html_e( 'Download', 'wp-gpx-maps' ); ?></a>',
' | ',
'<a href="#" class="copy-shortcode" title="<?php esc_html_e( 'Copy shortcode', 'wp-gpx-maps' ); ?>"><?php esc_html_e( 'Shortcode:', 'wp-gpx-maps' ); ?></a> <span class="code"> [sgpx gpx="<?php echo $relativeGpxPath; ?>' + row.name + '"]</span>',
].join('')
}
}, {
field: 'lastedit',
title: '<?php esc_html_e( 'Last modified', 'wp-gpx-maps' ); ?>',
sortable: true,
formatter: function(value, row, index) {
var d = new Date(value*1000);
return d.toLocaleDateString() + " " + d.toLocaleTimeString();
}
}, {
field: 'size',
title: '<?php esc_html_e( 'File size', 'wp-gpx-maps' ); ?>',
sortable: true,
formatter: function(value, row, index) { return humanFileSize(value); }
}],
sortName : 'lastedit',
sortOrder : 'desc',
data: <?php echo wp_json_encode( $myGpxFileNames ); ?>
});
jQuery('.delete_gpx_row').click(function(){
return confirm("<?php esc_html_e( 'Are you sure you want to delete the file?', 'wp-gpx-maps' ); ?>");
})
jQuery('.copy-shortcode').click(function(e){
var $temp = jQuery("<input>");
jQuery("body").append($temp);
var shortcode = jQuery(this).next().text().trim();
$temp.val(shortcode).select();
document.execCommand("copy");
$temp.remove();
e.preventDefault();
})
function humanFileSize(bytes, si) {
var thresh = si ? 1000 : 1024;
if(Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = si
? ['kB','MB','GB','TB','PB','EB','ZB','YB']
: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1)+' '+units[u];
};
</script>
</div>