Skip to content

Commit

Permalink
Added using $args as string.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallookoo committed Nov 16, 2022
1 parent b42119c commit 7a828ea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"authors": [
{
"name": "Sergio (kallookoo)",
"email": "sergio@dsergio.com"
"email": "kallookoo@gmail.com"
}
],
"autoload": {
Expand Down
42 changes: 31 additions & 11 deletions src/wp-parse-args-recursive.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
<?php // phpcs:disable WooCommerce.Commenting.CommentTags
/**
* Like wp_parse_args but supports recursivity
* By default converts the returned type based on the $args and $defaults
*
* @author Sergio (kallookoo) <sergio@dsergio.com>
* @author Sergio (kallookoo) <kallookoo@gmail.com>
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL2 or later
* @version 1.2
* @version 1.3.0
*
* @package dsergio\com\WordPress\Helpers
*/
Expand All @@ -15,17 +15,34 @@
* Like wp_parse_args but supports recursivity
* By default converts the returned type based on the $args and $defaults
*
* @param array|object $args Values to merge with $defaults.
* @param array|object $defaults Array, Object that serves as the defaults or string.
* @param boolean $preserve_type Optional. Convert output array into object if $args or $defaults if it is. Default true.
* @param boolean $preserve_integer_keys Optional. If given, integer keys will be preserved and merged instead of appended.
* @param string|array|object $args Values to merge with $defaults.
* @param array|object $defaults Optional. Array or Object that serves as the defaults.
* Default empty array.
* @param boolean $preserve_type Optional. Convert output array into object if $args or $defaults if it is.
* Default true.
* @param boolean $preserve_integer_keys Optional. If given, integer keys will be preserved and merged instead of appended.
* Default false.
*
* @return array|object $output Merged user defined values with defaults.
* @return array|object $output Merged user defined values with defaults.
*/
function wp_parse_args_recursive( $args, $defaults, $preserve_type = true, $preserve_integer_keys = false ) {
function wp_parse_args_recursive( $args, $defaults = array(), $preserve_type = true, $preserve_integer_keys = false ) {
$output = array();
if ( is_string( $args ) ) {
parse_str( $args, $parsed_args );
if ( $defaults && ( is_array( $defaults ) || is_object( $defaults ) ) ) {
return wp_parse_args_recursive( $parsed_args, $defaults, $preserve_type, $preserve_integer_keys );
}
return $parsed_args;
}

foreach ( array( $defaults, $args ) as $list ) {
foreach ( (array) $list as $key => $value ) {
$parsed_args = array();
if ( is_array( $list ) ) {
$parsed_args =& $args;
} elseif ( is_object( $list ) ) {
$parsed_args = get_object_vars( $list );
}
foreach ( $parsed_args as $key => $value ) {
if ( is_integer( $key ) && ! $preserve_integer_keys ) {
$output[] = $value;
} elseif (
Expand All @@ -39,6 +56,9 @@ function wp_parse_args_recursive( $args, $defaults, $preserve_type = true, $pres
}
}
}
return ( $preserve_type && ( is_object( $args ) || is_object( $defaults ) ) ) ? (object) $output : $output;
if ( $preserve_type && ( is_object( $args ) || is_object( $defaults ) ) ) {
$output = (object) $output;
}
return $output;
}
}

0 comments on commit 7a828ea

Please sign in to comment.