Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"scripts": {
"lint": "php ./vendor/bin/phpcs -ps .",
"make-pot": "./vendor/bin/wp make-pot ./ languages/prompress.pot --domain=prompress"
"make-pot": "./vendor/bin/wp i18n make-pot ./ languages/prompress.pot --domain=prompress"
},
"config": {
"sort-packages": true,
Expand Down
6 changes: 6 additions & 0 deletions inc/admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ function register_page() {
*/
function render_page() {
?>
<script>
var prompress = {
settings: <?php echo wp_json_encode( get_settings() ); ?>,
configTemplate: <?php echo wp_json_encode( get_prometheus_config_template() ) . "\n"; ?>
};
</script>
<div id="prompress-plugin-settings"></div>
<?php
}
Expand Down
27 changes: 27 additions & 0 deletions inc/rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ function register_rest_routes(): void {
[
'methods' => \WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\\storage_compatibility',
'permission_callback' => function () {
return \current_user_can( 'manage_options' );
},
]
);

Expand All @@ -52,6 +55,9 @@ function register_rest_routes(): void {
[
'methods' => \WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\\storage_wipe',
'permission_callback' => function () {
return \current_user_can( 'manage_options' );
},
]
);
}
Expand All @@ -72,6 +78,27 @@ function metrics_permissions(): bool {
return true;
}

if ( 'api-key' === $settings['authType'] ) {
$expected_header = strtolower( $settings['headerKey'] ?? '' );
if ( $expected_header ) {
$headers = \getallheaders();

$secret = null;
foreach ( $headers as $key => $value ) {
if ( strtolower( $key ) === $expected_header ) {
$secret = $value;
break;
}
}

if ( $secret ) {
return ( $settings['headerValue'] ?? null ) === $secret;
}
}

return false;
}

$auth_header = wp_get_auth_headers();
if (!empty($auth_header['Authorization'])) {
if (\preg_match('/Bearer\s(\S+)/', $auth_header['Authorization'], $matches)) {
Expand Down
37 changes: 37 additions & 0 deletions inc/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ function register_settings() {
'authentication' => [
'type' => 'boolean',
],
'authType' => [
'type' => 'string',
],
'token' => [
'type' => 'string',
],
'headerKey' => [
'type' => 'string',
],
'headerValue' => [
'type' => 'string',
],
'features' => [
'type' => 'object',
'properties' => [
Expand Down Expand Up @@ -75,6 +84,31 @@ function get_settings(): array {
return $settings;
}

/**
* Returns prometheus config template.
*/
function get_prometheus_config_template(): string {
$url = rtrim( get_home_url(), '/' ) . '/wp-json/prompress/v1/metrics';
$parts = wp_parse_url( $url );

$template = <<< EOC
scrape_configs:
- job_name: 'live-your-site'
scheme: %scheme%
%auth%
metric_path: '%uri%'
static_configs:
- targets: ['%domain%']

EOC;

return str_replace(
[ '%scheme%', '%uri%', '%domain%' ],
[ $parts['scheme'], $parts['path'], $parts['host'] ],
$template
);
}

/**
* Update settings.
*/
Expand Down Expand Up @@ -112,7 +146,10 @@ function default_settings(): array {
'active' => true,
'storage' => 'apc',
'authentication' => false,
'authType' => 'bearer',
'token' => '',
'headerKey' => 'x-prompress-auth',
'headerValue' => '',
'features' => [
'emails' => true,
'errors' => true,
Expand Down
Loading