| 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.1.2 |
| 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 |
// All the post types to exclude force refreshing from |
| 30 |
define( 'WP_FORCE_REFRESH_EXCLUDE_FROM_POST_TYPES', array( 'attachment' ) ); |
| 31 |
|
| 32 |
// Make sure we include the composer autoload file |
| 33 |
require_once __DIR__ . "/vendor/autoload.php"; |
| 34 |
|
| 35 |
// Include the functions file |
| 36 |
require_once __DIR__ . "/includes/functions.php"; |
| 37 |
|
| 38 |
/** |
| 39 |
* Function for getting the directory for this plugin |
| 40 |
* |
| 41 |
* @return string The full directory this plugin is located in (including the plugin directory) |
| 42 |
* |
| 43 |
* @version 2.0 Introducted in version 2.0 |
| 44 |
*/ |
| 45 |
function get_force_refresh_plugin_directory(){ |
| 46 |
|
| 47 |
// Declare our plugin directory |
| 48 |
$plugin_directory = plugin_dir_path(__FILE__); |
| 49 |
|
| 50 |
return $plugin_directory; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Function for getting the uri for this plugin |
| 55 |
* |
| 56 |
* @param string $file The optional file path you want to append to the urldecode(str) |
| 57 |
* |
| 58 |
* @return string The full url for the root of this plugin is located in (including the plugin directory) |
| 59 |
* |
| 60 |
* @version 2.0 Introducted in version 2.0 |
| 61 |
*/ |
| 62 |
function get_force_refresh_plugin_url($file = null){ |
| 63 |
|
| 64 |
// Declare our plugin directory |
| 65 |
$plugin_url = plugins_url($file, __FILE__); |
| 66 |
|
| 67 |
return $plugin_url; |
| 68 |
} |
| 69 |
|
| 70 |
// Add the menu where we'll configure the settings |
| 71 |
add_action( 'admin_menu', function(){ |
| 72 |
|
| 73 |
add_submenu_page( |
| 74 |
'tools.php', |
| 75 |
'Force Refresh', |
| 76 |
'Force Refresh', |
| 77 |
WP_FORCE_REFRESH_CAPABILITY, |
| 78 |
'force_refresh', |
| 79 |
__NAMESPACE__ . '\\manage_force_refresh' |
| 80 |
); |
| 81 |
|
| 82 |
}); |
| 83 |
|
| 84 |
// Add the action to add the Force Refresh capability to allow developers to customize which users and roles |
| 85 |
// can init a Force Refresh |
| 86 |
add_action("admin_init", function(){ |
| 87 |
|
| 88 |
$role = get_role("administrator"); |
| 89 |
|
| 90 |
$role->add_cap(WP_FORCE_REFRESH_CAPABILITY); |
| 91 |
|
| 92 |
}); |
| 93 |
|
| 94 |
// Add the script for normal frontfacing pages (non-admin) |
| 95 |
add_action("wp_enqueue_scripts", function(){ |
| 96 |
|
| 97 |
// Include the normal JS |
| 98 |
add_script("force-refresh-js", "/dist/js/force-refresh.js", true); |
| 99 |
|
| 100 |
// 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) |
| 101 |
wp_localize_script( |
| 102 |
"force-refresh-js", |
| 103 |
"force_refresh_js_object", |
| 104 |
array( |
| 105 |
// Get the ajax URL |
| 106 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 107 |
// Get the post ID |
| 108 |
'post_id' => get_the_ID(), |
| 109 |
// Get the refresh interval |
| 110 |
'refresh_interval' => get_option( WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS, WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT ) |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
// Now that it's registered, enqueue the script |
| 115 |
wp_enqueue_script("force-refresh-js"); |
| 116 |
|
| 117 |
}); |
| 118 |
|
| 119 |
// Add meta boxes for specific pages that we want to refresh |
| 120 |
add_action('add_meta_boxes', function(){ |
| 121 |
// All post types |
| 122 |
$all_post_types = get_post_types(); |
| 123 |
// Loop through all the post types |
| 124 |
foreach ($all_post_types as $post_type => $post_key) { |
| 125 |
// Get the post type attributes |
| 126 |
$post_type_attributes = get_post_type_object( $post_type ); |
| 127 |
// The post types public attribute |
| 128 |
$post_type_is_public = $post_type_attributes->public; |
| 129 |
// Only add the box if the post type is public and we're not excluding |
| 130 |
// the post type |
| 131 |
if ( $post_type_is_public && !in_array($post_type, WP_FORCE_REFRESH_EXCLUDE_FROM_POST_TYPES )){ |
| 132 |
// Add the box |
| 133 |
add_meta_box( |
| 134 |
'force_refresh_specific_page_refresh', |
| 135 |
'Force Refresh', |
| 136 |
__NAMESPACE__ . '\\force_refresh_specific_page_refresh_html', |
| 137 |
$post_type, |
| 138 |
'side' |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
}); |
| 143 |
|
| 144 |
?> |