<?php

//TODO:
//-Option to fade in counter when scrolling (like original counter)
//-Setting to exclude taxonomies from counting posts




function jt_divi_counter_module() {

  if (class_exists('JT_Builder_Module_1_0')) {

    class ET_Builder_Module_JT_Counter extends JT_Builder_Module_1_0 {

      function init() {
        $this->name = esc_html__( 'Counter', 'et_builder' );
        $this->slug = 'et_pb_jt_counter';
        $this->main_css_element = '%%order_class%%.et_pb_jt_counter';


        //Add module "default" fields: background, margin, padding, border, module_id, module_class
        $this->jt_add_module_default_options();

        //Add light/Dark text setting
        $this->jt_add_background_layout_option();

        //Add options toggles
        $this->jt_add_general_options_toggle('text', 'Text');
        $this->jt_add_general_options_toggle('counter', 'Counter');
        $this->jt_add_general_options_toggle('count_to', 'Count To');
        $this->jt_add_general_options_toggle('count_from', 'Count From');
        $this->jt_add_advanced_options_toggle('text', 'Text');
        $this->jt_add_advanced_options_toggle('counter', 'Counter');
        $this->jt_add_advanced_options_toggle('counter_circle', 'Circle');

        $this->jt_add_field( 'prefix', array(
            'label'           => esc_html__( 'Prefix', 'et_builder' ),
            'type'            => 'text',
            'option_category' => 'configuration',
            'toggle_slug'     => 'text',
            'description'     => esc_html__( 'Text to the left of the number. By default uses the same text style as the number.', 'et_builder' ),
          )
        );
        $this->jt_add_field( 'suffix', array(
            'label'           => esc_html__( 'Suffix', 'et_builder' ),
            'type'            => 'text',
            'option_category' => 'configuration',
            'toggle_slug'     => 'text',
            'description'     => esc_html__( 'Text to the right of the number. By default uses the same text style as the number.', 'et_builder' ),
          )
        );

        $this->jt_add_advanced_fonts_option( 'number', array(
            'label'    => esc_html__( 'Number', 'et_builder' ),
            'css'      => array(
              'main'        => "{$this->main_css_element} .et_pb_jt_counter_number",
              'important' => 'all',
            ),
            'line_height' => array(
              'default' => '1em',
              'range_settings' => array(
                'min'  => '0.1',
                'max'  => '10',
                'step' => '0.1',
              ),
              // 'default' => '1em',
            ),
  					'font_size' => array(
  						'default' => et_get_option( 'et_pb_jt_counter-number_font_size', '5' )  . 'em' ,
              'range_settings' => array(
                'min'  => '0.1',
                'max'  => '10',
                'step' => '0.1',
              ),
  					),
          )
        );

        //Text align Settings
        $this->jt_add_field( 'text_align', array(
            'label'           => esc_html__( 'Text Alignment', 'et_builder' ),
            'type'            => 'select',
            'option_category' => 'layout',
            'options'         => et_builder_get_text_orientation_options(),
            'tab_slug'        => 'advanced',
            'toggle_slug'     => 'text',
            'default'         => 'center',
            'description'     => esc_html__( 'How the text is aligned.', 'et_builder' ),
          ), array( 'center' )
        );


        //Count to section
        //Count to type
        $this->jt_add_field( 'counter_type', array(
            'label'           => esc_html__( 'Counter Type', 'et_builder' ),
            'type'            => 'select',
            'option_category' => 'configuration',
            'default'         => 'number',
            'options'         => array(
              'number'  => esc_html__( 'Number', 'et_builder' ),
              'circle'  => esc_html__( 'Circle', 'et_builder' ),
            ),
            'toggle_slug'     => 'counter',
            'description'     => esc_html__( 'Choose what counter you want to use.', 'et_builder' ),
            'affects'     => array(
              'count_circle_percent',
              'circle_bar_color',
              'circle_track_color',
              'circle_line_width',
              'circle_line_cap',
              'circle_size',
              'circle_use_scale',
              'circle_rotate',
            ),
          ), array('number')
        );
        $this->jt_add_field( 'count_circle_percent', array(
          'label'             => esc_html__( 'Circle Percent', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'type'              => 'text',
          'option_category'   => 'configuration',
          'value_type'        => 'int',
          'toggle_slug'       => 'counter',
          'value_min'         => 0,
          'value_max'         => 100,
          'number_validation' => true,
          'description'       => esc_html__( "Percent value of how much to fill the circle.", 'et_builder' ),
          )
        );
        $this->jt_add_field( 'count_to_type', array(
            'label'           => esc_html__( 'Count', 'et_builder' ),
            'type'            => 'select',
            'option_category' => 'configuration',
            'default'         => 'number',
            'options'         => array(
              'number'  => esc_html__( 'Numbers', 'et_builder' ),
              'date'    => esc_html__( 'Dates', 'et_builder' ),
              'post'    => esc_html__( 'Posts', 'et_builder' ),
            ),
            'toggle_slug'     => 'counter',
            'description'     => esc_html__( 'Choose what you want to count.', 'et_builder' ),
            'affects'     => array(
    					'count_to_number',
              'count_to_date',
              'count_to_include_date',
              'count_to_post',
              'count_to_post_offset',

              'count_from_number',
              'count_from_date',
              'count_from_include_date',
              'count_date_type',
              'count_number_decimals',
    				),
          ), array('number')
        );
        $this->jt_add_field( 'count_number_decimals', array(
          'label'             => esc_html__( 'Force Decimal Places', 'et_builder' ),
          'depends_show_if'   => 'number',
          'type'              => 'text',
          'option_category'   => 'basic_option',
          'value_type'        => 'int',
          'value_min'         => 0,
  				'value_max'         => 100,
          'number_validation' => true,
          'toggle_slug'       => 'counter',
          'description'       => esc_html__( "Force the counter to show decimal places. Leave empty to let the counter automatically determine if and how many decimal places to show.", 'et_builder' ),
          )
        );

        $this->jt_add_field( 'count_number_separator', array(
          'label'             => esc_html__( 'Thousands Separator', 'et_builder' ),
          'type'              => 'text',
          'option_category'   => 'basic_option',
          'toggle_slug'       => 'counter',
          'description'       => esc_html__( 'The separator to be used for separating thousands (e. g. "," to get "1,000" instead of "1000")', 'et_builder' ),
          )
        );


        //Count to text field
        $this->jt_add_field( 'count_to_number', array(
          'label'             => esc_html__( 'Number', 'et_builder' ),
          'depends_show_if'   => 'number',
          'type'              => 'text',
          'option_category'   => 'configuration',
          'value_type'        => 'int',
          // 'number_validation' => true,
          'toggle_slug'       => 'count_to',
          'description'       => esc_html__( "The number to count to. Leave empty to use 0 as the target number.", 'et_builder' ),
          )
        );

        //Count to date field
        $this->jt_add_field( 'count_to_date', array(
          'label'           => esc_html__( 'Date', 'et_builder' ),
          'depends_show_if' => 'date',
          'type'            => 'date_picker',
          'option_category' => 'configuration',
          'toggle_slug'     => 'count_to',
          'description'     => esc_html__( 'The date the counter is counting to. Leave empty to use the current date.', 'et_builder' ),
          )
        );
        $this->jt_add_field( 'count_to_include_date', array(
            'label'             => esc_html__( 'Include Date when counting', 'et_builder' ),
            'depends_show_if'   => 'date',
            'type'              => 'yes_no_button',
            'option_category'   => 'configuration',
            'options'           => array(
              'off' => esc_html__( 'Off', 'et_builder' ),
              'on'  => esc_html__( 'On', 'et_builder' ),
            ),
            'toggle_slug'       => 'count_to',
            'description'       => esc_html__( 'When activated, adds one period worth of time to the date before calculating the time difference.', 'et_builder' ),
          ), array('off')
        );
        $this->jt_add_field( 'count_date_type', array(
            'label'           => esc_html__( 'Type', 'et_builder' ),
            'depends_show_if' => 'date',
            'type'            => 'select',
            'option_category' => 'configuration',
            'toggle_slug'     => 'counter',
            'default'         => 'days',
            'options'         => array(
              'seconds'  				=> esc_html__( 'Seconds', 'et_builder' ),
              'minutes' 				=> esc_html__( 'Minutes', 'et_builder' ),
              'hours' 					=> esc_html__( 'Hours', 	'et_builder' ),
              'days' 						=> esc_html__( 'Days', 		'et_builder' ),
              'weeks' 					=> esc_html__( 'Weeks', 	'et_builder' ),
              'month' 					=> esc_html__( 'Month', 	'et_builder' ),
              'years' 					=> esc_html__( 'Years', 	'et_builder' ),
            ),
            'description'     => esc_html__( 'The period the counter should count (e. g. days or years).', 'et_builder' ),
          ), array('days')
        );
        //TODO: Y/N switch to activate continous counting (so counting seconds makes sense)

        //Count to posts, pages and other post types
        $this->jt_add_field( 'count_to_post', array(
          'label'           => esc_html__( 'Post Types to count', 'et_builder' ),
          'depends_show_if' => 'post',
  				'type'            => 'multiple_checkboxes',
  				'options'         => self::get_post_types_to_count(),
  				'additional_att'  => 'disable_on',
  				'option_category' => 'configuration',
  				'description'     => esc_html__( 'Choose which post types to count', 'et_builder' ),
  				'toggle_slug'     => 'count_to',
          )
        );
        $this->jt_add_field( 'count_to_post_offset', array(
          'label'             => esc_html__( 'Offset', 'et_builder' ),
          'depends_show_if'   => 'post',
  				'type'              => 'text',
  				'option_category'   => 'configuration',
  				'value_type'        => 'int',
  				'toggle_slug'       => 'count_to',
  				'description'       => esc_html__( "Offset to add or substract to/from the count. Positive numbers will be added, negative numbers will be substracted.", 'et_builder' ),
          )
        );



        //Count from section
        //Count from text field
        $this->jt_add_field( 'count_from_number', array(
          'label'             => esc_html__( 'Number', 'et_builder' ),
          'depends_show_if'   => 'number',
          'type'              => 'text',
          'option_category'   => 'configuration',
          'value_type'        => 'int',
          // 'number_validation' => true,
          'toggle_slug'       => 'count_from',
          'description'       => esc_html__( "The number to count from. Leave empty to use 0 as the starting number.", 'et_builder' ),
          )
        );

        //Count from date field
        $this->jt_add_field( 'count_from_date', array(
          'label'           => esc_html__( 'Date', 'et_builder' ),
          'depends_show_if' => 'date',
          'type'            => 'date_picker',
          'option_category' => 'configuration',
          'toggle_slug'     => 'count_from',
          'description'     => esc_html__( 'The date the counter is counting from. Leave empty to use the current date.', 'et_builder' ),
          )
        );
        $this->jt_add_field( 'count_from_include_date', array(
            'label'             => esc_html__( 'Include Date when counting', 'et_builder' ),
            'depends_show_if'   => 'date',
            'type'              => 'yes_no_button',
            'option_category'   => 'configuration',
            'options'           => array(
              'off' => esc_html__( 'Off', 'et_builder' ),
              'on'  => esc_html__( 'On', 'et_builder' ),
            ),
            'toggle_slug'       => 'count_from',
            'description'       => esc_html__( 'When activated, adds one period worth of time to the date before calculating the time difference.', 'et_builder' ),
          ), array('off')
        );


        //Count duration
        $this->jt_add_field( 'count_duration', array(
          'label'           => esc_html__( 'Animation Duration', 'et_builder' ),
  				'type'            => 'range',
  				'option_category' => 'layout',
          'default'         => '2',
  				'range_settings'  => array(
  					'min'  => '0.1',
  					'max'  => '10.0',
  					'step' => '0.1',
  				),
  				'tab_slug'        => 'advanced',
  				'toggle_slug'     => 'counter',
  				'validate_unit'   => false,
          'description'     => esc_html__( "Duration of the count animation.", 'et_builder' ),
        ), array( '2' ) );




        //-Setting für Circle counter:
        $this->jt_add_field( 'circle_bar_color', array(
          'label'             => esc_html__( 'Bar Color ', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'color-alpha',
          'custom_color'      => true,
          'description'       => esc_html__( 'Color of the counter bar.', 'et_builder' ),
        ) );
        $this->jt_add_field( 'circle_track_color', array(
          'label'             => esc_html__( 'Track Color ', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'color-alpha',
          'custom_color'      => true,
          'description'       => esc_html__( 'Color of the background circle.', 'et_builder' ),
        ) );
        $this->jt_add_field( 'circle_line_width', array(
          'label'             => esc_html__( 'Line Width ', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'range',
          'default'           => '5',
          'range_settings'  => array(
						'min'  => '1',
						'max'  => '100',
						'step' => '1',
					),
          'description'     => esc_html__( 'Circle line width. You can create pie charts using line cap "butt" and double the size of the line width.', 'et_builder' ),
        ), array( '5' ) );
        $this->jt_add_field( 'circle_line_cap', array(
          'label'             => esc_html__( 'Line Cap ', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'select',
          'default'           => 'round',
          'options'           => array(
            'round'   => esc_html__( 'Round', 'et_builder' ),
            'square'  => esc_html__( 'Square', 'et_builder' ),
            'butt'    => esc_html__( 'Butt', 'et_builder' ),
          ),
          'description'     => esc_html__( 'Line cap of the bar.', 'et_builder' ),
        ), array( 'round' ) );
        $this->jt_add_field( 'circle_size', array(
          'label'             => esc_html__( 'Circle Size', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'range',
          'range_settings'  => array(
						'min'  => '10',
						'max'  => '500',
						'step' => '1',
					),
          'description'       => esc_html__( 'Set to use a fixed size for the circle or leave empty to make the circle size responsive. Divis default circle counter uses a fixed size of 225.', 'et_builder' ),
        ) );
        $this->jt_add_field( 'circle_rotate', array(
          'label'             => esc_html__( 'Circle Rotate', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'range',
          'default'           => '0',
          'range_settings'    => array(
            'min'  => '0',
            'max'  => '360',
            'step' => '1',
          ),
          'description'       => esc_html__( 'Rotate the circle to offset the starting position.', 'et_builder' ),
        ), array( '0' ) );
        $this->jt_add_field( 'circle_use_scale', array(
          'label'             => esc_html__( 'Use Scale', 'et_builder' ),
          'depends_show_if'   => 'circle',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'yes_no_button',
          'default'           => 'off',
          'options'           => array(
            'off' => esc_html__( 'Off', 'et_builder' ),
            'on'  => esc_html__( 'On', 'et_builder' ),
          ),
          'affects'           => array(
            'circle_scale_length',
            'circle_scale_color',
          ),
          'description'       => esc_html__( 'Whether or not to show scale lines on around the circle.', 'et_builder' ),
        ), array( 'off' ) );
        $this->jt_add_field( 'circle_scale_length', array(
          'label'             => esc_html__( 'Scale Length', 'et_builder' ),
          'depends_show_if'   => 'on',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'range',
          'default'           => '5',
          'range_settings'    => array(
            'min'  => '1',
            'max'  => '20',
            'step' => '1',
          ),
          'description'       => esc_html__( 'Length of the scale lines.', 'et_builder' ),
        ), array( '5' ) );
        $this->jt_add_field( 'circle_scale_color', array(
          'label'             => esc_html__( 'Scale Color', 'et_builder' ),
          'depends_show_if'   => 'on',
          'toggle_slug'       => 'counter_circle',
          'option_category'   => 'layout',
          'tab_slug'          => 'advanced',
          'type'              => 'color-alpha',
          'custom_color'      => true,
          'description'       => esc_html__( 'Color of the scale lines.', 'et_builder' ),
        ) );




        $this->jt_custom_css_options['number_before'] = array(
          'label'    => esc_html__( 'Number Container (Before)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number:before',
        );
        $this->jt_custom_css_options['number'] = array(
          'label'    => esc_html__( 'Number Container', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number',
        );
        $this->jt_custom_css_options['number_after'] = array(
          'label'    => esc_html__( 'Number Container (After)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number:after',
        );

        $this->jt_custom_css_options['number_prefix_before'] = array(
          'label'    => esc_html__( 'Number Prefix (Before)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_prefix:before',
        );
        $this->jt_custom_css_options['number_prefix'] = array(
          'label'    => esc_html__( 'Number Prefix', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_prefix',
        );
        $this->jt_custom_css_options['number_prefix_after'] = array(
          'label'    => esc_html__( 'Number Prefix (After)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_prefix:after',
        );

        $this->jt_custom_css_options['number_number_before'] = array(
          'label'    => esc_html__( 'Number (Before)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_number:before',
        );
        $this->jt_custom_css_options['number_number'] = array(
          'label'    => esc_html__( 'Number', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_number',
        );
        $this->jt_custom_css_options['number_number_after'] = array(
          'label'    => esc_html__( 'Number (After)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_number:after',
        );

        $this->jt_custom_css_options['number_suffix_before'] = array(
          'label'    => esc_html__( 'Number Suffix (Before)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_suffix:before',
        );
        $this->jt_custom_css_options['number_suffix'] = array(
          'label'    => esc_html__( 'Number Suffix', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_suffix',
        );
        $this->jt_custom_css_options['number_suffix_after'] = array(
          'label'    => esc_html__( 'Number Suffix (After)', 'et_builder' ),
          'selector' => '.et_pb_jt_counter_number_suffix:after',
        );



        $this->jt_custom_css_options['module_hover'] = array(
          'label'    => esc_html__( 'Main Element (Module Hover)', 'et_builder' ),
          'selector' => ':hover',
          'no_space_before_selector' => true,
        );
        $this->jt_custom_css_options['number_module_hover'] = array(
          'label'    => esc_html__( 'Number Container (Module Hover)', 'et_builder' ),
          'selector' => ':hover .et_pb_jt_counter_number',
          'no_space_before_selector' => true,
        );
        $this->jt_custom_css_options['number_prefix_module_hover'] = array(
          'label'    => esc_html__( 'Number Prefix (Module Hover)', 'et_builder' ),
          'selector' => ':hover .et_pb_jt_counter_number_prefix',
          'no_space_before_selector' => true,
        );
        $this->jt_custom_css_options['number_number_module_hover'] = array(
          'label'    => esc_html__( 'Number (Module Hover)', 'et_builder' ),
          'selector' => ':hover .et_pb_jt_counter_number_number',
          'no_space_before_selector' => true,
        );
        $this->jt_custom_css_options['number_suffix_module_hover'] = array(
          'label'    => esc_html__( 'Number Suffix (Module Hover)', 'et_builder' ),
          'selector' => ':hover .et_pb_jt_counter_number_suffix',
          'no_space_before_selector' => true,
        );

        parent::init();
      }

      function shortcode_callback( $atts, $content = null, $function_name ) {
        wp_enqueue_script( 'easypiechart' );
        $prefix                   = $this->shortcode_atts['prefix'];
        $suffix                   = $this->shortcode_atts['suffix'];
        $count_duration           = $this->shortcode_atts['count_duration'];
        $text_align               = $this->shortcode_atts['text_align'];
        $count_to_type            = $this->shortcode_atts['count_to_type'];
        $counter_type             = $this->shortcode_atts['counter_type'];
        $count_circle_percent     = $this->shortcode_atts['count_circle_percent'];

        $count_to_number          = $this->shortcode_atts['count_to_number'];
        $count_number_decimals    = $this->shortcode_atts['count_number_decimals'];
        $count_from_number        = $this->shortcode_atts['count_from_number'];

        $count_date_type          = $this->shortcode_atts['count_date_type'];
        $count_to_date            = $this->shortcode_atts['count_to_date'];
        $count_to_include_date    = $this->shortcode_atts['count_to_include_date'];
        $count_from_date          = $this->shortcode_atts['count_from_date'];
        $count_from_include_date  = $this->shortcode_atts['count_from_include_date'];

        $count_to_post            = $this->shortcode_atts['count_to_post'];
        $count_to_post_offset     = $this->shortcode_atts['count_to_post_offset'];




        //Apply Text Alignment
        ET_Builder_Element::set_style( $function_name, array(
          'selector'    => '%%order_class%%',
          'declaration' => sprintf(
            'text-align: %1$s;',
            esc_attr( $this->shortcode_atts['text_align'] )
          )
        ) );



        //Apply number style
        $number_text_color  = $this->shortcode_atts['number_text_color'];
        $number_text_color  = isset( $number_text_color ) && '' !== $number_text_color ? $number_text_color : et_get_option( 'et_pb_jt_counter-number_text_color', et_builder_accent_color() );
        $number_font_size   = $this->shortcode_atts['number_font_size'];
        $number_font_size  = isset( $number_font_size ) && '' !== $number_font_size ? $number_font_size : et_get_option( 'et_pb_jt_counter-number_font_size', '5em' );
        $number_line_height = $this->shortcode_atts['number_line_height'];
        ET_Builder_Element::set_style( $function_name, array(
          'selector'    => '%%order_class%% .et_pb_jt_counter_number',
          'declaration' => sprintf(
            'color: %1$s;
             font-size: %2$s;
             line-height: %3$s;',
            esc_attr( $number_text_color ),
            esc_attr( $number_font_size ),
            esc_attr( $this->shortcode_atts['number_line_height'] )
          )
        ) );



        //Calculate the numbers to count
        $count_to_value   = 0;
        $count_from_value = 0;

        if ( 'number' == $count_to_type ) {
          $count_to_value   = $count_to_number;
          $count_from_value = $count_from_number;
        } else if ( 'date' == $count_to_type ) {

          $count_to_date    = '' !== $count_to_date   ? strtotime( $count_to_date )   : current_time( 'timestamp' );
          $count_from_date  = '' !== $count_from_date ? strtotime( $count_from_date ) : current_time( 'timestamp' );

          if ( 'on' === $count_to_include_date ) {
            switch ( $count_date_type ) {
              case 'seconds':
                $count_to_date += 1;
                break;
              case 'minutes':
                $count_to_date += 60;
                break;
              case 'hours':
                $count_to_date += 60 * 60;
                break;
              case 'weeks':
                $count_to_date += 60 * 60 * 24 * 7;
                break;
              case 'month':
                $count_to_date += 60 * 60 * 24 * (365 / 12);
                break;
              case 'years':
                $count_to_date += 60 * 60 * 24 * 365;
                break;
              default: //Default is days
                $count_to_date += 60 * 60 * 24;
            }
          }

          if ( 'on' === $count_from_include_date ) {
            switch ( $count_date_type ) {
              case 'seconds':
                $count_from_date += 1;
                break;
              case 'minutes':
                $count_from_date += 60;
                break;
              case 'hours':
                $count_from_date += 60 * 60;
                break;
              case 'weeks':
                $count_from_date += 60 * 60 * 24 * 7;
                break;
              case 'month':
                $count_from_date += 60 * 60 * 24 * (365 / 12);
                break;
              case 'years':
                $count_from_date += 60 * 60 * 24 * 365;
                break;
              default: //Default is days
                $count_from_date += 60 * 60 * 24;
            }
          }

          $difference_in_seconds = $count_to_date - $count_from_date;

          switch ( $count_date_type ) {
            case 'seconds':
              $count_to_value = sprintf('%1$s', floor( $difference_in_seconds ));
              break;
            case 'minutes':
              $count_to_value = sprintf('%1$s', floor( $difference_in_seconds / 60 ));
              break;
            case 'hours':
              $count_to_value = sprintf('%1$s', floor( $difference_in_seconds / (60 * 60) ));
              break;
            case 'weeks':
              $count_to_value = sprintf('%1$s', floor( $difference_in_seconds / (60 * 60 * 24 * 7)));
              break;
            case 'month':
              $count_to_value = sprintf('%1$s', floor( $difference_in_seconds / (60 * 60 * 24 * (365 / 12))));
              break;
            case 'years':
              $count_to_value = sprintf('%1$s', floor( $difference_in_seconds / (60 * 60 * 24 * 365)));
              break;
            default:
              $count_to_value = sprintf('%1$s', floor( $difference_in_seconds / (60 * 60 * 24)));
          }

        } else if ( 'post' == $count_to_type ) {
          $count_to_post   = explode( '|', $count_to_post );
          $i = 0;
          foreach (self::get_post_types_to_count() as $post_type => $label ) {
            $post_type_post_count = wp_count_posts($post_type)->publish;
            if ( isset($count_to_post[$i]) && 'on' === $count_to_post[$i] ) {
              $count_to_value += $post_type_post_count;
            }
            $i++;
          }

          if(is_numeric($count_to_post_offset)){
            $count_to_value += $count_to_post_offset;
          }
        }


        $additional_css_classes = array();
        if ( 'circle' === $counter_type ) $additional_css_classes[] = 'et_pb_jt_circle_counter';
        if ( 'number' === $counter_type ) $additional_css_classes[] = 'et_pb_jt_number_counter';

        $additional_css_classes[] = 'et_pb_module';


        return sprintf(
          '<div%1$s%2$s %3$s>
            <div class="et_pb_jt_counter_number">
              <span class="et_pb_jt_counter_number_prefix">%4$s</span><span class="et_pb_jt_counter_number_number">%5$s</span><span class="et_pb_jt_counter_number_suffix">%6$s</span>
            </div>
          </div>',
          $this->jt_get_module_id(),
          $this->jt_get_module_classes( $function_name, $additional_css_classes ),
          $this->get_easy_pie_chart_data( $count_to_value, $count_from_value ),
          $prefix,
          $count_from_value,
          $suffix
        );
      }

      function get_easy_pie_chart_data( $count_to_value, $count_from_value ){

        $easy_pie_chart_data = array();
        $easy_pie_chart_data[] = "data-count-to='{$count_to_value}'";
        $easy_pie_chart_data[] = "data-count-from='{$count_from_value}'";


        $count_duration        = $this->shortcode_atts['count_duration'] * 1000;
        $easy_pie_chart_data[] = "data-count-duration='{$count_duration}'";


        $count_number_decimals = $this->shortcode_atts['count_number_decimals'];
        $force_decimal_places = '' !== $count_number_decimals;
        $easy_pie_chart_data[] = "data-decimal-places='{$count_number_decimals}'";
        $easy_pie_chart_data[] = "data-force-decimal-places='{$force_decimal_places}'";


        $counter_type          = $this->shortcode_atts['counter_type'];
        $easy_pie_chart_data[] = "data-counter-type='{$counter_type}'";


        $count_circle_percent  = $this->shortcode_atts['count_circle_percent'];
        $easy_pie_chart_data[] = "data-circle-percent='{$count_circle_percent}'";


        $circle_bar_color      = $this->shortcode_atts['circle_bar_color'];
        $circle_bar_color      = isset($circle_bar_color) && '' !== $circle_bar_color ? $circle_bar_color : et_builder_accent_color();
        $easy_pie_chart_data[] = "data-circle-bar-color='{$circle_bar_color}'";


        $circle_track_color    = $this->shortcode_atts['circle_track_color'];
        $circle_track_color    = isset($circle_track_color) && '' !== $circle_track_color ? $circle_track_color : 'rgba(0,0,0,0.1)';
        $easy_pie_chart_data[] = "data-circle-track-color='{$circle_track_color}'";


        $circle_line_width     = $this->shortcode_atts['circle_line_width'];
        $easy_pie_chart_data[] = "data-circle-line-width='{$circle_line_width}'";


        $circle_line_cap       = $this->shortcode_atts['circle_line_cap'];
        $easy_pie_chart_data[] = "data-circle-line-cap='{$circle_line_cap}'";



        $circle_size           = $this->shortcode_atts['circle_size'];
        if( isset($circle_size) && '' !== $circle_size )
          $easy_pie_chart_data[] = "data-circle-size='{$circle_size}'";


        $circle_use_scale = $this->shortcode_atts['circle_use_scale'];
        if (isset($circle_use_scale) && 'on' === $circle_use_scale ) {
          $circle_scale_length = $this->shortcode_atts['circle_scale_length'];
          $circle_scale_color  = $this->shortcode_atts['circle_scale_color'];
          $circle_scale_color = isset($circle_scale_color) && '' !== $circle_scale_color ? $circle_scale_color : 'rgba(0,0,0,0.1)';
          $easy_pie_chart_data[] = "data-circle-scale-color='{$circle_scale_color}'";
          $easy_pie_chart_data[] = "data-circle-scale-length='{$circle_scale_length}'";
        }


        $circle_rotate = $this->shortcode_atts['circle_rotate'];
        $easy_pie_chart_data[] = "data-circle-rotate='{$circle_rotate}'";

        $count_number_separator = $this->shortcode_atts['count_number_separator'];
        $easy_pie_chart_data[] = "data-number-separator='{$count_number_separator}'";


        return implode( ' ', $easy_pie_chart_data );
      }

      static function get_post_types_to_count() {
        $post_types = array(
          'post'    => esc_html__( 'Posts', 'et_builder' ),
					'page'    => esc_html__( 'Pages', 'et_builder' ),
        );

        foreach ( get_post_types( array( 'public' => true, '_builtin' => false ), 'objects', 'and' ) as $post_type ) {
          $post_types[$post_type->name] = $post_type->labels->name;
        }

        return $post_types;
      }
    }

    add_shortcode('et_pb_jt_counter', array(new ET_Builder_Module_JT_Counter(), '_shortcode_callback'));
  }

}
add_action('et_builder_ready', 'jt_divi_counter_module', 10);
