Files
pkrstarsbot/client_webapp/code-snippets/HelperFunctions.php
2021-02-16 23:07:41 +01:00

64 lines
1.6 KiB
PHP

<?php
class HelperFunctions {
function declareGlobalsFn () {
// Functions declared inside a function have global scope
function page_id() {
global $wp_query;
return $wp_query->post->ID;
}
function php_console_log($data) {
$output = $data;
if (is_array($output)) {
$output = implode(',', $output);
}
echo "<script>console.log( '" . $output . "' );</script>";
}
function current_url() {
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
function redirect($url, $statusCode = 303) {
header('Location: ' . $url, true, $statusCode);
die();
}
function connect_to_server($server) {
if ($server == 'zinomedia') {
return new wpdb('pkrstarsbot','ichpkrstarsbot#1337','pkrstarsbot','localhost');
}
}
function generateHiddenInputsFromArray( $data = array(), $namePrefix = '' ) {
if(!is_array($data) || empty($data)) {
return '';
}
$html = "";
$namePrefix = trim($namePrefix);
if ( is_array( $data ) && !empty($data) ) {
foreach ( $data as $key => $value) {
$keyEsc = htmlentities($key);
if($namePrefix != '') {
$keyEsc = $namePrefix."[{$keyEsc}]";
}
if(is_array($value)) {
$html .= generateHiddenInputsFromArray($value,$keyEsc);
} else {
$valueEsc = htmlentities($value);
$html .= "<input type='hidden' name='{$keyEsc}' value='{$valueEsc}' />".PHP_EOL;
}
}
}
return $html;
}
}
}
$ob = new HelperFunctions();
$ob->declareGlobalsFn();