1000 || $height > 1000 ) { return new WP_Error( 'itsec-static-map-api-invalid-resize-dimensions', __( 'Maximum map dimensions is 1000px.', 'better-wp-security' ) ); } $f_info = pathinfo( $file ); $f_info['dirname'] = trailingslashit( $f_info['dirname'] ); $filename_resized = "{$f_info['dirname']}{$f_info['filename']}-{$width}x{$height}.{$f_info['extension']}"; if ( file_exists( $filename_resized ) ) { return $filename_resized; } $editor = wp_get_image_editor( $file ); if ( is_wp_error( $editor ) ) { return $editor; } // We're treating everything as @2x. if ( is_wp_error( $resized = $editor->resize( $width ? $width * 2 : null, $height ? $height * 2 : null, true ) ) ) { return $resized; } $saved = $editor->save( $filename_resized ); if ( is_wp_error( $saved ) ) { return $saved; } return $filename_resized; } /** * Get a cached map image or fetch. * * @param array $config * * @return string|WP_Error */ private static function get_cached_map_or_fetch( array $config ) { $dir = trailingslashit( ITSEC_Core::get_storage_dir( 'static-map', true ) ); $key = wp_hash( $config['lat'] . $config['long'] ); if ( isset( $config['width'] ) || isset( $config['height'] ) ) { $key .= sprintf( '-%sx%s', isset( $config['width'] ) ? $config['width'] : 1000, isset( $config['height'] ) ? $config['height'] : 1000 ); } $file_name = "{$key}.png"; if ( ! file_exists( $dir . $file_name ) ) { $url = self::get_api()->get_map( $config ); if ( ! function_exists( 'download_url' ) ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); } if ( ! function_exists( 'download_url' ) ) { return new WP_Error( 'itsec-static-map-api-file-cache-no-download-url', __( 'The download_url() function was not found.', 'better-wp-security' ) ); } $temp = download_url( $url ); if ( is_wp_error( $temp ) ) { return $temp; } $checked = wp_check_filetype_and_ext( $temp, $file_name, array( 'png' => 'image/png' ) ); if ( 'image/png' !== $checked['type'] ) { return $url; } rename( $temp, $dir . $file_name ); } return $dir . $file_name; } /** * Get the static map API * * @return ITSEC_Static_Map_API */ private static function get_api() { if ( null === self::$api ) { foreach ( self::get_apis() as $api ) { if ( $api->is_available() ) { self::$api = $api; break; } } } return self::$api; } /** * Get the static map API providers. * * @return ITSEC_Static_Map_API[] */ private static function get_apis() { /** * Filter the static map API providers. * * @param $apis ITSEC_Static_Map_API[] */ return apply_filters( 'itsec_static_map_apis', array() ); } }