Post.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © 2016 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Block\Amp\Og;
  9. use Magento\Store\Model\ScopeInterface;
  10. /**
  11. * Blog post opengraph for amp
  12. */
  13. if (class_exists('\Plumrocket\Amp\Block\Page\Head\Og\AbstractOg')) {
  14. class PostIntermediate extends \Plumrocket\Amp\Block\Page\Head\Og\AbstractOg {}
  15. } else {
  16. class PostIntermediate extends \Magento\Framework\View\Element\AbstractBlock {}
  17. }
  18. class Post extends PostIntermediate
  19. {
  20. /**
  21. * Retrieve open graph params
  22. *
  23. * @return array
  24. */
  25. public function getOgParams()
  26. {
  27. $params = parent::getOgParams();
  28. $post = $this->getPost();
  29. return array_merge($params, [
  30. 'type' => $post->getOgType() ?: 'article',
  31. 'url' => $this->_helper->getCanonicalUrl($post->getPostUrl()),
  32. 'image' => (string)$post->getImageUrl(),
  33. ]);
  34. }
  35. /**
  36. * Retrieve current post
  37. *
  38. * @return \Magefan\Blog\Model\Post
  39. */
  40. public function getPost()
  41. {
  42. return $this->_coreRegistry->registry('current_blog_post');
  43. }
  44. /**
  45. * Retrieve page main image
  46. *
  47. * @return string | null
  48. */
  49. public function getImage()
  50. {
  51. $image = $this->getPost()->getOgImage();
  52. if (!$image) {
  53. $image = $this->getPost()->getFirstImage();
  54. }
  55. if ($image) {
  56. return $this->stripTags($image);
  57. }
  58. }
  59. }