PluginProbe
Force Refresh / 2.9.1
Force Refresh v2.9.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
force-refresh / includes / functions.php

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

127 lines 3.8 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 use JordanLeven\Plugins\ForceRefresh\Services\Versions_Storage_Service;
11
12 /**
13 * Function for adding scripts for this plugin.
14 *
15 * @param string $handle The script handle.
16 *
17 * @param string $path The path to the script (relative to the JS dist directory).
18 *
19 * @param boolean $register Whether we should simply register the script instead of enqueuing it.
20 */
21 function add_script( $handle, $path, $register = false ) {
22 // Get the file path.
23 $file_path = get_force_refresh_plugin_directory() . $path;
24 // If the file doesn't exist, throw an error.
25 if ( ! file_exists( $file_path ) ) {
26 echo '<div class="notice notice-error">';
27 echo esc_html( "<p>${path} is missing.</p>" );
28 echo '</div>';
29 } else {
30 // Get the file version.
31 $file_version = filemtime( $file_path );
32 // If we want to only register the script.
33 if ( $register ) {
34 wp_register_script(
35 $handle,
36 get_force_refresh_plugin_url( $path ),
37 array(),
38 $file_version,
39 true
40 );
41 } else {
42 // Enqueue the style.
43 wp_enqueue_script(
44 $handle,
45 get_force_refresh_plugin_url( $path ),
46 array(),
47 $file_version,
48 true
49 );
50 }
51 }
52 }
53
54 /**
55 * Function for enqueueing styles for this plugin.
56 *
57 * @param string $handle The stylesheet handle.
58 * @param string $path The path to the stylesheet (relative to the CSS dist directory).
59 *
60 * @return void
61 */
62 function add_style( $handle, $path ) {
63 // Get the file path.
64 $file_path = get_force_refresh_plugin_directory() . $path;
65 // If the file doesn't exist, throw an error.
66 if ( ! file_exists( $file_path ) ) {
67 echo '<div class="notice notice-error">';
68 echo esc_html( "<p>${path} is missing.</p>" );
69 echo '</div>';
70 } else {
71 // Get the file version.
72 $file_version = filemtime( $file_path );
73 // Enqueue the style.
74 wp_enqueue_style( $handle, get_force_refresh_plugin_url( $path ), array(), $file_version );
75 }
76 }
77
78 /**
79 * Function to determine whether or not the currently logged-in user is able to request a refresh.
80 *
81 * @return bool true if the use is able to request a refresh
82 */
83 function user_can_request_force_refresh() {
84 return current_user_can( WP_FORCE_REFRESH_CAPABILITY );
85 }
86
87 /**
88 * Function for getting the directory for this plugin
89 *
90 * @return string The full directory this plugin is located in (including the plugin directory)
91 */
92 function get_force_refresh_plugin_directory() {
93 // Declare our plugin directory.
94 $plugin_directory = plugin_dir_path( get_main_plugin_file() );
95 return $plugin_directory;
96 }
97
98 /**
99 * Function for getting the URI for this plugin.
100 *
101 * @param string $file The optional file path you want to append to the urldecode(str).
102 *
103 * @return string The full url for the root of this plugin is located in (including the plugin
104 * directory)
105 */
106 function get_force_refresh_plugin_url( $file = null ) {
107 // Declare our plugin directory.
108 $plugin_url = plugins_url( $file, get_main_plugin_file() );
109 return $plugin_url;
110 }
111
112 /**
113 * Function to conditionally log data based if WP_DEBUG is set.
114 *
115 * @param mixed $log The data to log.
116 *
117 * @return void
118 */
119 function logger( $log ): void {
120 if ( defined( 'WP_DEBUG' ) && WP_DEBUG === true ) {
121 // phpcs:disable WordPress.PHP.DevelopmentFunctions
122 $log_formatted = sprintf( 'Force Refresh - %s', print_r( $log, true ) );
123 error_log( $log_formatted );
124 // phpcs:enable
125 }
126 }
127