PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.0
Defender Security – Malware Scanner, Login Security & Firewall v6.2.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / traits / array-utils.php

array-utils.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.0, at src/traits/array-utils.php

92 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Array utility methods.
4 *
5 * @since 5.4.0
6 * @package WP_Defender
7 */
8
9 namespace WP_Defender\Traits;
10
11 /**
12 * Trait for array utility methods.
13 */
14 trait Array_Utils {
15
16 /**
17 * Deep comparison of two arrays that can handle nested arrays and objects.
18 *
19 * @param mixed $old_data Old data to compare.
20 * @param mixed $new_data New data to compare.
21 *
22 * @return bool True if arrays differ, false if they are the same.
23 */
24 protected function arrays_differ_deeply( $old_data, $new_data ) {
25 // If types are different, they differ.
26 if ( gettype( $old_data ) !== gettype( $new_data ) ) {
27 return true;
28 }
29
30 // Handle non-array types.
31 if ( ! is_array( $old_data ) ) {
32 return $old_data !== $new_data;
33 }
34
35 // Handle arrays - normalize and compare.
36 $old_normalized = $this->normalize_array( $old_data );
37 $new_normalized = $this->normalize_array( $new_data );
38
39 // Compare array sizes first.
40 if ( count( $old_normalized ) !== count( $new_normalized ) ) {
41 return true;
42 }
43
44 // Compare each key-value pair.
45 foreach ( $old_normalized as $key => $value ) {
46 if ( ! array_key_exists( $key, $new_normalized ) ) {
47 return true;
48 }
49
50 if ( $this->arrays_differ_deeply( $value, $new_normalized[ $key ] ) ) {
51 return true;
52 }
53 }
54
55 return false;
56 }
57
58 /**
59 * Normalize array for comparison by sorting arrays and handling special cases.
60 *
61 * @param array $data_array Array to normalize.
62 *
63 * @return array Normalized array.
64 */
65 protected function normalize_array( $data_array ) {
66 if ( ! is_array( $data_array ) ) {
67 return $data_array;
68 }
69
70 $normalized = array();
71
72 foreach ( $data_array as $key => $value ) {
73 if ( is_array( $value ) ) {
74 // For indexed arrays (like preset values), sort to handle order differences.
75 if ( array_keys( $value ) === range( 0, count( $value ) - 1 ) ) {
76 sort( $value );
77 }
78 $normalized[ $key ] = $this->normalize_array( $value );
79 } else {
80 $normalized[ $key ] = $value;
81 }
82 }
83
84 // Sort associative arrays by key for consistent comparison.
85 if ( array() !== $normalized ) {
86 ksort( $normalized );
87 }
88
89 return $normalized;
90 }
91 }
92