| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileUploadTypes; |
| 4 |
|
| 5 |
/** |
| 6 |
* File Upload Types Settings. |
| 7 |
* |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
class Settings { |
| 11 |
|
| 12 |
/** |
| 13 |
* Admin page slug. |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
const SLUG = 'file-upload-types'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize. |
| 21 |
* |
| 22 |
* @since 1.1.0 |
| 23 |
*/ |
| 24 |
public function init() { |
| 25 |
|
| 26 |
$this->hooks(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Register hooks. |
| 31 |
* |
| 32 |
* @since 1.3.0 |
| 33 |
*/ |
| 34 |
private function hooks() { |
| 35 |
|
| 36 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 37 |
add_action( 'in_admin_header', [ $this, 'display_admin_header' ], 100 ); |
| 38 |
add_action( 'admin_menu', [ $this, 'add_settings_page' ] ); |
| 39 |
add_action( 'admin_init', [ $this, 'save_settings' ] ); |
| 40 |
add_action( 'file_upload_types_settings_after_nav_bar', [ $this, 'display_multiple_mimes_support_notice' ] ); |
| 41 |
add_action( 'admin_init', [ $this, 'enable_multiple_mimes_support' ] ); |
| 42 |
add_filter( 'admin_footer_text', [ $this, 'get_admin_footer' ], 1 ); |
| 43 |
add_action( 'admin_print_scripts', [ $this, 'remove_notices' ] ); |
| 44 |
add_action( 'wp_ajax_file_upload_types_check_sample', [ $this, 'check_sample' ] ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Checks if current screen is File Upload Types or not. |
| 49 |
* |
| 50 |
* @since 1.0.0 |
| 51 |
* |
| 52 |
* @return bool |
| 53 |
*/ |
| 54 |
public function is_admin_screen(): bool { |
| 55 |
|
| 56 |
$screen = get_current_screen(); |
| 57 |
$screen_id = $screen->id ?? ''; |
| 58 |
|
| 59 |
return $screen_id === 'settings_page_file-upload-types'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Enqueue all assets. |
| 64 |
* |
| 65 |
* @since 1.0.0 |
| 66 |
*/ |
| 67 |
public function enqueue_assets() { |
| 68 |
|
| 69 |
if ( ! $this->is_admin_screen() ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 74 |
|
| 75 |
wp_enqueue_style( |
| 76 |
'file-upload-types-css', |
| 77 |
plugins_url( 'assets/css/style.css', FILE_UPLOAD_TYPES_PLUGIN_FILE ), |
| 78 |
[], |
| 79 |
FILE_UPLOAD_TYPES_VERSION |
| 80 |
); |
| 81 |
|
| 82 |
wp_enqueue_script( |
| 83 |
'file-upload-types-dropzone', |
| 84 |
plugins_url( 'assets/js/dropzone.min.js', FILE_UPLOAD_TYPES_PLUGIN_FILE ), |
| 85 |
[], |
| 86 |
'5.9.3', |
| 87 |
true |
| 88 |
); |
| 89 |
|
| 90 |
wp_enqueue_script( |
| 91 |
'file-upload-types', |
| 92 |
plugins_url( 'assets/js/script' . $suffix . '.js', FILE_UPLOAD_TYPES_PLUGIN_FILE ), |
| 93 |
[ 'jquery' ], |
| 94 |
FILE_UPLOAD_TYPES_VERSION, |
| 95 |
true |
| 96 |
); |
| 97 |
|
| 98 |
// phpcs:ignore WPForms.PHP.ValidateDomain.InvalidDomain |
| 99 |
$strings = [ |
| 100 |
'default_section' => esc_html__( 'Default section can not be deleted.', 'file-upload-types' ), |
| 101 |
'nonce' => wp_create_nonce( 'file_upload_types_nonce' ), |
| 102 |
]; |
| 103 |
|
| 104 |
wp_localize_script( 'file-upload-types', 'file_upload_types_params', $strings ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Outputs the plugin admin header. |
| 109 |
* |
| 110 |
* @since 1.0.0 |
| 111 |
*/ |
| 112 |
public function display_admin_header() { |
| 113 |
|
| 114 |
if ( ! $this->is_admin_screen() ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
?> |
| 119 |
<div id="file-upload-types-header"> |
| 120 |
<img src="<?php echo esc_url( plugins_url( 'assets/images/logo.png', FILE_UPLOAD_TYPES_PLUGIN_FILE ) ); ?> " alt="<?php esc_attr_e( 'File Upload Types', 'file-upload-types' ); ?>" class="file-upload-types-header-logo"> |
| 121 |
</div> |
| 122 |
|
| 123 |
<?php |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Add File Upload Types submenu under Settings menu. |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
*/ |
| 131 |
public function add_settings_page() { |
| 132 |
|
| 133 |
add_options_page( |
| 134 |
esc_html__( 'File Upload Types', 'file-upload-types' ), |
| 135 |
esc_html__( 'File Upload Types', 'file-upload-types' ), |
| 136 |
'manage_options', |
| 137 |
self::SLUG, |
| 138 |
[ $this, 'display' ] |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Add contents to the settings page. |
| 144 |
* |
| 145 |
* @since 1.0.0 |
| 146 |
*/ |
| 147 |
public function display() { |
| 148 |
|
| 149 |
if ( ! $this->is_admin_screen() ) { |
| 150 |
return; |
| 151 |
} |
| 152 |
?> |
| 153 |
|
| 154 |
<div class="wrap" id="file-upload-types"> |
| 155 |
<div class="file-upload-types-page file-upload-types-page-settings "> |
| 156 |
<div class="file-upload-types-nav"> |
| 157 |
<div class="file-upload-types-nav-title"> |
| 158 |
<p> |
| 159 |
<?php esc_html_e( 'Settings', 'file-upload-types' ); ?> |
| 160 |
</p> |
| 161 |
</div> |
| 162 |
<div class="fie-upload-types-docs"> |
| 163 |
<p> |
| 164 |
<?php esc_html_e( 'Need some help?', 'file-upload-types' ); ?> |
| 165 |
<a href="https://wpforms.com/docs/how-to-allow-additional-file-upload-types/?utm_source=WordPress&utm_medium=fut-help&utm_campaign=file-upload-types&utm_content=view-documentation" |
| 166 |
target="_blank" |
| 167 |
rel="noopener noreferrer" |
| 168 |
class="button button-secondary"> |
| 169 |
<?php esc_html_e( 'View Documentation', 'file-upload-types' ); ?> |
| 170 |
</a> |
| 171 |
</p> |
| 172 |
</div> |
| 173 |
</div> |
| 174 |
|
| 175 |
<?php |
| 176 |
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName |
| 177 |
/** |
| 178 |
* Action after nav bar on File Upload Type plugin settings page. |
| 179 |
* |
| 180 |
* @since 1.2.0 |
| 181 |
*/ |
| 182 |
do_action( 'file_upload_types_settings_after_nav_bar' ); |
| 183 |
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName |
| 184 |
?> |
| 185 |
|
| 186 |
<form method="post" action=""> |
| 187 |
<div class="file-upload-types-content"> |
| 188 |
<div class="file-upload-types-table"> |
| 189 |
<?php |
| 190 |
$this->display_types_table(); |
| 191 |
$this->display_table_notice(); |
| 192 |
?> |
| 193 |
|
| 194 |
</div> |
| 195 |
|
| 196 |
<div class="file-upload-types-products"> |
| 197 |
<?php $this->display_am_products(); ?> |
| 198 |
</div> |
| 199 |
</div> |
| 200 |
|
| 201 |
<?php wp_nonce_field( 'file_upload_types_settings_save', 'file_upload_types_nonce_field' ); ?> |
| 202 |
|
| 203 |
<p class="file-upload-types-submit"> |
| 204 |
<button type="submit" value="submit" name="file-upload-types-submit" |
| 205 |
class="button button-primary"> |
| 206 |
<?php esc_html_e( 'Save Settings', 'file-upload-types' ); ?> |
| 207 |
</button> |
| 208 |
</p> |
| 209 |
</form> |
| 210 |
</div> |
| 211 |
</div> |
| 212 |
<?php |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Displays table contents. |
| 217 |
* |
| 218 |
* @since 1.0.0 |
| 219 |
* |
| 220 |
* @noinspection HtmlUnknownTarget |
| 221 |
*/ |
| 222 |
public function display_types_table() { // phpcs:ignore Generic.Metrics.NestingLevel.MaxExceeded, Generic.Metrics.CyclomaticComplexity.MaxExceeded |
| 223 |
|
| 224 |
?> |
| 225 |
<div class="before-table"> |
| 226 |
<div class="file-upload-types-heading"> |
| 227 |
<h3><?php esc_html_e( 'Add File Upload Types', 'file-upload-types' ); ?></h3> |
| 228 |
<p> |
| 229 |
<?php |
| 230 |
printf( |
| 231 |
wp_kses( /* translators: %1$s - URL to WordPress Codex page, %2$s - anchor link. */ |
| 232 |
__( 'Below is the list of files types that can be enabled, not including the <a href="%1$s" rel="noopener" target="_blank">files WordPress allows by default</a>. <br>Don\'t see what you need? No problem, <a href="%2$s" id="add-custom-file-types" rel="noopener noreferrer">add your custom file types</a>.', 'file-upload-types' ), |
| 233 |
[ |
| 234 |
'a' => [ |
| 235 |
'href' => [], |
| 236 |
'target' => [], |
| 237 |
'rel' => [], |
| 238 |
'id' => [], |
| 239 |
], |
| 240 |
'br' => [], |
| 241 |
] |
| 242 |
), |
| 243 |
'https://codex.wordpress.org/Uploading_Files#About_Uploading_Files_on_Dashboard', |
| 244 |
'#' |
| 245 |
); |
| 246 |
?> |
| 247 |
</p> |
| 248 |
</div> |
| 249 |
|
| 250 |
<div class="search-box"> |
| 251 |
<label for="file-upload-types-search"> |
| 252 |
<input type="search" id="file-upload-types-search" placeholder="<?php esc_attr_e( 'Search File Types', 'file-upload-types' ); ?>"> |
| 253 |
</label> |
| 254 |
</div> |
| 255 |
</div> |
| 256 |
|
| 257 |
<div class="table-container"> |
| 258 |
<table> |
| 259 |
<tr class="heading"> |
| 260 |
<td style="width: 35%;"><?php esc_html_e( 'Description', 'file-upload-types' ); ?></td> |
| 261 |
<td style="width: 40%;"><?php esc_html_e( 'MIME Type', 'file-upload-types' ); ?></td> |
| 262 |
<td style="width: 15%;"><?php esc_html_e( 'Extension', 'file-upload-types' ); ?></td> |
| 263 |
<td style="width: 10%;"> </td> |
| 264 |
</tr> |
| 265 |
</table> |
| 266 |
</div> |
| 267 |
<div style="overflow-y:scroll; overflow-x:hidden; height:500px;" class="table-container"> |
| 268 |
<table class="file-upload-types-table-main"> |
| 269 |
<?php |
| 270 |
$stored_types = get_option( 'file_upload_types', [] ); |
| 271 |
$enabled_types = isset( $stored_types['enabled'] ) ? (array) $stored_types['enabled'] : []; |
| 272 |
$custom_types = isset( $stored_types['custom'] ) ? (array) $stored_types['custom'] : []; |
| 273 |
$available_types = fut_get_available_file_types(); |
| 274 |
|
| 275 |
$types = array_merge( $custom_types, $available_types ); |
| 276 |
$temp_types = array_unique( array_column( $types, 'ext' ) ); |
| 277 |
$types = array_intersect_key( $types, $temp_types ); |
| 278 |
|
| 279 |
if ( ! empty( $enabled_types ) || ! empty( $custom_types ) ) : |
| 280 |
?> |
| 281 |
<tr class="section"> |
| 282 |
<td colspan="4"><?php esc_html_e( 'ENABLED', 'file-upload-types' ); ?></td> |
| 283 |
</tr> |
| 284 |
|
| 285 |
<?php |
| 286 |
foreach ( $types as $type ) { |
| 287 |
|
| 288 |
if ( |
| 289 |
! in_array( $type['ext'], $enabled_types, true ) && |
| 290 |
! in_array( $type['ext'], array_column( $custom_types, 'ext' ), true ) |
| 291 |
) { |
| 292 |
continue; |
| 293 |
} |
| 294 |
|
| 295 |
if ( is_array( $type['mime'] ) ) { |
| 296 |
$type['mime'] = implode( '</br>', $type['mime'] ); |
| 297 |
} |
| 298 |
|
| 299 |
echo '<tr>'; |
| 300 |
echo '<td style="width: 35%;">' . esc_html( $type['desc'] ) . '</td>'; |
| 301 |
echo '<td style="width: 40%;">' . wp_kses( $type['mime'], [ 'br' => [] ] ) . '</td>'; |
| 302 |
echo '<td style="width: 15%;">' . esc_html( $type['ext'] ) . '</td>'; |
| 303 |
echo '<td style="width: 10%; text-align: right;"><input type="checkbox" value="' . esc_attr( $type['ext'] ) . '" name="e_types[]" checked> </td>'; |
| 304 |
echo '</tr>'; |
| 305 |
} |
| 306 |
|
| 307 |
endif; |
| 308 |
?> |
| 309 |
<tr class="section"> |
| 310 |
<td colspan="4"><?php esc_html_e( 'AVAILABLE', 'file-upload-types' ); ?></td> |
| 311 |
</tr> |
| 312 |
<?php |
| 313 |
$available_types = fut_get_available_file_types(); |
| 314 |
$stored_types = get_option( 'file_upload_types', [] ); |
| 315 |
$enabled_types = $stored_types['enabled'] ?? []; |
| 316 |
$wp_ext_mimes = get_allowed_mime_types(); |
| 317 |
|
| 318 |
foreach ( $available_types as $type ) { |
| 319 |
|
| 320 |
if ( in_array( $type['ext'], $enabled_types, true ) ) { |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
// Ignore default WordPress already enabled extensions. |
| 325 |
foreach ( $wp_ext_mimes as $extension => $mime ) { |
| 326 |
|
| 327 |
$extensions = explode( '|', $extension ); |
| 328 |
|
| 329 |
foreach ( $extensions as $ext ) { |
| 330 |
|
| 331 |
if ( ltrim( $type['ext'], '.' ) === $ext ) { |
| 332 |
continue 3; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if ( is_array( $type['mime'] ) ) { |
| 338 |
$type['mime'] = implode( '</br>', $type['mime'] ); |
| 339 |
} |
| 340 |
|
| 341 |
echo '<tr>'; |
| 342 |
echo '<td style="width: 35%;">' . esc_html( $type['desc'] ) . '</td>'; |
| 343 |
echo '<td style="width: 40%;">' . wp_kses( $type['mime'], [ 'br' => [] ] ) . '</td>'; |
| 344 |
echo '<td style="width: 15%;">' . esc_html( $type['ext'] ) . '</td>'; |
| 345 |
echo '<td style="width: 10%; text-align: right;"><input type="checkbox" value="' . esc_attr( $type['ext'] ) . '" name="a_types[]"> </td>'; |
| 346 |
echo '</tr>'; |
| 347 |
} |
| 348 |
?> |
| 349 |
</table> |
| 350 |
</div> |
| 351 |
|
| 352 |
<div class="table-container"> |
| 353 |
<table class="rounded-left-bottom rounded-right-bottom"> |
| 354 |
<tr class="section" style="overflow-y:hidden"> |
| 355 |
<td colspan="4" id="custom-file-types"><?php esc_html_e( 'ADD CUSTOM FILE TYPES', 'file-upload-types' ); ?> |
| 356 |
<div class="file-upload-types-info" style="font-size: 14px;"> |
| 357 |
<img src="<?php echo esc_url( plugins_url( 'assets/images/question-circle-solid.svg', FILE_UPLOAD_TYPES_PLUGIN_FILE ) ); ?>" alt="<?php esc_attr_e( 'Help', 'file-upload-types' ); ?>"> |
| 358 |
<span class="tooltiptext"><?php echo esc_html__( 'Upload files and have their MIME type detected automatically. Multiple MIME types for a single extension can be separated by a comma.', 'file-upload-types' ); ?> </span> |
| 359 |
</div> |
| 360 |
</td> |
| 361 |
</tr> |
| 362 |
|
| 363 |
<tr class="dropzone"> |
| 364 |
<td colspan="4" class="rounded-left-bottom rounded-right-bottom"> |
| 365 |
<div class="file-upload-types-dropzone" id="c_types_file_sample_button"> |
| 366 |
<p><span class="dz-message"><span class="icon"></span><?php echo wp_kses_post( __( 'Drop files here or click to select files. You can also </span><a href="#">add file types manually</a>.', 'file-upload-types' ) ); ?></p> |
| 367 |
</div> |
| 368 |
</td> |
| 369 |
</tr> |
| 370 |
|
| 371 |
<tr class="repetitive-fields"> |
| 372 |
<td style="width: 40%;" class="cell_c_types_file_description rounded-left-bottom"> |
| 373 |
<label for="c_types_file_description"></label> |
| 374 |
<input |
| 375 |
type="text" name="c_types[desc][]" class="description c_types_file_description" |
| 376 |
id="c_types_file_description" style="display: inline-block" |
| 377 |
placeholder="<?php esc_attr_e( 'File Description', 'file-upload-types' ); ?>"> |
| 378 |
</td> |
| 379 |
<td style="width: 45%;" class="cell_c_types_file_mime_type"> |
| 380 |
<label for="c_types_file_mime_type"></label> |
| 381 |
<input |
| 382 |
type="text" name="c_types[mime][]" class="mime c_types_file_mime_type" |
| 383 |
id="c_types_file_mime_type" |
| 384 |
placeholder="<?php esc_attr_e( 'MIME Type', 'file-upload-types' ); ?>"> |
| 385 |
</td> |
| 386 |
<td style="width: 35%;" class="cell_c_types_file_extension"> |
| 387 |
<label for="c_types_file_extension"></label> |
| 388 |
<input |
| 389 |
type="text" name="c_types[ext][]" class="extension c_types_file_extension" |
| 390 |
id="c_types_file_extension" |
| 391 |
placeholder="<?php esc_attr_e( 'Extension', 'file-upload-types' ); ?>"> |
| 392 |
</td> |
| 393 |
<td style="width: 10%;" class="icons rounded-right-bottom"> |
| 394 |
<img |
| 395 |
class="file-upload-types-plus" |
| 396 |
src="<?php echo esc_url( plugins_url( 'assets/images/plus-circle-solid.svg', FILE_UPLOAD_TYPES_PLUGIN_FILE ) ); ?>" |
| 397 |
alt="<?php esc_attr_e( 'Add File Type', 'file-upload-types' ); ?>"> |
| 398 |
<img |
| 399 |
class="file-upload-types-minus" |
| 400 |
src="<?php echo esc_url( plugins_url( 'assets/images/trash-solid.svg', FILE_UPLOAD_TYPES_PLUGIN_FILE ) ); ?>" |
| 401 |
alt="<?php esc_attr_e( 'Remove File Type', 'file-upload-types' ); ?>"> |
| 402 |
</td> |
| 403 |
</tr> |
| 404 |
</table> |
| 405 |
</div> |
| 406 |
<?php |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Display notice about the security risks of allowing various file uploads. |
| 411 |
* |
| 412 |
* @since 1.5.0 |
| 413 |
* |
| 414 |
* @return void |
| 415 |
*/ |
| 416 |
private function display_table_notice() { |
| 417 |
|
| 418 |
$link = 'https://wpforms.com/docs/how-to-allow-additional-file-upload-types/?utm_source=WordPress&utm_medium=fut-help&utm_campaign=file-upload-types&utm_content=view-documentation#secure-upload'; |
| 419 |
|
| 420 |
echo '<p style="color: #6E6E6E">'; |
| 421 |
echo esc_html( |
| 422 |
__( |
| 423 |
'While allowing various file uploads can be convenient, it poses security risks.', |
| 424 |
'file-upload-types' |
| 425 |
) |
| 426 |
); |
| 427 |
echo '<br>'; |
| 428 |
echo esc_html( |
| 429 |
__( |
| 430 |
'Enhance your site\'s security by restricting uploads to specific, safe file types and limiting upload permissions to trusted users only.', |
| 431 |
'file-upload-types' |
| 432 |
) |
| 433 |
); |
| 434 |
echo '<br>'; |
| 435 |
printf( |
| 436 |
/* translators: %1$s - opening anchor tag, %2$s - closing anchor tag. */ |
| 437 |
esc_html__( 'Please refer %1$shere%2$s for more information.', 'file-upload-types' ), |
| 438 |
'<a href="' . esc_url( $link ) . '" target="_blank" rel="noopener noreferrer">', |
| 439 |
'</a>' |
| 440 |
); |
| 441 |
echo '</p>'; |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Displays recommended products section. |
| 446 |
* |
| 447 |
* @since 1.0.0 |
| 448 |
* |
| 449 |
* @noinspection HtmlUnknownTarget |
| 450 |
*/ |
| 451 |
public function display_am_products() { |
| 452 |
|
| 453 |
?> |
| 454 |
<h3><?php esc_html_e( 'You might like our other products', 'file-upload-types' ); ?></h3> |
| 455 |
|
| 456 |
<p> |
| 457 |
<?php |
| 458 |
echo wp_kses( |
| 459 |
__( 'File Upload Types is built by the team behind some of the most popular WordPress plugins.', 'file-upload-types' ), |
| 460 |
[] |
| 461 |
); |
| 462 |
?> |
| 463 |
</p> |
| 464 |
|
| 465 |
<div class="file-upload-types-recommended-plugins"> |
| 466 |
<div class="plugins-container"> |
| 467 |
<?php foreach ( $this->get_am_plugins() as $plugin ) : ?> |
| 468 |
<div class="plugin-container"> |
| 469 |
<div class="plugin-item"> |
| 470 |
<div class="details file-upload-types-clear"> |
| 471 |
<img src="<?php echo esc_url( $plugin['icon'] ); ?>" alt=""> |
| 472 |
<h5 class="plugin-name"> |
| 473 |
<?php echo esc_html( $plugin['name'] ); ?> |
| 474 |
</h5> |
| 475 |
<p class="plugin-desc"> |
| 476 |
<?php echo esc_html( $plugin['desc'] ); ?> |
| 477 |
</p> |
| 478 |
<p> |
| 479 |
<?php |
| 480 |
printf( |
| 481 |
wp_kses( /* translators: %2$s - Plugin Name. */ |
| 482 |
'<a href="%1$s" class="external-link" target="_blank" rel="noopener noreferrer">' . __( 'Get %2$s', 'file-upload-types' ) . '</a>', |
| 483 |
[ |
| 484 |
'img' => [ |
| 485 |
'src' => [], |
| 486 |
], |
| 487 |
'a' => [ |
| 488 |
'alt' => [], |
| 489 |
'href' => [], |
| 490 |
'class' => [], |
| 491 |
'target' => [], |
| 492 |
], |
| 493 |
] |
| 494 |
), |
| 495 |
esc_url( $plugin['url'] ), |
| 496 |
esc_html( $plugin['name'] ) |
| 497 |
); |
| 498 |
?> |
| 499 |
</p> |
| 500 |
</div> |
| 501 |
</div> |
| 502 |
</div> |
| 503 |
<?php endforeach; ?> |
| 504 |
</div> |
| 505 |
</div> |
| 506 |
<?php |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* Save settings to the database. |
| 511 |
* |
| 512 |
* @since 1.0.0 |
| 513 |
*/ |
| 514 |
public function save_settings() { // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded, WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks, Generic.Metrics.CyclomaticComplexity.TooHigh |
| 515 |
|
| 516 |
if ( ! isset( $_POST['file-upload-types-submit'] ) ) { |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
if ( |
| 521 |
! isset( $_POST['file_upload_types_nonce_field'] ) || |
| 522 |
! wp_verify_nonce( sanitize_key( $_POST['file_upload_types_nonce_field'] ), 'file_upload_types_settings_save' ) |
| 523 |
) { |
| 524 |
return; |
| 525 |
} |
| 526 |
|
| 527 |
// All new installations since 1.2.0 will have multiple mime types support enabled by default. |
| 528 |
if ( ! get_option( 'file_upload_types' ) ) { |
| 529 |
update_option( 'file_upload_types_multiple_mimes', 'enabled' ); |
| 530 |
} |
| 531 |
|
| 532 |
// phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 533 |
$enabled_types = isset( $_POST['e_types'] ) ? array_map( 'sanitize_text_field', $_POST['e_types'] ) : []; |
| 534 |
$available_types = isset( $_POST['a_types'] ) ? array_map( 'sanitize_text_field', $_POST['a_types'] ) : []; |
| 535 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 536 |
$custom_types_raw = (array) ( $_POST['c_types'] ?? [] ); |
| 537 |
// phpcs:enable WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 538 |
|
| 539 |
$custom_types = fut_format_raw_custom_types( $custom_types_raw ); |
| 540 |
$custom_types = fut_format_multiple_file_types( $custom_types ); |
| 541 |
|
| 542 |
foreach ( $custom_types as $key => $type ) { |
| 543 |
|
| 544 |
// Remove if mime type or extension is empty. |
| 545 |
if ( empty( $type['ext'] ) || empty( $type['mime'] ) ) { |
| 546 |
unset( $custom_types[ $key ] ); |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
$types = get_option( 'file_upload_types', [] ); |
| 551 |
$stored_custom_types = isset( $types['custom'] ) ? (array) $types['custom'] : []; |
| 552 |
|
| 553 |
foreach ( $stored_custom_types as $key => $value ) { |
| 554 |
|
| 555 |
if ( ! in_array( $value['ext'], $enabled_types, true ) ) { |
| 556 |
unset( $stored_custom_types[ $key ] ); |
| 557 |
} |
| 558 |
|
| 559 |
// Remove a duplicate type of the same extension. |
| 560 |
if ( in_array( $value['ext'], array_column( $custom_types, 'ext' ), true ) ) { |
| 561 |
unset( $stored_custom_types[ $key ] ); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
$enabled_types = array_merge( $enabled_types, $available_types ); |
| 566 |
|
| 567 |
$file_upload_types = [ |
| 568 |
'enabled' => $enabled_types, |
| 569 |
'custom' => array_merge( $custom_types, $stored_custom_types ), |
| 570 |
]; |
| 571 |
|
| 572 |
update_option( 'file_upload_types', $file_upload_types ); |
| 573 |
|
| 574 |
add_action( |
| 575 |
'file_upload_types_settings_after_nav_bar', |
| 576 |
static function () { |
| 577 |
?> |
| 578 |
<div class="notice notice-success file-upload-types-notice is-dismissible"> |
| 579 |
<p><strong><?php esc_html_e( 'Your settings have been saved.', 'file-upload-types' ); ?></strong></p> |
| 580 |
</div> |
| 581 |
<?php |
| 582 |
} |
| 583 |
); |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
* Display notice about multiple mime types support for old installations. |
| 588 |
* |
| 589 |
* @since 1.2.0 |
| 590 |
* |
| 591 |
* @noinspection HtmlUnknownTarget |
| 592 |
*/ |
| 593 |
public function display_multiple_mimes_support_notice() { |
| 594 |
|
| 595 |
if ( ! get_option( 'file_upload_types' ) || 'enabled' === get_option( 'file_upload_types_multiple_mimes' ) ) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
?> |
| 600 |
<div class="notice notice-info file-upload-types-notice"> |
| 601 |
<p><strong> |
| 602 |
<?php |
| 603 |
printf( |
| 604 |
wp_kses( /* translators: %1$s - Same page; %2$s - Documentation link for multiple types support. */ |
| 605 |
__( 'File Upload Types now supports multiple MIME types for each file extension to improve file upload compatibility! <br/> <strong><a href="%1$s">Enable multiple MIME types support</a> | <a href="%2$s" target="_blank" rel="noopener noreferrer">Learn More</a></strong>', 'file-upload-types' ), |
| 606 |
[ |
| 607 |
'br' => [], |
| 608 |
'strong' => [], |
| 609 |
'a' => [ |
| 610 |
'href' => [], |
| 611 |
'rel' => [], |
| 612 |
'target' => [], |
| 613 |
], |
| 614 |
] |
| 615 |
), |
| 616 |
esc_url( |
| 617 |
wp_nonce_url( |
| 618 |
add_query_arg( |
| 619 |
[ |
| 620 |
'page' => self::SLUG, |
| 621 |
'multiple_mimes' => 'enabled', |
| 622 |
], |
| 623 |
admin_url( 'options-general.php' ) |
| 624 |
), |
| 625 |
'enable-multiple-mime-types-support' |
| 626 |
) |
| 627 |
), |
| 628 |
'https://wpforms.com/docs/how-to-allow-additional-file-upload-types/#multiple-mime-types' |
| 629 |
) |
| 630 |
?> |
| 631 |
</strong></p> |
| 632 |
</div> |
| 633 |
<?php |
| 634 |
} |
| 635 |
|
| 636 |
/** |
| 637 |
* Enable multiple mime types support for old installations. |
| 638 |
* |
| 639 |
* @since 1.2.0 |
| 640 |
*/ |
| 641 |
public function enable_multiple_mimes_support() { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks |
| 642 |
|
| 643 |
if ( ! isset( $_GET['multiple_mimes'] ) || ( $_GET['multiple_mimes'] !== 'enabled' ) ) { |
| 644 |
return; |
| 645 |
} |
| 646 |
|
| 647 |
check_admin_referer( 'enable-multiple-mime-types-support' ); |
| 648 |
|
| 649 |
update_option( 'file_upload_types_multiple_mimes', 'enabled' ); |
| 650 |
|
| 651 |
add_action( |
| 652 |
'file_upload_types_settings_after_nav_bar', |
| 653 |
static function () { |
| 654 |
?> |
| 655 |
<div class="notice notice-success file-upload-types-notice is-dismissible"> |
| 656 |
<p><strong><?php esc_html_e( 'Support for multiple MIME types has been enabled.', 'file-upload-types' ); ?></strong></p> |
| 657 |
</div> |
| 658 |
<?php |
| 659 |
} |
| 660 |
); |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* List of AM plugins that we propose to install. |
| 665 |
* |
| 666 |
* @since 1.0.0 |
| 667 |
* |
| 668 |
* @return array |
| 669 |
*/ |
| 670 |
private function get_am_plugins(): array { |
| 671 |
|
| 672 |
return [ |
| 673 |
'wpf' => [ |
| 674 |
'icon' => plugins_url( 'assets/images/wpforms.svg', FILE_UPLOAD_TYPES_PLUGIN_FILE ), |
| 675 |
'name' => __( 'WPForms', 'file-upload-types' ), |
| 676 |
'desc' => __( 'The most beginner friendly WordPress form plugin.', 'file-upload-types' ), |
| 677 |
'url' => 'https://wpforms.com?utm_source=WordPress&utm_medium=fut-recommendations&utm_campaign=file-upload-types&utm_content=get-wpforms', |
| 678 |
], |
| 679 |
'smtp' => [ |
| 680 |
'icon' => plugins_url( 'assets/images/wp-mail-smtp.svg', FILE_UPLOAD_TYPES_PLUGIN_FILE ), |
| 681 |
'name' => __( 'WP Mail SMTP', 'file-upload-types' ), |
| 682 |
'desc' => __( 'The most popular SMTP and PHP mailer WordPress plugin.', 'file-upload-types' ), |
| 683 |
'url' => 'https://wpmailsmtp.com?utm_source=WordPress&utm_medium=fut-recommendations&utm_campaign=file-upload-types&utm_content=get-wp-mail-smtp', |
| 684 |
], |
| 685 |
'sl' => [ |
| 686 |
'icon' => plugins_url( 'assets/images/sendlayer.svg', FILE_UPLOAD_TYPES_PLUGIN_FILE ), |
| 687 |
'name' => __( 'SendLayer', 'file-upload-types' ), |
| 688 |
'desc' => __( 'An SMTP email delivery service that’s easy and powerful.', 'file-upload-types' ), |
| 689 |
'url' => 'https://sendlayer.com?utm_source=WordPress&utm_medium=fut-recommendations&utm_campaign=file-upload-types&utm_content=get-sendlayer', |
| 690 |
], |
| 691 |
'seo' => [ |
| 692 |
'icon' => plugins_url( 'assets/images/allinone.svg', FILE_UPLOAD_TYPES_PLUGIN_FILE ), |
| 693 |
'name' => __( 'All In One SEO', 'file-upload-types' ), |
| 694 |
'desc' => __( 'The best WordPress SEO plugin and toolkit.', 'file-upload-types' ), |
| 695 |
'url' => 'https://aioseo.com?utm_source=WordPress&utm_medium=fut-recommendations&utm_campaign=file-upload-types&utm_content=get-aioseo', |
| 696 |
], |
| 697 |
]; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* Display a text to ask users to review the plugin on WP.org. |
| 702 |
* |
| 703 |
* @since 1.0.0 |
| 704 |
* |
| 705 |
* @param string|mixed $text Rating Text. |
| 706 |
* |
| 707 |
* @return string |
| 708 |
* @noinspection HtmlUnknownTarget |
| 709 |
*/ |
| 710 |
public function get_admin_footer( $text ): string { |
| 711 |
|
| 712 |
if ( ! $this->is_admin_screen() ) { |
| 713 |
return (string) $text; |
| 714 |
} |
| 715 |
|
| 716 |
return sprintf( |
| 717 |
wp_kses( /* translators: %1$s - WP.org link. */ |
| 718 |
__( 'Please rate <strong>File Upload Types</strong> <a href="%1$s" target="_blank" rel="noopener noreferrer">★★★★★</a> on <a href="%1$s" target="_blank" rel="noopener noreferrer">WordPress.org</a> to help us spread the word.', 'file-upload-types' ), |
| 719 |
[ |
| 720 |
'strong' => [], |
| 721 |
'a' => [ |
| 722 |
'href' => [], |
| 723 |
'target' => [], |
| 724 |
'rel' => [], |
| 725 |
], |
| 726 |
] |
| 727 |
), |
| 728 |
'https://wordpress.org/support/plugin/file-upload-types/reviews/?filter=5#new-post' |
| 729 |
); |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* Removes the admin notices on file upload types settings page. |
| 734 |
* |
| 735 |
* @since 1.0.0 |
| 736 |
*/ |
| 737 |
public function remove_notices() { // phpcs:ignore Generic.Metrics.NestingLevel.MaxExceeded, Generic.Metrics.CyclomaticComplexity.TooHigh |
| 738 |
|
| 739 |
global $wp_filter; |
| 740 |
|
| 741 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 742 |
if ( ! isset( $_REQUEST['page'] ) || $_REQUEST['page'] !== self::SLUG ) { |
| 743 |
return; |
| 744 |
} |
| 745 |
|
| 746 |
foreach ( [ 'user_admin_notices', 'admin_notices', 'all_admin_notices' ] as $wp_notice ) { |
| 747 |
if ( ! empty( $wp_filter[ $wp_notice ]->callbacks ) && is_array( $wp_filter[ $wp_notice ]->callbacks ) ) { |
| 748 |
foreach ( $wp_filter[ $wp_notice ]->callbacks as $priority => $hooks ) { |
| 749 |
foreach ( $hooks as $name => $arr ) { |
| 750 |
unset( $wp_filter[ $wp_notice ]->callbacks[ $priority ][ $name ] ); |
| 751 |
} |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
/** |
| 758 |
* Check the sample file. |
| 759 |
* |
| 760 |
* @since 1.4.0 |
| 761 |
*/ |
| 762 |
public function check_sample() { |
| 763 |
|
| 764 |
check_ajax_referer( 'file_upload_types_nonce', 'nonce' ); |
| 765 |
|
| 766 |
if ( ! isset( $_FILES['file'] ) ) { |
| 767 |
wp_send_json_error( |
| 768 |
[ |
| 769 |
'message' => __( 'No file provided.', 'file-upload-types' ), |
| 770 |
], |
| 771 |
400 |
| 772 |
); |
| 773 |
} |
| 774 |
|
| 775 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 776 |
$sample = $_FILES['file']; |
| 777 |
$sample['name'] = sanitize_file_name( $sample['name'] ); |
| 778 |
$sample['tmp_name'] = sanitize_text_field( $sample['tmp_name'] ); |
| 779 |
$sample['type'] = sanitize_text_field( $sample['type'] ); |
| 780 |
|
| 781 |
$file_info = finfo_open( FILEINFO_MIME_TYPE ); |
| 782 |
$mime_type = finfo_file( $file_info, $sample['tmp_name'] ); |
| 783 |
$extension = pathinfo( $sample['name'], PATHINFO_EXTENSION ); |
| 784 |
|
| 785 |
if ( isset( $sample['type'] ) && $mime_type !== $sample['type'] ) { |
| 786 |
$mime_types_arr[] = $sample['type']; |
| 787 |
} |
| 788 |
|
| 789 |
$mime_types_arr[] = (string) $mime_type; |
| 790 |
|
| 791 |
if ( ! $mime_type || ! $extension ) { |
| 792 |
wp_send_json_error( |
| 793 |
[ |
| 794 |
'message' => __( 'Unable to detect the file MIME type.', 'file-upload-types' ), |
| 795 |
], |
| 796 |
400 |
| 797 |
); |
| 798 |
} |
| 799 |
|
| 800 |
finfo_close( $file_info ); |
| 801 |
|
| 802 |
wp_send_json_success( |
| 803 |
[ |
| 804 |
'mime_type' => implode( ', ', array_filter( $mime_types_arr ) ), |
| 805 |
'extension' => $extension, |
| 806 |
] |
| 807 |
); |
| 808 |
} |
| 809 |
} |
| 810 |
|