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