| @@ -6,9 +6,22 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace JordanLeven\Plugins\ForceRefresh; |
| 9 | 9 | |
| 10 | +use JordanLeven\Plugins\ForceRefresh\Services\Versions_Storage_Service; | |
| 11 | + | |
| 10 | 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 | +/** | |
| 11 | 24 | * Function for adding scripts for this plugin. |
| 12 | 25 | * |
| 13 | 26 | * @param string $handle The script handle. |
| 14 | 27 | * |
| @@ -20,34 +33,35 @@ | ||
| 20 | 33 | // Get the file path. |
| 21 | 34 | $file_path = get_force_refresh_plugin_directory() . $path; |
| 22 | 35 | // If the file doesn't exist, throw an error. |
| 23 | 36 | 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( 'jquery', 'jquery-ui-core' ), | |
| 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( 'jquery', 'jquery-ui-core' ), | |
| 45 | - $file_version, | |
| 46 | - true, | |
| 47 | - ); | |
| 48 | - } | |
| 37 | + print_error( "{$path} is missing." ); | |
| 38 | + return; | |
| 49 | 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 | + ); | |
| 50 | 64 | } |
| 51 | 65 | |
| 52 | 66 | /** |
| 53 | 67 | * Function for enqueueing styles for this plugin. |
| @@ -61,17 +75,16 @@ | ||
| 61 | 75 | // Get the file path. |
| 62 | 76 | $file_path = get_force_refresh_plugin_directory() . $path; |
| 63 | 77 | // If the file doesn't exist, throw an error. |
| 64 | 78 | 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 ); | |
| 79 | + print_error( "{$path} is missing." ); | |
| 80 | + return; | |
| 73 | 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 ); | |
| 74 | 87 | } |
| 75 | 88 | |
| 76 | 89 | /** |
| 77 | 90 | * Function to determine whether or not the currently logged-in user is able to request a refresh. |
| @@ -104,5 +117,38 @@ | ||
| 104 | 117 | function get_force_refresh_plugin_url( $file = null ) { |
| 105 | 118 | // Declare our plugin directory. |
| 106 | 119 | $plugin_url = plugins_url( $file, get_main_plugin_file() ); |
| 107 | 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 | + } | |
| 108 | 154 | } |