Skip to content

Commit ac2d95d

Browse files
committed
Updated JS injection to use wordpress-toolkit
1 parent 93e4390 commit ac2d95d

3 files changed

Lines changed: 21 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,9 @@ See the [Getting Started](https://github.com/dmhendricks/wordpress-base-plugin/w
7373
## Future Plans
7474

7575
* Add Customizer example
76-
* Add dynamically-created CSS/JS files based on settings
77-
* Refactor the Plugin class for better organization and self-documentation
78-
* Create decent documentation and perform cleanup on the [wordpress-toolkit](https://github.com/dmhendricks/wordpress-toolkit) dependency
76+
* Refactor the Plugin base class for better organization and self-documentation
7977
* Add [phpdotenv](https://github.com/etelford/phpdotenv) support
80-
* Add encrypted text field example
81-
* Add some useful Carbon Fields CSS classes
78+
* Add some Carbon Fields CSS helper classes
8279

8380
## Change Log
8481

app/EnqueueScripts.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22
namespace VendorName\PluginName;
3+
use WordPress_ToolKit\ScriptObject;
34

45
class EnqueueScripts extends Plugin {
56

67
function __construct() {
78

8-
// Enqueue frontend/backend scripts and global JavaScript variables
9-
add_action( 'wp_head', array( $this, 'inject_javascript_settings' ) );
10-
add_action( 'admin_head', array( $this, 'inject_javascript_settings' ) );
9+
// Enqueue frontend/backend scripts
1110
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts') );
1211
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts') );
1312

13+
// Inject plugin settings into page head
14+
$this->inject_javascript_settings();
15+
1416
// 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 ) {
@@ -80,19 +82,25 @@ public function enqueue_font_awesome() {
8082
}
8183

8284
/**
83-
* Add global JavaScript settings variables. You can add any variables/settings
84-
* that you want to make available to your JavaScripts
85+
* Inject JavaScript settings into header. You can add any variables/settings
86+
* that you want to make available to your JavaScripts.
8587
* @since 0.3.0
8688
*/
87-
public function inject_javascript_settings() {
89+
private function inject_javascript_settings() {
90+
91+
$args = array(
92+
'variable_name' => $this->prefix( 'plugin_settings', '_' ),
93+
'target' => ['wp', 'admin']
94+
);
8895

89-
$javascript_variables = array(
96+
$values = array(
9097
'admin_bar_add_clear_cache' => $this->get_plugin_option( 'admin_bar_add_clear_cache' ),
9198
'admin_bar_add_clear_cache_success' => __( 'WordPress cache has been cleared.', self::$textdomain ),
92-
'show_clear_cache_link' => current_user_can( 'manage_options' )
99+
'show_clear_cache_link' => current_user_can( 'manage_options' ),
93100
);
94101

95-
echo "<script>var _wpbp_plugin_settings = JSON.parse('" . json_encode( $javascript_variables ) . "');</script>";
102+
$js = new \WordPress_ToolKit\ScriptObject( $values );
103+
$js->injectJS( $args );
96104

97105
}
98106

src/js/common/common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
success: function(result)
2121
{
22-
alert( result.success ? _wpbp_plugin_settings['admin_bar_add_clear_cache_success'] : 'Error: ' + result.message );
22+
alert( result.success ? _myplugin_plugin_settings['admin_bar_add_clear_cache_success'] : 'Error: ' + result.message );
2323
}
2424
});
2525

@@ -28,7 +28,7 @@
2828
}
2929

3030
// Bind event to clear theme cache Admin Bar link
31-
if( typeof _wpbp_plugin_settings !== 'undefined' && _wpbp_plugin_settings['show_clear_cache_link'] && _wpbp_plugin_settings['admin_bar_add_clear_cache'] ) {
31+
if( typeof _myplugin_plugin_settings !== 'undefined' && _myplugin_plugin_settings['show_clear_cache_link'] && _myplugin_plugin_settings['admin_bar_add_clear_cache'] ) {
3232
$( '#wpadminbar' ).waitUntilExists(function() {
3333
$('#wp-admin-bar-clear_object_cache').on( 'click', function( event ) {
3434
event.preventDefault();

0 commit comments

Comments
 (0)