PluginProbe
Parse.ly / 3.2.1
Parse.ly v3.2.1
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / src / UI / class-network-admin-sites-list.php

class-network-admin-sites-list.php in Parse.ly 3.2.1, at src/UI/class-network-admin-sites-list.php

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