PluginProbe
Force Refresh / trunk
Force Refresh vtrunk
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 trunk, at includes/functions.php

155 lines 4.1 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 to print out an error message to the user
14 *
15 * @param string $message The message to display.
16 *
17 * @return void
18 */
19 function print_error( string $message ): void {
20 printf( '<div class="notice notice-error">%s</div>', esc_html( $message ) );
21 }
22
23 /**
24 * Function for adding scripts for this plugin.
25 *
26 * @param string $handle The script handle.
27 *
28 * @param string $path The path to the script (relative to the JS dist directory).
29 *
30 * @param boolean $register Whether we should simply register the script instead of enqueuing it.
31 */
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 }
40
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 );
52
53 return;
54 }
55
56 // Enqueue the style.
57 wp_enqueue_script(
58 $handle,
59 get_force_refresh_plugin_url( $path ),
60 array(),
61 $file_version,
62 true
63 );
64 }
65
66 /**
67 * Function for enqueueing styles for this plugin.
68 *
69 * @param string $handle The stylesheet handle.
70 * @param string $path The path to the stylesheet (relative to the CSS dist directory).
71 *
72 * @return void
73 */
74 function add_style( $handle, $path ) {
75 // Get the file path.
76 $file_path = get_force_refresh_plugin_directory() . $path;
77 // If the file doesn't exist, throw an error.
78 if ( ! file_exists( $file_path ) ) {
79 print_error( "{$path} is missing." );
80 return;
81 }
82
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 }
88
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 }
97
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;
107 }
108
109 /**
110 * Function for getting the URI for this plugin.
111 *
112 * @param string $file The optional file path you want to append to the urldecode(str).
113 *
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.
125 *
126 * @return array The plugin data.
127 */
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 }
132
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 }
141
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
153 }
154 }
155