Skip to content

Commit

Permalink
multisite tab
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBernskiold committed Oct 5, 2021
1 parent 45d056e commit ed299f4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. This projec
- New `forge setup:block-build` command to easily scaffold the block build process.
- Abstract `Job` class for creating long-running background tasks.
- Abstract `Bulk_Action` class for quickly scaffolding new bulk actions.
- Abstract `Multisite_Tab` class for quicklt scaffolding a new tab in the network admin website edit view.

### Fixed

Expand Down
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,60 @@ class My_Bulk_Action extends Bulk_Action {
}
}
```

_Don't forget to load the bulk action by adding it to the `$boot` array in the main plugin file._

### Adding a multisite tab

Extend this abstract class to add a new tab to the network admin website edit screen.

```php
use BernskioldMedia\WP\PluginBase\Admin\Multisite_Tab;

class My_Tab extends Multisite_Tab {

protected static string $nonce = 'my-tab-nonce';
protected static string $slug = 'my-tab';
protected static string $capability = 'manage_sites';

protected static function get_title(): string {
return __( 'My Tab', 'TEXTDOMAIN' );
}

public static function notice(): void {

if ( ! isset( $_GET['updated'], $_GET['page'] ) || self::$slug !== $_GET['page'] ) {
return;
}

?>
<div class="notice is-dismissible updated">
<p><?php esc_html_e( 'Success message.', 'TEXTDOMAIN' ); ?></p>
</div>
<?php

}

public static function save( WP_Site $site, $request_data ): void {
// Handle saving.
}

public static function render(): void {
$site = self::get_site_from_request();

if ( ! $site ) {
return;
}

?>
<div class="wrap">
<p>Tab Content</p>
</div>
<?php

}

}
```

_Don't forget to load the tab by adding it to the `$boot` array in the main plugin file._
107 changes: 107 additions & 0 deletions src/Admin/Multisite_Tab.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace BernskioldMedia\WP\PluginBase\Admin;

use BernskioldMedia\WP\PluginBase\Interfaces\Hookable;

/**
* Class Multisite_Tab
*
* @package BernskioldMedia\WP\PluginBase\Admin
*/
abstract class Multisite_Tab implements Hookable {

protected static string $slug = '';
protected static string $nonce = '';
protected static string $capability = 'manage_sites';

public static function hooks(): void {
add_filter( 'network_edit_site_nav_links', [ static::class, 'add_tab' ] );
add_action( 'network_admin_menu', [ static::class, 'add_page' ] );
add_action( 'network_admin_edit_' . static::$slug, [ static::class, 'handle_save' ] );
add_action( 'network_admin_notices', [ static::class, 'notice' ] );
}

abstract protected static function get_title(): string;

abstract public static function notice(): void;

abstract public static function save( \WP_Site $site, $request_data ): void;

abstract public static function render(): void;

/**
* Handle the saving.
*/
public static function handle_save(): void {
$site_id = (int) $_POST['id'];

if ( ! $site_id ) {
return;
}

$site = get_site( $site_id );

if ( ! $site ) {
return;
}

check_admin_referer( static::$nonce . '-' . $site_id );

static::save( $site, $_POST );

wp_redirect( add_query_arg( [
'page' => static::$slug,
'id' => $site_id,
'updated' => true,
], esc_url( network_admin_url( 'sites.php' ) ) ) );

exit;
}

/**
* Add Menu Page
*/
public static function add_page(): void {
add_submenu_page( null, static::get_title(), static::get_title(), static::$capability, static::$slug, [ static::class, 'render' ] );
}

/**
* Add the Tab
*
* @param array $tabs
*
* @return mixed
*/
public static function add_tab( array $tabs ) {
$tabs[ static::$slug ] = [
'label' => static::get_title(),
'url' => 'sites.php?page=' . static::$slug,
'cap' => static::$capability,
];

return $tabs;
}

/**
* Get Site Object from REQUEST.
*
* @return \WP_Site|null
*/
protected static function get_site_from_request(): ?\WP_Site {
$id = (int) $_REQUEST['id'];

if ( ! $id ) {
return null;
}

$site = get_site( $id );

if ( $site ) {
return $site;
}

return null;
}

}

0 comments on commit ed299f4

Please sign in to comment.