| 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 |
{ |
| 16 |
// Get custom filter path |
| 17 |
if (has_filter('jltwp_adminify_admin_path')) { |
| 18 |
return apply_filters('jltwp_adminify_admin_path', $path); |
| 19 |
} |
| 20 |
|
| 21 |
// Get plugin path |
| 22 |
return plugins_url($path, __DIR__); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get the editor/ builder of the given post. |
| 27 |
* |
| 28 |
* @param int $post_id ID of the post being checked. |
| 29 |
* @return string The content editor name. |
| 30 |
*/ |
| 31 |
public static function get_content_editor($post_id) |
| 32 |
{ |
| 33 |
$content_editor = 'default'; |
| 34 |
$content_editor = apply_filters('udb_content_editor', $content_editor, $post_id); |
| 35 |
|
| 36 |
return $content_editor; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Sanitize Checkbox. |
| 41 |
* |
| 42 |
* @param string|bool $checked Customizer option. |
| 43 |
*/ |
| 44 |
public function sanitize_checkbox($checked) |
| 45 |
{ |
| 46 |
return ((isset($checked) && true === $checked) ? true : false); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
/** |
| 52 |
* Check if Gutenberg Block Editor Page |
| 53 |
* |
| 54 |
* WordPress v5+ |
| 55 |
* |
| 56 |
* @return boolean |
| 57 |
*/ |
| 58 |
public static function is_block_editor_page() |
| 59 |
{ |
| 60 |
if (function_exists('is_gutenberg_page') && is_gutenberg_page()) { |
| 61 |
// The Gutenberg plugin is on. |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
if (!function_exists('get_current_screen')) { |
| 66 |
require_once ABSPATH . '/wp-admin/includes/screen.php'; |
| 67 |
$current_screen = get_current_screen(); |
| 68 |
if (function_exists('get_current_screen')) { |
| 69 |
if (!is_null($current_screen) && $current_screen->is_block_editor()) { |
| 70 |
// Gutenberg page on 5+. |
| 71 |
return true; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
/** |
| 80 |
* Classic Editor Page |
| 81 |
* |
| 82 |
* @return boolean |
| 83 |
*/ |
| 84 |
public static function is_classic_editor_page() |
| 85 |
{ |
| 86 |
if (function_exists('get_current_screen')) { |
| 87 |
$current_screen = get_current_screen(); |
| 88 |
if ($current_screen->base == 'post' && empty($current_screen->is_block_editor)) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
} |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Check if Elementor Page |
| 97 |
* |
| 98 |
* @return boolean |
| 99 |
*/ |
| 100 |
public static function is_elementor_editor_page() |
| 101 |
{ |
| 102 |
return !empty($_GET['elementor-preview']); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Image sanitization callback. |
| 107 |
* |
| 108 |
* Checks the image's file extension and mime type against a whitelist. If they're allowed, |
| 109 |
* send back the filename, otherwise, return the setting default. |
| 110 |
* |
| 111 |
* - Sanitization: image file extension |
| 112 |
* - Control: text, WP_Customize_Image_Control |
| 113 |
* |
| 114 |
* @see wp_check_filetype() https://developer.wordpress.org/reference/functions/wp_check_filetype/ |
| 115 |
* |
| 116 |
* @version 1.2.2 |
| 117 |
* |
| 118 |
* @param string $image Image filename. |
| 119 |
* @param WP_Customize_Setting $setting Setting instance. |
| 120 |
* |
| 121 |
* @return string The image filename if the extension is allowed; otherwise, the setting default. |
| 122 |
*/ |
| 123 |
public static function sanitize_image($image, $setting) |
| 124 |
{ |
| 125 |
|
| 126 |
/** |
| 127 |
* Array of valid image file types. |
| 128 |
* |
| 129 |
* The array includes image mime types that are included in wp_get_mime_types() |
| 130 |
*/ |
| 131 |
$mimes = array( |
| 132 |
'jpg|jpeg|jpe' => 'image/jpeg', |
| 133 |
'gif' => 'image/gif', |
| 134 |
'png' => 'image/png', |
| 135 |
'bmp' => 'image/bmp', |
| 136 |
'tif|tiff' => 'image/tiff', |
| 137 |
'ico' => 'image/x-icon', |
| 138 |
); |
| 139 |
|
| 140 |
// Allowed svg mime type in version 1.2.2. |
| 141 |
$allowed_mime = get_allowed_mime_types(); |
| 142 |
$svg_mime_check = isset($allowed_mime['svg']) ? true : false; |
| 143 |
|
| 144 |
if ($svg_mime_check) { |
| 145 |
$allow_mime = array('svg' => 'image/svg+xml'); |
| 146 |
$mimes = array_merge($mimes, $allow_mime); |
| 147 |
} |
| 148 |
|
| 149 |
// Return an array with file extension and mime_type. |
| 150 |
$file = wp_check_filetype($image, $mimes); |
| 151 |
|
| 152 |
// If $image has a valid mime_type, return it; otherwise, return the default. |
| 153 |
return esc_url_raw(($file['ext'] ? $image : $setting->default)); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get User Capabilities |
| 158 |
* |
| 159 |
* @param [type] $user |
| 160 |
* |
| 161 |
* @return void |
| 162 |
*/ |
| 163 |
public static function get_user_capabilities($user = null) |
| 164 |
{ |
| 165 |
$user = $user ? new \WP_User($user) : wp_get_current_user(); |
| 166 |
|
| 167 |
return array_keys($user->allcaps); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Get User Roles |
| 172 |
* |
| 173 |
* @param [type] $user |
| 174 |
* |
| 175 |
* @return void |
| 176 |
*/ |
| 177 |
static function get_user_roles($user) |
| 178 |
{ |
| 179 |
return $user->roles; |
| 180 |
} |
| 181 |
|
| 182 |
static function nocache_headers() |
| 183 |
{ |
| 184 |
if (headers_sent()) { |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
$headers = wp_get_nocache_headers(); |
| 189 |
|
| 190 |
unset($headers['Last-Modified']); |
| 191 |
|
| 192 |
header_remove('Last-Modified'); |
| 193 |
|
| 194 |
foreach ($headers as $name => $field_value) { |
| 195 |
header("{$name}: {$field_value}"); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
/** |
| 201 |
* Set allowed Hosts |
| 202 |
* |
| 203 |
* @param [type] $url |
| 204 |
* |
| 205 |
* @return void |
| 206 |
*/ |
| 207 |
public static function allowed_host($url) |
| 208 |
{ |
| 209 |
$url_parsed = parse_url($url); |
| 210 |
if (isset($url_parsed['host'])) { |
| 211 |
$allowed_hosts[] = $url_parsed['host']; |
| 212 |
add_filter('allowed_redirect_hosts', function ($hosts) use ($allowed_hosts) { |
| 213 |
return array_merge($hosts, $allowed_hosts); |
| 214 |
}); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
/** |
| 220 |
* Get Capabilites |
| 221 |
* |
| 222 |
* @return void |
| 223 |
*/ |
| 224 |
public static function get_capabilities() |
| 225 |
{ |
| 226 |
global $wp_roles; |
| 227 |
|
| 228 |
$caps = array(); |
| 229 |
|
| 230 |
foreach ($wp_roles->roles as $wp_role) { |
| 231 |
if (isset($wp_role['capabilities']) && is_array($wp_role['capabilities'])) { |
| 232 |
$caps = array_merge($caps, array_keys($wp_role['capabilities'])); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
$caps = array_unique($caps); |
| 237 |
|
| 238 |
$caps = (array) apply_filters('adminify_capabilities', $caps); |
| 239 |
|
| 240 |
sort($caps); |
| 241 |
|
| 242 |
return array_combine($caps, $caps); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Get User Capability Options |
| 247 |
* |
| 248 |
* @return void |
| 249 |
*/ |
| 250 |
public static function get_capability_options() |
| 251 |
{ |
| 252 |
$capabilities = self::get_capabilities(); |
| 253 |
$_capabilities = []; |
| 254 |
|
| 255 |
foreach ($capabilities as $capability) { |
| 256 |
$_capabilities[] = $capability; |
| 257 |
} |
| 258 |
return $_capabilities; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
/** |
| 263 |
* Remove spaces from Plugin Slug |
| 264 |
*/ |
| 265 |
public static function jltwp_adminify_slug_cleanup() |
| 266 |
{ |
| 267 |
return str_replace('-', '_', strtolower(WP_ADMINIFY_SLUG)); |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Function current_datetime() compability for wp version < 5.3 |
| 272 |
* |
| 273 |
* @return DateTimeImmutable |
| 274 |
*/ |
| 275 |
public static function jltwp_adminify_current_datetime() |
| 276 |
{ |
| 277 |
if (function_exists('current_datetime')) { |
| 278 |
return current_datetime(); |
| 279 |
} |
| 280 |
|
| 281 |
return new \DateTimeImmutable('now', self::jltwp_adminify_wp_timezone()); |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Function jltwp_adminify_wp_timezone() compability for wp version < 5.3 |
| 286 |
* |
| 287 |
* @return DateTimeZone |
| 288 |
*/ |
| 289 |
public static function jltwp_adminify_wp_timezone() |
| 290 |
{ |
| 291 |
if (function_exists('wp_timezone')) { |
| 292 |
return wp_timezone(); |
| 293 |
} |
| 294 |
|
| 295 |
return new \DateTimeZone(self::jltwp_adminify_wp_timezone_string()); |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* API Endpoint |
| 300 |
* |
| 301 |
* @return string |
| 302 |
*/ |
| 303 |
public static function api_endpoint() |
| 304 |
{ |
| 305 |
$api_endpoint_url = 'https://bo.jeweltheme.com'; |
| 306 |
$api_endpoint = apply_filters('jltwp_adminify_endpoint', $api_endpoint_url); |
| 307 |
|
| 308 |
return trailingslashit($api_endpoint); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* CRM Endpoint |
| 313 |
* |
| 314 |
* @return string |
| 315 |
*/ |
| 316 |
public static function crm_endpoint() |
| 317 |
{ |
| 318 |
$crm_endpoint_url = 'https://bo.jeweltheme.com/wp-json/jlt-api/v1/subscribe'; // Endpoint . |
| 319 |
$crm_endpoint = apply_filters('jltwp_adminify_crm_crm_endpoint', $crm_endpoint_url); |
| 320 |
|
| 321 |
return trailingslashit($crm_endpoint); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* CRM Endpoint |
| 326 |
* |
| 327 |
* @return string |
| 328 |
*/ |
| 329 |
public static function crm_survey_endpoint() |
| 330 |
{ |
| 331 |
$crm_feedback_endpoint_url = 'https://bo.jeweltheme.com/wp-json/jlt-api/v1/survey'; // Endpoint . |
| 332 |
$crm_feedback_endpoint = apply_filters('jltwp_adminify_crm_crm_endpoint', $crm_feedback_endpoint_url); |
| 333 |
|
| 334 |
return trailingslashit($crm_feedback_endpoint); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Function jltwp_adminify_wp_timezone_string() compability for wp version < 5.3 |
| 339 |
* |
| 340 |
* @return string |
| 341 |
*/ |
| 342 |
public static function jltwp_adminify_wp_timezone_string() |
| 343 |
{ |
| 344 |
$timezone_string = get_option('timezone_string'); |
| 345 |
|
| 346 |
if ($timezone_string) { |
| 347 |
return $timezone_string; |
| 348 |
} |
| 349 |
|
| 350 |
$offset = (float) get_option('gmt_offset'); |
| 351 |
$hours = (int) $offset; |
| 352 |
$minutes = ($offset - $hours); |
| 353 |
|
| 354 |
$sign = ($offset < 0) ? '-' : '+'; |
| 355 |
$abs_hour = abs($hours); |
| 356 |
$abs_mins = abs($minutes * 60); |
| 357 |
$tz_offset = sprintf('%s%02d:%02d', $sign, $abs_hour, $abs_mins); |
| 358 |
|
| 359 |
return $tz_offset; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Get Merged Data |
| 364 |
* |
| 365 |
* @param [type] $data . |
| 366 |
* @param string $start_date . |
| 367 |
* @param string $end_data . |
| 368 |
* |
| 369 |
* @author Jewel Theme <support@jeweltheme.com> |
| 370 |
*/ |
| 371 |
public static function get_merged_data($data, $start_date = '', $end_data = '') |
| 372 |
{ |
| 373 |
$_data = shortcode_atts( |
| 374 |
array( |
| 375 |
'image_url' => WP_ADMINIFY_ASSETS_IMAGE . '/promo-image.png', |
| 376 |
'start_date' => $start_date, |
| 377 |
'end_date' => $end_data, |
| 378 |
'counter_time' => '', |
| 379 |
'is_campaign' => 'false', |
| 380 |
'button_text' => 'Get Premium', |
| 381 |
'button_url' => 'https://wpadminify.com/pricing', |
| 382 |
'btn_color' => '#CC22FF', |
| 383 |
'notice' => '', |
| 384 |
'notice_timestamp' => '', |
| 385 |
), |
| 386 |
$data |
| 387 |
); |
| 388 |
|
| 389 |
if (empty($_data['image_url'])) { |
| 390 |
$_data['image_url'] = WP_ADMINIFY_ASSETS_IMAGE . '/promo-image.png'; |
| 391 |
} |
| 392 |
|
| 393 |
return $_data; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* wp_kses attributes map |
| 399 |
* |
| 400 |
* @param array $attrs . |
| 401 |
* |
| 402 |
* @author Jewel Theme <support@jeweltheme.com> |
| 403 |
*/ |
| 404 |
public static function wp_kses_atts_map(array $attrs) |
| 405 |
{ |
| 406 |
return array_fill_keys(array_values($attrs), true); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Custom method |
| 411 |
* |
| 412 |
* @param [type] $content . |
| 413 |
* |
| 414 |
* @author Jewel Theme <support@jeweltheme.com> |
| 415 |
*/ |
| 416 |
public static function wp_kses_custom($content) |
| 417 |
{ |
| 418 |
$allowed_tags = wp_kses_allowed_html('post'); |
| 419 |
|
| 420 |
$custom_tags = array( |
| 421 |
'select' => self::wp_kses_atts_map(array('class', 'id', 'style', 'width', 'height', 'title', 'data', 'name', 'autofocus', 'disabled', 'multiple', 'required', 'size')), |
| 422 |
'input' => self::wp_kses_atts_map(array('class', 'id', 'style', 'width', 'height', 'title', 'data', 'name', 'autofocus', 'disabled', 'required', 'size', 'type', 'checked', 'readonly', 'placeholder', 'value', 'maxlength', 'min', 'max', 'multiple', 'pattern', 'step', 'autocomplete')), |
| 423 |
'textarea' => self::wp_kses_atts_map(array('class', 'id', 'style', 'width', 'height', 'title', 'data', 'name', 'autofocus', 'disabled', 'required', 'rows', 'cols', 'wrap', 'maxlength')), |
| 424 |
'option' => self::wp_kses_atts_map(array('class', 'id', 'label', 'disabled', 'label', 'selected', 'value')), |
| 425 |
'optgroup' => self::wp_kses_atts_map(array('disabled', 'label', 'class', 'id')), |
| 426 |
'form' => self::wp_kses_atts_map(array('class', 'id', 'data', 'style', 'width', 'height', 'accept-charset', 'action', 'autocomplete', 'enctype', 'method', 'name', 'novalidate', 'rel', 'target')), |
| 427 |
'svg' => self::wp_kses_atts_map(array('class', 'xmlns', 'viewbox', 'width', 'height', 'fill', 'aria-hidden', 'aria-labelledby', 'role')), |
| 428 |
'rect' => self::wp_kses_atts_map(array('rx', 'width', 'height', 'fill')), |
| 429 |
'path' => self::wp_kses_atts_map(array('d', 'fill')), |
| 430 |
'g' => self::wp_kses_atts_map(array('fill')), |
| 431 |
'defs' => self::wp_kses_atts_map(array('fill')), |
| 432 |
'linearGradient' => self::wp_kses_atts_map(array('id', 'x1', 'x2', 'y1', 'y2', 'gradientUnits')), |
| 433 |
'stop' => self::wp_kses_atts_map(array('stop-color', 'offset', 'stop-opacity')), |
| 434 |
'style' => self::wp_kses_atts_map(array('type')), |
| 435 |
'div' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 436 |
'ul' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 437 |
'li' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 438 |
'label' => self::wp_kses_atts_map(array('class', 'for')), |
| 439 |
'span' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 440 |
'h1' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 441 |
'h2' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 442 |
'h3' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 443 |
'h4' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 444 |
'h5' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 445 |
'h6' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 446 |
'a' => self::wp_kses_atts_map(array('class', 'href', 'target', 'rel')), |
| 447 |
'p' => self::wp_kses_atts_map(array('class', 'id', 'style', 'data')), |
| 448 |
'table' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 449 |
'thead' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 450 |
'tbody' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 451 |
'tr' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 452 |
'th' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 453 |
'td' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 454 |
'i' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 455 |
'button' => self::wp_kses_atts_map(array('class', 'id')), |
| 456 |
'nav' => self::wp_kses_atts_map(array('class', 'id', 'style')), |
| 457 |
'time' => self::wp_kses_atts_map(array('datetime')), |
| 458 |
'br' => array(), |
| 459 |
'strong' => array(), |
| 460 |
'style' => array(), |
| 461 |
'img' => self::wp_kses_atts_map(array('class', 'src', 'alt', 'height', 'width', 'srcset', 'id', 'loading')), |
| 462 |
); |
| 463 |
|
| 464 |
$allowed_tags = array_merge_recursive($allowed_tags, $custom_tags); |
| 465 |
|
| 466 |
return wp_kses(stripslashes_deep($content), $allowed_tags); |
| 467 |
} |
| 468 |
} |
| 469 |
|