Skip to content

Commit ec5a2ac

Browse files
committed
Moved array_merge_recursive_distinct() from Plugin to Utils class
1 parent 5b0a961 commit ec5a2ac

2 files changed

Lines changed: 44 additions & 30 deletions

File tree

app/Plugin.php

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,20 @@ public function is_ajax() {
109109
return defined('DOING_AJAX') && DOING_AJAX;
110110
}
111111

112+
/**
113+
* A wrapper for the plugin's data fiala prefix as defined in $settings
114+
*
115+
* @return string Prefix
116+
*/
117+
public function prefix( $field_name = null ) {
118+
if( $field_name ) {
119+
return self::$prefix . $field_name;
120+
}
121+
return self::$prefix;
122+
}
123+
112124
/**
113-
* Wrapper for phpversion() and version_compare()
125+
* Wrapper for phpversion() and version_compare(), intended for legacy support.
114126
*
115127
* @return bool
116128
*/
@@ -181,33 +193,4 @@ public function get_script_url($script, $return_minified = false) {
181193
return $this->get_script_path($script, $return_minified, true);
182194
}
183195

184-
/**
185-
* Merges two array, eliminating duplicates
186-
*
187-
* array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
188-
* Matching keys' values in the second array overwrite those in the first array, as is the
189-
* case with array_merge().
190-
*
191-
* @param array $array1
192-
* @param array $array2
193-
* @return array
194-
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
195-
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
196-
*/
197-
private function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
198-
// Credit: http://php.net/manual/en/function.array-merge-recursive.php#92195
199-
$merged = $array1;
200-
201-
foreach ( $array2 as $key => &$value )
202-
{
203-
if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) ) {
204-
$merged [$key] = self::array_merge_recursive_distinct ( $merged [$key], $value );
205-
} else {
206-
$merged [$key] = $value;
207-
}
208-
}
209-
210-
return $merged;
211-
}
212-
213196
}

app/Utils.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,37 @@ public static function show_notice($msg, $type = 'error', $is_dismissible = fals
2424

2525
}
2626

27+
/**
28+
* Merges two array, eliminating duplicates
29+
*
30+
* array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
31+
* Matching keys' values in the second array overwrite those in the first array, as is the
32+
* case with array_merge().
33+
*
34+
* @param array $array1
35+
* @param array $array2
36+
* @return array
37+
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
38+
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
39+
* @see http://php.net/manual/en/function.array-merge-recursive.php#92195 Source
40+
*/
41+
private function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
42+
43+
$merged = $array1;
44+
45+
foreach ( $array2 as $key => &$value )
46+
{
47+
if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] ) ) {
48+
$merged [$key] = self::array_merge_recursive_distinct ( $merged [$key], $value );
49+
} else {
50+
$merged [$key] = $value;
51+
}
52+
}
53+
54+
return $merged;
55+
56+
}
57+
2758
/**
2859
* Get the slug of the current page/post
2960
*

0 commit comments

Comments
 (0)