-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwp-media-zip-upload.php
71 lines (58 loc) · 1.94 KB
/
wp-media-zip-upload.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
<?php
/*
Plugin Name: WP Media Zip Upload
Plugin URI: https://github.com/yawalkar/wp-media-zip-upload
Description: Restrict other extensions for your plugin admin upload functionality.
Author: Nitin Yawalkar
Author URI: https://www.nitinyawalkar.com
Version: 1.0
*/
if( !class_exists( "NY_WP_ZIP_UPLOAD" ) ){
class NY_WP_ZIP_UPLOAD{
function __construct(){
add_action( 'admin_menu', array( $this, 'ny_plugin_setup_menu' ) );
}
function ny_plugin_setup_menu(){
$page = add_menu_page(
'Plugin Admin Page',
'ZIP Upload',
'administrator',
'ny-zip-upload',
array( $this, 'zip_upload_init' )
);
add_action('admin_print_scripts-'.$page, array( $this, 'admin_media_scripts' ) );
}
function admin_media_scripts(){
wp_enqueue_media();
wp_enqueue_script("ny-media-js", plugins_url( "media.js", __FILE__ ), array( 'jquery' ),'',true );
}
function zip_upload_init(){
?>
<div class="wrap about-wrap">
<h1>WP Media Zip Upload</h1>
<div class="about-text">This is just a test plugin to test the zip upload functionality by restricting all other file extensions.</div>
<div class="wp-container">
<input type="text" class="wp_upload_field" />
<button class="button button-primary wp_uploader_button">Upload Zip</button>
</div>
</div>
<?php
}
}
new NY_WP_ZIP_UPLOAD;
}
// Function to handle and restrict upload to our custom extension only for our plugin admin page
if( !function_exists( "ny_import_upload_prefilter" ) ){
add_filter( 'wp_handle_upload_prefilter', 'ny_import_upload_prefilter' );
function ny_import_upload_prefilter( $file )
{
$page = isset( $_POST['admin_page'] ) ? $_POST['admin_page'] : '';
if( isset( $page ) && $page == "ny-zip-upload" ) {
$ext = pathinfo( $file['name'], PATHINFO_EXTENSION );
if ( $ext !== "zip" ) {
$file['error'] = "The uploaded ". $ext ." file is not supported. Please upload the exported text file. e.g. .zip";
}
}
return $file;
}
}