Skip to content
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
wants to merge 23 commits into
base: develop
Choose a base branch
from

Conversation

kkarpieszuk
Copy link
Contributor

Fixes #35

  • adds file types natively supported by WP to the Settings > File upload types table so user can now disable them if needed

Test steps:

  • activate fut plugin
  • go to wp-admin > settings > file upload types and check if file types with description "WordPress natively registered type" are added and automatically enabled
  • disable some poplar native file type (for example .jpeg) and save
  • install wpforms and create form with file upload field
  • preview form and try to upload .jpeg file -> you should see error file extension is not supported
  • go to wp-admin > settings > file upload types and re-enable .jpeg file extension
  • go to form and try to upload .jpeg file -> you should be able to do this

Copy link
Contributor

@cheh cheh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kkarpieszuk sorry for being silent for so long time 🙏 and thanks for handling it by yourself!

I've made a first round of reviews and here is my feedback:

  1. There is an issue once I've checked out to PR branch without additional actions - *.jpg file type is not allowed, see. The same with other natively registered types, e.g. https://a.supportally.com/4NWVC2, https://a.supportally.com/TUH50E. Env.: WP 5.2 / PHP 5.6.
  2. See comment below.

src/Plugin.php Outdated Show resolved Hide resolved
src/Allowed.php Outdated Show resolved Hide resolved
src/Allowed.php Outdated Show resolved Hide resolved
src/Allowed.php Outdated Show resolved Hide resolved
src/Allowed.php Outdated Show resolved Hide resolved
src/Allowed.php Outdated Show resolved Hide resolved
src/Allowed.php Outdated Show resolved Hide resolved
src/Migrations/Dispatcher.php Outdated Show resolved Hide resolved
@kkarpieszuk
Copy link
Contributor Author

@cheh I have fixed issue with missing option to upload native type even though it was checked as available

additionally, I have applied all your suggested changes. In a few comments you asked why I decided for some approach and my general answer is: it was because I changed strategy during the work on this and sometimes I forgot to clean the older apporach (like injecting Plugin class into Allowed). I cleared all such cases and now code should be simpler.

@kkarpieszuk kkarpieszuk assigned cheh and unassigned kkarpieszuk Jun 27, 2022
@cheh
Copy link
Contributor

cheh commented Jun 27, 2022

Unfortunately, there is PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR) in /plugins/file-upload-types/src/functions.php on line 31 on PHP 5.6 / WP 5.2.

I guess the root of the problem is using Plugin::get_instance() - we don't need to wrap it in brackets, e.g. Plugin::get_instance()->get_native_types();

@kkarpieszuk
Copy link
Contributor Author

I moved it to separate line for better clarity (also in one more place where I wrapped it in brackets in the same way). @cheh please continue

Copy link
Contributor

@cheh cheh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an interesting case when the user adds a fake MIME Type with an extension that already exists - https://a.supportally.com/iTIwIg. Personally, I expected that I wasn't able to upload *.numbers file.

add_filter( 'fileuploadtypes_settings_transform_native_type', [ $this, 'get_upload_type' ] );
}

// phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this phpcs:disable is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is needed in fact: without this, phpcs says the complexity of this method is too high.


$stored_types = get_option( 'file_upload_types', [] );
$native_types = isset( $stored_types['native'] ) ? (array) $stored_types['native'] : [];
$enabled_types = isset( $stored_types['enabled'] ) ? (array) $stored_types['enabled'] : [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is used in several places, perhaps it could be moved into a separate method (maybe introduce a new Helper class) or global function. So, using that we always receive an array with needed keys (enabled, native, custom).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introduced new StoredTypes class with magic __get method. It cries for __set method as well but adding it would be completely out of scope of this ticket: if I did it, I would have to rewrite tons of code in this plugin and it will be not related to native types anymore.

So for the time being I didn't touch any code related to setting/saving data to wp_options


remove_filter( 'upload_mimes', [ $this->allowed, 'allowed_types' ] );

self::$unfiltered_types = get_allowed_mime_types();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good point is to skip types allowed by the FUT plugin.
But what about other third-party plugins that can allow custom/own MIME types?
In that case, we cannot assure that all retrieved types are allowed natively by WordPress.

Does WP have any other ways here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"native" name is a bit misinforming here, but I didn't find anything better. In fact the logic here is to add support for any file types which are not coming from our json file.

So it doesn't matter if the file type is natively added by WP plugin or some plugin, other than File Upload Types plugin.

Therefore, the logic here is correct: we just want to get all file types which are not affected by FUT plugin. We should keep this piece of code as is.

if ( ! empty( $native_types ) ) :

?>
<tr class="section">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a new abstraction/class for passing data to templates and move HTML markup to a template file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can completely do this, but I vote to create an GH issue for this and handle separately as this is not 100% related to the task. I mean this piece of code is, but what do you think about introducing templates for all html we already have? This code to echo pieces of <table> is here and there: it would be great to convert them to templates as a separate task, what to you say?

*
* @since {VERSION}
*
* @return void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, we don't have an agreement to add return void for method/functions if they don't return anything. Could you please remove it here and in other places?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

$option_changed = false;

foreach ( $all_migrations as $name => $callback ) {
if ( ! isset( $already_run[ $name ] ) && is_callable( $callback ) && $callback() ) {
Copy link
Contributor

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.

foreach ( $all_migrations as $name => $callback ) {
if ( ! isset( $already_run[ $name ] ) && is_callable( $callback ) && $callback() ) {
$already_run[ $name ] = 1;
$option_changed = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, remove one extra tab-indent here and above.

$all_migrations = $this->get_migrations_list();
$option_changed = false;

foreach ( $all_migrations as $name => $callback ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->get_migrations_list() could be used instead of $all_migrations.

src/Settings.php Outdated
@@ -290,6 +292,10 @@ public function display_types_table() { // phpcs:ignore Generic.Metrics.NestingL
}

endif;

// phpcs:disable WPForms.Comments.PHPDocHooks.RequiredHookDocumentation
do_action( 'fileuploadtypes_settings_display_types_table_after_enabled_types' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesomemotive/wpforms-phpcs package has an issue and notify about an invalid hook name incorrectly. For better readability the FileUploadTypes namespace should be converted to file_upload_types.

Could you please change nabes for all new hooks?
Obviously, you will have to use phpcs:disable / phpcs:enable for reducing a noise.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

src/Allowed.php Outdated
*
* @var array
*/
private $enabled_types;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be initialized as an empty array.

@cheh cheh removed their assignment Jun 28, 2022
@kkarpieszuk
Copy link
Contributor Author

@cheh I've applied most of your suggestions and if I didn't I provided my reasoning comments why not.

about this:

There is an interesting case when the user adds a fake MIME Type with an extension that already exists - https://a.supportally.com/iTIwIg. Personally, I expected that I wasn't able to upload *.numbers file.

Yup, but the same is in develop branch so it is not related to the task.

In general I found - but I didn't explore it deeply yet, I am still quite new to FUT plugin and mime types handling in WP - WordPress probably in fact ignores mime types when restricting and looks only on file extension. Weird and I might be wrong, but it requires deeper investigation and that's why I suggest a separate ticket.

So to sum up again:

  • I've applied suggested changes when neccessary
  • I've provided comments why I can't change some parts
  • I suggest two separate tickets: one for html templates and one for the mime issue

@kkarpieszuk kkarpieszuk assigned cheh and unassigned kkarpieszuk Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restricting file upload types
2 participants