-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
restricting file upload types #49
Open
kkarpieszuk
wants to merge
23
commits into
develop
Choose a base branch
from
fut/35-restricting-file-upload-types
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
d6cb9c7
[wip] Restricting file upload types
kkarpieszuk be912e3
Merge remote-tracking branch 'origin/develop' into fut/35-restricting…
kkarpieszuk 1a009b7
[fixup] works but it needs to enable native types during registration
kkarpieszuk a4d04b7
[fixup] auto enable native file formats
kkarpieszuk 408cbd7
[fixup] rephrazed description over table
kkarpieszuk 3d44042
Merge remote-tracking branch 'origin/develop' into fut/35-restricting…
kkarpieszuk b89ff68
[fixup] added working version of the front end filtering
kkarpieszuk 4912193
[fixup] moved inits to Plugin class
kkarpieszuk 748c7a7
[fixup] phpdoc
kkarpieszuk e3ee0cf
[fixup] decoupled Allowed and Plugin class
kkarpieszuk e1ab53f
[fixup] performance fix for getting native types
kkarpieszuk cedfe75
[fixup] phpcs
kkarpieszuk 5f566dc
[fixup] removed todo
kkarpieszuk 4c14e87
[fixup] decoupled dispatcher
kkarpieszuk 7ea8dbd
[fixup] decoupled Settings and Admin
kkarpieszuk 9c68ffb
[fixup] added immediate native file type registration after installat…
kkarpieszuk d27e268
[fixup] fix native file types are disallowed by default and cannot be…
kkarpieszuk 05e4b57
[fixup] applied suggested changes
kkarpieszuk d599f47
[fixup] plugin instantiation fix
kkarpieszuk c03506e
[fixup] new StoredTypes helper class
kkarpieszuk f635358
[fixup] cleaned Dispatcher
kkarpieszuk de17721
[fixup] renamed hooks
kkarpieszuk dd68c81
[fixup] parameter default value as array
kkarpieszuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
namespace FileUploadTypes; | ||
|
||
/** | ||
* Logic related to filtering wp_get_mime_types results, mostly on front end. | ||
* | ||
* @since {VERSION} | ||
*/ | ||
class Allowed { | ||
|
||
/** | ||
* Enabled types. | ||
* | ||
* @since {VERSION} | ||
* | ||
* @var array | ||
*/ | ||
private $enabled_types = []; | ||
|
||
/** | ||
* Register hooks. | ||
* | ||
* @since {VERSION} | ||
* | ||
* @return void | ||
*/ | ||
public function hooks() { | ||
|
||
add_filter( 'upload_mimes', [ $this, 'allowed_types' ] ); | ||
} | ||
|
||
/** | ||
* File types allowed to upload. | ||
* | ||
* @link https://developer.wordpress.org/reference/functions/wp_get_mime_types/ | ||
* | ||
* @since {VERSION} | ||
* | ||
* @param array $mime_types List of all allowed in WordPress mime types. | ||
* | ||
* @return array | ||
*/ | ||
public function allowed_types( $mime_types ) { | ||
|
||
foreach ( $mime_types as $extensions => $mime ) { | ||
$extensions_array = explode( '|', $extensions ); | ||
$mime_types = count( $extensions_array ) === 1 | ||
? $this->remove_single_extension( $extensions, $mime_types ) | ||
: $this->process_multiple_extensions( $extensions_array, $mime_types ); | ||
} | ||
|
||
return $mime_types; | ||
} | ||
|
||
/** | ||
* Maybe remove single extension. | ||
* | ||
* @since {VERSION} | ||
* | ||
* @param string $extension File extension. | ||
* @param array $mime_types WordPress allowed types. | ||
* | ||
* @return array | ||
*/ | ||
private function remove_single_extension( $extension, $mime_types ) { | ||
|
||
if ( ! array_key_exists( $extension, $this->get_enabled_types() ) ) { | ||
unset( $mime_types[ $extension ] ); | ||
} | ||
|
||
return $mime_types; | ||
} | ||
|
||
/** | ||
* Process each extension from pipeline separated extensions. | ||
* | ||
* If extension is not allowed, remove it from mime types. | ||
* | ||
* @since {VERSION} | ||
* | ||
* @param array $extensions Allowed extensions, exploded on | sign. | ||
* @param array $mime_types WordPress allowed mime types. | ||
* | ||
* @return array Filtered WordPress allowed mime types. | ||
*/ | ||
private function process_multiple_extensions( $extensions, $mime_types ) { | ||
|
||
$concatenated_extensions = implode( '|', $extensions ); | ||
$mime = $mime_types[ $concatenated_extensions ]; | ||
|
||
unset( $mime_types[ $concatenated_extensions ] ); | ||
|
||
foreach ( $extensions as $index => $extension ) { | ||
if ( ! array_key_exists( $extension, $this->get_enabled_types() ) ) { | ||
unset( $extensions[ $index ] ); | ||
} | ||
} | ||
if ( ! empty( $extensions ) ) { | ||
$mime_types[ implode( '|', $extensions ) ] = $mime; | ||
} | ||
|
||
return $mime_types; | ||
} | ||
|
||
/** | ||
* Get stored FUT enabled types. | ||
* | ||
* @since {VERSION} | ||
* | ||
* @return array|string[] | ||
*/ | ||
private function get_enabled_types() { | ||
|
||
if ( ! $this->enabled_types ) { | ||
|
||
$plugin = Plugin::get_instance(); | ||
|
||
// Only add first mime type to the allowed list. Aliases will be dynamically added when required. | ||
$this->enabled_types = array_map( | ||
static function( $enabled_types ) { | ||
|
||
return sanitize_mime_type( ! is_array( $enabled_types ) ? $enabled_types : $enabled_types[0] ); | ||
}, | ||
$plugin->enabled_types() | ||
); | ||
} | ||
|
||
return $this->enabled_types; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace FileUploadTypes\Migrations; | ||
|
||
/** | ||
* Various logic dispatcher class. | ||
* | ||
* @since {VERSION} | ||
*/ | ||
class Dispatcher { | ||
|
||
/** | ||
* Register hooks. | ||
* | ||
* @since {VERSION} | ||
*/ | ||
public function hooks() { | ||
|
||
add_action( 'init', [ $this, 'run' ] ); | ||
} | ||
|
||
/** | ||
* Run all migration logics. | ||
* | ||
* @since {VERSION} | ||
*/ | ||
public function run() { | ||
|
||
$already_run = get_option( 'file_upload_types_migrations_done', [] ); | ||
$option_changed = false; | ||
|
||
foreach ( $this->get_migrations_list() as $name => $callback ) { | ||
|
||
if ( ! isset( $already_run[ $name ] ) && is_callable( $callback ) && $callback() ) { | ||
$already_run[ $name ] = 1; | ||
$option_changed = true; | ||
} | ||
} | ||
|
||
if ( $option_changed ) { | ||
update_option( 'file_upload_types_migrations_done', $already_run ); | ||
} | ||
} | ||
|
||
/** | ||
* Get available migrations. | ||
* | ||
* @since {VERSION} | ||
* | ||
* @return array[] | ||
*/ | ||
private function get_migrations_list() { | ||
|
||
// phpcs:disable WPForms.PHP.ValidateHooks.InvalidHookName | ||
return [ | ||
/** | ||
* Get callback method for add_native_file_upload_types migration. | ||
* | ||
* @since {VERSION} | ||
* | ||
* @param callable $callback Callback. | ||
*/ | ||
'add_native_file_upload_types' => apply_filters( 'file_upload_types_migrations_dispatcher_add_native_file_upload_types_callback', null ), | ||
]; | ||
// phpcs:enable WPForms.PHP.ValidateHooks.InvalidHookName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add a blank line before
if
.