Skip to content

Commit 965f135

Browse files
authored
Fetch svg styles on loading admin (#241)
* refactor SVG stylesheet presence detection * fetch SVG stylesheet when admin page is loaded if it's not already loaded * fixes * auto-formatting * remove unused vars
1 parent ee475f2 commit 965f135

File tree

2 files changed

+104
-15
lines changed

2 files changed

+104
-15
lines changed

includes/class-fontawesome-svg-styles-manager.php

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,64 @@ public function register_svg_styles( $fa ) {
153153
}
154154

155155
/**
156-
* Internal use only, not part of this plugin's public API.
157156
*
158-
* Fetches SVG support style asset(s) for self-hosting.
157+
* Internal use only, not part of the plugin's public API.
158+
*
159+
* Returns the full path to the SVG support stylesheet on the server's filesystem.
159160
*
160161
* @param $fa FontAwesome
161162
* @param $fa_release_provider FontAwesome_Release_Provider
162163
* @throws ReleaseMetadataMissingException
163-
* @throws ApiRequestException
164-
* @throws ApiResponseException
165164
* @throws ReleaseProviderStorageException
166165
* @throws SelfhostSetupException
167166
* @throws ConfigCorruptionException when called with an invalid configuration
168-
* @return void
167+
* @internal
168+
* @ignore
169+
* @return string
169170
*/
170-
public function fetch_svg_styles( $fa, $fa_release_provider ) {
171+
public function selfhost_asset_full_path( $fa ) {
172+
$options = $fa->options();
173+
$concrete_version = $fa->concrete_version( $options );
174+
175+
if ( ! $concrete_version ) {
176+
throw new SelfhostSetupException(
177+
esc_html__(
178+
'Failed to determine Font Awesome version when setting up self-hosted assets.',
179+
'font-awesome'
180+
)
181+
);
182+
}
183+
184+
$asset_path = $this->selfhost_asset_path( $concrete_version );
185+
186+
if ( ! $asset_path || ! isset( $asset_path['dir'] ) || ! isset( $asset_path['file'] ) ) {
187+
throw new SelfhostSetupException(
188+
esc_html__(
189+
'Failed to determine filesystem location for self-hosted asset. Please report this on the plugin support forum so it can be investigated.',
190+
'font-awesome'
191+
)
192+
);
193+
}
194+
195+
return trailingslashit( $asset_path['dir'] ) . $asset_path['file'];
196+
}
197+
198+
/**
199+
* Internal use only, not part of the plugin's public API.
200+
*
201+
* This checks whether the SVG support stylesheet is present.
202+
*
203+
* @param $fa FontAwesome
204+
* @param $fa_release_provider FontAwesome_Release_Provider
205+
* @throws ReleaseMetadataMissingException
206+
* @throws ReleaseProviderStorageException
207+
* @throws SelfhostSetupException
208+
* @throws ConfigCorruptionException when called with an invalid configuration
209+
* @internal
210+
* @ignore
211+
* @return bool
212+
*/
213+
public function is_svg_stylesheet_present( $fa ) {
171214
if ( ! current_user_can( 'manage_options' ) ) {
172215
throw new SelfhostSetupException(
173216
esc_html__(
@@ -177,18 +220,58 @@ public function fetch_svg_styles( $fa, $fa_release_provider ) {
177220
);
178221
}
179222

180-
$options = $fa->options();
181-
$concrete_version = fa()->concrete_version( $options );
223+
$asset_full_path = $this->selfhost_asset_full_path( $fa );
182224

183-
if ( ! $concrete_version ) {
225+
if ( ! function_exists( 'WP_Filesystem' ) ) {
226+
require_once ABSPATH . 'wp-admin/includes/file.php';
227+
}
228+
229+
if ( ! WP_Filesystem( false ) ) {
184230
throw new SelfhostSetupException(
185231
esc_html__(
186-
'Failed to determine Font Awesome version when setting up self-hosted assets.',
232+
'Failed to initialize filesystem usage for creating self-hosted assets. Please report this on the plugin support forum so it can be investigated.',
187233
'font-awesome'
188234
)
189235
);
190236
}
191237

238+
global $wp_filesystem;
239+
240+
return $wp_filesystem->exists( $asset_full_path );
241+
}
242+
243+
/**
244+
* Internal use only, not part of this plugin's public API.
245+
*
246+
* Fetches SVG support style asset(s) for self-hosting.
247+
*
248+
* @param $fa FontAwesome
249+
* @param $fa_release_provider FontAwesome_Release_Provider
250+
* @throws ReleaseMetadataMissingException
251+
* @throws ApiRequestException
252+
* @throws ApiResponseException
253+
* @throws ReleaseProviderStorageException
254+
* @throws SelfhostSetupException
255+
* @throws ConfigCorruptionException when called with an invalid configuration
256+
* @return void
257+
*/
258+
public function fetch_svg_styles( $fa, $fa_release_provider ) {
259+
if ( ! current_user_can( 'manage_options' ) ) {
260+
throw new SelfhostSetupException(
261+
esc_html__(
262+
'Current user lacks permissions required to fetch Font Awesome SVG stylesheets for self-hosting. Try logging in as an admin user.',
263+
'font-awesome'
264+
)
265+
);
266+
}
267+
268+
if ( $this->is_svg_stylesheet_present( $fa ) ) {
269+
// Nothing more to do.
270+
return;
271+
}
272+
273+
$concrete_version = $fa->concrete_version( $fa->options() );
274+
192275
$asset_path = $this->selfhost_asset_path( $concrete_version );
193276

194277
if ( ! $asset_path || ! isset( $asset_path['dir'] ) || ! isset( $asset_path['file'] ) ) {
@@ -217,11 +300,6 @@ public function fetch_svg_styles( $fa, $fa_release_provider ) {
217300

218301
global $wp_filesystem;
219302

220-
if ( $wp_filesystem->exists( $full_asset_path ) ) {
221-
// Nothing more to do.
222-
return;
223-
}
224-
225303
$resource = $fa_release_provider->get_svg_styles_resource( $concrete_version );
226304

227305
if ( ! $resource->source() || ! $resource->integrity_key() ) {

includes/class-fontawesome.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,6 +1036,17 @@ function ( $plugin_file, $plugin_data ) {
10361036
10,
10371037
3
10381038
);
1039+
1040+
try {
1041+
$svg_styles_manager = FontAwesome_SVG_Styles_Manager::instance();
1042+
if ( ! $svg_styles_manager->is_svg_stylesheet_present( $this ) ) {
1043+
$svg_styles_manager->fetch_svg_styles( $this, $this->release_provider() );
1044+
}
1045+
} catch ( Exception $e ) {
1046+
notify_admin_fatal_error( $e );
1047+
} catch ( Error $e ) {
1048+
notify_admin_fatal_error( $e );
1049+
}
10391050
}
10401051

10411052
/**

0 commit comments

Comments
 (0)