Skip to content

Commit

Permalink
Switch from direct PHP functions to wrappers around WP_Filesystem for…
Browse files Browse the repository at this point in the history
… access local files
  • Loading branch information
dshanske committed Dec 28, 2024
1 parent b4d33b4 commit 367cac2
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 21 deletions.
4 changes: 2 additions & 2 deletions includes/class-airport-location.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function get( $search, $field = 'iata_code' ) {
return $airport;
}
$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/airports.csv';
if ( ! file_exists( $file ) ) {
if ( ! sloc_file_exists( $file ) ) {
return new WP_Error( 'filesystem_error', "File doesn't exist" );
}
$fp = fopen( $file, 'r' ); // phpcs:ignore
Expand Down Expand Up @@ -147,7 +147,7 @@ public static function get_airline( $search, $field = 'iata_code' ) {
return $airline;
}
$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/airlines.csv';
if ( ! file_exists( $file ) ) {
if ( ! sloc_file_exists( $file ) ) {
return new WP_Error( 'filesystem_error', "File doesn't exist" );
}
$fp = fopen( $file, 'r' ); // phpcs:ignore
Expand Down
2 changes: 1 addition & 1 deletion includes/class-geo-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public static function map_archive_template( $original_template ) {
);

foreach ( $look as $l ) {
if ( file_exists( $l . $template_name ) ) {
if ( sloc_file_exists( $l . $template_name ) ) {
return $l . $template_name;
}
}
Expand Down
4 changes: 2 additions & 2 deletions includes/class-geo-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,8 @@ public static function get_icon( $icon = null, $summary = null ) {
$summary = array_key_exists( $icon, $list ) ? $list[ $icon ] : $icon;
}
$svg = sprintf( '%1$ssvgs/%2$s.svg', plugin_dir_path( __DIR__ ), $icon );
if ( file_exists( $svg ) ) {
$svg = file_get_contents( $svg );
if ( sloc_file_exists( $svg ) ) {
$svg = sloc_get_contents( $svg );
}
if ( $svg ) {
return PHP_EOL . sprintf( '<span class="sloc-location-icon sloc-icon-%1$s" style="display: inline-block; max-height: 1em; margin-right: 0.1em;" aria-hidden="true" aria-label="%2$s" title="%2$s" >%3$s</span>', esc_attr( $icon ), esc_attr( $summary ), $svg );
Expand Down
6 changes: 3 additions & 3 deletions includes/class-loc-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public static function simloc_options() {
<?php
if ( 'debug' === $active_tab ) {
?>

<p> <?php esc_html_e( 'Test the raw responses from the lookup features. This feature only appears if you have WP_DEBUG enabled.', 'simple-location' ); ?> </p>
<?php
load_template( plugin_dir_path( __DIR__ ) . 'templates/geocode-form.php' );
Expand Down Expand Up @@ -1071,7 +1071,7 @@ public static function country_callback( array $args ) {
$name = $args['label_for'];

$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/iso_3166-1.json';
$codes = json_decode( file_get_contents( $file ), true );
$codes = json_decode( sloc_get_contents( $file ), true );
$codes = $codes['3166-1'];
$country = get_option( $name );

Expand Down Expand Up @@ -1158,7 +1158,7 @@ public static function sloc_provider_settings() {
?>
<h4><?php esc_html_e( 'Simple Location Depends on Third Party Services', 'simple-location' ); ?></h4>
<p><?php esc_html_e( 'Many of these services require you to sign up for an account and provide an API key below. Many have free and paid tiers. For this reason, the plugin offers multiple providers. At this moment, Nominatim, and the US National Weather Service can be used without API keys and there is no map service that does not require an API key. Geonames requires an account, but is otherwise free to use. If you are uncertain of which to try, start with the defaults.', 'simple-location' ); ?></p>

<?php
}

Expand Down
4 changes: 2 additions & 2 deletions includes/class-loc-timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ function ( $a, $b ) {

private static function tz_data() {
$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/tz_cities.txt';
if ( ! file_exists( $file ) ) {
if ( ! sloc_file_exists( $file ) ) {
return new WP_Error( 'filesystem_error', "File doesn't exist" );
}
return file( $file );
return sloc_get_contents_array( $file );
}

public static function rest_prepare_post( $response, $post, $request ) {
Expand Down
21 changes: 21 additions & 0 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,3 +629,24 @@ function sloc_get_attachment_datetime( $attachment, $field = 'created' ) {
}
return date_create_immutable_from_format( 'Y-m-d H:i:s', $time, wp_timezone() );
}

function sloc_file_exists( $file ) {
require_once ( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
require_once ( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
$wpfile = new WP_Filesystem_Direct( false );
return $wpfile->exists( $file );
}

function sloc_get_contents( $file ) {
require_once ( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
require_once ( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
$wpfile = new WP_Filesystem_Direct( false );
return $wpfile->get_contents( $file );
}

function sloc_get_contents_array( $file ) {
require_once ( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
require_once ( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
$wpfile = new WP_Filesystem_Direct( false );
return $wpfile->get_contents_array( $file );
}
18 changes: 9 additions & 9 deletions includes/trait-geolocation-trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static function country_data( $iso ) {
$iso = strtoupper( trim( $iso ) );

$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/iso_3166-1.json';
$codes = json_decode( file_get_contents( $file ), true );
$codes = json_decode( sloc_get_contents( $file ), true );
$codes = $codes['3166-1'];
$match = wp_filter_object_list( $codes, array( 'alpha_' . strlen( $iso ) => $iso ) );
if ( is_array( $match ) && 1 === count( $match ) ) {
Expand All @@ -72,7 +72,7 @@ public static function country_data( $iso ) {
public static function country_data_name( $name ) {
$name = trim( $name );
$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/iso_3166-1.json';
$codes = json_decode( file_get_contents( $file ), true );
$codes = json_decode( sloc_get_contents( $file ), true );
$codes = $codes['3166-1'];
$match = wp_filter_object_list( $codes, array( 'name' => $name ) );
if ( is_array( $match ) && 1 === count( $match ) ) {
Expand Down Expand Up @@ -151,11 +151,11 @@ public static function region_data( $code, $country ) {
$codes = array();

$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/iso_3166-2/' . $country . '.json';
if ( ! file_exists( $file ) ) {
if ( ! sloc_file_exists( $file ) ) {
return false;
}

$codes = json_decode( file_get_contents( $file ), true );
$codes = json_decode( sloc_get_contents( $file ), true );

$match = wp_filter_object_list( $codes, array( 'code' => $country . '-' . $code ) );

Expand All @@ -178,11 +178,11 @@ public static function region_data_name( $name, $country ) {
$codes = array();

$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/iso_3166-2/' . $country . '.json';
if ( ! file_exists( $file ) ) {
if ( ! sloc_file_exists( $file ) ) {
return false;
}

$codes = json_decode( file_get_contents( $file ), true );
$codes = json_decode( sloc_get_contents( $file ), true );

$match = wp_filter_object_list( $codes, array( 'name' => $name ) );

Expand Down Expand Up @@ -260,7 +260,7 @@ public function timezone() {

public static function country_select( $country ) {
$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/iso_3166-1.json';
$codes = json_decode( file_get_contents( $file ), true );
$codes = json_decode( sloc_get_contents( $file ), true );
$codes = $codes['3166-1'];

echo '<select name="country" id="country">';
Expand All @@ -276,11 +276,11 @@ public static function region_select( $region, $country ) {
return false;
}
$file = trailingslashit( plugin_dir_path( __DIR__ ) ) . 'data/iso_3166-2/' . $country . '.json';
if ( ! file_exists( $file ) ) {
if ( ! sloc_file_exists( $file ) ) {
return false;
}

$codes = json_decode( file_get_contents( $file ), true );
$codes = json_decode( sloc_get_contents( $file ), true );
$codes = wp_list_pluck( $codes, 'name', 'code' );
if ( ! array_key_exists( $country . '-' . $region, $codes ) && ! empty( $region ) ) {
printf( '<input class="widefat" type=text" name="region" value="%s" required />', esc_attr( $region ) );
Expand Down
4 changes: 2 additions & 2 deletions includes/trait-weather-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public static function get_icon( $icon, $summary = null ) {
$summary = $icon;
}
$svg = sprintf( '%1$ssvgs/%2$s.svg', plugin_dir_path( __DIR__ ), $icon );
if ( file_exists( $svg ) ) {
return PHP_EOL . sprintf( '<span class="sloc-weather-icon sloc-icon-%1$s" aria-hidden="true" aria-label="%2$s" title="%2$s" >%3$s</span>', esc_attr( $icon ), esc_attr( $summary ), file_get_contents( $svg ) );
if ( sloc_file_exists( $svg ) ) {
return PHP_EOL . sprintf( '<span class="sloc-weather-icon sloc-icon-%1$s" aria-hidden="true" aria-label="%2$s" title="%2$s" >%3$s</span>', esc_attr( $icon ), esc_attr( $summary ), sloc_get_contents( $svg ) );
}
return '';
}
Expand Down

0 comments on commit 367cac2

Please sign in to comment.