PluginProbe
Force Refresh / 3.2.1
Force Refresh v3.2.1
3.2.1 3.2.0 3.1.2 3.1.1 3.1.0 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.2.0 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.11.0 2.11.1 2.12.0 All 54 releases
← All changes | includes/functions.php +110 -223 2.1.13.2.1 View file →
@@ -1,267 +1,154 @@
1 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 2 /**
52 - * Hook used to enqueue CSS and JS, as well as add the optional Force Refresh button to the WordPress Admin Bar.
3 + * All of our utility functions that are used throughout the plugin.
4 + *
5 + * @package ForceRefresh
53 6 */
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 7
69 -});
8 +namespace JordanLeven\Plugins\ForceRefresh;
70 9
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 -});
10 +use JordanLeven\Plugins\ForceRefresh\Services\Versions_Storage_Service;
90 11
91 12 /**
92 - * Function to show the Force Refresh option in the WP Admin bar.
13 + * Function to print out an error message to the user
93 14 *
94 - * @return void
15 + * @param string $message The message to display.
95 16 *
96 - * @version 1.0 Added in version 2.0
17 + * @return void
97 18 */
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 );
19 +function print_error( string $message ): void {
20 + printf( '<div class="notice notice-error">%s</div>', esc_html( $message ) );
109 21 }
110 22
111 23 /**
112 - * Main function to manage settings for Force Refresh.
24 + * Function for adding scripts for this plugin.
113 25 *
114 - * @return void
26 + * @param string $handle The script handle.
115 27 *
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.
28 + * @param string $path The path to the script (relative to the JS dist directory).
146 29 *
147 - * @version 1.0 Introducted in version 2.0
30 + * @param boolean $register Whether we should simply register the script instead of enqueuing it.
148 31 */
149 -function add_force_refresh_script(){
32 +function add_script( $handle, $path, $register = false ) {
33 + // Get the file path.
34 + $file_path = get_force_refresh_plugin_directory() . $path;
35 + // If the file doesn't exist, throw an error.
36 + if ( ! file_exists( $file_path ) ) {
37 + print_error( "{$path} is missing." );
38 + return;
39 + }
150 40
151 - // Include the admin JS
152 - add_script("force-refresh-main-admin-js", "/dist/js/force-refresh-main-admin.js", true);
41 + // Get the file version.
42 + $file_version = filemtime( $file_path );
43 + // If we want to only register the script.
44 + if ( $register ) {
45 + wp_register_script(
46 + $handle,
47 + get_force_refresh_plugin_url( $path ),
48 + array(),
49 + $file_version,
50 + true
51 + );
153 52
154 - // Create the data we're going to localize to the script
155 - $localized_data = array();
53 + return;
54 + }
156 55
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
56 + // Enqueue the style.
57 + wp_enqueue_script(
58 + $handle,
59 + get_force_refresh_plugin_url( $path ),
60 + array(),
61 + $file_version,
62 + true
177 63 );
178 -
179 - // Now that it's registered, enqueue the script
180 - wp_enqueue_script("force-refresh-main-admin-js");
181 -
182 64 }
183 65
184 66 /**
185 67 * Function for enqueueing styles for this plugin.
186 68 *
187 - * @param string $handle The stylesheet handle
188 - * @param string $path The path to the stylesheet (relative to the CSS dist directory)
69 + * @param string $handle The stylesheet handle.
70 + * @param string $path The path to the stylesheet (relative to the CSS dist directory).
189 71 *
190 - * @return void
191 - *
192 - * @version 1.0 Introducted in version 1.0
72 + * @return void
193 73 */
194 -function add_style($handle, $path){
195 -
196 - // Get the file path
74 +function add_style( $handle, $path ) {
75 + // Get the file path.
197 76 $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.");
77 + // If the file doesn't exist, throw an error.
78 + if ( ! file_exists( $file_path ) ) {
79 + print_error( "{$path} is missing." );
80 + return;
208 81 }
209 82
210 - // Otherwise, work normally
211 - else {
83 + // Get the file version.
84 + $file_version = filemtime( $file_path );
85 + // Enqueue the style.
86 + wp_enqueue_style( $handle, get_force_refresh_plugin_url( $path ), array(), $file_version );
87 +}
212 88
213 - // Get the file version
214 - $file_version = filemtime($file_path);
89 +/**
90 + * Function to determine whether or not the currently logged-in user is able to request a refresh.
91 + *
92 + * @return bool true if the use is able to request a refresh
93 + */
94 +function user_can_request_force_refresh() {
95 + return current_user_can( WP_FORCE_REFRESH_CAPABILITY );
96 +}
215 97
216 - // Enqueue the style
217 - wp_enqueue_style($handle, get_force_refresh_plugin_url($path), array(), $file_version);
218 - }
98 +/**
99 + * Function for getting the directory for this plugin
100 + *
101 + * @return string The full directory this plugin is located in (including the plugin directory)
102 + */
103 +function get_force_refresh_plugin_directory() {
104 + // Declare our plugin directory.
105 + $plugin_directory = plugin_dir_path( get_main_plugin_file() );
106 + return $plugin_directory;
219 107 }
220 108
221 109 /**
222 - * Function for adding scripts for this plugin.
110 + * Function for getting the URI for this plugin.
223 111 *
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
112 + * @param string $file The optional file path you want to append to the urldecode(str).
227 113 *
228 - * @return void
114 + * @return string The full url for the root of this plugin is located in (including the plugin
115 + * directory)
116 + */
117 +function get_force_refresh_plugin_url( $file = null ) {
118 + // Declare our plugin directory.
119 + $plugin_url = plugins_url( $file, get_main_plugin_file() );
120 + return $plugin_url;
121 +}
122 +
123 +/**
124 + * Gets the current plugin data from the main plugin file.
229 125 *
230 - * @version 1.0 Introducted in version 1.0
126 + * @return array The plugin data.
231 127 */
232 -function add_script($handle, $path, $register = false){
128 +function get_force_refresh_plugin_data(): array {
129 + require_once ABSPATH . 'wp-admin/includes/plugin.php';
130 + return get_plugin_data( get_main_plugin_file() );
131 +}
233 132
234 - // Get the file path
235 - $file_path = get_force_refresh_plugin_directory() . $path;
133 +/**
134 + * Get the current WordPress version.
135 + *
136 + * @return string
137 + */
138 +function get_wordpress_version(): string {
139 + return (string) get_bloginfo( 'version' );
140 +}
236 141
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 -
142 +/**
143 + * Function to conditionally log data if WP_DEBUG is set.
144 + *
145 + * @param mixed $log The data to log.
146 + */
147 +function logger( $log ): void {
148 + if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
149 + // phpcs:disable WordPress.PHP.DevelopmentFunctions
150 + $log_formatted = sprintf( 'Force Refresh - %s', print_r( $log, true ) );
151 + error_log( $log_formatted );
152 + // phpcs:enable
244 153 }
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 154 }
266 -
267 -?>