| 1 |
<?php |
| 2 |
/** |
| 3 |
* UI: Network Admin site list class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\UI; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
|
| 15 |
/** |
| 16 |
* Renders the additions to the WordPress Multisite Network Admin Sites List |
| 17 |
* page. |
| 18 |
* |
| 19 |
* @since 3.2.0 |
| 20 |
*/ |
| 21 |
final class Network_Admin_Sites_List { |
| 22 |
public const COLUMN_NAME = 'parsely-site-id'; |
| 23 |
/** |
| 24 |
* Instance of Parsely class. |
| 25 |
* |
| 26 |
* @var Parsely |
| 27 |
*/ |
| 28 |
private $parsely; |
| 29 |
|
| 30 |
/** |
| 31 |
* Constructor. |
| 32 |
* |
| 33 |
* @param Parsely $parsely Instance of Parsely class. |
| 34 |
*/ |
| 35 |
public function __construct( Parsely $parsely ) { |
| 36 |
$this->parsely = $parsely; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Attaches network admin page functionality to the appropriate action and |
| 41 |
* filter hooks. |
| 42 |
* |
| 43 |
* @since 3.2.0 |
| 44 |
*/ |
| 45 |
public function run(): void { |
| 46 |
add_filter( 'manage_sites_action_links', array( self::class, 'add_action_link' ), 10, 2 ); |
| 47 |
add_filter( 'wpmu_blogs_columns', array( self::class, 'add_site_id_column' ) ); |
| 48 |
add_action( 'manage_sites_custom_column', array( $this, 'populate_site_id_column' ), 10, 2 ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Uses the manage_sites_action_links filter to append a link to the settings |
| 53 |
* page in the "row actions". |
| 54 |
* |
| 55 |
* @since 3.2.0 |
| 56 |
* |
| 57 |
* @param array<string, mixed> $actions The list of actions meant to be displayed for the current site's |
| 58 |
* context in the row actions. |
| 59 |
* @param int $_blog_id The blog ID for the current context. |
| 60 |
* @return array<string, mixed> The list of actions including ours. |
| 61 |
*/ |
| 62 |
public static function add_action_link( array $actions, int $_blog_id ): array { |
| 63 |
// phpcs:ignore WordPress.WP.Capabilities.Undetermined |
| 64 |
if ( ! current_user_can( Parsely::CAPABILITY ) ) { |
| 65 |
return $actions; |
| 66 |
} |
| 67 |
|
| 68 |
$actions['parsely-settings'] = sprintf( |
| 69 |
'<a href="%1$s" aria-label="%2$s">%3$s</a>', |
| 70 |
esc_url( esc_url( Parsely::get_settings_url( $_blog_id ) ) ), |
| 71 |
esc_attr( self::generate_aria_label_for_blog_id( $_blog_id ) ), |
| 72 |
__( 'Parse.ly Settings', 'wp-parsely' ) |
| 73 |
); |
| 74 |
|
| 75 |
return $actions; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Generates ARIA label content. |
| 80 |
* |
| 81 |
* @since 3.2.0 |
| 82 |
* |
| 83 |
* @param int $_blog_id Which sub-site to include in the ARIA label. |
| 84 |
* @return string ARIA label content including the blogname. |
| 85 |
*/ |
| 86 |
private static function generate_aria_label_for_blog_id( int $_blog_id ): string { |
| 87 |
$site = get_blog_details( $_blog_id ); |
| 88 |
$blogname = false === $site ? '' : $site->blogname; |
| 89 |
|
| 90 |
return sprintf( |
| 91 |
/* translators: blog name or blog id if empty */ |
| 92 |
__( 'Go to Parse.ly stats for "%s"', 'wp-parsely' ), |
| 93 |
'' === $blogname ? $_blog_id : $blogname |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Uses the wpmu_blogs_columns filter to register the column where we'll |
| 99 |
* display the site's Site ID (if configured). |
| 100 |
* |
| 101 |
* @since 3.2.0 |
| 102 |
* |
| 103 |
* @param array<string, mixed> $sites_columns The list of columns meant to be displayed in the sites list table. |
| 104 |
* @return array<string, mixed> The list of columns to display in the network admin table including ours. |
| 105 |
*/ |
| 106 |
public static function add_site_id_column( array $sites_columns ): array { |
| 107 |
$sites_columns[ self::COLUMN_NAME ] = __( 'Parse.ly Site ID', 'wp-parsely' ); |
| 108 |
return $sites_columns; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Uses the manage_sites_custom_column action to output each site's Site ID |
| 113 |
* (if configured). |
| 114 |
* |
| 115 |
* @since 3.2.0 |
| 116 |
* |
| 117 |
* @param string $column_name The column name for the current context. |
| 118 |
* @param int $_blog_id The blog ID for the current context. |
| 119 |
*/ |
| 120 |
public function populate_site_id_column( string $column_name, int $_blog_id ): void { |
| 121 |
if ( self::COLUMN_NAME !== $column_name ) { |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.switch_to_blog_switch_to_blog |
| 126 |
switch_to_blog( $_blog_id ); |
| 127 |
$site_id = $this->parsely->get_site_id(); |
| 128 |
restore_current_blog(); |
| 129 |
|
| 130 |
if ( strlen( $site_id ) > 0 ) { |
| 131 |
echo esc_html( $site_id ); |
| 132 |
} else { |
| 133 |
echo esc_html( '—' ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|