Completed
Branch FET/network-option (6f0325)
by
unknown
15:04 queued 07:49
created
core/services/database/OptionEngine.php 1 patch
Indentation   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -13,75 +13,75 @@
 block discarded – undo
13 13
 class OptionEngine
14 14
 {
15 15
 
16
-    /**
17
-     * @var   bool
18
-     */
19
-    private $is_network_option;
16
+	/**
17
+	 * @var   bool
18
+	 */
19
+	private $is_network_option;
20 20
 
21
-    /**
22
-     * @var   int
23
-     */
24
-    private $network_ID;
21
+	/**
22
+	 * @var   int
23
+	 */
24
+	private $network_ID;
25 25
 
26 26
 
27
-    /**
28
-     * @param bool $is_network_option
29
-     */
30
-    public function __construct(bool $is_network_option = false)
31
-    {
32
-        $this->is_network_option = $is_network_option;
33
-        $this->network_ID        = get_current_network_id();
34
-    }
27
+	/**
28
+	 * @param bool $is_network_option
29
+	 */
30
+	public function __construct(bool $is_network_option = false)
31
+	{
32
+		$this->is_network_option = $is_network_option;
33
+		$this->network_ID        = get_current_network_id();
34
+	}
35 35
 
36 36
 
37
-    /**
38
-     * @param string $option_name
39
-     * @param mixed  $default_value
40
-     * @return false|mixed|void
41
-     */
42
-    public function getOption(string $option_name, $default_value)
43
-    {
44
-        return $this->is_network_option
45
-            ? get_network_option($this->network_ID, $option_name, $default_value)
46
-            : get_option($option_name, $default_value);
47
-    }
37
+	/**
38
+	 * @param string $option_name
39
+	 * @param mixed  $default_value
40
+	 * @return false|mixed|void
41
+	 */
42
+	public function getOption(string $option_name, $default_value)
43
+	{
44
+		return $this->is_network_option
45
+			? get_network_option($this->network_ID, $option_name, $default_value)
46
+			: get_option($option_name, $default_value);
47
+	}
48 48
 
49 49
 
50
-    /**
51
-     * @param string $option_name
52
-     * @param mixed  $value
53
-     * @param string $autoload
54
-     * @return bool
55
-     */
56
-    public function addOption(string $option_name, $value, string $autoload): bool
57
-    {
58
-        return $this->is_network_option
59
-            ? add_network_option($this->network_ID, $option_name, $value)
60
-            : add_option($option_name, $value, '', $autoload);
61
-    }
50
+	/**
51
+	 * @param string $option_name
52
+	 * @param mixed  $value
53
+	 * @param string $autoload
54
+	 * @return bool
55
+	 */
56
+	public function addOption(string $option_name, $value, string $autoload): bool
57
+	{
58
+		return $this->is_network_option
59
+			? add_network_option($this->network_ID, $option_name, $value)
60
+			: add_option($option_name, $value, '', $autoload);
61
+	}
62 62
 
63 63
 
64
-    /**
65
-     * @param string $option_name
66
-     * @param mixed  $value
67
-     * @return bool
68
-     */
69
-    public function updateOption(string $option_name, $value): bool
70
-    {
71
-        return $this->is_network_option
72
-            ? update_network_option($this->network_ID, $option_name, $value)
73
-            : update_option($option_name, $value);
74
-    }
64
+	/**
65
+	 * @param string $option_name
66
+	 * @param mixed  $value
67
+	 * @return bool
68
+	 */
69
+	public function updateOption(string $option_name, $value): bool
70
+	{
71
+		return $this->is_network_option
72
+			? update_network_option($this->network_ID, $option_name, $value)
73
+			: update_option($option_name, $value);
74
+	}
75 75
 
76 76
 
77
-    /**
78
-     * @param string $option_name
79
-     * @return bool
80
-     */
81
-    public function deleteOption(string $option_name): bool
82
-    {
83
-        return $this->is_network_option
84
-            ? delete_network_option($this->network_ID, $option_name)
85
-            : delete_option($option_name);
86
-    }
77
+	/**
78
+	 * @param string $option_name
79
+	 * @return bool
80
+	 */
81
+	public function deleteOption(string $option_name): bool
82
+	{
83
+		return $this->is_network_option
84
+			? delete_network_option($this->network_ID, $option_name)
85
+			: delete_option($option_name);
86
+	}
87 87
 }
Please login to merge, or discard this patch.
core/services/database/WordPressOption.php 1 patch
Indentation   +177 added lines, -177 removed lines patch added patch discarded remove patch
@@ -17,181 +17,181 @@
 block discarded – undo
17 17
  */
18 18
 abstract class WordPressOption
19 19
 {
20
-    const NOT_SET_YET = 'wordpress-option-value-not-yet-set';
21
-
22
-    /**
23
-     * WordPress makes it difficult to determine if an option successfully saved or not,
24
-     * which is sometimes really important to know, especially if the information you are saving is critical.
25
-     * The following options allow us to have a better chance of knowing when an update actually failed
26
-     * or when everything is OK but it just didn't update because the value hasn't changed.
27
-     */
28
-    const UPDATE_SUCCESS = 1;
29
-
30
-    const UPDATE_NONE    = 0;
31
-
32
-    const UPDATE_ERROR   = -1;
33
-
34
-    /**
35
-     * @var boolean
36
-     */
37
-    private $autoload = false;
38
-
39
-    /**
40
-     * @var mixed
41
-     */
42
-    private $default_value = null;
43
-
44
-    /**
45
-     * @var string
46
-     */
47
-    private $option_name = '';
48
-
49
-    /**
50
-     * @var mixed
51
-     */
52
-    private $value = WordPressOption::NOT_SET_YET;
53
-
54
-    /**
55
-     * @var OptionEngine
56
-     */
57
-    private $option_engine;
58
-
59
-
60
-    /**
61
-     * WordPressOption constructor.
62
-     *
63
-     * @param string $option_name
64
-     * @param mixed  $default_value
65
-     * @param bool   $autoload				if true, will load the option on EVERY request
66
-     * @param bool 	 $is_network_option		if true, will save the option to the network as opposed to the current blog
67
-     */
68
-    public function __construct(
69
-        string $option_name,
70
-        $default_value,
71
-        bool $autoload = false,
72
-        bool $is_network_option = false
73
-    ) {
74
-        $this->setAutoload($autoload);
75
-        $this->setDefaultValue($default_value);
76
-        $this->setOptionName($option_name);
77
-        $this->option_engine = new OptionEngine($is_network_option);
78
-    }
79
-
80
-
81
-    /**
82
-     * @param bool|string $autoload
83
-     */
84
-    public function setAutoload($autoload): void
85
-    {
86
-        $this->autoload = filter_var($autoload, FILTER_VALIDATE_BOOLEAN);
87
-    }
88
-
89
-
90
-    /**
91
-     * @param mixed $default_value
92
-     */
93
-    public function setDefaultValue($default_value): void
94
-    {
95
-        $this->default_value = $default_value;
96
-    }
97
-
98
-
99
-    /**
100
-     * @param string $option_name
101
-     */
102
-    public function setOptionName(string $option_name): void
103
-    {
104
-        $this->option_name = sanitize_key($option_name);
105
-    }
106
-
107
-
108
-    /**
109
-     * @return string
110
-     */
111
-    public function optionExists(): string
112
-    {
113
-        return $this->option_engine->getOption(
114
-            $this->option_name,
115
-            WordPressOption::NOT_SET_YET
116
-        ) !== WordPressOption::NOT_SET_YET;
117
-    }
118
-
119
-
120
-    /**
121
-     * @return string
122
-     */
123
-    public function getOptionName(): string
124
-    {
125
-        return $this->option_name;
126
-    }
127
-
128
-
129
-    /**
130
-     * @return false|mixed|void
131
-     */
132
-    public function loadOption()
133
-    {
134
-        if ($this->value === WordPressOption::NOT_SET_YET) {
135
-            $this->value = $this->option_engine->getOption($this->option_name, $this->default_value);
136
-        }
137
-        return $this->value;
138
-    }
139
-
140
-
141
-    /**
142
-     * @param $value
143
-     * @return int
144
-     */
145
-    public function updateOption($value): int
146
-    {
147
-        // don't update if value has not changed since last update
148
-        if ($this->valueIsUnchanged($value)) {
149
-            return WordPressOption::UPDATE_NONE;
150
-        }
151
-        $this->value = $value;
152
-        // because the options for updating differ when adding an option for the first time
153
-        // we use the WordPressOption::NOT_SET_YET to determine if things already exist in the db
154
-        $updated = $this->optionExists()
155
-            ? $this->option_engine->updateOption($this->option_name, $this->value)
156
-            : $this->option_engine->addOption($this->option_name, $this->value, $this->autoload());
157
-
158
-        if ($updated) {
159
-            return WordPressOption::UPDATE_SUCCESS;
160
-        }
161
-        return WordPressOption::UPDATE_ERROR;
162
-    }
163
-
164
-
165
-    private function valueIsUnchanged($value): bool
166
-    {
167
-        if (is_array($value) && is_array($this->value)) {
168
-            $diff = EEH_Array::array_diff_recursive($value, $this->value);
169
-            // $diff = array_diff($value, $this->value);
170
-            return empty($diff);
171
-        }
172
-        // emulate WP's method for checking equality
173
-        return $value === $this->value && maybe_serialize($value) === maybe_serialize($this->value);
174
-    }
175
-
176
-
177
-    /**
178
-     * @return string
179
-     */
180
-    private function autoload(): string
181
-    {
182
-        return $this->autoload ? 'yes' : 'no';
183
-    }
184
-
185
-
186
-    /**
187
-     * Deletes the option from the database
188
-     * for the rest of the request
189
-     *
190
-     * @return bool
191
-     * @since  $VID:$
192
-     */
193
-    public function deleteOption(): bool
194
-    {
195
-        return $this->option_engine->deleteOption($this->option_name);
196
-    }
20
+	const NOT_SET_YET = 'wordpress-option-value-not-yet-set';
21
+
22
+	/**
23
+	 * WordPress makes it difficult to determine if an option successfully saved or not,
24
+	 * which is sometimes really important to know, especially if the information you are saving is critical.
25
+	 * The following options allow us to have a better chance of knowing when an update actually failed
26
+	 * or when everything is OK but it just didn't update because the value hasn't changed.
27
+	 */
28
+	const UPDATE_SUCCESS = 1;
29
+
30
+	const UPDATE_NONE    = 0;
31
+
32
+	const UPDATE_ERROR   = -1;
33
+
34
+	/**
35
+	 * @var boolean
36
+	 */
37
+	private $autoload = false;
38
+
39
+	/**
40
+	 * @var mixed
41
+	 */
42
+	private $default_value = null;
43
+
44
+	/**
45
+	 * @var string
46
+	 */
47
+	private $option_name = '';
48
+
49
+	/**
50
+	 * @var mixed
51
+	 */
52
+	private $value = WordPressOption::NOT_SET_YET;
53
+
54
+	/**
55
+	 * @var OptionEngine
56
+	 */
57
+	private $option_engine;
58
+
59
+
60
+	/**
61
+	 * WordPressOption constructor.
62
+	 *
63
+	 * @param string $option_name
64
+	 * @param mixed  $default_value
65
+	 * @param bool   $autoload				if true, will load the option on EVERY request
66
+	 * @param bool 	 $is_network_option		if true, will save the option to the network as opposed to the current blog
67
+	 */
68
+	public function __construct(
69
+		string $option_name,
70
+		$default_value,
71
+		bool $autoload = false,
72
+		bool $is_network_option = false
73
+	) {
74
+		$this->setAutoload($autoload);
75
+		$this->setDefaultValue($default_value);
76
+		$this->setOptionName($option_name);
77
+		$this->option_engine = new OptionEngine($is_network_option);
78
+	}
79
+
80
+
81
+	/**
82
+	 * @param bool|string $autoload
83
+	 */
84
+	public function setAutoload($autoload): void
85
+	{
86
+		$this->autoload = filter_var($autoload, FILTER_VALIDATE_BOOLEAN);
87
+	}
88
+
89
+
90
+	/**
91
+	 * @param mixed $default_value
92
+	 */
93
+	public function setDefaultValue($default_value): void
94
+	{
95
+		$this->default_value = $default_value;
96
+	}
97
+
98
+
99
+	/**
100
+	 * @param string $option_name
101
+	 */
102
+	public function setOptionName(string $option_name): void
103
+	{
104
+		$this->option_name = sanitize_key($option_name);
105
+	}
106
+
107
+
108
+	/**
109
+	 * @return string
110
+	 */
111
+	public function optionExists(): string
112
+	{
113
+		return $this->option_engine->getOption(
114
+			$this->option_name,
115
+			WordPressOption::NOT_SET_YET
116
+		) !== WordPressOption::NOT_SET_YET;
117
+	}
118
+
119
+
120
+	/**
121
+	 * @return string
122
+	 */
123
+	public function getOptionName(): string
124
+	{
125
+		return $this->option_name;
126
+	}
127
+
128
+
129
+	/**
130
+	 * @return false|mixed|void
131
+	 */
132
+	public function loadOption()
133
+	{
134
+		if ($this->value === WordPressOption::NOT_SET_YET) {
135
+			$this->value = $this->option_engine->getOption($this->option_name, $this->default_value);
136
+		}
137
+		return $this->value;
138
+	}
139
+
140
+
141
+	/**
142
+	 * @param $value
143
+	 * @return int
144
+	 */
145
+	public function updateOption($value): int
146
+	{
147
+		// don't update if value has not changed since last update
148
+		if ($this->valueIsUnchanged($value)) {
149
+			return WordPressOption::UPDATE_NONE;
150
+		}
151
+		$this->value = $value;
152
+		// because the options for updating differ when adding an option for the first time
153
+		// we use the WordPressOption::NOT_SET_YET to determine if things already exist in the db
154
+		$updated = $this->optionExists()
155
+			? $this->option_engine->updateOption($this->option_name, $this->value)
156
+			: $this->option_engine->addOption($this->option_name, $this->value, $this->autoload());
157
+
158
+		if ($updated) {
159
+			return WordPressOption::UPDATE_SUCCESS;
160
+		}
161
+		return WordPressOption::UPDATE_ERROR;
162
+	}
163
+
164
+
165
+	private function valueIsUnchanged($value): bool
166
+	{
167
+		if (is_array($value) && is_array($this->value)) {
168
+			$diff = EEH_Array::array_diff_recursive($value, $this->value);
169
+			// $diff = array_diff($value, $this->value);
170
+			return empty($diff);
171
+		}
172
+		// emulate WP's method for checking equality
173
+		return $value === $this->value && maybe_serialize($value) === maybe_serialize($this->value);
174
+	}
175
+
176
+
177
+	/**
178
+	 * @return string
179
+	 */
180
+	private function autoload(): string
181
+	{
182
+		return $this->autoload ? 'yes' : 'no';
183
+	}
184
+
185
+
186
+	/**
187
+	 * Deletes the option from the database
188
+	 * for the rest of the request
189
+	 *
190
+	 * @return bool
191
+	 * @since  $VID:$
192
+	 */
193
+	public function deleteOption(): bool
194
+	{
195
+		return $this->option_engine->deleteOption($this->option_name);
196
+	}
197 197
 }
Please login to merge, or discard this patch.
core/helpers/EEH_Array.helper.php 2 patches
Indentation   +201 added lines, -201 removed lines patch added patch discarded remove patch
@@ -11,221 +11,221 @@  discard block
 block discarded – undo
11 11
  */
12 12
 class EEH_Array extends EEH_Base
13 13
 {
14
-    /**
15
-     * This method basically works the same as the PHP core function array_diff except it allows you to compare arrays
16
-     * of EE_Base_Class objects NOTE: This will ONLY work on an array of EE_Base_Class objects
17
-     *
18
-     * @uses array_udiff core php function for setting up our own array comparison
19
-     * @uses self::_compare_objects as the custom method for array_udiff
20
-     * @param  array $array1 an array of objects
21
-     * @param  array $array2 an array of objects
22
-     * @return array         an array of objects found in array 1 that aren't found in array 2.
23
-     */
24
-    public static function object_array_diff($array1, $array2)
25
-    {
26
-        return array_udiff($array1, $array2, array('self', '_compare_objects'));
27
-    }
14
+	/**
15
+	 * This method basically works the same as the PHP core function array_diff except it allows you to compare arrays
16
+	 * of EE_Base_Class objects NOTE: This will ONLY work on an array of EE_Base_Class objects
17
+	 *
18
+	 * @uses array_udiff core php function for setting up our own array comparison
19
+	 * @uses self::_compare_objects as the custom method for array_udiff
20
+	 * @param  array $array1 an array of objects
21
+	 * @param  array $array2 an array of objects
22
+	 * @return array         an array of objects found in array 1 that aren't found in array 2.
23
+	 */
24
+	public static function object_array_diff($array1, $array2)
25
+	{
26
+		return array_udiff($array1, $array2, array('self', '_compare_objects'));
27
+	}
28 28
 
29
-    /**
30
-     * Given that $arr is an array, determines if it's associative or numerically AND sequentially indexed
31
-     *
32
-     * @param array $array
33
-     * @return boolean
34
-     */
35
-    public static function is_associative_array(array $array): bool
36
-    {
37
-        return ! empty($array) && array_keys($array) !== range(0, count($array) - 1);
38
-    }
29
+	/**
30
+	 * Given that $arr is an array, determines if it's associative or numerically AND sequentially indexed
31
+	 *
32
+	 * @param array $array
33
+	 * @return boolean
34
+	 */
35
+	public static function is_associative_array(array $array): bool
36
+	{
37
+		return ! empty($array) && array_keys($array) !== range(0, count($array) - 1);
38
+	}
39 39
 
40
-    /**
41
-     * Gets an item from the array and leave the array intact. Use in place of end()
42
-     * when you don't want to change the array
43
-     *
44
-     * @param array $arr
45
-     * @return mixed what ever is in the array
46
-     */
47
-    public static function get_one_item_from_array($arr)
48
-    {
49
-        $item = end($arr);
50
-        reset($arr);
51
-        return $item;
52
-    }
40
+	/**
41
+	 * Gets an item from the array and leave the array intact. Use in place of end()
42
+	 * when you don't want to change the array
43
+	 *
44
+	 * @param array $arr
45
+	 * @return mixed what ever is in the array
46
+	 */
47
+	public static function get_one_item_from_array($arr)
48
+	{
49
+		$item = end($arr);
50
+		reset($arr);
51
+		return $item;
52
+	}
53 53
 
54
-    /**
55
-     * Detects if this is a multi-dimensional array
56
-     * meaning that at least one top-level value is an array. Eg [ [], ...]
57
-     *
58
-     * @param mixed $arr
59
-     * @return boolean
60
-     */
61
-    public static function is_multi_dimensional_array($arr)
62
-    {
63
-        if (is_array($arr)) {
64
-            foreach ($arr as $item) {
65
-                if (is_array($item)) {
66
-                    return true; // yep, there's at least 2 levels to this array
67
-                }
68
-            }
69
-        }
70
-        return false; // there's only 1 level, or it's not an array at all!
71
-    }
54
+	/**
55
+	 * Detects if this is a multi-dimensional array
56
+	 * meaning that at least one top-level value is an array. Eg [ [], ...]
57
+	 *
58
+	 * @param mixed $arr
59
+	 * @return boolean
60
+	 */
61
+	public static function is_multi_dimensional_array($arr)
62
+	{
63
+		if (is_array($arr)) {
64
+			foreach ($arr as $item) {
65
+				if (is_array($item)) {
66
+					return true; // yep, there's at least 2 levels to this array
67
+				}
68
+			}
69
+		}
70
+		return false; // there's only 1 level, or it's not an array at all!
71
+	}
72 72
 
73
-    /**
74
-     * Shorthand for isset( $arr[ $index ] ) ? $arr[ $index ] : $default
75
-     *
76
-     * @param array $arr
77
-     * @param mixed $index
78
-     * @param mixed $default
79
-     * @return mixed
80
-     */
81
-    public static function is_set($arr, $index, $default)
82
-    {
83
-        return isset($arr[ $index ]) ? $arr[ $index ] : $default;
84
-    }
73
+	/**
74
+	 * Shorthand for isset( $arr[ $index ] ) ? $arr[ $index ] : $default
75
+	 *
76
+	 * @param array $arr
77
+	 * @param mixed $index
78
+	 * @param mixed $default
79
+	 * @return mixed
80
+	 */
81
+	public static function is_set($arr, $index, $default)
82
+	{
83
+		return isset($arr[ $index ]) ? $arr[ $index ] : $default;
84
+	}
85 85
 
86
-    /**
87
-     * Exactly like `maybe_unserialize`, but also accounts for a WP bug: http://core.trac.wordpress.org/ticket/26118
88
-     *
89
-     * @param mixed $value usually a string, but could be an array or object
90
-     * @return mixed the UN-serialized data
91
-     */
92
-    public static function maybe_unserialize($value)
93
-    {
94
-        $data = maybe_unserialize($value);
95
-        // it's possible that this still has serialized data if it's the session.
96
-        //  WP has a bug, http://core.trac.wordpress.org/ticket/26118 that doesn't unserialize this automatically.
97
-        $token = 'C';
98
-        $data = is_string($data) ? trim($data) : $data;
99
-        if (is_string($data) && strlen($data) > 1 && $data[0] == $token && preg_match("/^{$token}:[0-9]+:/s", $data)) {
100
-            return unserialize($data);
101
-        } else {
102
-            return $data;
103
-        }
104
-    }
86
+	/**
87
+	 * Exactly like `maybe_unserialize`, but also accounts for a WP bug: http://core.trac.wordpress.org/ticket/26118
88
+	 *
89
+	 * @param mixed $value usually a string, but could be an array or object
90
+	 * @return mixed the UN-serialized data
91
+	 */
92
+	public static function maybe_unserialize($value)
93
+	{
94
+		$data = maybe_unserialize($value);
95
+		// it's possible that this still has serialized data if it's the session.
96
+		//  WP has a bug, http://core.trac.wordpress.org/ticket/26118 that doesn't unserialize this automatically.
97
+		$token = 'C';
98
+		$data = is_string($data) ? trim($data) : $data;
99
+		if (is_string($data) && strlen($data) > 1 && $data[0] == $token && preg_match("/^{$token}:[0-9]+:/s", $data)) {
100
+			return unserialize($data);
101
+		} else {
102
+			return $data;
103
+		}
104
+	}
105 105
 
106 106
 
107
-    /**
108
-     * insert_into_array
109
-     *
110
-     * @param array        $target_array the array to insert new data into
111
-     * @param array        $array_to_insert the new data to be inserted
112
-     * @param int|string|null $offset a known key within $target_array where new data will be inserted
113
-     * @param bool         $add_before whether to add new data before or after the offset key
114
-     * @param bool         $preserve_keys whether or not to reset numerically indexed arrays
115
-     * @return array
116
-     */
117
-    public static function insert_into_array(
118
-        array $target_array = array(),
119
-        array $array_to_insert = array(),
120
-        $offset = null,
121
-        bool $add_before = true,
122
-        bool $preserve_keys = true
123
-    ) {
124
-        $target_array_keys = array_keys($target_array);
125
-        // if no offset key was supplied
126
-        if (empty($offset)) {
127
-            // use start or end of $target_array based on whether we are adding before or not
128
-            $offset = $add_before ? 0 : count($target_array);
129
-        }
130
-        // if offset key is a string, then find the corresponding numeric location for that element
131
-        $offset = is_int($offset) ? $offset : array_search($offset, $target_array_keys, true);
132
-        // add one to the offset if adding after
133
-        $offset = $add_before ? $offset : $offset + 1;
134
-        // but ensure offset does not exceed the length of the array
135
-        $offset = $offset > count($target_array) ? count($target_array) : $offset;
136
-        // reindex array ???
137
-        if ($preserve_keys) {
138
-            // take a slice of the target array from the beginning till the offset,
139
-            // then add the new data
140
-            // then add another slice that starts at the offset and goes till the end
141
-            return array_slice($target_array, 0, $offset, true) + $array_to_insert + array_slice(
142
-                $target_array,
143
-                $offset,
144
-                null,
145
-                true
146
-            );
147
-        } else {
148
-            // since we don't want to preserve keys, we can use array_splice
149
-            array_splice($target_array, $offset, 0, $array_to_insert);
150
-            return $target_array;
151
-        }
152
-    }
107
+	/**
108
+	 * insert_into_array
109
+	 *
110
+	 * @param array        $target_array the array to insert new data into
111
+	 * @param array        $array_to_insert the new data to be inserted
112
+	 * @param int|string|null $offset a known key within $target_array where new data will be inserted
113
+	 * @param bool         $add_before whether to add new data before or after the offset key
114
+	 * @param bool         $preserve_keys whether or not to reset numerically indexed arrays
115
+	 * @return array
116
+	 */
117
+	public static function insert_into_array(
118
+		array $target_array = array(),
119
+		array $array_to_insert = array(),
120
+		$offset = null,
121
+		bool $add_before = true,
122
+		bool $preserve_keys = true
123
+	) {
124
+		$target_array_keys = array_keys($target_array);
125
+		// if no offset key was supplied
126
+		if (empty($offset)) {
127
+			// use start or end of $target_array based on whether we are adding before or not
128
+			$offset = $add_before ? 0 : count($target_array);
129
+		}
130
+		// if offset key is a string, then find the corresponding numeric location for that element
131
+		$offset = is_int($offset) ? $offset : array_search($offset, $target_array_keys, true);
132
+		// add one to the offset if adding after
133
+		$offset = $add_before ? $offset : $offset + 1;
134
+		// but ensure offset does not exceed the length of the array
135
+		$offset = $offset > count($target_array) ? count($target_array) : $offset;
136
+		// reindex array ???
137
+		if ($preserve_keys) {
138
+			// take a slice of the target array from the beginning till the offset,
139
+			// then add the new data
140
+			// then add another slice that starts at the offset and goes till the end
141
+			return array_slice($target_array, 0, $offset, true) + $array_to_insert + array_slice(
142
+				$target_array,
143
+				$offset,
144
+				null,
145
+				true
146
+			);
147
+		} else {
148
+			// since we don't want to preserve keys, we can use array_splice
149
+			array_splice($target_array, $offset, 0, $array_to_insert);
150
+			return $target_array;
151
+		}
152
+	}
153 153
 
154 154
 
155
-    /**
156
-     * array_merge() is slow and should never be used while looping over data
157
-     * if you don't need to preserve keys from all arrays, then using a foreach loop is much faster
158
-     * so really this acts more like array_replace( $array1, $array2 )
159
-     * or a union with the arrays flipped ( $array2 + $array1 )
160
-     * this saves a few lines of code and improves readability
161
-     *
162
-     * @param array $array1
163
-     * @param array $array2
164
-     * @return array
165
-     */
166
-    public static function merge_arrays_and_overwrite_keys(array $array1, array $array2)
167
-    {
168
-        foreach ($array2 as $key => $value) {
169
-            $array1[ $key ] = $value;
170
-        }
171
-        return $array1;
172
-    }
155
+	/**
156
+	 * array_merge() is slow and should never be used while looping over data
157
+	 * if you don't need to preserve keys from all arrays, then using a foreach loop is much faster
158
+	 * so really this acts more like array_replace( $array1, $array2 )
159
+	 * or a union with the arrays flipped ( $array2 + $array1 )
160
+	 * this saves a few lines of code and improves readability
161
+	 *
162
+	 * @param array $array1
163
+	 * @param array $array2
164
+	 * @return array
165
+	 */
166
+	public static function merge_arrays_and_overwrite_keys(array $array1, array $array2)
167
+	{
168
+		foreach ($array2 as $key => $value) {
169
+			$array1[ $key ] = $value;
170
+		}
171
+		return $array1;
172
+	}
173 173
 
174 174
 
175
-    /**
176
-     * given a flat array like $array = array('A', 'B', 'C')
177
-     * will convert into a multidimensional array like $array[A][B][C]
178
-     * if $final_value is provided and is anything other than null,
179
-     * then that will be set as the value for the innermost array key
180
-     * like so: $array[A][B][C] = $final_value
181
-     *
182
-     * @param array $flat_array
183
-     * @param mixed $final_value
184
-     * @return array
185
-     */
186
-    public static function convert_array_values_to_keys(array $flat_array, $final_value = null)
187
-    {
188
-        $multidimensional = array();
189
-        $reference = &$multidimensional;
190
-        foreach ($flat_array as $key) {
191
-            $reference[ $key ] = array();
192
-            $reference = &$reference[ $key ];
193
-        }
194
-        if ($final_value !== null) {
195
-            $reference = $final_value;
196
-        }
197
-        return $multidimensional;
198
-    }
175
+	/**
176
+	 * given a flat array like $array = array('A', 'B', 'C')
177
+	 * will convert into a multidimensional array like $array[A][B][C]
178
+	 * if $final_value is provided and is anything other than null,
179
+	 * then that will be set as the value for the innermost array key
180
+	 * like so: $array[A][B][C] = $final_value
181
+	 *
182
+	 * @param array $flat_array
183
+	 * @param mixed $final_value
184
+	 * @return array
185
+	 */
186
+	public static function convert_array_values_to_keys(array $flat_array, $final_value = null)
187
+	{
188
+		$multidimensional = array();
189
+		$reference = &$multidimensional;
190
+		foreach ($flat_array as $key) {
191
+			$reference[ $key ] = array();
192
+			$reference = &$reference[ $key ];
193
+		}
194
+		if ($final_value !== null) {
195
+			$reference = $final_value;
196
+		}
197
+		return $multidimensional;
198
+	}
199 199
 
200 200
 
201
-    /**
202
-     * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
203
-     * @param array $array
204
-     * @return bool
205
-     */
206
-    public static function is_array_numerically_and_sequentially_indexed(array $array)
207
-    {
208
-        return empty($array) || array_keys($array) === range(0, count($array) - 1);
209
-    }
201
+	/**
202
+	 * @see http://stackoverflow.com/questions/173400/how-to-check-if-php-array-is-associative-or-sequential
203
+	 * @param array $array
204
+	 * @return bool
205
+	 */
206
+	public static function is_array_numerically_and_sequentially_indexed(array $array)
207
+	{
208
+		return empty($array) || array_keys($array) === range(0, count($array) - 1);
209
+	}
210 210
 
211 211
 
212
-    /**
213
-     * recursively walks through an array and adds slashes to all no array elements
214
-     *
215
-     * @param mixed $element
216
-     * @return array|string
217
-     * @since   4.10.29.p
218
-     */
219
-    public static function addSlashesRecursively($element)
220
-    {
221
-        if (is_array($element)) {
222
-            foreach ($element as $key => $value) {
223
-                $element[ $key ] = EEH_Array::addSlashesRecursively($value);
224
-            }
225
-            return $element;
226
-        }
227
-        return is_string($element) ? addslashes($element) : $element;
228
-    }
212
+	/**
213
+	 * recursively walks through an array and adds slashes to all no array elements
214
+	 *
215
+	 * @param mixed $element
216
+	 * @return array|string
217
+	 * @since   4.10.29.p
218
+	 */
219
+	public static function addSlashesRecursively($element)
220
+	{
221
+		if (is_array($element)) {
222
+			foreach ($element as $key => $value) {
223
+				$element[ $key ] = EEH_Array::addSlashesRecursively($value);
224
+			}
225
+			return $element;
226
+		}
227
+		return is_string($element) ? addslashes($element) : $element;
228
+	}
229 229
 
230 230
 
231 231
 	/**
@@ -294,7 +294,7 @@  discard block
 block discarded – undo
294 294
 	 *	print_r( EEH_Array::flattenArray($example, true) );
295 295
 	 *
296 296
 	 * 	"a:A, b:B, c:[ d:D, e:E, f:[ G, H, I ] ], [ J, K ], L, M, n:[ o:P ]"
297
- 	 *
297
+	 *
298 298
 	 * @param array $array		the array to be flattened
299 299
 	 * @param bool  $to_string	[true] will flatten the entire array down into a string
300 300
 	 *                         	[false] will only flatten sub-arrays down into strings and return a array
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
      */
81 81
     public static function is_set($arr, $index, $default)
82 82
     {
83
-        return isset($arr[ $index ]) ? $arr[ $index ] : $default;
83
+        return isset($arr[$index]) ? $arr[$index] : $default;
84 84
     }
85 85
 
86 86
     /**
@@ -166,7 +166,7 @@  discard block
 block discarded – undo
166 166
     public static function merge_arrays_and_overwrite_keys(array $array1, array $array2)
167 167
     {
168 168
         foreach ($array2 as $key => $value) {
169
-            $array1[ $key ] = $value;
169
+            $array1[$key] = $value;
170 170
         }
171 171
         return $array1;
172 172
     }
@@ -188,8 +188,8 @@  discard block
 block discarded – undo
188 188
         $multidimensional = array();
189 189
         $reference = &$multidimensional;
190 190
         foreach ($flat_array as $key) {
191
-            $reference[ $key ] = array();
192
-            $reference = &$reference[ $key ];
191
+            $reference[$key] = array();
192
+            $reference = &$reference[$key];
193 193
         }
194 194
         if ($final_value !== null) {
195 195
             $reference = $final_value;
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
     {
221 221
         if (is_array($element)) {
222 222
             foreach ($element as $key => $value) {
223
-                $element[ $key ] = EEH_Array::addSlashesRecursively($value);
223
+                $element[$key] = EEH_Array::addSlashesRecursively($value);
224 224
             }
225 225
             return $element;
226 226
         }
@@ -242,17 +242,17 @@  discard block
 block discarded – undo
242 242
 		foreach ($array_1 as $key => $value) {
243 243
 			if (array_key_exists($key, $array_2)) {
244 244
 				if (is_array($value)) {
245
-					$inner_diff = EEH_Array::array_diff_recursive($value, $array_2[ $key ]);
245
+					$inner_diff = EEH_Array::array_diff_recursive($value, $array_2[$key]);
246 246
 					if (count($inner_diff)) {
247
-						$diff[ $key ] = $inner_diff;
247
+						$diff[$key] = $inner_diff;
248 248
 					}
249 249
 				} else {
250
-					if ($value != $array_2[ $key ]) {
251
-						$diff[ $key ] = $value;
250
+					if ($value != $array_2[$key]) {
251
+						$diff[$key] = $value;
252 252
 					}
253 253
 				}
254 254
 			} else {
255
-				$diff[ $key ] = $value;
255
+				$diff[$key] = $value;
256 256
 			}
257 257
 		}
258 258
 		return $diff;
@@ -306,11 +306,11 @@  discard block
 block discarded – undo
306 306
 	{
307 307
 		$flat_array = [];
308 308
 		foreach ($array as $key => $value) {
309
-			$flat_array[ $key ] = is_array($value)
309
+			$flat_array[$key] = is_array($value)
310 310
 				? EEH_Array::flattenArray($value, true, false)
311 311
 				: $value;
312 312
 		}
313
-		if (! $to_string) {
313
+		if ( ! $to_string) {
314 314
 			return $flat_array;
315 315
 		}
316 316
 		$flat = '';
Please login to merge, or discard this patch.