Skip to content

Instantly share code, notes, and snippets.

@felds
Created September 7, 2016 20:58
Show Gist options
  • Select an option

  • Save felds/f5954e4e9677cf63ff707fc903dc6c17 to your computer and use it in GitHub Desktop.

Select an option

Save felds/f5954e4e9677cf63ff707fc903dc6c17 to your computer and use it in GitHub Desktop.
Wordpress Utils
<?php
/**
* Return `true` if any of the items evals to `true`, `false` otherwise
*
* @param Array $list The list to be evaluated
* @return bool
*/
function all($list)
{
return count(array_filter($list)) === count($list);
}
if (WP_DEBUG) {
assert(all([ 0, 0, 0, 1, 0 ]) === false);
assert(all([ 0, 0, 0, 0, 0 ]) === false);
assert(all([ 1, 1, 1, 1, true ]) === true);
}
/**
* Return `true` if any of the items evals to `true`, `false` otherwise
*
* @param Array $list The list to be evaluated
* @return bool
*/
function any($list)
{
return (bool) array_filter($list);
}
if (WP_DEBUG) {
assert(any([ 0, 0, 0, 1, 0 ]) === true);
assert(any([ 0, 0, 0, 0, 0 ]) === false);
}
/**
* Remove the schema from an URL
*
* @param string $url
* @return string
* @link https://tools.ietf.org/html/rfc2396#appendix-B
*/
function rm_scheme($url)
{
return preg_replace('~^[^:/?#]+:(//)?~', '', $url);
}
/**
* Return a template asset URL
*
* @param string
* @return string
*/
function asset($path_from_template)
{
return get_template_directory_uri() . '/' . $path_from_template;
}
/**
* Return a responsive <img> tag for a giver attachment id
*
* @param int $id
* @param string $size The size of the image as configured on functions.php
* @param bool|string If string, use it as alt; if false, don't use a alt; if true, use the attachment alt
* @return string|null The image tag on success; Null otherwise
*/
function img_tag($id, $size = 'medium', $alt = true)
{
if (!wp_attachment_is_image($id))
return;
$src = wp_get_attachment_image_url($id, $size);
$srcset = wp_get_attachment_image_srcset($id, $size);
if ($alt === true) {
$alt = get_post_meta($id, '_wp_attachment_image_alt', true);
} elseif ($alt === false) {
$alt = "";
}
return sprintf(
'<img src="%s" srcset="%s" alt="%s" />',
esc_url($src),
esc_attr($srcset),
esc_attr($alt)
);
}
/**
* @return array
*/
function na_get_external_link_types()
{
$types = [
// 'phone' => "Telefone",
'link' => "Website",
'email' => "Email",
'location' => "Mapa",
'google-plus' => "Google+",
'facebook' => "Facebook",
'instagram' => "Instagram",
'twitter' => "Twitter",
'vine' => "Vine",
'youtube' => "YouTube",
'vimeo' => "Vimeo",
'pinterest' => "Pinterest",
];
return $types;
}
/**
* @param string $name The icon name
* @param string $caption The screen-readable caption
* @return An SVG use tag with screen-readable caption
*/
function icomoon_tag($name, $caption = null, $class = "icon")
{
$name = esc_attr($name);
$caption_html = $caption ? "<span class=\"sr-only\">{$caption}</span>" : '';
return "<svg class=\"{$class}\"><use xlink:href=\"#{$name}\"></use>{$caption}</svg>";
}
/**
* Get the archive title
*
* @return string|null
*/
function get_archive_title()
{
if (!is_archive())
return null;
$qo = get_queried_object();
if (is_author())
return $qo->get('display_name');
if (is_date())
return get_the_date();
return $qo->name;
}
/**
* Get the items for a menu as an array by the menu's location
*
* @param string $theme_location The location of the menu
* @return array|null The menu items on success; Null otherwise
*/
function get_menu_items_by_location($theme_location)
{
if (!$theme_location)
return;
$locations = get_nav_menu_locations();
if (!isset($locations[$theme_location]))
return;
$items = wp_get_nav_menu_items($locations[$theme_location]);
if ($items === false)
return;
return $items;
}
/**
* Get the contents of a template part
*
* @return string
*/
function get_template_part_contents($slug, $name = null)
{
ob_start();
get_template_part($slug, $name);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
/**
* Get the URL for the post thumb
* @return null|string
*/
function get_post_thumbnail_url($size = 'post-thumbnail', $post_id = null)
{
if (!has_post_thumbnail()) return;
$image_id = get_post_thumbnail_id($post_id);
$image_info = wp_get_attachment_image_src($image_id, $size);
return esc_url($image_info[0]);
}
/**
* Select the items in $input which the keys are in $keys
* @return array
*/
function array_pick_keys(array $input, array $keys)
{
return array_intersect_key(
$input,
array_flip($keys)
);
}
/**
* Dump the data and die
*/
function dd(...$vars)
{
foreach ($vars as $var) {
if (function_exists('dump'))
dump($var);
else
var_dump($var);
}
die;
}
/**
* @param string $src The source of the symbol def
* @param string $caption The alt and title of the svg
* @param string $class The class of the SVG icon
* @return string The svg tag with an <use> statement
*/
function svg_use_tag($src, $caption, $class = "icon")
{
return "
<svg class=\"$class\" aria-labelledby=\"title\" aria-label=\"$caption\" alt=\"$caption\">
<title>$caption</title>
<use xlink:href=\"$src\"></use>
</svg>
";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment