| 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 |
|