Skip to content

Instantly share code, notes, and snippets.

View LucaRosaldi's full-sized avatar

Luca Rosaldi LucaRosaldi

View GitHub Profile
@LucaRosaldi
LucaRosaldi / pretty-print.php
Last active February 24, 2022 10:23
PHP: Pretty Print (Debug function)
<?php
/**
* Pretty Print.
*
* @param [mixed] $val The value to print
*/
function pp( $value ) {
switch ( $type = gettype( $value ) ) {
@LucaRosaldi
LucaRosaldi / sql_woocommerce_get_customers.mysql
Last active February 24, 2022 10:17
WordPress, WooCommerce, MySQL: Get list of customers with at least one order placed (for email marketing)
SELECT wp_users.user_email as "Email", firstmeta.meta_value AS "First Name", lastmeta.meta_value AS "Last Name", ordermeta.meta_value AS "Orders Placed" FROM wp_users
LEFT JOIN wp_usermeta as firstmeta on wp_users.ID = firstmeta.user_id and firstmeta.meta_key = 'first_name'
LEFT JOIN wp_usermeta as lastmeta on wp_users.ID = lastmeta.user_id and lastmeta.meta_key = 'last_name'
LEFT JOIN wp_usermeta as ordermeta on wp_users.ID = ordermeta.user_id and ordermeta.meta_key = '_order_count'
WHERE wp_users.ID IN (
SELECT user_id FROM wp_usermeta WHERE meta_key = '_order_count' AND meta_value > 0
)
@LucaRosaldi
LucaRosaldi / console-debug-overflow.js
Last active February 24, 2022 10:15
JS: Paste in console to show elements which overflow the viewport horizontally.
/* Paste this snippet in the console */
var docWidth = document.documentElement.offsetWidth;
[].forEach.call(
document.querySelectorAll('*'),
function(el) {
if (el.offsetWidth > docWidth) {
console.log(el);
}
}
@LucaRosaldi
LucaRosaldi / get-browser-language-code.php
Last active February 24, 2022 10:13
PHP: Get Browser Language, optionally passing a list of available languages.
<?php
/**
* Get browser language, optionally passing a list of available languages.
*
* @param [array] $available_languages Available languages for the site
* @param [string] $default Default language for the site
* @return [string] Language code
*/
function get_browser_language_code( $available_languages = [], $default = 'en' ) : string
{
@LucaRosaldi
LucaRosaldi / container-queries.css
Last active January 7, 2022 10:13
CSS: Container Queries
.widget-name {
container: inline-size / widget-name;
/* Shorthand for: */
/* container-type: inline-size; */
/* container-name: widget-name; */
/* For quick testing, do this to get a resize handle on desktop: */
/* resize: both; */
/* overflow: hidden; */
}
@LucaRosaldi
LucaRosaldi / utils.js
Last active July 7, 2021 19:22
JS: utilities module
/**
* Check if passed argument is a callable function.
*
* @param {Callable} func
* @return {Boolean}
*/
export function isFunction( func ) {
return func && {}.toString.call( func ) === '[object Function]';
}
@LucaRosaldi
LucaRosaldi / input-numeric.html
Created May 5, 2021 06:28
HTML: Better altenative to input type="number"
<!-- https://technology.blog.gov.uk/2020/02/24/why-the-gov-uk-design-system-team-changed-the-input-type-for-numbers/ -->
<input type="text" inputmode="numeric" pattern="[0-9]*">
@LucaRosaldi
LucaRosaldi / touch-query.css
Created March 12, 2020 09:01
CSS: Media Query for touch and non-touch devices
@media (hover: hover) {
/* targets only non-touch devices */
}
@media (hover: none) {
/* targets only touch devices */
}
@LucaRosaldi
LucaRosaldi / get_user_ip.php
Last active September 25, 2019 07:42
PHP: Get User Geolocation with ipgeolocationapi.com
<?php
/**
* Get the user IP address from the server request.
*
* @return string
*/
function get_user_ip() : string {
if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
return $_SERVER[ 'HTTP_CLIENT_IP' ];
@LucaRosaldi
LucaRosaldi / .htaccess
Created September 17, 2019 12:47
APACHE: Redirect to HTTPS
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]