<?php

class Tbm_Tagmailer_Display_Layouts {

	public $common;

	function __construct() {
		$this->common = new Tbm_Tagmailer_Common();
	}

	/**
	 * Save the first title in a query var
	 *
	 * @param $post WP_Post or $post_id
	 */
	private function set_the_first_title( $post ) {

		if ( ! $post ) {
			return false;
		}

        // If $post is and ID, get the post
        if ( is_numeric( $post ) ) {
            $post = get_post( $post );
        }

		$first_title = $post->post_title;

		if ( ! $first_title ) {
			return false;
		}

		if ( ! get_query_var( 'first_title' ) ) {
			set_query_var( 'first_title', $first_title );
		}

		return true;
	}

	public function html_newsletter_display_title( object $obj ) {

		if ( empty( $obj->tbm_nl_block_title ) ) {
			return '';
		}

		?>
        <table cellpadding="0" cellspacing="0" border="0" width="100%"
               style="width: 100% !important; min-width: 100%; max-width: 100%; background-color:<?php echo $obj->tbl_nl_primary_color; ?>;padding: 0 6%;">
            <tr>
                <td align="left" valign="top">
                    <div style="height: 10px; line-height: 10px;">&nbsp;</div>
                    <font class="mob_title1" face="<?php echo $obj->tbm_nl_font_family; ?>" color="#fff"
                          style="font-size: 25px; line-height: 30px; mso-line-height-rule:exactly;letter-spacing: 0.8px; font-weight: 300;text-transform: uppercase;">
                    <span class="mob_title1"
                          style="font-family: <?php echo $obj->tbm_nl_font_family; ?>; color: <?php echo $obj->tbl_nl_heading_color; ?>; font-size: 25px; line-height: 30px; font-weight: 300; mso-line-height-rule:exactly; letter-spacing: 0.8px;text-transform: uppercase;"><?php echo $obj->tbm_nl_block_title; ?></span>
                    </font>
                    <div style="height: 10px; line-height: 10px;">&nbsp;</div>
                </td>
            </tr>
        </table>


		<?php
		return true;
	}

	public function html_newsletter_display_post( object $obj ) {
		if ( isset( $obj->tmb_site_id ) && is_multisite() ) {
			switch_to_blog( $obj->tmb_site_id );
		}

		$not_in = get_query_var( 'notin' ) ?: array();

		$date = $this->html_get_post_date( $obj );

		$post    = false;
		$catname = '';
		$catlink = '';

		if ( ! isset( $obj->tbm_nl_post ) || $obj->tbm_nl_last_post == true ) {

			$args = [
				'post__not_in'   => $not_in,
				'post_type'      => isset( $obj->tbm_nl_pilot_post_type ) && is_array( $obj->tbm_nl_pilot_post_type ) ? $obj->tbm_nl_pilot_post_type : [ 'post' ],
				'posts_per_page' => 1,
			];

			if ( ! empty( $date ) ) {
				$args['date_query'] = [
					'before' => $date . ' 23:59:59',
					'after'  => $date . ' 00:00:00',
				];
			}

			if ( $obj->tbm_nl_ignore_post_date ) {
				$args['date_query'] = [];
			}

			$args = apply_filters( 'html_newsletter_get_posts__args', apply_filters( 'html_newsletter_display_post__args', $args ) );

			$posts = get_posts( $args );
			$post  = reset( $posts );

		} else if ( isset( $obj->tbm_nl_post->ID ) ) {
			$post = $obj->tbm_nl_post;
		} else if ( isset( $obj->tbm_nl_post['selected_posts'][0]->ID ) ) {
			$post = $obj->tbm_nl_post['selected_posts'][0];
		} else {
			$post = $obj->tbm_nl_post;
		}
		if ( ! $post ) {
			if ( isset( $obj->tmb_site_id ) && is_multisite() ) {
				restore_current_blog();
			}

			return false;
		}

		// Catch the feat
		$featured_img = $this->html_newsletter_get_featured( $post, 'hero' );

		// recupero la label
		$label = $this->html_newsletter_get_label( $post, $obj );

		if ( $label ) {
			$catname = $label['catname'];
			$catlink = ! empty( $label['catlink'] ) ? $label['catlink'] : '';
		}

		$this->html_newsletter_display_title( $obj );
		$not_in[] = $post->ID;

		// Set font-sizes
		$font_size   = 25 * $obj->tbm_nl_font_title_multiplier;
		$line_height = $font_size * 1.2;

		$label_font_size   = 16 * $obj->tbm_nl_font_title_multiplier;
		$label_line_height = $label_font_size * 1.2;

		$this->set_the_first_title( $post );

		set_query_var( 'notin', $not_in );
		?>
        <!-- HERO IMG + TITLE + SUBTITLE start -->
        <table cellpadding="0" cellspacing="0" border="0" width="88%"
               style="width: 88% !important; min-width: 88%; max-width: 88%;">
            <tr>
				<?php do_action( "newsletter_display_post_top", $post ); ?>
                <td align="left" valign="top">
					<?php if ( ! empty( $featured_img['desktop'] ) ) : ?>
                        <div style="height: 16px; line-height: 16px; font-size: 20px;">&nbsp;</div>
                        <a href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                           target="_blank" style="display: block; max-width: 100%;">
                            <img src="<?php echo $featured_img['desktop']; ?>" alt="img" width="100%" border="0"
                                 srcset="<?php echo $featured_img['mobile']; ?> 320w,
                                                     <?php echo $featured_img['desktop']; ?> 560w,"
                                 sizes="(max-width: 649px) 320px,
                                                    560px"
                                 style="display: block; width: 100%;"/>
                        </a>
					<?php endif; ?>
                    <div style="height: 16px; line-height: 16px; font-size: 20px;">&nbsp;</div>
                    <font face="<?php echo $obj->tbm_nl_font_family; ?>"
                          style="font-size: 16px; line-height: 22px; mso-line-height-rule:exactly; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px;">
						<?php if ( isset( $catname ) ) : ?>
                            <span style="color: <?php echo $obj->tbl_nl_label_color; ?>;font-family: <?php echo $obj->tbm_nl_font_family; ?>; font-size: <?php echo $label_font_size; ?>px; line-height: <?php echo $label_line_height; ?>px; mso-line-height-rule:exactly; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px;">
                            <a href="<?php echo $catlink; ?>"
                               style="color: <?php echo $obj->tbl_nl_label_color; ?>"><?php echo $catname; ?></a>
                        </span>
						<?php endif; ?>
                    </font>
                    <div style="height: 4px; line-height: 4px; font-size: 8px;">&nbsp;</div>
                    <font class="mob_title1" face="<?php echo $obj->tbm_nl_font_family; ?>" color="#1a1a1a"
                          style="font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px; mso-line-height-rule:exactly; font-weight: 300; letter-spacing: -1.5px;">
                    <span class="mob_title1"
                          style="font-family: <?php echo $obj->tbm_nl_font_family; ?>; color: #1a1a1a; font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px; mso-line-height-rule:exactly;font-weight: 300; letter-spacing: -1.5px;">
                        <a
                                href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                                style="font-family: <?php echo $obj->tbm_nl_font_family; ?>;color:#000"><?php echo get_the_title( $post ); ?></a></span>
                    </font>

                </td>
				<?php do_action( "newsletter_display_post_bottom", $post ); ?>
            </tr>
        </table>
        <!-- HERO IMG + TITLE + SUBTITLE end -->

		<?php
		$this->html_newsletter_display_section_separator();

		if ( isset( $obj->tmb_site_id ) && is_multisite() ) {
			restore_current_blog();
		}

		return true;
	}

	private function html_get_post_date( $obj, $post_type = array(), $tax_query = array() ) {

		// Recupero l'opzione del sito e imposto i giorni da sottrarre
		$days_ago        = isset( $obj->tbm_nl_newsletter_days_ago ) && is_numeric( $obj->tbm_nl_newsletter_days_ago ) ? $obj->tbm_nl_newsletter_days_ago : 0;
		$pilot_post_type = isset( $obj->tbm_nl_pilot_post_type ) && is_array( $obj->tbm_nl_pilot_post_type ) ? $obj->tbm_nl_pilot_post_type : acf_get_post_types();
		$post_type       = ! empty( $post_type ) ? $post_type : $pilot_post_type;

		if ( empty( $post_type ) ) {
			return false;
		}

		// Chiedo i post del giorno minimo
		$args = array(
			'post_type'      => $post_type,
			'posts_per_page' => 2,
			'order_by'       => 'date',
			'order'          => 'DESC'
		);

		if ( ! empty( $tax_query ) ) {
			$args['tax_query'] = $tax_query;
		}

		// Se nelle opzioni è impostato un giorno minimo superiore a 0, definisco la data
		if ( $days_ago > 0 ) {
			$args['date_query'] = array(
				'before' => date( "Y-m-d 23:59:59", strtotime( '-' . $days_ago . ' days', $obj->tbm_nl_today ) )
			);
		}
		$args = apply_filters( 'html_newsletter_get_posts__args', apply_filters( 'html_get_post_date__args', $args ) );

		/**
		 * Get posts
		 */
		$posts = get_posts( $args );

		// Get the first post date: will be the date of all posts
		$last_date = reset( $posts )->post_date;

		return ( substr( $last_date, 0, 10 ) );
	}

	private function html_newsletter_get_label( $post, $obj, $force = true ) {
		$out = [];
		if ( ! $obj->tbm_nl_post_type_label && ! $force ) {
			return $out;
		}

		if ( ! taxonomy_exists( $obj->tbm_nl_post_type_label ) && ! $force ) {
			return $out;
		}

		// Empty if post type is page
		if ( get_post_type( $post ) === 'page' ) {

			$out['catname'] = '';
			$out['catlink'] = '';

			return $out;
		}

		$cat_terms = wp_get_object_terms( $post->ID, $obj->tbm_nl_post_type_label );

		if ( $cat_terms ) {

			$catname = $cat_terms[0]->name;
			$catlink = get_term_link( $cat_terms[0]->slug, $obj->tbm_nl_post_type_label );

			$out['catname'] = $catname;
			$out['catlink'] = $catlink;

			return $out;
		}

		// If label is not forced, exit now
		if ( ! $force ) {
			return $out;
		}

		// Try with category
		$cat_terms = wp_get_object_terms( $post->ID, 'category' );

		if ( $cat_terms ) {

			$catname = $cat_terms[0]->name;
			$catlink = get_term_link( $cat_terms[0]->slug, 'category' );

			$out['catname'] = $catname;
			$out['catlink'] = $catlink;

			return $out;
		}

		// Try with post type
		$post_type = get_post_type( $post->ID );

		if ( $post_type ) {

			$post_type_objects = get_post_type_object( $post_type );

			$out['catname'] = $post_type_objects->labels->singular_name;
			$out['catlink'] = get_post_type_archive_link( $post_type );

			return $out;
		}

		return $out;
	}

	private function html_newsletter_get_featured( $post, $zone = '' ): array {

		if ( $zone === 'triplete' ) {
			$img_desktop = get_the_post_thumbnail_url( $post->ID, 'newsletter_small' );
			$img_mobile  = get_the_post_thumbnail_url( $post->ID, 'newsletter_medium' );
		} else if ( $zone === 'hero' ) {
			$img_desktop = get_the_post_thumbnail_url( $post->ID, 'newsletter_large' );
			$img_mobile  = get_the_post_thumbnail_url( $post->ID, 'newsletter_medium' );
		} else {
			$img_desktop = get_the_post_thumbnail_url( $post->ID, 'newsletter_small' );
			$img_mobile  = get_the_post_thumbnail_url( $post->ID, 'newsletter_medium' );
		}


		return array( 'desktop' => $img_desktop, 'mobile' => $img_mobile );
	}

	/**
	 * Display a button with a CTA
	 *
	 * @param Object $obj options and elements
	 */
	public function html_newsletter_display_button( $obj ) {

		$button_text = $obj->tbm_nl_button_text;
		$button_url  = $this->html_get_ga_link( $obj, 'button', $obj->tbm_nl_button_url );
		$button_cta  = $obj->tbm_nl_button_cta;

		if ( ! $button_url || ! $button_cta ) {
			return '';
		}

		$font_size   = 16 * $obj->tbm_nl_font_title_multiplier;
		$line_height = $font_size * 1.2;

		$this->html_newsletter_display_title( $obj );
		?>


        <!--[if mso | IE]>
        <table align="center" border="0" cellpadding="0" cellspacing="0" class="" role="presentation"
               style="width:600px;"
               width="600" bgcolor="#ffffff">
            <tr>
                <td style="line-height:0;font-size:0;mso-line-height-rule:exactly;"><![endif]-->
        <div style="background:#ffffff;background-color:#ffffff;margin:0 auto;max-width:600px;">
            <table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation"
                   style="background:#ffffff;background-color:#ffffff;width:100%;">
                <tbody>
                <tr>
                    <td style="direction:ltr;font-size:0;padding:20px 0;text-align:center;">
                        <!--[if mso | IE]>
                        <table role="presentation" border="0" cellpadding="0" cellspacing="0">
                            <tr>
                                <td class="" style="vertical-align:top;width:600px;"><![endif]-->
                        <div class="mj-column-per-100 mj-outlook-group-fix"
                             style="font-size:0;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;">
                            <table border="0" cellpadding="0" cellspacing="0" role="presentation"
                                   style="vertical-align:top;" width="100%">
                                <tbody>
								<?php if ( $button_text ) { ?>
                                    <tr>
                                        <td align="center" style="font-size:0;padding:10px 25px;word-break:break-word;">
                                            <div style="font-family:<?php echo $obj->tbm_nl_font_family; ?>;font-size:<?php echo $font_size; ?>px;font-weight:400;line-height:<?php echo $line_height; ?>px;text-align:center;color:#333333;"><?php echo $button_text; ?></div>
                                        </td>
                                    </tr>
								<?php } ?>
                                <tr>
                                    <td align="center" vertical-align="middle"
                                        style="font-size:0;padding:10px 25px;word-break:break-word;">
                                        <table border="0" cellpadding="0" cellspacing="0" role="presentation"
                                               style="border-collapse:separate;line-height:100%;">
                                            <tbody>
                                            <tr>
                                                <td align="center" bgcolor="<?php echo $obj->tbl_nl_primary_color; ?>"
                                                    role="presentation"
                                                    style="border:none;border-radius:2px;cursor:auto;mso-padding-alt:15px 30px;background:<?php echo $obj->tbl_nl_primary_color; ?>;"
                                                    valign="middle">
                                                    <a href="<?php echo $button_url; ?>"
                                                       style="display:inline-block;background:<?php echo $obj->tbl_nl_primary_color; ?>;color:#ffffff;font-family:<?php echo $obj->tbm_nl_font_family; ?>;font-size:<?php echo $font_size; ?>px;font-weight:normal;line-height:120%;margin:0;text-decoration:none;text-transform:none;padding:15px 30px;mso-padding-alt:0;border-radius:2px;"
                                                       target="_blank">
														<?php echo $button_cta; ?>
                                                    </a>
                                                </td>
                                            </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                        <!--[if mso | IE]></td></tr></table><![endif]-->
                    </td>
                </tr>
                </tbody>
            </table>
        </div>
        <!--[if mso | IE]></td></tr></table><![endif]-->
        <!-- /tbm_button -->

		<?php
		$this->html_newsletter_display_section_separator();


		if ( isset( $obj->tmb_site_id ) && is_multisite() ) {
			restore_current_blog();
		}

		return true;

	}

	/**
	 * Block manual post
	 *
	 * @param Object $obj options and elements
	 */
	public function html_newsletter_display_posts( object $obj ) {
		$not_in = get_query_var( 'notin' ) ?: array();
		$posts  = $obj->tbm_nl_posts;

		if ( isset( $obj->tbm_nl_posts['selected_posts'] ) ) {
			$posts = $obj->tbm_nl_posts['selected_posts'];
		}

		if ( $posts ) {

			// Set the query var for firs title
			$this->set_the_first_title( $posts[0] );

			// Set query var to exclude posts already displayed
			set_query_var( 'notin', array_merge( $not_in, wp_list_pluck( $posts, 'ID' ) ) );

			$this->html_newsletter_display_title( $obj );

			foreach ( $posts as $post ) {

				$post = is_numeric( $post ) ? get_post( $post ) : $post;

				$this->html_newsletter_display_post_small( $post, $obj );
			}

			$this->html_newsletter_display_section_separator();

		}

		if ( isset( $obj->tmb_site_id ) && is_multisite() ) {
			restore_current_blog();
		}

	}

	private function html_newsletter_display_separator() {
		// If we need a separator, code it here
		return '';
	}

	/**
	 * Blocco html del separatore di sezione [NEW]
	 */
	private function html_newsletter_display_section_separator() {
		// If we need a separator, code it here
		?>
        <!-- DIVIDER start -->
        <div style="height: 22px; line-height: 22px; font-size: 22px;">&nbsp;</div>
        <table cellpadding="0" cellspacing="0" border="0" width="88%"
               style="width: 88% !important; min-width: 88%; max-width: 88%; border-width: 1px; border-style: solid; border-color: #e8e8e8; border-top: none; border-left: none; border-right: none;">

        </table><!-- DIVIDER end -->
		<?php
	}

	private function html_get_ga_link( $obj, $name = "null", $url = '' ): string {

		if ( false === filter_var( $url, FILTER_VALIDATE_URL ) ) {
			$url = get_bloginfo( 'url' );
		}

		$link_ga = add_query_arg( array(
			'utm_source'   => urlencode( $obj->tbm_nl_utm_source ),
			'utm_medium'   => urlencode( $obj->tbm_nl_utm_medium ),
			'utm_campaign' => urlencode( $obj->tbm_nl_utm_campaign ),
			'utm_content'  => $name
		), $url );

		// Append at the end to avoid esc_url filter
		return esc_url( $link_ga );
	}

	/**
	 * Blocco html del post piccolo [NEW]
	 *
	 * @param $post
	 * @param $obj
	 */
	private function html_newsletter_display_post_small( $post, $obj ) {

		// Serve un'immagine in evidenza?
		$ico                    = true;
		$featured_img_url       = $this->html_get_post_type_icon( $post->post_type );
		$featured_img           = $this->html_newsletter_get_featured( $post );
		$catlink                = '';
		$catname                = '';
		$featured_img_url_small = '';

		if ( $obj->tbm_nl_use_featured_img && ! empty( $featured_img['desktop'] ) ) {
			$featured_img_url       = $featured_img['desktop'];
			$featured_img_url_small = $featured_img['mobile'];
			$ico                    = false;
		}

		$font_size   = 22 * $obj->tbm_nl_font_title_multiplier;
		$line_height = $font_size * 1.2;

		$label_font_size   = 16 * $obj->tbm_nl_font_title_multiplier;
		$label_line_height = $label_font_size * 1.2;

		// recupero la label
		$label = $this->html_newsletter_get_label( $post, $obj );

		if ( $label ) {
			$catname = $label['catname'];
			$catlink = ! empty( $label['catlink'] ) ? $label['catlink'] : '';
		}

		// Serve un riassunto?
		$excerpt = '';
		if ( $obj->tbm_nl_layout === 'excerpt' ) {
			$excerpt_text = get_post_meta( $post->ID, 'in_breve', true ) ?: get_the_excerpt( $post->ID );
			if ( $excerpt_text ) {
				$excerpt = '<p style="line-height:19px;margin:0;margin-top:10px;color:#222;text-align:left;font-weight:400;font-family:\'Roboto Condensed\',-apple-system,\'Segoe UI\',Helvetica,Arial,sans-serif;text-decoration:none;font-size:13px">' . $excerpt_text . '</p>';
			}
		}

		?>
		<?php if ( $ico ) : ?>
            <table cellpadding="0" cellspacing="0" border="0" width="100%"
                   style="width: 100% !important; min-width: 100%; max-width: 100%;">
                <tr>
                    <td align="center" valign="top">
                        <!--[if (gte mso 9)|(IE)]>
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td align="center" valign="top" width="110"><![endif]-->
                        <div class="mob_icon" style="display: inline-block; vertical-align: top; width: 110px;">
                            <table class="mob_card" cellpadding="0" cellspacing="0" border="0" width="70"
                                   style="width: 70px !important; min-width: 70px; max-width: 70px;margin-top: 10px">
                                <tr>
                                    <td align="left" valign="middle">
                                        <div style="height: 22px; line-height: 22px; font-size: 33px;">&nbsp;</div>
                                        <a href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                                           target="_blank" style="display: block; max-width: 100%;">
                                            <img src="<?php echo $featured_img_url; ?>" alt="img" width="100%"
                                                 border="0"
                                                 style="display: block; width: 100%;"/>
                                        </a>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <!--[if (gte mso 9)|(IE)]></td>
                        <td align="center" valign="top" width="440"><![endif]-->
                        <div class="mob_btn" style="display: inline-block; vertical-align: top; width: 440px;">
                            <table class="mob_card" cellpadding="0" cellspacing="0" border="0" width="410"
                                   style="width: 410px; min-width: 410px; max-width: 410px;">
                                <tr>
                                    <td align="left" valign="middle">
										<?php if ( isset( $catname ) ) : ?>
                                            <div style="height: 22px; line-height: 22px; font-size: 20px;">&nbsp;</div>
                                            <font face="<?php echo $obj->tbm_nl_font_family; ?>"
                                                  style="font-size: <?php echo $label_font_size; ?>px; line-height: <?php echo $label_line_height; ?>px; mso-line-height-rule:exactly; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px;">
                                            <span style="color: <?php echo $obj->tbl_nl_label_color; ?>; font-family: <?php echo $obj->tbm_nl_font_family; ?>; font-size: <?php echo $label_font_size; ?>px; line-height: <?php echo $label_line_height; ?>px; mso-line-height-rule:exactly; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px;"><a
                                                        href="<?php echo $catlink; ?>"
                                                        style="color: <?php echo $obj->tbl_nl_label_color; ?>"><?php echo $catname; ?></a></span>
                                            </font>
										<?php endif; ?>
                                        <div style="height: 4px; line-height: 4px; font-size: 8px;">&nbsp;</div>
                                        <font face="<?php echo $obj->tbm_nl_font_family; ?>" color="#101010"
                                              style="font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px;mso-line-height-rule:exactly;font-weight: 300; letter-spacing: -1px;">
                                        <span style="font-family: <?php echo $obj->tbm_nl_font_family; ?>; color: #101010; font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px; mso-line-height-rule:exactly;font-weight: 300; letter-spacing: -1px;"><a
                                                    href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                                                    style="font-family: <?php echo $obj->tbm_nl_font_family; ?>;color:#000"><?php echo get_the_title( $post ); ?></a></span>
                                        </font>
                                        <div style="height: 8px; line-height: 8px; font-size: 6px;">&nbsp;</div>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <!--[if (gte mso 9)|(IE)]>
                        </td></tr>
                        </table><![endif]-->
                    </td>
                </tr>
            </table>
		<?php elseif ( $featured_img_url ) : ?>
            <table cellpadding="0" cellspacing="0" border="0" width="100%"
                   style="width: 100% !important; min-width: 100%; max-width: 100%;">
                <tr>
					<?php do_action( "newsletter_display_post_small_top", $post ); ?>
                    <td align="center" valign="top">
                        <!--[if (gte mso 9)|(IE)]>
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td align="center" valign="top" width="200"><![endif]-->
                        <div class="mob_btn" style="display: inline-block; vertical-align: top; width: 200px;">
                            <table class="mob_card" cellpadding="0" cellspacing="0" border="0" width="170"
                                   style="width: 170px !important; min-width: 170px; max-width: 170px;">
                                <tr>
                                    <td align="left" valign="middle">
                                        <div style="height: 22px; line-height: 22px; font-size: 33px;">&nbsp;</div>
                                        <a href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                                           target="_blank" style="display: block; max-width: 100%;">
                                            <!--[if mso]>
                                        <table width="50%"><tr><td><img width="200" src="<?php echo $featured_img_url; ?>" alt="immagine" style="text-align: left; width: 200px; border: 0; text-decoration:none; vertical-align: middle;"></td></tr></table>
                                        <div style="display:none">
                                        <![endif]-->
                                            <img src="<?php echo $featured_img_url_small; ?>" alt="img" width="100%"
                                                 border="0"
                                                 srcset="<?php echo $featured_img_url_small; ?> 320w,
                                                     <?php echo $featured_img_url; ?> 107w,"
                                                 sizes="(max-width: 649px) 320px,
                                                    107px"
                                                 style="display: block; width: 100%;"/>
                                            <!--[if mso]>
                                            </div>
                                            <![endif]-->
                                        </a>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <!--[if (gte mso 9)|(IE)]></td>
                        <td align="center" valign="top" width="350"><![endif]-->
                        <div class="mob_btn" style="display: inline-block; vertical-align: top; width: 350px;">
                            <table class="mob_card" cellpadding="0" cellspacing="0" border="0" width="320"
                                   style="width: 320px; min-width: 320px; max-width: 320px;">
                                <tr>
                                    <td align="left" valign="middle">
										<?php if ( isset( $catname ) ) : ?>
                                            <div style="height: 22px; line-height: 22px; font-size: 20px;">&nbsp;</div>
                                            <font face="<?php echo $obj->tbm_nl_font_family; ?>"
                                                  style="font-size: <?php echo $label_font_size ?>px; line-height: <?php echo $label_line_height ?>px; mso-line-height-rule:exactly; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px;">
                                            <span style="color: <?php echo $obj->tbl_nl_label_color; ?>;font-family: <?php echo $obj->tbm_nl_font_family; ?>; font-size: <?php echo $label_font_size; ?>px; line-height: <?php echo $label_line_height; ?>px; mso-line-height-rule:exactly; font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px;">
                                                <a href="<?php echo $catlink; ?>"
                                                   style="color: <?php echo $obj->tbl_nl_label_color; ?>"><?php echo $catname; ?></a></span>
                                            </font>
										<?php endif; ?>
                                        <div style="height: 4px; line-height: 4px; font-size: 8px;">&nbsp;</div>
                                        <font face="<?php echo $obj->tbm_nl_font_family; ?>" color="#101010"
                                              style="font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px;mso-line-height-rule:exactly;font-weight: 300; letter-spacing: -1px;">
                                        <span style="font-family: <?php echo $obj->tbm_nl_font_family; ?>; color: #101010; font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px; mso-line-height-rule:exactly;font-weight: 300; letter-spacing: -1px;"><a
                                                    href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                                                    style="font-family: <?php echo $obj->tbm_nl_font_family; ?>;color:#000"><?php echo get_the_title( $post ); ?></a></span>
                                        </font>
                                        <div style="height: 4px; line-height: 4px; font-size: 8px;">&nbsp;</div>
                                        <font face="<?php echo $obj->tbm_nl_font_family; ?>" color="#101010"
                                              style="font-size: 16px; line-height: 22px; mso-line-height-rule:exactly;">
                                            <span style="font-family: <?php echo $obj->tbm_nl_font_family; ?>; color: #101010; font-size: 16px; line-height: 22px; mso-line-height-rule:exactly;"><?php echo $excerpt; ?></span>
                                        </font>
                                        <div style="height: 8px; line-height: 8px; font-size: 6px;">&nbsp;</div>
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <!--[if (gte mso 9)|(IE)]>
                        </td></tr>
                        </table><![endif]-->
                    </td>
					<?php do_action( "newsletter_display_post_small_bottom", $post ); ?>
                </tr>
            </table>
		<?php
		endif; ?>


		<?php
	}

	/**
	 * Ritorna l'icona del post type [NEW]
	 *
	 * @param $post_type
	 *
	 * @return bool|string
	 */
	private function html_get_post_type_icon( $post_type ) {

		if ( file_exists( get_template_directory() . '/newsletter/img/' . $post_type . '.png' ) ) {
			return get_template_directory() . '/newsletter/img/' . $post_type . '.png';
		}

		if ( file_exists( TBM_TAGMAILER_PLUGIN_DIR . 'public/images/' . $post_type . '.png' ) ) {
			return TBM_TAGMAILER_PLUGIN_URL . 'public/images/' . $post_type . '.png';
		}

		return TBM_TAGMAILER_PLUGIN_URL . 'public/images/blog.png';
	}

	/**
	 * Post per category
	 *
	 * @param $obj
	 */
	public function html_newsletter_display_posts_from_categories( $obj ) {
		$not_in = get_query_var( 'notin' ) ?: array();

		$tax_query = array(
			array(
				'taxonomy' => 'category',
				'terms'    => $obj->tbm_nl_category,
			),
		);

		$posts_per_page = ( $obj->tbm_nl_layout === 'tre' ) ? '3' : $obj->tbm_nl_numbers;

		$date = $this->html_get_post_date( $obj, array( 'any' ), $tax_query );

		$args = [
			'post_type'      => 'any',
			'posts_per_page' => (int) $posts_per_page,
			'post__not_in'   => $not_in,
			'tax_query'      => $tax_query,
		];

		if ( ! empty( $date ) ) {
			$args['date_query'] = [
				'before' => $date . ' 23:59:59',
				'after'  => $date . ' 00:00:00',
			];
		}

		if ( $obj->tbm_nl_ignore_post_date ) {
			$args['date_query'] = [];
		}

		if ( $obj->tbm_nl_random_content ) {
			$args['orderby']    = 'rand';
			$args['date_query'] = [
				'after' => $obj->tbm_nl_last_number . ' ' . $obj->tbm_nl_last_date . ' ago',
			];
		}

		$args  = apply_filters( 'html_newsletter_get_posts__args', apply_filters( 'html_newsletter_display_posts_from_categories__args', $args ) );
		$posts = get_posts( $args );

		if ( $posts ) {

			// Set the query var for firs title
			$this->set_the_first_title( $posts[0] );

			// Set query var to exclude posts already displayed
			set_query_var( 'notin', array_merge( $not_in, wp_list_pluck( $posts, 'ID' ) ) );

			if ( $obj->tbm_nl_layout === 'tre' && count( $posts ) === 3 ) {
				$this->html_newsletter_display_title( $obj );

				$this->html_newsletter_display_triplete( $posts, $obj );
			} else {
				$this->html_newsletter_display_title( $obj );

				// variabile di controllo
				$old_date = "";
				foreach ( $posts as $post ) {
					if ( ( $old_date == "" ) || ( $old_date == date( 'Y-m-d', strtotime( $post->post_date ) ) ) || $obj->tbm_nl_ignore_post_date ) {

						$this->html_newsletter_display_post_small( $post, $obj );

						// setto la variabile di controllo
						if ( $old_date == "" ) {
							$old_date = date( 'Y-m-d', strtotime( $post->post_date ) );
						}

					}
				}
			}

			$this->html_newsletter_display_section_separator();

		}


	}

	/**
	 * Post per tag
	 *
	 * @param $obj
	 */
	public function html_newsletter_display_posts_from_tags( $obj ) {
		$not_in = get_query_var( 'notin' ) ?: array();

		$tax_query = [
			[
				'taxonomy' => 'post_tag',
				'terms'    => $obj->tbm_nl_tag,
			],
		];

		// Triplete layout must show three posts
		$posts_per_page = ( $obj->tbm_nl_layout === 'tre' ) ? '3' : $obj->tbm_nl_numbers;

		$date = $this->html_get_post_date( $obj, array( 'any' ), $tax_query );

		$args = [
			'post_type'      => 'any',
			'posts_per_page' => (int) $posts_per_page,
			'post__not_in'   => $not_in,
			'tax_query'      => $tax_query,
		];

		if ( ! empty( $date ) ) {
			$args['date_query'] = [
				'before' => $date . ' 23:59:59',
				'after'  => $date . ' 00:00:00',
			];
		}

		if ( $obj->tbm_nl_ignore_post_date ) {
			$args['date_query'] = [];
		}

		if ( $obj->tbm_nl_random_content ) {
			$args['orderby']    = 'rand';
			$args['date_query'] = [
				'after' => $obj->tbm_nl_last_number . ' ' . $obj->tbm_nl_last_date . ' ago',
			];
		}

		$args = apply_filters( 'html_newsletter_get_posts__args', apply_filters( 'html_newsletter_display_posts_from_tags__args', $args ) );

		/**
		 * Get posts
		 */
		$posts = get_posts( $args );

		if ( $posts ) {

			// Set the query var for firs title
			$this->set_the_first_title( $posts[0] );

			// Set query var to exclude posts already displayed
			set_query_var( 'notin', array_merge( $not_in, wp_list_pluck( $posts, 'ID' ) ) );

			if ( $obj->tbm_nl_layout === 'tre' && count( $posts ) === 3 ) {
				$this->html_newsletter_display_title( $obj );
				$this->html_newsletter_display_triplete( $posts, $obj );
			} else {
				$this->html_newsletter_display_title( $obj );
				// variabile di controllo
				$old_date = "";
				foreach ( $posts as $post ) {

					if ( ( $old_date == "" ) || ( $old_date == date( 'Y-m-d', strtotime( $post->post_date ) ) ) || $obj->tbm_nl_ignore_post_date ) {

						$this->html_newsletter_display_post_small( $post, $obj );

						// setto la variabile di controllo
						if ( $old_date == "" ) {
							$old_date = date( 'Y-m-d', strtotime( $post->post_date ) );
						}

					}
				}
			}
			$this->html_newsletter_display_section_separator();
		}

	}

	/**
	 * Post per post type
	 *
	 * @param Object $obj The object containing all the options
	 */
	public function html_newsletter_display_posts_from_post_type( $obj ) {

		if ( isset( $obj->tbm_nl_posttypes_site ) && is_multisite() ) {
			switch_to_blog( $obj->tbm_nl_posttypes_site );
		}

		$not_in = get_query_var( 'notin' ) ?: array();

		$posts_per_page = ( $obj->tbm_nl_layout === 'tre' ) ? '3' : $obj->tbm_nl_numbers;

		$tbm_nl_post_type = $obj->tbm_nl_post_type ? $obj->tbm_nl_post_type : array();

		$args = [
			'post_type'      => $obj->tbm_nl_post_type,
			'post__not_in'   => $not_in,
			'posts_per_page' => (int) $posts_per_page,
		];

		$date = $this->html_get_post_date( $obj, $tbm_nl_post_type );

		if ( ! empty( $date ) ) {
			$args['date_query'] = [
				'before' => $date . ' 23:59:59',
				'after'  => $date . ' 00:00:00',
			];
		}

		if ( $obj->tbm_nl_ignore_post_date ) {
			$args['date_query'] = [];
		}

		if ( $obj->tbm_nl_random_content ) {
			$args['orderby']    = 'rand';
			$args['date_query'] = [
				'after' => $obj->tbm_nl_last_number . ' ' . $obj->tbm_nl_last_date . ' ago',
			];
		}

		$args = apply_filters( 'html_newsletter_get_posts__args', apply_filters( 'html_newsletter_display_posts_from_post_type__args', $args ) );

		/**
		 * Get posts
		 */
		$posts = get_posts( $args );

		if ( $posts ) {

			// Set the query var for first title
			$this->set_the_first_title( $posts[0] );

			// Set query var to exclude posts already displayed
			set_query_var( 'notin', array_merge( $not_in, wp_list_pluck( $posts, 'ID' ) ) );

			if ( $obj->tbm_nl_layout === 'tre' && count( $posts ) === 3 ) {
				$this->html_newsletter_display_title( $obj );
				$this->html_newsletter_display_triplete( $posts, $obj );
			} else {
				$this->html_newsletter_display_title( $obj );
				foreach ( $posts as $post ) {
					$this->html_newsletter_display_post_small( $post, $obj );
				}
			}
			$this->html_newsletter_display_section_separator();
		}

		if ( isset( $obj->tbm_nl_posttypes_site ) && is_multisite() ) {
			restore_current_blog();
		}
	}

	/**
	 * Blocco html del post triplete [NEW]
	 *
	 * @param $posts
	 * @param Object $obj The object containing all the options
	 */
	private function html_newsletter_display_triplete( $posts, $obj ) {
		$not_in = get_query_var( 'notin', array() );
		$posts  = array_slice( $posts, 0, 3 );

		$catlink = '';
		$catname = '';

		// Font size
		$font_size   = 22 * $obj->tbm_nl_font_title_multiplier;
		$line_height = $font_size * 1.2;

		$label_font_size   = 16 * $obj->tbm_nl_font_title_multiplier;
		$label_line_height = $label_font_size * 1.2;

		if ( count( $posts ) !== 3 ) {
			return '';
		}

		?>
        <table cellpadding="0" cellspacing="0" border="0" width="100%"
               style="width: 100% !important; min-width: 100%; max-width: 100%;">
            <tr>
                <td align="center" valign="top">
                    <!--[if (gte mso 9)|(IE)]>
                <table border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <?php
					foreach ( $posts

					as $key => $post ) :

					// Catch the feat
					$featured_img = $this->html_newsletter_get_featured( $post, 'triplete' );

					// recupero la label
					$label = $this->html_newsletter_get_label( $post, $obj );

					if ( $label ) {
						$catname = $label['catname'];
						$catlink = $label['catlink'];
					}
					$not_in[] = $post->ID;
					?>
                        <td align="center" valign="top" width="183"><![endif]-->
                    <div class="mob_btn" style="display: inline-block; vertical-align: top; width: 183px;">
                        <table class="mob_card" cellpadding="0" cellspacing="0" border="0" width="153"
                               style="width: 153px !important; min-width: 153px; max-width: 153px;">
                            <tr>
                                <td align="left" valign="top">
                                    <div style="height: 35px; line-height: 35px; font-size: 33px;">&nbsp;</div>
									<?php if ( ! empty( $featured_img['desktop'] ) ) : ?>
                                        <a href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                                           target="_blank" style="display: block; max-width: 100%;">
                                            <img src="<?php echo $featured_img['desktop']; ?>" alt="img" width="100%"
                                                 border="0"
                                                 srcset="<?php echo $featured_img['mobile']; ?> 320w,
                                                     <?php echo $featured_img['desktop']; ?> 107w,"
                                                 sizes="(max-width: 649px) 320px,
                                                    107px"
                                                 style="display: block; width: 100%;"/>
                                        </a>
									<?php endif; ?>
									<?php if ( $catname ) : ?>
                                        <div style="height: 22px; line-height: 22px; font-size: 20px;">&nbsp;</div>
                                        <font face="<?php echo $obj->tbm_nl_font_family; ?>"
                                              style="font-size:  <?php echo $label_font_size; ?>px; line-height:  <?php echo $label_line_height; ?>px; mso-line-height-rule:exactly;font-weight: 700; text-transform: uppercase;letter-spacing: 0.8px;">
                                        <span style="color: <?php echo $obj->tbl_nl_label_color; ?>;font-family: <?php echo $obj->tbm_nl_font_family; ?>; font-size:  <?php echo $label_font_size; ?>px; line-height:  <?php echo $label_line_height; ?>px; font-weight: 700; text-transform: uppercase;mso-line-height-rule:exactly;letter-spacing: 0.8px;">
                                            <a href="<?php echo $catlink; ?>"
                                               style="color: <?php echo $obj->tbl_nl_label_color; ?>"><?php echo $catname; ?></a></span>
                                        </font>
									<?php endif; ?>
                                    <div style="height: 4px; line-height: 4px; font-size: 8px;">&nbsp;</div>
                                    <font face="<?php echo $obj->tbm_nl_font_family; ?>" color="#101010"
                                          style="font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px;mso-line-height-rule:exactly;font-weight: 300; letter-spacing: -1px;">
                                    <span style="font-family: <?php echo $obj->tbm_nl_font_family; ?>; color: #101010; font-size: <?php echo $font_size; ?>px; line-height: <?php echo $line_height; ?>px;mso-line-height-rule:exactly; font-weight: 300; letter-spacing: -1px;"><a
                                                href="<?php echo $this->html_get_ga_link( $obj, $post->post_name, get_permalink( $post->ID ) ); ?>"
                                                target="_blank"
                                                style="font-family: <?php echo $obj->tbm_nl_font_family; ?>;color:#000"><?php echo get_the_title( $post ); ?></a></span>
                                    </font>
                                    <div style="height: 8px; line-height: 8px; font-size: 6px;">&nbsp;</div>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <!--[if (gte mso 9)|(IE)]></td>
                <?php
					endforeach;
					set_query_var( 'notin', $not_in );
					?>
                </tr>
                </table><![endif]-->
                </td>
            </tr>
        </table>
		<?php
		return true;
	}

	/**
	 * html code
	 *
	 * @param Object $obj The object containing all the options
	 */
	public function html_newsletter_display_html_code( $obj ) {
		echo $obj->tbm_nl_html_code;
	}

	/**
	 * Block banner or HTML code
	 *
	 * @param Object $obj The object containing all the options
	 */
	public function html_newsletter_display_banner( object $obj ) {

		if ( isset( $obj->tmb_site_id ) && is_multisite() ) {
			switch_to_blog( $obj->tmb_site_id );
		}

		$banner_image = $obj->tbm_nl_banner_img;
		$banner_url   = $obj->tbm_nl_banner_url;

		if ( $banner_image ) {
			if ( $banner_url ) {
				$nl_banner_embed = '<a href="' . $banner_url . '" style="color:#2d2a26;text-decoration:none"><img src="' . $banner_image . '" border="0" class="img-hero" style="outline:0;text-decoration:none;-ms-interpolation-mode:bicubic;width:auto;max-width:100%;clear:both;display:block;border:none" alt="banner image"> </a>';
			} else {
				$nl_banner_embed = '<img src="' . $banner_image . '" border="0" class="img-hero" style="outline:0;text-decoration:none;-ms-interpolation-mode:bicubic;width:auto;max-width:100%;clear:both;display:block;border:none">';
			}

			?>

            <table cellpadding="0" cellspacing="0" border="0" width="100%"
                   style="width: 100% !important; min-width: 100%; max-width: 100%;margin:15px 0">
                <tr>
                    <td align="center" valign="top">
                        <!--[if (gte mso 9)|(IE)]>
                        <table border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <td align="center" valign="top" width="275"><![endif]-->
						<?php echo $nl_banner_embed; ?>
                        <!--[if (gte mso 9)|(IE)]></td>
                        </tr>
                        </table><![endif]-->
                    </td>
                </tr>
            </table>

			<?php
			$this->html_newsletter_display_section_separator();
		}

		if ( isset( $obj->tmb_site_id ) && is_multisite() ) {
			restore_current_blog();
		}

	}


}
