Created
January 10, 2019 11:39
-
-
Save panayotoff/2e6e7bad23128b4756a99d5f4ff97537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Return the asset path as variable | |
* */ | |
function get_asset($url) | |
{ | |
return get_template_directory_uri() . '/assets/' . $url; | |
} | |
/* | |
* Prints the asset url | |
* */ | |
function the_asset($url) | |
{ | |
echo asset($url); | |
} | |
/** | |
* Get image url | |
*/ | |
function get_image($url){ | |
return get_asset('images/' . $url); | |
} | |
function the_image($url){ | |
echo get_image($url); | |
} | |
/** | |
* Help pritty print variable | |
*/ | |
function pre($var){ | |
$styles = [ | |
'display:block', | |
'padding:1em', | |
'margin:1em', | |
'border-radius:3px', | |
'font-size:14px', | |
'font-family: monospace', | |
'white-space: pre-wrap', | |
'word-wrap: break-word', | |
'overflow-x:auto', | |
'color: #004267', | |
'border:1px solid #00426733', | |
'box-shadow: 0 1px 1px rgba(0,0,0,0.08)', | |
'line-height:1.5em', | |
'text-align:left', | |
'background-image: linear-gradient(180deg, #eaeaea 50%, #fff 50%)', | |
'background-size: 100% 3em', // Double the line height | |
'background-position: 0 1em;' // Offset by top padding | |
]; | |
echo sprintf('<pre style="%s">%s</pre>', | |
implode(';', $styles), | |
var_export($var, true) | |
); | |
} | |
/** | |
* Get srcset for image | |
*/ | |
function get_image_srcset($image_id, $image_size){ | |
if(!empty($image_id)) { | |
$image_src = wp_get_attachment_image_url( $image_id, $image_size); | |
$image_srcset = wp_get_attachment_image_srcset( $image_id, $image_size ); | |
return sprintf('src="%1$s" srcset="%2$s"', $image_src, $image_srcset); | |
} | |
} | |
/** | |
* Helper to get srcset for image to use with lazyload lib | |
*/ | |
function get_image_lazyload_srcset($image_id, $image_size, $max_width){ | |
if(!empty($image_id)) { | |
// default size | |
$image_src = wp_get_attachment_image_url( $image_id, 'medium' ); | |
$image_srcset = wp_get_attachment_image_srcset( $image_id, $image_size ); | |
return sprintf('src="%1$s" data-srcset="%2$s" data-sizes=(max-width: %3$s) 100vw, %3$s', | |
$image_src, | |
$image_srcset, | |
$max_width | |
); | |
} | |
} | |
/** | |
* Add options ( data-options='{"key":"val"}' ) | |
*/ | |
function get_data_options($options_arr){ | |
if(!$options_arr || !count($options_arr)){ | |
return ''; | |
} | |
return sprintf("data-options='%s'", json_encode($options_arr)); | |
} | |
function the_data_options($options_arr){ | |
echo get_data_options($options_arr); | |
} | |
/** | |
* Is doing front-end ajax | |
*/ | |
function request_is_frontend_ajax() | |
{ | |
$script_filename = isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : ''; | |
//Try to figure out if frontend AJAX request... If we are DOING_AJAX; let's look closer | |
if((defined('DOING_AJAX') && DOING_AJAX)){ | |
//From wp-includes/functions.php, wp_get_referer() function. | |
//Required to fix: https://core.trac.wordpress.org/ticket/25294 | |
$ref = ''; | |
if ( ! empty( $_REQUEST['_wp_http_referer'] ) ){ | |
$ref = wp_unslash( $_REQUEST['_wp_http_referer'] ); | |
} elseif ( ! empty( $_SERVER['HTTP_REFERER'] ) ){ | |
$ref = wp_unslash( $_SERVER['HTTP_REFERER'] ); | |
} | |
//If referer does not contain admin URL and we are using the admin-ajax.php endpoint, this is likely a frontend AJAX request | |
if(((strpos($ref, admin_url()) === false) && (basename($script_filename) === 'admin-ajax.php'))){ | |
return true; | |
} | |
} | |
//If no checks triggered, we end up here - not an AJAX request. | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment