ltsecurityinc安防产品在线商城代码
This commit is contained in:
Executable
+160
@@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
use \Magento\Framework\UrlInterface;
|
||||
|
||||
class Curl extends \Magento\Framework\App\Helper\AbstractHelper {
|
||||
|
||||
protected $_userAgent;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\App\ProductMetadataInterface $productMetadata,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
) {
|
||||
$version = $productMetadata->getVersion();
|
||||
$url = $storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB);
|
||||
$this->_userAgent = "Magento/$version; $url";
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if Curl available
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function test() {
|
||||
$test = function_exists('curl_version');
|
||||
return $test;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get user agent
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getUserAgent() {
|
||||
return $this->_userAgent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do request
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $args
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function request($url, $args = array()) {
|
||||
|
||||
$defaults = array(
|
||||
'headers' => array(),
|
||||
'cookies' => array(),
|
||||
'httpversion' => CURL_HTTP_VERSION_NONE,
|
||||
'timeout' => 30,
|
||||
'method' => 'POST',
|
||||
'body' => array()
|
||||
);
|
||||
$args = array_merge($defaults, $args);
|
||||
|
||||
$headers = array();
|
||||
foreach ($args['headers'] as $key => $value) {
|
||||
$headers[] = "$key: $value";
|
||||
}
|
||||
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
if ($args['method'] == 'POST' && $args['body'] && is_array($args['body'])) {
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args['body']));
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_USERAGENT, $this->_userAgent);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_COOKIE, implode('; ', $args['cookies']));
|
||||
curl_setopt($ch, CURLOPT_FAILONERROR, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, $args['timeout']);
|
||||
curl_setopt($ch, CURLOPT_HTTP_VERSION, $args['httpversion'] == '1.0' ? CURL_HTTP_VERSION_1_0 : ($args['httpversion'] == '1.1' ? CURL_HTTP_VERSION_1_1 : CURL_HTTP_VERSION_NONE));
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($ch, CURLOPT_ENCODING, '');
|
||||
|
||||
$output = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$error = curl_errno($ch);
|
||||
$message = curl_error($ch);
|
||||
|
||||
curl_close ($ch);
|
||||
|
||||
$result = array(
|
||||
'response' => array('code' => $code, 'message' => $message),
|
||||
'body' => $output
|
||||
);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get content from remote url
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $args
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function requestGet($url, $args = array()) {
|
||||
$args['method'] = 'GET';
|
||||
return $this->request($url, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open content from remote url
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function remoteFileOpen($url) {
|
||||
$args = array(
|
||||
'method' => 'GET',
|
||||
'timeout' => 5,
|
||||
'redirection' => 5,
|
||||
'httpversion' => '1.0',
|
||||
'blocking' => true,
|
||||
'body' => null
|
||||
);
|
||||
$response = $this->request($url, $args);
|
||||
return $response['response']['code'] == 200 ? $response['body'] : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get response code from response
|
||||
*
|
||||
* @param array $response
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getCode($response) {
|
||||
return $response['response']['code'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get body from response
|
||||
*
|
||||
* @param array $response
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getBody($response) {
|
||||
return $response['body'];
|
||||
}
|
||||
|
||||
}
|
||||
Executable
+133
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
use Magento\Store\Model\System\Store;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class Data extends AbstractHelper {
|
||||
|
||||
const REVSLIDER_PRODUCT = 'revslider_magento2';
|
||||
const ASSETS_ROUTE = 'nwdthemes/revslider/public/assets/';
|
||||
|
||||
protected static $logger;
|
||||
protected $_systemStore;
|
||||
|
||||
public static $_GET = array();
|
||||
public static $_REQUEST = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
Context $context,
|
||||
Store $systemStore
|
||||
) {
|
||||
$this->_systemStore = $systemStore;
|
||||
|
||||
parent::__construct($context);
|
||||
|
||||
self::$logger = $context->getLogger();
|
||||
|
||||
$requestParams = $context->getRequest()->getParams();
|
||||
self::$_GET = array_merge(self::$_GET, $requestParams);
|
||||
self::$_REQUEST = array_merge(self::$_REQUEST, $requestParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set page for get imitation
|
||||
*
|
||||
* @param string $page
|
||||
*/
|
||||
|
||||
public static function setPage($page) {
|
||||
self::$_GET['page'] = $page;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set page for get imitation
|
||||
*
|
||||
* @param string $view
|
||||
*/
|
||||
|
||||
public static function setView($view) {
|
||||
self::$_GET['view'] = $view;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get store options for multiselect
|
||||
*
|
||||
* @return array Array of store options
|
||||
*/
|
||||
|
||||
public function getStoreOptions() {
|
||||
$storeValues = $this->_systemStore->getStoreValuesForForm(false, true);
|
||||
$storeValues = $this->_makeFlatStoreOptions($storeValues);
|
||||
return $storeValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Make flat store options
|
||||
*
|
||||
* @param array $storeValues Store values tree array
|
||||
* @retrun array Flat store values array
|
||||
*/
|
||||
|
||||
private function _makeFlatStoreOptions($storeValues) {
|
||||
$arrStoreValues = array();
|
||||
foreach ($storeValues as $_storeValue) {
|
||||
if ( ! is_array($_storeValue['value']) ) {
|
||||
$arrStoreValues[] = $_storeValue;
|
||||
} else {
|
||||
$arrStoreValues[] = array(
|
||||
'label' => $_storeValue['label'],
|
||||
'value' => 'option_disabled'
|
||||
);
|
||||
$_arrSubStoreValues = $this->_makeFlatStoreOptions($_storeValue['value']);
|
||||
foreach ($_arrSubStoreValues as $_subStoreValue) {
|
||||
$arrStoreValues[] = $_subStoreValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $arrStoreValues;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log variable
|
||||
*
|
||||
* @param mixed $var
|
||||
*/
|
||||
public static function log($var) {
|
||||
$log = [];
|
||||
foreach (func_get_args() as $arg)
|
||||
$log[] = is_string($arg)
|
||||
? $arg
|
||||
: (is_bool($arg)
|
||||
? var_export($arg, true)
|
||||
: (is_object($arg)
|
||||
? 'class ' . get_class($arg)
|
||||
: print_r($arg, true)));
|
||||
$logMessage = '[NWD::Revslider] ' . implode(', ', $log);
|
||||
if (! self::$logger) {
|
||||
self::$logger = ObjectManager::getInstance()->get(LoggerInterface::class);
|
||||
}
|
||||
self::$logger->info($logMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log exception details to nwd_revslider.log
|
||||
*
|
||||
* @param \Exception $e
|
||||
*/
|
||||
public static function logException($e) {
|
||||
$trace = [];
|
||||
foreach ($e->getTrace() as $data) if (isset($data['file']))
|
||||
$trace[] = $data['file'].':'.$data['line'];
|
||||
self::log('Revolution Slider Exception: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine(), $trace);
|
||||
}
|
||||
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
use Nwdthemes\Revslider\Model\Revslider\Framework\PclZip;
|
||||
use Magento\Framework\App\Helper\AbstractHelper;
|
||||
use Magento\Framework\App\Helper\Context;
|
||||
|
||||
class Filesystem extends AbstractHelper {
|
||||
|
||||
protected $_context;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
Context $context
|
||||
) {
|
||||
$this->_context = $context;
|
||||
|
||||
parent::__construct($this->_context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Init filesystem class
|
||||
*/
|
||||
|
||||
public function WP_Filesystem() {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unzip file
|
||||
*
|
||||
* @param string Zip file
|
||||
* @param string Destination path
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function unzip_file($file, $path) {
|
||||
|
||||
// make sure it have trailing slash
|
||||
$path = rtrim($path, '/\\') . DIRECTORY_SEPARATOR;
|
||||
|
||||
if ( ! $this->wp_mkdir_p($path)) return false;
|
||||
|
||||
if (class_exists('\ZipArchive', false)) {
|
||||
$zip = new \ZipArchive;
|
||||
$zipResult = $zip->open($file, \ZipArchive::CREATE);
|
||||
if ($zipResult === true) {
|
||||
for($i = 0; $i < $zip->numFiles; $i++) {
|
||||
$fileName = $zip->getNameIndex($i);
|
||||
$fileInfo = pathinfo($fileName);
|
||||
if (strpos($fileName, '_') !== 0 && strpos($fileName, '.') !== 0 && strpos($fileInfo['basename'], '_') !== 0 && strpos($fileInfo['basename'], '.') !== 0) {
|
||||
if ($fileInfo['dirname'] !== '.' && ! file_exists($path.$fileInfo['dirname'])) {
|
||||
$parts = explode('/', $fileInfo['dirname']);
|
||||
$dirPath = $path;
|
||||
foreach ($parts as $part) {
|
||||
$dirPath .= $part . DIRECTORY_SEPARATOR;
|
||||
$this->wp_mkdir_p($dirPath);
|
||||
}
|
||||
}
|
||||
if (substr($fileName, -1) !== '/' && substr($fileName, -1) !== '\\') {
|
||||
copy("zip://".$file."#".$fileName, $path.str_replace('//', DIRECTORY_SEPARATOR, $fileName));
|
||||
}
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
}
|
||||
} else {
|
||||
$pclZip = new PclZip($file);
|
||||
$zipResult = $pclZip->extract($path) ? true : false;
|
||||
}
|
||||
|
||||
return $zipResult;
|
||||
}
|
||||
|
||||
public function recurse_move($src, $dst) {
|
||||
$src = rtrim($src,'/\\');
|
||||
$dst = rtrim($dst,'/\\');
|
||||
$dir = opendir($src);
|
||||
$this->wp_mkdir_p($dst);
|
||||
while(false !== ( $file = readdir($dir)) ) {
|
||||
if (( $file != '.' ) && ( $file != '..' )) {
|
||||
if ( is_dir($src . '/' . $file) ) {
|
||||
$this->recurse_move($src . '/' . $file,$dst . '/' . $file);
|
||||
} else {
|
||||
$this->rename($src . '/' . $file,$dst . '/' . $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($dir);
|
||||
rmdir($src);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make writable directory
|
||||
*
|
||||
* @param string $path
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function wp_mkdir_p($dir) {
|
||||
if (file_exists($dir) && is_dir($dir)) {
|
||||
return true;
|
||||
} else {
|
||||
return @mkdir($dir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if file exists
|
||||
*
|
||||
* @param string Path to file
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function exists($path) {
|
||||
return file_exists($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read file
|
||||
*
|
||||
* @param string Path to file
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function get_contents($path) {
|
||||
return file_get_contents($path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete file
|
||||
*
|
||||
* @param string Path to file
|
||||
* @param boolean Is recursive
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function delete($path, $recursive = false) {
|
||||
if (is_dir($path)) {
|
||||
$dir = opendir($path);
|
||||
while(false !== ( $file = readdir($dir)) ) {
|
||||
if (( $file != '.' ) && ( $file != '..' )) {
|
||||
if ( is_dir($path . '/' . $file) ) {
|
||||
if ($recursive) {
|
||||
$this->delete($path . '/' . $file, $recursive);
|
||||
}
|
||||
} else {
|
||||
unlink($path . '/' . $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir($dir);
|
||||
rmdir($path);
|
||||
} elseif (is_file($path)) {
|
||||
unlink($path);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+2439
File diff suppressed because it is too large
Load Diff
+577
@@ -0,0 +1,577 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
use \Magento\Framework\App\Filesystem\DirectoryList;
|
||||
use \Magento\Framework\UrlInterface;
|
||||
|
||||
class Images extends \Magento\Framework\App\Helper\AbstractHelper {
|
||||
|
||||
const IMAGE_DIR = 'revslider';
|
||||
const IMAGE_DIR_EXCLUDES = '/plugins,/templates,/thumbs,/rstemp';
|
||||
const IMAGE_THUMB_DIR = 'revslider/thumbs';
|
||||
const RS_IMAGE_PATH = 'revslider';
|
||||
const MEDIA_URL_PACEHOLDER = '{revslider_base_url}';
|
||||
const ALLOWED_MEDIA_EXTENSIONS = ['jpg', 'jpeg', 'jpe', 'gif', 'png', 'bmp', 'mpeg', 'mpg', 'mpe', 'mp4', 'm4v', 'ogv', 'webm'];
|
||||
|
||||
protected $_directory;
|
||||
protected $_imageFactory;
|
||||
protected $_storeManager;
|
||||
protected $_imageBuilder;
|
||||
protected $_catalogImageHelper;
|
||||
protected $_catalogProductHelper;
|
||||
protected $_productFactory;
|
||||
protected $_productMediaConfig;
|
||||
protected $_galleryImagesHelper;
|
||||
|
||||
public static $imageSizes = array(
|
||||
'gallery' => array('width' => 195, 'height' => 130),
|
||||
'thumbnail' => array('width' => 150, 'height' => 150),
|
||||
'medium' => array('width' => 300, 'height' => 200),
|
||||
'large' => array('width' => 1024, 'height' => 682),
|
||||
'post-thumbnail' => array('width' => 825, 'height' => 510)
|
||||
);
|
||||
|
||||
public static $downsizePercents = [25, 50, 75];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\Filesystem $filesystem,
|
||||
\Magento\Framework\Image\Factory $imageFactory,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,
|
||||
\Magento\Catalog\Helper\Image $catalogImageHelper,
|
||||
\Magento\Catalog\Helper\Product $catalogProductHelper,
|
||||
\Magento\Catalog\Model\ProductFactory $productFactory,
|
||||
\Magento\Catalog\Model\Product\Media\Config $productMediaConfig,
|
||||
\Magento\Cms\Helper\Wysiwyg\Images $galleryImagesHelper
|
||||
) {
|
||||
$this->_imageFactory = $imageFactory;
|
||||
$this->_storeManager = $storeManager;
|
||||
$this->_imageBuilder = $imageBuilder;
|
||||
$this->_catalogImageHelper = $catalogImageHelper;
|
||||
$this->_catalogProductHelper = $catalogProductHelper;
|
||||
$this->_productFactory = $productFactory;
|
||||
$this->_productMediaConfig = $productMediaConfig;
|
||||
$this->_galleryImagesHelper = $galleryImagesHelper;
|
||||
|
||||
parent::__construct($context);
|
||||
|
||||
$this->_directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
$this->_directory->create(self::IMAGE_DIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get images directory
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getImageDir() {
|
||||
return self::IMAGE_DIR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image thumbs directory
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getImageThumbDir() {
|
||||
return self::IMAGE_THUMB_DIR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize image
|
||||
*
|
||||
* @param string $fileName
|
||||
* @param int $width
|
||||
* @param int $height
|
||||
* @return string Resized image url
|
||||
*/
|
||||
|
||||
public function resizeImg($fileName, $width, $height = '', $targetPath = false) {
|
||||
|
||||
$fileName = $this->imageClean($fileName);
|
||||
if (strpos($fileName, '//') !== false && strpos($fileName, $this->imageBaseUrl()) === false) {
|
||||
return $fileName;
|
||||
}
|
||||
|
||||
if ( ! $height) {
|
||||
$height = $width;
|
||||
}
|
||||
|
||||
$thumbDir = self::IMAGE_THUMB_DIR;
|
||||
$resizeDir = $thumbDir . "/resized_{$width}x{$height}";
|
||||
|
||||
$this->_directory->create($resizeDir);
|
||||
|
||||
$baseURL = str_replace(array('https://', 'http://'), '//', $this->getBaseUrl());
|
||||
$fileName = str_replace(array('https://', 'http://'), '//', $fileName);
|
||||
$fileName = str_replace($baseURL, '', $fileName);
|
||||
|
||||
$imageFile = str_replace('revslider/', '', $fileName);
|
||||
|
||||
$basePath = $this->getBaseDir() . DIRECTORY_SEPARATOR . $fileName;
|
||||
|
||||
if ($targetPath) {
|
||||
$newPath = $targetPath;
|
||||
} else {
|
||||
$newPath = $this->getBaseDir() . DIRECTORY_SEPARATOR . $resizeDir . DIRECTORY_SEPARATOR . $imageFile;
|
||||
}
|
||||
|
||||
if ($width != '') {
|
||||
if (file_exists($basePath) && is_file($basePath) && ! file_exists($newPath)) {
|
||||
$imageObj = $this->_imageFactory->create($basePath);
|
||||
$imageObj->constrainOnly(TRUE);
|
||||
$imageObj->keepAspectRatio(TRUE);
|
||||
$imageObj->keepFrame(FALSE);
|
||||
$imageObj->keepTransparency(TRUE);
|
||||
//$imageObj->backgroundColor(array(255,255,255));
|
||||
$imageObj->resize($width, $height);
|
||||
$res = $imageObj->save($newPath);
|
||||
}
|
||||
$resizedURL = $this->getBaseUrl() . $resizeDir . '/' . $imageFile;
|
||||
} else {
|
||||
$resizedURL = $this->getBaseUrl() . $fileName;
|
||||
}
|
||||
return $resizedURL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image id by url
|
||||
*
|
||||
* @param string $url
|
||||
* @return int
|
||||
*/
|
||||
|
||||
public function attachment_url_to_postid($url) {
|
||||
return $this->get_image_id_by_url($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image id by url
|
||||
*
|
||||
* @param string $url
|
||||
* @return int
|
||||
*/
|
||||
|
||||
public function get_image_id_by_url($url) {
|
||||
$id = false;
|
||||
$imagePath = $this->imageFile($url);
|
||||
if ($imagePath && file_exists($this->imageBaseDir() . DIRECTORY_SEPARATOR . $imagePath)) {
|
||||
$imagePath = $this->_getOriginalImage($imagePath);
|
||||
$id = $this->_galleryImagesHelper->idEncode($imagePath);
|
||||
}
|
||||
return $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image url by id and size
|
||||
*
|
||||
* @param int Image id
|
||||
* @param string Size type
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function wp_get_attachment_image_src($attachment_id, $size='thumbnail') {
|
||||
return $this->image_downsize($attachment_id, $size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attached file
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function get_attached_file($attachment_id) {
|
||||
if ($attachment_id && strlen($attachment_id) > 11) {
|
||||
$imageFile = $this->imageFile($this->_galleryImagesHelper->idDecode($attachment_id));
|
||||
$imageFile = $this->_getOriginalImage($imageFile);
|
||||
$image = $this->imageBaseDir() . DIRECTORY_SEPARATOR . $imageFile;
|
||||
if (file_exists($image)) {
|
||||
return $image;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image info
|
||||
*
|
||||
* @param int $imageId
|
||||
* @return array|boolean
|
||||
*/
|
||||
public function wp_get_attachment_metadata($imageId) {
|
||||
$data = false;
|
||||
if ($image = $this->get_attached_file($imageId)) {
|
||||
// TODO get original image file if already resized one provided
|
||||
$size = getimagesize($image);
|
||||
$width = $size[0];
|
||||
$height = $size[1];
|
||||
$sizes = [];
|
||||
foreach (self::$imageSizes as $imageSizeName => $imageSize) {
|
||||
$sizes[$imageSizeName] = [
|
||||
'file' => $this->imageFile($image),
|
||||
'width' => $imageSize['width'],
|
||||
'height' => $imageSize['height']
|
||||
];
|
||||
}
|
||||
foreach (self::$downsizePercents as $downsizePercent) {
|
||||
$customWidth = round($width * $downsizePercent / 100);
|
||||
$customHeight = round($height * $downsizePercent / 100);
|
||||
$sizes['custom_' . $customWidth . 'x' . $customHeight] = [
|
||||
'file' => $this->imageFile($image),
|
||||
'width' => $customWidth,
|
||||
'height' => $customHeight
|
||||
];
|
||||
}
|
||||
$data = [
|
||||
'width' => $width,
|
||||
'height' => $height,
|
||||
'file' => $this->imageFile($image),
|
||||
'sizes' => $sizes,
|
||||
'image_meta' => []
|
||||
];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize image by id and preset size
|
||||
*
|
||||
* @param int Image id
|
||||
* @param string Size type
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function image_downsize($id, $size = 'medium') {
|
||||
|
||||
$downsizedImage = false;
|
||||
|
||||
if ((string)(int)$id === (string)$id && $product = $this->_productFactory->create()->load($id)) {
|
||||
|
||||
switch ($size) {
|
||||
case 'thumbnail' :
|
||||
$image = $this->_catalogImageHelper
|
||||
->init($product, 'product_thumbnail_image')
|
||||
->setImageFile($product->getFile());
|
||||
$downsizedImage = [
|
||||
$this->imageClean($image->getUrl()),
|
||||
$image->getWidth(),
|
||||
$image->getHeight()
|
||||
];
|
||||
break;
|
||||
case 'small' :
|
||||
$imageUrl = $this->_catalogProductHelper->getSmallImageUrl($product);
|
||||
$downsizedImage = [
|
||||
$imageUrl,
|
||||
250,
|
||||
250
|
||||
];
|
||||
break;
|
||||
case 'medium' :
|
||||
case 'large' :
|
||||
case 'base' :
|
||||
case 'full' :
|
||||
default :
|
||||
$imageUrl = $this->_catalogProductHelper->getImageUrl($product);
|
||||
$downsizedImage = [
|
||||
$imageUrl,
|
||||
1000,
|
||||
1000
|
||||
];
|
||||
break;
|
||||
}
|
||||
|
||||
} elseif ($id) {
|
||||
|
||||
if ($image = $this->get_attached_file($id)) {
|
||||
|
||||
$image = $this->imageFile($image);
|
||||
|
||||
switch ($size) {
|
||||
case 'base' :
|
||||
case 'full' :
|
||||
|
||||
if ($imageSize = getimagesize($this->imagePath($image))) {
|
||||
$width = $imageSize[0];
|
||||
$height = $imageSize[1];
|
||||
$imageUrl = $this->imageUrl($image);
|
||||
$downsizedImage = array($imageUrl, $width, $height);
|
||||
}
|
||||
|
||||
break;
|
||||
default :
|
||||
|
||||
if (strpos($size, 'custom_') === 0) {
|
||||
$customSize = explode('x', str_replace('custom_', '', $size));
|
||||
$width = $customSize[0];
|
||||
$height = $customSize[1];
|
||||
} else {
|
||||
$targetSize = isset(self::$imageSizes[$size]) ? self::$imageSizes[$size] : reset(self::$imageSizes);
|
||||
$width = $targetSize['width'];
|
||||
$height = $targetSize['height'];
|
||||
}
|
||||
|
||||
$imageUrl = $this->image_resize($this->imageUrl($image), $width, $height);
|
||||
$downsizedImage = array($imageUrl, $width, $height);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $downsizedImage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize image
|
||||
*
|
||||
* @param string Image url
|
||||
* @param int Width
|
||||
* @param int Height
|
||||
* @param boolean Is crop
|
||||
* @param boolean Is single
|
||||
* @param boolean Is upscale
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function image_resize($url, $width = null, $height = null, $crop = null, $single = true, $upscale = false) {
|
||||
return $this->resizeImg($url, $width, $height);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize image to location
|
||||
*
|
||||
* @param string Image url
|
||||
* @param int Width
|
||||
* @param int Height
|
||||
* @param string Target path
|
||||
* @return string
|
||||
*/
|
||||
public function image_resize_to($url, $width = null, $height = null, $targetPath = false) {
|
||||
return $this->resizeImg($this->image_to_url($url), $width, $height, $targetPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for Resize Image
|
||||
*/
|
||||
|
||||
public function rev_aq_resize($url, $width = null, $height = null, $crop = null, $single = true, $upscale = false) {
|
||||
return $this->image_resize($url, $width, $height, $crop, $single, $upscale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert image name to url
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function image_to_url($image) {
|
||||
$image = $this->imageFile($image);
|
||||
if (empty($image) || strpos($image, '//') !== false) {
|
||||
$url = $image;
|
||||
} else {
|
||||
$url = $this->imageBaseUrl() . $image;
|
||||
}
|
||||
$urlImageData = explode('media/', $url);
|
||||
if (isset($urlImageData['1'])) {
|
||||
$url = $this->getBaseUrl() . ltrim($urlImageData['1'], '/');
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert image url to path
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function image_url_to_path($url) {
|
||||
if (strpos($url, $this->imageBaseUrl()) === false && strpos($url, $this->getBaseUrl()) !== false) {
|
||||
$image = str_replace($this->getBaseUrl(), '', $url);
|
||||
$path = $this->getBaseDir() . DIRECTORY_SEPARATOR . $image;
|
||||
} else {
|
||||
$image = str_replace($this->imageBaseUrl(), '', $url);
|
||||
$path = $this->imageBaseDir() . $image;
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image url
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function imageUrl($image) {
|
||||
if ($image && strpos($image, '//') === false) {
|
||||
$url = $this->imageFile($image);
|
||||
if (strpos($url, self::IMAGE_DIR . '/') === 0) {
|
||||
$url = substr($url, strlen(self::IMAGE_DIR . '/'));
|
||||
}
|
||||
$url = $this->imageBaseUrl() . $url;
|
||||
} else {
|
||||
$url = $this->imageClean($image);
|
||||
}
|
||||
if ($this->_storeManager->getStore()->isCurrentlySecure()) {
|
||||
$url = str_replace('http://', 'https://', $url);
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image path
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function imagePath($image) {
|
||||
$path = '';
|
||||
if (strpos($image, $this->imageBaseUrl()) === false && strpos($image, $this->getBaseUrl()) !== false) {
|
||||
$image = str_replace($this->getBaseUrl(), '', $image);
|
||||
if ($image) {
|
||||
$path = $this->getBaseDir() . DIRECTORY_SEPARATOR . str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $image);
|
||||
}
|
||||
} else {
|
||||
$image = str_replace($this->imageBaseUrl(), '', $image);
|
||||
if ($image) {
|
||||
$path = $this->imageBaseDir() . DIRECTORY_SEPARATOR . str_replace(array('\\', '/'), DIRECTORY_SEPARATOR, $image);
|
||||
}
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get image file from url or path
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function imageFile($image) {
|
||||
$replace = array(
|
||||
$this->imageBaseDir(),
|
||||
str_replace('http://', 'https://', $this->imageBaseUrl()),
|
||||
str_replace('https://', 'http://', $this->imageBaseUrl()),
|
||||
str_replace('http://', 'https://', $this->getBaseUrl()),
|
||||
str_replace('https://', 'http://', $this->getBaseUrl()),
|
||||
$this->getBaseDir()
|
||||
);
|
||||
foreach ($replace as $key => $item) {
|
||||
$replace[$key] = rtrim($item, DIRECTORY_SEPARATOR . '/');
|
||||
}
|
||||
$file = str_replace($replace, '', $this->imageClean($image));
|
||||
|
||||
// workaround for image urls from other store
|
||||
if (strpos($file, '://') !== false) {
|
||||
$parts = explode('/media/' . self::IMAGE_DIR, $file);
|
||||
if (isset($parts[1])) {
|
||||
$file = $parts[1];
|
||||
}
|
||||
}
|
||||
|
||||
$file = ltrim($file, DIRECTORY_SEPARATOR . '/');
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean image from artifacts
|
||||
*
|
||||
* @param string
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function imageClean($image) {
|
||||
$noHttpUrl = false;
|
||||
if (substr($image, 0, 2) == '//') {
|
||||
$noHttpUrl = true;
|
||||
$image = ltrim($image, '/');
|
||||
}
|
||||
$image = str_replace(array('\\', '//', ':/'), array('/', '/', '://'), $image);
|
||||
// fix for Windows filesystem path like C:/
|
||||
if (strpos($image, '://') === 1) {
|
||||
$image = str_replace('://', ':/', $image);
|
||||
}
|
||||
if ($noHttpUrl) {
|
||||
$image = '//' . $image;
|
||||
}
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get media base dir
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getBaseDir() {
|
||||
return $this->_directory->getAbsolutePath();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get images base path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function imageBaseDir() {
|
||||
return $this->_directory->getAbsolutePath(self::IMAGE_DIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get media base url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getBaseUrl() {
|
||||
return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get images base url
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function imageBaseUrl() {
|
||||
return $this->getBaseUrl() . self::IMAGE_DIR . '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is media file
|
||||
*
|
||||
* @param string $file
|
||||
* @return boolean
|
||||
*/
|
||||
public function isMedia($file) {
|
||||
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||
return $ext && in_array(strtolower($ext), self::ALLOWED_MEDIA_EXTENSIONS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the original image from the resized one
|
||||
*
|
||||
* @param string $image
|
||||
* @return string
|
||||
*/
|
||||
protected function _getOriginalImage($image) {
|
||||
if (strpos($image, 'thumbs/resized_') == 0) {
|
||||
$image = preg_replace('/thumbs\/resized_\d+x\d+\//m', '', $image);
|
||||
}
|
||||
return $image;
|
||||
}
|
||||
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
class Options extends \Magento\Framework\App\Helper\AbstractHelper {
|
||||
|
||||
protected $_optionFactory;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Nwdthemes\Revslider\Model\OptionFactory $optionFactory
|
||||
) {
|
||||
$this->_optionFactory = $optionFactory;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get option
|
||||
*
|
||||
* @param string Handle
|
||||
* @param string Default value
|
||||
* @return string Option value
|
||||
*/
|
||||
|
||||
public function getOption($handle, $default = false) {
|
||||
$collection = $this->_optionFactory->create()
|
||||
->getCollection()
|
||||
->addFieldToFilter('handle', $handle)
|
||||
->setPageSize(1);
|
||||
if ($collection->count()) {
|
||||
$option = $collection->getFirstItem();
|
||||
$value = $option->getData('option');
|
||||
if ((strpos($value, 'a:') !== false
|
||||
|| strpos($value, 's:') !== false
|
||||
|| strpos($value, 'O:') !== false
|
||||
|| strpos($value, 'i:') !== false
|
||||
|| strpos($value, 'b:') !== false)
|
||||
&& (($unserializedValue = @unserialize($value)) !== false || $value == 'b:0;')) {
|
||||
$value = $unserializedValue;
|
||||
} elseif ((strpos($value, 'a:') !== false
|
||||
|| strpos($value, 's:') !== false
|
||||
|| strpos($value, 'O:') !== false
|
||||
|| strpos($value, 'i:') !== false
|
||||
|| strpos($value, 'b:') !== false)
|
||||
&& ($unserializedValue = @unserialize($this->_fixSerializedString($value))) !== false) {
|
||||
// save fixed serialized string
|
||||
$this->updateOption($handle, $unserializedValue);
|
||||
$value = $unserializedValue;
|
||||
}
|
||||
return $value;
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update option
|
||||
*
|
||||
* @param string $handle
|
||||
* @param string value
|
||||
*/
|
||||
|
||||
public function updateOption($handle, $value = '') {
|
||||
$collection = $this->_optionFactory
|
||||
->create()
|
||||
->getCollection()
|
||||
->addFieldToFilter('handle', $handle)
|
||||
->setPageSize(1);
|
||||
if ($collection->count()) {
|
||||
$option = $collection->getFirstItem();
|
||||
} else {
|
||||
$option = $this->_optionFactory
|
||||
->create()
|
||||
->setData('handle', $handle);
|
||||
}
|
||||
$option
|
||||
->setData('option', is_string($value) ? $value : serialize($value))
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete option
|
||||
*
|
||||
* @param string $handle
|
||||
*/
|
||||
|
||||
public function deleteOption($handle) {
|
||||
$collection = $this->_optionFactory
|
||||
->create()
|
||||
->getCollection()
|
||||
->addFieldToFilter('handle', $handle)
|
||||
->setPageSize(1);
|
||||
if ($collection->count()) {
|
||||
$collection
|
||||
->getFirstItem()
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fix strings length in serialized string
|
||||
*
|
||||
* @param string $serializedString
|
||||
* @return string
|
||||
*/
|
||||
protected function _fixSerializedString($serializedString) {
|
||||
$fixedString = preg_replace_callback(
|
||||
'/s:([0-9]+):\"(.*?)\";/',
|
||||
function ($matches) {
|
||||
return "s:".strlen($matches[2]).':"'.$matches[2].'";';
|
||||
},
|
||||
$serializedString
|
||||
);
|
||||
return $fixedString;
|
||||
}
|
||||
|
||||
}
|
||||
+311
@@ -0,0 +1,311 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
use \Magento\Framework\UrlInterface;
|
||||
use \Magento\Framework\App\Filesystem\DirectoryList;
|
||||
|
||||
class Plugin extends \Magento\Framework\App\Helper\AbstractHelper {
|
||||
|
||||
const WP_PLUGIN_DIR = 'revslider/plugins/';
|
||||
const MAX_FAILS = 5;
|
||||
const PLUGIN_PREFIX = 'revslider-';
|
||||
|
||||
protected $_registerHelper;
|
||||
protected $_optionsHelper;
|
||||
protected $_storeManager;
|
||||
protected $_queryHelper;
|
||||
protected $_curlHelper;
|
||||
protected $_filesystemHelper;
|
||||
protected $_imagesHelper;
|
||||
protected $_resource;
|
||||
protected $_googleFonts;
|
||||
protected $_revSliderLoadBalancer;
|
||||
|
||||
protected static $directory;
|
||||
protected static $storeManager;
|
||||
|
||||
private $_plugins = null;
|
||||
private $_pluginsLoaded = false;
|
||||
private $_activePlugins = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\Filesystem $filesystem,
|
||||
\Nwdthemes\Revslider\Helper\Data $dataHelper,
|
||||
\Nwdthemes\Revslider\Helper\Options $optionsHelper,
|
||||
\Nwdthemes\Revslider\Helper\Register $registerHelper,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
||||
\Nwdthemes\Revslider\Helper\Query $queryHelper,
|
||||
\Nwdthemes\Revslider\Helper\Curl $curlHelper,
|
||||
\Nwdthemes\Revslider\Helper\Filesystem $filesystemHelper,
|
||||
\Nwdthemes\Revslider\Helper\Images $imagesHelper,
|
||||
\Magento\Framework\App\ResourceConnection $resource,
|
||||
\Nwdthemes\Revslider\Model\Revslider\GoogleFonts $googleFonts
|
||||
) {
|
||||
$this->_optionsHelper = $optionsHelper;
|
||||
$this->_registerHelper = $registerHelper;
|
||||
$this->_storeManager = $storeManager;
|
||||
$this->_queryHelper = $queryHelper;
|
||||
$this->_curlHelper = $curlHelper;
|
||||
$this->_filesystemHelper = $filesystemHelper;
|
||||
$this->_imagesHelper = $imagesHelper;
|
||||
$this->_resource = $resource;
|
||||
$this->_googleFonts = $googleFonts;
|
||||
|
||||
self::$directory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
|
||||
self::$storeManager = $storeManager;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plugin dir
|
||||
*/
|
||||
|
||||
public static function getPluginDir() {
|
||||
return self::$directory->getAbsolutePath() . self::WP_PLUGIN_DIR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plugins url
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $plugin
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function getPluginUrl($file, $plugin) {
|
||||
return $this->_storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_MEDIA)
|
||||
. self::WP_PLUGIN_DIR
|
||||
. $this->getPluginName($plugin)
|
||||
. '/'
|
||||
. $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get installed plugins list
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function getPlugins() {
|
||||
if (is_null($this->_plugins)) {
|
||||
$this->_plugins = $this->_scanPlugins();
|
||||
}
|
||||
return $this->_plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if plugin is active
|
||||
*
|
||||
* @param string $plugin
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function isPluginActive($plugin) {
|
||||
return in_array($plugin, $this->getActivePlugins());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of active plugins
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function getActivePlugins() {
|
||||
if (is_null($this->_activePlugins)) {
|
||||
$activePlugins = $this->_optionsHelper->getOption('active_plugins');
|
||||
$this->_activePlugins = $activePlugins ? $activePlugins : array();
|
||||
}
|
||||
return $this->_activePlugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate plugin
|
||||
*
|
||||
* @param string $plugin
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function activatePlugin($plugin) {
|
||||
$activePlugins = $this->getActivePlugins();
|
||||
if ( ! in_array($plugin, $activePlugins)) {
|
||||
$activePlugins[] = $plugin;
|
||||
$this->_updateActivePlugins($activePlugins);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate plugin
|
||||
*
|
||||
* @param string $plugin
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function deactivatePlugin($plugin) {
|
||||
$activePlugins = $this->getActivePlugins();
|
||||
foreach ($activePlugins as $key => $_plugin) {
|
||||
if ($plugin == $_plugin) {
|
||||
unset($activePlugins[$key]);
|
||||
}
|
||||
}
|
||||
$this->_updateActivePlugins($activePlugins);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update plugin
|
||||
*
|
||||
* @param string $updateUrl
|
||||
* @param string $plugin
|
||||
* @return boolean
|
||||
*/
|
||||
|
||||
public function updatePlugin($updateUrl, $plugin) {
|
||||
|
||||
$url = "$updateUrl/magento2/addons/{$plugin}/{$plugin}.zip";
|
||||
$file = self::getPluginDir() . $plugin . '.zip';
|
||||
|
||||
if ( ! $response = $this->_curlHelper->request($url, array('timeout' => 45))) {
|
||||
$result = false;
|
||||
}else{
|
||||
$this->_filesystemHelper->wp_mkdir_p(dirname($file));
|
||||
if ( ! @file_put_contents($file, $response['body'])) {
|
||||
$result = false;
|
||||
} else {
|
||||
if ($this->_filesystemHelper->unzip_file($file, self::getPluginDir())) {
|
||||
$result = true;
|
||||
}
|
||||
@unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load active plugins
|
||||
*
|
||||
* @param \Nwdthemes\Revslider\Helper\Framework $frameworkHelper
|
||||
*/
|
||||
|
||||
public function loadPlugins(\Nwdthemes\Revslider\Helper\Framework $frameworkHelper) {
|
||||
if ( ! $this->_pluginsLoaded) {
|
||||
|
||||
if ($failed_plugin = $this->_optionsHelper->getOption('try_load_plugin')) {
|
||||
$fails_count = $this->_optionsHelper->getOption('fails_count', 0);
|
||||
if ($fails_count >= self::MAX_FAILS) {
|
||||
$this->deactivatePlugin($failed_plugin);
|
||||
$this->_optionsHelper->updateOption('fails_count', 0);
|
||||
} else {
|
||||
$this->_optionsHelper->updateOption('fails_count', $fails_count + 1);
|
||||
}
|
||||
$this->_optionsHelper->updateOption('try_load_plugin', false);
|
||||
}
|
||||
|
||||
foreach ($this->getActivePlugins() as $plugin) {
|
||||
if (file_exists(self::getPluginDir() . $plugin)) {
|
||||
$this->_optionsHelper->updateOption('try_load_plugin', $plugin);
|
||||
$frameworkHelper->includeFile(self::getPluginDir() . $plugin);
|
||||
if ($failed_plugin == $plugin) {
|
||||
$this->_optionsHelper->updateOption('fails_count', 0);
|
||||
}
|
||||
$this->_optionsHelper->updateOption('try_load_plugin', false);
|
||||
}
|
||||
}
|
||||
|
||||
$frameworkHelper->do_action('plugins_loaded', $frameworkHelper);
|
||||
|
||||
$this->_pluginsLoaded = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plugin name from path
|
||||
* @param string $plugin
|
||||
* @return string
|
||||
*/
|
||||
public function getPluginName($plugin) {
|
||||
$pluginName = basename($plugin, '.php');
|
||||
if ($pluginName && strpos($pluginName, self::PLUGIN_PREFIX) !== 0) {
|
||||
$pluginName = $this->getPluginName(substr($plugin, 0, -strlen($pluginName)));
|
||||
}
|
||||
return $pluginName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find installed plugins
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
private function _scanPlugins() {
|
||||
$path = self::getPluginDir();
|
||||
$plugins = array();
|
||||
foreach (glob($path . '*' , GLOB_ONLYDIR) as $dir) {
|
||||
$dirName = basename($dir);
|
||||
$fileName = $dirName . '.php';
|
||||
$filePath = $dir . '/' . $fileName;
|
||||
if (file_exists($filePath)) {
|
||||
$plugin = array();
|
||||
$fileContent = file_get_contents($filePath);
|
||||
$fileContent = strstr($fileContent, '*/', true);
|
||||
foreach (explode("\n", $fileContent) as $line) {
|
||||
$parts = explode(': ', $line);
|
||||
if (count($parts) == 2) {
|
||||
switch (trim(strtolower(str_replace('*', '', $parts[0])))) {
|
||||
case 'plugin name' : $key = 'Name'; break;
|
||||
case 'plugin uri' : $key = 'PluginURI'; break;
|
||||
case 'description' : $key = 'Description'; break;
|
||||
case 'author' : $key = 'Author'; break;
|
||||
case 'version' : $key = 'Version'; break;
|
||||
case 'author uri' : $key = 'AuthorURI'; break;
|
||||
default: $key = str_replace(' ', '', trim($parts[0])); break;
|
||||
}
|
||||
$plugin[$key] = trim($parts[1]);
|
||||
}
|
||||
}
|
||||
if (isset($plugin['Name']) && isset($plugin['Version'])) {
|
||||
$plugin['Network'] = false;
|
||||
$plugin['Title'] = $plugin['Name'];
|
||||
$plugin['AuthorName'] = $plugin['Author'];
|
||||
$plugins[$dirName . '/' . $fileName] = $plugin;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update active plugins
|
||||
*
|
||||
* @param array $plugins
|
||||
*/
|
||||
|
||||
private function _updateActivePlugins($plugins) {
|
||||
$this->_activePlugins = $plugins;
|
||||
$this->_optionsHelper->updateOption('active_plugins', $plugins);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate old plugins to avoid compatibility issues
|
||||
*/
|
||||
public function deactivateOldPlugins() {
|
||||
// TODO: Re-activate when pllugin data updates will be online
|
||||
/*
|
||||
foreach ($this->getPlugins() as $pluginName => $pluginData) {
|
||||
$requiredVersion = $pluginName == 'revslider-backup-addon/revslider-backup-addon.php' ? '2.0.2' : '3';
|
||||
if (version_compare($pluginData['Version'], $requiredVersion, '<')) {
|
||||
$this->deactivatePlugin($pluginName);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
}
|
||||
+294
@@ -0,0 +1,294 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
use \Nwdthemes\Revslider\Helper\Data;
|
||||
use \Magento\Catalog\Model\Product\Attribute\Source\Status;
|
||||
use \Magento\Catalog\Model\Product\Visibility;
|
||||
use \Magento\Framework\App\ActionInterface;
|
||||
use \Magento\Reports\Model\Product\Index;
|
||||
|
||||
class Products extends \Magento\Framework\App\Helper\AbstractHelper {
|
||||
|
||||
protected $_coreRegistry;
|
||||
protected $_categoryFactory;
|
||||
protected $_productFactory;
|
||||
protected $_indexFactory;
|
||||
protected $_stockHelper;
|
||||
protected $_storeManager;
|
||||
protected $_cartHelper;
|
||||
protected $_dataHelper;
|
||||
protected $_urlBuilder;
|
||||
protected $_urlEncoder;
|
||||
|
||||
private $_products = array();
|
||||
private $_categories = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\Registry $coreRegistry,
|
||||
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
|
||||
\Magento\Catalog\Model\ProductFactory $productFactory,
|
||||
\Magento\CatalogInventory\Helper\Stock $stockHelper,
|
||||
\Magento\Checkout\Helper\Cart $cartHelper,
|
||||
\Magento\Checkout\Helper\Data $dataHelper,
|
||||
\Magento\Reports\Model\Product\Index\Factory $indexFactory,
|
||||
\Magento\Store\Model\StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->_coreRegistry = $coreRegistry;
|
||||
$this->_categoryFactory = $categoryFactory;
|
||||
$this->_productFactory = $productFactory;
|
||||
$this->_indexFactory = $indexFactory;
|
||||
$this->_stockHelper = $stockHelper;
|
||||
$this->_storeManager = $storeManager;
|
||||
$this->_cartHelper = $cartHelper;
|
||||
$this->_dataHelper = $dataHelper;
|
||||
|
||||
$this->_urlBuilder = $context->getUrlBuilder();
|
||||
$this->_urlEncoder = $context->getUrlEncoder();
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets product
|
||||
*
|
||||
* @param id $id
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function getProduct($id) {
|
||||
|
||||
if ( ! $id ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (isset($this->_products[$id]))
|
||||
{
|
||||
return $this->_products[$id];
|
||||
}
|
||||
|
||||
$product = $this->_productFactory->create()->load($id);
|
||||
return $this->_prepareProduct($product);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets products by querey
|
||||
*
|
||||
* @param array $query
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function getProductsByQuery($query) {
|
||||
|
||||
$productsCollection = $this->_productFactory->create()->getCollection();
|
||||
|
||||
if (isset($query['tax_query'][0]['taxonomy']) && $query['tax_query'][0]['taxonomy'] == 'category' && isset($query['tax_query'][0]['terms']) && ! empty($query['tax_query'][0]['terms']) && is_array($query['tax_query'][0]['terms'])) {
|
||||
$productsCollection->addCategoriesFilter(['in' => $query['tax_query'][0]['terms']]);
|
||||
} elseif (isset($query['post__in']) && ! empty($query['post__in']) && is_array($query['post__in'])) {
|
||||
$productsCollection->addFieldToFilter('entity_id', ['in' => $query['post__in']]);
|
||||
} else {
|
||||
return array();
|
||||
}
|
||||
|
||||
$this->_stockHelper->addIsInStockFilterToCollection($productsCollection);
|
||||
|
||||
$productsCollection
|
||||
->addStoreFilter($this->_storeManager->getStore())
|
||||
->addFieldToFilter('status', Status::STATUS_ENABLED)
|
||||
->addFieldToFilter('visibility', array(Visibility::VISIBILITY_BOTH, Visibility::VISIBILITY_IN_CATALOG, Visibility::VISIBILITY_IN_SEARCH))
|
||||
->addAttributeToSelect('*');
|
||||
|
||||
if (isset($query['orderby'])) {
|
||||
$productsCollection->setOrder($query['orderby'], isset($query['order']) ? $query['order'] : 'desc');
|
||||
}
|
||||
|
||||
if (isset($query['showposts'])) {
|
||||
$productsCollection->setPageSize($query['showposts']);
|
||||
}
|
||||
|
||||
$products = array();
|
||||
foreach ($productsCollection as $product) {
|
||||
$products[] = $this->_prepareProduct($product);
|
||||
}
|
||||
|
||||
return $products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current product Id
|
||||
* get most recent product viewed if there is no current one
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
|
||||
public function getCurrentProductId() {
|
||||
$currentProduct = $this->_coreRegistry->registry('product');
|
||||
if ($currentProduct) {
|
||||
$currentProductId = $currentProduct->getId();
|
||||
} else {
|
||||
$recentProducts = $this->_indexFactory->get('viewed')->getCollection()->getAllIds();
|
||||
$recentProducts = $this->getProductsByQuery(array('post__in' => $recentProducts, 'showposts' => 1));
|
||||
if ($recentProducts) {
|
||||
$currentProductId = $recentProducts[0]['ID'];
|
||||
} else {
|
||||
$currentProductId = false;
|
||||
}
|
||||
}
|
||||
return $currentProductId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets category
|
||||
*
|
||||
* @param id $id
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function getCategory($id) {
|
||||
|
||||
if (isset($this->_categories[$id]))
|
||||
{
|
||||
return $this->_categories[$id];
|
||||
}
|
||||
|
||||
$category = $this->_categoryFactory->create()->load($id);
|
||||
return $this->_prepareCategory($category);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets categories
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
public function getCategories() {
|
||||
|
||||
$categoriesCollection = $this->_categoryFactory->create()
|
||||
->getCollection()
|
||||
->addAttributeToSelect('name')
|
||||
->addAttributeToSort('path', 'asc');
|
||||
|
||||
$categories = array();
|
||||
foreach ($categoriesCollection as $category) {
|
||||
if ($_category = $this->_prepareCategory($category)) {
|
||||
$categories[] = $_category;
|
||||
}
|
||||
}
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare product data for slider
|
||||
*
|
||||
* @param object $product
|
||||
* @return array
|
||||
*/
|
||||
|
||||
private function _prepareProduct($product) {
|
||||
|
||||
if (isset($this->_products[$product->getId()]))
|
||||
{
|
||||
return $this->_products[$product->getId()];
|
||||
}
|
||||
|
||||
$arrProduct = $product->getData();
|
||||
$arrProduct['item'] = $product;
|
||||
|
||||
try{
|
||||
$arrProduct['image'] = $product->getImageUrl();
|
||||
} catch (\Exception $e) {
|
||||
Data::logException($e);
|
||||
$arrProduct['image'] = '';
|
||||
}
|
||||
|
||||
switch ($product->getTypeId()) {
|
||||
case 'configurable' :
|
||||
$price = null;
|
||||
$specialPrice = null;
|
||||
foreach ($product->getTypeInstance()->getUsedProducts($product) as $subProduct) {
|
||||
$price = $price ? min($price, $subProduct->getPrice()) : $subProduct->getPrice();
|
||||
$specialPrice = $specialPrice ? min($specialPrice, $subProduct->getSpecialPrice()) : $subProduct->getSpecialPrice();
|
||||
}
|
||||
break;
|
||||
case 'simple' :
|
||||
default:
|
||||
$price = $product->getPrice();
|
||||
$specialPrice = $product->getSpecialPrice();
|
||||
break;
|
||||
}
|
||||
|
||||
$arrProduct['ID'] = $product->getId();
|
||||
$arrProduct['post_excerpt'] = $product->getShortDescription();
|
||||
$arrProduct['post_content'] = $product->getDescription();
|
||||
$arrProduct['post_status'] = 'published';
|
||||
$arrProduct['post_category'] = '';
|
||||
$arrProduct['cart_link'] = $this->_cartHelper->getAddUrl($product);
|
||||
$arrProduct['wishlist_link'] = $this->_urlBuilder->getUrl('wishlist/index/add', ['product' => $product->getId()]);
|
||||
$arrProduct['price'] = $price ? $this->_dataHelper->formatPrice($price) : '';
|
||||
$arrProduct['special_price'] = $specialPrice ? $this->_dataHelper->formatPrice($specialPrice) : '';
|
||||
$arrProduct['view_link'] = $product->getProductUrl();
|
||||
$arrProduct['add_to_cart_action_callback'] = $this->_getAddToCartActionCallback($product);
|
||||
|
||||
$this->_products[$product->getId()] = $arrProduct;
|
||||
|
||||
return $arrProduct;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare category data for slider
|
||||
*
|
||||
* @param object $product
|
||||
* @return array
|
||||
*/
|
||||
|
||||
private function _prepareCategory($category) {
|
||||
|
||||
if (isset($this->_categories[$category->getId()]))
|
||||
{
|
||||
return $this->_categories[$category->getId()];
|
||||
}
|
||||
|
||||
$arrCategory = $category->getData();
|
||||
|
||||
if ( ! ($category->getId() > 1 && isset($arrCategory['name']) && isset($arrCategory['level'])))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$arrCategory['count'] = 1;
|
||||
$arrCategory['name'] = str_repeat('- ', max(0, $arrCategory['level'] - 1)) . $arrCategory['name'];
|
||||
$arrCategory['cat_ID'] = $category->getId();
|
||||
$arrCategory['term_id'] = $category->getId();
|
||||
$arrCategory['url'] = $category->getUrl($category);
|
||||
|
||||
$this->_categories[$category->getId()] = $arrCategory;
|
||||
|
||||
return $arrCategory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate add to cart form action for callback
|
||||
*
|
||||
* @param object $product
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function _getAddToCartActionCallback($product) {
|
||||
$url = $this->_cartHelper->getAddUrl($product);
|
||||
$data = json_encode([
|
||||
'action' => $url,
|
||||
'data' => [
|
||||
'product' => $product->getEntityId(),
|
||||
ActionInterface::PARAM_NAME_URL_ENCODED => $this->_urlEncoder->encode($url),
|
||||
]
|
||||
]);
|
||||
return "$.mage.dataPost().postData($data);";
|
||||
}
|
||||
|
||||
}
|
||||
+323
@@ -0,0 +1,323 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
use \Nwdthemes\Revslider\Helper\Data;
|
||||
|
||||
class Query extends \Magento\Framework\App\Helper\AbstractHelper {
|
||||
|
||||
const ARRAY_A = 'ARRAY_A';
|
||||
|
||||
protected $_context;
|
||||
protected $_resource;
|
||||
protected $_animationFactory;
|
||||
protected $_backupFactory;
|
||||
protected $_cssFactory;
|
||||
protected $_navigationFactory;
|
||||
protected $_slideFactory;
|
||||
protected $_sliderFactory;
|
||||
protected $_staticSlideFactory;
|
||||
|
||||
public $prefix = '';
|
||||
public $base_prefix = '';
|
||||
public $last_error = '';
|
||||
public $last_query = '';
|
||||
public $insert_id = '';
|
||||
public $lastRowID = '';
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\App\ResourceConnection $resource,
|
||||
\Nwdthemes\Revslider\Model\AnimationFactory $animationFactory,
|
||||
\Nwdthemes\Revslider\Model\BackupFactory $backupFactory,
|
||||
\Nwdthemes\Revslider\Model\CssFactory $cssFactory,
|
||||
\Nwdthemes\Revslider\Model\NavigationFactory $navigationFactory,
|
||||
\Nwdthemes\Revslider\Model\SlideFactory $slideFactory,
|
||||
\Nwdthemes\Revslider\Model\SliderFactory $sliderFactory,
|
||||
\Nwdthemes\Revslider\Model\StaticSlideFactory $staticSlideFactory
|
||||
) {
|
||||
$this->_context = $context;
|
||||
$this->_resource = $resource;
|
||||
$this->_animationFactory = $animationFactory;
|
||||
$this->_backupFactory = $backupFactory;
|
||||
$this->_cssFactory = $cssFactory;
|
||||
$this->_navigationFactory = $navigationFactory;
|
||||
$this->_slideFactory = $slideFactory;
|
||||
$this->_sliderFactory = $sliderFactory;
|
||||
$this->_staticSlideFactory = $staticSlideFactory;
|
||||
|
||||
parent::__construct($this->_context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get query results
|
||||
*
|
||||
* @param string Query
|
||||
* @param string Result format
|
||||
* @return array
|
||||
*/
|
||||
public function get_results($query, $mode = self::ARRAY_A) {
|
||||
|
||||
$queryArray = explode('FROM', $query);
|
||||
if (count($queryArray) < 2) {
|
||||
die();
|
||||
}
|
||||
$queryArray = explode('WHERE', $queryArray[1]);
|
||||
$table = trim($queryArray[0]);
|
||||
$collection = $this->getFactory($table)->create()->getCollection();
|
||||
|
||||
$where = isset($queryArray[1]) ? trim($queryArray[1]) : '';
|
||||
|
||||
if (strpos($where, 'ORDER BY') !== false) {
|
||||
$whereArray = explode('ORDER BY', $where);
|
||||
$where = trim($whereArray[0]);
|
||||
$orderArray = explode(' ', trim($whereArray[1]));
|
||||
$orderBy = trim($orderArray[0], '"\' ');
|
||||
$orderDir = isset($orderArray[1]) ? trim($orderArray[1], '"\' ') : 'ASC';
|
||||
}
|
||||
|
||||
if ($where) {
|
||||
if (strpos($where, ' IN(') !== false) {
|
||||
list($field, $value) = explode(' IN(', $where);
|
||||
$condition = ['in' => explode(',', trim($value, '"\' )'))];
|
||||
} elseif (strpos($where, '!=') === false) {
|
||||
list($field, $value) = explode('=', $where);
|
||||
$condition = trim($value, '"\' ');
|
||||
} else {
|
||||
list($field, $value) = explode('!=', $where);
|
||||
$condition = ['neq' => trim($value, '"\' ')];
|
||||
}
|
||||
if (isset($field)) {
|
||||
$collection->addFieldToFilter(trim($field, '`"\' '), $condition);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty($orderBy)) {
|
||||
$collection->setOrder($orderBy, $orderDir);
|
||||
}
|
||||
|
||||
$response = array();
|
||||
foreach ($collection as $_item) {
|
||||
$response[] = $_item->getData();
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get query row
|
||||
*
|
||||
* @param string Query
|
||||
* @param string Result format
|
||||
* @return array
|
||||
*/
|
||||
public function get_row($query, $mode = '') {
|
||||
$query = $this->_convertTableNames($query);
|
||||
$readConnection = $this->_resource->getConnection('core_read');
|
||||
$result = $readConnection->fetchRow($query);
|
||||
return $mode == self::ARRAY_A ? $result : (object) $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert row
|
||||
*
|
||||
* @param string Table name
|
||||
* @param array Data
|
||||
* @return int
|
||||
*/
|
||||
|
||||
public function insert($table, $data = array()) {
|
||||
$model = $this->getFactory($table)->create();
|
||||
$model->setData($data);
|
||||
try {
|
||||
$model->save();
|
||||
} catch (\Exception $e) {
|
||||
Data::logException($e);
|
||||
$this->throwError($e->getMessage());
|
||||
}
|
||||
$this->lastRowID = $model->getId();
|
||||
$this->insert_id = $this->lastRowID;
|
||||
return $this->lastRowID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update row
|
||||
*
|
||||
* @param string Table name
|
||||
* @param array Data
|
||||
* @param array Where
|
||||
*/
|
||||
|
||||
public function update($table, $data = array(), $where = false) {
|
||||
if (is_array($where) && $where) {
|
||||
$collection = $this->getFactory($table)->create()->getCollection();
|
||||
foreach ($where as $_field => $_value) {
|
||||
$collection->addFieldToFilter($_field, $_value);
|
||||
}
|
||||
$item = $collection->getFirstItem();
|
||||
try {
|
||||
$item
|
||||
->addData($data)
|
||||
->setId( $item->getId() )
|
||||
->save();
|
||||
} catch (\Exception $e) {
|
||||
Data::logException($e);
|
||||
$this->throwError($e->getMessage());
|
||||
}
|
||||
} else {
|
||||
$this->throwError('No id provided.');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete row
|
||||
*
|
||||
* @param string Table name
|
||||
* @param array Data
|
||||
* @param array Where
|
||||
*/
|
||||
|
||||
public function delete($table, $where) {
|
||||
if ( ! $table) {
|
||||
$this->throwError(__('Table name should not be empty.'));
|
||||
}
|
||||
if ( ! $where) {
|
||||
$this->throwError(__('Where should not be empty.'));
|
||||
}
|
||||
$collection = $this->getFactory($table)->create()->getCollection();
|
||||
foreach ($where as $field => $value) {
|
||||
$collection->addFieldToFilter($field, $value);
|
||||
}
|
||||
foreach ($collection as $_item) {
|
||||
$_item->delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare query
|
||||
*
|
||||
* @param string Query
|
||||
* @param mixed Args
|
||||
* @return array
|
||||
*/
|
||||
public function prepare($query, $args) {
|
||||
$args = func_get_args();
|
||||
array_shift( $args );
|
||||
// If args were passed as an array (as in vsprintf), move them up
|
||||
if ( isset( $args[0] ) && is_array($args[0]) )
|
||||
$args = $args[0];
|
||||
$query = str_replace( "'%s'", '%s', $query ); // in case someone mistakenly already singlequoted it
|
||||
$query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
|
||||
$query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
|
||||
$query = preg_replace( '|(?<!%)%s|', "%s", $query ); // quote the strings, avoiding escaped strings like %%s
|
||||
array_walk( $args, array( $this, 'escape_by_ref' ) );
|
||||
return @vsprintf( $query, $args );
|
||||
}
|
||||
|
||||
public function escape_by_ref(&$arg) {
|
||||
if( (string)(int)$arg != $arg) $arg = $this->_resource->getConnection('core_write')->quote($arg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run sql query
|
||||
*
|
||||
* param string Query
|
||||
*/
|
||||
|
||||
public function query($query) {
|
||||
$query = $this->_convertTableNames($query);
|
||||
$writeConnection = $this->_resource->getConnection('core_write');
|
||||
$result = $writeConnection->query($query);
|
||||
$this->insert_id = $writeConnection->lastInsertId();
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get results of SQL query
|
||||
*
|
||||
* @param string $query
|
||||
* @return array
|
||||
*/
|
||||
public function get_query_results($query) {
|
||||
return $this->query($query)->fetchAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run sql query and get result variable
|
||||
*
|
||||
* param string Query
|
||||
* return var
|
||||
*/
|
||||
|
||||
public function get_var($query) {
|
||||
$query = $this->_convertTableNames($query);
|
||||
$readConnection = $this->_resource->getConnection('core_read');
|
||||
return $readConnection->fetchOne($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate DB alter query by difference
|
||||
*
|
||||
* @param string Query
|
||||
* @param boolean Is Execute
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function dbDelta($sql, $isExecute = true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get model factory by name
|
||||
*
|
||||
* @param string Name
|
||||
* @return object
|
||||
*/
|
||||
|
||||
public function getFactory($name) {
|
||||
switch ($name) {
|
||||
case 'nwdthemes_revslider_animations' :
|
||||
$factory = $this->_animationFactory;
|
||||
break;
|
||||
case 'nwdthemes_revslider_backup' :
|
||||
$factory = $this->_backupFactory;
|
||||
break;
|
||||
case 'nwdthemes_revslider_css' :
|
||||
$factory = $this->_cssFactory;
|
||||
break;
|
||||
case 'nwdthemes_revslider_navigations' :
|
||||
$factory = $this->_navigationFactory;
|
||||
break;
|
||||
case 'nwdthemes_revslider_slides' :
|
||||
$factory = $this->_slideFactory;
|
||||
break;
|
||||
case 'nwdthemes_revslider_sliders' :
|
||||
$factory = $this->_sliderFactory;
|
||||
break;
|
||||
case 'nwdthemes_revslider_static_slides' :
|
||||
$factory = $this->_staticSlideFactory;
|
||||
break;
|
||||
}
|
||||
return $factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert table names in query
|
||||
*
|
||||
* @param string $query
|
||||
* @return string
|
||||
*/
|
||||
protected function _convertTableNames($query) {
|
||||
preg_match('#\b(nwdthemes_revslider_\w+)\b#', $query, $modelNames);
|
||||
$modelNames = array_unique($modelNames);
|
||||
foreach ($modelNames as $modelName) {
|
||||
$query = str_replace($modelName, $this->_resource->getTableName($modelName), $query);
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
}
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Nwdthemes\Revslider\Helper;
|
||||
|
||||
class Register extends \Magento\Framework\App\Helper\AbstractHelper {
|
||||
|
||||
protected $_registry;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
|
||||
public function __construct(
|
||||
\Magento\Framework\App\Helper\Context $context,
|
||||
\Magento\Framework\Registry $registry
|
||||
) {
|
||||
$this->_registry = $registry;
|
||||
|
||||
parent::__construct($context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add action
|
||||
*
|
||||
* @param string $handle
|
||||
* @param array $action
|
||||
*/
|
||||
|
||||
public function addAction($handle, $action) {
|
||||
$this->addToRegister('actions', $handle, $action);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove action
|
||||
*
|
||||
* @param string $handle
|
||||
* @param array $action
|
||||
*/
|
||||
|
||||
function removeAction($handle, $action) {
|
||||
$actions = $this->getFromRegister('actions', $handle);
|
||||
foreach ($actions as $_key => $_action) {
|
||||
if ($_action == $action) {
|
||||
unset($actions[$_key]);
|
||||
$this->setToRegister('actions', $handle, $actions);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do action
|
||||
*
|
||||
* @param string $handle
|
||||
* @param mixed $args
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function doAction($handle, $args) {
|
||||
|
||||
ob_start();
|
||||
$return = false;
|
||||
foreach ($this->getFromRegister('actions', $handle) as $action) {
|
||||
if (is_array($action) && count($action) == 2) {
|
||||
$class = is_object($action[0]) ? get_class($action[0]) : $action[0];
|
||||
$reflectionMethod = new \ReflectionMethod($class, $action[1]);
|
||||
$argsNum = $reflectionMethod->getNumberOfParameters();
|
||||
while ($argsNum > count($args)) {
|
||||
$args[] = '';
|
||||
}
|
||||
}
|
||||
$_return = call_user_func_array($action, $args);
|
||||
if ($_return) {
|
||||
$args[1] = $_return;
|
||||
$return = $_return;
|
||||
}
|
||||
}
|
||||
$output = ob_get_contents();
|
||||
ob_end_clean();
|
||||
|
||||
$output = str_replace(' for WordPress', '', $output);
|
||||
if ( ! in_array('action_no_output', $args)) {
|
||||
echo $output;
|
||||
}
|
||||
|
||||
return $output ? $output : $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if action added
|
||||
*
|
||||
* @param string $handle
|
||||
* @param array $action
|
||||
* @return boolean
|
||||
*/
|
||||
public function hasAction($handle, $action) {
|
||||
$actions = $this->getFromRegister('actions', $handle);
|
||||
$hasAction = isset($actions[$handle]) && is_array($actions[$handle]) && in_array($action, $actions[$handle]);
|
||||
return $hasAction;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds handle data to register by key
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $handle
|
||||
* @param mixed $data
|
||||
*/
|
||||
|
||||
public function addToRegister($key, $handle, $data) {
|
||||
$item = $this->getFromRegister($key, $handle);
|
||||
$item[] = $data;
|
||||
$this->setToRegister($key, $handle, $item);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets handle data to register by key
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $handle
|
||||
* @param mixed $data
|
||||
*/
|
||||
|
||||
public function setToRegister($key, $handle, $data) {
|
||||
$registerKey = 'nwdrevslider_' . $key;
|
||||
$register = $this->getFromRegister($key);
|
||||
$register[$handle] = $data;
|
||||
$this->_registry->unregister($registerKey);
|
||||
$this->_registry->register($registerKey, $register);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get data from register
|
||||
*
|
||||
* @param string $key
|
||||
* @param string|boolean $handle
|
||||
* @param mixed $default
|
||||
* @return mixed
|
||||
*/
|
||||
|
||||
public function getFromRegister($key, $handle = false, $default = array()) {
|
||||
$registerKey = 'nwdrevslider_' . $key;
|
||||
$register = $this->_registry->registry($registerKey) ? $this->_registry->registry($registerKey) : array();
|
||||
return $handle !== false
|
||||
? (isset($register[$handle]) ? $register[$handle] : $default)
|
||||
: $register;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user