ltsecurityinc安防产品在线商城代码
This commit is contained in:
+208
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Athlete2 Theme
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Olegnax.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.olegnax.com/license
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://www.olegnax.com/)
|
||||
* @license https://www.olegnax.com/license
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
class CssFiles extends AbstractHelper {
|
||||
|
||||
/**
|
||||
* Store Manager
|
||||
*
|
||||
* @var \Magento\Store\Model\StoreManagerInterface
|
||||
*/
|
||||
private $_storeManager;
|
||||
private $google_font_enable;
|
||||
protected $store_code;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $webFolder;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $dymanicFolder;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $generatedCssDir;
|
||||
|
||||
public function __construct( Context $context, StoreManagerInterface $storeManager ) {
|
||||
|
||||
$this->_storeManager = $storeManager;
|
||||
|
||||
$base = BP;
|
||||
|
||||
$this->webFolder = 'athlete2/web/';
|
||||
$this->dymanicFolder = 'athlete2/dymanic';
|
||||
$this->generatedCssDir = sprintf( '%s/pub/media/%s', $base, $this->dymanicFolder );
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
public function getDymanicDir() {
|
||||
return $this->generatedCssDir;
|
||||
}
|
||||
|
||||
public function getBaseMediaUrl( $path = '' ) {
|
||||
return $this->_storeManager->getStore()->getBaseUrl( UrlInterface::URL_TYPE_MEDIA ) . $path;
|
||||
}
|
||||
|
||||
public function getModuleMediaUrl( $path = '' ) {
|
||||
return $this->getBaseMediaUrl( $this->dymanicFolder ) . $path;
|
||||
}
|
||||
|
||||
public function getModuleWebUrl( $path = '' ) {
|
||||
return $this->getBaseMediaUrl( $this->webFolder ) . $path;
|
||||
}
|
||||
|
||||
public function getDynamicCss( $name, $code = null ) {
|
||||
return $this->getDynamicFile( $name, $code );
|
||||
}
|
||||
|
||||
public function getDynamicJs( $name, $code = null ) {
|
||||
return $this->getDynamicFile( $name, $code, 'js' );
|
||||
}
|
||||
|
||||
public function getDynamicFile( $name, $code = null, $format = 'css' ) {
|
||||
if ( empty( $code ) ) {
|
||||
$code = $this->_storeManager->getStore()->getCode();
|
||||
}
|
||||
return sprintf( '%s/%s_%s.%s', $this->getModuleMediaUrl(), $name, $code, $format );
|
||||
}
|
||||
|
||||
public function getConfig( $path, $storeCode = null ) {
|
||||
$value = $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeCode);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function getFont( $font_name ) {
|
||||
$font = $this->getConfigFont( $font_name );
|
||||
|
||||
if ( empty( $font ) ) {
|
||||
$default = $this->defaultFont();
|
||||
if ( array_key_exists( $font_name, $default ) ) {
|
||||
$font = $default[ $font_name ];
|
||||
}
|
||||
}
|
||||
|
||||
return $font;
|
||||
}
|
||||
public function fontOutput($font_name_google, $font_name_custom){
|
||||
$font_name = 'Open Sans';
|
||||
if('' != $font_name_custom) {
|
||||
$font_name = preg_replace("/[^a-zA-Z0-9-_\s]/", "", $font_name_custom);
|
||||
} elseif($font_name_google) {
|
||||
$font_name = $font_name_google;
|
||||
}
|
||||
return '\'' . $font_name . '\', Helvetica, Arial, sans-serif';
|
||||
}
|
||||
public function getFontWeight($font_name)
|
||||
{
|
||||
$font_name = $font_name . '_weight';
|
||||
$fontWeight = $this->getConfigFont($font_name);
|
||||
|
||||
if ( empty( $fontWeight ) ) {
|
||||
// fallback. use default font weight option if there are no specific weight option for selected font
|
||||
$fontWeight = $this->getConfigFont( 'general_font_weight' );
|
||||
}
|
||||
return $fontWeight;
|
||||
}
|
||||
|
||||
public function getGoogleFontUrl()
|
||||
{
|
||||
if (!$this->isGoogleFontEnable()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$default = array_keys($this->defaultFont());
|
||||
$fonts = [];
|
||||
foreach ($default as $font_name) {
|
||||
$font = $this->getFont($font_name);
|
||||
if (!empty($font)) {
|
||||
$gfw = explode(',', $this->getFontWeight($font_name));
|
||||
$ar = array_key_exists($font, $fonts) ? $fonts[$font] : [];
|
||||
$fonts[$font] = array_merge($ar, $gfw);
|
||||
}
|
||||
}
|
||||
$fonts = array_map('array_unique', $fonts);
|
||||
$fonts = array_map('array_filter', $fonts);
|
||||
$fonts = array_filter($fonts);
|
||||
foreach ($fonts as $font => $weight) {
|
||||
$fonts[$font] = sprintf('%s:%s', $font, implode(',', $weight));
|
||||
}
|
||||
$fonts = implode('|', $fonts);
|
||||
$fonts = urlencode($fonts);
|
||||
|
||||
return '//fonts.googleapis.com/css?family=' . $fonts . '&display='. ($this->getConfigFont( 'fonts_display_mode' ) ?: 'fallback');
|
||||
}
|
||||
|
||||
protected function getConfigFont( $font_name ) {
|
||||
$store_code = $this->getStoreCode();
|
||||
$appearance_general = $this->getConfig( 'athlete2_design/appearance_general', $store_code );
|
||||
|
||||
if ( is_array($appearance_general) && isset( $appearance_general[ $font_name ] ) && !empty( $appearance_general[ $font_name ] ) ) {
|
||||
return $appearance_general[ $font_name ];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
protected function isGoogleFontEnable() {
|
||||
if ( is_null( $this->google_font_enable ) ) {
|
||||
$this->google_font_enable = $this->getConfig( 'athlete2_design/appearance_general/google_font_enable', $this->getStoreCode() );
|
||||
}
|
||||
return $this->google_font_enable;
|
||||
}
|
||||
|
||||
public function setStoreCode( $store_code = null ) {
|
||||
$this->store_code = $store_code;
|
||||
}
|
||||
|
||||
public function getStoreCode() {
|
||||
return $this->store_code;
|
||||
}
|
||||
|
||||
|
||||
protected function defaultFont() {
|
||||
return [
|
||||
'body_font' => 'Open Sans',
|
||||
'menu_font' => 'Open Sans',
|
||||
'general_font' => 'Open Sans',
|
||||
'button_font' => 'Open Sans',
|
||||
'title_font' => 'Poppins',
|
||||
'title_font_fancy' => 'Open Sans',
|
||||
'title_font_medium' => 'Poppins',
|
||||
'footer_content_font' => 'Open Sans',
|
||||
'copyright_content_font' => 'Open Sans',
|
||||
'sidebar_title_font'=> 'Open Sans',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+564
@@ -0,0 +1,564 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Athlete2 Theme
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Olegnax.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.olegnax.com/license
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://www.olegnax.com/)
|
||||
* @license https://www.olegnax.com/license
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Exception;
|
||||
use Magento\Catalog\Helper\Product\Compare;
|
||||
use Magento\Cms\Model\Template\FilterProvider;
|
||||
use Magento\Customer\Model\Session;
|
||||
use Magento\Framework\App\Area;
|
||||
use Magento\Framework\App\Cache\Frontend\Pool;
|
||||
use Magento\Framework\App\Cache\TypeListInterface;
|
||||
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Framework\App\Config\Storage\WriterInterface;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\App\ProductMetadataInterface;
|
||||
use Magento\Framework\App\State;
|
||||
use Magento\Framework\Message\ManagerInterface;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Wishlist\Helper\Data;
|
||||
use Olegnax\Athlete2\Block\ChildTemplate;
|
||||
use Olegnax\Athlete2\Model\Api;
|
||||
use Olegnax\Athlete2\Model\Client;
|
||||
use Olegnax\Athlete2\Model\Encryption;
|
||||
use Olegnax\Athlete2\Model\Request;
|
||||
use stdClass;
|
||||
|
||||
class Helper extends AbstractHelper
|
||||
{
|
||||
|
||||
const XML_ENABLED = 'athlete2_settings/general/enable';
|
||||
const CONFIG_MODULE = 'athlete2_settings';
|
||||
const PRODUCT_CODE = '23693737';
|
||||
const STORE_CODE = '89MBca8Id0G8P61';
|
||||
const CHILD_TEMPLATE = ChildTemplate::class;
|
||||
const XML_PATH_LAZY = 'athlete2_settings/general/lazyload';
|
||||
const DATA_ATTR_NO_LAZY = 'no_need_lazy';
|
||||
|
||||
private $_get;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ObjectManager
|
||||
*/
|
||||
public $objectManager;
|
||||
protected $_lazyExcludeClass;
|
||||
protected $isArea =[];
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->objectManager = ObjectManager::getInstance();
|
||||
|
||||
parent::__construct($context);
|
||||
if (!$this->getModuleConfig(
|
||||
'general/install_date',
|
||||
0,
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
|
||||
)
|
||||
) {
|
||||
$this->setModuleConfig('general/install_date', time());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isLazyLoadEnabled()
|
||||
{
|
||||
return $this->getSystemValue(Helper::XML_ENABLED)
|
||||
&& $this->getSystemValue(Helper::XML_PATH_LAZY)
|
||||
&& $this->objectManager->get(\Olegnax\Core\Helper\Helper::class)->isLazyLoadEnabled();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param integer $storeCode
|
||||
* @param string $scopeType
|
||||
* @return mixed
|
||||
*/
|
||||
public function getModuleConfig($path = '', $storeCode = null, $scopeType = ScopeInterface::SCOPE_STORE)
|
||||
{
|
||||
if (empty($path)) {
|
||||
$path = static::CONFIG_MODULE;
|
||||
} else {
|
||||
$path = static::CONFIG_MODULE . '/' . $path;
|
||||
}
|
||||
return $this->getSystemValue($path, $storeCode, $scopeType);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param integer $storeCode
|
||||
* @param string $scopeType
|
||||
* @return mixed
|
||||
*/
|
||||
public function getSystemValue($path, $storeCode = null, $scopeType = ScopeInterface::SCOPE_STORE)
|
||||
{
|
||||
$value = $this->scopeConfig->getValue(
|
||||
$path,
|
||||
$scopeType,
|
||||
$storeCode
|
||||
);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function setModuleConfig(
|
||||
$path,
|
||||
$value,
|
||||
$scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
||||
$scopeId = 0
|
||||
) {
|
||||
if (!empty($path)) {
|
||||
$path = static::CONFIG_MODULE . '/' . $path;
|
||||
}
|
||||
return $this->setSystemValue($path, $value, $scope, $scopeId);
|
||||
}
|
||||
|
||||
public function setSystemValue($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
|
||||
{
|
||||
return $this->_loadObject(WriterInterface::class)->save($path, $value, $scope, $scopeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return mixed
|
||||
*/
|
||||
public function _loadObject($path)
|
||||
{
|
||||
return $this->objectManager->get($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnabled()
|
||||
{
|
||||
return (bool)$this->getConfig(static::XML_ENABLED);
|
||||
}
|
||||
|
||||
public function getConfig($path, $storeCode = null)
|
||||
{
|
||||
return $this->getSystemValue($path, $storeCode);
|
||||
}
|
||||
|
||||
public function getLayoutTemplateHtml($block, $option_path = '', $fileName = '', $arguments = [])
|
||||
{
|
||||
$value = $this->getConfig($option_path);
|
||||
|
||||
if (is_string($value) || is_numeric($value)) {
|
||||
return $this->getLayoutTemplateHtmlbyValue($block, $value, $fileName, $arguments);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getLayoutTemplateHtmlbyValue(
|
||||
$block,
|
||||
$value = null,
|
||||
$fileName = null,
|
||||
$arguments = [],
|
||||
$separator = '/'
|
||||
) {
|
||||
$_fileName = '';
|
||||
if (empty($fileName)) {
|
||||
$blockTemplate = $block->getTemplate();
|
||||
if (preg_match('/(\.[^\.]+?)$/', $blockTemplate)) {
|
||||
$fileName = preg_replace('/(\.[^\.]+?)$/', '%s%s$1', $blockTemplate);
|
||||
} else {
|
||||
$fileName .= '%s%s';
|
||||
}
|
||||
if (!preg_match('#([^_:]+)_([^_:]+)::#i', $fileName)) {
|
||||
$className = array_slice(array_filter(explode('\\', get_class($block))), 0, 2);
|
||||
if ('Magento' !== $className[0]) {
|
||||
$fileName = implode('_', $className) . '::' . $fileName;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$_fileName = $fileName;
|
||||
}
|
||||
$blockName = $separator . $block->getNameInLayout() . $separator . $_fileName . $separator . $value;
|
||||
$fileName = sprintf($fileName, $separator, $value);
|
||||
while ($block->getLayout()->getBlock($blockName)) {
|
||||
$blockName .= '_0';
|
||||
}
|
||||
$_block = $block->getLayout()->createBlock(static::CHILD_TEMPLATE, $blockName);
|
||||
$block->setChild($_block->getNameInLayout(), $_block);
|
||||
if (!empty($arguments) && is_array($arguments)) {
|
||||
$_block->addData($arguments);
|
||||
}
|
||||
$content = $_block->setTemplate($fileName)->toHtml();
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
public function isAdmin()
|
||||
{
|
||||
return $this->isArea(Area::AREA_ADMINHTML);
|
||||
}
|
||||
|
||||
public function isArea($area = Area::AREA_FRONTEND)
|
||||
{
|
||||
if (!isset($this->isArea[$area])) {
|
||||
/** @var State $state */
|
||||
$state = $this->_loadObject(State::class);
|
||||
|
||||
try {
|
||||
$this->isArea[$area] = ($state->getAreaCode() == $area);
|
||||
} catch (Exception $e) {
|
||||
$this->isArea[$area] = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->isArea[$area];
|
||||
}
|
||||
|
||||
public function deleteSystemValue($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
|
||||
{
|
||||
return $this->_loadObject(WriterInterface::class)->delete($path, $scope, $scopeId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function _createObject($path, $arguments = [])
|
||||
{
|
||||
return $this->objectManager->create($path, $arguments);
|
||||
}
|
||||
|
||||
public function isLoggedIn()
|
||||
{
|
||||
return $this->getSession()->isLoggedIn();
|
||||
}
|
||||
|
||||
public function getSession()
|
||||
{
|
||||
return ObjectManager::getInstance()->create(Session::class);
|
||||
}
|
||||
|
||||
function getWishlistCount()
|
||||
{
|
||||
$this->getSession();
|
||||
return $this->_loadObject(Data::class)->getItemCount();
|
||||
}
|
||||
|
||||
function getCompareListUrl()
|
||||
{
|
||||
$this->getSession();
|
||||
return $this->_loadObject(Compare::class)->getListUrl();
|
||||
}
|
||||
|
||||
function getCompareListCount()
|
||||
{
|
||||
$this->getSession();
|
||||
return $this->_loadObject(Compare::class)->getItemCount();
|
||||
}
|
||||
|
||||
public function isActivePlugin($name)
|
||||
{
|
||||
return $this->_moduleManager->isOutputEnabled($name);
|
||||
}
|
||||
|
||||
function getVersion()
|
||||
{
|
||||
return $this->_loadObject(ProductMetadataInterface::class)->getVersion();
|
||||
}
|
||||
|
||||
public function getBlockTemplateProcessor($content = '')
|
||||
{
|
||||
if (empty($content) || !is_string($content)) {
|
||||
$content = '';
|
||||
}
|
||||
return $this->_loadObject(FilterProvider::class)->getBlockFilter()->filter(trim($content));
|
||||
}
|
||||
|
||||
public function isHomePage()
|
||||
{
|
||||
$currentUrl = $this->getUrl('', ['_current' => true]);
|
||||
$urlRewrite = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
|
||||
return $currentUrl == $urlRewrite;
|
||||
}
|
||||
|
||||
public function getUrl($route = '', $params = [])
|
||||
{
|
||||
/** @var UrlInterface $urlBuilder */
|
||||
$urlBuilder = $this->_loadObject(UrlInterface::class);
|
||||
return $urlBuilder->getUrl($route, $params);
|
||||
}
|
||||
|
||||
public function isAccountPage()
|
||||
{
|
||||
$request = $this->objectManager->get(\Magento\Framework\App\Action\Context::class)->getRequest();
|
||||
return $request->getFullActionName() == 'customer_account_login';
|
||||
}
|
||||
public function isMulticheckout()
|
||||
{
|
||||
$request = $this->objectManager->get(\Magento\Framework\App\Action\Context::class)->getRequest();
|
||||
return $request->getFullActionName() == 'multishipping_checkout_login';
|
||||
}
|
||||
public function isMobile()
|
||||
{
|
||||
$user_agent = $_SERVER['HTTP_USER_AGENT'];
|
||||
$result = false;
|
||||
if (!empty($user_agent)) {
|
||||
$result = preg_match(
|
||||
'/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i',
|
||||
$user_agent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i',
|
||||
substr($user_agent, 0, 4)
|
||||
);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $a
|
||||
* @return object|stdClass
|
||||
* @throws Exception
|
||||
*/
|
||||
public function activate($a)
|
||||
{
|
||||
if (empty($a)) {
|
||||
throw new Exception(__('License Key can not be empty.'));
|
||||
}
|
||||
$b = static::STORE_CODE;
|
||||
$c = static::PRODUCT_CODE;
|
||||
$d = Request::DAILY_FREQUENCY;
|
||||
$e = parse_url($this->getSystemDefaultValue('web/unsecure/base_url'), PHP_URL_HOST);
|
||||
$e = preg_replace('#^www\.#i', '', $e);
|
||||
|
||||
$f = Api::activate(
|
||||
Client::instance(),
|
||||
function () use (&$b, &$c, &$a, &$d, &$e) {
|
||||
return Request::create(
|
||||
'https://olegnax.com/wp-admin/admin-ajax.php',
|
||||
$b,
|
||||
$c,
|
||||
$a,
|
||||
$e,
|
||||
$d
|
||||
);
|
||||
},
|
||||
[&$this, 'encrypt_save']
|
||||
);
|
||||
return $f;
|
||||
}
|
||||
|
||||
public function getSystemDefaultValue($path)
|
||||
{
|
||||
$value = $this->scopeConfig->getValue(
|
||||
$path,
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
||||
0
|
||||
);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|object|stdClass
|
||||
* @throws Exception
|
||||
*/
|
||||
public function deactivate()
|
||||
{
|
||||
$a = $this->load_decrypt();
|
||||
if ($a === false) {
|
||||
return false;
|
||||
}
|
||||
$b = parse_url($this->getSystemDefaultValue('web/unsecure/base_url'), PHP_URL_HOST);
|
||||
$b = preg_replace('#^www\.#i', '', $b);
|
||||
// Validate
|
||||
$c = Api::deactivate(
|
||||
Client::instance(),
|
||||
function () use (&$a) {
|
||||
return new Request($a);
|
||||
},
|
||||
[&$this, 'encrypt_save'],
|
||||
$b
|
||||
);
|
||||
if ($c && $c->error === true) {
|
||||
$this->encrypt_save('');
|
||||
// Force deactivation
|
||||
$c->error = false;
|
||||
$c->message = __('Deactivated.');
|
||||
}
|
||||
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|string
|
||||
*/
|
||||
protected function load_decrypt()
|
||||
{
|
||||
// Load
|
||||
$a = $this->getSystemDefaultValue(base64_decode('YXRobGV0ZTJfbGljZW5zZS9nZW5lcmFsL2xpY2Vuc2U='));
|
||||
// Decrypt
|
||||
if (!empty($a)) {
|
||||
$b = static::STORE_CODE;
|
||||
return Encryption::c(
|
||||
$a,
|
||||
$b
|
||||
);
|
||||
} else {
|
||||
$a = false;
|
||||
}
|
||||
|
||||
return $a;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $a
|
||||
*/
|
||||
public function encrypt_save($a)
|
||||
{
|
||||
// Check for downloadbles and updates
|
||||
$b = json_decode((string)$a);
|
||||
$c = $this->load_decrypt();
|
||||
$d = $c !== false ? json_decode((string)$c) : $c;
|
||||
$e = static::STORE_CODE;
|
||||
// Save license string
|
||||
$this->configFactory()->saveConfig(
|
||||
base64_decode('YXRobGV0ZTJfbGljZW5zZS9nZW5lcmFsL2xpY2Vuc2U='),
|
||||
is_string($a) ? Encryption::a($a, $e) : '',
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
||||
0
|
||||
);
|
||||
$this->configFactory()->saveConfig(
|
||||
'athlete2_license/general/avaible_update',
|
||||
($d !== false
|
||||
&& isset($b->data->downloadable)
|
||||
&& isset($d->data->downloadable)
|
||||
&& $b->data->downloadable->name !== $d->data->downloadable->name
|
||||
) ? 1 : 0,
|
||||
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
|
||||
0
|
||||
);
|
||||
$this->clearCache();
|
||||
}
|
||||
|
||||
public function configFactory()
|
||||
{
|
||||
return $this->_loadObject(ConfigInterface::class);
|
||||
}
|
||||
|
||||
public function clearCache($types = ['config'])
|
||||
{
|
||||
$cacheTypeList = $this->_loadObject(TypeListInterface::class);
|
||||
$CacheFrontendPool = $this->_loadObject(Pool::class);
|
||||
foreach ($types as $type) {
|
||||
$cacheTypeList->cleanType($type);
|
||||
}
|
||||
foreach ($CacheFrontendPool as $cacheFrontend) {
|
||||
$cacheFrontend->getBackend()->clean();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get()
|
||||
{
|
||||
if (is_null($this->_get)){
|
||||
$license_key = $this->load_decrypt();
|
||||
$this->_get = $license_key !== false && $this->validate() ? json_decode($license_key) : false;
|
||||
}
|
||||
|
||||
return $this->_get;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $a
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function validate($a = false)
|
||||
{
|
||||
$b = $this->load_decrypt();
|
||||
if ($b === false) {
|
||||
return false;
|
||||
}
|
||||
// Prepare connection retries
|
||||
$c = null;
|
||||
$d = 0;
|
||||
$e = null;
|
||||
$f = parse_url($this->getSystemDefaultValue('web/unsecure/base_url'), PHP_URL_HOST);
|
||||
$f = preg_replace('#^www\.#i', '', $f);
|
||||
|
||||
// Validate
|
||||
return Api::validate(
|
||||
Client::instance(),
|
||||
function () use (&$b) {
|
||||
return new Request($b);
|
||||
},
|
||||
[&$this, 'encrypt_save'],
|
||||
$f,
|
||||
$a, // Force
|
||||
$c === null ? true : $c,
|
||||
$d ? $d : 2,
|
||||
$e ? $e : '+4 hour'
|
||||
);
|
||||
}
|
||||
|
||||
public function check()
|
||||
{
|
||||
$a = $this->load_decrypt();
|
||||
if ($a === false) {
|
||||
return false;
|
||||
}
|
||||
$f = parse_url($this->getSystemDefaultValue('web/unsecure/base_url'), PHP_URL_HOST);
|
||||
$f = preg_replace('#^www\.#i', '', $f);
|
||||
// Prepare connection retries
|
||||
$c = null;
|
||||
$d = 0;
|
||||
$e = null;
|
||||
|
||||
// Validate and return response
|
||||
return Api::check(
|
||||
Client::instance(),
|
||||
function () use (&$a) {
|
||||
return new Request($a);
|
||||
},
|
||||
[&$this, 'encrypt_save'],
|
||||
$f,
|
||||
$c === null ? true : $c,
|
||||
$d ? $d : 2,
|
||||
$e ? $e : '+1 hour'
|
||||
);
|
||||
}
|
||||
|
||||
private function messageManager()
|
||||
{
|
||||
return $this->_loadObject(ManagerInterface::class);
|
||||
}
|
||||
}
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
/**
|
||||
* Image helper
|
||||
*
|
||||
* Copyright © 2015 Magento. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
class Image extends \Olegnax\Core\Helper\Image
|
||||
{
|
||||
}
|
||||
+438
@@ -0,0 +1,438 @@
|
||||
<?php
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Exception;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
use Magento\Framework\App\State;
|
||||
use Magento\Framework\Escaper;
|
||||
use Magento\Framework\Exception\LocalizedException;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Asset\Repository;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class LazyLoad extends AbstractHelper
|
||||
{
|
||||
|
||||
const PLACEHOLDERE = 'athlete2/placeholder.png';
|
||||
const PLACEHOLDER_TEMPLATE = '{dirname}/lazy-placeholders/{width}_{height}.{extension}';
|
||||
const SVG_ENABLED = 'athlete2_settings/general/lazyload_svg';
|
||||
const EXCLUDE = 'athlete2_settings/general/lazyload_exclude';
|
||||
const RESIZED_PLACEHOLDER = 'athlete2_settings/general/lazyload_resized_placeholder';
|
||||
const SIMPLE = 'athlete2_settings/general/lazyload_simple';
|
||||
const PRELOADER_IMG = 'Olegnax_Core/images/preloader-img.svg';
|
||||
protected $_lazyExcludeClass;
|
||||
protected $isEnabled;
|
||||
/**
|
||||
* @var Helper
|
||||
*/
|
||||
protected $athleteHelper;
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $storeManager;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $urlMedia;
|
||||
/**
|
||||
* @var Image
|
||||
*/
|
||||
protected $imageHelper;
|
||||
/**
|
||||
* @var false|string
|
||||
*/
|
||||
protected $placeholder;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $placeholderDefault;
|
||||
/**
|
||||
* @var Escaper
|
||||
*/
|
||||
protected $escaper;
|
||||
/**
|
||||
* @var State
|
||||
*/
|
||||
protected $_appState;
|
||||
|
||||
/**
|
||||
* LazyLoad constructor.
|
||||
* @param Context $context
|
||||
* @param Image $imageHelper
|
||||
* @param StoreManagerInterface $storeManager
|
||||
* @param Escaper $escaper
|
||||
* @param Helper $helper
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Image $imageHelper,
|
||||
StoreManagerInterface $storeManager,
|
||||
Escaper $escaper,
|
||||
State $appState,
|
||||
Helper $helper
|
||||
) {
|
||||
$this->athleteHelper = $helper;
|
||||
$this->escaper = $escaper;
|
||||
$this->imageHelper = $imageHelper;
|
||||
$this->_appState = $appState;
|
||||
$this->urlMedia = rtrim(preg_replace(
|
||||
'@^http[s]*\:@i',
|
||||
'',
|
||||
$storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA)
|
||||
), "/");
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html
|
||||
* @return bool
|
||||
*/
|
||||
public function _filterImg($html = '')
|
||||
{
|
||||
if (preg_match('@(data-original=\"|data-ox-image)@i', $html)) {
|
||||
return false;
|
||||
}
|
||||
$class = $this->getExcludeClass();
|
||||
if (!empty($class) && preg_match('@class="([^"]+)"@i', $html, $matches)) {
|
||||
$matches = array_filter(explode(' ', $matches[1]));
|
||||
$intersect = array_intersect($class, $matches);
|
||||
return empty($intersect);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|mixed|string[]
|
||||
*/
|
||||
public function getExcludeClass()
|
||||
{
|
||||
if (empty($this->_lazyExcludeClass)) {
|
||||
$class = $this->getConfig(static::EXCLUDE);
|
||||
if (empty($class)) {
|
||||
$class = ['rev-slidebg', 'lazy', 'owl-lazy', 'rs-lazyload'];
|
||||
} elseif (preg_match_all('@\S+@', $class, $matches)) {
|
||||
$class = array_filter($matches[0]);
|
||||
$class[] = 'lazy';
|
||||
$class[] = 'owl-lazy';
|
||||
$class[] = 'rev-slidebg';
|
||||
$class[] = 'rs-lazyload';
|
||||
}
|
||||
if (is_array($class) && !empty($class)) {
|
||||
$this->_lazyExcludeClass = $class;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_lazyExcludeClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $storeCode
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig($path, $storeCode = null)
|
||||
{
|
||||
$value = $this->scopeConfig->getValue($path, ScopeInterface::SCOPE_STORE, $storeCode);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html
|
||||
* @return string|string[]
|
||||
*/
|
||||
public function replaceImageToLazy($html = '')
|
||||
{
|
||||
if ($this->isEnabled() && preg_match_all('@<img[^<>]+src\="[^<>]+>@ims', $html, $matches)) {
|
||||
$matches = array_filter($matches[0], [$this, '_filterImg']);
|
||||
if (!empty($matches) && is_array($matches)) {
|
||||
$_matches = [];
|
||||
$resizedPlaceholder = '1' == $this->getConfig(static::RESIZED_PLACEHOLDER);
|
||||
$simple = '1' == $this->getConfig(static::SIMPLE);
|
||||
foreach ($matches as $htmlImg) {
|
||||
$_matches[$htmlImg] = $this->modifyImg($htmlImg, $simple, $resizedPlaceholder);
|
||||
}
|
||||
if (!empty($_matches)) {
|
||||
$search = array_keys($_matches);
|
||||
$replace = array_values($_matches);
|
||||
$html = str_replace($search, $replace, $html);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEnabled()
|
||||
{
|
||||
if (is_null($this->isEnabled)) {
|
||||
$this->isEnabled = $this->athleteHelper->isLazyLoadEnabled();
|
||||
}
|
||||
|
||||
return $this->isEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html
|
||||
* @param bool $simple
|
||||
* @param bool $resizedPlaceholder
|
||||
* @return string
|
||||
*/
|
||||
protected function modifyImg($html, $simple = false, $resizedPlaceholder = false)
|
||||
{
|
||||
$encoded = false !== strpos((string)$html, '\"');
|
||||
if ($encoded) {
|
||||
$html = json_decode('"' . $html . '"');
|
||||
}
|
||||
|
||||
if (!$simple) {
|
||||
$placeholder = $this->getDefaultPlaceHolder();
|
||||
if ($resizedPlaceholder) {
|
||||
$size = [];
|
||||
if (preg_match('@width="(\d+)"@im', $html, $matches)) {
|
||||
$size[] = (int)$matches[1];
|
||||
}
|
||||
if (preg_match('@height="(\d+)"@im', $html, $matches)) {
|
||||
$size[] = (int)$matches[1];
|
||||
}
|
||||
$size = array_filter($size);
|
||||
if (2 != count($size) && preg_match('@src="([^"]+)"@im', $html, $matches)) {
|
||||
$size = $this->getSizeImageFromUrl($matches[1]);
|
||||
}
|
||||
if (2 == count($size)) {
|
||||
if ($this->isSvg()) {
|
||||
$placeholder = $this->createPlaceholder($size);
|
||||
} else {
|
||||
try {
|
||||
$image = $this->getPlaceholderHelper()->adaptiveResize($size)->getUrl();
|
||||
$placeholder = $image;
|
||||
} catch (Exception $e) {
|
||||
if ( State::MODE_DEVELOPER == $this->_appState->getMode()) {
|
||||
$this->_logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!preg_match('@ height="@i', $html)) {
|
||||
$html = preg_replace('@(src="[^\"]+")@im', '$1 height="' . $size[1] . '"', $html);
|
||||
}
|
||||
if (!preg_match('@ width="@i', $html)) {
|
||||
$html = preg_replace('@(src="[^\"]+")@im', '$1 width="' . $size[0] . '"', $html);
|
||||
}
|
||||
}
|
||||
}
|
||||
$html = preg_replace('@src="([^\"]+)"@im', 'src="' . $placeholder . '" data-original="$1"', $html);
|
||||
|
||||
$html = preg_replace('@class=""@i', '', $html);
|
||||
if (preg_match('@class="@i', $html)) {
|
||||
$html = preg_replace('@class="([^\"]+)"@im', 'class="$1 lazy"', $html);
|
||||
} else {
|
||||
$html = preg_replace('@<img@im', '$0 class="lazy"', $html);
|
||||
}
|
||||
$pattern = '@\sloading\s*=\s*"lazy"@i';
|
||||
if ( preg_match( $pattern, $html ) ) {
|
||||
$html = preg_replace( $pattern, '', $html );
|
||||
}
|
||||
}
|
||||
|
||||
// if (!preg_match('@\sloading\s*=\s*"lazy"@i', $html) && !preg_match('@src="data@i', $html)) {
|
||||
// $html = preg_replace('@<img@im', '$0 loading="lazy"', $html);
|
||||
// }
|
||||
|
||||
$html = preg_replace('@ {2,}@', ' ', preg_replace('@[\r\n\t]+@', ' ', $html));
|
||||
if ($encoded) {
|
||||
$html = trim((string)json_encode($html), '"');
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDefaultPlaceHolder()
|
||||
{
|
||||
if ($this->isSvg()) {
|
||||
$this->placeholderDefault = $this->createPlaceholder();
|
||||
}
|
||||
if (empty($this->placeholderDefault)) {
|
||||
$this->placeholderDefault = $this->getViewFileUrl(static::PRELOADER_IMG);
|
||||
}
|
||||
return $this->placeholderDefault;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isSvg()
|
||||
{
|
||||
return (bool)$this->athleteHelper->getSystemValue(static::SVG_ENABLED);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int[] $size
|
||||
* @return string
|
||||
*/
|
||||
public function createPlaceholder($size = [1, 1])
|
||||
{
|
||||
$html = 'data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ' . $size[0] . ' ' .
|
||||
$size[1] . '" width="' . $size[0] . '" height="' . $size[1] . '"></svg>';
|
||||
return $this->escaper->escapeHtmlAttr($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve url of a view file
|
||||
*
|
||||
* @param string $fileId
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
public function getViewFileUrl($fileId, array $params = [])
|
||||
{
|
||||
try {
|
||||
$params = array_merge(['_secure' => $this->_loadObject(RequestInterface::class)->isSecure()], $params);
|
||||
return $this->_loadObject(Repository::class)->getUrlWithParams($fileId, $params);
|
||||
} catch (LocalizedException $e) {
|
||||
return $this->_getNotFoundUrl();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $object
|
||||
* @return mixed
|
||||
*/
|
||||
protected function _loadObject($object)
|
||||
{
|
||||
return ObjectManager::getInstance()->get($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get 404 file not found url
|
||||
*
|
||||
* @param string $route
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
protected function _getNotFoundUrl($route = '', $params = ['_direct' => 'core/index/notFound'])
|
||||
{
|
||||
return $this->getUrl($route, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate url by route and parameters
|
||||
*
|
||||
* @param string $route
|
||||
* @param array $params
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl($route = '', $params = [])
|
||||
{
|
||||
return $this->_loadObject(UrlInterface::class)->getUrl($route, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return array|int[]
|
||||
*/
|
||||
protected function getSizeImageFromUrl($path)
|
||||
{
|
||||
if (!empty($path)) {
|
||||
return $this->getSizeImage($this->getAbsoluteImagePath($path));
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @return array|null
|
||||
*/
|
||||
protected function getSizeImage($path)
|
||||
{
|
||||
try {
|
||||
$image = $this->imageHelper->init($path);
|
||||
if (!$image->getOriginalWidth() || !$image->getOriginalHeight()) {
|
||||
throw new Exception(__('Image not found: ' . $path));
|
||||
}
|
||||
|
||||
return [$image->getOriginalWidth(), $image->getOriginalHeight()];
|
||||
} catch (Exception $e) {
|
||||
if ( State::MODE_DEVELOPER == $this->_appState->getMode()) {
|
||||
$this->_logger->warning('Athlete Lazy Loader: The file does not exist or not accessible, skipping. Response: ' . $e->getMessage());
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return string|string[]|null
|
||||
*/
|
||||
protected function getAbsoluteImagePath($url)
|
||||
{
|
||||
$url = (string)preg_replace(
|
||||
'@^http[s]*\:@i',
|
||||
'',
|
||||
$url
|
||||
);
|
||||
if (false === strpos($url, $this->urlMedia)) {
|
||||
$path = preg_replace('@^\/media@i', '', $url);
|
||||
} else {
|
||||
$path = str_replace($this->urlMedia, '', $url);
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Image
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getPlaceholderHelper()
|
||||
{
|
||||
return $this->imageHelper->init(
|
||||
static::PLACEHOLDERE,
|
||||
[
|
||||
'fileTemplate' => static::PLACEHOLDER_TEMPLATE,
|
||||
'quality' => 1,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
public function getPlaceHolder($url)
|
||||
{
|
||||
$size = $this->getSizeImageFromUrl($url);
|
||||
if (!empty($size)) {
|
||||
if ($this->isSvg()) {
|
||||
return $this->createPlaceholder($size);
|
||||
} else {
|
||||
try {
|
||||
$image = $this->getPlaceholderHelper()->adaptiveResize($size)->getUrl();
|
||||
return $image;
|
||||
} catch (Exception $e) {
|
||||
if ( State::MODE_DEVELOPER == $this->_appState->getMode()) {
|
||||
$this->_logger->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return $this->getDefaultPlaceHolder();
|
||||
}
|
||||
}
|
||||
+261
@@ -0,0 +1,261 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Athlete2 Theme
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Olegnax.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.olegnax.com/license
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://www.olegnax.com/)
|
||||
* @license https://www.olegnax.com/license
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\ResourceModel\Url;
|
||||
use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\App\ResourceConnection;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Registry;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class PrevNextProduct extends AbstractHelper
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var ObjectManager
|
||||
*/
|
||||
public $_objectManager;
|
||||
/**
|
||||
*
|
||||
* @var Registry
|
||||
*/
|
||||
protected $_coreRegistry;
|
||||
/**
|
||||
*
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
protected $_productRepository;
|
||||
/**
|
||||
*
|
||||
* @var ResourceConnection
|
||||
*/
|
||||
protected $_resource;
|
||||
/**
|
||||
*
|
||||
* @var Url
|
||||
*/
|
||||
protected $_catalogUrl;
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $_storeManager;
|
||||
|
||||
/**
|
||||
* PrevNextProduct constructor.
|
||||
* @param Context $context
|
||||
* @param Registry $coreRegistry
|
||||
* @param ProductRepositoryInterface $productRepository
|
||||
* @param Url $catalogUrl
|
||||
* @param ResourceConnection $resource
|
||||
* @param StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Registry $coreRegistry,
|
||||
ProductRepositoryInterface $productRepository,
|
||||
Url $catalogUrl,
|
||||
ResourceConnection $resource,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->_coreRegistry = $coreRegistry;
|
||||
$this->_productRepository = $productRepository;
|
||||
$this->_resource = $resource;
|
||||
$this->_catalogUrl = $catalogUrl;
|
||||
$this->_storeManager = $storeManager;
|
||||
$this->_objectManager = ObjectManager::getInstance();
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|ProductInterface|Product
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getNextProduct()
|
||||
{
|
||||
return $this->getSiblingProduct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $index
|
||||
* @return bool|ProductInterface|Product
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
protected function getSiblingProduct($index = 1)
|
||||
{
|
||||
$product = $this->_coreRegistry->registry('current_product');
|
||||
if (!$product) {
|
||||
return false;
|
||||
}
|
||||
$productId = $product->getId();
|
||||
$category = $this->_coreRegistry->registry('current_category');
|
||||
|
||||
if (!$category) {
|
||||
/** @var Product $product */
|
||||
$product = $this->_objectManager->create(Product::class)->load($productId);
|
||||
/** @var Category $category */
|
||||
$category = $product->getCategory();
|
||||
if (!$category) {
|
||||
foreach ($product->getCategoryCollection() as $parent_cat) {
|
||||
$category = $parent_cat;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($category) {
|
||||
$productIds = array_keys($this->getProductsPosition($category));
|
||||
$productPositions = array_flip($productIds);
|
||||
if (array_key_exists($productId, $productPositions)) {
|
||||
$newProductPosition = $productPositions[$productId] + $index;
|
||||
|
||||
if (array_key_exists($newProductPosition, $productIds)) {
|
||||
$productId = $productIds[$newProductPosition];
|
||||
|
||||
$product = $this->_productRepository->getById($productId);
|
||||
$product->setCategoryId($category->getId());
|
||||
|
||||
if ($productId = $product->getId()) {
|
||||
$urlDatas = $this->getRewriteByProductStore([
|
||||
$productId => $category->getStoreId()
|
||||
]);
|
||||
|
||||
if (array_key_exists($productId, $urlDatas)) {
|
||||
$urlData = $urlDatas[$productId];
|
||||
$product->setUrlDataObject(new DataObject($urlData));
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Category $category
|
||||
* @return array
|
||||
*/
|
||||
protected function getProductsPosition($category)
|
||||
{
|
||||
$connection = $this->_resource->getConnection();
|
||||
$select = $connection->select()->from(
|
||||
$this->_resource->getTableName('catalog_category_product_index'),
|
||||
['product_id', 'position']
|
||||
)->where(
|
||||
'category_id = :category_id'
|
||||
)->where(
|
||||
'store_id = :store_id'
|
||||
)->order('position', 'ASC');
|
||||
$bind = [
|
||||
'category_id' => (int)$category->getId(),
|
||||
'store_id' => $category->getStoreId(),
|
||||
];
|
||||
|
||||
return $connection->fetchPairs($select, $bind);
|
||||
}
|
||||
|
||||
public function getRewriteByProductStore(array $products)
|
||||
{
|
||||
$result = [];
|
||||
|
||||
if (empty($products)) {
|
||||
return $result;
|
||||
}
|
||||
$connection = $this->_catalogUrl->getConnection();
|
||||
|
||||
$storesProducts = [];
|
||||
foreach ($products as $productId => $storeId) {
|
||||
$storesProducts[$storeId][] = $productId;
|
||||
}
|
||||
/** @var TableMaintainer $tableMaintainer */
|
||||
$tableMaintainer = ObjectManager::getInstance()->get(TableMaintainer::class);
|
||||
|
||||
foreach ($storesProducts as $storeId => $productIds) {
|
||||
$select = $connection->select()->from(
|
||||
['i' => $tableMaintainer->getMainTable($storeId)],
|
||||
['product_id', 'store_id', 'visibility']
|
||||
)->joinLeft(
|
||||
['u' => $this->_catalogUrl->getMainTable()],
|
||||
'i.product_id = u.entity_id AND i.store_id = u.store_id'
|
||||
. ' AND u.entity_type = "' . ProductUrlRewriteGenerator::ENTITY_TYPE . '"',
|
||||
['request_path']
|
||||
)->joinLeft(
|
||||
['r' => $this->_catalogUrl->getTable('catalog_url_rewrite_product_category')],
|
||||
'u.url_rewrite_id = r.url_rewrite_id AND r.category_id is NULL',
|
||||
[]
|
||||
);
|
||||
|
||||
$bind = [];
|
||||
foreach ($productIds as $productId) {
|
||||
$catId = $this->_storeManager->getStore($storeId)->getRootCategoryId();
|
||||
$productBind = 'product_id' . $productId;
|
||||
$storeBind = 'store_id' . $storeId;
|
||||
$catBind = 'category_id' . $catId;
|
||||
$bindArray = [
|
||||
'i.product_id = :' . $productBind,
|
||||
'i.store_id = :' . $storeBind,
|
||||
'i.category_id = :' . $catBind,
|
||||
'u.metadata is Null',
|
||||
];
|
||||
$cond = '(' . implode(' AND ', $bindArray) . ')';
|
||||
$bind[$productBind] = $productId;
|
||||
$bind[$storeBind] = $storeId;
|
||||
$bind[$catBind] = $catId;
|
||||
$select->orWhere($cond);
|
||||
}
|
||||
|
||||
$rowSet = $connection->fetchAll($select, $bind);
|
||||
foreach ($rowSet as $row) {
|
||||
$result[$row['product_id']] = [
|
||||
'store_id' => $row['store_id'],
|
||||
'visibility' => $row['visibility'],
|
||||
'url_rewrite' => $row['request_path'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|ProductInterface|Product
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getPreviousProduct()
|
||||
{
|
||||
return $this->getSiblingProduct(-1);
|
||||
}
|
||||
|
||||
}
|
||||
+716
@@ -0,0 +1,716 @@
|
||||
<?php
|
||||
/**
|
||||
* Athlete2 Theme
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Olegnax.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.olegnax.com/license
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://www.olegnax.com/)
|
||||
* @license https://www.olegnax.com/license
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Exception;
|
||||
use Magento\Catalog\Block\Product\Image as CatalogBlockProductImage;
|
||||
use Magento\Catalog\Helper\Image;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\Product\Gallery\ReadHandler;
|
||||
use Magento\Catalog\Model\Product\Image\ParamsBuilder;
|
||||
use Magento\Catalog\Model\View\Asset\ImageFactory;
|
||||
use Magento\Catalog\Model\View\Asset\PlaceholderFactory;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\App\ProductMetadataInterface;
|
||||
use Magento\Framework\Config\View;
|
||||
use Magento\Framework\DataObject;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Image\AdapterFactory;
|
||||
use Magento\Framework\View\ConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
/**
|
||||
* Description of Image
|
||||
*
|
||||
* @author Master
|
||||
*/
|
||||
class ProductImage extends AbstractHelper
|
||||
{
|
||||
const HOVER_IMAGE_ENABLED = 'athlete2_settings/products_listing/hover_image';
|
||||
const TEMPLATE = 'Magento_Catalog::product/image_with_borders.phtml';
|
||||
const HOVER_TEMPLATE = 'Magento_Catalog::product/hover_image_with_borders.phtml';
|
||||
const THUMB_TEMPLATE = 'Magento_Catalog::product/thumb_image_with_borders.phtml';
|
||||
/**
|
||||
* @var Image
|
||||
*/
|
||||
protected $imageHelper;
|
||||
/**
|
||||
* @var ConfigInterface
|
||||
*/
|
||||
protected $viewConfig;
|
||||
/**
|
||||
* @var View
|
||||
*/
|
||||
protected $configView;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $_magentoVersion;
|
||||
/**
|
||||
* @var Video
|
||||
*/
|
||||
protected $videoHelper;
|
||||
/**
|
||||
* @var AdapterFactory
|
||||
*/
|
||||
protected $adapterFactory;
|
||||
/**
|
||||
* @var ParamsBuilder
|
||||
*/
|
||||
private $imageParamsBuilder;
|
||||
|
||||
/**
|
||||
* ProductImage constructor.
|
||||
*
|
||||
* @param Context $context
|
||||
* @param Image $imageHelper
|
||||
* @param ConfigInterface $viewConfig
|
||||
* @param ParamsBuilder $imageParamsBuilder
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Image $imageHelper,
|
||||
ConfigInterface $viewConfig,
|
||||
ParamsBuilder $imageParamsBuilder,
|
||||
Video $videoHelper,
|
||||
AdapterFactory $adapterFactory
|
||||
) {
|
||||
$this->imageHelper = $imageHelper;
|
||||
$this->viewConfig = $viewConfig;
|
||||
$this->imageParamsBuilder = $imageParamsBuilder;
|
||||
$this->videoHelper = $videoHelper;
|
||||
$this->adapterFactory = $adapterFactory;
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function checkDependencies() {
|
||||
try {
|
||||
$this->adapterFactory->create();
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
$this->_logger->critical($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $imageId
|
||||
* @param string $imageIdHover
|
||||
* @param string $template
|
||||
* @param array $attributes
|
||||
* @param array $properties
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getImageHover(
|
||||
Product $product,
|
||||
$imageId,
|
||||
$imageIdHover,
|
||||
$template = self::HOVER_TEMPLATE,
|
||||
array $attributes = [],
|
||||
$properties = []
|
||||
) {
|
||||
if (!$this->hasHoverImage($product, $imageId, $imageIdHover)
|
||||
&& !$this->videoHelper->videoOnHover($product)
|
||||
) {
|
||||
return $this->getImage($product, $imageId, self::TEMPLATE, $attributes, $properties);
|
||||
}
|
||||
if ($this->videoHelper->videoOnHover($product)) {
|
||||
$imageIdHover = $imageId;
|
||||
}
|
||||
|
||||
$image = $this->_getImage($product, $imageId, $properties)->getUrl();
|
||||
$imageMiscParams = $this->getImageParams($imageId);
|
||||
$image_hoverMiscParams = $this->getImageParams($imageIdHover);
|
||||
|
||||
$image_hover = $this->resizeImage(
|
||||
$product,
|
||||
$imageIdHover,
|
||||
[
|
||||
$imageMiscParams['image_width'],
|
||||
$imageMiscParams['image_height'],
|
||||
],
|
||||
$properties
|
||||
)->getUrl();
|
||||
|
||||
$data = [
|
||||
'data' => [
|
||||
'template' => $template,
|
||||
'product_id' => $product->getId(),
|
||||
'product' => $product,
|
||||
'image_id' => $imageId,
|
||||
'image_hover_id' => $imageIdHover,
|
||||
'image_url' => $image,
|
||||
'image_hover_url' => $image_hover,
|
||||
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
|
||||
'label_hover' => $this->getLabel($product, $image_hoverMiscParams['image_type']),
|
||||
'width' => $imageMiscParams['image_width'],
|
||||
'height' => $imageMiscParams['image_height'],
|
||||
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
|
||||
'class' => $this->getClass($attributes),
|
||||
'custom_attributes' => $this->getStringCustomAttributes($attributes),
|
||||
],
|
||||
];
|
||||
|
||||
return $this->_createTemplate($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $imageId
|
||||
* @param string $imageId_hover
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasHoverImage(Product $product, $imageId, $imageId_hover)
|
||||
{
|
||||
if ($this->hoverImageEnabled()) {
|
||||
if ($imageId != $imageId_hover) {
|
||||
$_imageId = $this->getImageParams($imageId);
|
||||
$_imageId_hover = $this->getImageParams($imageId_hover);
|
||||
if ($_imageId['image_type'] !== $_imageId_hover['image_type']) {
|
||||
$image = $product->getData($_imageId['image_type']);
|
||||
$image_hover = $product->getData($_imageId_hover['image_type']);
|
||||
return $image && $image_hover && 'no_selection' !== $image_hover && $image !== $image_hover;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function hoverImageEnabled()
|
||||
{
|
||||
return (bool)$this->getConfig(static::HOVER_IMAGE_ENABLED);
|
||||
}
|
||||
|
||||
public function getConfig($path, $storeCode = null)
|
||||
{
|
||||
return $this->getSystemValue($path, $storeCode);
|
||||
}
|
||||
|
||||
public function getSystemValue($path, $storeCode = null, $scopeType = ScopeInterface::SCOPE_STORE)
|
||||
{
|
||||
$value = $this->scopeConfig->getValue(
|
||||
$path,
|
||||
$scopeType,
|
||||
$storeCode
|
||||
);
|
||||
if (is_null($value)) {
|
||||
$value = '';
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $imageId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getImageParams($imageId)
|
||||
{
|
||||
$viewImageConfig = $this->getConfigView()->getMediaAttributes(
|
||||
'Magento_Catalog',
|
||||
Image::MEDIA_TYPE_CONFIG_NODE,
|
||||
$imageId
|
||||
);
|
||||
|
||||
$imageMiscParams = $this->imageParamsBuilder->build($viewImageConfig);
|
||||
if (empty($imageMiscParams)) {
|
||||
$imageMiscParams = $this->getDefaultParams();
|
||||
$this->_logger->critical(sprintf('No options found for "%s" images!', $imageId));
|
||||
}
|
||||
|
||||
return $imageMiscParams;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve config view
|
||||
*
|
||||
* @return View
|
||||
*/
|
||||
protected function getConfigView()
|
||||
{
|
||||
if (!$this->configView) {
|
||||
$this->configView = $this->viewConfig->getViewConfig();
|
||||
}
|
||||
return $this->configView;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected function getDefaultParams()
|
||||
{
|
||||
return [
|
||||
"image_type" => "small_image",
|
||||
"image_height" => 240,
|
||||
"image_width" => 240,
|
||||
"background" => [255, 255, 255],
|
||||
"quality" => 80,
|
||||
"keep_aspect_ratio" => true,
|
||||
"keep_frame" => true,
|
||||
"keep_transparency" => true,
|
||||
"constrain_only" => true,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $imageId
|
||||
* @param string $template
|
||||
* @param array $attributes
|
||||
* @param array $properties
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getImage(
|
||||
Product $product,
|
||||
$imageId,
|
||||
$template = self::TEMPLATE,
|
||||
array $attributes = [],
|
||||
$properties = []
|
||||
) {
|
||||
$imageMiscParams = $this->getImageParams($imageId);
|
||||
$originalFilePath = $product->getData($imageMiscParams['image_type']);
|
||||
|
||||
$isPlaceholder = $originalFilePath === null || $originalFilePath === 'no_selection';
|
||||
$class = '\Magento\Catalog\Model\View\Asset\\' . ($isPlaceholder ? 'PlaceholderFactory' : 'ImageFactory');
|
||||
if (class_exists($class)) {
|
||||
/** @var ImageFactory|PlaceholderFactory $viewAssetImageFactory */
|
||||
$viewAssetImageFactory = ObjectManager::getInstance()->get($class);
|
||||
$image = $viewAssetImageFactory->create(
|
||||
[
|
||||
'miscParams' => $imageMiscParams,
|
||||
'filePath' => $originalFilePath,
|
||||
'type' => $imageMiscParams['image_type']
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$image = $this->_getImage($product, $imageId, $properties);
|
||||
}
|
||||
|
||||
$attributes = $attributes === null ? [] : $attributes;
|
||||
|
||||
$data = [
|
||||
'data' => [
|
||||
'template' => $template,
|
||||
'product_id' => $product->getId(),
|
||||
'product' => $product,
|
||||
'image_id' => $imageId,
|
||||
'image_url' => $image->getUrl(),
|
||||
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
|
||||
'width' => $imageMiscParams['image_width'],
|
||||
'height' => $imageMiscParams['image_height'],
|
||||
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
|
||||
'class' => $this->getClass($attributes),
|
||||
'custom_attributes' => $this->getStringCustomAttributes($attributes),
|
||||
],
|
||||
];
|
||||
|
||||
return $this->_createTemplate($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $imageId
|
||||
* @param array $properties
|
||||
*
|
||||
* @return Image
|
||||
*/
|
||||
private function _getImage(Product $product, $imageId, $properties = [])
|
||||
{
|
||||
return $this->imageHelper->init($product, $imageId, $properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
*
|
||||
* @param string $imageType
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getLabel(Product $product, string $imageType): string
|
||||
{
|
||||
$label = "";
|
||||
if (!empty($imageType)) {
|
||||
$label = $product->getData($imageType . '_' . 'label');
|
||||
}
|
||||
if (empty($label)) {
|
||||
$label = $product->getName();
|
||||
}
|
||||
return (string)$label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate image ratio
|
||||
*
|
||||
* @param $width
|
||||
* @param $height
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
private function getRatio(int $width, int $height): float
|
||||
{
|
||||
if ($width && $height) {
|
||||
return $height / $width;
|
||||
}
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve image class for HTML element
|
||||
*
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getClass(array $attributes): string
|
||||
{
|
||||
return $attributes['class'] ?? 'product-image-photo';
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve image custom attributes for HTML element
|
||||
*
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return string|array
|
||||
*/
|
||||
private function getStringCustomAttributes(array $attributes)
|
||||
{
|
||||
if (!$this->compareVersion()) {
|
||||
return $attributes;
|
||||
}
|
||||
$result = [];
|
||||
foreach ($attributes as $name => $value) {
|
||||
$result[] = $name . '="' . $value . '"';
|
||||
}
|
||||
return !empty($result) ? implode(' ', $result) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $version
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function compareVersion($version = '2.4.0')
|
||||
{
|
||||
return version_compare($this->getMagentoVersion(), $version, '<');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
private function getMagentoVersion()
|
||||
{
|
||||
if (!$this->_magentoVersion) {
|
||||
$this->_magentoVersion = ObjectManager::getInstance()->get(ProductMetadataInterface::class)->getVersion();
|
||||
}
|
||||
|
||||
return $this->_magentoVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return CatalogBlockProductImage
|
||||
*/
|
||||
private function _createTemplate($data = [])
|
||||
{
|
||||
return ObjectManager::getInstance()->create(CatalogBlockProductImage::class, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $imageId
|
||||
* @param array|int $size
|
||||
* @param array $properties
|
||||
*
|
||||
* @return Image
|
||||
*/
|
||||
public function resizeImage(Product $product, $imageId, $size, $properties = [])
|
||||
{
|
||||
$size = $this->prepareSize($size);
|
||||
$image = $this->_getImage($product, $imageId, $properties);
|
||||
if ($this->checkDependencies()) {
|
||||
$image->resize($size[0], $size[1]);
|
||||
}
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|int $size
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function prepareSize($size)
|
||||
{
|
||||
if (is_array($size) && 1 >= count($size)) {
|
||||
$size = array_shift($size);
|
||||
}
|
||||
if (!is_array($size)) {
|
||||
$size = [$size, $size];
|
||||
}
|
||||
$size = array_map('floatval', $size);
|
||||
$size = array_map('abs', $size);
|
||||
return $size;
|
||||
}
|
||||
|
||||
public function getResizedImagesProduct(
|
||||
Product $product,
|
||||
$size,
|
||||
$template = self::THUMB_TEMPLATE,
|
||||
array $attributes = [],
|
||||
$properties = []
|
||||
) {
|
||||
ObjectManager::getInstance()->get(ReadHandler::class)->execute($product);
|
||||
|
||||
$images = $product->getMediaGalleryImages();
|
||||
|
||||
$_images = [];
|
||||
$imageHelper = ObjectManager::getInstance()->get(\Olegnax\Athlete2\Helper\Image::class);
|
||||
$media_path = ObjectManager::getInstance()->get(Filesystem::class)->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('');
|
||||
|
||||
foreach ($images as $image) {
|
||||
$id = $image->getId();
|
||||
$path = str_replace($media_path, '', $image->getPath());
|
||||
$image = $imageHelper->init($path, $properties);
|
||||
$image->adaptiveResize($size);
|
||||
|
||||
$image_width = $image->getWidth();
|
||||
$image_height = $image->getHeight();
|
||||
$image = $image->getUrl();
|
||||
|
||||
$_images[] = [
|
||||
'image_id' => $id,
|
||||
'image_url' => $image,
|
||||
'label' => $this->getLabel($product, ''),
|
||||
'width' => $image_width,
|
||||
'height' => $image_height,
|
||||
'ratio' => $this->getRatio($image_width, $image_height),
|
||||
];
|
||||
}
|
||||
|
||||
$data = [
|
||||
'data' => [
|
||||
'template' => $template,
|
||||
'product_id' => $product->getId(),
|
||||
'product' => $product,
|
||||
'images' => $_images,
|
||||
'width' => isset($_images[0]['width']) ? $_images[0]['width'] : 1,
|
||||
'height' => isset($_images[0]['height']) ? $_images[0]['height'] : 1,
|
||||
'ratio' => isset($_images[0]['ratio']) ? $_images[0]['ratio'] : 1,
|
||||
'class' => $this->getClass($attributes),
|
||||
'custom_attributes' => $this->getStringCustomAttributes($attributes),
|
||||
],
|
||||
];
|
||||
|
||||
return $this->_createTemplate($data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $imageId
|
||||
* @param string $imageId_hover
|
||||
* @param array|int $size
|
||||
* @param string $template
|
||||
* @param array $attributes
|
||||
* @param array $properties
|
||||
*
|
||||
* @return CatalogBlockProductImage|mixed
|
||||
*/
|
||||
public function getResizedImageHover(
|
||||
Product $product,
|
||||
$imageId,
|
||||
$imageId_hover,
|
||||
$size,
|
||||
$template = self::HOVER_TEMPLATE,
|
||||
array $attributes = [],
|
||||
$properties = []
|
||||
) {
|
||||
if (!is_array($template)) {
|
||||
$template = [
|
||||
$template,
|
||||
self::TEMPLATE,
|
||||
];
|
||||
} else {
|
||||
foreach ([self::HOVER_TEMPLATE, self::TEMPLATE] as $key => $value) {
|
||||
if (!isset($template[$key]) || empty($template[$key])) {
|
||||
$template[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$this->hasHoverImage($product, $imageId, $imageId_hover)
|
||||
&& !$this->videoHelper->videoOnHover($product)
|
||||
) {
|
||||
return $this->getResizedImage($product, $imageId, $size, $template[1], $attributes, $properties);
|
||||
}
|
||||
if ($this->videoHelper->videoOnHover($product)) {
|
||||
$imageId_hover = $imageId;
|
||||
}
|
||||
$imageMiscParams = $this->getImageParams($imageId);
|
||||
if (empty($size)) {
|
||||
$size = [$imageMiscParams['image_width'], $imageMiscParams['image_height']];
|
||||
} elseif (is_array($size)) {
|
||||
foreach (['image_width', 'image_height'] as $key => $value) {
|
||||
if (!isset($size[$key]) || empty($size[$key])) {
|
||||
$size[$key] = $imageMiscParams[$value];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$image = $this->resizeImage($product, $imageId, $size, $properties);
|
||||
try {
|
||||
[
|
||||
$imageMiscParams['image_width'],
|
||||
$imageMiscParams['image_height']
|
||||
] = $image->getResizedImageInfo();
|
||||
} catch (Exception $e) {
|
||||
$this->_logger->error("OX Product Image: " . $e->getMessage());
|
||||
$imageMiscParams['image_width'] = $imageMiscParams['image_height'] = 1;
|
||||
}
|
||||
$placeholder = $image->getDefaultPlaceholderUrl();
|
||||
$image = $image->getUrl();
|
||||
$image_hover = $this->resizeImage($product, $imageId_hover, $size, $properties)->getUrl();
|
||||
$image_hoverMiscParams = $this->getImageParams($imageId_hover);
|
||||
if ($image_hover == $placeholder) {
|
||||
return $this->getResizedImage($product, $imageId, $size, $template[1], $attributes, $properties);
|
||||
}
|
||||
if ($image == $placeholder) {
|
||||
return $this->getImage($product, $imageId, $template[1], $attributes, $properties);
|
||||
}
|
||||
|
||||
if (array_key_exists('class', $attributes)) {
|
||||
unset($attributes['class']);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'data' => [
|
||||
'template' => $template[0],
|
||||
'product_id' => $product->getId(),
|
||||
'product' => $product,
|
||||
'image_id' => $imageId,
|
||||
'image_hover_id' => $imageId_hover,
|
||||
'image_url' => $image,
|
||||
'image_hover_url' => $image_hover,
|
||||
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
|
||||
'label_hover' => $this->getLabel($product, $image_hoverMiscParams['image_type']),
|
||||
'width' => $imageMiscParams['image_width'],
|
||||
'height' => $imageMiscParams['image_height'],
|
||||
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
|
||||
'class' => $this->getClass($attributes),
|
||||
'custom_attributes' => $this->getStringCustomAttributes($attributes),
|
||||
],
|
||||
];
|
||||
|
||||
return $this->_createTemplate($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $imageId
|
||||
* @param array|int $size
|
||||
* @param string $template
|
||||
* @param array $attributes
|
||||
* @param array $properties
|
||||
*
|
||||
* @return CatalogBlockProductImage|mixed
|
||||
*/
|
||||
public function getResizedImage(
|
||||
Product $product,
|
||||
$imageId,
|
||||
$size,
|
||||
$template = self::TEMPLATE,
|
||||
array $attributes = [],
|
||||
$properties = []
|
||||
) {
|
||||
$imageMiscParams = $this->getImageParams($imageId);
|
||||
if (empty($size)) {
|
||||
return $this->getImage($product, $imageId, $template, $attributes, $properties);
|
||||
}
|
||||
if (is_array($size)) {
|
||||
foreach (['image_width', 'image_height'] as $key => $value) {
|
||||
if (!isset($size[$key]) || empty($size[$key])) {
|
||||
$size[$key] = $imageMiscParams[$value];
|
||||
}
|
||||
}
|
||||
}
|
||||
$image = $this->resizeImage($product, $imageId, $size, $properties);
|
||||
$imageMiscParams = $this->getImageParams($imageId);
|
||||
try {
|
||||
[
|
||||
$imageMiscParams['image_width'],
|
||||
$imageMiscParams['image_height']
|
||||
] = $image->getResizedImageInfo();
|
||||
} catch (Exception $e) {
|
||||
$this->_logger->error("OX Product Image: " . $e->getMessage());
|
||||
$imageMiscParams['image_width'] = $imageMiscParams['image_height'] = 1;
|
||||
}
|
||||
$placeholder = $image->getDefaultPlaceholderUrl();
|
||||
$image = $image->getUrl();
|
||||
if ($image == $placeholder) {
|
||||
return $this->getImage($product, $imageId, $template, $attributes, $properties);
|
||||
}
|
||||
|
||||
$data = [
|
||||
'data' => [
|
||||
'template' => $template,
|
||||
'product_id' => $product->getId(),
|
||||
'product' => $product,
|
||||
'image_id' => $imageId,
|
||||
'image_url' => $image,
|
||||
'label' => $this->getLabel($product, $imageMiscParams['image_type']),
|
||||
'width' => $imageMiscParams['image_width'],
|
||||
'height' => $imageMiscParams['image_height'],
|
||||
'ratio' => $this->getRatio($imageMiscParams['image_width'], $imageMiscParams['image_height']),
|
||||
'class' => $this->getClass($attributes),
|
||||
'custom_attributes' => $this->getStringCustomAttributes($attributes),
|
||||
],
|
||||
];
|
||||
|
||||
return $this->_createTemplate($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $image
|
||||
* @param array|int $size
|
||||
* @param array $properties
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getUrlResizedImage(Product $product, $image, $size, $properties = [])
|
||||
{
|
||||
$image = $this->resizeImage($product, $image, $size, $properties);
|
||||
return $image->getUrl();
|
||||
}
|
||||
}
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* Athlete2 Theme
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Olegnax.com license that is
|
||||
* available through the world-wide-web at this URL:
|
||||
* https://www.olegnax.com/license
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this extension to newer
|
||||
* version in the future.
|
||||
*
|
||||
* @category Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://www.olegnax.com/)
|
||||
* @license https://www.olegnax.com/license
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Magento\Catalog\Model\Category;
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Catalog\Model\ResourceModel\Category\Collection;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class ProductList extends Helper
|
||||
{
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $storeManager;
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->storeManager = $storeManager;
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return Category|null
|
||||
*/
|
||||
public function getLastCategory($product)
|
||||
{
|
||||
/** @var Collection $categoryCollection */
|
||||
$categoryCollection = $product->getCategoryCollection()
|
||||
->addAttributeToSelect('name')
|
||||
->addAttributeToFilter('is_active', '1');
|
||||
if ($rootCategoryId = $this->getRootCategoryId()) {
|
||||
$categoryCollection->addFieldToFilter('path', ['like' => '1/' . $rootCategoryId . '%']);
|
||||
}
|
||||
$category = null;
|
||||
if ($categoryCollection->getSize()) {
|
||||
$category = null;
|
||||
/** @var Category $_category */
|
||||
foreach ($categoryCollection as $_category) {
|
||||
if (!empty($_category)) {
|
||||
$size_path = count(explode('/', (string)$_category->getPath()));
|
||||
}
|
||||
$_size_path = 0;
|
||||
if (!empty($category)) {
|
||||
$_size_path = count(explode('/', (string)$category->getPath()));
|
||||
}
|
||||
if ($_size_path < $size_path) {
|
||||
$category = $_category;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
protected function getRootCategoryId()
|
||||
{
|
||||
return $this->storeManager->getGroup()->getRootCategoryId();
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
|
||||
class UpdaterTemplate extends Helper
|
||||
{
|
||||
const XML_PATH_HEADER_HEDER = 'header/header_layout';
|
||||
const XML_PATH_MOBILE_STICKY = 'header/sticky_header_mobile';
|
||||
const DEFAULT_HEADER_HEDER = '1';
|
||||
const TPL_PATH_HEADER = "html/header/%s.phtml";
|
||||
|
||||
const XML_PATH_LISTING_PRODUCTS = 'products_listing/products_layout';
|
||||
const TPL_PATH_PRODUCT_LIST = "Magento_Catalog::product/list/%s.phtml";
|
||||
const TPL_PATH_PRODUCT_LIST_ITEM = "Magento_Catalog::product/list/items/%s.phtml";
|
||||
|
||||
public function setHeaderLayout()
|
||||
{
|
||||
return sprintf(
|
||||
static::TPL_PATH_HEADER,
|
||||
$this->getModuleConfig(static::XML_PATH_HEADER_HEDER)
|
||||
);
|
||||
}
|
||||
public function setMobileSticky()
|
||||
{
|
||||
return $this->getModuleConfig(static::XML_PATH_MOBILE_STICKY) ? 'no' : 'yes';
|
||||
}
|
||||
public function setSearchProductsLayout()
|
||||
{
|
||||
return $this->setProductsLayout();
|
||||
}
|
||||
|
||||
public function setProductsLayout()
|
||||
{
|
||||
return sprintf(
|
||||
static::TPL_PATH_PRODUCT_LIST,
|
||||
$this->getModuleConfig(static::XML_PATH_LISTING_PRODUCTS) ?: static::DEFAULT_HEADER_HEDER
|
||||
);
|
||||
}
|
||||
|
||||
public function setUpSellProductsLayout()
|
||||
{
|
||||
return $this->setRelatedProductsLayout();
|
||||
}
|
||||
|
||||
public function setRelatedProductsLayout()
|
||||
{
|
||||
return sprintf(
|
||||
static::TPL_PATH_PRODUCT_LIST_ITEM,
|
||||
$this->getModuleConfig(static::XML_PATH_LISTING_PRODUCTS) ?: '1'
|
||||
);
|
||||
}
|
||||
|
||||
public function setCrossSellProductsLayout()
|
||||
{
|
||||
return $this->setRelatedProductsLayout();
|
||||
}
|
||||
|
||||
public function setGalleryTemplate()
|
||||
{
|
||||
$fullActionName = ObjectManager::getInstance()->get(RequestInterface::class)->getFullActionName();
|
||||
|
||||
$value = 'fast';
|
||||
if ($fullActionName == 'catalog_product_view'){
|
||||
$value = $this->getModuleConfig('product/gallery_layout') ?: 'fast';
|
||||
if ($value == '1col' || $value == '2cols') {
|
||||
$value = 'noslider';
|
||||
}
|
||||
}
|
||||
return sprintf('Magento_Catalog::product/view/gallery-%s.phtml', $value );
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+344
@@ -0,0 +1,344 @@
|
||||
<?php
|
||||
/**
|
||||
* @author Olegnax
|
||||
* @package Olegnax_Athlete2
|
||||
* @copyright Copyright (c) 2023 Olegnax (http://olegnax.com/). All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
namespace Olegnax\Athlete2\Helper;
|
||||
|
||||
use Magento\Catalog\Model\Product;
|
||||
use Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\Escaper;
|
||||
use Magento\Framework\Exception\NoSuchEntityException;
|
||||
use Magento\Framework\Filesystem;
|
||||
use Magento\Framework\Filesystem\Directory\ReadInterface;
|
||||
use Magento\Framework\Filesystem\Glob;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\Template;
|
||||
use Magento\Framework\View\LayoutInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class Video extends AbstractHelper
|
||||
{
|
||||
const ORDER_MIME = [
|
||||
'video/webm',
|
||||
'video/mp4',
|
||||
'video/ogg',
|
||||
];
|
||||
|
||||
const TEMPLATE_VIDEO = 'Olegnax_Athlete2::video.phtml';
|
||||
/**
|
||||
* @var ReadInterface
|
||||
*/
|
||||
protected $_mediaDirectory;
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
protected $_storeManager;
|
||||
|
||||
/**
|
||||
* @var LayoutInterface
|
||||
*/
|
||||
protected $_layout;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $_productVideos;
|
||||
|
||||
/**
|
||||
* Video constructor.
|
||||
* @param Context $context
|
||||
* @param LayoutInterface $layout
|
||||
* @param Filesystem $filesystem
|
||||
* @param StoreManagerInterface $storeManager
|
||||
*/
|
||||
public function __construct(
|
||||
Context $context,
|
||||
LayoutInterface $layout,
|
||||
Filesystem $filesystem,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->_layout = $layout;
|
||||
$this->_mediaDirectory = $filesystem->getDirectoryRead(DirectoryList::MEDIA);
|
||||
$this->_storeManager = $storeManager;
|
||||
$this->_productVideos = [];
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return string
|
||||
*/
|
||||
public static function prepareAttributes(array $attributes)
|
||||
{
|
||||
$escaper = ObjectManager::getInstance()->get(Escaper::class);
|
||||
$attributes = array_filter($attributes);
|
||||
if (empty($attributes)) {
|
||||
return '';
|
||||
}
|
||||
$html = '';
|
||||
foreach ($attributes as $attributeName => $attributeValue) {
|
||||
if (is_bool($attributeValue)) {
|
||||
if ($attributeValue) {
|
||||
$html .= sprintf(
|
||||
' %s',
|
||||
$attributeName
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$html .= sprintf(
|
||||
' %s="%s"',
|
||||
$attributeName,
|
||||
$escaper->escapeHtmlAttr($attributeValue)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function issetVideo($product)
|
||||
{
|
||||
$files = $this->_getVideo($product);
|
||||
|
||||
return !empty($files);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return string[]
|
||||
*/
|
||||
private function _getVideo($product)
|
||||
{
|
||||
if (!$product) {
|
||||
return [];
|
||||
}
|
||||
$productId = $product->getId();
|
||||
if (!array_key_exists($productId, $this->_productVideos)) {
|
||||
$this->_productVideos[$productId] = [];
|
||||
}
|
||||
if (empty($this->_productVideos[$productId])) {
|
||||
$videoPath = (string)$this->getProductData($product, 'ox_gallery_video');
|
||||
$videoPath = ltrim($videoPath, '\\\/');
|
||||
if (!empty($videoPath)) {
|
||||
$videoAbsolutePath = $this->getAbsolutePath($videoPath);
|
||||
$videoReplaceAbsolutePath = preg_replace('#\.[a-z0-9]{3,}$#i', '', $videoAbsolutePath);
|
||||
$this->_productVideos[$productId] = Glob::glob($videoReplaceAbsolutePath . '\.*');
|
||||
if (empty($this->_productVideos[$productId])) {
|
||||
$this->_productVideos[$productId] = [];
|
||||
if (file_exists($videoAbsolutePath)) {
|
||||
$this->_productVideos[$productId][] = $videoAbsolutePath;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_productVideos[$productId];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param string $key
|
||||
* @return mixed|null
|
||||
*/
|
||||
private function getProductData($product, $key)
|
||||
{
|
||||
$productId = $product->getId();
|
||||
$data = $product->getData($key);
|
||||
if (null === $data) {
|
||||
$product = $product->load($productId);
|
||||
if ($product->getId() == $productId) {
|
||||
$data = $product->getData($key);
|
||||
}
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function getAbsolutePath(
|
||||
$path = ''
|
||||
) {
|
||||
return $this->_mediaDirectory->getAbsolutePath($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function stopOnClick($product)
|
||||
{
|
||||
return (bool)$this->getProductData($product, 'ox_gallery_video_stop_on_click');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function loopVideo($product)
|
||||
{
|
||||
return (bool)$this->getProductData($product, 'ox_gallery_video_loop');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function videoPosition($product)
|
||||
{
|
||||
return (int)$this->getProductData($product, 'ox_gallery_video_index') ?: 2;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function videoOnHover($product)
|
||||
{
|
||||
return (bool)$this->getProductData($product, 'ox_gallery_video_listing_hover');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function autoplayVideo($product)
|
||||
{
|
||||
return (bool)$this->getProductData($product, 'ox_gallery_video_autoplay');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @return bool
|
||||
*/
|
||||
public function showControls($product)
|
||||
{
|
||||
return (bool)$this->getProductData($product, 'ox_gallery_video_controls');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Product $product
|
||||
* @param array $config
|
||||
* @return string
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
public function getVideo($product, $config = [])
|
||||
{
|
||||
$files = $this->_getVideo($product);
|
||||
|
||||
if (!empty($files)) {
|
||||
$mimeFiles = [];
|
||||
foreach ($files as $file) {
|
||||
$fileMime = $this->_detectMimeType($file);
|
||||
$mimeFiles[$fileMime] = $this->getUrlPath($file);
|
||||
}
|
||||
if (!empty($mimeFiles)) {
|
||||
uksort($mimeFiles, [$this, 'sortByMime']);
|
||||
|
||||
return $this->getLayout()
|
||||
->createBlock(
|
||||
Template::class,
|
||||
'',
|
||||
[
|
||||
'data' => array_replace(
|
||||
[
|
||||
'mime_files' => $mimeFiles,
|
||||
'product' => $product,
|
||||
],
|
||||
$config
|
||||
),
|
||||
]
|
||||
)
|
||||
->setTemplate(static::TEMPLATE_VIDEO)
|
||||
->toHtml();
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to detect the mime type of a file
|
||||
*
|
||||
* @param string $file File
|
||||
* @return string Mimetype of given file
|
||||
*/
|
||||
protected function _detectMimeType($file)
|
||||
{
|
||||
$result = '';
|
||||
if (class_exists('finfo', false)) {
|
||||
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
|
||||
$mime = @finfo_open($const);
|
||||
|
||||
if (!empty($mime)) {
|
||||
$result = finfo_file($mime, $file);
|
||||
}
|
||||
|
||||
unset($mime);
|
||||
}
|
||||
|
||||
if (empty($result) && (function_exists('mime_content_type')
|
||||
&& ini_get('mime_magic.magicfile'))) {
|
||||
$result = mime_content_type($file);
|
||||
}
|
||||
|
||||
if (empty($result)) {
|
||||
$result = 'application/octet-stream';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @return string
|
||||
* @throws NoSuchEntityException
|
||||
*/
|
||||
protected function getUrlPath(
|
||||
$path = ''
|
||||
) {
|
||||
$path = str_replace($this->getAbsolutePath(), '', $path);
|
||||
$path = $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA) . $path;
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LayoutInterface
|
||||
*/
|
||||
public function getLayout()
|
||||
{
|
||||
return $this->_layout;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $itemPrev
|
||||
* @param string $itemNext
|
||||
* @return int
|
||||
*/
|
||||
public function sortByMime(
|
||||
$itemPrev,
|
||||
$itemNext
|
||||
) {
|
||||
$indexPrev = in_array($itemPrev, static::ORDER_MIME) ? array_search($itemPrev, static::ORDER_MIME) : 9999;
|
||||
$indexNext = in_array($itemNext, static::ORDER_MIME) ? array_search($itemNext, static::ORDER_MIME) : 9999;
|
||||
if ($indexPrev == $indexNext) {
|
||||
return 0;
|
||||
} elseif ($indexPrev > $indexNext) {
|
||||
return 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user