HEX
HEX
Server: Apache
System: Linux localhost.localdomain 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64
User: www (1001)
PHP: 8.1.32
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/ahmsolaiman.com/wp-content/plugins/types/application/models/media/service.php
<?php

/**
 * @since 2.3
 */
class Types_Media_Service {

	/**
	 * @var Types_Interface_Media[]
	 */
	private $media = array();

	/**
	 * @var Types_Media_Mapper_Interface
	 */
	private $mapper;


	/**
	 * @param string $url
	 *
	 * @return Types_Interface_Media|false
	 */
	public function find_by_url( $url ) {
		if ( array_key_exists( $url, $this->media ) ) {
			return $this->media[ $url ];
		}

		$media = $this->get_mapper()->find_by_url( $url );

		if ( $media ) {
			$id = $media->get_id();
			if ( $id ) {
				$this->media[ $id ] = $media;
			}
		}

		return $this->media[ $url ] = $media;
	}


	/**
	 * @param int $id
	 *
	 * @return Types_Interface_Media|Types_Media
	 */
	public function find_by_id( $id ) {
		if ( array_key_exists( $id, $this->media ) ) {
			return $this->media[ $id ];
		}

		$media = $this->get_mapper()->find_by_id( $id );

		return $this->media[ $media->get_url() ] = $this->media[ $id ] = $media;
	}


	/**
	 * @param Types_Interface_Media $media
	 * @param array $args
	 * @param bool $domain_image
	 *
	 * @return string
	 */
	public function resize_image( Types_Interface_Media $media, $args, $domain_image = true ) {
		// todo [since 2.3] this method is a container of legacy methods
		$args = array_merge( array(
			'return' => 'object',
			'suppress_errors' => false,
			'clear_cache' => false,
		), $args );

		if ( ! $domain_image && wpcf_get_settings( 'images_remote' ) ) {
			if ( ! function_exists( 'wpcf_fields_image_get_remote' ) ) {
				require_once( WPCF_EMBEDDED_ABSPATH . '/includes/fields/image.php' );
			}
			$remote = wpcf_fields_image_get_remote( $media->get_url() );

			if ( is_wp_error( $remote ) ) {
				return $media->get_url();
			}

			$image_abspath = $remote['abspath'];
		} else {
			if ( ! class_exists( 'Types_Image_Utils' ) ) {
				require_once WPCF_EMBEDDED_ABSPATH . '/views/image.php';
			}
			$image_abspath = Types_Image_Utils::getAbsPath( $media->get_url() );
		}

		WPCF_Loader::loadView( 'image' );
		$resized = types_image_resize( $image_abspath, $args );

		if ( is_wp_error( $resized ) ) {
			return $media->get_url();
		}

		$image_abspath = $resized->path;
		if ( wpcf_get_settings( 'add_resized_images_to_library' ) ) {
			if ( ! function_exists( 'wpcf_image_is_attachment' ) || ! function_exists( 'wpcf_image_add_to_library' ) ) {
				require_once( WPCF_EMBEDDED_ABSPATH . '/includes/fields/image.php' );
			}
			if ( ! wpcf_image_is_attachment( $resized->url ) ) {
				global $post;
				wpcf_image_add_to_library( $post, $image_abspath );
			}
		}

		return $resized->url;
	}


	/**
	 * Allow to override mapper
	 *
	 * @param Types_Media_Mapper_Interface $mapper
	 */
	public function set_mapper( Types_Media_Mapper_Interface $mapper ) {
		$this->mapper = $mapper;
	}


	/**
	 * If no mapper set it will use Types_Media_Mapper as default
	 *
	 * @return Types_Media_Mapper_Interface
	 */
	private function get_mapper() {
		if ( $this->mapper !== null ) {
			return $this->mapper;
		}

		return $this->mapper = new Types_Media_Mapper( new Types_Wordpress_Media() );
	}


	/**
	 * Delete all Types resized images by attachment post id
	 *
	 * @action delete_attachment
	 *
	 * @param int $attachment_post_id
	 */
	public function delete_resized_images_by_attachment_id( $attachment_post_id ) {
		if ( ! $attachment_file = get_attached_file( $attachment_post_id ) ) {
			// no attachment file for $attachment_post_id
			return;
		}

		$attachment_file_path_parts = pathinfo( $attachment_file );

		$files_pattern = $attachment_file_path_parts['dirname'] . DIRECTORY_SEPARATOR
			. $attachment_file_path_parts['filename'] . '-wpcf*';

		foreach ( glob( $files_pattern ) as $filename ) {
			@unlink( $filename );
		}
	}
}