Last active
April 9, 2019 06:54
-
-
Save monkeymonk/28d7fa52cbc5e833261480c7b009125b to your computer and use it in GitHub Desktop.
Wordpress Helpers class #wordpress
This file contains hidden or 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
{ | |
"require": { | |
"raveren/kint": "^0.9.1", | |
"illuminate/database": "~5.0", | |
"illuminate/validation": "^5.2" | |
}, | |
"autoload": { | |
"psr-4": { | |
"WeDevs\\ORM\\": "workbench/tareq1988/wp-eloquent/src/" | |
} | |
} | |
} |
This file contains hidden or 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 | |
use Illuminate\Validation\Factory as ValidatorFactory; | |
use Symfony\Component\Translation\Translator; | |
/** | |
* Class Helpers | |
* | |
* Some useful methods | |
* | |
* @see https://github.com/MozaikAgency/wp-theme-starter/blob/master/theme/ | |
*/ | |
class Helpers | |
{ | |
public static function arrayToCSV($array, $delimiter = ';', $downloadFile = null) | |
{ | |
if ($downloadFile) { | |
header('Content-Encoding: UTF-8'); | |
header('Content-Type: text/csv; charset=utf-8'); | |
header('Content-Disposition: attachement; filename="' . $downloadFile . '"'); | |
} | |
ob_start(); | |
$f = fopen('php://output', 'w') or ("Can't open php://output"); | |
$n = 0; | |
foreach ($array as $line) { | |
$n++; | |
if (!fputcsv($f, $line, $delimiter)) { | |
("Can't write line $n: $line"); | |
} | |
} | |
fclose($f) or ("Can't close php://output"); | |
$str = ob_get_contents(); | |
ob_end_clean(); | |
if (!$downloadFile) { | |
return $str; | |
} | |
echo $str; | |
die(); | |
} | |
public static function checkPassword($input, $hashed) | |
{ | |
return wp_check_password($input, $hashed); | |
} | |
public static function crypt($input, $salt = 'd41d8cd98f00b204e9800998ecf8427e') | |
{ | |
return base64_encode($input . $salt); | |
} | |
public static function decrypt($input, $salt = 'd41d8cd98f00b204e9800998ecf8427e') | |
{ | |
return str_replace($salt, '', base64_decode($input)); | |
} | |
public static function isAjax() | |
{ | |
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'); | |
} | |
public static function getIp() | |
{ | |
$client = null; | |
$forward = null; | |
if (isset($_SERVER['HTTP_CLIENT_IP'])) { | |
$client = $_SERVER['HTTP_CLIENT_IP']; | |
} | |
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { | |
$forward = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
} | |
$remote = $_SERVER['REMOTE_ADDR']; | |
if (filter_var($client, FILTER_VALIDATE_IP)) { | |
$ip = $client; | |
} elseif(filter_var($forward, FILTER_VALIDATE_IP)) { | |
$ip = $forward; | |
} else { | |
$ip = $remote; | |
} | |
return $ip; | |
} | |
public static function hashPassword($input) | |
{ | |
return wp_hash_password($input); | |
} | |
public static function jsonResponse($data, $status = 200, $msg = '') | |
{ | |
$response = [ | |
'data' => $data, | |
'msg' => $msg, | |
'status' => $status, | |
]; | |
// @see http://stackoverflow.com/a/18204039/1420009 | |
status_header($status); | |
header("content-type: application/json"); | |
die(json_encode($response)); | |
} | |
public static function remove_accents($str) | |
{ | |
$accents_to_remove = array('ά', 'έ', 'ή', 'ί', 'ό', 'ύ', 'ώ', 'Ά', 'Έ', 'Ή', 'Ί', 'ΐ', 'Ό', 'Ύ', 'Ώ', 'ς', 'À', 'Â', 'Á', 'Ã', 'Ä', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Î', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'à', 'à', 'á', 'â', 'ã', 'ä', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ò', 'ó', 'ô', 'õ', 'ù', 'ú', 'û', 'ü'); | |
$replace_with = array('α', 'ε', 'η', 'ι', 'ο', 'υ', 'ω', 'Α', 'Ε', 'Η', 'Ι', 'ι', 'Ο', 'Υ', 'Ω', 'Σ', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u'); | |
return str_replace($accents_to_remove, $replace_with, remove_accents($str)); | |
} | |
/** | |
* Create a script tag | |
* | |
* @example | |
* Helpers::script('console.log("foo!");'); // return "<script>console.log("foo!");</script>" | |
* | |
* @example | |
* Helpers::script([ | |
* 'console.log("foo!");', | |
* 'console.log("bar!");' | |
* ], true); // return "<script>$(document).ready(function () {console.log("foo!"); console.log("bar!");});</script>" | |
* | |
* @param $scripts | |
* @param bool $domReady | |
* @return string | |
*/ | |
public static function script($scripts, $domReady = false) | |
{ | |
if (!is_array($scripts)) { | |
$scripts = [$scripts]; | |
} | |
if ($domReady) { | |
array_unshift($scripts, '$(document).ready(function () {'); | |
$scripts[] = '});'; | |
} | |
return '<script>' . implode(' ', $scripts) . '</script>'; | |
} | |
/** | |
* @example | |
* Utils::slugify('Hello world!'); // return "hello-world" | |
* | |
* @param string $string - Text to slugify | |
* @return string | |
*/ | |
public static function slugify($string) | |
{ | |
return strtolower(preg_replace(['/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'], ['', '-', ''], remove_accents($string))); | |
} | |
/** | |
* Create a CSS rule in a style tag | |
* | |
* @example | |
* Html::style('.my-selector', [ | |
* 'background-color' => '#fff', | |
* 'color' => '#000', | |
* ]); // return "<style>.my-selector { background-color: #fff; color: #000; }</style>" | |
* | |
* @example | |
* Html::style([ | |
* '.my-selector' => [ | |
* 'background-color' => '#fff', | |
* 'color' => '#000', | |
* ] | |
* ]); // return "<style>.my-selector { background-color: #fff; color: #000; }</style>" | |
* | |
* @param $styles | |
* @param null $rules | |
* @return string | |
*/ | |
public static function style($styles, $rules = null) | |
{ | |
$instructions = []; | |
if (!is_array($styles)) { | |
$tmp = []; | |
$tmp[$styles] = $rules; | |
$styles = $tmp; | |
} | |
foreach ($styles as $selector => $style) { | |
$style = []; | |
foreach ($style as $key => $rule) { | |
$style[] = $key . ': ' . $rule . ';'; | |
} | |
$instructions[] = $selector . '{' . implode(' ', $style) . '} '; | |
} | |
return '<style>' . implode(' ', $instructions) . '</style>'; | |
} | |
public static function validator($data, $rules, $language = 'en') | |
{ | |
$factory = new ValidatorFactory(new Translator($language)); | |
return $factory->make($data, $rules); | |
} | |
public static function validate($data, $rules, $language = 'en') | |
{ | |
$validator = self::validator($data, $rules, $language); | |
if ($validator->fails()) { | |
return $validator->messages()->toArray(); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment