HEX
Server: LiteSpeed
System: Linux php-prod-1.spaceapp.ru 5.15.0-157-generic #167-Ubuntu SMP Wed Sep 17 21:35:53 UTC 2025 x86_64
User: xnsbb3110 (1041)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //proc/self/cwd/wp-content/new_update/plugins/wp-smushit/lib/class-wp-smush-helper.php
<?php
/**
 * @package WP Smush
 * @subpackage Admin
 * @version 1.0
 *
 * @author Umesh Kumar <umesh@incsub.com>
 *
 * @copyright (c) 2017, Incsub (http://incsub.com)
 */


if ( ! class_exists( 'WpSmushHelper' ) ) {

	class WpSmushHelper {

		function __construct() {
			$this->init();
		}

		function init() {

		}

		/**
		 * Return unfiltered file path
		 *
		 * @param $attachment_id
		 *
		 * @return bool
		 */
		function get_attached_file( $attachment_id ) {
			if ( empty( $attachment_id ) ) {
				return false;
			}

			$file_path = get_attached_file( $attachment_id );
			if ( ! empty( $file_path ) && strpos( $file_path, 's3' ) !== false ) {
				$file_path = get_attached_file( $attachment_id, true );
			}

			return $file_path;
		}

		/**
		 * Iterate over PNG->JPG Savings to return cummulative savings for an image
		 *
		 * @param string $attachment_id
		 *
		 * @return array|bool
		 */
		function get_pngjpg_savings( $attachment_id = '' ) {
			//Initialize empty array
			$savings = array(
				'bytes'       => 0,
				'size_before' => 0,
				'size_after'  => 0
			);

			//Return empty array if attaachment id not provided
			if( empty( $attachment_id ) ) {
				return $savings;
			}

			$pngjpg_savings = get_post_meta( $attachment_id, WP_SMUSH_PREFIX . 'pngjpg_savings', true );
			if( empty( $pngjpg_savings ) || !is_array( $pngjpg_savings )) {
				return $savings;
			}

			foreach ( $pngjpg_savings as $size => $s_savings ) {
				if( empty( $s_savings ) ) {
					continue;
				}
				$savings['size_before'] += $s_savings['size_before'];
				$savings['size_after'] += $s_savings['size_after'];
			}
			$savings['bytes'] = $savings['size_before'] - $savings['size_after'];

			return $savings;
		}

		/**
		 * Multiple Needles in an array
		 *
		 * @param $haystack
		 * @param $needle
		 * @param int $offset
		 *
		 * @return bool
		 */
		function strposa( $haystack, $needle, $offset = 0 ) {
			if ( ! is_array( $needle ) ) {
				$needle = array( $needle );
			}
			foreach ( $needle as $query ) {
				if ( strpos( $haystack, $query, $offset ) !== false ) {
					return true;
				} // stop on first true result
			}

			return false;
		}


		/**
		 * Checks if file for given attachment id exists on s3, otherwise looks for local path
		 *
		 * @param $id
		 * @param $file_path
		 *
		 * @return bool
		 */
		function file_exists( $id, $file_path ) {

			//If not attachment id is given return false
			if ( empty( $id ) ) {
				return false;
			}

			//Get file path, if not provided
			if ( empty( $file_path ) ) {
				$file_path = $this->get_attached_file( $id );
			}

			global $wpsmush_s3;

			//If S3 is enabled
			if ( is_object( $wpsmush_s3 ) && method_exists( $wpsmush_s3, 'is_image_on_s3' ) && $wpsmush_s3->is_image_on_s3( $id ) ) {
				$file_exists = true;
			} else {
				$file_exists = file_exists( $file_path );
			}

			return $file_exists;
		}
	}

	global $wpsmush_helper;
	$wpsmush_helper = new WpSmushHelper();

}