1 | <?php |
||||||
2 | namespace NirjharLo\WP_Plugin_Framework\Lib; |
||||||
3 | |||||||
4 | if ( ! defined( 'ABSPATH' ) ) exit; |
||||||
5 | |||||||
6 | /** |
||||||
7 | * Plugin upload for WordPress front end or backend |
||||||
8 | * |
||||||
9 | * @author Nirjhar Lo |
||||||
10 | * @package wp-plugin-framework |
||||||
11 | */ |
||||||
12 | if ( ! class_exists( 'Upload' ) ) { |
||||||
13 | |||||||
14 | final class Upload { |
||||||
15 | |||||||
16 | |||||||
17 | /** |
||||||
18 | * Add basic form |
||||||
19 | * |
||||||
20 | * @return Void |
||||||
21 | */ |
||||||
22 | public function __construct() { |
||||||
23 | |||||||
24 | if ( isset($_POST['UploadSubmit']) ) { |
||||||
25 | $this->upload_controller(); |
||||||
26 | } |
||||||
27 | |||||||
28 | $this->upload_form(); |
||||||
29 | } |
||||||
30 | |||||||
31 | |||||||
32 | /** |
||||||
33 | * Outputs the content of the widget |
||||||
34 | * |
||||||
35 | * @return Html |
||||||
0 ignored issues
–
show
|
|||||||
36 | */ |
||||||
37 | public function upload_form() { ?> |
||||||
38 | |||||||
39 | <form method="POST" action="" enctype="multipart/form-data"> |
||||||
40 | <input name="UploadFile" type="file" multiple="false"/> |
||||||
41 | <?php submit_button( __( 'Upload', 'stv' ), 'secondary', 'UploadSubmit' ); ?> |
||||||
0 ignored issues
–
show
The function
submit_button was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() The function
__ was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
42 | </form> |
||||||
43 | <?php |
||||||
44 | } |
||||||
45 | |||||||
46 | |||||||
47 | /** |
||||||
48 | * Manage the Upload file |
||||||
49 | * |
||||||
50 | * @return Void |
||||||
51 | */ |
||||||
52 | public function upload_controller() { |
||||||
53 | |||||||
54 | $file = $_FILES['UploadFile']; |
||||||
55 | $type = $file['type']; |
||||||
56 | |||||||
57 | // Check in your file type |
||||||
58 | if( $type != 'application/_TYPE_' ) { |
||||||
59 | add_action( 'admin_notices', array( $this, 'file_type_error_admin_notice' ) ); |
||||||
0 ignored issues
–
show
The function
add_action was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
60 | } else { |
||||||
61 | |||||||
62 | if (!function_exists('wp_handle_upload')){ |
||||||
63 | require_once(ABSPATH . 'wp-admin/includes/image.php'); |
||||||
0 ignored issues
–
show
|
|||||||
64 | require_once(ABSPATH . 'wp-admin/includes/file.php'); |
||||||
65 | require_once(ABSPATH . 'wp-admin/includes/media.php'); |
||||||
66 | } |
||||||
67 | |||||||
68 | $overrides = array( 'test_form' => false); |
||||||
69 | $attachment = wp_handle_upload($file, $overrides); |
||||||
70 | |||||||
71 | if( is_array( $attachment_id ) && array_key_exists( 'path', $attachment ) ) { |
||||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||||
72 | $upload_path = $attachment['path']; |
||||||
0 ignored issues
–
show
|
|||||||
73 | |||||||
74 | add_action( 'admin_notices', array( $this, 'success_notice' ) ); |
||||||
75 | |||||||
76 | // Use $upload_path for any purpose. For example storing temporarily |
||||||
77 | // update_option( 'some_token', $upload_path ); |
||||||
78 | } else { |
||||||
79 | add_action( 'admin_notices', array( $this, 'file_error_admin_notice' ) ); |
||||||
80 | $upload_path = false; |
||||||
81 | } |
||||||
82 | } |
||||||
83 | } |
||||||
84 | |||||||
85 | |||||||
86 | /** |
||||||
87 | * Notify wrong file type |
||||||
88 | * |
||||||
89 | * @return Html |
||||||
90 | */ |
||||||
91 | public function file_type_error_admin_notice() { ?> |
||||||
92 | |||||||
93 | <div class="notice notice-error is-dismissible"> |
||||||
94 | <p><?php _e( 'Please Upload correct type of file only.', 'textdomain' ); ?></p> |
||||||
0 ignored issues
–
show
The function
_e was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
95 | </div> |
||||||
96 | <?php |
||||||
97 | } |
||||||
98 | |||||||
99 | |||||||
100 | /** |
||||||
101 | * Notify error in upload process |
||||||
102 | * |
||||||
103 | * @return Html |
||||||
104 | */ |
||||||
105 | public function file_error_admin_notice() { ?> |
||||||
106 | |||||||
107 | <div class="notice notice-error is-dismissible"> |
||||||
108 | <p><?php _e( 'File Upload failed.', 'textdomain' ); ?></p> |
||||||
0 ignored issues
–
show
The function
_e was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
109 | </div> |
||||||
110 | <?php |
||||||
111 | } |
||||||
112 | |||||||
113 | |||||||
114 | /** |
||||||
115 | * Notify on success |
||||||
116 | * |
||||||
117 | * @return Html |
||||||
118 | */ |
||||||
119 | public function success_notice() { ?> |
||||||
120 | |||||||
121 | <div class="notice notice-success is-dismissible"> |
||||||
122 | <p><?php _e( 'Successfully saved file details.', 'textdomain' ); ?></p> |
||||||
0 ignored issues
–
show
The function
_e was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
123 | </div> |
||||||
124 | <?php |
||||||
125 | } |
||||||
126 | } |
||||||
127 | } ?> |
||||||
128 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths