Skip to content

Instantly share code, notes, and snippets.

@camaleaun
Last active January 26, 2019 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camaleaun/a9ab8de21dd9830708f927b6db69c021 to your computer and use it in GitHub Desktop.
Save camaleaun/a9ab8de21dd9830708f927b6db69c021 to your computer and use it in GitHub Desktop.
Formatting functions
/**
* Extract full name to first and last name.
*
* @version 1.0.1
* @link https://gist.github.com/camaleaun/a9ab8de21dd9830708f927b6db69c021
* @param string $name Full name.
* @param string $object Optional. If true return as OBJECT (with 'first' and 'last' as properties) (Default ARRAY_N).
* @return array|object First and last names.
*/
function pluginname_split_name( $name, $object = false ) {
$name = trim( $name );
$last_name = '';
if ( strpos( $name, ' ' ) != false ) {
preg_match( '#\s([^$]+)$#', $name, $last_name );
$last_name = $last_name[1];
}
$first_name = trim( preg_replace( '#' . $last_name . '$#', '', $name ) );
if ( ! $object ) {
$full_name = array( $first_name, $last_name );
} else {
$full_name = (object) array(
'first' => $first_name,
'last' => $last_name,
);
}
return apply_filters( 'pluginname_split_name', $full_name );
}
function pluginname_svg_kses( $name ) {
$origin = $svg;
$svg = preg_replace( '/\r|\n/', '', $svg );
$svg = preg_replace( '/(\>)\s*(\<)/m', '$1$2', $svg );
$svg = str_replace( ' viewBox=', ' viewbox=', $svg );
$allowed = array(
'svg' => array(
'xmlns' => array(),
'viewbox' => array(),
),
'path' => array(
'fill' => array(),
'd' => array(),
),
);
$svg = wp_kses( $svg, $allowed );
$svg = str_replace( ' viewbox=', ' viewBox=', $svg );
$sanitized = $svg;
return apply_filters( 'pluginname_svg_kses', $sanitized, $origin, $allowed );
}
/**
* Adapted from part of 'wc_create_new_customer' (WooCommerce function)
*/
function pluginname_username_by_email( $email ) {
$username = sanitize_user( current( explode( '@', $email ) ), true );
// Ensure username is unique.
$append = 1;
$o_username = $username;
while ( username_exists( $username ) ) {
$username = $o_username . $append;
$append++;
}
return $username;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment