Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
[TASK] Add compatible with Magento 2.2, 2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyennn committed Oct 12, 2018
1 parent a725d08 commit e3f3e85
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class Data extends AbstractHelper
*/
protected $_escaper;

protected $_serializer;

public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
Expand All @@ -80,8 +82,8 @@ public function __construct(
Encryptor $encryptor,
\Magento\Framework\Escaper $_escaper,
ProductFactory $productFactory,
CategoryFactory $categoryFactory

CategoryFactory $categoryFactory,
\GhoSter\AutoInstagramPost\Model\Serialize\Serializer $serializer
)
{
$this->_storeManager = $storeManager;
Expand All @@ -92,6 +94,7 @@ public function __construct(
$this->productFactory = $productFactory;
$this->categoryFactory = $categoryFactory;
$this->_escaper = $_escaper;
$this->_serializer = $serializer;
parent::__construct($context);
}

Expand Down Expand Up @@ -307,7 +310,7 @@ public function getCustomHashHashtags($store = null)
);

if ($hashTagConfig && $this->isEnableHashtag() && $this->isEnableCustomHashtag()) {
$hashTags = unserialize($hashTagConfig);
$hashTags = $this->_serializer->unserialize($hashTagConfig);

if (is_array($hashTags)) {
foreach ($hashTags as $key => $hashTag) {
Expand Down
86 changes: 86 additions & 0 deletions Model/Serialize/Serializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace GhoSter\AutoInstagramPost\Model\Serialize;

class Serializer
{
/**
* {@inheritDoc}
* @since 100.2.0
*/
public function unserialize($string)
{
if ($this->is_serialized($string)) {
$string = $this->serialize($string);
}
$result = json_decode($string, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new \InvalidArgumentException('Unable to unserialize value.');

}
return $result;
}


function is_serialized($value, &$result = null)
{
// Bit of a give away this one
if (!is_string($value)) {
return false;
}
// Serialized false, return true. unserialize() returns false on an
// invalid string or it could return false if the string is serialized
// false, eliminate that possibility.
if ($value === 'b:0;') {
$result = false;
return true;
}
$length = strlen($value);
$end = '';
switch ($value[0]) {
case 's':
if ($value[$length - 2] !== '"') {
return false;
}
case 'b':
case 'i':
case 'd':
// This looks odd but it is quicker than isset()ing
$end .= ';';
case 'a':
case 'O':
$end .= '}';
if ($value[1] !== ':') {
return false;
}
switch ($value[2]) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
break;
default:
return false;
}
case 'N':
$end .= ';';
if ($value[$length - 1] !== $end[0]) {
return false;
}
break;
default:
return false;
}
if (($result = @unserialize($value)) === false) {
$result = null;
return false;
}
return true;
}
}

0 comments on commit e3f3e85

Please sign in to comment.