| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Image_Sources; |
| 4 |
|
| 5 |
/** |
| 6 |
* Image Sources Utils |
| 7 |
*/ |
| 8 |
class Utils { |
| 9 |
/** |
| 10 |
* Transform the licenses from the options textfield into an array |
| 11 |
* |
| 12 |
* @param string $licences text with licenses. |
| 13 |
* @return array|bool $new_licences array with licenses and license information or false if no array created. |
| 14 |
*/ |
| 15 |
public static function licences_text_to_array( $licences = '' ) { |
| 16 |
if ( $licences === '' ) { |
| 17 |
return false; |
| 18 |
} |
| 19 |
// split the text by line |
| 20 |
$licences_array = preg_split( '/\r?\n/', trim( $licences ) ); |
| 21 |
if ( count( $licences_array ) === 0 ) { |
| 22 |
return false; |
| 23 |
} |
| 24 |
// create the array with licence => url |
| 25 |
$new_licences = []; |
| 26 |
foreach ( $licences_array as $_licence ) { |
| 27 |
if ( trim( $_licence ) !== '' ) { |
| 28 |
$temp = explode( '|', $_licence ); |
| 29 |
$new_licences[ $temp[0] ] = []; |
| 30 |
if ( isset( $temp[1] ) ) { |
| 31 |
$new_licences[ $temp[0] ]['url'] = esc_url( $temp[1] ); |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if ( $new_licences === [] ) { |
| 37 |
return false; |
| 38 |
} else { |
| 39 |
return $new_licences; |
| 40 |
} |
| 41 |
} |
| 42 |
} |