Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
crstauf committed Feb 7, 2024
1 parent d990948 commit 4553c85
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 0 deletions.
26 changes: 26 additions & 0 deletions includes/abstracts/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,32 @@ public function placeholder( $attributes = null, $settings = null ) : \Image_Tag
return $created;
}

/**
* Convert to Placehold.co image.
*
* @param null|mixed[]|Attributes $attributes
* @param null|mixed[]|Settings $settings
* @uses $this->is_type()
* @return \Image_Tag\Types\Placehold
*/
public function placehold( $attributes = null, $settings = null ) : \Image_Tag\Types\Placehold {
if ( is_a( $this, \Image_Tag\Types\Placehold::class ) ) {
trigger_error( sprintf( 'Image is already type <code>%s</code>', $this->get_type() ) );
return $this;
}

$attributes = wp_parse_args( ( array ) $attributes, $this->attributes->store );
$settings = wp_parse_args( ( array ) $settings, $this->settings->store );

$created = Image_Tag::create( 'placehold', $attributes, $settings );

if ( ! is_a( $created, \Image_Tag\Types\Placehold::class ) ) {
return new \Image_Tag\Types\Placehold;
}

return $created;
}

/**
* Convert to Unsplash Source photo.
*
Expand Down
5 changes: 5 additions & 0 deletions includes/types/Image_Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public static function create( $source, $attributes = null, $settings = null ) :
return new Image_Tag\Types\Picsum( $attributes, $settings );
}

if ( 'placehold' === $_source ) {
require_once Plugin::inc() . 'types/Placehold.php';
return new Image_Tag\Types\Placehold( $attributes, $settings );
}

if ( 'placeholder' === $_source ) {
require_once Plugin::inc() . 'types/Placeholder.php';
return new Image_Tag\Types\Placeholder( $attributes, $settings );
Expand Down
240 changes: 240 additions & 0 deletions includes/types/Placehold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php
/**
* Placeholder image service: Placehold.co
*
* @link https://placehold.co/
*/

declare( strict_types=1 );

namespace Image_Tag\Types;

use Image_Tag\Data_Stores\Attributes;
use Image_Tag\Data_Stores\Settings;

defined( 'WPINC' ) || die();

/**
* Class: Image_Tag\Types\Placeholder
*/
class Placehold extends \Image_Tag\Abstracts\Base implements \Image_Tag\Interfaces\Dynamic_Source {

const BASE_URL = 'https://placehold.co';

/**
* @var string[] Image types.
*/
const TYPES = array(
'placehold',
'__placeholder',
);

/**
* Construct.
*
* @param null|mixed[]|Attributes $attributes
* @param null|mixed[]|Settings $settings
* @uses $this->construct()
*/
public function __construct( $attributes = null, $settings = null ) {
$this->construct( $attributes, $settings );
}

/**
* Create Attributes object to use for output.
*
* @uses Base::output_attributes()
* @uses $this->generate_source()
* @return Attributes
*/
protected function output_attributes() : Attributes {
$attributes = parent::output_attributes();
$dimensions = array();

$attributes->update( 'src', $this->generate_source() );

# Width
if ( $this->settings->has( 'width' ) ) {
$dimensions[] = $this->settings->get( 'width' );
} else if ( $this->attributes->has( 'width' ) ) {
$dimensions[] = $this->attributes->get( 'width' );
}

# Height
if ( $this->settings->has( 'height' ) ) {
$dimensions[] = $this->settings->get( 'height' );
} else if ( $this->attributes->has( 'height' ) ) {
$dimensions[] = $this->attributes->get( 'height' );
}

if ( 1 === count( $dimensions ) ) {
$dimensions[] = $dimensions[0];
}

if ( ! $attributes->has( 'width' ) && ! empty( $dimensions[0] ) ) {
$attributes->set( 'width', $dimensions[0] );
}

if ( ! $attributes->has( 'height' ) && ! empty( $dimensions[1] ) ) {
$attributes->set( 'height', $dimensions[1] );
}

return $attributes;
}

/**
* Generate image source.
*
* @uses Settings::has()
* @uses Settings::get()
* @uses Attributes::has()
* @uses Attributes::get()
* @return string
*/
public function generate_source() : string {
if ( array_key_exists( __FUNCTION__, $this->cache ) ) {
return $this->cache[ __FUNCTION__ ];
}

$dimensions = array();
$src = array( static::BASE_URL );

# Width
if ( $this->settings->has( 'width' ) ) {
$dimensions[] = $this->settings->get( 'width' );
} else if ( $this->attributes->has( 'width' ) ) {
$dimensions[] = $this->attributes->get( 'width' );
}

# Height
if ( $this->settings->has( 'height' ) ) {
$dimensions[] = $this->settings->get( 'height' );
} else if ( $this->attributes->has( 'height' ) ) {
$dimensions[] = $this->attributes->get( 'height' );
}

if ( 1 === count( $dimensions ) ) {
$dimensions[] = $dimensions[0];
}

$dimensions = implode( 'x', $dimensions );

# Retina
if ( $this->settings->has( 'retina' ) ) {
$retina = absint( $this->settings->get( 'retina' ) );
$dimensions .= sprintf( '@%sx', $retina );
}

$src[] = $dimensions;

# Colors
if ( $this->settings->has( 'bg_color' ) && $this->settings->has( 'text_color' ) ) {
$src[] = $this->settings->get( 'bg_color' );
$src[] = $this->settings->get( 'text_color' );
}

# Format
if ( $this->settings->has( 'format' ) ) {
$src[] = $this->settings->get( 'format' );
}

# Convert to string
$src = implode( '/', $src );

# Custom text
if ( $this->settings->has( 'text' ) ) {
$src = add_query_arg( 'text', urlencode( $this->settings->get( 'text' ) ), $src );
}

$this->cache[ __FUNCTION__ ] = $src;

return $src;
}

/**
* Get ratio of image dimensions: width divided by height.
*
* @return float
*/
public function ratio() : float {
$width = 0;
$height = 0;

if ( $this->settings->has( 'width', false ) ) {
$width = $this->settings->get( 'width' );
} else if ( $this->attributes->has( 'width', false ) ) {
$width = $this->attributes->get( 'width' );
}

if ( $this->settings->has( 'height', false ) ) {
$height = $this->settings->get( 'height' );
} else if ( $this->attributes->has( 'height', false ) ) {
$height = $this->attributes->get( 'height' );
}

if ( empty( $height ) ) {
$height = $width;
}

if ( empty( $width ) ) {
$width = $height;
}

if ( empty( $height ) ) {
return 0;
}

return absint( $width ) / absint( $height );
}

/**
* Perform validation checks.
*
* @uses $this->validate_dimensions()
* @return \WP_Error
*/
protected function perform_validation_checks() : \WP_Error {
$errors = new \WP_Error;

try {
$this->validate_dimensions();
} catch ( \Exception $e ) {
$errors->add( 'placehold', $e->getMessage() );
}

return $errors;
}

/**
* Check that at least one dimension is set.
*
* @uses Settings::has()
* @uses Attributes::has()
* @return void
*/
protected function validate_dimensions() : void {
if (
$this->settings->has( 'width', false )
|| $this->settings->has( 'height', false )
|| $this->attributes->has( 'width', false )
|| $this->attributes->has( 'height', false )
) {
return;
}

throw new \Exception( 'Placehold requires at least one dimension.' );
}

/**
* Prevent conversion to same type.
*
* @param null|mixed[]|Attributes $attributes
* @param null|mixed[]|Settings $settings
* @return self
*/
public function placehold( $attributes = null, $settings = null ) : self {
trigger_error( sprintf( 'Image is already type <code>%s</code>', $this->get_type() ) );
return $this;
}

}

0 comments on commit 4553c85

Please sign in to comment.