| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPAdminify\Inc\Classes; |
| 4 |
|
| 5 |
// no direct access allowed |
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class Helper { |
| 11 |
|
| 12 |
|
| 13 |
// Admin Path |
| 14 |
public static function jltwp_adminify_admin_path( $path ) { |
| 15 |
// Get custom filter path |
| 16 |
if ( has_filter( 'jltwp_adminify_admin_path' ) ) { |
| 17 |
return apply_filters( 'jltwp_adminify_admin_path', $path ); |
| 18 |
} |
| 19 |
|
| 20 |
// Get plugin path |
| 21 |
return plugins_url( $path, __DIR__ ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Get the editor/ builder of the given post. |
| 26 |
* |
| 27 |
* @param int $post_id ID of the post being checked. |
| 28 |
* @return string The content editor name. |
| 29 |
*/ |
| 30 |
public static function get_content_editor( $post_id ) { |
| 31 |
$content_editor = 'default'; |
| 32 |
$content_editor = apply_filters( 'udb_content_editor', $content_editor, $post_id ); |
| 33 |
|
| 34 |
return $content_editor; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Sanitize Checkbox. |
| 39 |
* |
| 40 |
* @param string|bool $checked Customizer option. |
| 41 |
*/ |
| 42 |
public function sanitize_checkbox( $checked ) { |
| 43 |
return ( ( isset( $checked ) && true === $checked ) ? true : false ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Image sanitization callback. |
| 49 |
* |
| 50 |
* Checks the image's file extension and mime type against a whitelist. If they're allowed, |
| 51 |
* send back the filename, otherwise, return the setting default. |
| 52 |
* |
| 53 |
* - Sanitization: image file extension |
| 54 |
* - Control: text, WP_Customize_Image_Control |
| 55 |
* |
| 56 |
* @see wp_check_filetype() https://developer.wordpress.org/reference/functions/wp_check_filetype/ |
| 57 |
* |
| 58 |
* @version 1.2.2 |
| 59 |
* |
| 60 |
* @param string $image Image filename. |
| 61 |
* @param WP_Customize_Setting $setting Setting instance. |
| 62 |
* |
| 63 |
* @return string The image filename if the extension is allowed; otherwise, the setting default. |
| 64 |
*/ |
| 65 |
public static function sanitize_image( $image, $setting ) { |
| 66 |
|
| 67 |
/** |
| 68 |
* Array of valid image file types. |
| 69 |
* |
| 70 |
* The array includes image mime types that are included in wp_get_mime_types() |
| 71 |
*/ |
| 72 |
$mimes = [ |
| 73 |
'jpg|jpeg|jpe' => 'image/jpeg', |
| 74 |
'gif' => 'image/gif', |
| 75 |
'png' => 'image/png', |
| 76 |
'bmp' => 'image/bmp', |
| 77 |
'tif|tiff' => 'image/tiff', |
| 78 |
'ico' => 'image/x-icon', |
| 79 |
]; |
| 80 |
|
| 81 |
// Allowed svg mime type in version 1.2.2. |
| 82 |
$allowed_mime = get_allowed_mime_types(); |
| 83 |
$svg_mime_check = isset( $allowed_mime['svg'] ) ? true : false; |
| 84 |
|
| 85 |
if ( $svg_mime_check ) { |
| 86 |
$allow_mime = [ 'svg' => 'image/svg+xml' ]; |
| 87 |
$mimes = array_merge( $mimes, $allow_mime ); |
| 88 |
} |
| 89 |
|
| 90 |
// Return an array with file extension and mime_type. |
| 91 |
$file = wp_check_filetype( $image, $mimes ); |
| 92 |
|
| 93 |
// If $image has a valid mime_type, return it; otherwise, return the default. |
| 94 |
return esc_url_raw( ( $file['ext'] ? $image : $setting->default ) ); |
| 95 |
} |
| 96 |
} |
| 97 |
|