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

WIP: Clear cache button #57

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,15 @@ function render_listings_page() {
* Render the WP101 settings page.
*/
function render_settings_page() {
if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'wp101-flush-cache' ) ) {
/**
* Flush caches related to the WP101 plugin.
*/
do_action( 'wp101_flush_cache' );

add_settings_error( 'wp101', 'flush_cache', __( 'The WP101 cache has been flushed.', 'wp101' ), 'updated' );
}

include WP101_VIEWS . '/settings.php';
}

Expand Down
41 changes: 41 additions & 0 deletions tests/test-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
*/
class AdminTest extends TestCase {

public function setUp() {
global $wp_settings_errors;

parent::setUp();

$_POST = [];
$wp_settings_errors = [];
}

public function test_enqueue_scripts_registers_scripts() {
$this->assertFalse( wp_style_is( 'wp101-admin', 'registered' ) );
$this->assertFalse( wp_script_is( 'wp101-admin', 'registered' ) );
Expand Down Expand Up @@ -350,6 +359,38 @@ public function test_special_wp_errors_are_skipped_in_error_output( $code ) {
$this->assertNotContains( 'My error message', $output );
}

/**
* @ticket https://github.com/101videos/wp101plugin/issues/51
*/
public function test_render_settings_page_should_be_able_to_flush_the_cache() {
$_POST = [
'_wpnonce' => wp_create_nonce( 'wp101-flush-cache' ),
];

ob_start();
Admin\render_settings_page();
ob_get_clean();

$this->assertSame( 1, did_action( 'wp101_flush_cache' ) );
$this->assertNotEmpty( get_settings_errors() );
}

/**
* @ticket https://github.com/101videos/wp101plugin/issues/51
*/
public function test_render_settings_page_should_verify_cache_nonce() {
$_POST = [
'_wpnonce' => 'invalidNonce',
];

ob_start();
Admin\render_settings_page();
ob_get_clean();

$this->assertSame( 0, did_action( 'wp101_flush_cache' ) );
$this->assertEmpty( get_settings_errors() );
}

/**
* Retrieve the WP101 menu node(s) visible for the current user.
*
Expand Down
89 changes: 51 additions & 38 deletions views/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,68 @@

<?php settings_errors(); ?>

<h2 id="api-key"><?php echo esc_html_x( 'WP101Plugin.com API Key', 'settings section heading', 'wp101' ); ?></h2>
<p><?php esc_html_e( 'Your API key enables your WordPress site to connect to WP101 and retrieve all of your videos.', 'wp101' ); ?></p>
<p><strong><?php esc_html_e( 'Don\'t have an API key?', 'wp101' ); ?></strong></p>
<p><a href="https://wp101plugin.com" class="button" target="_blank"><?php esc_html_e( 'Get your key now!', 'wp101' ); ?></a></p>
<section id="api-key">
<h2><?php echo esc_html_x( 'WP101Plugin.com API Key', 'settings section heading', 'wp101' ); ?></h2>
<p><?php esc_html_e( 'Your API key enables your WordPress site to connect to WP101 and retrieve all of your videos.', 'wp101' ); ?></p>
<p><strong><?php esc_html_e( 'Don\'t have an API key?', 'wp101' ); ?></strong></p>
<p><a href="https://wp101plugin.com" class="button" target="_blank"><?php esc_html_e( 'Get your key now!', 'wp101' ); ?></a></p>

<?php if ( defined( 'WP101_API_KEY' ) ) : ?>
<?php if ( defined( 'WP101_API_KEY' ) ) : ?>

<div id="wp101-api-key-set-via-constant-notice" class="notice notice-info">
<p><strong><?php esc_html_e( 'Your API key is defined in your wp-config.php file.', 'wp101' ); ?></strong></p>
<p><?php esc_html_e( 'To make changes, please open your wp-config.php file in a text editor and look for the line that includes:', 'wp101' ); ?></p>
<pre><code>define( 'WP101_API_KEY', '...' );</code></pre>
</div>
<div id="wp101-api-key-set-via-constant-notice" class="notice notice-info">
<p><strong><?php esc_html_e( 'Your API key is defined in your wp-config.php file.', 'wp101' ); ?></strong></p>
<p><?php esc_html_e( 'To make changes, please open your wp-config.php file in a text editor and look for the line that includes:', 'wp101' ); ?></p>
<pre><code>define( 'WP101_API_KEY', '...' );</code></pre>
</div>

<?php else : ?>
<?php else : ?>

<div id="wp101-settings-api-key-form" <?php echo ! empty( $api_key ) ? 'class="hide-if-js"' : ''; ?>>
<form method="post" action="options.php">
<?php settings_fields( 'wp101' ); ?>
<div id="wp101-settings-api-key-form" <?php echo ! empty( $api_key ) ? 'class="hide-if-js"' : ''; ?>>
<form method="post" action="options.php">
<?php settings_fields( 'wp101' ); ?>

<table class="form-table">
<th scope="row">
<label for="wp101-api-key"><?php esc_html_e( 'API key', 'wp101' ); ?></label>
</th>
<td>
<input name="wp101_api_key" id="wp101-api-key" type="text" class="regular-text code" />
</td>
</table>

<?php submit_button(); ?>
</form>
</div>

<?php endif; ?>

<?php if ( ! empty( $api_key ) ) : ?>

<div id="wp101-settings-api-key-display">
<table class="form-table">
<th scope="row">
<label for="wp101-api-key"><?php esc_html_e( 'API key', 'wp101' ); ?></label>
</th>
<td>
<input name="wp101_api_key" id="wp101-api-key" type="text" class="regular-text code" />
<code><?php echo esc_html( $masked ); ?></code>
<?php if ( ! defined( 'WP101_API_KEY' ) ) : ?>
<button id="wp101-settings-replace-api-key" class="button" style="vertical-align: baseline;"><?php esc_html_e( 'Replace my API Key', 'wp101' ); ?></button>
<?php endif; ?>
</td>
</table>
</div>

<?php endif; ?>
</section>

<section id="tools">
<h2><?php echo esc_html_x( 'Troubleshooting', 'settings section heading', 'wp101' ); ?></h2>

<form method="POST">
<p><?php esc_html_e( 'If you\'re running into issues viewing WP101 content (or recently-purchased content is not appearing), you may need to flush the WP101 cache:', 'wp101' ); ?></p>
<input type="submit" value="<?php esc_attr_e( 'Flush WP101 Cache', 'wp101' ); ?>" class="button button-primary" />
<?php wp_nonce_field( 'wp101-flush-cache' ); ?>
</form>

<?php submit_button(); ?>
</form>
</div>

<?php endif; ?>

<?php if ( ! empty( $api_key ) ) : ?>

<div id="wp101-settings-api-key-display">
<table class="form-table">
<th scope="row">
<label for="wp101-api-key"><?php esc_html_e( 'API key', 'wp101' ); ?></label>
</th>
<td>
<code><?php echo esc_html( $masked ); ?></code>
<?php if ( ! defined( 'WP101_API_KEY' ) ) : ?>
<button id="wp101-settings-replace-api-key" class="button" style="vertical-align: baseline;"><?php esc_html_e( 'Replace my API Key', 'wp101' ); ?></button>
<?php endif; ?>
</td>
</table>
</div>

<?php endif; ?>
</section>
</div>