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