counter.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor counter widget.
  8. *
  9. * Elementor widget that displays stats and numbers in an escalating manner.
  10. *
  11. * @since 1.0.0
  12. */
  13. class Widget_Counter extends Widget_Base {
  14. /**
  15. * Get widget name.
  16. *
  17. * Retrieve counter 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 'counter';
  26. }
  27. /**
  28. * Get widget title.
  29. *
  30. * Retrieve counter 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 __( 'Counter', 'elementor' );
  39. }
  40. /**
  41. * Get widget icon.
  42. *
  43. * Retrieve counter 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-counter';
  52. }
  53. /**
  54. * Retrieve the list of scripts the counter widget depended on.
  55. *
  56. * Used to set scripts dependencies required to run the widget.
  57. *
  58. * @since 1.3.0
  59. * @access public
  60. *
  61. * @return array Widget scripts dependencies.
  62. */
  63. public function get_script_depends() {
  64. return [ 'jquery-numerator' ];
  65. }
  66. /**
  67. * Get widget keywords.
  68. *
  69. * Retrieve the list of keywords the widget belongs to.
  70. *
  71. * @since 2.1.0
  72. * @access public
  73. *
  74. * @return array Widget keywords.
  75. */
  76. public function get_keywords() {
  77. return [ 'counter' ];
  78. }
  79. /**
  80. * Register counter widget controls.
  81. *
  82. * Adds different input fields to allow the user to change and customize the widget settings.
  83. *
  84. * @since 1.0.0
  85. * @access protected
  86. */
  87. protected function _register_controls() {
  88. $this->start_controls_section(
  89. 'section_counter',
  90. [
  91. 'label' => __( 'Counter', 'elementor' ),
  92. ]
  93. );
  94. $this->add_control(
  95. 'starting_number',
  96. [
  97. 'label' => __( 'Starting Number', 'elementor' ),
  98. 'type' => Controls_Manager::NUMBER,
  99. 'default' => 0,
  100. ]
  101. );
  102. $this->add_control(
  103. 'ending_number',
  104. [
  105. 'label' => __( 'Ending Number', 'elementor' ),
  106. 'type' => Controls_Manager::NUMBER,
  107. 'default' => 100,
  108. ]
  109. );
  110. $this->add_control(
  111. 'prefix',
  112. [
  113. 'label' => __( 'Number Prefix', 'elementor' ),
  114. 'type' => Controls_Manager::TEXT,
  115. 'default' => '',
  116. 'placeholder' => 1,
  117. ]
  118. );
  119. $this->add_control(
  120. 'suffix',
  121. [
  122. 'label' => __( 'Number Suffix', 'elementor' ),
  123. 'type' => Controls_Manager::TEXT,
  124. 'default' => '',
  125. 'placeholder' => __( 'Plus', 'elementor' ),
  126. ]
  127. );
  128. $this->add_control(
  129. 'duration',
  130. [
  131. 'label' => __( 'Animation Duration', 'elementor' ),
  132. 'type' => Controls_Manager::NUMBER,
  133. 'default' => 2000,
  134. 'min' => 100,
  135. 'step' => 100,
  136. ]
  137. );
  138. $this->add_control(
  139. 'thousand_separator',
  140. [
  141. 'label' => __( 'Thousand Separator', 'elementor' ),
  142. 'type' => Controls_Manager::SWITCHER,
  143. 'default' => 'yes',
  144. 'label_on' => __( 'Show', 'elementor' ),
  145. 'label_off' => __( 'Hide', 'elementor' ),
  146. ]
  147. );
  148. $this->add_control(
  149. 'thousand_separator_char',
  150. [
  151. 'label' => __( 'Separator', 'elementor' ),
  152. 'type' => Controls_Manager::SELECT,
  153. 'condition' => [
  154. 'thousand_separator' => 'yes',
  155. ],
  156. 'options' => [
  157. '' => 'Default',
  158. '.' => 'Dot',
  159. ' ' => 'Space',
  160. ],
  161. ]
  162. );
  163. $this->add_control(
  164. 'title',
  165. [
  166. 'label' => __( 'Title', 'elementor' ),
  167. 'type' => Controls_Manager::TEXT,
  168. 'label_block' => true,
  169. 'default' => __( 'Cool Number', 'elementor' ),
  170. 'placeholder' => __( 'Cool Number', 'elementor' ),
  171. ]
  172. );
  173. $this->add_control(
  174. 'view',
  175. [
  176. 'label' => __( 'View', 'elementor' ),
  177. 'type' => Controls_Manager::HIDDEN,
  178. 'default' => 'traditional',
  179. ]
  180. );
  181. $this->end_controls_section();
  182. $this->start_controls_section(
  183. 'section_number',
  184. [
  185. 'label' => __( 'Number', 'elementor' ),
  186. 'tab' => Controls_Manager::TAB_STYLE,
  187. ]
  188. );
  189. $this->add_control(
  190. 'number_color',
  191. [
  192. 'label' => __( 'Text Color', 'elementor' ),
  193. 'type' => Controls_Manager::COLOR,
  194. 'scheme' => [
  195. 'type' => Scheme_Color::get_type(),
  196. 'value' => Scheme_Color::COLOR_1,
  197. ],
  198. 'selectors' => [
  199. '{{WRAPPER}} .elementor-counter-number-wrapper' => 'color: {{VALUE}};',
  200. ],
  201. ]
  202. );
  203. $this->add_group_control(
  204. Group_Control_Typography::get_type(),
  205. [
  206. 'name' => 'typography_number',
  207. 'scheme' => Scheme_Typography::TYPOGRAPHY_1,
  208. 'selector' => '{{WRAPPER}} .elementor-counter-number-wrapper',
  209. ]
  210. );
  211. $this->end_controls_section();
  212. $this->start_controls_section(
  213. 'section_title',
  214. [
  215. 'label' => __( 'Title', 'elementor' ),
  216. 'tab' => Controls_Manager::TAB_STYLE,
  217. ]
  218. );
  219. $this->add_control(
  220. 'title_color',
  221. [
  222. 'label' => __( 'Text Color', 'elementor' ),
  223. 'type' => Controls_Manager::COLOR,
  224. 'scheme' => [
  225. 'type' => Scheme_Color::get_type(),
  226. 'value' => Scheme_Color::COLOR_2,
  227. ],
  228. 'selectors' => [
  229. '{{WRAPPER}} .elementor-counter-title' => 'color: {{VALUE}};',
  230. ],
  231. ]
  232. );
  233. $this->add_group_control(
  234. Group_Control_Typography::get_type(),
  235. [
  236. 'name' => 'typography_title',
  237. 'scheme' => Scheme_Typography::TYPOGRAPHY_2,
  238. 'selector' => '{{WRAPPER}} .elementor-counter-title',
  239. ]
  240. );
  241. $this->end_controls_section();
  242. }
  243. /**
  244. * Render counter widget output in the editor.
  245. *
  246. * Written as a Backbone JavaScript template and used to generate the live preview.
  247. *
  248. * @since 1.0.0
  249. * @access protected
  250. */
  251. protected function _content_template() {
  252. ?>
  253. <div class="elementor-counter">
  254. <div class="elementor-counter-number-wrapper">
  255. <span class="elementor-counter-number-prefix">{{{ settings.prefix }}}</span>
  256. <span class="elementor-counter-number" data-duration="{{ settings.duration }}" data-to-value="{{ settings.ending_number }}" data-delimiter="{{ settings.thousand_separator ? settings.thousand_separator_char || ',' : '' }}">{{{ settings.starting_number }}}</span>
  257. <span class="elementor-counter-number-suffix">{{{ settings.suffix }}}</span>
  258. </div>
  259. <# if ( settings.title ) {
  260. #><div class="elementor-counter-title">{{{ settings.title }}}</div><#
  261. } #>
  262. </div>
  263. <?php
  264. }
  265. /**
  266. * Render counter widget output on the frontend.
  267. *
  268. * Written in PHP and used to generate the final HTML.
  269. *
  270. * @since 1.0.0
  271. * @access protected
  272. */
  273. protected function render() {
  274. $settings = $this->get_settings_for_display();
  275. $this->add_render_attribute( 'counter', [
  276. 'class' => 'elementor-counter-number',
  277. 'data-duration' => $settings['duration'],
  278. 'data-to-value' => $settings['ending_number'],
  279. ] );
  280. if ( ! empty( $settings['thousand_separator'] ) ) {
  281. $delimiter = empty( $settings['thousand_separator_char'] ) ? ',' : $settings['thousand_separator_char'];
  282. $this->add_render_attribute( 'counter', 'data-delimiter', $delimiter );
  283. }
  284. ?>
  285. <div class="elementor-counter">
  286. <div class="elementor-counter-number-wrapper">
  287. <span class="elementor-counter-number-prefix"><?php echo $settings['prefix']; ?></span>
  288. <span <?php echo $this->get_render_attribute_string( 'counter' ); ?>><?php echo $settings['starting_number']; ?></span>
  289. <span class="elementor-counter-number-suffix"><?php echo $settings['suffix']; ?></span>
  290. </div>
  291. <?php if ( $settings['title'] ) : ?>
  292. <div class="elementor-counter-title"><?php echo $settings['title']; ?></div>
  293. <?php endif; ?>
  294. </div>
  295. <?php
  296. }
  297. }