Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Labs: GitHub-177: Add functionality to define constant to enable nginx logging #316

Merged
merged 18 commits into from
Jan 17, 2024
Merged
5 changes: 2 additions & 3 deletions .github/ci/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function start_services() {
# Create, setup and populate learn.rtcamp.com base site with data
function create_and_configure_site () {

ee site create $SITE_NAME --wp --php74
ee site create $SITE_NAME --wp --php74
cd $SITE_ROOT/wp-content/plugins/
rm -rf nginx-helper
ls
Expand All @@ -36,7 +36,6 @@ function create_and_configure_site () {
ls
wp plugin activate nginx-helper --allow-root
wp user create automation [email protected] --role=administrator --user_pass=automation --allow-root
wp theme activate twentytwentyone --allow-root
}


Expand Down Expand Up @@ -96,4 +95,4 @@ function main() {
run_playwright_tests
}

main
main
27 changes: 24 additions & 3 deletions admin/class-nginx-helper-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Nginx_Helper_Admin {
*
* @since 2.0.0
* @access public
* @var string $options Purge options.
* @var string[] $options Purge options.
*/
public $options;

Expand Down Expand Up @@ -345,6 +345,27 @@ public function nginx_helper_settings_link( $links ) {

}

/**
* Check if the nginx log is enabled.
*
* @since 2.2.4
* @return boolean
*/
public function is_nginx_log_enabled() {

$options = get_site_option( 'rt_wp_nginx_helper_options', array() );

if ( ! empty( $options['enable_log'] ) && 1 === (int) $options['enable_log'] ) {
return true;
}

if ( defined( 'NGINX_HELPER_LOG' ) && true === NGINX_HELPER_LOG ) {
return true;
}

return false;
}

/**
* Retrieve the asset path.
*
Expand Down Expand Up @@ -732,10 +753,10 @@ public function purge_all() {
}

if ( 'purge' === $action ) {

/**
* Fire an action after the entire cache has been purged whatever caching type is used.
*
*
* @since 2.2.2
*/
do_action( 'rt_nginx_helper_after_purge_all' );
Expand Down
4 changes: 2 additions & 2 deletions admin/class-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ public function log( $msg, $level = 'INFO' ) {

global $nginx_helper_admin;

if ( ! $nginx_helper_admin->options['enable_log'] ) {
if ( ! $nginx_helper_admin->is_nginx_log_enabled() ) {
return;
}

Expand Down Expand Up @@ -562,7 +562,7 @@ public function check_and_truncate_log_file() {

global $nginx_helper_admin;

if ( ! $nginx_helper_admin->options['enable_log'] ) {
if ( ! $nginx_helper_admin->is_nginx_log_enabled() ) {
return;
}

Expand Down
1 change: 1 addition & 0 deletions admin/css/nginx-helper-admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@ form#purgeall .button-primary:focus {
}

.rt-purge_url { width: 100%; }

23 changes: 21 additions & 2 deletions admin/partials/nginx-helper-general-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,29 @@
<?php } ?>
<tr valign="top">
<td>
<input type="checkbox" value="1" id="enable_log" name="enable_log"<?php checked( $nginx_helper_settings['enable_log'], 1 ); ?> />
<?php
$is_checkbox_enabled = false;
if ( 1 === (int) $nginx_helper_settings['enable_log'] ) {
$is_checkbox_enabled = true;
}
?>
<input
type="checkbox" value="1" id="enable_log" name="enable_log"
<?php checked( $nginx_helper_admin->is_nginx_log_enabled(), true ); ?>
<?php echo esc_attr( $is_checkbox_enabled ? '' : ' disabled ' ); ?>
gagan0123 marked this conversation as resolved.
Show resolved Hide resolved
/>
<label for="enable_log">
<?php esc_html_e( 'Enable Logging', 'nginx-helper' ); ?>
</label>
<?php
if ( ! $nginx_helper_admin->is_nginx_log_enabled() ) {
printf(
'<pre>[<b>NOTE:</b> %1$s<br /><b>define( \'NGINX_HELPER_LOG\', true )</b> %2$s <b>wp-config.php</b>]</pre>',
vishalkakadiya marked this conversation as resolved.
Show resolved Hide resolved
esc_html__( 'To activate the logging feature, you must define the constant', 'nginx-helper' ),
esc_html__( 'in your', 'nginx-helper' )
);
}
?>
</td>
</tr>
<tr valign="top">
Expand Down Expand Up @@ -626,7 +645,7 @@
<?php
}
?>
<div class="postbox enable_log"<?php echo ( empty( $nginx_helper_settings['enable_log'] ) ) ? ' style="display: none;"' : ''; ?>>
<div class="postbox enable_log"<?php echo ( ! $nginx_helper_admin->is_nginx_log_enabled() ) ? ' style="display: none;"' : ''; ?>>
<h3 class="hndle">
<span><?php esc_html_e( 'Logging Options', 'nginx-helper' ); ?></span>
</h3>
Expand Down
21 changes: 9 additions & 12 deletions tests/e2e-playwright/specs/04_enable-debug-option-test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ test.describe("Enable the debug option", () => {

expect(page.locator(".rt_option_title")).toHaveText("Nginx Settings");

if (await page.locator("#enable_log").uncheck) {
await page.click("#enable_log");
}
if (await page.locator("#enable_stamp").uncheck) {
await page.click("#enable_stamp");
}

await page.click("#smart_http_expire_save");

await page.waitForTimeout(1000);
expect(page.locator("div[class='updated'] p")).toHaveText(
"Settings saved."
// Assert that the checkbox is disabled
await expect(page.locator("input#enable_log")).toBeDisabled();

expect(
page.locator(
"#post_form > div:nth-child(5) > div > table > tbody > tr:nth-child(1) > td > pre"
)
).toHaveText(
"[NOTE: To activate the logging feature, you must define the constantdefine( 'NGINX_HELPER_LOG', true ) in your wp-config.php]"
vishalkakadiya marked this conversation as resolved.
Show resolved Hide resolved
);
});

Expand Down