PluginProbe
Force Refresh / 2.7.0
Force Refresh v2.7.0
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 2.12.1 All 53 releases
force-refresh / includes / functions.php

functions.php in Force Refresh 2.7.0, at includes/functions.php

140 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * All of our utility functions that are used throughout the plugin.
4 *
5 * @package ForceRefresh
6 */
7
8 namespace JordanLeven\Plugins\ForceRefresh;
9
10 /**
11 * Function for adding scripts for this plugin.
12 *
13 * @param string $handle The script handle.
14 *
15 * @param string $path The path to the script (relative to the JS dist directory).
16 *
17 * @param boolean $register Whether we should simply register the script instead of enqueuing it.
18 */
19 function add_script( $handle, $path, $register = false ) {
20 // Get the file path.
21 $file_path = get_force_refresh_plugin_directory() . $path;
22 // If the file doesn't exist, throw an error.
23 if ( ! file_exists( $file_path ) ) {
24 echo '<div class="notice notice-error">';
25 echo esc_html( "<p>${path} is missing.</p>" );
26 echo '</div>';
27 } else {
28 // Get the file version.
29 $file_version = filemtime( $file_path );
30 // If we want to only register the script.
31 if ( $register ) {
32 wp_register_script(
33 $handle,
34 get_force_refresh_plugin_url( $path ),
35 array(),
36 $file_version,
37 true
38 );
39 } else {
40 // Enqueue the style.
41 wp_enqueue_script(
42 $handle,
43 get_force_refresh_plugin_url( $path ),
44 array(),
45 $file_version,
46 true
47 );
48 }
49 }
50 }
51
52 /**
53 * Function for enqueueing styles for this plugin.
54 *
55 * @param string $handle The stylesheet handle.
56 * @param string $path The path to the stylesheet (relative to the CSS dist directory).
57 *
58 * @return void
59 */
60 function add_style( $handle, $path ) {
61 // Get the file path.
62 $file_path = get_force_refresh_plugin_directory() . $path;
63 // If the file doesn't exist, throw an error.
64 if ( ! file_exists( $file_path ) ) {
65 echo '<div class="notice notice-error">';
66 echo esc_html( "<p>${path} is missing.</p>" );
67 echo '</div>';
68 } else {
69 // Get the file version.
70 $file_version = filemtime( $file_path );
71 // Enqueue the style.
72 wp_enqueue_style( $handle, get_force_refresh_plugin_url( $path ), array(), $file_version );
73 }
74 }
75
76 /**
77 * Function to determine whether or not the currently logged-in user is able to request a refresh.
78 *
79 * @return bool true if the use is able to request a refresh
80 */
81 function user_can_request_force_refresh() {
82 return current_user_can( WP_FORCE_REFRESH_CAPABILITY );
83 }
84
85 /**
86 * Function for getting the directory for this plugin
87 *
88 * @return string The full directory this plugin is located in (including the plugin directory)
89 */
90 function get_force_refresh_plugin_directory() {
91 // Declare our plugin directory.
92 $plugin_directory = plugin_dir_path( get_main_plugin_file() );
93 return $plugin_directory;
94 }
95
96 /**
97 * Function for getting the URI for this plugin.
98 *
99 * @param string $file The optional file path you want to append to the urldecode(str).
100 *
101 * @return string The full url for the root of this plugin is located in (including the plugin
102 * directory)
103 */
104 function get_force_refresh_plugin_url( $file = null ) {
105 // Declare our plugin directory.
106 $plugin_url = plugins_url( $file, get_main_plugin_file() );
107 return $plugin_url;
108 }
109
110 /**
111 * Function to retrieve the current set refresh interval.
112 *
113 * @return int The set refresh interval
114 */
115 function get_force_refresh_option_refresh_interval(): int {
116 $refresh_interval = (string) get_option(
117 WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS,
118 WP_FORCE_REFRESH_OPTION_REFRESH_INTERVAL_IN_SECONDS_DEFAULT
119 );
120
121 return $refresh_interval;
122 }
123
124
125 /**
126 * Function to retrieve whether or not the refresh button should appear in the
127 * admin bar.
128 *
129 * @return bool True if we should show the refresh button in the admin bar
130 */
131 function get_force_refresh_option_show_in_admin_bar(): bool {
132 $show_in_admin_bar = get_option(
133 WP_FORCE_REFRESH_OPTION_SHOW_IN_WP_ADMIN_BAR,
134 'false'
135 );
136
137 return 'true' === $show_in_admin_bar;
138 }
139
140