Instantly share code, notes, and snippets.
Fast REST Response for Super Admin All Sites Menu (https://github.com/soderlind/super-admin-all-sites-menu), save in mu-plugins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* name: Fast REST Response for Super Admin All Sites Menu | |
* | |
* @package Soderlind\Multisite\MU | |
* @author Per Soderlind | |
* @copyright 2021 Per Soderlind | |
* @license GPL-2.0+ | |
* | |
* @wordpress-plugin | |
* Plugin Name: Fast REST Response for Super Admin All Sites Menu | |
* Description: Must Use plugin for Super Admin All Sites Menu (https://github.com/soderlind/super-admin-all-sites-menu) | |
* Version: 0.0.1 | |
* Author: Per Soderlind | |
* Author URI: https://soderlind.no | |
* License: GPL-2.0+ | |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | |
*/ | |
declare( strict_types = 1 ); | |
namespace Soderlind\Multisite\MU; | |
if ( ! defined( 'ABSPATH' ) ) { | |
wp_die(); | |
} | |
/** | |
* Fires once all must-use and network-activated plugins have loaded. | |
* | |
*/ | |
add_action( 'muplugins_loaded',function() : void { | |
$request_uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); | |
if ( '/wp-json/super-admin-all-sites-menu/v1/sites/' !== trailingslashit( $request_uri ) ) { | |
return; | |
} | |
$params = json_decode(file_get_contents("php://input"), true); // get HTTP Raw POST data. | |
if ( ! is_array( $params ) ) { | |
return; | |
} | |
$offset = ( isset( $params['offset'] ) ) ? filter_var( wp_unslash( $params['offset'] ), FILTER_VALIDATE_INT, [ 'default' => 0 ] ) : 0; | |
$mua = new MU_Super_Admin_All_Sites_Menu(); | |
wp_send_json( $mua->get_sites_menu( $offset ) ); | |
} ); | |
class MU_Super_Admin_All_Sites_Menu { | |
/** | |
* REST load increments. | |
* | |
* @var int | |
*/ | |
private $load_increments; | |
/** | |
* Timestamp. | |
* | |
* @var string | |
*/ | |
private $timestamp; | |
public function __construct() { | |
$this->load_increments = \apply_filters( 'all_sites_menu_load_increments', 100 ); | |
$this->timestamp = $this->get_timestamp(); | |
} | |
/** | |
* Get menu data. | |
* | |
* @param integer $offset get_sites offset. | |
* | |
* @return array | |
*/ | |
public function get_sites_menu( int $offset = 0 ) : array { | |
$menu = []; | |
$sites = \get_sites( | |
[ | |
'orderby' => 'path', | |
'number' => $this->load_increments, | |
'offset' => $offset, | |
'deleted' => '0', | |
'mature' => '0', | |
'archived' => '0', | |
'spam' => '0', | |
] | |
); | |
for ( $i = 0; $i < count( $sites ); $i++ ) { | |
$blogid = $sites[$i]->blog_id; | |
$blogname = $sites[$i]->__get( 'blogname' ); | |
$menu_id = 'blog-' . $blogid; | |
$blavatar = '<div class="blavatar"></div>'; | |
$siteurl = $sites[$i]->__get( 'siteurl' ); | |
$adminurl = $siteurl . '/wp-admin'; | |
if ( ! $blogname ) { | |
$blogname = preg_replace( '#^(https?://)?(www.)?#', '', $siteurl ); | |
} | |
// The $site->public value is set to 2, by the Restricted Site Access plugin, when a site has restricted access. | |
if ( 2 === (int) $sites[$i]->public ) { | |
$blavatar = '<div class="blavatar" style="color:#f00;"></div>'; | |
} | |
$menu[] = [ | |
'parent' => 'my-sites-list', | |
'id' => $menu_id, | |
'name' => strtoupper( $blogname ), // Index in local storage. | |
'title' => $blavatar . $blogname, | |
'admin' => $adminurl, | |
'url' => $siteurl, | |
'timestamp' => $this->timestamp, | |
]; | |
} | |
if ( [] !== $menu ) { | |
$response['response'] = 'success'; | |
$response['data'] = $menu; | |
} else { | |
$response['response'] = 'unobserve'; | |
$response['data'] = ''; | |
} | |
return $response; | |
} | |
/** | |
* Get the timestamp. | |
* | |
* @return string | |
*/ | |
private function get_timestamp() : string { | |
$timestamp = get_site_transient( 'allsitemenutimestamp' ); | |
if ( ! $timestamp ) { | |
$timestamp = (string) time(); | |
set_site_transient( 'allsitemenutimestamp', $timestamp, 3600 ); | |
} | |
return $timestamp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment