/** * Makes from an array of arrays a flat array. * * @param array $array the arra to flatten * @return array flattenned array */ function gp_array_flatten($array) { $res = array(); foreach ($array as $value) { $res = array_merge($res, is_array($value) ? gp_array_flatten($value) : array($value)); } return $res; }
/** * Joins paths, and takes care of slashes between them * * Example: gp_url_join( '/project', array( 'wp', 'dev) ) -> '/project/wp/dev' * * The function will keep leading and trailing slashes of the whole URL, but won't * allow more than consecutive slash inside. * * @param mixed components... arbitrary number of string or path components * @return string URL, built of all the components, separated with / */ function gp_url_join() { $components = func_get_args(); $components_in_flat_array = array_filter(gp_array_flatten($components)); $components_with_slashes = implode('/', $components_in_flat_array); $components_without_consecutive_slashes = preg_replace('|/{2,}|', '/', $components_with_slashes); $components_without_consecutive_slashes = str_replace(array('http:/', 'https:/'), array('http://', 'https://'), $components_without_consecutive_slashes); return $components_without_consecutive_slashes; }
/** * Joins paths, and takes care of slashes between them * * Example: gp_url_join( '/project', array( 'wp', 'dev) ) -> '/project/wp/dev' * * The function will keep leading and trailing slashes of the whole URL, but won't * allow more than consecutive slash inside. * * @param mixed components... arbitrary number of string or path components * @return string URL, built of all the components, separated with / */ function gp_url_join() { $components = func_get_args(); $components_in_flat_array = array_filter(gp_array_flatten($components)); $components_with_slashes = implode('/', $components_in_flat_array); // Make sure all instances of the final URL are returned with a proper permalink ending. $components_with_slashes = user_trailingslashit($components_with_slashes); $components_without_consecutive_slashes = preg_replace('|/{2,}|', '/', $components_with_slashes); $components_without_consecutive_slashes = str_replace(array('http:/', 'https:/'), array('http://', 'https://'), $components_without_consecutive_slashes); return $components_without_consecutive_slashes; }
/** * Joins paths, and takes care of slashes between them */ function gp_url_join() { $args = func_get_args(); // we need array_values() in order to make sure the indices of $args are consecutive from 0 to count()-1 $args = array_values(array_filter(gp_array_flatten($args))); if (empty($args)) { return ''; } $start_slash = gp_startswith($args[0], '/') && trim($args[0], '/') != '' ? '/' : ''; $end_slash = gp_endswith($args[count($args) - 1], '/') && trim($args[count($args) - 1], '/') != '' ? '/' : ''; $args = array_map(create_function('$x', 'return trim($x, "/");'), $args); return $start_slash . implode('/', $args) . $end_slash; }
function gp_breadcrumb($breadcrumb = null, $args = array()) { $defaults = array('separator' => '<span class="separator">' . _x('→', 'breadcrumb') . '</span>', 'breadcrumb-template' => '<span class="breadcrumb">{separator}{breadcrumb}</span>'); $args = array_merge($defaults, $args); if (!is_null($breadcrumb)) { $breadcrumb = gp_array_flatten($breadcrumb); $breadcrumb_string = implode($args['separator'], array_filter($breadcrumb)); $whole_breadcrumb = str_replace('{separator}', $args['separator'], $args['breadcrumb-template']); $whole_breadcrumb = str_replace('{breadcrumb}', $breadcrumb_string, $whole_breadcrumb); add_filter('gp_breadcrumb', function () use($whole_breadcrumb) { return $whole_breadcrumb; }, 5); } else { return apply_filters('gp_breadcrumb', ''); } }
function test_gp_array_flatten() { $this->assertEquals(array(), gp_array_flatten(array())); $this->assertEquals(array(1, 2, 3), gp_array_flatten(array(1, array(2, 3)))); $this->assertEquals(array(1, 2, 3, 4, 5, 6, 7), gp_array_flatten(array(1, array(2, array(3, 4), 5), 6, array(7)))); }
function gp_breadcrumb($breadcrumb = null, $args = array()) { if ($breadcrumb) { $breadcrumb = gp_array_flatten($breadcrumb); add_filter('gp_breadcrumb_items', function ($breadcrumbs) use($breadcrumb) { return array_merge($breadcrumbs, $breadcrumb); }, 1); } else { $breadcrumbs = apply_filters('gp_breadcrumb_items', array()); if ($breadcrumbs) { $defaults = array('before' => '<li>', 'after' => '</li>', 'breadcrumb-template' => '<ul class="breadcrumb">{breadcrumb}</ul>'); $args = array_merge($defaults, $args); $whole_breadcrumb = ''; foreach ($breadcrumbs as $breadcrumb) { $whole_breadcrumb .= $args['before'] . $breadcrumb . $args['after']; } $whole_breadcrumb = str_replace('{breadcrumb}', $whole_breadcrumb, $args['breadcrumb-template']); return apply_filters('gp_breadcrumb', $whole_breadcrumb); } } }
function gp_breadcrumb($breadcrumb = null, $args = array()) { if ($breadcrumb) { $breadcrumb = gp_array_flatten($breadcrumb); add_filter('gp_breadcrumb_items', function ($breadcrumbs) use($breadcrumb) { return array_merge($breadcrumbs, $breadcrumb); }, 1); } else { /** * Filter the list of breadcrumb navigation items. * * @since 1.0.0 * * @param array $breadcrumb_items Breadcrumb items as HTML string. */ $breadcrumbs = apply_filters('gp_breadcrumb_items', array()); if ($breadcrumbs) { $defaults = array('before' => '<li>', 'after' => '</li>', 'breadcrumb-template' => '<ul class="breadcrumb">{breadcrumb}</ul>'); $args = array_merge($defaults, $args); $whole_breadcrumb = ''; foreach ($breadcrumbs as $breadcrumb) { $whole_breadcrumb .= $args['before'] . $breadcrumb . $args['after']; } $whole_breadcrumb = str_replace('{breadcrumb}', $whole_breadcrumb, $args['breadcrumb-template']); /** * Filter the breadcrumb HTML output. * * @since 1.0.0 * * @param string $whole_breadcrumb Breadcrumb HTML. */ return apply_filters('gp_breadcrumb', $whole_breadcrumb); } } }