| 1 |
<?php |
| 2 |
|
| 3 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 4 |
|
| 5 |
// Include the handlebars function |
| 6 |
require_once __DIR__ . "/function-handlebars.php"; |
| 7 |
|
| 8 |
// All ajax calls from browsers |
| 9 |
require_once __DIR__ . "/ajax-calls-client.php"; |
| 10 |
|
| 11 |
// All ajax calls from admins |
| 12 |
require_once __DIR__ . "/ajax-calls-admin.php"; |
| 13 |
|
| 14 |
function force_refresh_specific_page_refresh_html() { |
| 15 |
define('HTML_CLASS_META_BOX', 'force-refresh-meta-box'); |
| 16 |
define('FILE_NAME_META_BOX_ADMIN', 'force-refresh-meta-box-admin-js'); |
| 17 |
// Include the admin JS |
| 18 |
add_script( FILE_NAME_META_BOX_ADMIN, '/dist/js/force-refresh-meta-box-admin.js', true ); |
| 19 |
// Get the current screen |
| 20 |
$current_screen = get_current_screen(); |
| 21 |
// Create the data we're going to localize to the script |
| 22 |
$localized_data = array( |
| 23 |
// Wrap in inner array to preserve primitive types |
| 24 |
'localData' => array( |
| 25 |
// Add the API URL for the script |
| 26 |
'apiUrl' => get_stylesheet_directory_uri(), |
| 27 |
// Add the API URL for the script |
| 28 |
'siteId' => get_current_blog_id(), |
| 29 |
// Create a nonce for the user |
| 30 |
'nonce' => wp_create_nonce(WP_FORCE_REFRESH_ACTION), |
| 31 |
// Create a nonce for the user |
| 32 |
'postId' => get_the_ID(), |
| 33 |
'postType' => $current_screen->post_type, |
| 34 |
'postName' => get_the_title(), |
| 35 |
'targetClass' => HTML_CLASS_META_BOX, |
| 36 |
// Add the refresh interval |
| 37 |
'refreshInterval' => get_option(WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT), |
| 38 |
) |
| 39 |
); |
| 40 |
// Localize the data |
| 41 |
wp_localize_script( |
| 42 |
FILE_NAME_META_BOX_ADMIN, |
| 43 |
"forceRefreshLocalJs", |
| 44 |
$localized_data |
| 45 |
); |
| 46 |
// Now that it's registered, enqueue the script |
| 47 |
wp_enqueue_script(FILE_NAME_META_BOX_ADMIN); |
| 48 |
echo "<div class=\"" . HTML_CLASS_META_BOX . "\"></div>"; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Hook used to enqueue CSS and JS, as well as add the optional Force Refresh button to the WordPress Admin Bar. |
| 53 |
*/ |
| 54 |
add_action('admin_head', function(){ |
| 55 |
// Get this option from the database. Default value is null/false |
| 56 |
$show_force_refresh_in_wp_admin_bar = get_option(WP_FORCE_REFRESH_OPTION_SHOW_IN_WP_ADMIN_BAR) === 'true'; |
| 57 |
// If the option to show Force Refresh in the admin bar is enabled, then we need to show the item in the WordPress Admin Bar |
| 58 |
if ( $show_force_refresh_in_wp_admin_bar ){ |
| 59 |
// Show the Force Refresh option in the WordPress Admin Bar |
| 60 |
add_action( 'wp_before_admin_bar_render', __NAMESPACE__ . '\\show_force_refresh_in_wp_admin_bar', 999 ); |
| 61 |
} |
| 62 |
// Since a Force Refresh can take place from any page, we also need to add the Handlebars template for a notice |
| 63 |
add_handlebars( WP_FORCE_REFRESH_HANDLEBARS_ADMIN_NOTICE_TEMPLATE_ID, 'force-refresh-main-admin-notice.handlebars' ); |
| 64 |
// Include the admin CSS |
| 65 |
add_style("force-refresh-admin-css", "/dist/css/force-refresh-admin.css"); |
| 66 |
// Add the Force Refresh script |
| 67 |
add_force_refresh_script(); |
| 68 |
|
| 69 |
}); |
| 70 |
|
| 71 |
/** |
| 72 |
* Hook used to save admin actions for Force Refresh. |
| 73 |
*/ |
| 74 |
add_action('admin_init', function(){ |
| 75 |
// If we are saving data from the Force Refresh options |
| 76 |
if (isset($_POST['force-refresh-admin-options-save']) && $_POST['force-refresh-admin-options-save'] == true){ |
| 77 |
// Get updated options for viewing the refresh button in the WP Admin bar |
| 78 |
$show_in_admin_bar = isset($_POST['show-in-wp-admin-bar']) ? $_POST['show-in-wp-admin-bar'] : false; |
| 79 |
// Update the show in Admin Bar option |
| 80 |
update_option(WP_FORCE_REFRESH_OPTION_SHOW_IN_WP_ADMIN_BAR, $show_in_admin_bar); |
| 81 |
// Get updated options for the refresh interval |
| 82 |
$refresh_interval = isset($_POST['refresh-interval']) ? $_POST['refresh-interval'] : null; |
| 83 |
// If the new refresh interval option came through all right |
| 84 |
if ($refresh_interval){ |
| 85 |
// Update the refresh interval |
| 86 |
update_option(WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, $refresh_interval); |
| 87 |
} |
| 88 |
} |
| 89 |
}); |
| 90 |
|
| 91 |
/** |
| 92 |
* Function to show the Force Refresh option in the WP Admin bar. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
* |
| 96 |
* @version 1.0 Added in version 2.0 |
| 97 |
*/ |
| 98 |
function show_force_refresh_in_wp_admin_bar(){ |
| 99 |
// Globalize the WP Admin Bar object |
| 100 |
global $wp_admin_bar; |
| 101 |
// Add the item to show up in the WP Admin Bar |
| 102 |
$args = array( |
| 103 |
'id' => 'force-refresh', |
| 104 |
'title' => "<i class=\"fa fa-refresh\" aria-hidden=\"true\"></i> <span>Force Refresh Site</span>", |
| 105 |
'href' => null, |
| 106 |
); |
| 107 |
// Add the menu |
| 108 |
$wp_admin_bar->add_menu( $args ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Main function to manage settings for Force Refresh. |
| 113 |
* |
| 114 |
* @return void |
| 115 |
* |
| 116 |
* @version 1.0 Introducted in version 1.0 |
| 117 |
*/ |
| 118 |
function manage_force_refresh(){ |
| 119 |
// See what the existing settings are for showing Force Refresh in the WordPress Admin bar |
| 120 |
$preset_option_show_force_refresh_in_wp_admin_bar = get_option(WP_FORCE_REFRESH_OPTION_SHOW_IN_WP_ADMIN_BAR, false); |
| 121 |
// See what the existing settings are for the refresh interval |
| 122 |
$preset_option_refresh_interval = get_option(WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT); |
| 123 |
// Add the script |
| 124 |
add_force_refresh_script(); |
| 125 |
// Render the HTML |
| 126 |
render_handlebars("force-refresh-main-admin.handlebars", |
| 127 |
array( |
| 128 |
"site_name" => get_bloginfo(), |
| 129 |
"options" => array( |
| 130 |
// For the Show Force Refresh in Admin Menu option |
| 131 |
"preset_value_force_refresh_in_admin_bar_show" => $preset_option_show_force_refresh_in_wp_admin_bar === 'true' ? "selected" : null, |
| 132 |
"preset_value_force_refresh_in_admin_bar_hide" => $preset_option_show_force_refresh_in_wp_admin_bar === 'false' ? "selected" : null, |
| 133 |
|
| 134 |
// For the refresh interval option |
| 135 |
"preset_value_force_refresh_refresh_interval_30" => $preset_option_refresh_interval === '30' ? "selected" : null, |
| 136 |
"preset_value_force_refresh_refresh_interval_60" => $preset_option_refresh_interval === '60' ? "selected" : null, |
| 137 |
"preset_value_force_refresh_refresh_interval_90" => $preset_option_refresh_interval === '90' ? "selected" : null, |
| 138 |
"preset_value_force_refresh_refresh_interval_120" => $preset_option_refresh_interval === '120' ? "selected" : null |
| 139 |
) |
| 140 |
) |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Function to add the Force Refresh script, which contains the nonce required to initiate the call. |
| 146 |
* |
| 147 |
* @version 1.0 Introducted in version 2.0 |
| 148 |
*/ |
| 149 |
function add_force_refresh_script(){ |
| 150 |
|
| 151 |
// Include the admin JS |
| 152 |
add_script("force-refresh-main-admin-js", "/dist/js/force-refresh-main-admin.js", true); |
| 153 |
|
| 154 |
// Create the data we're going to localize to the script |
| 155 |
$localized_data = array(); |
| 156 |
|
| 157 |
// Add the API URL for the script |
| 158 |
$localized_data['api_url'] = get_stylesheet_directory_uri(); |
| 159 |
|
| 160 |
// Add the API URL for the script |
| 161 |
$localized_data['site_id'] = get_current_blog_id(); |
| 162 |
|
| 163 |
// Create a nonce for the user |
| 164 |
$localized_data['nonce'] = wp_create_nonce(WP_FORCE_REFRESH_ACTION); |
| 165 |
|
| 166 |
// Add the ID of the handlebars notice |
| 167 |
$localized_data['handlebars_admin_notice_template_id'] = WP_FORCE_REFRESH_HANDLEBARS_ADMIN_NOTICE_TEMPLATE_ID; |
| 168 |
|
| 169 |
// Add the refresh interval |
| 170 |
$localized_data['refresh_interval'] = get_option(WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT); |
| 171 |
|
| 172 |
// Localize the data |
| 173 |
wp_localize_script( |
| 174 |
"force-refresh-main-admin-js", |
| 175 |
"force_refresh_local_js", |
| 176 |
$localized_data |
| 177 |
); |
| 178 |
|
| 179 |
// Now that it's registered, enqueue the script |
| 180 |
wp_enqueue_script("force-refresh-main-admin-js"); |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Function for enqueueing styles for this plugin. |
| 186 |
* |
| 187 |
* @param string $handle The stylesheet handle |
| 188 |
* @param string $path The path to the stylesheet (relative to the CSS dist directory) |
| 189 |
* |
| 190 |
* @return void |
| 191 |
* |
| 192 |
* @version 1.0 Introducted in version 1.0 |
| 193 |
*/ |
| 194 |
function add_style($handle, $path){ |
| 195 |
|
| 196 |
// Get the file path |
| 197 |
$file_path = get_force_refresh_plugin_directory() . $path; |
| 198 |
|
| 199 |
// If the file doesn't exist, throw an error |
| 200 |
if (!file_exists($file_path)){ |
| 201 |
|
| 202 |
echo "<div class=\"notice notice-error\">"; |
| 203 |
echo "<p>$path is missing.</p>"; |
| 204 |
echo "</div>"; |
| 205 |
|
| 206 |
// Log error to server |
| 207 |
error_log("$path is missing."); |
| 208 |
} |
| 209 |
|
| 210 |
// Otherwise, work normally |
| 211 |
else { |
| 212 |
|
| 213 |
// Get the file version |
| 214 |
$file_version = filemtime($file_path); |
| 215 |
|
| 216 |
// Enqueue the style |
| 217 |
wp_enqueue_style($handle, get_force_refresh_plugin_url($path), array(), $file_version); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Function for adding scripts for this plugin. |
| 223 |
* |
| 224 |
* @param string $handle The script handle |
| 225 |
* @param string $path The path to the script (relative to the JS dist directory) |
| 226 |
* @param boolean $register Whether we should simply register the script instead of enqueing it |
| 227 |
* |
| 228 |
* @return void |
| 229 |
* |
| 230 |
* @version 1.0 Introducted in version 1.0 |
| 231 |
*/ |
| 232 |
function add_script($handle, $path, $register = false){ |
| 233 |
|
| 234 |
// Get the file path |
| 235 |
$file_path = get_force_refresh_plugin_directory() . $path; |
| 236 |
|
| 237 |
// If the file doesn't exist, throw an error |
| 238 |
if (!file_exists($file_path)){ |
| 239 |
|
| 240 |
echo "<div class=\"notice notice-error\">"; |
| 241 |
echo "<p>$path is missing.</p>"; |
| 242 |
echo "</div>"; |
| 243 |
|
| 244 |
} |
| 245 |
// Otherwise, work normally |
| 246 |
else { |
| 247 |
|
| 248 |
// Get the file version |
| 249 |
$file_version = filemtime($file_path); |
| 250 |
|
| 251 |
// If we want to only register the script |
| 252 |
if ($register){ |
| 253 |
|
| 254 |
wp_register_script($handle, get_force_refresh_plugin_url($path), array("jquery", "jquery-ui-core"), $file_version); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
// Otherwise, we want to enqueue the script |
| 259 |
else { |
| 260 |
|
| 261 |
// Enqueue the style |
| 262 |
wp_enqueue_script($handle, get_force_refresh_plugin_url($path), array("jquery", "jquery-ui-core"), $file_version); |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
?> |