| 1 |
<?php |
| 2 |
|
| 3 |
namespace Pushly\Admin; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class SiteHealth { |
| 10 |
|
| 11 |
/** @var LogStore */ |
| 12 |
private $store; |
| 13 |
|
| 14 |
public function __construct( LogStore $store ) { |
| 15 |
$this->store = $store; |
| 16 |
} |
| 17 |
|
| 18 |
public function register_hooks(): void { |
| 19 |
add_filter( 'debug_information', [ $this, 'add_debug_info' ] ); |
| 20 |
add_filter( 'site_status_tests', [ $this, 'add_status_tests' ] ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Adds a "Pushly" section to Site Health → Info. |
| 25 |
* |
| 26 |
* @param array $info Existing debug information sections. |
| 27 |
* @return array Modified debug information sections. |
| 28 |
*/ |
| 29 |
public function add_debug_info( array $info ): array { |
| 30 |
$options = get_option( 'pushly', [] ); |
| 31 |
|
| 32 |
$sdk_key = $options['sdk_key'] ?? ''; |
| 33 |
if ( strlen( $sdk_key ) >= 4 ) { |
| 34 |
$masked = str_repeat( '*', strlen( $sdk_key ) - 4 ) . substr( $sdk_key, -4 ); |
| 35 |
} else { |
| 36 |
$masked = str_repeat( '*', strlen( $sdk_key ) ); |
| 37 |
} |
| 38 |
|
| 39 |
$enabled_post_types = $options['enabled_post_types'] ?? []; |
| 40 |
$post_types_display = ! empty( $enabled_post_types ) |
| 41 |
? implode( ', ', $enabled_post_types ) |
| 42 |
: __( 'None', 'pushly' ); |
| 43 |
|
| 44 |
$info['pushly'] = [ |
| 45 |
'label' => __( 'Pushly', 'pushly' ), |
| 46 |
'fields' => [ |
| 47 |
'plugin_version' => [ |
| 48 |
'label' => __( 'Plugin version', 'pushly' ), |
| 49 |
'value' => PUSHLY__PLUGIN_VERSION, |
| 50 |
], |
| 51 |
'sdk_key' => [ |
| 52 |
'label' => __( 'SDK key', 'pushly' ), |
| 53 |
'value' => $masked, |
| 54 |
], |
| 55 |
'api_key_configured' => [ |
| 56 |
'label' => __( 'API key configured', 'pushly' ), |
| 57 |
'value' => ! empty( $options['api_key'] ) |
| 58 |
? __( 'Yes', 'pushly' ) |
| 59 |
: __( 'No', 'pushly' ), |
| 60 |
], |
| 61 |
'sending_enabled' => [ |
| 62 |
'label' => __( 'Sending enabled', 'pushly' ), |
| 63 |
'value' => ! empty( $options['sending_enabled'] ) |
| 64 |
? __( 'Yes', 'pushly' ) |
| 65 |
: __( 'No', 'pushly' ), |
| 66 |
], |
| 67 |
'debug_logging_enabled' => [ |
| 68 |
'label' => __( 'Debug logging enabled', 'pushly' ), |
| 69 |
'value' => ! empty( $options['debug_logging_enabled'] ) |
| 70 |
? __( 'Yes', 'pushly' ) |
| 71 |
: __( 'No', 'pushly' ), |
| 72 |
], |
| 73 |
'enabled_post_types' => [ |
| 74 |
'label' => __( 'Enabled post types', 'pushly' ), |
| 75 |
'value' => $post_types_display, |
| 76 |
], |
| 77 |
'log_entry_count' => [ |
| 78 |
'label' => __( 'Log entry count', 'pushly' ), |
| 79 |
'value' => $this->store->count(), |
| 80 |
], |
| 81 |
], |
| 82 |
]; |
| 83 |
|
| 84 |
return $info; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Registers a direct status test for API key configuration. |
| 89 |
* |
| 90 |
* @param array $tests Existing site status tests. |
| 91 |
* @return array Modified site status tests. |
| 92 |
*/ |
| 93 |
public function add_status_tests( array $tests ): array { |
| 94 |
$tests['direct']['pushly_api_key'] = [ |
| 95 |
'label' => __( 'Pushly API key configuration', 'pushly' ), |
| 96 |
'test' => [ $this, 'test_api_key_configured' ], |
| 97 |
]; |
| 98 |
|
| 99 |
return $tests; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Tests whether the API key is configured when sending is enabled. |
| 104 |
* |
| 105 |
* @return array Site Health test result. |
| 106 |
*/ |
| 107 |
public function test_api_key_configured(): array { |
| 108 |
$options = get_option( 'pushly', [] ); |
| 109 |
$sending_enabled = ! empty( $options['sending_enabled'] ); |
| 110 |
$api_key_set = ! empty( $options['api_key'] ); |
| 111 |
|
| 112 |
if ( $sending_enabled && ! $api_key_set ) { |
| 113 |
return [ |
| 114 |
'label' => __( 'Pushly sending is enabled without an API key', 'pushly' ), |
| 115 |
'status' => 'critical', |
| 116 |
'badge' => [ |
| 117 |
'label' => __( 'Pushly', 'pushly' ), |
| 118 |
'color' => 'red', |
| 119 |
], |
| 120 |
'description' => '<p>' . __( 'Pushly notification sending is enabled, but no API key has been configured. Notifications cannot be delivered without a valid API key.', 'pushly' ) . '</p>', |
| 121 |
'test' => 'pushly_api_key', |
| 122 |
]; |
| 123 |
} |
| 124 |
|
| 125 |
return [ |
| 126 |
'label' => __( 'Pushly API key is configured correctly', 'pushly' ), |
| 127 |
'status' => 'good', |
| 128 |
'badge' => [ |
| 129 |
'label' => __( 'Pushly', 'pushly' ), |
| 130 |
'color' => 'blue', |
| 131 |
], |
| 132 |
'description' => '<p>' . __( 'The Pushly plugin API key configuration is valid.', 'pushly' ) . '</p>', |
| 133 |
'test' => 'pushly_api_key', |
| 134 |
]; |
| 135 |
} |
| 136 |
} |
| 137 |
|