DefaultCaptcha.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Captcha\Block\Captcha;
  7. /**
  8. * Captcha block
  9. */
  10. class DefaultCaptcha extends \Magento\Framework\View\Element\Template
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $_template = 'Magento_Captcha::default.phtml';
  16. /**
  17. * @var string
  18. */
  19. protected $_captcha;
  20. /**
  21. * @var \Magento\Captcha\Helper\Data
  22. */
  23. protected $_captchaData;
  24. /**
  25. * @param \Magento\Framework\View\Element\Template\Context $context
  26. * @param \Magento\Captcha\Helper\Data $captchaData
  27. * @param array $data
  28. */
  29. public function __construct(
  30. \Magento\Framework\View\Element\Template\Context $context,
  31. \Magento\Captcha\Helper\Data $captchaData,
  32. array $data = []
  33. ) {
  34. parent::__construct($context, $data);
  35. $this->_captchaData = $captchaData;
  36. }
  37. /**
  38. * Returns template path
  39. *
  40. * @return string
  41. */
  42. public function getTemplate()
  43. {
  44. return $this->getIsAjax() ? '' : $this->_template;
  45. }
  46. /**
  47. * Returns URL to controller action which returns new captcha image
  48. *
  49. * @return string
  50. */
  51. public function getRefreshUrl()
  52. {
  53. $store = $this->_storeManager->getStore();
  54. return $store->getUrl('captcha/refresh', ['_secure' => $store->isCurrentlySecure()]);
  55. }
  56. /**
  57. * Renders captcha HTML (if required)
  58. *
  59. * @return string
  60. */
  61. protected function _toHtml()
  62. {
  63. if ($this->getCaptchaModel()->isRequired()) {
  64. $this->getCaptchaModel()->generate();
  65. return parent::_toHtml();
  66. }
  67. return '';
  68. }
  69. /**
  70. * Returns captcha model
  71. *
  72. * @return \Magento\Captcha\Model\CaptchaInterface
  73. */
  74. public function getCaptchaModel()
  75. {
  76. return $this->_captchaData->getCaptcha($this->getFormId());
  77. }
  78. }