Assets.php
3 weeks ago
Cache.php
3 weeks ago
Date.php
3 weeks ago
Debug.php
3 weeks ago
FeedReader.php
3 weeks ago
HTML.php
3 weeks ago
Helper.php
3 weeks ago
JSON.php
3 weeks ago
Nonce.php
3 weeks ago
Notice.php
3 weeks ago
Number.php
3 weeks ago
NumberConverter.php
3 weeks ago
Param.php
3 weeks ago
Sanitizing.php
3 weeks ago
Strip.php
3 weeks ago
Templates.php
3 weeks ago
User.php
3 weeks ago
Validating.php
3 weeks ago
WooCommerce.php
3 weeks ago
WordPress.php
3 weeks ago
Helper.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPParsidate\Helper; |
| 4 | |
| 5 | class Helper { |
| 6 | /** |
| 7 | * Reorder array list |
| 8 | * |
| 9 | * @param array $array Input array |
| 10 | * @param array $orders Order number lists |
| 11 | * |
| 12 | * @return array|false Reordered array |
| 13 | */ |
| 14 | public static function reorderArray( $array, $orders ) { |
| 15 | $reorderArray = []; |
| 16 | $orders = array_map( 'intval', $orders ); |
| 17 | |
| 18 | if ( count( array_unique( array_keys( $orders ) ) ) !== count( array_unique( array_values( $orders ) ) ) ) { |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | foreach ( $orders as $index => $order ) { |
| 23 | $reorderArray[ $order ] = $array[ $index ]; |
| 24 | } |
| 25 | |
| 26 | ksort( $reorderArray ); |
| 27 | |
| 28 | return $reorderArray; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Random string |
| 33 | * |
| 34 | * @param int $length String length |
| 35 | * @param bool $smallAlphabet Use small alphabet |
| 36 | * @param bool $largeAlphabet Use large alphabet |
| 37 | * @param bool $numbers Use numbers |
| 38 | * |
| 39 | * @return false|string Random string, Otherwise return false if empty |
| 40 | */ |
| 41 | public static function randomString( $length, $smallAlphabet = true, $largeAlphabet = true, $numbers = true ) { |
| 42 | $strings = []; |
| 43 | if ( $smallAlphabet ) { |
| 44 | $strings = array_merge( $strings, range( 'a', 'z' ) ); |
| 45 | } |
| 46 | if ( $largeAlphabet ) { |
| 47 | $strings = array_merge( $strings, range( 'A', 'Z' ) ); |
| 48 | } |
| 49 | if ( $numbers ) { |
| 50 | $strings = array_merge( $strings, range( 1, 9 ) ); |
| 51 | } |
| 52 | |
| 53 | if ( empty( $strings ) ) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | shuffle( $strings ); |
| 58 | |
| 59 | $strings = implode( "", $strings ); |
| 60 | |
| 61 | return substr( str_shuffle( $strings ), 0, $length ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Inserts any number of scalars or arrays at the point |
| 66 | * in the haystack immediately after the search key ($needle) was found, |
| 67 | * or at the end if the needle is not found or not supplied. |
| 68 | * Modifies $haystack in place. |
| 69 | * https://stackoverflow.com/a/7257599/3224296 |
| 70 | * |
| 71 | * @param array &$haystack the associative array to search. This will be modified by the function |
| 72 | * @param int $needle the key to search for |
| 73 | * @param array $stuff one or more arrays or scalars to be inserted into $haystack |
| 74 | * |
| 75 | * @return array the index at which $needle was found |
| 76 | */ |
| 77 | public static function arrayInsertAfter( array $haystack, int $needle = 0, array $stuff = [] ): array { |
| 78 | return array_merge( array_slice( $haystack, 0, $needle, true ), $stuff, |
| 79 | array_slice( $haystack, $needle, count( $haystack ) - 1, true ) ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * URL to key |
| 84 | * Use for cache base on url string |
| 85 | * |
| 86 | * @param string $url |
| 87 | * @param bool $hostOnly |
| 88 | * |
| 89 | * @return false|string Key string, if url isn't valid return false |
| 90 | */ |
| 91 | public static function urlToKey( string $url, $hostOnly = false ) { |
| 92 | if ( Validating::isUrl( $url ) ) { |
| 93 | if ( $hostOnly ) { |
| 94 | $url = wp_parse_url( $url, PHP_URL_HOST ); |
| 95 | } else { |
| 96 | $url = wp_parse_url( $url, PHP_URL_HOST ) . wp_parse_url( $url, PHP_URL_PATH ); |
| 97 | $url = trim( $url, '/' ); |
| 98 | } |
| 99 | |
| 100 | return Sanitizing::title( $url ); |
| 101 | } |
| 102 | |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 |