rebase from live enviornment

This commit is contained in:
Rachit Bhargava
2024-01-09 22:14:20 -05:00
parent ff0b49a046
commit 3a22fcaa4a
15968 changed files with 2344674 additions and 45234 deletions

View File

@@ -0,0 +1,552 @@
<?php
namespace Imagify\Media;
use Imagify\CDN\PushCDNInterface;
use Imagify\Context\ContextInterface;
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
/**
* Abstract used for "media groups" (aka attachments).
*
* @since 1.9
* @author Grégory Viguier
*/
abstract class AbstractMedia implements MediaInterface {
/**
* The media ID.
*
* @var int
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $id;
/**
* Context (where the media "comes from").
*
* @var ContextInterface
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $context;
/**
* CDN to use.
*
* @var PushCDNInterface
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $cdn;
/**
* Tell if the media/context is network-wide.
*
* @var bool
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $is_network_wide = false;
/**
* Tell if the file is an image.
*
* @var bool
* @since 1.9
* @access protected
* @see $this->is_image()
* @author Grégory Viguier
*/
protected $is_image;
/**
* Tell if the file is a pdf.
*
* @var bool
* @since 1.9
* @access protected
* @see $this->is_pdf()
* @author Grégory Viguier
*/
protected $is_pdf;
/**
* Store the file mime type + file extension (if the file is supported).
*
* @var array
* @since 1.9
* @access protected
* @see $this->get_file_type()
* @author Grégory Viguier
*/
protected $file_type;
/**
* Filesystem object.
*
* @var object Imagify_Filesystem
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $filesystem;
/**
* The constructor.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param int $id The media ID.
*/
public function __construct( $id ) {
$this->id = (int) $id;
$this->filesystem = \Imagify_Filesystem::get_instance();
}
/**
* Get the media ID.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return int
*/
public function get_id() {
return $this->id;
}
/**
* Tell if the current media is valid.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_valid() {
return $this->get_id() > 0;
}
/**
* Get the media context name.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string
*/
public function get_context() {
return $this->get_context_instance()->get_name();
}
/**
* Get the media context instance.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return ContextInterface
*/
public function get_context_instance() {
if ( $this->context ) {
if ( is_string( $this->context ) ) {
$this->context = imagify_get_context( $this->context );
}
return $this->context;
}
$class_name = get_class( $this );
$class_name = '\\' . trim( $class_name, '\\' );
$class_name = str_replace( '\\Media\\', '\\Context\\', $class_name );
$this->context = new $class_name();
return $this->context;
}
/**
* Get the CDN instance.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool|PushCDNInterface A PushCDNInterface instance. False if no CDN is used.
*/
public function get_cdn() {
if ( isset( $this->cdn ) ) {
return $this->cdn;
}
if ( ! $this->is_valid() ) {
$this->cdn = false;
return $this->cdn;
}
$media_id = $this->get_id();
$context = $this->get_context_instance();
/**
* The CDN to use for this media.
*
* @since 1.9
* @author Grégory Viguier
*
* @param bool|PushCDNInterface $cdn A PushCDNInterface instance. False if no CDN is used.
* @param int $media_id The media ID.
* @param ContextInterface $context The context object.
*/
$this->cdn = apply_filters( 'imagify_cdn', false, $media_id, $context );
if ( ! $this->cdn || ! $this->cdn instanceof PushCDNInterface ) {
$this->cdn = false;
return $this->cdn;
}
return $this->cdn;
}
/** ----------------------------------------------------------------------------------------- */
/** ORIGINAL FILE =========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the original media's path if the file exists.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_original_path() {
if ( ! $this->is_valid() ) {
return false;
}
$original_path = $this->get_raw_original_path();
if ( ! $original_path || ! $this->filesystem->exists( $original_path ) ) {
return false;
}
return $original_path;
}
/** ----------------------------------------------------------------------------------------- */
/** FULL SIZE FILE ========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the path to the medias full size file if the file exists.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_fullsize_path() {
if ( ! $this->is_valid() ) {
return false;
}
$original_path = $this->get_raw_fullsize_path();
if ( ! $original_path || ! $this->filesystem->exists( $original_path ) ) {
return false;
}
return $original_path;
}
/** ----------------------------------------------------------------------------------------- */
/** BACKUP FILE ============================================================================= */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the backup file path if the file exists.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_backup_path() {
if ( ! $this->is_valid() ) {
return false;
}
$backup_path = $this->get_raw_backup_path();
if ( ! $backup_path || ! $this->filesystem->exists( $backup_path ) ) {
return false;
}
return $backup_path;
}
/**
* Check if the media has a backup of the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool True if the media has a backup.
*/
public function has_backup() {
return (bool) $this->get_backup_path();
}
/** ----------------------------------------------------------------------------------------- */
/** MEDIA DATA ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if the current media type is supported.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_supported() {
return (bool) $this->get_mime_type();
}
/**
* Tell if the current media refers to an image, based on file extension.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool Returns false in case it's an image but not in a supported format (bmp for example).
*/
public function is_image() {
if ( isset( $this->is_image ) ) {
return $this->is_image;
}
$this->is_image = strpos( (string) $this->get_mime_type(), 'image/' ) === 0;
return $this->is_image;
}
/**
* Tell if the current media refers to a pdf, based on file extension.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_pdf() {
if ( isset( $this->is_pdf ) ) {
return $this->is_pdf;
}
$this->is_pdf = 'application/pdf' === $this->get_mime_type();
return $this->is_pdf;
}
/**
* Get the original file extension (if supported by Imagify).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|null
*/
public function get_extension() {
return $this->get_file_type()->ext;
}
/**
* Get the original file mime type (if supported by Imagify).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string
*/
public function get_mime_type() {
return $this->get_file_type()->type;
}
/**
* Get the file mime type + file extension (if the file is supported by Imagify).
* This test is ran against the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array
*/
public function get_allowed_mime_types() {
return imagify_get_mime_types( $this->get_context_instance()->get_allowed_mime_types() );
}
/**
* If the media is an image, update the dimensions in the database with the current file dimensions.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool True on success. False on failure.
*/
public function update_dimensions() {
if ( ! $this->is_image() ) {
// The media is not a supported image.
return false;
}
$dimensions = $this->filesystem->get_image_size( $this->get_raw_fullsize_path() );
if ( ! $dimensions ) {
// Could not get the new dimensions.
return false;
}
$context = $this->get_context();
/**
* Triggered before updating an image width and height into its metadata.
*
* @since 1.9
* @see Imagify_Filesystem->get_image_size()
* @author Grégory Viguier
*
* @param int $media_id The media ID.
* @param array $dimensions {
* An array with, among other data:
*
* @type int $width The image width.
* @type int $height The image height.
* }
*/
do_action( "imagify_before_update_{$context}_media_data_dimensions", $this->get_id(), $dimensions );
$this->update_media_data_dimensions( $dimensions );
/**
* Triggered after updating an image width and height into its metadata.
*
* @since 1.9
* @see Imagify_Filesystem->get_image_size()
* @author Grégory Viguier
*
* @param int $media_id The media ID.
* @param array $dimensions {
* An array with, among other data:
*
* @type int $width The image width.
* @type int $height The image height.
* }
*/
do_action( "imagify_after_update_{$context}_media_data_dimensions", $this->get_id(), $dimensions );
return true;
}
/**
* Update the media data dimensions.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param array $dimensions {
* An array containing width and height.
*
* @type int $width The image width.
* @type int $height The image height.
* }
*/
abstract protected function update_media_data_dimensions( $dimensions );
/**
* Get the file mime type + file extension (if the file is supported by Imagify).
* This test is ran against the original file.
*
* @since 1.9
* @access protected
* @see wp_check_filetype()
* @author Grégory Viguier
*
* @return object
*/
protected function get_file_type() {
if ( isset( $this->file_type ) ) {
return $this->file_type;
}
$this->file_type = (object) [
'ext' => '',
'type' => '',
];
if ( ! $this->is_valid() ) {
return $this->file_type;
}
$path = $this->get_raw_fullsize_path();
if ( ! $path ) {
return $this->file_type;
}
$this->file_type = (object) wp_check_filetype( $path, $this->get_allowed_mime_types() );
return $this->file_type;
}
/**
* Filter the result of $this->get_media_files().
*
* @since 1.9
* @access protected
* @see $this->get_media_files()
* @author Grégory Viguier
*
* @param array $files An array with the size names as keys ('full' is used for the full size file), and arrays of data as values.
* @return array
*/
protected function filter_media_files( $files ) {
/**
* Filter the media files.
*
* @since 1.9
* @author Grégory Viguier
*
* @param array $files An array with the size names as keys ('full' is used for the full size file), and arrays of data as values.
* @param MediaInterface $media This instance.
*/
return (array) apply_filters( 'imagify_media_files', $files, $this );
}
}

View File

@@ -0,0 +1,358 @@
<?php
namespace Imagify\Media;
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
/**
* Media class for the custom folders.
*
* @since 1.9
* @author Grégory Viguier
*/
class CustomFolders extends AbstractMedia {
use \Imagify\Traits\MediaRowTrait;
use \Imagify\Deprecated\Traits\Media\CustomFoldersDeprecatedTrait;
/**
* Context (where the media "comes from").
*
* @var string
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $context = 'custom-folders';
/**
* The attachment SQL DB class.
*
* @var string
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $db_class_name = 'Imagify_Files_DB';
/**
* Tell if the media/context is network-wide.
*
* @var bool
* @since 1.9
* @access protected
* @author Grégory Viguier
*/
protected $is_network_wide = true;
/**
* The constructor.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param int|array|object $id The file ID. It can also be an array or object representing the file data.
*/
public function __construct( $id ) {
if ( ! static::constructor_accepts( $id ) ) {
$this->invalidate_row();
parent::__construct( 0 );
return;
}
if ( is_numeric( $id ) ) {
$this->id = (int) $id;
$this->get_row();
} else {
$prim_key = $this->get_row_db_instance()->get_primary_key();
$this->row = (array) $id;
$this->id = $this->row[ $prim_key ];
}
parent::__construct( $this->id );
}
/**
* Tell if the given entry can be accepted in the constructor.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param mixed $id Whatever.
* @return bool
*/
public static function constructor_accepts( $id ) {
return $id && ( is_numeric( $id ) || is_array( $id ) || is_object( $id ) );
}
/** ----------------------------------------------------------------------------------------- */
/** ORIGINAL FILE =========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the original media's path.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_original_path() {
if ( ! $this->is_valid() ) {
return false;
}
if ( $this->get_cdn() ) {
return $this->get_cdn()->get_file_path( 'original' );
}
$row = $this->get_row();
if ( ! $row || empty( $row['path'] ) ) {
return false;
}
return \Imagify_Files_Scan::remove_placeholder( $row['path'] );
}
/** ----------------------------------------------------------------------------------------- */
/** FULL SIZE FILE ========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the URL of the medias full size file.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_fullsize_url() {
if ( ! $this->is_valid() ) {
return false;
}
if ( $this->get_cdn() ) {
return $this->get_cdn()->get_file_url();
}
$row = $this->get_row();
if ( ! $row || empty( $row['path'] ) ) {
return false;
}
return \Imagify_Files_Scan::remove_placeholder( $row['path'], 'url' );
}
/**
* Get the path to the medias full size file, even if the file doesn't exist.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_fullsize_path() {
if ( ! $this->is_valid() ) {
return false;
}
if ( $this->get_cdn() ) {
return $this->get_cdn()->get_file_path();
}
$row = $this->get_row();
if ( ! $row || empty( $row['path'] ) ) {
return false;
}
return \Imagify_Files_Scan::remove_placeholder( $row['path'] );
}
/** ----------------------------------------------------------------------------------------- */
/** BACKUP FILE ============================================================================= */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the backup URL, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_backup_url() {
if ( ! $this->is_valid() ) {
return false;
}
return site_url( $this->filesystem->make_path_relative( $this->get_raw_backup_path() ) );
}
/**
* Get the backup file path, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_backup_path() {
if ( ! $this->is_valid() ) {
return false;
}
return \Imagify_Custom_Folders::get_file_backup_path( $this->get_raw_original_path() );
}
/** ----------------------------------------------------------------------------------------- */
/** THUMBNAILS ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Create the media thumbnails.
* And since this context does not support thumbnails...
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool|WP_Error True on success. A \WP_Error instance on failure.
*/
public function generate_thumbnails() {
if ( ! $this->is_valid() ) {
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
}
return true;
}
/** ----------------------------------------------------------------------------------------- */
/** MEDIA DATA ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if the current media has the required data (the data containing the file paths and thumbnails).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function has_required_media_data() {
return $this->is_valid();
}
/**
* Get the list of the files of this media, including the full size file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array {
* An array with the size names as keys ('full' is used for the full size file), and arrays of data as values:
*
* @type string $size The size name.
* @type string $path Absolute path to the file.
* @type int $width The file width.
* @type int $height The file height.
* @type string $mime-type The file mime type.
* @type bool $disabled True if the size is disabled in the plugins settings.
* }
*/
public function get_media_files() {
if ( ! $this->is_valid() ) {
return [];
}
$fullsize_path = $this->get_raw_fullsize_path();
if ( ! $fullsize_path ) {
return [];
}
$dimensions = $this->get_dimensions();
$sizes = [
'full' => [
'size' => 'full',
'path' => $fullsize_path,
'width' => $dimensions['width'],
'height' => $dimensions['height'],
'mime-type' => $this->get_mime_type(),
'disabled' => false,
],
];
return $this->filter_media_files( $sizes );
}
/**
* If the media is an image, get its width and height.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array
*/
public function get_dimensions() {
if ( ! $this->is_image() ) {
return [
'width' => 0,
'height' => 0,
];
}
$row = $this->get_row();
return [
'width' => ! empty( $row['width'] ) ? $row['width'] : 0,
'height' => ! empty( $row['height'] ) ? $row['height'] : 0,
];
}
/**
* Update the media data dimensions.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param array $dimensions {
* An array containing width and height.
*
* @type int $width The image width.
* @type int $height The image height.
* }
*/
protected function update_media_data_dimensions( $dimensions ) {
$row = $this->get_row();
if ( ! is_array( $row ) ) {
$row = [];
}
if ( isset( $row['width'], $row['height'] ) && $row['width'] === $dimensions['width'] && $row['height'] === $dimensions['height'] ) {
return;
}
$row['width'] = $dimensions['width'];
$row['height'] = $dimensions['height'];
$this->update_row( $row );
}
}

View File

@@ -0,0 +1,337 @@
<?php
namespace Imagify\Media;
use Imagify\CDN\PushCDNInterface;
use Imagify\Context\ContextInterface;
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
/**
* Interface to use for "media groups" (aka attachments).
*
* @since 1.9
* @author Grégory Viguier
*/
interface MediaInterface {
/**
* Tell if the given entry can be accepted in the constructor.
* For example it can include `is_numeric( $id )` if the constructor accepts integers.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param mixed $id Whatever.
* @return bool
*/
public static function constructor_accepts( $id );
/**
* Get the media ID.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return int
*/
public function get_id();
/**
* Tell if the current media is valid.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_valid();
/**
* Get the media context name.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string
*/
public function get_context();
/**
* Get the media context instance.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return ContextInterface
*/
public function get_context_instance();
/**
* Get the CDN instance.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool|PushCDNInterface A PushCDNInterface instance. False if no CDN is used.
*/
public function get_cdn();
/** ----------------------------------------------------------------------------------------- */
/** ORIGINAL FILE =========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the original file path, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_original_path();
/**
* Get the original media's path if the file exists.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_original_path();
/** ----------------------------------------------------------------------------------------- */
/** FULL SIZE FILE ========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the URL of the medias full size file.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_fullsize_url();
/**
* Get the path to the medias full size file, even if the file doesn't exist.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_fullsize_path();
/**
* Get the path to the medias full size file if the file exists.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_fullsize_path();
/** ----------------------------------------------------------------------------------------- */
/** BACKUP FILE ============================================================================= */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the backup URL, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_backup_url();
/**
* Get the backup file path, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_backup_path();
/**
* Get the backup file path if the file exists.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_backup_path();
/**
* Check if the media has a backup of the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool True if the media has a backup.
*/
public function has_backup();
/** ----------------------------------------------------------------------------------------- */
/** THUMBNAILS ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Create the media thumbnails.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool|WP_Error True on success. A \WP_Error instance on failure.
*/
public function generate_thumbnails();
/** ----------------------------------------------------------------------------------------- */
/** MEDIA DATA ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if the current media type is supported.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_supported();
/**
* Tell if the current media refers to an image, based on file extension.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool Returns false in case it's an image but not in a supported format (bmp for example).
*/
public function is_image();
/**
* Tell if the current media refers to a pdf, based on file extension.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_pdf();
/**
* Get the original file extension (if supported by Imagify).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|null
*/
public function get_extension();
/**
* Get the original file mime type (if supported by Imagify).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string
*/
public function get_mime_type();
/**
* Get the file mime type + file extension (if the file is supported by Imagify).
* This test is ran against the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array
*/
public function get_allowed_mime_types();
/**
* Tell if the current media has the required data (the data containing the file paths and thumbnails).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function has_required_media_data();
/**
* Get the list of the files of this media, including the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array {
* An array with the size names as keys ('full' is used for the original file), and arrays of data as values:
*
* @type string $path Absolute path to the file.
* @type int $width The file width.
* @type int $height The file height.
* @type string $mime-type The file mime type.
* }
*/
public function get_media_files();
/**
* If the media is an image, get its width and height.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array
*/
public function get_dimensions();
/**
* If the media is an image, update the dimensions in the database with the current file dimensions.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool True on success. False on failure.
*/
public function update_dimensions();
}

View File

@@ -0,0 +1,390 @@
<?php
namespace Imagify\Media;
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
/**
* Fallback class for "media groups" (aka attachments).
*
* @since 1.9
* @author Grégory Viguier
*/
class Noop implements MediaInterface {
use \Imagify\Deprecated\Traits\Media\NoopDeprecatedTrait;
/**
* Tell if the given entry can be accepted in the constructor.
* For example it can include `is_numeric( $id )` if the constructor accepts integers.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param mixed $id Whatever.
* @return bool
*/
public static function constructor_accepts( $id ) {
return false;
}
/**
* Get the media ID.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return int
*/
public function get_id() {
return 0;
}
/**
* Tell if the current media is valid.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_valid() {
return false;
}
/**
* Get the media context name.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string
*/
public function get_context() {
return 'noop';
}
/**
* Get the media context instance.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return ContextInterface
*/
public function get_context_instance() {
return \Imagify\Context\Noop::get_instance();
}
/**
* Get the CDN instance.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool|PushCDNInterface A PushCDNInterface instance. False if no CDN is used.
*/
public function get_cdn() {
return false;
}
/** ----------------------------------------------------------------------------------------- */
/** ORIGINAL FILE =========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the original file path, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_original_path() {
return false;
}
/**
* Get the original media's path if the file exists.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_original_path() {
return false;
}
/** ----------------------------------------------------------------------------------------- */
/** FULL SIZE FILE ========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the URL of the medias full size file.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_fullsize_url() {
return false;
}
/**
* Get the path to the medias full size file, even if the file doesn't exist.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_fullsize_path() {
return false;
}
/**
* Get the path to the medias full size file if the file exists.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_fullsize_path() {
return false;
}
/** ----------------------------------------------------------------------------------------- */
/** BACKUP FILE ============================================================================= */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the backup URL, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_backup_url() {
return false;
}
/**
* Get the backup file path, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_backup_path() {
return false;
}
/**
* Get the backup file path if the file exists.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False if it doesn't exist.
*/
public function get_backup_path() {
return false;
}
/**
* Check if the media has a backup of the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool True if the media has a backup.
*/
public function has_backup() {
return false;
}
/** ----------------------------------------------------------------------------------------- */
/** THUMBNAILS ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Create the media thumbnails.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool|WP_Error True on success. A \WP_Error instance on failure.
*/
public function generate_thumbnails() {
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
}
/** ----------------------------------------------------------------------------------------- */
/** MEDIA DATA ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if the current media type is supported.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_supported() {
return false;
}
/**
* Tell if the current media refers to an image, based on file extension.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool Returns false in case it's an image but not in a supported format (bmp for example).
*/
public function is_image() {
return false;
}
/**
* Tell if the current media refers to a pdf, based on file extension.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function is_pdf() {
return false;
}
/**
* Get the original file extension (if supported by Imagify).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|null
*/
public function get_extension() {
return '';
}
/**
* Get the original file mime type (if supported by Imagify).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string
*/
public function get_mime_type() {
return '';
}
/**
* Get the file mime type + file extension (if the file is supported by Imagify).
* This test is ran against the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array
*/
public function get_allowed_mime_types() {
return imagify_get_mime_types( 'all' );
}
/**
* Tell if the current media has the required data (the data containing the file paths and thumbnails).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function has_required_media_data() {
return false;
}
/**
* Get the list of the files of this media, including the original file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array {
* An array with the size names as keys ('full' is used for the original file), and arrays of data as values:
*
* @type string $path Absolute path to the file.
* @type int $width The file width.
* @type int $height The file height.
* @type string $mime-type The file mime type.
* }
*/
public function get_media_files() {
return [];
}
/**
* If the media is an image, get its width and height.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array
*/
public function get_dimensions() {
return [
'width' => 0,
'height' => 0,
];
}
/**
* If the media is an image, update the dimensions in the database with the current file dimensions.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool True on success. False on failure.
*/
public function update_dimensions() {
return false;
}
}

View File

@@ -0,0 +1,430 @@
<?php
namespace Imagify\Media;
defined( 'ABSPATH' ) || die( 'Cheatin uh?' );
/**
* Media class for the medias in the WP library.
*
* @since 1.9
* @author Grégory Viguier
*/
class WP extends AbstractMedia {
use \Imagify\Deprecated\Traits\Media\WPDeprecatedTrait;
/**
* Tell if were playing in WP 5.3s garden.
*
* @var bool
* @since 1.9.8
* @access protected
* @author Grégory Viguier
*/
protected $is_wp53;
/**
* The constructor.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param int|\WP_Post $id The attachment ID, or \WP_Post object.
*/
public function __construct( $id ) {
if ( ! static::constructor_accepts( $id ) ) {
parent::__construct( 0 );
return;
}
if ( is_numeric( $id ) ) {
$id = get_post( (int) $id );
}
if ( ! $id || 'attachment' !== $id->post_type ) {
parent::__construct( 0 );
return;
}
parent::__construct( $id->ID );
}
/**
* Tell if the given entry can be accepted in the constructor.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @param mixed $id Whatever.
* @return bool
*/
public static function constructor_accepts( $id ) {
return $id && ( is_numeric( $id ) || $id instanceof \WP_Post );
}
/** ----------------------------------------------------------------------------------------- */
/** ORIGINAL FILE =========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the original file path, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_original_path() {
if ( ! $this->is_valid() ) {
return false;
}
if ( $this->get_cdn() ) {
return $this->get_cdn()->get_file_path( 'original' );
}
if ( $this->is_wp_53() ) {
// `wp_get_original_image_path()` may return false.
$path = wp_get_original_image_path( $this->id );
} else {
$path = false;
}
if ( ! $path ) {
$path = get_attached_file( $this->id );
}
return $path ? $path : false;
}
/** ----------------------------------------------------------------------------------------- */
/** FULL SIZE FILE ========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the URL of the medias full size file.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_fullsize_url() {
if ( ! $this->is_valid() ) {
return false;
}
if ( $this->get_cdn() ) {
return $this->get_cdn()->get_file_url();
}
$url = wp_get_attachment_url( $this->id );
return $url ? $url : false;
}
/**
* Get the path to the medias full size file, even if the file doesn't exist.
*
* @since 1.9.8
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_fullsize_path() {
if ( ! $this->is_valid() ) {
return false;
}
if ( $this->get_cdn() ) {
return $this->get_cdn()->get_file_path();
}
$path = get_attached_file( $this->id );
return $path ? $path : false;
}
/** ----------------------------------------------------------------------------------------- */
/** BACKUP FILE ============================================================================= */
/** ----------------------------------------------------------------------------------------- */
/**
* Get the backup URL, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file URL. False on failure.
*/
public function get_backup_url() {
if ( ! $this->is_valid() ) {
return false;
}
return get_imagify_attachment_url( $this->get_raw_backup_path() );
}
/**
* Get the backup file path, even if the file doesn't exist.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return string|bool The file path. False on failure.
*/
public function get_raw_backup_path() {
if ( ! $this->is_valid() ) {
return false;
}
return get_imagify_attachment_backup_path( $this->get_raw_original_path() );
}
/** ----------------------------------------------------------------------------------------- */
/** THUMBNAILS ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Create the media thumbnails.
* With WP 5.3+, this will also generate a new full size file if the original file is wider or taller than a defined threshold.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool|WP_Error True on success. A \WP_Error instance on failure.
*/
public function generate_thumbnails() {
if ( ! $this->is_valid() ) {
return new \WP_Error( 'invalid_media', __( 'This media is not valid.', 'imagify' ) );
}
if ( ! function_exists( 'wp_generate_attachment_metadata' ) ) {
require_once ABSPATH . 'wp-admin/includes/image.php';
}
// Store the path to the current full size file before generating the thumbnails.
$old_full_size_path = $this->get_raw_fullsize_path();
$metadata = wp_generate_attachment_metadata( $this->get_id(), $this->get_raw_original_path() );
if ( empty( $metadata['file'] ) ) {
// Σ(゚Д゚).
update_post_meta( $this->get_id(), '_wp_attachment_metadata', $metadata );
return true;
}
/**
* Don't change the full size file name.
* WP 5.3+ will rename the full size file if the resizing threshold has changed (not the same as the one used to generate it previously).
* This will force WP to keep the previous file name.
*/
$old_full_size_file_name = $this->filesystem->file_name( $old_full_size_path );
$new_full_size_file_name = $this->filesystem->file_name( $metadata['file'] );
if ( $new_full_size_file_name !== $old_full_size_file_name ) {
$new_full_size_path = $this->filesystem->dir_path( $old_full_size_path ) . $new_full_size_file_name;
$moved = $this->filesystem->move( $new_full_size_path, $old_full_size_path, true );
if ( $moved ) {
$metadata['file'] = $this->filesystem->dir_path( $metadata['file'] ) . $old_full_size_file_name;
update_post_meta( $this->get_id(), '_wp_attached_file', $metadata['file'] );
}
}
update_post_meta( $this->get_id(), '_wp_attachment_metadata', $metadata );
return true;
}
/** ----------------------------------------------------------------------------------------- */
/** MEDIA DATA ============================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if the current media has the required data (the data containing the file paths and thumbnails).
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return bool
*/
public function has_required_media_data() {
if ( ! $this->is_valid() ) {
return false;
}
$file = get_post_meta( $this->id, '_wp_attached_file', true );
if ( ! $file || preg_match( '@://@', $file ) || preg_match( '@^.:\\\@', $file ) ) {
return false;
}
return (bool) wp_get_attachment_metadata( $this->id, true );
}
/**
* Get the list of the files of this media, including the full size file.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array {
* An array with the size names as keys ('full' is used for the full size file), and arrays of data as values:
*
* @type string $size The size name.
* @type string $path Absolute path to the file.
* @type int $width The file width.
* @type int $height The file height.
* @type string $mime-type The file mime type.
* @type bool $disabled True if the size is disabled in the plugins settings.
* }
*/
public function get_media_files() {
if ( ! $this->is_valid() ) {
return [];
}
$fullsize_path = $this->get_raw_fullsize_path();
if ( ! $fullsize_path ) {
return [];
}
$dimensions = $this->get_dimensions();
$all_sizes = [
'full' => [
'size' => 'full',
'path' => $fullsize_path,
'width' => $dimensions['width'],
'height' => $dimensions['height'],
'mime-type' => $this->get_mime_type(),
'disabled' => false,
],
];
if ( $this->is_image() ) {
$sizes = wp_get_attachment_metadata( $this->id, true );
$sizes = ! empty( $sizes['sizes'] ) && is_array( $sizes['sizes'] ) ? $sizes['sizes'] : [];
$sizes = array_intersect_key( $sizes, $this->get_context_instance()->get_thumbnail_sizes() );
} else {
$sizes = [];
}
if ( ! $sizes ) {
return $all_sizes;
}
$dir_path = $this->filesystem->dir_path( $fullsize_path );
$disallowed_sizes = get_imagify_option( 'disallowed-sizes' );
$is_active_for_network = imagify_is_active_for_network();
foreach ( $sizes as $size => $size_data ) {
$all_sizes[ $size ] = [
'size' => $size,
'path' => $dir_path . $size_data['file'],
'width' => $size_data['width'],
'height' => $size_data['height'],
'mime-type' => $size_data['mime-type'],
'disabled' => ! $is_active_for_network && isset( $disallowed_sizes[ $size ] ),
];
}
return $this->filter_media_files( $all_sizes );
}
/**
* If the media is an image, get its width and height.
*
* @since 1.9
* @access public
* @author Grégory Viguier
*
* @return array
*/
public function get_dimensions() {
if ( ! $this->is_image() ) {
return [
'width' => 0,
'height' => 0,
];
}
$values = wp_get_attachment_image_src( $this->id, 'full' );
return [
'width' => $values[1],
'height' => $values[2],
];
}
/**
* Update the media data dimensions.
*
* @since 1.9
* @access protected
* @author Grégory Viguier
*
* @param array $dimensions {
* An array containing width and height.
*
* @type int $width The image width.
* @type int $height The image height.
* }
*/
protected function update_media_data_dimensions( $dimensions ) {
$metadata = wp_get_attachment_metadata( $this->id );
if ( ! is_array( $metadata ) ) {
$row = [];
}
if ( isset( $metadata['width'], $metadata['height'] ) && $metadata['width'] === $dimensions['width'] && $metadata['height'] === $dimensions['height'] ) {
return;
}
$metadata['width'] = $dimensions['width'];
$metadata['height'] = $dimensions['height'];
update_post_meta( $this->get_id(), '_wp_attachment_metadata', $metadata );
}
/** ----------------------------------------------------------------------------------------- */
/** INTERNAL TOOLS ========================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if were playing in WP 5.3s garden.
*
* @since 1.9.8
* @access protected
* @author Grégory Viguier
*
* @return bool
*/
protected function is_wp_53() {
if ( isset( $this->is_wp53 ) ) {
return $this->is_wp53;
}
$this->is_wp53 = function_exists( 'wp_get_original_image_path' );
return $this->is_wp53;
}
}