Skip to content

Commit

Permalink
Added postmeta
Browse files Browse the repository at this point in the history
  • Loading branch information
ainsleyclark committed Dec 21, 2021
1 parent 423c575 commit 3d43aa6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions core/Package/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace Squidge\Package;

use Exception;
use Squidge\Log\Logger;

if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
Expand All @@ -25,6 +26,12 @@
class Service
{

/**
* META_KEY is the meta key for lookup for the
* service.
*/
const META_KEY = "_squidge_compressed";

/**
* Processes the file attachment.
*
Expand All @@ -42,9 +49,9 @@ public static function process($attachment, $args)
$attachment = wp_get_attachment_metadata($attachment);
}

// Return if the cwebp library is not installed.
// Return if the library is not installed.
if (!self::installed()) {
throw new Exception(self::$cmd_name . " is not installed");
return;
}

// Check if the file key exists.
Expand All @@ -58,6 +65,12 @@ public static function process($attachment, $args)
return;
}

// Check if the attachment has already been compressed.
$id = attachment_url_to_postid($attachment['file']);
if (self::has_compressed($id)) {
return;
}

// Convert main image.
static::convert($mainFile, self::get_mime_type($mainFile), $args);

Expand All @@ -72,6 +85,9 @@ public static function process($attachment, $args)
}
static::convert($path, self::get_mime_type($path), $args);
}

// Update post meta for attachment.
self::update_meta($id);
}

/**
Expand Down Expand Up @@ -162,4 +178,30 @@ private static function get_mime_type($file)
{
return mime_content_type($file);
}

/**
* Determines if the attachment has been compressed.
*
* @param $id
* @return bool
* @since 0.1.3
* @date 21/12/2021
*/
private static function has_compressed($id)
{
$meta = get_post_meta($id, self::META_KEY);
return !empty($meta);
}

/**
* Updates post meta for squidge.
*
* @param $id
* @since 0.1.3
* @date 21/12/2021
*/
private static function update_meta($id)
{
update_post_meta($id, self::META_KEY, true);
}
}
Binary file added images/test-large.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/test-large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3d43aa6

Please sign in to comment.