Make WordPress Core

Opened 12 years ago

Closed 9 years ago

#22924 closed enhancement (fixed)

Add filter to theme editor accepted files

Reported by: pross's profile pross Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version: 3.5
Component: Themes Keywords: has-patch commit
Focuses: Cc:

Description

Proposing adding a filter to theme-editor.php so theme authors can add to the list of files that are editable. ie html/less/js files.

Hers an example of what could be possible:

add_filter( 'wp_theme_editor_filetypes', 'add_my_filetype', 10, 2 );
function add_my_filetype( $allowed_types, $theme ) {
	
	$extra = $theme->get_files( 'less', 1 );
	return $allowed_types += $extra;
}

Attachments (8)

theme-editor.patch (611 bytes) - added by pross 12 years ago.
22924.diff (1.4 KB) - added by MikeHansenMe 10 years ago.
Refreshed with docs
22924.2.diff (1.7 KB) - added by SergeyBiryukov 9 years ago.
22924.png (4.9 KB) - added by SergeyBiryukov 9 years ago.
22924.3.diff (4.0 KB) - added by SergeyBiryukov 9 years ago.
22924.3.png (5.5 KB) - added by SergeyBiryukov 9 years ago.
22924.4.diff (4.0 KB) - added by SergeyBiryukov 9 years ago.
Forgot to remove 'txt' from defaults in previous patch
22924.5.diff (4.1 KB) - added by DrewAPicture 9 years ago.

Download all attachments as: .zip

Change History (24)

@pross
12 years ago

#1 @SergeyBiryukov
12 years ago

  • Version changed from trunk to 3.5

#2 @MikeHansenMe
12 years ago

  • Cc mdhansen@… added
  • Type changed from defect (bug) to enhancement

Created a test.less file in my theme, added the patch and example. It worked great, I think this would be a good addition.

#4 @iseulde
10 years ago

  • Component changed from Editor to Administration

#5 @iseulde
10 years ago

  • Component changed from Administration to Themes

@MikeHansenMe
10 years ago

Refreshed with docs

#7 @wonderboymusic
9 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 35134:

Theme Editor: add a filter, wp_theme_editor_filetypes, to allow more filetypes to be edited via the Theme Editor. Usage of the Theme Editor is strenuously discouraged, but we should allow you to add whatever filetypes (.less, .scss) you would like.

Props MikeHansenMe, pross.
Fixes #22924.

#8 @wonderboymusic
9 years ago

  • Milestone changed from Awaiting Review to 4.4

#9 follow-up: @DrewAPicture
9 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Is the intention of the new wp_theme_editor_filetypes filter to make it possible to bring in arbitrary files or to expand the filetypes? Seems like based on the name alone we might be better off doing an array of file types then looping with $theme->get_files() on those types, e.g.

(pseudo)

<?php
/**
 * Filter the list of files allowed for editing in the Theme editor.
 *
 * @since 4.4.0
 *
 * @param array  $file_types List of file types.
 * @param object $theme      The current Theme object.
 */
$file_types = apply_filters( 'wp_theme_editor_filetypes', array( 'php', 'css' ), $theme );

foreach ( $file_types as $type ) {
        $allowed_files + $theme->get_files( $type );
}

...

if ( $allowed_files ) :
        if ( $theme->get_files( 'php' ) || $theme->parent() ) :
?>

The hook description also needs a little work, it's super vague.

#10 in reply to: ↑ 9 ; follow-up: @pross
9 years ago

Replying to DrewAPicture:

Is the intention of the new wp_theme_editor_filetypes filter to make it possible to bring in arbitrary files or to expand the filetypes? Seems like based on the name alone we might be better off doing an array of file types then looping with $theme->get_files() on those types, e.g.

Original intention was to allow users the ability to edit .less files that were part of a theme.
The theme would add 'less' to the allowed filetypes using the filter.

#11 in reply to: ↑ 10 ; follow-up: @DrewAPicture
9 years ago

Replying to pross:

Replying to DrewAPicture:

Is the intention of the new wp_theme_editor_filetypes filter to make it possible to bring in arbitrary files or to expand the filetypes? Seems like based on the name alone we might be better off doing an array of file types then looping with $theme->get_files() on those types, e.g.

Original intention was to allow users the ability to edit .less files that were part of a theme.
The theme would add 'less' to the allowed filetypes using the filter.

Precisely. The problem is that with this hook name it implies filetypes not files. So if the intention is to actually filter file types, something closer to my solution in comment:9 would probably be better-suited.

@SergeyBiryukov
9 years ago

#12 in reply to: ↑ 11 ; follow-up: @SergeyBiryukov
9 years ago

Replying to DrewAPicture:

The problem is that with this hook name it implies filetypes not files. So if the intention is to actually filter file types, something closer to my solution in comment:9 would probably be better-suited.

22924.2.diff implements comment:9. php and css cannot be removed, only new types can be added.

One issue with both [35134] and the patch is that new files are added under "Styles" heading, which doesn't make sense (see 22924.png, where I've added txt as an example).

#13 in reply to: ↑ 12 @SergeyBiryukov
9 years ago

  • Keywords commit added

Replying to SergeyBiryukov:

One issue with both [35134] and the patch is that new files are added under "Styles" heading, which doesn't make sense (see 22924.png, where I've added txt as an example).

22924.3.diff fixes that. Other types are displayed in their own section, see 22924.3.png.

If new file types are added to the beginning of the array, before php, that section is displayed first.

This ticket was mentioned in Slack in #core by drew. View the logs.


9 years ago

@SergeyBiryukov
9 years ago

Forgot to remove 'txt' from defaults in previous patch

@DrewAPicture
9 years ago

#15 @DrewAPicture
9 years ago

@SergeyBiryukov: 22924.5.diff:

  • Removes txt type per your patch
  • $theme is a WP_Theme object
  • DRYs the base list of types
  • Adds an array_unique() around the merge

#16 @SergeyBiryukov
9 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 35180:

Theme Editor: Repurpose the 'wp_theme_editor_filetypes' filter added in [35134] to actually filter file types instead of files.

Default types include 'php' and 'css' and cannot be removed, only new types can be added.

Props DrewAPicture, SergeyBiryukov.
Fixes #22924.

Note: See TracTickets for help on using tickets.