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