PluginProbe
CBX Map for Google Map & OpenStreetMap / 2.0.0
CBX Map for Google Map & OpenStreetMap v2.0.0
trunk 1.1.10 1.1.11 1.1.12 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5
cbxgooglemap / includes / Functions / cbxgooglemap-functions.php

cbxgooglemap-functions.php in CBX Map for Google Map & OpenStreetMap 2.0.0, at includes/Functions/cbxgooglemap-functions.php

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