updates from most recent website build

This commit is contained in:
Steve Ross
2024-12-20 17:09:16 +00:00
parent c437137134
commit 586da211df
46 changed files with 3087 additions and 22 deletions

168
app/Utilities/Colour.php Normal file
View File

@@ -0,0 +1,168 @@
<?php
namespace App\Utilities;
class Colour
{
public function name2hex($colour = null, $tint = null)
{
if(!$colour) return false;
if($colour == 'black'):
$hex = '#000000';
elseif($colour == 'white'):
$hex = '#FFFFFF';
else:
// TODO: replace company_info settings page and lookup function
$hex = $this->values()[(string)$colour];
endif;
if($tint):
$tints = $this->tints();
$hex = $this->adjustBrightness($hex, $tints[$tint]);
endif;
return $hex;
}
public function values()
{
$colours = get_field('badegg_colours', 'option');
$values = [];
if($colours):
foreach($colours as $index => $props):
$index = $index + 1;
$hex = @$props['hex'];
if($hex) $values[$this->latinate($index)] = $hex;
endforeach;
endif;
$values['0'] = '#FFFFFF';
$values['black'] = '#000000';
return $values;
}
public function tints()
{
return [
'lightest' => 100,
'lighter' => 66,
'light' => 33,
'0' => 0,
'dark' => -33,
'darker' => -66,
'darkest' => -100,
];
}
public function is_dark($colour = '#FFFFF', $tint = null, $override = null)
{
if($override == 'light') return true;
if($override == 'dark') return false;
// https://css-tricks.com/snippets/php/convert-hex-to-rgb/
if($tint) $colour = $this->adjustBrightness($colour, $this->tints()[$tint]);
if ( @$colour[0] == '#' ) {
$colour = substr( $colour, 1 );
}
if ( strlen( $colour ) == 6 ) {
list( $r, $g, $b ) = [
$colour[0] . $colour[1],
$colour[2] . $colour[3],
$colour[4] . $colour[5],
];
} elseif ( strlen( $colour ) == 3 ) {
list( $r, $g, $b ) = [
$colour[0] . $colour[0],
$colour[1] . $colour[1],
$colour[2] . $colour[2],
];
} else {
return false;
}
$r = hexdec( $r );
$g = hexdec( $g );
$b = hexdec( $b );
// return array( 'red' => $r, 'green' => $g, 'blue' => $b );
$hsp = sqrt(
0.299 * ($r * $r) +
0.587 * ($g * $g) +
0.114 * ($b * $b)
);
if($hsp > 200) {
return false;
} else {
return true;
}
}
public function is_light($colour = '#000000', $tint = null)
{
if($this->is_dark($colour, $tint)) {
return false;
} else {
return true;
}
}
public function adjustBrightness($hex, $steps)
{
// Steps should be between -255 and 255. Negative = darker, positive = lighter
$steps = max(-255, min(255, $steps));
// Normalize into a six character long hex string
$hex = str_replace('#', '', $hex);
if (strlen($hex) == 3) {
$hex = str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2);
}
// Split into three parts: R, G and B
$color_parts = str_split($hex, 2);
$return = '#';
foreach ($color_parts as $color) {
$color = hexdec($color); // Convert to decimal
$color = max(0,min(255,$color + $steps)); // Adjust color
$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
}
return $return;
}
public function latinate($x = 0)
{
$latinate = [
1 => 'primary',
2 => 'secondary',
3 => 'tertiary',
4 => 'quaternary',
5 => 'quinary',
6 => 'senary',
7 => 'septenary',
8 => 'octonary',
9 => 'nonary',
10 => 'denary',
11 => 'undenary',
12 => 'duodenary',
];
if(array_key_exists($x, $latinate)):
return $latinate[$x];
else:
return 0;
endif;
}
}

View File

@@ -0,0 +1,105 @@
<?php
namespace App\Utilities;
class CssClasses {
public function section($props = [])
{
$Colour = new Colour;
$hex = $Colour->name2hex(@$props['bg_colour'], @$props['bg_tint']);
$pattern = @$props['pattern'];
$pattern_top = @$props['pattern_top'];
$pattern_bottom = @$props['pattern_bottom'];
$classes = [
'section',
'section-' . str_replace('acf/', '', $props['name']),
'bg-' . $this->colourTint([
'colour' => @$props['bg_colour'],
'tint' => @$props['bg_tint'],
]),
];
if($Colour->is_dark($hex) && $this->is_knockout_block($props['name']))
$classes[] = 'knockout';
if(@$props['padding_top'])
$classes[] = 'section-zero-top';
if(@$props['padding_bottom'])
$classes[] = 'section-zero-bottom';
if($pattern):
if($pattern == 'both'):
$classes[] = 'pattern-top';
$classes[] = 'pattern-bottom';
else:
$classes[] = 'pattern-' . $pattern;
endif;
if(in_array($pattern, ['top', 'both']))
$classes[] = 'pattern-top-' . $this->colourTint($pattern_top);
if(in_array($pattern, ['bottom', 'both']))
$classes[] = 'pattern-bottom-' . $this->colourTint($pattern_bottom);
endif;
if(@$props['bg_image'])
$classes[] = "bg-watermarked";
if(@$props['className']) $args = array_merge($classes, explode(' ', $props['className']));
return $classes;
}
public function button($args = [])
{
$default_args = [
'colour' => null,
'style' => null,
];
$args = wp_parse_args($args, $default_args);
$classes = [
'button',
];
if($args['colour']) $classes[] = $args['colour'];
if($args['style']) $classes[] = $args['style'];
return $classes;
}
public function colourTint($props = [])
{
if(@$props['colour']):
$colour = $props['colour'];
if($props['colour'] != 'black' && @$props['tint']):
$colour .= '-' . $props['tint'];
endif;
else:
$colour = 'white';
endif;
return $colour;
}
public function is_knockout_block($name = null)
{
$blacklist = [
'bad-example',
];
if(in_array($name, $blacklist)):
return false;
else:
return true;
endif;
}
}

View File

@@ -0,0 +1,124 @@
<?php
namespace App\Utilities;
class ImageSrcset
{
public function add($args = [])
{
$args = wp_parse_args($args, $this->default_args());
$multipliers = $this->multipliers();
if(is_null($args['height'])) $args['height'] = $args['width'];
if($args['sizes'] < 5) $multipliers = array_slice($multipliers, 0, $args['sizes']);
foreach ( $multipliers as $slug => $scale ):
add_image_size (
$args['name'] . '-' . $slug,
round((int)$args['width'] * $scale),
round((int)$args['height'] * $scale),
$args['crop']
);
endforeach;
}
public function default_args()
{
return [
'name' => 'hero',
'width' => 1920,
'height' => null,
'crop' => true,
'sizes' => 5,
];
}
public function multipliers()
{
return [
'xl' => 1,
'lg' => 0.75,
'md' => 0.52083333,
'sm' => 0.33333333,
'xs' => 0.20833333,
];
}
public function render($args = [])
{
global $_wp_additional_image_sizes;
$default_args = [
'name' => 'hero',
'image' => null,
'lazy' => true,
'sizes' => 5,
'class' => null,
];
$args = wp_parse_args($args, $default_args);
$name = $args['name'];
$image = ($args['image']) ? $args['image'] : get_post_thumbnail_id();
if(!$image) return false;
$properties = @$_wp_additional_image_sizes[$name . '-xl'];
$width = @$properties['width'];
$height = @$properties['height'];
$sizes = [
'xl' => 1,
'lg' => 0.75,
'md' => 0.52083333,
'sm' => 0.33333333,
'xs' => 0.20833333,
];
if($args['sizes'] < 5) $sizes = array_slice($sizes, 0, $args['sizes']);
$class = $name . '-image';
if($args['class']) $class .= ' ' . $args['class'];
ob_start();
$full = wp_get_attachment_image_src($image, $name . '-xl');
$lazy = wp_get_attachment_image_src($image, 'lazy');
$alt = get_post_meta( $image, '_wp_attachment_image_alt', true );
$srcsets = [];
foreach($sizes as $size => $multiplier) {
$file = wp_get_attachment_image_src($image, $name . '-' . $size);
$srcsets[] = $file[0] . ' ' . $file[1] . 'w';
}
$atts = [
'class' => $class,
'src' => $full[0],
'srcset' => implode(', ', $srcsets),
'width' => $width,
'height' => $height,
'alt' => $alt,
];
if($args['lazy']):
$atts['class'] .= ' lazy';
$atts['src'] = $lazy[0];
$atts['srcset'] = null;
$atts['data-src'] = $full[0];
$atts['data-srcset'] = implode(', ', $srcsets);
endif;
?>
<img
<?php foreach($atts as $att => $value):
if($value) echo $att . '="' . $value . '" ';
endforeach; ?>
/>
<?php
return ob_get_clean();
}
}

View File

@@ -0,0 +1,39 @@
<?php
namespace App\Utilities;
class VideoSrcset
{
public function sizes($video_srcset = [])
{
if(empty($video_srcset)) return false;
$sizes = [];
foreach($video_srcset as $size => $video):
if($video):
$sizes[$size] = $video['width'];
endif;
endforeach;
if(!empty($sizes)):
return json_encode($sizes);
else:
return false;
endif;
}
public function smallest($video_srcset = [])
{
if(empty($video_srcset)) return false;
$smallest = null;
foreach($video_srcset as $size => $video):
if($smallest) continue;
if($video) $smallest = $size;
endforeach;
return $smallest;
}
}