| 1 |
<?php |
| 2 |
|
| 3 |
namespace JordanLeven\Plugins\ForceRefresh; |
| 4 |
|
| 5 |
/* |
| 6 |
Plugin Name: Force Refresh |
| 7 |
Plugin URI: |
| 8 |
Description: Force Refresh is a simple plugin that allows you to force a page refresh for users currently visiting your site. |
| 9 |
Version: 2.0.0 |
| 10 |
Author: Jordan Leven |
| 11 |
Author URI: https://github.com/jordanleven |
| 12 |
Contributors: |
| 13 |
*/ |
| 14 |
|
| 15 |
// Define the name of the action for the refresh. This is used with the nonce to create a unique action |
| 16 |
// when admins request a refresh. |
| 17 |
define("WP_FORCE_REFRESH_ACTION", "wp_force_refresh"); |
| 18 |
// Define the name of the capability used to invoke a refresh. This is used for developers who want to fine-tune |
| 19 |
// control of what types of users and roles can request a refresh. |
| 20 |
define("WP_FORCE_REFRESH_CAPABILITY", "invoke_force_refresh"); |
| 21 |
// Define the ID for the Handlebars admin notice. This is used to add notifications to the admin screen when a user requests a refresh. |
| 22 |
define("WP_FORCE_REFRESH_HANDLEBARS_ADMIN_NOTICE_TEMPLATE_ID", "invoke_force_refresh"); |
| 23 |
// Define the option for showing the Force Refresh button in the WordPress Admin Bar |
| 24 |
define("WP_FORCE_REFRESH_OPTION_SHOW_IN_WP_ADMIN_BAR", "force_refresh_show_in_wp_admin_bar"); |
| 25 |
// Define the option for the refresh interval (how often the site should check for a new version) |
| 26 |
define("WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS", "force_refresh_refresh_interval"); |
| 27 |
// Define the default refresh interval |
| 28 |
define("WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT", 120); |
| 29 |
|
| 30 |
// Make sure we include the composer autoload file |
| 31 |
require_once __DIR__ . "/library/vendor/autoload.php"; |
| 32 |
|
| 33 |
// Include the functions file |
| 34 |
require_once __DIR__ . "/library/custom/functions.php"; |
| 35 |
|
| 36 |
/** |
| 37 |
* Function for getting the directory for this plugin |
| 38 |
* |
| 39 |
* @return string The full directory this plugin is located in (including the plugin directory) |
| 40 |
* |
| 41 |
* @version 2.0 Introducted in version 2.0 |
| 42 |
*/ |
| 43 |
function get_force_refresh_plugin_directory(){ |
| 44 |
|
| 45 |
// Declare our plugin directory |
| 46 |
$plugin_directory = plugin_dir_path(__FILE__); |
| 47 |
|
| 48 |
return $plugin_directory; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Function for getting the uri for this plugin |
| 53 |
* |
| 54 |
* @param string $file The optional file path you want to append to the urldecode(str) |
| 55 |
* |
| 56 |
* @return string The full url for the root of this plugin is located in (including the plugin directory) |
| 57 |
* |
| 58 |
* @version 2.0 Introducted in version 2.0 |
| 59 |
*/ |
| 60 |
function get_force_refresh_plugin_url($file = null){ |
| 61 |
|
| 62 |
// Declare our plugin directory |
| 63 |
$plugin_url = plugins_url($file, __FILE__); |
| 64 |
|
| 65 |
return $plugin_url; |
| 66 |
} |
| 67 |
|
| 68 |
// Add the menu where we'll configure the settings |
| 69 |
add_action( 'admin_menu', function(){ |
| 70 |
|
| 71 |
add_submenu_page( |
| 72 |
'tools.php', |
| 73 |
'Force Refresh', |
| 74 |
'Force Refresh', |
| 75 |
WP_FORCE_REFRESH_CAPABILITY, |
| 76 |
'force_refresh', |
| 77 |
__NAMESPACE__ . '\\manage_force_refresh' |
| 78 |
); |
| 79 |
|
| 80 |
}); |
| 81 |
|
| 82 |
// Add the action to add the Force Refresh capability to allow developers to customize which users and roles |
| 83 |
// can init a Force Refresh |
| 84 |
add_action("admin_init", function(){ |
| 85 |
|
| 86 |
$role = get_role("administrator"); |
| 87 |
|
| 88 |
$role->add_cap(WP_FORCE_REFRESH_CAPABILITY); |
| 89 |
|
| 90 |
}); |
| 91 |
|
| 92 |
// Add the script for normal frontfacing pages (non-admin) |
| 93 |
add_action("wp_enqueue_scripts", function(){ |
| 94 |
|
| 95 |
// Include the normal JS |
| 96 |
add_script("force-refresh-js", "/library/dist/js/force-refresh.built.min.js", true); |
| 97 |
|
| 98 |
// Localize the admin ajax URL. This doesn't sound like the best idea but WP is into it (https://codex.wordpress.org/AJAX_in_Plugins) |
| 99 |
wp_localize_script( |
| 100 |
"force-refresh-js", |
| 101 |
"force_refresh_js_object", |
| 102 |
array( |
| 103 |
// Get the ajax URL |
| 104 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 105 |
// Get the post ID |
| 106 |
'post_id' => get_the_ID(), |
| 107 |
// Get the refresh interval |
| 108 |
'refresh_interval' => get_option( WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT ) |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
// Now that it's registered, enqueue the script |
| 113 |
wp_enqueue_script("force-refresh-js"); |
| 114 |
|
| 115 |
}); |
| 116 |
|
| 117 |
// Add meta boxes for specific pages that we want to refresh |
| 118 |
add_action('add_meta_boxes', function(){ |
| 119 |
|
| 120 |
// An array of all screens where we want to implement the meta box |
| 121 |
$screens = array("page", "post"); |
| 122 |
|
| 123 |
foreach ($screens as $screen) { |
| 124 |
// Add the box |
| 125 |
add_meta_box( |
| 126 |
'force_refresh_specific_page_refresh', |
| 127 |
'Force Refresh', |
| 128 |
__NAMESPACE__ . '\\force_refresh_specific_page_refresh_html', |
| 129 |
$screen, |
| 130 |
'side' |
| 131 |
); |
| 132 |
} |
| 133 |
}); |
| 134 |
|
| 135 |
?> |