| 1 |
<?php |
| 2 |
// If this file is called directly, abort. |
| 3 |
if ( ! defined( 'WPINC' ) ) { |
| 4 |
die; |
| 5 |
} |
| 6 |
|
| 7 |
use enshrined\svgSanitize\Sanitizer; |
| 8 |
use FreepikLabs\DomPurify\Purifier; |
| 9 |
|
| 10 |
if ( ! function_exists( 'cbxgooglemap_esc_svg' ) ) { |
| 11 |
/** |
| 12 |
* SVG sanitizer |
| 13 |
* |
| 14 |
* @param string $svg_content The content of the SVG file |
| 15 |
* |
| 16 |
* @return string|false The SVG content if found, or false on failure. |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
function cbxgooglemap_esc_svg( $svg_content = '' ) { |
| 20 |
// Create a new sanitizer instance |
| 21 |
$sanitizer = new Sanitizer(); |
| 22 |
|
| 23 |
return $sanitizer->sanitize( $svg_content ); |
| 24 |
}// end method cbxgooglemap_esc_svg |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
if ( ! function_exists( 'cbxgooglemap_load_svg' ) ) { |
| 29 |
/** |
| 30 |
* Load an SVG file from a directory. |
| 31 |
* |
| 32 |
* @param string $svg_name The name of the SVG file (without the .svg extension). |
| 33 |
* @param string $directory The directory where the SVG files are stored. |
| 34 |
* |
| 35 |
* @return string|false The SVG content if found, or false on failure. |
| 36 |
* @since 1.0.0 |
| 37 |
*/ |
| 38 |
function cbxgooglemap_load_svg( $svg_name = '', $folder = '' ) { |
| 39 |
//note: code partially generated using chatgpt |
| 40 |
if ( $svg_name == '' ) { |
| 41 |
return ''; |
| 42 |
} |
| 43 |
|
| 44 |
if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 45 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 46 |
} |
| 47 |
|
| 48 |
$credentials = request_filesystem_credentials( site_url() . '/wp-admin/', '', false, false, null ); |
| 49 |
if ( ! WP_Filesystem( $credentials ) ) { |
| 50 |
return; // Error handling here |
| 51 |
} |
| 52 |
|
| 53 |
global $wp_filesystem; |
| 54 |
|
| 55 |
$directory = cbxgooglemap_icon_path(); |
| 56 |
|
| 57 |
// Sanitize the file name to prevent directory traversal attacks. |
| 58 |
$svg_name = sanitize_file_name( $svg_name ); |
| 59 |
if ( $folder != '' ) { |
| 60 |
$folder = trailingslashit( $folder ); |
| 61 |
} |
| 62 |
|
| 63 |
// Construct the full file path. |
| 64 |
$file_path = $directory . $folder . $svg_name . '.svg'; |
| 65 |
|
| 66 |
$file_path = apply_filters( 'cbxgooglemap_svg_file_path', $file_path, $svg_name ); |
| 67 |
|
| 68 |
// Check if the file exists. |
| 69 |
if ( $wp_filesystem->exists( $file_path ) && is_readable( $file_path ) ) { |
| 70 |
// Get the SVG file content. |
| 71 |
return $wp_filesystem->get_contents( $file_path ); |
| 72 |
} else { |
| 73 |
// Return false if the file does not exist or is not readable. |
| 74 |
return ''; |
| 75 |
} |
| 76 |
}//end method cbxgooglemap_load_svg |
| 77 |
} |
| 78 |
|
| 79 |
if ( ! function_exists( 'cbxgooglemap_icon_path' ) ) { |
| 80 |
/** |
| 81 |
* Form icon path |
| 82 |
* |
| 83 |
* @return mixed|null |
| 84 |
* @since 1.0.0 |
| 85 |
*/ |
| 86 |
function cbxgooglemap_icon_path() { |
| 87 |
$directory = trailingslashit( CBXGOOGLEMAP_ROOT_PATH ) . 'assets/icons/'; |
| 88 |
|
| 89 |
return apply_filters( 'cbxgooglemap_icon_path', $directory ); |
| 90 |
}//end method cbxgooglemap_icon_path |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! function_exists( 'cbxgooglemap_is_rest_api_request' ) ) { |
| 94 |
/** |
| 95 |
* Check if doing rest request |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
*/ |
| 99 |
function cbxgooglemap_is_rest_api_request() { |
| 100 |
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
if ( empty( $_SERVER['REQUEST_URI'] ) ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
$rest_prefix = trailingslashit( rest_get_url_prefix() ); |
| 109 |
|
| 110 |
return ( false !== strpos( sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ), $rest_prefix ) ); |
| 111 |
}//end function cbxgooglemap_is_rest_api_request |
| 112 |
} |
| 113 |
|
| 114 |
if ( ! function_exists( 'cbxgooglemap_doing_it_wrong' ) ) { |
| 115 |
/** |
| 116 |
* Wrapper for _doing_it_wrong(). |
| 117 |
* |
| 118 |
* @param string $function Function used. |
| 119 |
* @param string $message Message to log. |
| 120 |
* @param string $version Version the message was added in. |
| 121 |
* |
| 122 |
* @since 1.0.0 |
| 123 |
*/ |
| 124 |
function cbxgooglemap_doing_it_wrong( $function, $message, $version ) { |
| 125 |
// @codingStandardsIgnoreStart |
| 126 |
$message .= ' Backtrace: ' . wp_debug_backtrace_summary(); |
| 127 |
|
| 128 |
if ( wp_doing_ajax() || cbxgooglemap_is_rest_api_request() ) { |
| 129 |
do_action( 'doing_it_wrong_run', $function, $message, $version ); |
| 130 |
error_log( "{$function} was called incorrectly. {$message}. This message was added in version {$version}." ); |
| 131 |
} else { |
| 132 |
_doing_it_wrong( $function, $message, $version ); |
| 133 |
} |
| 134 |
// @codingStandardsIgnoreEnd |
| 135 |
}//end function cbxgooglemap_doing_it_wrong |
| 136 |
} |
| 137 |
|
| 138 |
if ( ! function_exists( 'cbxgooglemap_login_url_with_redirect' ) ) { |
| 139 |
function cbxgooglemap_login_url_with_redirect() { |
| 140 |
//$login_url = wp_login_url(); |
| 141 |
//$redirect_url = ''; |
| 142 |
|
| 143 |
if ( is_singular() ) { |
| 144 |
$login_url = wp_login_url( get_permalink() ); |
| 145 |
//$redirect_url = get_permalink(); |
| 146 |
} else { |
| 147 |
global $wp; |
| 148 |
$login_url = wp_login_url( home_url( add_query_arg( [], $wp->request ) ) ); |
| 149 |
//$redirect_url = home_url( add_query_arg( [], $wp->request ) ); |
| 150 |
} |
| 151 |
|
| 152 |
return $login_url; |
| 153 |
}//end function cbxgooglemap_login_url_with_redirect |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! function_exists( 'cbxgooglemap_check_and_deactivate_plugin' ) ) { |
| 157 |
/** |
| 158 |
* Check any plugin and if version less than |
| 159 |
* |
| 160 |
* @param string $plugin_slug plugin slug |
| 161 |
* @param string $required_version required plugin version |
| 162 |
* @param string $transient transient name |
| 163 |
* |
| 164 |
* @return bool|void |
| 165 |
* @since 2.0.0 |
| 166 |
*/ |
| 167 |
function cbxgooglemap_check_and_deactivate_plugin( $plugin_slug = '', $required_version = '', $transient = '' ) { |
| 168 |
if ( $plugin_slug == '' ) { |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
if ( $required_version == '' ) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 177 |
include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 178 |
} |
| 179 |
|
| 180 |
// Check if the plugin is active |
| 181 |
if ( is_plugin_active( $plugin_slug ) ) { |
| 182 |
// Get the plugin data |
| 183 |
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin_slug ); |
| 184 |
$plugin_version = isset( $plugin_data['Version'] ) ? $plugin_data['Version'] : ''; |
| 185 |
if ( $plugin_version == '' || is_null( $plugin_version ) ) { |
| 186 |
return; |
| 187 |
} |
| 188 |
|
| 189 |
// Compare the plugin version with the required version |
| 190 |
if ( version_compare( $plugin_version, $required_version, '<' ) ) { |
| 191 |
// Deactivate the plugin |
| 192 |
deactivate_plugins( $plugin_slug ); |
| 193 |
if ( $transient != '' ) { |
| 194 |
set_transient( $transient, 1 ); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
//return false; |
| 200 |
}//end method cbxgooglemap_check_and_deactivate_plugin |
| 201 |
} |
| 202 |
|
| 203 |
if(!function_exists('cbxgooglemap_decode_entities_array')){ |
| 204 |
function cbxgooglemap_decode_entities_array($arr = []){ |
| 205 |
return array_map(function ($v) { |
| 206 |
return is_string($v) ? html_entity_decode($v, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8') : $v; |
| 207 |
}, $arr); |
| 208 |
} |
| 209 |
} |