Last active
May 7, 2021 09:38
-
-
Save anwas/2cfcc3e16ef651487bb5e3223f7903c5 to your computer and use it in GitHub Desktop.
[DEV functions] Functions list #php #function #dev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/********************************************************************** | |
* *** | |
* Konstanta ABSPATH naudoja funkcijose su failo operacijomis | |
*********************************************************************/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
define( 'ABSPATH', dirname( __FILE__, 5 ) ); // svetainės root aplankas (pritaikyti pagal projektą) (web) | |
} | |
setlocale( LC_ALL, ['lt_LT.UTF-8', 'lt_LT', 'lt'] ); | |
/** | |
* Functions list | |
* | |
* esc_html( $string ) | |
* anwp_get_user_ip() | |
* | |
* anwp_get_pp( $value ) | |
* anwp_get_vep( $value ) | |
* anwp_get_vdp( $value ) | |
* anwp_debug( $value = 'neperduotas joks parametras', $what_func = 'vep' ) | |
* anwp_script_run_time( $start_time = NULL ) | |
* anwp_php_version_at_least( $version = '' ) | |
* | |
* anwp_get_strtotime( $date = null, $format = null, $timezone = null, $lang = null ) | |
* anwp_get_x_days( $args = null ) | |
* anwp_get_first_easter_day( $year = NULL ) | |
* anwp_get_second_easter_day( $year = NULL ) | |
* anwp_validate_date( $date, $format = 'Y-m-d' ) | |
* anwp_get_weekday_by_date( $date = NULL ) | |
* anwp_get_formated_timestring_by_date( $date = NULL, $date_format = 'Y-m-d', $ftime_format = '%c' ) | |
* | |
* anwp_make_slug( $string = '' ) | |
* anwp_string_to_latin( $string = '' ) | |
* mb_ucfirst( $string = '' ) | |
* mb_string_to_array( $string = '', $length = 1 ) | |
* mb_str_split( $string = '', $length = 1 ) | |
* anwp_mb_str_rand( $length = 32, $characters = '0123456789aąbcčdeęėfghiįyjklmnoprsštuųūvzžwxqAĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽWXQ ' ) | |
* | |
* anwp_get_file_info( $file, $returned_values = array( 'name', 'server_path', 'size', 'date' ) ) | |
* anwp_file_exists( $file_path, $view_path = ABSPATH ) | |
* anwp_get_file_content( $file_path, $view_path = ABSPATH ) | |
* anwp_get_url_content( $url, $useragent = 'cURL', $headers = false, $follow_redirects = false, $debug = false ) | |
* | |
* anwp_array_pluck( $col_name, $array ) | |
* | |
* @author anwas | |
*/ | |
/********************************************************************** | |
* *** | |
* Gauti kovertuotą su htmlspecialchars eilutę | |
*********************************************************************/ | |
if ( ! function_exists( 'esc_html' ) ) { | |
function esc_html( $string ) { | |
if ( ! is_array( $string ) && ! is_object( $string ) ) { | |
return htmlspecialchars( trim( $string ), ENT_QUOTES, 'UTF-8', false ); | |
} else { | |
return 'Perduotas parametras NĖRA eilutė (string)'; | |
} | |
} | |
} | |
/********************************************************************** | |
* *** | |
* Guti naudotojo IP adresą | |
*********************************************************************/ | |
function anwp_get_user_ip() { | |
$client = isset( $_SERVER['HTTP_CLIENT_IP'] ) ? $_SERVER['HTTP_CLIENT_IP'] : FALSE; | |
$forward = isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE; | |
$remote = $_SERVER['REMOTE_ADDR']; | |
if ( filter_var( $client, FILTER_VALIDATE_IP ) ) { | |
$ip = $client; | |
} | |
else if ( filter_var( $forward, FILTER_VALIDATE_IP ) ) { | |
$ip = $forward; | |
} | |
else { | |
$ip = $remote; | |
} | |
return $ip; | |
} | |
if ( ! function_exists( 'anwp_get_pp' ) ) { | |
/* | |
|------------------------------------------------------------------------------ | |
| Gauname kintamąjį aiškioje peržiūrai formoje | |
|------------------------------------------------------------------------------ | |
*/ | |
function anwp_get_pp( $value ) { | |
return print_r( $value, true ); | |
} | |
} // if ( ! function_exists( 'anwp_get_pp' ) ) | |
if ( ! function_exists( 'anwp_get_vep' ) ) { | |
/* | |
|------------------------------------------------------------------------------ | |
| Gauname kintamąjį aiškioje peržiūrai formoje | |
|------------------------------------------------------------------------------ | |
*/ | |
function anwp_get_vep( $value ) { | |
ob_start(); | |
var_export( $value ); | |
$o = ob_get_clean(); | |
$output = ( false !== $o ) ? $o : 'Negauta jokių rezultatų'; | |
return $output; | |
} | |
} // if ( ! function_exists( 'anwp_get_vep' ) ) | |
if ( ! function_exists( 'anwp_get_vdp' ) ) { | |
/* | |
|------------------------------------------------------------------------------ | |
| Gauname kintamąjį aiškioje peržiūrai formoje | |
|------------------------------------------------------------------------------ | |
*/ | |
function anwp_get_vdp( $value ) { | |
ob_start(); | |
var_dump( $value ); | |
$o = ob_get_clean(); | |
$output = ( false !== $o ) ? $o : 'Negauta jokių rezultatų'; | |
return $output; | |
} | |
} // if ( ! function_exists( 'anwp_get_vdp' ) ) | |
if ( ! function_exists( 'anwp_debug' ) ) { | |
/********************************************************************** | |
* *** | |
* Išvesti perduoto paramtro reikšmę fixed bloke | |
*********************************************************************/ | |
function anwp_debug( $value = 'neperduotas joks parametras', $what_func = 'pp', $is_escape = true ) { | |
$available_func = [ | |
'pp', // print_r | |
'vdp', // var_dump | |
'vep', // var_export | |
]; | |
$fileinfo = 'negauta informacijos apie failą'; | |
$backtrace = debug_backtrace(); | |
// Gauname failą ir eilutę iš kur iškviesta anwp_debug() funkcija | |
if ( ! empty( $backtrace[0] ) && is_array( $backtrace[0] ) ) { | |
$fileinfo = $backtrace[0]['file'] . ':' . $backtrace[0]['line']; | |
} | |
$output_str = ''; | |
$open_tags = '<pre style="margin: 0; padding: 1rem;"><code>'; | |
$close_tags = '</code></pre>'; | |
if ( in_array( $what_func, $available_func ) ) { | |
switch ( $what_func ) { | |
case 'vdp': | |
$output = anwp_get_vdp( $value ); | |
break; | |
case 'vep': | |
$output = anwp_get_vep( $value ); | |
break; | |
default: | |
$output = anwp_get_pp( $value ); | |
break; | |
} | |
} else { | |
$output = anwp_get_pp( $value ); | |
} | |
$output_str .= '<div style="position: fixed; right: 0.5rem; bottom: 3rem; z-index: 99999; max-width: 50vw; max-height: 50vh; overflow: auto; box-shadow: 0 2px 8px 3px rgba( 0, 0, 0, 0.5 ); background: #f3f3f3;">'; | |
$output_str .= '<div style="background: #0f0f0f; color: #f0f0f0; padding: 1rem; font-family: monospace;">'; | |
$output_str .= '<strong>Failas:</strong> ' . $fileinfo . '<br />'; | |
$output_str .= '<strong>Vykdymo trukmė:</strong> ' . anwp_script_run_time() . ' sek.'; | |
$output_str .= '</div>'; | |
$output_str .= $open_tags; | |
$output_str .= ( true === $is_escape ) ? esc_html( $output ) : $output; | |
$output_str .= $close_tags; | |
$output_str .= '</div>'; | |
echo $output_str; | |
unset( $fileinfo, $backtrace, $output_str, $open_tags, $close_tags, $output, $value ); | |
} | |
} | |
/********************************************************************** | |
* *** | |
* Gauti scenarijaus vykdymo trukmę | |
*********************************************************************/ | |
function anwp_script_run_time( $start_time = NULL ) { | |
$now = microtime( TRUE ); | |
if ( isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ) { | |
// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array. | |
// It contains the timestamp of the start of the request with microsecond precision. | |
return ( $now - $_SERVER['REQUEST_TIME_FLOAT'] ); | |
} else if ( ! is_null( $start_time ) ) { | |
return ( $now - $start_time ); | |
} else { | |
return NULL; | |
} | |
} | |
if ( ! function_exists( 'anwp_php_version_at_least' ) ) { | |
/** | |
* Funkcijai būtina pateikti tinkamą versijos formatą | |
* Pvz.: 5.4 ar 7.1.0 | |
* Po skaičių būtinai turi būti taškas, nes kitaip funkcija grąžins TRUE | |
* Pateiktus eilutę, kuri prasideda neegzistuojančia versija, | |
* bet yra mažesnė už jau esančią sistemoje, funkcija grąžina TRUE | |
* Pvz.: pateikus versiją 6.0, bus grąžinta TRUE reikšmė, | |
* bet pateikus 8.0, gali būti grąžinta FALSE | |
*/ | |
function anwp_php_version_at_least( $version = '' ) { | |
if ( empty( trim( $version ) ) | |
|| is_null( trim( $version ) ) | |
|| ! is_numeric( mb_substr( trim( $version ), 0, 1, 'utf-8' ) ) | |
|| true === $version ) { | |
return FALSE; | |
} | |
if ( version_compare( PHP_VERSION, trim( $version ), '>=' ) ) { | |
return TRUE; | |
} | |
return FALSE; | |
} | |
} // if ( ! function_exists( 'anwp_php_version_at_least' ) ) | |
if ( ! function_exists( 'anwp_get_strtotime' ) ) { | |
function anwp_get_strtotime( $date = null, $format = null, $timezone = null, $lang = null ) { | |
// Patern http://userguide.icu-project.org/formatparse/datetime | |
/********************************************************************** | |
* *** | |
* 'yyyy-MM-dd HH:mm:ss' – 2019-11-03 04:13:41 | |
* 'GGGG' – po Kristaus | |
* 'cccc' – sekmadienis | |
* 'zzzz' – Rytų Europos žiemos laikas | |
* 'VV' – Europe/Vilnius | |
* 'OOOO' – GMT+02:00 | |
* "yyyy 'm.' MMMM 'mėn'. d 'd.'" – 2019 m. lapkričio mėn. 3 d. | |
* 'LLLL' – lapkritis | |
*********************************************************************/ | |
$date = ( is_null( $date ) ) ? 'now' : $date; | |
$format = ( is_null( $format ) ) ? 'yyyy-MM-dd HH:mm:ss, cccc – VV OOOO' : $format; | |
$timezone = ( is_null( $timezone ) ) ? 'Europe/Vilnius' : $timezone; | |
$lang = ( is_null( $lang ) ) ? 'lt_LT' : $lang; | |
$fmt = new IntlDateFormatter( "{$lang}", IntlDateFormatter::FULL, IntlDateFormatter::NONE, "{$timezone}", IntlDateFormatter::GREGORIAN, "{$format}" ); | |
$datetime = new DateTime( "{$date}", new DateTimeZone( "{$timezone}" ) ); | |
$fmt->setTimeZone( "{$timezone}" ); | |
return $fmt->format( $datetime ); | |
} | |
} // if ( ! function_exists( 'anwp_get_strtotime' ) ) | |
if ( ! function_exists( 'anwp_get_x_days' ) ) { | |
function anwp_get_x_days( $args = null ) { | |
if ( is_null( $args ) || empty( $args ) || ! is_array( $args ) ) { | |
$args = [ | |
'days' => 7, | |
'offset' => 0, | |
'direction' => 'last', // available: last || next | |
'format' => 'yyyy-MM-dd', | |
'timezone' => 'Europe/Vilnius', | |
'lang' => 'lt_LT', | |
]; | |
} | |
$args['days'] = ( ! isset( $args['days'] ) ) ? 7 : $args['days']; | |
if ( $args['days'] <= 0 ) { | |
$args['days'] = 1; | |
} | |
$args['offset'] = ( ! isset( $args['offset'] ) || ! is_integer( $args['offset'] ) ) ? 0 : abs( $args['offset'] ); | |
$args['direction'] = ( ! isset( $args['direction'] ) || ! is_string( $args['direction'] ) ) ? 'last' : mb_strtolower( $args['direction'], 'UTF-8' ); | |
$args['format'] = ( ! isset( $args['format'] ) ) ? 'yyyy-MM-dd' : $args['format']; | |
$args['timezone'] = ( ! isset( $args['timezone'] ) ) ? 'Europe/Vilnius' : $args['timezone']; | |
$args['lang'] = ( ! isset( $args['lang'] ) ) ? 'lt_LT' : $args['lang']; | |
// Patern http://userguide.icu-project.org/formatparse/datetime | |
/********************************************************************** | |
* *** | |
* 'yyyy-MM-dd HH:mm:ss' – 2019-11-03 04:13:41 | |
* 'GGGG' – po Kristaus | |
* 'cccc' – sekmadienis | |
* 'zzzz' – Rytų Europos žiemos laikas | |
* 'VV' – Europe/Vilnius | |
* 'OOOO' – GMT+02:00 | |
* "yyyy 'm.' MMMM 'mėn'. d 'd.'" – 2019 m. lapkričio mėn. 3 d. | |
* 'LLLL' – lapkritis | |
*********************************************************************/ | |
$fmt = new IntlDateFormatter( "{$args['lang']}", IntlDateFormatter::FULL, IntlDateFormatter::NONE, "{$args['timezone']}", IntlDateFormatter::GREGORIAN, "{$args['format']}" ); | |
$date_array = []; | |
$datetime = new DateTime( 'now', new DateTimeZone( "{$args['timezone']}" ) ); | |
if ( $args['offset'] !== 0 && $args['direction'] === 'next' ) { | |
$datetime->modify( "+{$args['offset']} day" ); | |
} else if ( $args['offset'] !== 0 && $args['direction'] === 'last' ) { | |
$datetime->modify( "-{$args['offset']} day" ); | |
} | |
for ( $i = 0; $i < $args['days']; $i++ ) { | |
$date_array[] = $fmt->format( $datetime ); | |
if ( $args['direction'] === 'last' ) { | |
$datetime->modify( '-1 day' ); | |
} else { | |
$datetime->modify( '+1 day' ); | |
} | |
} | |
// return array_reverse( $date_array ); | |
return $date_array; // funkcijose tik vienas veiksmas, reikalui esant, gautą rezultatą bus galima apversti | |
} | |
} // if ( ! function_exists( 'anwp_get_x_days' ) ) | |
if ( ! function_exists( 'anwp_get_first_easter_day' ) ) { | |
function anwp_get_first_easter_day( $year = NULL ) { | |
if ( is_null( $year ) ) { | |
return date( 'Y-m-d', easter_date() ); // esamų metų pirmoji Velykų diena | |
} | |
$year = (int) $year; | |
if ( is_integer( $year ) && ( 1970 <= $year && 2037 >= $year ) ) { | |
return date( 'Y-m-d', easter_date( $year ) ); // nurodytų metų pirmoji Velykų diena. Galimas metų rėžis: 1970-2037 imtinai | |
} | |
return NULL; | |
} | |
} // if ( ! function_exists( 'anwp_get_first_easter_day' ) ) | |
if ( ! function_exists( 'anwp_get_second_easter_day' ) ) { | |
function anwp_get_second_easter_day( $year = NULL ) { | |
if ( is_null( $year ) ) { | |
$date = new DateTime( date( 'Y-m-d', easter_date() ) ); | |
return $date->modify( '+1 day' )->format( 'Y-m-d' ); | |
} | |
$year = (int) $year; | |
if ( is_integer( $year ) && ( 1970 <= $year && 2037 >= $year ) ) { | |
return ( new DateTime( ( new DateTime() )->modify( date( 'Y-m-d', easter_date( $year ) ) )->format( 'Y-m-d' ) ) )->modify( '+1 day' )->format( 'Y-m-d' ); | |
} | |
return NULL; | |
} | |
} // if ( ! function_exists( 'anwp_get_second_easter_day' ) ) | |
if ( ! function_exists( 'anwp_validate_date' ) ) { | |
function anwp_validate_date( $date, $format = 'Y-m-d' ) { | |
$d = DateTime::createFromFormat( $format, $date ); | |
// return ( $d && ( $d->format( $format ) == $date ) ) ? $d : NULL; | |
return ( $d && ( $d->format( $format ) == $date ) ); | |
} | |
} // if ( ! function_exists( 'anwp_validate_date') ) | |
if ( ! function_exists( 'anwp_get_weekday_by_date' ) ) { | |
function anwp_get_weekday_by_date( $date = NULL ) { | |
if ( is_null( $date ) ) { | |
return NULL; | |
} | |
$d = DateTime::createFromFormat( 'Y-m-d', $date ); | |
if ( $d && ( $d->format( 'Y-m-d' ) === $date ) ) { | |
setlocale( LC_ALL, ['lt_LT.UTF-8', 'lt_LT', 'lt'] ); | |
$format = '%A'; // Pirmadienis | |
return strftime( $format, mktime( 0, 0, 0, $d->format( 'm' ), $d->format( 'd' ), $d->format( 'Y' ) ) ); | |
} | |
return NULL; | |
} | |
} // if ( ! function_exists( 'anwp_get_weekday_by_date' ) ) | |
if ( ! function_exists( 'anwp_get_formated_timestring_by_date' ) ) { | |
function anwp_get_formated_timestring_by_date( $date = NULL, $date_format = 'Y-m-d', $ftime_format = '%c' ) { | |
if ( is_null( $date ) ) { | |
return NULL; | |
} | |
$d = DateTime::createFromFormat( $date_format, $date ); | |
if ( $d && ( $d->format( $date_format ) === $date ) ) { | |
setlocale( LC_ALL, ['lt_LT.UTF-8', 'lt_LT', 'lt'] ); | |
// $ftime_format = '%A'; // Pirmadienis | |
// $ftime_format = '%a'; // Pr | |
// $ftime_format = '%B'; // Sausis | |
// $ftime_format = '%b'; // Sau | |
// $ftime_format = '%c'; // 2017 m. sausio 31 d. 00:00:00 | |
// $ftime_format = '%F'; // 2017-01-31 | |
// $ftime_format = '%D'; // 31/01/17 | |
return strftime( $ftime_format, mktime( $d->format( 'H' ), $d->format( 'i' ), $d->format( 's' ), $d->format( 'm' ), $d->format( 'd' ), $d->format( 'Y' ) ) ); | |
} | |
return NULL; | |
} | |
} // if ( ! function_exists( 'anwp_get_formated_timestring_by_date' ) ) | |
if ( ! function_exists( 'anwp_make_slug' ) ) { | |
/* | |
|------------------------------------------------------------------------------ | |
| gautą eilute padaro tik lotyniškomis raidėmis, skaičiais ir - ženklais | |
|------------------------------------------------------------------------------ | |
*/ | |
function anwp_make_slug( $string = '' ) { | |
if ( ! is_string( $string ) ) { | |
return null; | |
} | |
// išsaugome esamę lokalę ir pakeičiame į lietuvišką | |
// $orig_locale = setlocale( LC_CTYPE, 0 ); | |
// setlocale( LC_CTYPE, 'lt_LT.utf8' ); | |
// eilutės pradžioje ir pabaigoje panaikiname tarpus | |
$string = trim( $string ); | |
$string = anwp_string_to_latin( $string ); | |
// visus nurodytus simbolius pakeičiame - (minus) ženklu | |
$string = str_replace( [' ', '_', '/', '\\', '|', '.', ',', '\'', '"', '`'], '-', $string ); | |
// paliekame tik lotyniškas raides ir spec. simbolius | |
// $string = iconv( 'UTF-8', 'ASCII//TRANSLIT', $string ); | |
// visos raidės padaromos mažosiomis bei | |
// paliekame tik raides, skaičius ir - (minus) ženklus | |
$string = preg_replace( '/[^a-zA-Z0-9\-]+/', '', mb_strtolower( $string ) ); | |
// grąžiname buvusią lokalę | |
// setlocale( LC_CTYPE, $orig_locale ); | |
// iš eilės daugiau nei po vieną einančius - (minus) ženklus pakeičiame vieninteliu - (minus) ženklu | |
// ir pradžioje bei pabaigoje panaikinami - (minus) ženklai, jei jie yra | |
return trim( preg_replace( '/\-+/', '-', $string ), '-' ); | |
} | |
} // if ( ! function_exists( 'anwp_make_slug' ) ) | |
if ( ! function_exists( 'anwp_string_to_latin' ) ) { | |
function anwp_string_to_latin( $string = '' ) { | |
if ( ! is_string( $string ) ) { | |
return null; | |
} | |
// any character will firstly be converted to a latin character. If that's finished, replace all latin characters by their ASCII replacement | |
// pirmoje versijoje visos raidės padaromos mažosiomis | |
// antra versija € konvertuoja į EUR, taip pat šioje versijoje nereikia keisti lokalės | |
// http://php.net/manual/en/transliterator.transliterate.php | |
// $string = transliterator_transliterate( 'Any-Latin; Latin-ASCII; Lower()', $string ); | |
// grąžintą rezultatą galima dar apdoroti su $string = str_replace( ['\'', '"', '`'], '', $string ); komanda | |
return iconv( 'UTF-8', 'ASCII//TRANSLIT//IGNORE', transliterator_transliterate( 'Any-Latin; Latin-ASCII', $string ) ); | |
} | |
} | |
// Nuo PHP 5.4.8 versijos. Iki šios versijos NULL grąžindavo 0 | |
if ( ! function_exists( 'mb_ucfirst' ) && function_exists( 'mb_substr' ) ) { | |
function mb_ucfirst( $string = '' ) { | |
if ( version_compare( PHP_VERSION, '5.4.0') >= 0 ) { | |
return mb_strtoupper( mb_substr( $string, 0, 1, 'UTF-8' ) ) . mb_substr( $string, 1, NULL, 'UTF-8' ); | |
} else { | |
return NULL; | |
} | |
} | |
} // if ( ! function_exists( 'mb_ucfirst' ) && function_exists( 'mb_substr' ) ) | |
if ( ! function_exists( 'mb_string_to_array' ) ) { | |
function mb_string_to_array( $string = '', $length = 1 ) { | |
$strlen = mb_strlen( $string, 'utf-8' ); | |
while ( $strlen ) { | |
$array[] = mb_substr( $string, 0, $length, 'utf-8' ); | |
$string = mb_substr( $string, $length, $strlen, 'utf-8' ); | |
$strlen = mb_strlen( $string, 'utf-8' ); | |
} | |
return $array; | |
} | |
} // if ( ! function_exists( 'mb_string_to_array' ) ) | |
if ( ! function_exists( 'mb_str_split' ) ) { | |
/********************************************************************** | |
* *** | |
* Returns an array containing substrings of subject | |
* split along boundaries matched by pattern, | |
* or FALSE on failure. | |
*********************************************************************/ | |
function mb_str_split( $string = '', $length = 1 ) { | |
mb_regex_encoding( 'utf-8' ); | |
mb_internal_encoding( 'utf-8' ); | |
# Split at all position not after the start: ^ | |
# and not before the end: $ | |
// return preg_split( '/(?<!^)(?!$)/u', $string, null, PREG_SPLIT_NO_EMPTY ); | |
return preg_split( '/(.{' . $length . '})/us', $string, null, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE ); | |
} | |
} // if ( ! function_exists( 'mb_str_split' ) ) | |
if ( ! function_exists( 'anwp_mb_str_rand' ) ) { | |
function anwp_mb_str_rand( $length = 32, $characters = '0123456789aąbcčdeęėfghiįyjklmnoprsštuųūvzžwxqAĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽWXQ ' ) { | |
if ( ! is_int( $length ) || $length < 0 ) { | |
return false; | |
} | |
// $characters_array = mb_string_to_array( $characters ); | |
$characters_array = mb_str_split( $characters ); | |
// $characters_array = preg_split( '//u', $characters, null, PREG_SPLIT_NO_EMPTY ); | |
$characters_array_length = count( $characters_array ) - 1; | |
$string = ''; | |
for ( $i = $length; $i > 0; $i-- ) { | |
$string .= $characters_array[ mt_rand( 0, $characters_array_length ) ]; | |
} | |
return $string; | |
} | |
} // if ( ! function_exists( 'anwp_mb_str_rand' ) ) | |
if ( ! function_exists( 'anwp_get_file_info' ) ) { | |
/** | |
* Get File Info | |
* | |
* Given a file and path, returns the name, path, size, date modified | |
* Second parameter allows you to explicitly declare what information you want returned | |
* Options are: name, server_path, size, date, readable, writable, executable, fileperms | |
* Returns FALSE if the file cannot be found. | |
* | |
* @param string path to file | |
* @param mixed array or comma separated string of information returned | |
* @return array | |
*/ | |
function anwp_get_file_info( $file_path, $view_path = ABSPATH, $returned_values = array( 'name', 'server_path', 'size', 'date' ) ) { | |
if ( mb_substr( $file_path, 1, 1 ) === '/' ) { | |
$view_path = rtrim( $view_path, '/' ); | |
} | |
$file = $view_path . $file_path; | |
if ( ! file_exists( $file ) ) { | |
return FALSE; | |
} | |
if ( is_string( $returned_values ) ) { | |
$returned_values = explode( ',', $returned_values ); | |
} | |
if ( $returned_values === true ) { | |
$returned_values = ['name', 'server_path', 'size', 'date', 'readable', 'writable', 'executable', 'fileperms']; | |
} | |
foreach ( $returned_values as $key ) { | |
switch ( $key ) { | |
case 'name': | |
$fileinfo['name'] = basename( $file ); | |
break; | |
case 'server_path': | |
$fileinfo['server_path'] = $file; | |
break; | |
case 'size': | |
$fileinfo['size'] = filesize( $file ); | |
break; | |
case 'date': | |
$fileinfo['date'] = filemtime( $file ); | |
break; | |
case 'readable': | |
$fileinfo['readable'] = is_readable( $file ); | |
break; | |
case 'writable': | |
// $fileinfo['writable'] = is_really_writable( $file ); | |
$fileinfo['writable'] = is_writable( $file ); | |
break; | |
case 'executable': | |
$fileinfo['executable'] = is_executable( $file ); | |
break; | |
case 'fileperms': | |
$fileinfo['fileperms'] = fileperms( $file ); | |
break; | |
} | |
} | |
return $fileinfo; | |
} | |
} // if ( ! function_exists( 'anwp_get_file_info' ) ) | |
if ( ! function_exists( 'anwp_file_exists' ) ) { | |
/* | |
|------------------------------------------------------------------------------ | |
| Jei failas egzistuoja, grąžina pilną kelią iki jo | |
|------------------------------------------------------------------------------ | |
*/ | |
function anwp_file_exists( $file_path, $view_path = ABSPATH ) { | |
if ( mb_substr( $file_path, 1, 1 ) === '/' ) { | |
$view_path = rtrim( $view_path, '/' ); | |
} | |
if ( $file = anwp_get_file_info( $file_path, $view_path, array( 'server_path' ) ) ) { | |
return $file[ 'server_path' ]; | |
} | |
else { | |
return FALSE; | |
} | |
} | |
} // if ( ! function_exists( 'anwp_file_exists' ) ) | |
if ( ! function_exists( 'anwp_get_file_content' ) ) { | |
/* | |
|------------------------------------------------------------------------------ | |
| Jei failas egzistuoja, grąžina jo turinį | |
|------------------------------------------------------------------------------ | |
*/ | |
function anwp_get_file_content( $file_path, $view_path = ABSPATH ) { | |
if ( mb_substr( $file_path, 1, 1 ) === '/' ) { | |
$view_path = rtrim( $view_path, '/' ); | |
} | |
if ( $file = anwp_get_file_info( $view_path . $file_path, array( 'server_path' ) ) ) { | |
return trim( file_get_contents( $file[ 'server_path' ] ) ); | |
} | |
else { | |
return FALSE; | |
} | |
} | |
} // if ( ! function_exists( 'anwp_get_file_content' ) ) | |
if ( ! function_exists( 'anwp_get_url_content' ) ) { | |
/** | |
* Fetch cont wit curl(). | |
* | |
* @link anwp_get_url_content function by Andy Langton: https://andylangton.co.uk/ | |
* @link https://andylangton.co.uk/blog/development/use-php-and-curl-retrieve-url-contents-few-options | |
* | |
* @param string $url | |
* @param string $useragent | |
* @param string|boolean $headers | |
* @param array $http_header | |
* @param array $data_array | |
* @param boolean $follow_redirects | |
* @param boolean $debug | |
* @return array | |
*/ | |
function anwp_get_url_content( string $url, string $useragent = 'cURL', string|bool $headers = false, array $http_header = array(), array $data_array = array(), bool $follow_redirects = false, bool $debug = false ) { // phpcs:ignore Generic.PHP.Syntax.PHPSyntax | |
$result = array(); | |
// initialise the CURL library. | |
$ch = curl_init(); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_init | |
if ( ! empty( $data_array ) ) { | |
$data = http_build_query( $data_array ); | |
$url = $url . '?' . $data; | |
} | |
// specify the URL to be retrieved. | |
curl_setopt( $ch, CURLOPT_URL, $url ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
// we want to get the contents of the URL and store it in a variable. | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
// specify the useragent: this is a required courtesy to site owners. | |
curl_setopt( $ch, CURLOPT_USERAGENT, $useragent ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
// ignore SSL errors. | |
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
// return headers as requested. | |
if ( true === $headers ) { | |
curl_setopt( $ch, CURLOPT_HEADER, 1 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
} | |
// only return headers. | |
if ( 'headers only' === $headers ) { | |
curl_setopt( $ch, CURLOPT_NOBODY, 1 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
} | |
if ( ! empty( $http_header ) ) { | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_header ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
} | |
// follow redirects - note this is disabled by default in most PHP installs from 4.4.4 up. | |
if ( true === $follow_redirects ) { | |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt | |
} | |
if ( true === $debug ) { | |
// if debugging, return an array with CURL's debug info and the URL contents. | |
$result['contents'] = curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec | |
$result['info'] = curl_getinfo( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_getinfo | |
} else { | |
// otherwise just return the contents as a variable. | |
$result['contents'] = curl_exec( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_exec | |
} | |
if ( true === $headers ) { | |
$header_size = curl_getinfo( $ch, CURLINFO_HEADER_SIZE ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_getinfo | |
$result['header'] = $result['contents']; | |
$result['contents'] = substr( $result['contents'], $header_size ); | |
} | |
// free resources. | |
curl_close( $ch ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_close | |
// send back the data. | |
return $result; | |
} | |
} | |
if ( ! function_exists( 'anwp_array_pluck' ) ) { | |
/* | |
|------------------------------------------------------------------------------ | |
| iš asociatyvaus masyvo gauti duomenis pagal masyvo elemento raktą (stulpelius) | |
|------------------------------------------------------------------------------ | |
*/ | |
function anwp_array_pluck( $col_name, $array ) { | |
return array_map( function( $item ) use( $col_name ) { | |
return $item[ $col_name ]; | |
}, $array ); | |
} | |
} // if ( ! function_exists( 'anwp_array_pluck' ) ) | |
/********************************************************************** | |
* *** | |
* EXAMPLES | |
*********************************************************************/ | |
// anwp_debug( ABSPATH, 'vep' ); | |
// anwp_debug( anwp_get_file_info( __FILE__ ) ); | |
// anwp_debug( anwp_file_exists( __FILE__, '' ) ); | |
// anwp_debug( anwp_file_exists( ABSPATH . 'wp-content/plugins/anwp-helpers/includes/anwp-helpers-functions-various.php', '' ), 'vdp' ); | |
// anwp_debug( [ | |
// 'ABSPATH' => ABSPATH, | |
// '__FILE__' => __FILE__, | |
// '__DIR__' => __DIR__, | |
// 'fInfo' => anwp_get_vep( anwp_get_file_info( __FILE__, '', true ) ), | |
// 'fExists1' => anwp_file_exists( __FILE__, '' ), | |
// 'fExists2' => anwp_file_exists( ABSPATH . 'wp-content/plugins/anwp-helpers/includes/anwp-helpers-functions.php', '' ), | |
// 'anwp_make_slug' => anwp_make_slug( 'Hello Dolly\nėra tik paprastas įskiepis – kartu jis yra visos kartos vilties ir ryžto simbolis, sudėtas į du žodžius, € – Самые большие реки - Волга, Обь, Енисей, Лена и Амур' ), | |
// 'anwp_make_slug2' => anwp_make_slug( anwp_file_exists( ABSPATH . 'wp-content/plugins/anwp-helpers/includes/anwp-helpers-functions.php', '' ) ), | |
// 'anwp_string_to_latin' => anwp_string_to_latin( '€ – Самые большие 2 города России - Москва, Санкт-Петербург, Екатеринбург, Нижний Новгород, Самара, Омск | Её общая площадь составляет около семнадцати миллионов квадратных километров | soft sign ь and hard sign ъ | Ъ | Ь' ), | |
// 'mb_str_split' => anwp_get_vep( mb_str_split( 'Dolly \ nėra квадратных большие – €' ) ), | |
// 'anwp_get_vdp' => anwp_get_vdp( 'Dolly \ nėra квадратных большие – €' ), | |
// 'mb_string_to_array' => anwp_get_vep( mb_string_to_array( 'Dolly \ nėra квадратных большие – €' ) ), | |
// 'anwp_mb_str_rand' => anwp_mb_str_rand(), | |
// ], 'vep', false ); | |
// anwp_debug( mb_string_to_array( 'Dolly \ nėra квадратных большие – €' ) ); | |
// anwp_debug( anwp_mb_str_rand( 5 ) ); | |
// anwp_debug( anwp_get_url_content( 'https://perlas.lt/lt/index' ), 'pp', true ); | |
// anwp_debug( anwp_get_first_easter_day( 2037 ), 'vep' ); | |
// anwp_debug( anwp_get_second_easter_day( 2037 ), 'vep' ); | |
// anwp_debug( anwp_validate_date( '0099-11-03', 'Y-m-d' ), 'vep' ); | |
// anwp_debug( anwp_get_weekday_by_date( '2099-12-31' ), 'vep' ); | |
// anwp_debug( anwp_get_formated_timestring_by_date( '2099-12-31', 'Y-m-d', '%c (%A)' ), 'vep' ); | |
// anwp_debug( anwp_get_formated_timestring_by_date( '2099-12-31 01:30:59', 'Y-m-d H:i:s', '%c' ), 'vep' ); | |
// anwp_debug( anwp_script_run_time() . ' sek.', 'vep' ); | |
// anwp_debug( anwp_get_x_days( [ 'direction' => 'next', 'offset' => 1, 'format' => "yyyy-MM-dd HH:mm:ss – cccc" ] ) ); | |
// anwp_debug( anwp_get_strtotime( '-42 years june 28', "yyyy 'metų' MMMM 'mėnesio' d 'diena' (cccc)" ) ); | |
// anwp_debug( anwp_get_strtotime( 'midnight first day of last month', "yyyy 'metų' (GGGG) MMMM 'mėnesio' d 'diena' (cccc) HH:mm:ss" ) ); | |
// anwp_debug( anwp_get_strtotime( 'first day of next month', "yyyy 'metų' (GGGG) MMMM 'mėnesio' d 'diena' (cccc) HH:mm:ss" ) ); | |
// anwp_debug( anwp_get_strtotime( 'midnight 0001-01-01', "yyyy 'metų' (GGGG) MMMM 'mėnesio' d 'diena' (cccc) HH:mm:ss" ) ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment