Last active
January 7, 2021 01:55
-
-
Save chrisgoddard/be4d92ad778e1f9f2856 to your computer and use it in GitHub Desktop.
WordPress Utility Functions
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 | |
/* | |
* @name: WordPress utility functions | |
* @desc: Useful utility functions for WordPress | |
* @author: https://github.com/chrisgoddard | |
*/ | |
/* | |
* Func: Logger | |
* @desc: for logging events within program lifecycle | |
*/ | |
if(!function_exists('logger')){ | |
function logger($input = NULL) | |
{ | |
$backtrace = debug_backtrace(); | |
if (!$input) $input = ''; | |
if (is_string($input)): | |
$log = $input; | |
else: | |
$log = print_r($input, true); | |
endif; | |
error_log($backtrace[0]['file']." - ".$backtrace[1]['function']." - line:".$backtrace[0]['line']."\n".$log); | |
} | |
} | |
/* | |
* Register Script with auto-versioning | |
*/ | |
if(!function_exists('wputil_register_script')){ | |
function wputil_register_script($slug, $rel_file, Array $deps = array(), $enqueue = false, $footer = true ) | |
{ | |
$rel_file = wputil_unleadingslashit($rel_file); | |
wp_register_script( $slug, trailingslashit( get_template_directory_uri() ) . $rel_file, $deps, filemtime( trailingslashit( get_template_directory() ) . $rel_file), $footer ); | |
if( $enqueue ){ | |
wp_enqueue_script( $slug ); | |
} | |
return $slug; | |
} | |
} | |
/* | |
* Register Style with auto-versioning | |
*/ | |
if(!function_exists('wputil_register_style')){ | |
function wputil_register_style($slug, $rel_file, Array $deps = array(), $enqueue = false, $media = null) | |
{ | |
$rel_file = wputil_unleadingslashit($rel_file); | |
wp_register_style($slug, trailingslashit( get_template_directory_uri() ) . $rel_file, $deps, filemtime( trailingslashit( get_template_directory() ) . $rel_file ), $media ); | |
if( $enqueue ){ | |
wp_enqueue_style( $slug ); | |
} | |
return $slug; | |
} | |
} | |
// opposite of Built-in WP functions for trailing slashes | |
if(!function_exists('wputil_leadingslashit')){ | |
function wputil_leadingslashit($string) { | |
return '/' . unleadingslashit($string); | |
} | |
} | |
/* | |
* remove leading slash from string | |
*/ | |
if(!function_exists('wputil_unleadingslashit')){ | |
function wputil_unleadingslashit($string) { | |
return ltrim($string, '/'); | |
} | |
} | |
/* | |
* Set a theme include directory | |
*/ | |
global $_wputil_include_directory; | |
$_wputil_include_directory = 'inc/'; | |
if(!function_exists('wputil_set_include_directory')){ | |
function wputil_set_include_directory($dir, $base = false) | |
{ | |
global $_wputil_include_directory; | |
if(!$base){ | |
$_wputil_include_directory = trailingslashit( get_template_directory() ) . trailingslashit(wputil_unleadingslashit($dir)); | |
} else { | |
$_wputil_include_directory = trailingslashit($base) . trailingslashit(wputil_unleadingslashit($dir)); | |
} | |
return $_wputil_include_directory; | |
} | |
} | |
/* | |
* Load an include from include directory | |
*/ | |
if(!function_exists('wputil_include')){ | |
function wputil_include($file) | |
{ | |
global $_wputil_include_directory; | |
if($_wputil_include_directory){ | |
include trailingslashit( $_wputil_include_directory ) . wputil_unleadingslashit($file); | |
} else { | |
include trailingslashit( get_template_directory() ) . wputil_unleadingslashit($file); | |
} | |
} | |
} | |
/* | |
* returns WordPress subdirectory if applicable | |
*/ | |
if(!function_exists('wputil_base_dir')){ | |
function wputil_base_dir() { | |
preg_match('!(https?://[^/|"]+)([^"]+)?!', site_url(), $matches); | |
if (count($matches) === 3) { | |
return end($matches); | |
} else { | |
return ''; | |
} | |
} | |
} | |
/* | |
* Slugfiy string | |
*/ | |
if(!function_exists('wputil_slugify')){ | |
function wputil_slugify($input, $del = '-') | |
{ | |
return strtolower(str_replace(' ', $del, $input)); | |
} | |
} | |
/* | |
* Convert Hexidecimal color to rgb | |
*/ | |
if(!function_exists('wputil_hex2rgb')){ | |
function wputil_hex2rgb($hex, $string = false) { | |
$hex = str_replace("#", "", $hex); | |
if(strlen($hex) == 3) { | |
$r = hexdec(substr($hex,0,1).substr($hex,0,1)); | |
$g = hexdec(substr($hex,1,1).substr($hex,1,1)); | |
$b = hexdec(substr($hex,2,1).substr($hex,2,1)); | |
} else { | |
$r = hexdec(substr($hex,0,2)); | |
$g = hexdec(substr($hex,2,2)); | |
$b = hexdec(substr($hex,4,2)); | |
} | |
$rgb = array($r, $g, $b); | |
if($string){ | |
return implode(",", $rgb); | |
} else { | |
return $rgb; | |
} | |
} | |
} | |
/* | |
* Convert RGB to hexideximal color | |
*/ | |
if(!function_exists('wputil_rgb2hex')){ | |
function wputil_rgb2hex($rgb) { | |
$hex = "#"; | |
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT); | |
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT); | |
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT); | |
return $hex; // returns the hex value including the number sign (#) | |
} | |
} | |
/* | |
* Calculate text color from background color | |
*/ | |
if(!function_exists('wputil_font_color_from_background')){ | |
function wputil_font_color_from_background($hexcolor, $light = '#ffffff', $dark = '#000000', $threshold = 130){ | |
$rgbcolor = wputil_hex2rgb($hexcolor); | |
$brightness = sqrt( .299 * pow($rgbcolor[0], 2) + .587 * pow($rgbcolor[1], 2) + .114 * pow($rgbcolor[2], 2) ); | |
if($brightness < $threshold){ | |
return $light; | |
} else { | |
return $dark; | |
} | |
} | |
} | |
if(!function_exists('wputil_admin_debug')){ | |
function wputil_admin_debug() | |
{ | |
if (current_user_can( 'manage_options' )) { | |
echo '<pre>'; | |
$out = ( count(func_get_args()) > 1 ) ? func_get_args() : func_get_arg(0) ; | |
print_r( $out ); | |
echo '</pre>'; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment