| 1 |
<?php |
| 2 |
/** |
| 3 |
* UI: Site Health |
| 4 |
* |
| 5 |
* Extends WordPress Core's site health with the plugin's debug information. |
| 6 |
* |
| 7 |
* @package Parsely |
| 8 |
* @since 3.4.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
declare(strict_types=1); |
| 12 |
|
| 13 |
namespace Parsely\UI; |
| 14 |
|
| 15 |
use Parsely\Parsely; |
| 16 |
|
| 17 |
/** |
| 18 |
* Provides debug information about the plugin. |
| 19 |
* |
| 20 |
* @since 3.4.0 |
| 21 |
* |
| 22 |
* @phpstan-type Site_Health_Info array{ |
| 23 |
* parsely?: Parsely_Health_Info |
| 24 |
* } |
| 25 |
* |
| 26 |
* @phpstan-type Parsely_Health_Info array{ |
| 27 |
* label: string, |
| 28 |
* description: string, |
| 29 |
* show_count: bool, |
| 30 |
* fields: array<string, mixed>, |
| 31 |
* } |
| 32 |
*/ |
| 33 |
final class Site_Health { |
| 34 |
/** |
| 35 |
* Instance of Parsely class. |
| 36 |
* |
| 37 |
* @var Parsely |
| 38 |
*/ |
| 39 |
private $parsely; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor. |
| 43 |
* |
| 44 |
* @param Parsely $parsely Instance of Parsely class. |
| 45 |
*/ |
| 46 |
public function __construct( Parsely $parsely ) { |
| 47 |
$this->parsely = $parsely; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Registers the site health methods. |
| 52 |
* |
| 53 |
* @since 3.4.0 |
| 54 |
*/ |
| 55 |
public function run(): void { |
| 56 |
add_filter( 'site_status_tests', array( $this, 'check_site_id' ) ); |
| 57 |
add_filter( 'debug_information', array( $this, 'options_debug_info' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Adds a healthcheck function that tests whether the Site ID is set or not. |
| 62 |
* |
| 63 |
* @since 3.4.0 |
| 64 |
* |
| 65 |
* @param array<string, mixed> $tests An associative array of direct and asynchronous tests. |
| 66 |
* |
| 67 |
* @return array<string, mixed> |
| 68 |
*/ |
| 69 |
public function check_site_id( array $tests ): array { |
| 70 |
$test = function() { |
| 71 |
$result = array( |
| 72 |
'label' => __( 'The Site ID is correctly set up', 'wp-parsely' ), |
| 73 |
'status' => 'good', |
| 74 |
'badge' => array( |
| 75 |
'label' => 'Parse.ly', |
| 76 |
'color' => 'green', |
| 77 |
), |
| 78 |
'description' => sprintf( |
| 79 |
'<p>%s</p>', |
| 80 |
__( 'Your Site ID uniquely represents your site within Parse.ly. It is required for the tracking to work correctly.', 'wp-parsely' ) |
| 81 |
), |
| 82 |
'test' => 'loopback_requests', |
| 83 |
); |
| 84 |
|
| 85 |
if ( $this->parsely->site_id_is_missing() ) { |
| 86 |
$result['status'] = 'critical'; |
| 87 |
$result['label'] = __( 'You need to provide the Site ID', 'wp-parsely' ); |
| 88 |
$result['actions'] = __( 'The site ID can be set in the <a href="/wp-admin/options-general.php?page=parsely">Parse.ly Settings Page</a>.', 'wp-parsely' ); |
| 89 |
} |
| 90 |
|
| 91 |
return $result; |
| 92 |
}; |
| 93 |
|
| 94 |
/** |
| 95 |
* Variable. |
| 96 |
* |
| 97 |
* @var array<mixed> |
| 98 |
*/ |
| 99 |
$direct = $tests['direct']; |
| 100 |
$direct['parsely'] = array( |
| 101 |
'label' => __( 'Parse.ly Site ID', 'wp-parsely' ), |
| 102 |
'test' => $test, |
| 103 |
); |
| 104 |
|
| 105 |
return $tests; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Adds the Parse.ly options to core's debug information array. |
| 110 |
* |
| 111 |
* @since 3.4.0 |
| 112 |
* |
| 113 |
* @param Site_Health_Info $args The debug information to be added to the core information page. |
| 114 |
* |
| 115 |
* @return Site_Health_Info |
| 116 |
*/ |
| 117 |
public function options_debug_info( $args ) { |
| 118 |
$options = $this->parsely->get_options(); |
| 119 |
|
| 120 |
$args['parsely'] = array( |
| 121 |
'label' => __( 'Parse.ly Options', 'wp-parsely' ), |
| 122 |
'description' => __( 'Shows the options stored in the database used by the wp-parsely plugin.', 'wp-parsely' ), |
| 123 |
'show_count' => true, |
| 124 |
); |
| 125 |
|
| 126 |
foreach ( $options as $name => $value ) { |
| 127 |
$args['parsely']['fields'][ $name ] = array( |
| 128 |
'label' => $name, |
| 129 |
'value' => $value, |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
return $args; |
| 134 |
} |
| 135 |
} |
| 136 |
|