Completed
Push — master ( 29c589...df73b7 )
by Jared
03:00
created

TimberTwig::add_list_separators()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.2
cc 4
eloc 12
nc 4
nop 3
1
<?php
2
3
class TimberTwig {
4
5
	public static $dir_name;
6
7
	/**
8
	 * Initialization
9
	 */
10
	public static function init() {
11
		new TimberTwig();
12
	}
13
14
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
		add_action( 'timber/twig/filters', array( $this, 'add_timber_filters_deprecated' ) );
16
		add_action( 'timber/twig/filters', array( $this, 'add_timber_filters' ) );
17
	}
18
19
	/**
20
	 * These are all deprecated and will be removed in 0.21.0
21
	 *
22
	 * @param Twig_Environment $twig
23
	 * @deprecated since 0.20.7
24
	 * @return Twig_Environment
25
	 */
26
	function add_timber_filters_deprecated( $twig ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
		$twig->addFilter( new Twig_SimpleFilter( 'get_src_from_attachment_id', 'twig_get_src_from_attachment_id' ) );
28
		$twig->addFilter( new Twig_SimpleFilter( 'wp_body_class', array( $this, 'body_class' ) ) );
29
		$twig->addFilter( new Twig_SimpleFilter( 'twitterify', array( 'TimberHelper', 'twitterify' ) ) );
30
		$twig->addFilter( new Twig_SimpleFilter( 'twitterfy', array( 'TimberHelper', 'twitterify' ) ) );
31
		$twig->addFilter( new Twig_SimpleFilter( 'string', function($arr, $glue = ' '){
32
			return twig_join_filter($arr, $glue);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
33
		} ) );
34
		return $twig;
35
	}
36
37
	/**
38
	 *
39
	 *
40
	 * @param Twig_Environment $twig
41
	 * @return Twig_Environment
42
	 */
43
	function add_timber_filters( $twig ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
44
		/* image filters */
45
		$twig->addFilter( new Twig_SimpleFilter( 'resize', array( 'TimberImageHelper', 'resize' ) ) );
46
		$twig->addFilter( new Twig_SimpleFilter( 'retina', array( 'TimberImageHelper', 'retina_resize' ) ) );
47
		$twig->addFilter( new Twig_SimpleFilter( 'letterbox', array( 'TimberImageHelper', 'letterbox' ) ) );
48
		$twig->addFilter( new Twig_SimpleFilter( 'tojpg', array( 'TimberImageHelper', 'img_to_jpg' ) ) );
49
50
		/* debugging filters */
51
		$twig->addFilter( new Twig_SimpleFilter( 'docs', 'twig_object_docs' ) );
52
		$twig->addFilter( new Twig_SimpleFilter( 'get_class',  'get_class' ) );
53
		$twig->addFilter( new Twig_SimpleFilter( 'get_type', 'get_type' ) );
54
		$twig->addFilter( new Twig_SimpleFilter( 'print_r', function( $arr ) {
55
					return print_r( $arr, true );
0 ignored issues
show
introduced by
The use of function print_r() is discouraged
Loading history...
56
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
57
		$twig->addFilter( new Twig_SimpleFilter( 'print_a', function( $arr ) {
58
					return '<pre>' . self::object_docs( $arr, true ) . '</pre>';
59
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
60
61
		/* other filters */
62
		$twig->addFilter( new Twig_SimpleFilter( 'stripshortcodes', 'strip_shortcodes' ) );
63
		$twig->addFilter( new Twig_SimpleFilter( 'array', array( $this, 'to_array' ) ) );
64
		$twig->addFilter( new Twig_SimpleFilter( 'excerpt', 'wp_trim_words' ) );
65
		$twig->addFilter( new Twig_SimpleFilter( 'function', array( $this, 'exec_function' ) ) );
66
		$twig->addFilter( new Twig_SimpleFilter( 'pretags', array( $this, 'twig_pretags' ) ) );
67
		$twig->addFilter( new Twig_SimpleFilter( 'sanitize', 'sanitize_title' ) );
68
		$twig->addFilter( new Twig_SimpleFilter( 'shortcodes', 'do_shortcode' ) );
69
		$twig->addFilter( new Twig_SimpleFilter( 'time_ago', array( $this, 'time_ago' ) ) );
70
		$twig->addFilter( new Twig_SimpleFilter( 'wpautop', 'wpautop' ) );
71
		$twig->addFilter( new Twig_SimpleFilter( 'list', array( $this, 'add_list_separators' ) ) );
72
73
		$twig->addFilter( new Twig_SimpleFilter( 'relative', function ( $link ) {
74
					return TimberURLHelper::get_rel_url( $link, true );
75
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
76
77
		$twig->addFilter( new Twig_SimpleFilter( 'date', array( $this, 'intl_date' ) ) );
78
79
		$twig->addFilter( new Twig_SimpleFilter( 'truncate', function ( $text, $len ) {
80
					return TimberHelper::trim_words( $text, $len );
81
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
82
83
		/* actions and filters */
84
		$twig->addFunction( new Twig_SimpleFunction( 'action', function ( $context ) {
85
					$args = func_get_args();
86
					array_shift( $args );
87
					$args[] = $context;
88
					call_user_func_array( 'do_action', $args );
89
				}, array( 'needs_context' => true ) ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
90
91
		$twig->addFilter( new Twig_SimpleFilter( 'apply_filters', function () {
92
					$args = func_get_args();
93
					$tag = current( array_splice( $args, 1, 1 ) );
94
95
					return apply_filters_ref_array( $tag, $args );
96
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
97
		$twig->addFunction( new Twig_SimpleFunction( 'function', array( &$this, 'exec_function' ) ) );
98
		$twig->addFunction( new Twig_SimpleFunction( 'fn', array( &$this, 'exec_function' ) ) );
99
100
		$twig->addFunction( new Twig_SimpleFunction( 'shortcode', 'do_shortcode' ) );
101
102
		/* TimberObjects */
103 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'TimberPost', function ( $pid, $PostClass = 'TimberPost' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
105
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
106
							$p = new $PostClass( $p );
107
						}
108
						return $pid;
109
					}
110
					return new $PostClass( $pid );
111
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
112 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'TimberImage', function ( $pid, $ImageClass = 'TimberImage' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
113
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
114
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
115
							$p = new $ImageClass( $p );
116
						}
117
						return $pid;
118
					}
119
					return new $ImageClass( $pid );
120
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
121 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'TimberTerm', function ( $pid, $TermClass = 'TimberTerm' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
123
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
124
							$p = new $TermClass( $p );
125
						}
126
						return $pid;
127
					}
128
					return new $TermClass( $pid );
129
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
130 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'TimberUser', function ( $pid, $UserClass = 'TimberUser' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
132
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
133
							$p = new $UserClass( $p );
134
						}
135
						return $pid;
136
					}
137
					return new $UserClass( $pid );
138
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
139
140
		/* TimberObjects Alias */
141 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'Post', function ( $pid, $PostClass = 'TimberPost' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
143
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
144
							$p = new $PostClass( $p );
145
						}
146
						return $pid;
147
					}
148
					return new $PostClass( $pid );
149
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
150 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'Image', function ( $pid, $ImageClass = 'TimberImage' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
152
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
153
							$p = new $ImageClass( $p );
154
						}
155
						return $pid;
156
					}
157
					return new $ImageClass( $pid );
158
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
159 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'Term', function ( $pid, $TermClass = 'TimberTerm' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
161
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
162
							$p = new $TermClass( $p );
163
						}
164
						return $pid;
165
					}
166
					return new $TermClass( $pid );
167
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
168 View Code Duplication
		$twig->addFunction( new Twig_SimpleFunction( 'User', function ( $pid, $UserClass = 'TimberUser' ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
169
					if ( is_array( $pid ) && !TimberHelper::is_array_assoc( $pid ) ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 3 tabs, found 5
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
170
						foreach ( $pid as &$p ) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 4 tabs, found 6
Loading history...
171
							$p = new $UserClass( $p );
172
						}
173
						return $pid;
174
					}
175
					return new $UserClass( $pid );
176
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
177
178
		/* bloginfo and translate */
179
		$twig->addFunction( 'bloginfo', new Twig_SimpleFunction( 'bloginfo', function ( $show = '', $filter = 'raw' ) {
180
					return get_bloginfo( $show, $filter );
181
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
182
		$twig->addFunction( '__', new Twig_SimpleFunction( '__', function ( $text, $domain = 'default' ) {
183
					return __( $text, $domain );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$text'
Loading history...
184
				} ) );
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 2 tabs, found 4
Loading history...
185
		/* get_twig is deprecated, use timber/twig */
186
		$twig = apply_filters( 'get_twig', $twig );
187
		$twig = apply_filters( 'timber/twig', $twig );
188
		return $twig;
189
	}
190
191
	/**
192
	 *
193
	 *
194
	 * @param mixed   $arr
195
	 * @return array
196
	 */
197
	function to_array( $arr ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
198
		if ( is_array( $arr ) ) {
199
			return $arr;
200
		}
201
		$arr = array( $arr );
202
		return $arr;
203
	}
204
205
	/**
206
	 *
207
	 *
208
	 * @param string  $function_name
209
	 * @return mixed
210
	 */
211
	function exec_function( $function_name ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
212
		$args = func_get_args();
213
		array_shift( $args );
214
		if ( is_string($function_name) ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
215
			$function_name = trim( $function_name );
216
		}
217
		return call_user_func_array( $function_name, ( $args ) );
218
	}
219
220
	/**
221
	 *
222
	 *
223
	 * @param string  $content
224
	 * @return string
225
	 */
226
	function twig_pretags( $content ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
227
		return preg_replace_callback( '|<pre.*>(.*)</pre|isU', array( &$this, 'convert_pre_entities' ), $content );
228
	}
229
230
	/**
231
	 *
232
	 *
233
	 * @param array   $matches
234
	 * @return string
235
	 */
236
	function convert_pre_entities( $matches ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
237
		return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
238
	}
239
240
	/**
241
	 * @param mixed   $body_classes
242
	 * @deprecated 0.20.7
243
	 * @return string
244
	 */
245
	function body_class( $body_classes ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
246
		ob_start();
247
		if ( is_array( $body_classes ) ) {
248
			$body_classes = explode( ' ', $body_classes );
249
		}
250
		body_class( $body_classes );
251
		$return = ob_get_contents();
252
		ob_end_clean();
253
		return $return;
254
	}
255
256
	/**
257
	 *
258
	 *
259
	 * @param string  $date
260
	 * @param string  $format (optional)
0 ignored issues
show
Documentation introduced by
Should the type for parameter $format not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
261
	 * @return string
262
	 */
263
	function intl_date( $date, $format = null ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
264
		if ( $format === null ) {
265
			$format = get_option( 'date_format' );
266
		}
267
268
		if ( $date instanceof DateTime ) {
269
			$timestamp = $date->getTimestamp();
270
		} else if (is_numeric( $date ) && strtotime( $date ) === false ) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
Found "=== false". Use Yoda Condition checks, you must
Loading history...
271
			$timestamp = intval( $date );
272
		} else {
273
			$timestamp = strtotime( $date );
274
		}
275
276
		return date_i18n( $format, $timestamp );
277
	}
278
279
	//debug
280
281
	/**
282
	 *
283
	 *
284
	 * @param mixed   $obj
285
	 * @param bool    $methods
286
	 * @return string
287
	 */
288
	function object_docs( $obj, $methods = true ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
289
		$class = get_class( $obj );
290
		$properties = (array)$obj;
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
291
		if ( $methods ) {
292
			/** @var array $methods */
293
			$methods = $obj->get_method_values();
294
		}
295
		$rets = array_merge( $properties, $methods );
296
		ksort( $rets );
297
		$str = print_r( $rets, true );
0 ignored issues
show
introduced by
The use of function print_r() is discouraged
Loading history...
298
		$str = str_replace( 'Array', $class . ' Object', $str );
299
		return $str;
300
	}
301
302
	/**
303
	 * @param int|string $from
304
	 * @param int|string $to
0 ignored issues
show
Documentation introduced by
Should the type for parameter $to not be integer|string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
305
	 * @param string $format_past
306
	 * @param string $format_future
307
	 * @return string
308
	 */
309
	function time_ago( $from, $to = null, $format_past = '%s ago', $format_future = '%s from now' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
310
		$to = $to === null ? time() : $to;
311
		$to = is_int( $to ) ? $to : strtotime( $to );
312
		$from = is_int( $from ) ? $from : strtotime( $from );
313
314
		if ( $from < $to ) {
315
			return sprintf( $format_past, human_time_diff( $from, $to ) );
316
		} else {
317
			return sprintf( $format_future, human_time_diff( $to, $from ) );
318
		}
319
	}
320
321
	/**
322
	 * @param array $arr
323
	 * @param string $first_delimiter
324
	 * @param string $second_delimiter
325
	 * @return string
326
	 */
327
	function add_list_separators( $arr, $first_delimiter = ',', $second_delimiter = 'and' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
328
		$length = count( $arr );
329
		$list = '';
330
		foreach( $arr as $index => $item ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
331
			if ( $index < $length - 2 ) {
332
				$delimiter = $first_delimiter.' ';
333
			} elseif ( $index == $length - 2 ) {
334
				$delimiter = ' '.$second_delimiter.' ';
335
			} else {
336
				$delimiter = '';
337
			}
338
			$list = $list.$item.$delimiter;
339
		}
340
		return $list;
341
	}
342
343
}
344