Skip to content

Commit

Permalink
Merge pull request #2 from happyprime/fix/improvements
Browse files Browse the repository at this point in the history
Focus on templates, ignore template parts
  • Loading branch information
jeremyfelt authored May 3, 2023
2 parents cec0cf4 + 824a4bc commit 8be03d0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 50 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Restrict Network Templates

Restrict the use of templates and `network-` prefixed template parts to the main site on a network.
Restrict the management of templates to a network's main site.

## Description

This plugin should be network activated on a multisite network. When activated:

* The list of default template types is filtered to return an empty list.
* The `/wp/v2/templates` endpoint in WordPress returns an empty list.
* Any template part with a filename starting with `network-` is removed from lists of template parts.
* The `WP_REST_Templates_Controller` permissions check is overridden to prevent the update of templates and `network-` prefixed template parts.
* The `WP_REST_Templates_Controller` permissions check is overridden to prevent the update of templates outside of the main site.

This plugin works in tandem with [Network Template Parts](https://github.com/happyprime/network-template-parts) to provide a framework for a shared look and feel of websites on a multisite network.

Activating this plugin **will** impact the usefulness of the full site editor in WordPress and will require thinking about the site in parts rather than full templates. It assumes the main template files in your theme will be built using the blocks provided in [Network Template Parts](https://github.com/happyprime/network-template-parts).
Activating this plugin **will** impact the usefulness of the full site editor in WordPress and will require thinking about the site in parts rather than full templates.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "happyprime/restrict-network-templates",
"description": "Restrict the use of templates and network- prefixed parts to the main site.",
"version": "0.0.1",
"description": "Restrict the management of templates to a network's main site.",
"version": "1.0.0",
"type": "wordpress-plugin",
"license": "GPLv2-or-later",
"config": {
Expand Down
54 changes: 10 additions & 44 deletions plugin.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Plugin Name: Restrict Network Templates
* Description: Restrict the use of templates and network- prefixed parts to the main site.
* Version: 0.0.1
* Description: Restrict the management of templates to a network's main site.
* Version: 1.0.0
* Plugin URI: https://github.com/happyprime/restrict-network-templates/
* Author: Happy Prime
* Author URI: https://happyprime.co
Expand All @@ -26,11 +26,10 @@

add_filter( 'default_template_types', '__return_empty_array' );
add_filter( 'rest_post_dispatch', __NAMESPACE__ . '\filter_wp_template_rest_response', 10, 3 );
add_filter( 'get_block_templates', __NAMESPACE__ . '\filter_block_template_parts', 10, 3 );
add_filter( 'rest_request_before_callbacks', __NAMESPACE__ . '\rest_pre_check', 10, 3 );

/**
* Filter REST requests for templates to only those the user is able to edit.
* Filter REST requests for templates to include results only on the main site.
*
* @param \WP_REST_Respones $response The prepared REST response.
* @param \WP_REST_Server $server The REST server.
Expand All @@ -50,67 +49,34 @@ function filter_wp_template_rest_response( $response, $server, $request ) {
}

/**
* Filter block template parts on sub-sites so that network-level template
* parts are not offered to the user.
*
* @param \WP_Block_Template[] $query_result Array of found block templates.
* @param array $query {
* Optional. Arguments to retrieve templates.
*
* @type array $slug__in List of slugs to include.
* @type int $wp_id Post ID of customized template.
* }
* @param string $template_type wp_template or wp_template_part.
* @return \WP_Block_Template[] Modified array of found block templates.
*/
function filter_block_template_parts( $query_result, $query, $template_type ) {
if ( is_main_site() || 'wp_template_part' !== $template_type ) {
return $query_result;
}

$filtered = [];

foreach ( $query_result as $template ) {
if ( 'network-' === substr( $template->slug, 0, 8 ) ) {
continue;
}
$filtered[] = $template;
}

return $filtered;
}

/**
* Prevent a user from saving templates in the site editor.
* Prevent a user from saving templates in the site editor on sub-sites.
*
* @param mixed $response Result to send to the client. This is a pre-check, so we expect null.
* @param array $handler Route handler used for the request.
* @param \WP_REST_Request $request Request used to generate the response.
*/
function rest_pre_check( $response, $handler, $request ) {
if ( 'GET' === $request->get_method() ) {
if ( is_main_site() ) {
return $response;
}

if ( ! is_array( $handler['callback'] ) || ! $handler['callback'][0] instanceof \WP_REST_Templates_Controller ) {
if ( 'GET' === $request->get_method() ) {
return $response;
}

$route = $request->get_route();

if ( '/wp/v2/templates/' !== substr( $route, 0, 17 ) ) {
if ( ! is_array( $handler['callback'] ) || ! $handler['callback'][0] instanceof \WP_REST_Templates_Controller ) {
return $response;
}

$template_id = array_pop( explode( '/wp/v2/templates/', $route ) );
$route = $request->get_route();

if ( current_user_can( 'edit-template-part', $template_id ) ) {
if ( ! str_starts_with( $route, '/wp/v2/templates' ) ) {
return $response;
}

return new \WP_Error(
'rest_cannot_manage_templates',
__( 'Sorry, you are not allowed to access the templates on this site.' ),
__( 'Sorry, templates must be managed on the main site.' ),
array(
'status' => rest_authorization_required_code(),
)
Expand Down
28 changes: 28 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Restrict Network Templates
Contributors: happyprime, jeremyfelt, slocker, philcable
Tags: site-editor, templates, multisite
Requires at least: 6.2
Tested up to: 6.2
Stable tag: 1.0.0
License: GPLv2 or later
Requires PHP: 7.4

Restrict the management of templates to a network's main site.

## Description

This plugin should be network activated on a multisite network. When activated:

* The list of default template types is filtered to return an empty list.
* The `/wp/v2/templates` endpoint in WordPress returns an empty list.
* The `WP_REST_Templates_Controller` permissions check is overridden to prevent the update of templates outside of the main site.

This plugin works in tandem with [Network Template Parts](https://github.com/happyprime/network-template-parts) to provide a framework for a shared look and feel of websites on a multisite network.

Activating this plugin **will** impact the usefulness of the full site editor in WordPress and will require thinking about the site in parts rather than full templates.

## Changelog

### 1.0.0

Initial release

0 comments on commit 8be03d0

Please sign in to comment.