image-box.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor image box widget.
  8. *
  9. * Elementor widget that displays an image, a headline and a text.
  10. *
  11. * @since 1.0.0
  12. */
  13. class Widget_Image_Box extends Widget_Base {
  14. /**
  15. * Get widget name.
  16. *
  17. * Retrieve image box widget name.
  18. *
  19. * @since 1.0.0
  20. * @access public
  21. *
  22. * @return string Widget name.
  23. */
  24. public function get_name() {
  25. return 'image-box';
  26. }
  27. /**
  28. * Get widget title.
  29. *
  30. * Retrieve image box widget title.
  31. *
  32. * @since 1.0.0
  33. * @access public
  34. *
  35. * @return string Widget title.
  36. */
  37. public function get_title() {
  38. return __( 'Image Box', 'elementor' );
  39. }
  40. /**
  41. * Get widget icon.
  42. *
  43. * Retrieve image box widget icon.
  44. *
  45. * @since 1.0.0
  46. * @access public
  47. *
  48. * @return string Widget icon.
  49. */
  50. public function get_icon() {
  51. return 'eicon-image-box';
  52. }
  53. /**
  54. * Get widget keywords.
  55. *
  56. * Retrieve the list of keywords the widget belongs to.
  57. *
  58. * @since 2.1.0
  59. * @access public
  60. *
  61. * @return array Widget keywords.
  62. */
  63. public function get_keywords() {
  64. return [ 'image', 'photo', 'visual', 'box' ];
  65. }
  66. /**
  67. * Register image box widget controls.
  68. *
  69. * Adds different input fields to allow the user to change and customize the widget settings.
  70. *
  71. * @since 1.0.0
  72. * @access protected
  73. */
  74. protected function _register_controls() {
  75. $this->start_controls_section(
  76. 'section_image',
  77. [
  78. 'label' => __( 'Image Box', 'elementor' ),
  79. ]
  80. );
  81. $this->add_control(
  82. 'image',
  83. [
  84. 'label' => __( 'Choose Image', 'elementor' ),
  85. 'type' => Controls_Manager::MEDIA,
  86. 'dynamic' => [
  87. 'active' => true,
  88. ],
  89. 'default' => [
  90. 'url' => Utils::get_placeholder_image_src(),
  91. ],
  92. ]
  93. );
  94. $this->add_group_control(
  95. Group_Control_Image_Size::get_type(),
  96. [
  97. 'name' => 'thumbnail', // Usage: `{name}_size` and `{name}_custom_dimension`, in this case `thumbnail_size` and `thumbnail_custom_dimension`.
  98. 'default' => 'full',
  99. 'separator' => 'none',
  100. ]
  101. );
  102. $this->add_control(
  103. 'title_text',
  104. [
  105. 'label' => __( 'Title & Description', 'elementor' ),
  106. 'type' => Controls_Manager::TEXT,
  107. 'dynamic' => [
  108. 'active' => true,
  109. ],
  110. 'default' => __( 'This is the heading', 'elementor' ),
  111. 'placeholder' => __( 'Enter your title', 'elementor' ),
  112. 'label_block' => true,
  113. ]
  114. );
  115. $this->add_control(
  116. 'description_text',
  117. [
  118. 'label' => __( 'Content', 'elementor' ),
  119. 'type' => Controls_Manager::TEXTAREA,
  120. 'dynamic' => [
  121. 'active' => true,
  122. ],
  123. 'default' => __( 'Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.', 'elementor' ),
  124. 'placeholder' => __( 'Enter your description', 'elementor' ),
  125. 'separator' => 'none',
  126. 'rows' => 10,
  127. 'show_label' => false,
  128. ]
  129. );
  130. $this->add_control(
  131. 'link',
  132. [
  133. 'label' => __( 'Link to', 'elementor' ),
  134. 'type' => Controls_Manager::URL,
  135. 'dynamic' => [
  136. 'active' => true,
  137. ],
  138. 'placeholder' => __( 'https://your-link.com', 'elementor' ),
  139. 'separator' => 'before',
  140. ]
  141. );
  142. $this->add_control(
  143. 'position',
  144. [
  145. 'label' => __( 'Image Position', 'elementor' ),
  146. 'type' => Controls_Manager::CHOOSE,
  147. 'default' => 'top',
  148. 'options' => [
  149. 'left' => [
  150. 'title' => __( 'Left', 'elementor' ),
  151. 'icon' => 'fa fa-align-left',
  152. ],
  153. 'top' => [
  154. 'title' => __( 'Top', 'elementor' ),
  155. 'icon' => 'fa fa-align-center',
  156. ],
  157. 'right' => [
  158. 'title' => __( 'Right', 'elementor' ),
  159. 'icon' => 'fa fa-align-right',
  160. ],
  161. ],
  162. 'prefix_class' => 'elementor-position-',
  163. 'toggle' => false,
  164. ]
  165. );
  166. $this->add_control(
  167. 'title_size',
  168. [
  169. 'label' => __( 'Title HTML Tag', 'elementor' ),
  170. 'type' => Controls_Manager::SELECT,
  171. 'options' => [
  172. 'h1' => 'H1',
  173. 'h2' => 'H2',
  174. 'h3' => 'H3',
  175. 'h4' => 'H4',
  176. 'h5' => 'H5',
  177. 'h6' => 'H6',
  178. 'div' => 'div',
  179. 'span' => 'span',
  180. 'p' => 'p',
  181. ],
  182. 'default' => 'h3',
  183. ]
  184. );
  185. $this->add_control(
  186. 'view',
  187. [
  188. 'label' => __( 'View', 'elementor' ),
  189. 'type' => Controls_Manager::HIDDEN,
  190. 'default' => 'traditional',
  191. ]
  192. );
  193. $this->end_controls_section();
  194. $this->start_controls_section(
  195. 'section_style_image',
  196. [
  197. 'label' => __( 'Image', 'elementor' ),
  198. 'tab' => Controls_Manager::TAB_STYLE,
  199. ]
  200. );
  201. $this->add_responsive_control(
  202. 'image_space',
  203. [
  204. 'label' => __( 'Spacing', 'elementor' ),
  205. 'type' => Controls_Manager::SLIDER,
  206. 'default' => [
  207. 'size' => 15,
  208. ],
  209. 'range' => [
  210. 'px' => [
  211. 'min' => 0,
  212. 'max' => 100,
  213. ],
  214. ],
  215. 'selectors' => [
  216. '{{WRAPPER}}.elementor-position-right .elementor-image-box-img' => 'margin-left: {{SIZE}}{{UNIT}};',
  217. '{{WRAPPER}}.elementor-position-left .elementor-image-box-img' => 'margin-right: {{SIZE}}{{UNIT}};',
  218. '{{WRAPPER}}.elementor-position-top .elementor-image-box-img' => 'margin-bottom: {{SIZE}}{{UNIT}};',
  219. '(mobile){{WRAPPER}} .elementor-image-box-img' => 'margin-bottom: {{SIZE}}{{UNIT}};',
  220. ],
  221. ]
  222. );
  223. $this->add_responsive_control(
  224. 'image_size',
  225. [
  226. 'label' => __( 'Width', 'elementor' ),
  227. 'type' => Controls_Manager::SLIDER,
  228. 'default' => [
  229. 'size' => 30,
  230. 'unit' => '%',
  231. ],
  232. 'tablet_default' => [
  233. 'unit' => '%',
  234. ],
  235. 'mobile_default' => [
  236. 'unit' => '%',
  237. ],
  238. 'size_units' => [ '%' ],
  239. 'range' => [
  240. '%' => [
  241. 'min' => 5,
  242. 'max' => 100,
  243. ],
  244. ],
  245. 'selectors' => [
  246. '{{WRAPPER}} .elementor-image-box-wrapper .elementor-image-box-img' => 'width: {{SIZE}}{{UNIT}};',
  247. ],
  248. ]
  249. );
  250. $this->add_control(
  251. 'hover_animation',
  252. [
  253. 'label' => __( 'Hover Animation', 'elementor' ),
  254. 'type' => Controls_Manager::HOVER_ANIMATION,
  255. ]
  256. );
  257. $this->start_controls_tabs( 'image_effects' );
  258. $this->start_controls_tab( 'normal',
  259. [
  260. 'label' => __( 'Normal', 'elementor' ),
  261. ]
  262. );
  263. $this->add_group_control(
  264. Group_Control_Css_Filter::get_type(),
  265. [
  266. 'name' => 'css_filters',
  267. 'selector' => '{{WRAPPER}} .elementor-image-box-img img',
  268. ]
  269. );
  270. $this->add_control(
  271. 'image_opacity',
  272. [
  273. 'label' => __( 'Opacity', 'elementor' ),
  274. 'type' => Controls_Manager::SLIDER,
  275. 'range' => [
  276. 'px' => [
  277. 'max' => 1,
  278. 'min' => 0.10,
  279. 'step' => 0.01,
  280. ],
  281. ],
  282. 'selectors' => [
  283. '{{WRAPPER}} .elementor-image-box-img img' => 'opacity: {{SIZE}};',
  284. ],
  285. ]
  286. );
  287. $this->add_control(
  288. 'background_hover_transition',
  289. [
  290. 'label' => __( 'Transition Duration', 'elementor' ),
  291. 'type' => Controls_Manager::SLIDER,
  292. 'default' => [
  293. 'size' => 0.3,
  294. ],
  295. 'range' => [
  296. 'px' => [
  297. 'max' => 3,
  298. 'step' => 0.1,
  299. ],
  300. ],
  301. 'selectors' => [
  302. '{{WRAPPER}} .elementor-image-box-img img' => 'transition-duration: {{SIZE}}s',
  303. ],
  304. ]
  305. );
  306. $this->end_controls_tab();
  307. $this->start_controls_tab( 'hover',
  308. [
  309. 'label' => __( 'Hover', 'elementor' ),
  310. ]
  311. );
  312. $this->add_group_control(
  313. Group_Control_Css_Filter::get_type(),
  314. [
  315. 'name' => 'css_filters_hover',
  316. 'selector' => '{{WRAPPER}}:hover .elementor-image-box-img img',
  317. ]
  318. );
  319. $this->add_control(
  320. 'image_opacity_hover',
  321. [
  322. 'label' => __( 'Opacity', 'elementor' ),
  323. 'type' => Controls_Manager::SLIDER,
  324. 'range' => [
  325. 'px' => [
  326. 'max' => 1,
  327. 'min' => 0.10,
  328. 'step' => 0.01,
  329. ],
  330. ],
  331. 'selectors' => [
  332. '{{WRAPPER}}:hover .elementor-image-box-img img' => 'opacity: {{SIZE}};',
  333. ],
  334. ]
  335. );
  336. $this->end_controls_tab();
  337. $this->end_controls_tabs();
  338. $this->end_controls_section();
  339. $this->start_controls_section(
  340. 'section_style_content',
  341. [
  342. 'label' => __( 'Content', 'elementor' ),
  343. 'tab' => Controls_Manager::TAB_STYLE,
  344. ]
  345. );
  346. $this->add_responsive_control(
  347. 'text_align',
  348. [
  349. 'label' => __( 'Alignment', 'elementor' ),
  350. 'type' => Controls_Manager::CHOOSE,
  351. 'options' => [
  352. 'left' => [
  353. 'title' => __( 'Left', 'elementor' ),
  354. 'icon' => 'fa fa-align-left',
  355. ],
  356. 'center' => [
  357. 'title' => __( 'Center', 'elementor' ),
  358. 'icon' => 'fa fa-align-center',
  359. ],
  360. 'right' => [
  361. 'title' => __( 'Right', 'elementor' ),
  362. 'icon' => 'fa fa-align-right',
  363. ],
  364. 'justify' => [
  365. 'title' => __( 'Justified', 'elementor' ),
  366. 'icon' => 'fa fa-align-justify',
  367. ],
  368. ],
  369. 'selectors' => [
  370. '{{WRAPPER}} .elementor-image-box-wrapper' => 'text-align: {{VALUE}};',
  371. ],
  372. ]
  373. );
  374. $this->add_control(
  375. 'content_vertical_alignment',
  376. [
  377. 'label' => __( 'Vertical Alignment', 'elementor' ),
  378. 'type' => Controls_Manager::SELECT,
  379. 'options' => [
  380. 'top' => __( 'Top', 'elementor' ),
  381. 'middle' => __( 'Middle', 'elementor' ),
  382. 'bottom' => __( 'Bottom', 'elementor' ),
  383. ],
  384. 'default' => 'top',
  385. 'prefix_class' => 'elementor-vertical-align-',
  386. ]
  387. );
  388. $this->add_control(
  389. 'heading_title',
  390. [
  391. 'label' => __( 'Title', 'elementor' ),
  392. 'type' => Controls_Manager::HEADING,
  393. 'separator' => 'before',
  394. ]
  395. );
  396. $this->add_responsive_control(
  397. 'title_bottom_space',
  398. [
  399. 'label' => __( 'Spacing', 'elementor' ),
  400. 'type' => Controls_Manager::SLIDER,
  401. 'range' => [
  402. 'px' => [
  403. 'min' => 0,
  404. 'max' => 100,
  405. ],
  406. ],
  407. 'selectors' => [
  408. '{{WRAPPER}} .elementor-image-box-title' => 'margin-bottom: {{SIZE}}{{UNIT}};',
  409. ],
  410. ]
  411. );
  412. $this->add_control(
  413. 'title_color',
  414. [
  415. 'label' => __( 'Color', 'elementor' ),
  416. 'type' => Controls_Manager::COLOR,
  417. 'default' => '',
  418. 'selectors' => [
  419. '{{WRAPPER}} .elementor-image-box-content .elementor-image-box-title' => 'color: {{VALUE}};',
  420. ],
  421. 'scheme' => [
  422. 'type' => Scheme_Color::get_type(),
  423. 'value' => Scheme_Color::COLOR_1,
  424. ],
  425. ]
  426. );
  427. $this->add_group_control(
  428. Group_Control_Typography::get_type(),
  429. [
  430. 'name' => 'title_typography',
  431. 'selector' => '{{WRAPPER}} .elementor-image-box-content .elementor-image-box-title',
  432. 'scheme' => Scheme_Typography::TYPOGRAPHY_1,
  433. ]
  434. );
  435. $this->add_control(
  436. 'heading_description',
  437. [
  438. 'label' => __( 'Description', 'elementor' ),
  439. 'type' => Controls_Manager::HEADING,
  440. 'separator' => 'before',
  441. ]
  442. );
  443. $this->add_control(
  444. 'description_color',
  445. [
  446. 'label' => __( 'Color', 'elementor' ),
  447. 'type' => Controls_Manager::COLOR,
  448. 'default' => '',
  449. 'selectors' => [
  450. '{{WRAPPER}} .elementor-image-box-content .elementor-image-box-description' => 'color: {{VALUE}};',
  451. ],
  452. 'scheme' => [
  453. 'type' => Scheme_Color::get_type(),
  454. 'value' => Scheme_Color::COLOR_3,
  455. ],
  456. ]
  457. );
  458. $this->add_group_control(
  459. Group_Control_Typography::get_type(),
  460. [
  461. 'name' => 'description_typography',
  462. 'selector' => '{{WRAPPER}} .elementor-image-box-content .elementor-image-box-description',
  463. 'scheme' => Scheme_Typography::TYPOGRAPHY_3,
  464. ]
  465. );
  466. $this->end_controls_section();
  467. }
  468. /**
  469. * Render image box widget output on the frontend.
  470. *
  471. * Written in PHP and used to generate the final HTML.
  472. *
  473. * @since 1.0.0
  474. * @access protected
  475. */
  476. protected function render() {
  477. $settings = $this->get_settings_for_display();
  478. $has_content = ! empty( $settings['title_text'] ) || ! empty( $settings['description_text'] );
  479. $html = '<div class="elementor-image-box-wrapper">';
  480. if ( ! empty( $settings['link']['url'] ) ) {
  481. $this->add_render_attribute( 'link', 'href', $settings['link']['url'] );
  482. if ( $settings['link']['is_external'] ) {
  483. $this->add_render_attribute( 'link', 'target', '_blank' );
  484. }
  485. if ( ! empty( $settings['link']['nofollow'] ) ) {
  486. $this->add_render_attribute( 'link', 'rel', 'nofollow' );
  487. }
  488. }
  489. if ( ! empty( $settings['image']['url'] ) ) {
  490. $this->add_render_attribute( 'image', 'src', $settings['image']['url'] );
  491. $this->add_render_attribute( 'image', 'alt', Control_Media::get_image_alt( $settings['image'] ) );
  492. $this->add_render_attribute( 'image', 'title', Control_Media::get_image_title( $settings['image'] ) );
  493. if ( $settings['hover_animation'] ) {
  494. $this->add_render_attribute( 'image', 'class', 'elementor-animation-' . $settings['hover_animation'] );
  495. }
  496. $image_html = Group_Control_Image_Size::get_attachment_image_html( $settings, 'thumbnail', 'image' );
  497. if ( ! empty( $settings['link']['url'] ) ) {
  498. $image_html = '<a ' . $this->get_render_attribute_string( 'link' ) . '>' . $image_html . '</a>';
  499. }
  500. $html .= '<figure class="elementor-image-box-img">' . $image_html . '</figure>';
  501. }
  502. if ( $has_content ) {
  503. $html .= '<div class="elementor-image-box-content">';
  504. if ( ! empty( $settings['title_text'] ) ) {
  505. $this->add_render_attribute( 'title_text', 'class', 'elementor-image-box-title' );
  506. $this->add_inline_editing_attributes( 'title_text', 'none' );
  507. $title_html = $settings['title_text'];
  508. if ( ! empty( $settings['link']['url'] ) ) {
  509. $title_html = '<a ' . $this->get_render_attribute_string( 'link' ) . '>' . $title_html . '</a>';
  510. }
  511. $html .= sprintf( '<%1$s %2$s>%3$s</%1$s>', $settings['title_size'], $this->get_render_attribute_string( 'title_text' ), $title_html );
  512. }
  513. if ( ! empty( $settings['description_text'] ) ) {
  514. $this->add_render_attribute( 'description_text', 'class', 'elementor-image-box-description' );
  515. $this->add_inline_editing_attributes( 'description_text' );
  516. $html .= sprintf( '<p %1$s>%2$s</p>', $this->get_render_attribute_string( 'description_text' ), $settings['description_text'] );
  517. }
  518. $html .= '</div>';
  519. }
  520. $html .= '</div>';
  521. echo $html;
  522. }
  523. /**
  524. * Render image box widget output in the editor.
  525. *
  526. * Written as a Backbone JavaScript template and used to generate the live preview.
  527. *
  528. * @since 1.0.0
  529. * @access protected
  530. */
  531. protected function _content_template() {
  532. ?>
  533. <#
  534. var html = '<div class="elementor-image-box-wrapper">';
  535. if ( settings.image.url ) {
  536. var image = {
  537. id: settings.image.id,
  538. url: settings.image.url,
  539. size: settings.thumbnail_size,
  540. dimension: settings.thumbnail_custom_dimension,
  541. model: view.getEditModel()
  542. };
  543. var image_url = elementor.imagesManager.getImageUrl( image );
  544. var imageHtml = '<img src="' + image_url + '" class="elementor-animation-' + settings.hover_animation + '" />';
  545. if ( settings.link.url ) {
  546. imageHtml = '<a href="' + settings.link.url + '">' + imageHtml + '</a>';
  547. }
  548. html += '<figure class="elementor-image-box-img">' + imageHtml + '</figure>';
  549. }
  550. var hasContent = !! ( settings.title_text || settings.description_text );
  551. if ( hasContent ) {
  552. html += '<div class="elementor-image-box-content">';
  553. if ( settings.title_text ) {
  554. var title_html = settings.title_text;
  555. if ( settings.link.url ) {
  556. title_html = '<a href="' + settings.link.url + '">' + title_html + '</a>';
  557. }
  558. view.addRenderAttribute( 'title_text', 'class', 'elementor-image-box-title' );
  559. view.addInlineEditingAttributes( 'title_text', 'none' );
  560. html += '<' + settings.title_size + ' ' + view.getRenderAttributeString( 'title_text' ) + '>' + title_html + '</' + settings.title_size + '>';
  561. }
  562. if ( settings.description_text ) {
  563. view.addRenderAttribute( 'description_text', 'class', 'elementor-image-box-description' );
  564. view.addInlineEditingAttributes( 'description_text' );
  565. html += '<p ' + view.getRenderAttributeString( 'description_text' ) + '>' + settings.description_text + '</p>';
  566. }
  567. html += '</div>';
  568. }
  569. html += '</div>';
  570. print( html );
  571. #>
  572. <?php
  573. }
  574. }