Skip to content

Commit b4652f4

Browse files
committed
Added Clear Cache link (Ajax) to admin bar dropdown
1 parent 9e7c60a commit b4652f4

3 files changed

Lines changed: 105 additions & 7 deletions

File tree

app/Core.php

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
<?php
22
namespace VendorName\PluginName;
3+
//use WordPress_ToolKit\ObjectCache;
34

45
class Core extends Plugin {
56

67
function __construct() {
78

8-
// Add page, post type and parent classes to <body> tag for selector targeting
9+
// Example - Add page, post type and parent classes to <body> tag for selector targeting
910
add_filter( 'body_class', array( &$this, 'add_body_classes' ) );
1011

11-
// Remove Emoji code from header
12+
// Example - Remove Emoji code from header
1213
if( $this->get_plugin_option( 'remove_header_emojicons' ) ) {
1314
if(!$this->is_ajax()) add_filter( 'init', array( $this, 'disable_wp_emojicons' ) );
1415
}
1516

17+
/**
18+
* Example - Ajax call for when "Clear Cache" is clicked from the admin bar dropdown.
19+
*
20+
* Note: If this Ajax call was intended to be available to those who are not
21+
* logged in, you would need to uncommend the 'wp_ajax_nopriv_clear_object_cache_ajax'
22+
* hook.
23+
*/
24+
if( current_user_can( 'manage_options' ) && $this->get_plugin_option( 'admin_bar_add_clear_cache' ) ) {
25+
add_action( 'admin_bar_menu', array( $this, 'admin_bar_add_clear_cache' ), 900 );
26+
//add_action( 'wp_ajax_nopriv_clear_object_cache_ajax', array( self::$cache, 'flush' ) );
27+
add_action( 'wp_ajax_clear_object_cache_ajax', array( self::$cache, 'flush' ) );
28+
}
29+
1630
}
1731

1832
/**
@@ -60,4 +74,39 @@ public function disable_wp_emojicons() {
6074

6175
}
6276

77+
/**
78+
* Add "Clear Cache" link to admin bar dropdown
79+
* @since 0.3.0
80+
*/
81+
public function admin_bar_add_clear_cache( $wp_admin_bar ) {
82+
83+
$args = array(
84+
'id' => 'clear_object_cache',
85+
'title' => __( 'Clear Cache', self::$textdomain ),
86+
'parent' => 'site-name',
87+
'href' => '#'
88+
);
89+
$wp_admin_bar->add_node( $args );
90+
91+
}
92+
93+
/**
94+
* Ajax handler for 'clear_object_cache_ajax' call.
95+
* @since 0.3.0
96+
*/
97+
public function clear_object_cache_ajax() {
98+
99+
$result = ['success' => true];
100+
101+
try {
102+
self::$cache->flush();
103+
} catch (Exception $e) {
104+
$result = ['success' => false, 'message' => $e->getMessage()];
105+
}
106+
107+
echo json_encode( $result );
108+
wp_die();
109+
110+
}
111+
63112
}

app/EnqueueScripts.php

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ function __construct() {
77

88
// TODO: Put back font awesome
99

10-
// Enqueue frontend and backend scripts
10+
// Enqueue frontend/backend scripts and global JavaScript variables
11+
add_action( 'wp_head', array( $this, 'inject_javascript_settings' ) );
12+
add_action( 'admin_head', array( $this, 'inject_javascript_settings' ) );
1113
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts') );
1214
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts') );
1315

14-
// Load Font Awesome from CDN, if enabled in Settings Page
16+
// Example - Load Font Awesome from CDN, if enabled in Settings Page
1517
$enqueue_font_awesome = $this->get_plugin_option( 'enqueue_font_awesome' );
1618
if( $enqueue_font_awesome ) {
1719
if( in_array( 'frontend', $enqueue_font_awesome) )
@@ -24,32 +26,78 @@ function __construct() {
2426

2527
/**
2628
* Enqueue scripts used on frontend of site
29+
* @since 0.1.0
2730
*/
2831
public function enqueue_frontend_scripts() {
2932

33+
// Enqueue script dependencies
34+
$this->enqueue_vendor_scripts();
35+
3036
// Enqueuing custom CSS for child theme (Twentysixteen was used for testing)
31-
wp_enqueue_style( 'wordpress-base-plugin', $this->get_script_url('assets/css/site.css'), null, $this->get_script_version('assets/css/site.css') );
37+
wp_enqueue_style( 'wordpress-base-plugin', $this->get_script_url('assets/css/wordpress-base-plugin.css'), null, $this->get_script_version('assets/css/wordpress-base-plugin.css') );
38+
39+
// Enqueue frontend JavaScript
40+
wp_enqueue_script( 'wordpress-base-plugin', $this->get_script_url('assets/js/wordpress-base-plugin.js'), array('jquery', 'wordpress-base-plugin-vendor'), $this->get_script_version('assets/js/wordpress-base-plugin.js'), true );
41+
wp_localize_script( 'wordpress-base-plugin', 'wpbp_ajax_filter_params', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
3242

3343
}
3444

3545
/**
3646
* Enqueue scripts used in WP admin interface
47+
* @since 0.1.0
3748
*/
3849
public function enqueue_admin_scripts() {
3950

40-
wp_enqueue_script( 'wordpress-base-plugin', $this->get_script_url('assets/js/wordpress-base-plugin-admin.js'), array('jquery'), $this->get_script_version('assets/js/wordpress-base-plugin-admin.js') );
51+
// Enqueue script dependencies
52+
$this->enqueue_vendor_scripts();
53+
54+
// Enqueuing custom CSS for child theme (Twentysixteen was used for testing)
55+
wp_enqueue_style( 'wordpress-base-plugin', $this->get_script_url('assets/css/wordpress-base-plugin-admin.css'), null, $this->get_script_version('assets/css/wordpress-base-plugin-admin.css') );
56+
57+
// Enqueue WP Admin JavaScript
58+
wp_enqueue_script( 'wordpress-base-plugin-admin', $this->get_script_url('assets/js/wordpress-base-plugin-admin.js'), array('jquery', 'wordpress-base-plugin-vendor'), $this->get_script_version('assets/js/wordpress-base-plugin-admin.js'), true );
59+
wp_localize_script( 'wordpress-base-plugin-admin', 'wpbp_ajax_filter_params', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
60+
61+
}
62+
63+
/**
64+
* Enqueue vendor scripts compiled from src/js/vendor
65+
* @since 0.3.0
66+
*/
67+
private function enqueue_vendor_scripts() {
68+
69+
// Enqueue common (frontend/backend) JavaScript
70+
wp_enqueue_script( 'wordpress-base-plugin-vendor', $this->get_script_url('assets/js/wordpress-base-plugin-vendor.js'), array('jquery'), $this->get_script_version('assets/js/wordpress-base-plugin-vendor.js'), true );
4171

4272
}
4373

4474
/**
4575
* Enqueue Font Awesome
76+
* @since 0.1.0
4677
*/
4778
public function enqueue_font_awesome() {
4879

4980
wp_enqueue_style( 'font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css', null, '4.7.0' );
5081

5182
}
5283

84+
/**
85+
* Add global JavaScript settings variables. You can add any variables/settings
86+
* that you want to make available to your JavaScripts
87+
* @since 0.3.0
88+
*/
89+
public function inject_javascript_settings() {
90+
91+
$javascript_variables = array(
92+
'admin_bar_add_clear_cache' => $this->get_plugin_option( 'admin_bar_add_clear_cache' ),
93+
'admin_bar_add_clear_cache_success' => __( 'WordPress cache has been cleared.', self::$textdomain ),
94+
'show_clear_cache_link' => current_user_can( 'manage_options' )
95+
);
96+
97+
echo "<script>var _wpbp_plugin_settings = JSON.parse('" . json_encode( $javascript_variables ) . "');</script>";
98+
99+
}
100+
53101
/**
54102
* Returns script ?ver= version based on environment (WP_ENV)
55103
*
@@ -93,7 +141,7 @@ public function get_script_version($script, $return_minified = false, $script_ve
93141
* @return string The URL or path to minified or regular $script.
94142
* @since 0.1.0
95143
*/
96-
public function get_script_path($script, $return_minified = false, $return_url = false) {
144+
public function get_script_path($script, $return_minified = true, $return_url = false) {
97145
$script = trim($script, '/');
98146
if($return_minified && strpos($script, '.') && $this->is_production()) {
99147
$script_parts = explode('.', $script);

app/Settings/Carbon_Page.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ private function create_tabbed_options_page() {
4444
->set_page_parent('options-general.php')
4545
->add_tab( __('General', self::$textdomain), array(
4646
Field::make('checkbox', $this->prefix('uninstall_remove_settings'), __('Delete Plugin Settings On Uninstall', self::$textdomain) ),
47+
Field::make('checkbox', $this->prefix('admin_bar_add_clear_cache'), __('Add "Clear Cache" Link to Admin Bar Dropdown Menu', self::$textdomain) )->set_default_value(true),
4748
Field::make('checkbox', $this->prefix('remove_header_emojicons'), __('Remove Emoji Code From Page Headers', self::$textdomain) )
4849
->help_text( __('Checking this box will remove the default Emoji code from page headers.', self::$textdomain) ),
4950
Field::make( 'set', $this->prefix('enqueue_font_awesome'), __('Load Font Awesome from CDN', self::$textdomain) )

0 commit comments

Comments
 (0)