58 lines
1.7 KiB
PHP
Executable File
58 lines
1.7 KiB
PHP
Executable File
<?php
|
|
|
|
namespace IpSupply\News\Block\Widget;
|
|
|
|
use Magento\Framework\View\Element\Template;
|
|
use Magento\Widget\Block\BlockInterface;
|
|
use Magento\Framework\View\Element\Template\Context;
|
|
use Magento\Framework\App\ObjectManager;
|
|
use Magento\Store\Model\StoreManagerInterface;
|
|
use Magento\Framework\UrlInterface;
|
|
use DOMDocument;
|
|
use DOMXPath;
|
|
|
|
class NewsWidget extends Template implements BlockInterface {
|
|
protected $_objectManager;
|
|
protected $_storeManager;
|
|
|
|
public function __construct(StoreManagerInterface $_storeManager, Context $context, array $data = []) {
|
|
$this->_objectManager = ObjectManager::getInstance();
|
|
$this->_storeManager = $_storeManager;
|
|
parent::__construct($context, $data);
|
|
}
|
|
|
|
public function getRSSData() {
|
|
$rssUrl = $this->getData("url");
|
|
$xml = simplexml_load_file($rssUrl); // data type of $xml is Object
|
|
$dataResponse = array();
|
|
foreach ($xml->channel->item as $item) {
|
|
$description = strip_tags($item->description, ['a']);
|
|
|
|
$doc = new DOMDocument();
|
|
libxml_use_internal_errors(true);
|
|
$doc->loadHTML($item->description);
|
|
$xpath = new DOMXPath($doc);
|
|
$imgs = $xpath->query("//img");
|
|
if (count($imgs) != 0) {
|
|
$imageURL = $imgs[0]->getAttribute("src");
|
|
}
|
|
else {
|
|
$imageURL = null;
|
|
}
|
|
|
|
$obj = array(
|
|
"image_url" => $imageURL,
|
|
"title" => $item->title,
|
|
"link" => $item->link,
|
|
"description" => $description,
|
|
);
|
|
array_push($dataResponse, $obj);
|
|
}
|
|
return $dataResponse;
|
|
}
|
|
|
|
function getAssetUrl($asset) {
|
|
$assetRepository = $this->_objectManager->get('Magento\Framework\View\Asset\Repository');
|
|
return $assetRepository->createAsset($asset)->getUrl();
|
|
}
|
|
} |