WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 4 years ago

#30594 closed enhancement (fixed)

New function to network enable a theme

Reported by: igmoweb Owned by: jeremyfelt
Milestone: 4.6 Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch has-unit-tests
Focuses: administration, multisite Cc:

Description

I haven't found any ticket asking about this but what if we create a new function to network enable/disable a theme?

Right now, wp-admin/themes.php is doing the following thing to enable themes in a network:

...
$allowed_themes = get_site_option( 'allowedthemes' );
	switch ( $action ) {
		case 'enable':
			check_admin_referer('enable-theme_' . $_GET['theme']);
			$allowed_themes[ $_GET['theme'] ] = true;
			update_site_option( 'allowedthemes', $allowed_themes );
...

We could encapsulate that in new functions, one to enabling and another to disabling.

Just in case, the code was pretty easy so I upload here a patch.

Thanks.

Attachments (7)

enable_themes_function.diff (3.1 KB) - added by igmoweb 6 years ago.
Enable/Disable themes network widly functions
enable_themes_function_2.diff (4.1 KB) - added by igmoweb 5 years ago.
Patch updated
enable_themes_function_3.diff (4.2 KB) - added by igmoweb 5 years ago.
Updated patch
enable_themes_function_3_unit_tests.diff (2.0 KB) - added by igmoweb 5 years ago.
Unit tests
30594.diff (7.3 KB) - added by igmoweb 4 years ago.
New functions + Unit tests
30594.2.diff (7.2 KB) - added by igmoweb 4 years ago.
Patch + Unit tests splited into 4 cases
30594.3.diff (7.0 KB) - added by jeremyfelt 4 years ago.

Download all attachments as: .zip

Change History (24)

@igmoweb
6 years ago

Enable/Disable themes network widly functions

#1 @helen
6 years ago

  • Version trunk deleted

#2 @jeremyfelt
5 years ago

  • Milestone changed from Awaiting Review to Future Release
  • Summary changed from New function to network activate a theme to New function to network enable a theme

Thanks for the ticket igmoweb, this seems pretty sensible.

  • I'd go with the singular wp_network_enable_theme() and wp_network_disable_theme().
  • We may be able to attach these as static methods to WP_Theme.

@igmoweb
5 years ago

Patch updated

#3 @igmoweb
5 years ago

Patch updated (enable_themes_function_2.diff). Now using WP_Theme and also improved the comments.

Thanks.

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


5 years ago

#5 @swissspidy
5 years ago

  • Keywords has-patch needs-unit-tests added

@igmoweb
5 years ago

Updated patch

#6 @igmoweb
5 years ago

I've updated the patch (enable_themes_function_3.diff), as it needed some refreshment and also attached two unit tests.

Thanks.

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


4 years ago

#8 @igmoweb
4 years ago

@jeremyfelt As discussed in #core, I'm attaching new patch: Functions + unit tests in the same patch

I've also added a few more asserts that make sure that network enabling/disabling the same theme twice works as expected.

@igmoweb
4 years ago

New functions + Unit tests

#9 @ericlewis
4 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

#10 @jeremyfelt
4 years ago

  • Milestone changed from Future Release to 4.5
  • Owner set to igmoweb
  • Status changed from new to assigned

Thanks @igmoweb, this is looking good.

Some minor adjustments:

  • I think we can follow how WP_Theme is setup in general and use WP_Theme::network_enable_theme() and WP_Theme::network_disable_theme() directly without providing the wp_network_* wrapper functions.
  • The additions to class-wp-theme.php need to be formatted per core code style. It looks like spacing is a bit off in the methods and the if statements need braces.
  • Let's split the tests up into 4 parts: network enable a single theme, network enable an array of themes, network disable a single theme, and network disable an array of themes.
Last edited 4 years ago by jeremyfelt (previous) (diff)

#11 @igmoweb
4 years ago

Hi @jeremyfelt

Here's the new patch.

Thanks.

@igmoweb
4 years ago

Patch + Unit tests splited into 4 cases

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


4 years ago

#13 @jorbin
4 years ago

  • Milestone changed from 4.5 to Future Release

Due to lack of activity and the upcoming 4.5 beta 1 deadline, this is going to be punted.

@jeremyfelt
4 years ago

#14 @jeremyfelt
4 years ago

  • Keywords 4.6-early added

Excellent stuff @igmoweb, thank you.

I've refreshed and adjusted the tests a bit in 30594.3.diff and added an is_multisite() check to each of the new methods. I think we'll be able to get this in early for 4.6.

#15 @igmoweb
4 years ago

Awesome. Thanks!

#16 @jeremyfelt
4 years ago

  • Keywords 4.6-early removed
  • Milestone changed from Future Release to 4.6
  • Owner changed from igmoweb to jeremyfelt
  • Status changed from assigned to accepted

#17 @jeremyfelt
4 years ago

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

In 37202:

Multisite: Introduce WP_Theme methods to network enable/disable themes.

  • WP_Theme::network_enable_theme() can be used to enable a theme or array of themes on a network.
  • WP_Theme::network_disable_theme() can be used to disable a theme or array of themes on a network.
  • Use these new methods in the network admin vs direct update_site_option() calls.
  • Add tests.

Props igmoweb.
Fixes #30594.

Note: See TracTickets for help on using tickets.