<?php

class Tbm_Tagmailer_ACF {

	private $bundled_acf = false;

	private $enabled_acf = false;

	public function __construct() {
		// Define path and URL to the ACF plugin.
		define( 'MY_ACF_PATH', TBM_TAGMAILER_PLUGIN_DIR . 'includes/acf/' );
		define( 'MY_ACF_URL', TBM_TAGMAILER_PLUGIN_URL . 'includes/acf/' );

		// Include the ACF plugin.
		if ( ! class_exists( 'acf' ) ) {
			include_once( MY_ACF_PATH . 'acf.php' );
			$this->bundled_acf = true;
		}

		// Checks if PRO functions are available
		if ( ! function_exists( 'acf_add_options_page' ) ) {
			return;
		}

		$this->enabled_acf = true;
	}

	public function acf_settings_url( $url ) {
		if ( $this->bundled_acf ) {
			return MY_ACF_URL;
		}

		return $url;
	}

	public function acf_init_pages() {

		if ( ! $this->enabled_acf ) {
			return;
		}

		$args = array(
			'page_title' => 'Tagmailer',
			'menu_slug'  => 'tagmailer_config',
			'icon_url'   => 'dashicons-admin-settings',
			'capability' => 'edit_others_posts',
		);
		acf_add_options_page( $args );

		acf_add_options_sub_page( array(
			'menu_title'  => 'Creazione Newsletter',
			'page_title'  => 'Tagmailer Creazione Newsletter',
			'menu_slug'   => 'newsletter_blocks',
			'parent_slug' => 'tagmailer_config',
			'capability'  => 'edit_others_posts',
		) );

		acf_add_options_sub_page( array(
			'menu_title'  => 'Configurazione Newsletter',
			'page_title'  => 'Tagmailer Configurazione Newsletter',
			'menu_slug'   => 'newsletter_setup',
			'parent_slug' => 'tagmailer_config',
			'capability'  => 'activate_plugins',
		) );
	}

	public function acf_init_admin() {

		if ( ! $this->enabled_acf ) {
			return;
		}

		// Check
		if ( ! function_exists( 'acf_add_local_field_group' ) ) {
			return;
		}

		acf_add_local_field_group( array(
			'key'                   => 'group_5a5c762a42d20',
			'title'                 => 'Opzioni Newsletter',
			'fields'                => array(
				array(
					'key'               => 'field_5a5c76309222c',
					'label'             => 'Sospendi invio',
					'name'              => 'tbl_nl_suspend',
					'type'              => 'true_false',
					'instructions'      => '',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '33',
						'class' => '',
						'id'    => '',
					),
					'message'           => 'Sospendi l\'invio della newsletter quotidiana',
					'default_value'     => 0,
					'ui'                => 1,
					'ui_on_text'        => '',
					'ui_off_text'       => '',
				),

				array(
					'key'               => 'field_5a5c76309444r',
					'label'             => 'Disabilita profilazione',
					'name'              => 'tbl_nl_disable_profilation',
					'type'              => 'true_false',
					'instructions'      => '',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '33',
						'class' => '',
						'id'    => '',
					),
					'message'           => 'Disabilita la presenza di campi supplementari',
					'default_value'     => 0,
					'ui'                => 1,
					'ui_on_text'        => '',
					'ui_off_text'       => '',
				),
				array(
					'key'               => 'field_5a5c76309333c',
					'label'             => 'Form Automatico',
					'name'              => 'tbl_nl_automatic_form',
					'type'              => 'true_false',
					'instructions'      => '',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '33',
						'class' => '',
						'id'    => '',
					),
					'message'           => 'Inserisci form alla fine del contenuto',
					'default_value'     => 0,
					'ui'                => 1,
					'ui_on_text'        => '',
					'ui_off_text'       => '',
				),
				array(
					'key'               => 'field_5a5c76639333d',
					'label'             => 'Post Type abilitati all\'iscrizione',
					'name'              => 'tbl_nl_post_types_form',
					'type'              => 'select',
					'instructions'      => 'Seleziona la tipologia di post in cui vuoi mostrare il form di iscrizione alla newsletter',
					'required'          => 0,
					'conditional_logic' => array(
						array(
							array(
								'field'    => 'field_5a5c76309333c',
								'operator' => '==',
								'value'    => '1',
							),
						),
					),
					'wrapper'           => array(
						'width' => '',
						'class' => '',
						'id'    => '',
					),
					'choices'           => array(), //get_post_types( array( 'public' => true ) ),
					'default_value'     => array(),
					'allow_null'        => 0,
					'multiple'          => 1,
					'ui'                => 1,
					'ajax'              => 0,
					'return_format'     => 'value',
					'placeholder'       => '',
				),
				array(
					'key'           => 'field_3a5cdx6298iujh',
					'label'         => 'API Key',
					'name'          => 'tbl_tagmailer_api_key',
					'type'          => 'text',
					'instructions'  => 'La chiave da usare per le chiamate all\'API di Tagmailer',
					'default_value' => '',
					'allow_null'    => 0,
					'multiple'      => 0,
					'ui'            => 1,
					'ajax'          => 0,
					'return_format' => 'value',
					'placeholder'   => '',
				),
				array(
					'key'               => 'field_3a5cd786282rtg',
					'label'             => 'Lista di iscrizione di default',
					'name'              => 'tbl_nl_list_id',
					'type'              => 'select',
					'instructions'      => 'Scegli la lista di default in cui registrare gli utenti.',
					'required'          => 1,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '',
						'class' => '',
						'id'    => '',
					),
					'choices'           => array(), // tbm_nl_get_lists
					'default_value'     => array(),
					'allow_null'        => 0,
					'multiple'          => 0,
					'ui'                => 1,
					'ajax'              => 0,
					'return_format'     => 'value',
					'placeholder'       => '',
				),
				array(
					'key'               => 'field_653ce93c3e678',
					'label'             => 'Lista di iscrizione in base al contenuto',
					'name'              => 'tbm_nl_mutilist',
					'aria-label'        => '',
					'type'              => 'repeater',
					'instructions'      => 'Puoi specificare una lista di iscrizione diversa in base ad una combinazione di post type, categoria e tag. Se non specificato, verrà usata la lista di default. In caso di conflitti, il primo valore ha precedenza sugli altri. Un valore vuoto significa che quel campo vale per tutti gli elementi.',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '',
						'class' => '',
						'id'    => '',
					),
					'layout'            => 'table',
					'pagination'        => 0,
					'min'               => 0,
					'max'               => 0,
					'collapsed'         => '',
					'button_label'      => 'Aggiungi Combinazione',
					'rows_per_page'     => 20,
					'sub_fields'        => array(
						array(
							'key'               => 'field_653ce9513e679',
							'label'             => 'Post Type',
							'name'              => 'post_type',
							'aria-label'        => '',
							'type'              => 'select',
							'instructions'      => '',
							'required'          => 0,
							'conditional_logic' => 0,
							'wrapper'           => array(
								'width' => '25',
								'class' => '',
								'id'    => '',
							),
							'choices'           => array(),
							'default_value'     => false,
							'return_format'     => 'value',
							'multiple'          => 1,
							'allow_null'        => 0,
							'ui'                => 1,
							'ajax'              => 0,
							'placeholder'       => '',
							'parent_repeater'   => 'field_653ce93c3e678',
						),
						array(
							'key'                  => 'field_653ce9913e67a',
							'label'                => 'Categoria',
							'name'                 => 'category',
							'aria-label'           => '',
							'type'                 => 'taxonomy',
							'instructions'         => '',
							'required'             => 0,
							'conditional_logic'    => 0,
							'wrapper'              => array(
								'width' => '25',
								'class' => '',
								'id'    => '',
							),
							'taxonomy'             => 'category',
							'add_term'             => 0,
							'save_terms'           => 0,
							'load_terms'           => 0,
							'return_format'        => 'id',
							'field_type'           => 'multi_select',
							'allow_null'           => 0,
							'bidirectional'        => 0,
							'multiple'             => 0,
							'bidirectional_target' => array(),
							'parent_repeater'      => 'field_653ce93c3e678',
						),
						array(
							'key'                  => 'field_653ce9b83e67b',
							'label'                => 'Tag',
							'name'                 => 'post_tag',
							'aria-label'           => '',
							'type'                 => 'taxonomy',
							'instructions'         => '',
							'required'             => 0,
							'conditional_logic'    => 0,
							'wrapper'              => array(
								'width' => '25',
								'class' => '',
								'id'    => '',
							),
							'taxonomy'             => 'post_tag',
							'add_term'             => 0,
							'save_terms'           => 0,
							'load_terms'           => 0,
							'return_format'        => 'id',
							'field_type'           => 'multi_select',
							'allow_null'           => 0,
							'bidirectional'        => 0,
							'multiple'             => 0,
							'bidirectional_target' => array(),
							'parent_repeater'      => 'field_653ce93c3e678',
						),
						array(
							'key'               => 'field_653cee9bf8c9c',
							'label'             => 'Lista',
							'name'              => 'multilist_id',
							'aria-label'        => '',
							'type'              => 'select',
							'instructions'      => '',
							'required'          => 1,
							'conditional_logic' => 0,
							'wrapper'           => array(
								'width' => '',
								'class' => '',
								'id'    => '',
							),
							'choices'           => array(), // tbm_nl_get_lists
							'default_value'     => false,
							'return_format'     => 'value',
							'multiple'          => 0,
							'allow_null'        => 0,
							'ui'                => 0,
							'ajax'              => 0,
							'placeholder'       => '',
							'parent_repeater'   => 'field_653ce93c3e678',
						),
					),
				),
				array(
					'key'           => 'field_3a5cd786247hft',
					'label'         => 'Link alla privacy policy',
					'name'          => 'tbl_nl_policy_url',
					'type'          => 'text',
					'instructions'  => 'Inserisci qui l\'indirizzo da usare nel link della privacy policy. Di default è <code>/privacy-policy</code>',
					'default_value' => '/privacy-policy',
					'allow_null'    => 0,
					'multiple'      => 0,
					'ui'            => 1,
					'ajax'          => 0,
					'return_format' => 'value',
					'placeholder'   => '',
				),

			),
			'location'              => array(
				array(
					array(
						'param'    => 'options_page',
						'operator' => '==',
						'value'    => 'newsletter_setup',
					),
				),
			),
			'menu_order'            => 0,
			'position'              => 'normal',
			'style'                 => 'default',
			'label_placement'       => 'top',
			'instruction_placement' => 'label',
			'hide_on_screen'        => '',
			'active'                => 1,
			'description'           => '',
		) );


	}

	public function acf_init_blocks() {

		if ( ! $this->enabled_acf ) {
			return;
		}

		/**
		 * Define sub fields
		 */
		$blocco_tipologia = array(
			array(
				'key'               => 'field_5aa6787ff5a44',
				'label'             => 'Titolo della sezione',
				'name'              => 'tbm_nl_block_title',
				'type'              => 'text',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_5a577ed7c17df',
				'label'             => 'Elenco post per Post Type',
				'name'              => 'tbm_nl_post_type',
				'type'              => 'select',
				'instructions'      => '',
				'required'          => 1,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(), //tbm_get_post_types( array( 'public' => true ) ),
				'default_value'     => array(),
				'allow_null'        => 1,
				'multiple'          => 1,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
				'disabled'          => 0,
				'readonly'          => 0,
			),
			array(
				'key'               => 'field_5aa1a11ba1ab2',
				'label'             => 'Layout',
				'name'              => 'tbm_nl_layout',
				'type'              => 'select',
				'instructions'      => 'Il layout triplete mostra tre piccoli box con immagine su un rigo',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(
					'elenco'  => 'Elenco',
					'tre'     => 'Triplete',
					'excerpt' => 'Elenco con riassunto'
				),
				'default_value'     => array(),
				'allow_null'        => 0,
				'multiple'          => 0,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
				'disabled'          => 0,
				'readonly'          => 0,
			),
			array(
				'key'               => 'field_5a577da12e85f',
				'label'             => 'Numero di elementi',
				'name'              => 'tbm_nl_numbers',
				'type'              => 'number',
				'instructions'      => 'Quanti elementi mostrare? Vale solo nel caso di Layout Elenco',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 1,
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'min'               => 1,
				'max'               => '',
				'step'              => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_5a58adba8a008',
				'label'             => 'Utilizza immagine in evidenza',
				'name'              => 'tbm_nl_use_featured_img',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '25',
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 1,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),

			array(
				'key'               => 'field_5aa192cc620qz',
				'label'             => 'Ignora la data unica',
				'name'              => 'tbm_nl_ignore_post_date',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 0,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),
			array(
				'key'               => 'field_5ee9d30a7dfca',
				'label'             => 'Pubblica contenuti casuali',
				'name'              => 'tbm_nl_random_content',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 0,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),
			array(
				'key'               => 'field_5ee9e7457dfcc',
				'label'             => 'Degli ultimi ...',
				'name'              => 'tbm_nl_last_number',
				'type'              => 'number',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '34',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 1,
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'min'               => 1,
				'max'               => 31,
				'step'              => 1,
			),
			array(
				'key'               => 'field_5ee9e7637dfcd',
				'label'             => 'Data',
				'name'              => 'tbm_nl_last_date',
				'type'              => 'select',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(
					'months' => 'Mesi',
					'years'  => 'Anni',
					'days'   => 'Giorni'
				),
				'default_value'     => array(),
				'allow_null'        => 0,
				'multiple'          => 0,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
			),
		);
		$blocco_tag       = array(
			array(
				'key'               => 'field_5aa6787ff5a24',
				'label'             => 'Titolo della sezione',
				'name'              => 'tbm_nl_block_title',
				'type'              => 'text',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_5a577ddb8379e',
				'label'             => 'Tags',
				'name'              => 'tbm_nl_tag',
				'type'              => 'taxonomy',
				'instructions'      => 'Uno o più tag da cui seleziona i post da mostrare',
				'required'          => 1,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'taxonomy'          => 'post_tag',
				'field_type'        => 'multi_select',
				'allow_null'        => 0,
				'add_term'          => 0,
				'save_terms'        => 0,
				'load_terms'        => 0,
				'return_format'     => 'value'
			),
			array(
				'key'               => 'field_z1a98q5c5b',
				'label'             => 'Layout',
				'name'              => 'tbm_nl_layout',
				'type'              => 'select',
				'instructions'      => 'Il layout triplete mostra, su un rigo, tre piccoli box con thumbnail ',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(
					'elenco'  => 'Elenco',
					'tre'     => 'Triplete',
					'excerpt' => 'Elenco con riassunto'
				),
				'default_value'     => array(),
				'allow_null'        => 0,
				'multiple'          => 0,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
				'disabled'          => 0,
				'readonly'          => 0,
			),
			array(
				'key'               => 'field_rx1c7yi8y6',
				'label'             => 'Numero di elementi',
				'name'              => 'tbm_nl_numbers',
				'type'              => 'number',
				'instructions'      => 'Quanti elementi mostrare? Per il layout triplete devono essere almeno 3',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 1,
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'min'               => 1,
				'max'               => '',
				'step'              => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_1ldxf8oxyv',
				'label'             => 'Utilizza immagine in evidenza',
				'name'              => 'tbm_nl_use_featured_img',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 1,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),

			array(
				'key'               => 'field_5aa192cc696yt',
				'label'             => 'Ignora la data unica',
				'name'              => 'tbm_nl_ignore_post_date',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 0,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),
			array(
				'key'               => 'field_5ee9d30a7dfja',
				'label'             => 'Pubblica contenuti casuali',
				'name'              => 'tbm_nl_random_content',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 0,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),
			array(
				'key'               => 'field_5ee9e7457dfjc',
				'label'             => 'Degli ultimi ...',
				'name'              => 'tbm_nl_last_number',
				'type'              => 'number',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '34',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 1,
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'min'               => 1,
				'max'               => 31,
				'step'              => 1,
			),
			array(
				'key'               => 'field_5ee9e7637dfjd',
				'label'             => 'Data',
				'name'              => 'tbm_nl_last_date',
				'type'              => 'select',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(
					'months' => 'Mesi',
					'years'  => 'Anni',
					'days'   => 'Giorni'
				),
				'default_value'     => array(),
				'allow_null'        => 0,
				'multiple'          => 0,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
			),
		);
		$blocco_cat       = array(
			array(
				'key'               => 'field_5aa6787ff5a14',
				'label'             => 'Titolo della sezione',
				'name'              => 'tbm_nl_block_title',
				'type'              => 'text',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_5a577d182e85e',
				'label'             => 'Categorie',
				'name'              => 'tbm_nl_category',
				'type'              => 'taxonomy',
				'instructions'      => 'Una o più categorie da cui selezionare i post da mostrare',
				'required'          => 1,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'taxonomy'          => 'category',
				'field_type'        => 'multi_select',
				'allow_null'        => 0,
				'add_term'          => 0,
				'save_terms'        => 0,
				'load_terms'        => 0,
				'return_format'     => 'value',
			),
			array(
				'key'               => 'field_l84pn96bhq',
				'label'             => 'Layout',
				'name'              => 'tbm_nl_layout',
				'type'              => 'select',
				'instructions'      => 'Il layout triplete mostra tre piccoli box con immagine su un rigo',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(
					'elenco'  => 'Elenco',
					'tre'     => 'Triplete',
					'excerpt' => 'Elenco con riassunto'
				),
				'default_value'     => array(),
				'allow_null'        => 0,
				'multiple'          => 0,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
				'disabled'          => 0,
				'readonly'          => 0,
			),
			array(
				'key'               => 'field_feh427gqi6',
				'label'             => 'Numero di elementi',
				'name'              => 'tbm_nl_numbers',
				'type'              => 'number',
				'instructions'      => 'Quanti elementi mostrare? Vale solo nel caso di Layout Elenco',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 1,
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'min'               => 1,
				'max'               => '',
				'step'              => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_ccxpw2k1a5',
				'label'             => 'Utilizza immagine in evidenza',
				'name'              => 'tbm_nl_use_featured_img',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 1,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),

			array(
				'key'               => 'field_5aa192cc639jc',
				'label'             => 'Ignora la data unica',
				'name'              => 'tbm_nl_ignore_post_date',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => 25,
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 0,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),
			array(
				'key'               => 'field_5ee9d30a7dfxa',
				'label'             => 'Pubblica contenuti casuali',
				'name'              => 'tbm_nl_random_content',
				'type'              => 'true_false',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'message'           => '',
				'default_value'     => 0,
				'ui'                => 1,
				'ui_on_text'        => '',
				'ui_off_text'       => '',
			),
			array(
				'key'               => 'field_5ee9e7457dfxc',
				'label'             => 'Degli ultimi ...',
				'name'              => 'tbm_nl_last_number',
				'type'              => 'number',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '34',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 1,
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'min'               => 1,
				'max'               => 31,
				'step'              => 1,
			),
			array(
				'key'               => 'field_5ee9e7637dfxd',
				'label'             => 'Data',
				'name'              => 'tbm_nl_last_date',
				'type'              => 'select',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(
					'months' => 'Mesi',
					'years'  => 'Anni',
					'days'   => 'Giorni'
				),
				'default_value'     => array(),
				'allow_null'        => 0,
				'multiple'          => 0,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
			),
		);

		/**
		 * Define configs fields
		 */
		$configurazione = array(
			array(
				'key'               => 'field_5a58ccc74da77',
				'label'             => 'Titolo Newsletter',
				'name'              => 'nl_newsletter_title',
				'type'              => 'text',
				'instructions'      => 'Il titolo della newsletter. Puoi usare: <code>%date%</code> per inserire la data di invio; <code>%post%</code> per usare il titolo del <strong>primo articolo</strong> della newsletter.',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 'Novità %date%',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '149',
			),
			array(
				'key'               => 'field_5a58ccc23we77',
				'label'             => 'Sottotitolo Newsletter',
				'name'              => 'nl_newsletter_subtitle',
				'type'              => 'text',
				'instructions'      => 'Il testo indicato al di sotto del logo. Se non compilato verrà utilizzata la data di invio',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '35',
			),
			array(
				'key'               => 'field_5bb3951273ca3',
				'label'             => 'Configurazione',
				'name'              => '',
				'type'              => 'tab',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'placement'         => 'top',
				'endpoint'          => 0,
			),
			array(
				'key'               => 'field_5a58c4c32b2a8',
				'label'             => 'Logo',
				'name'              => 'nl_newsletter_logo',
				'type'              => 'image',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '50',
					'class' => '',
					'id'    => '',
				),
				'return_format'     => 'array',
				'preview_size'      => 'thumbnail',
				'library'           => 'all',

			),
			array(
				'key'               => 'field_5a58c4e02b2a9',
				'label'             => 'Url Link Logo',
				'name'              => 'nl_newsletter_logo_url',
				'type'              => 'url',
				'instructions'      => 'Url di collegamento del logo',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '50',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
			),
			array(
				'key'               => 'field_5a577ed7c13e2',
				'label'             => 'Tassonomia per le label',
				'name'              => 'nl_post_type_label',
				'type'              => 'select',
				'instructions'      => 'Quale tassonomia usare per le etichette al di sopra dei titoli',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '38',
					'class' => '',
					'id'    => '',
				),
				'choices'           => get_taxonomies( array( 'public' => true ) ),
				'default_value'     => array(),
				'allow_null'        => 1,
				'multiple'          => 0,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
				'disabled'          => 0,
				'readonly'          => 0,
			),

			array(
				'key'               => 'field_5a577ed7cKJ7R',
				'label'             => 'Post Type Pilota',
				'name'              => 'nl_pilot_post_type',
				'type'              => 'select',
				'instructions'      => 'Il post type che genera la data unica di pubblicazione',
				'required'          => 1,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '38',
					'class' => '',
					'id'    => '',
				),
				'choices'           => array(), //tbm_get_post_types( array( 'public' => true ) ),
				'default_value'     => array(),
				'allow_null'        => 1,
				'multiple'          => 1,
				'ui'                => 1,
				'ajax'              => 0,
				'return_format'     => 'value',
				'placeholder'       => '',
				'disabled'          => 0,
				'readonly'          => 0,
			),
			array(
				'key'               => 'field_3a38c5872b2ac',
				'label'             => 'Giorni di pubblicazione a ritroso',
				'name'              => 'nl_newsletter_days_ago',
				'type'              => 'number',
				'instructions'      => '1 pubblica i post di ieri.',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '24',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => 1,
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'min'               => '',
				'max'               => '',
				'step'              => '',
			),
			array(
				'key'               => 'field_5b0c2db67s098',
				'label'             => 'Riassunto',
				'name'              => 'nl_newsletter_excerpt',
				'type'              => 'textarea',
				'instructions'      => 'Il testo qui indicato verrà pubblicato nella preview della email',
				'default_value'     => 'I principali articoli prodotti dalla redazione direttamente nella vostra casella email',
				'required'          => 0,
				'conditional_logic' => 0,
				'rows'              => 2,
				'maxlength'         => 155,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				)
			),
			// Disclaimer
			array(
				'key'               => 'field_5b0c2db67f154',
				'label'             => 'Disclaimer',
				'name'              => 'nl_newsletter_disclaimer',
				'type'              => 'textarea',
				'instructions'      => 'Il testo qui indicato verrà pubblicato a fondo newsletter',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				)
			),
			// Colori
			array(
				'key'               => 'field_5a9d92d43ab39',
				'label'             => 'Colore primario',
				'name'              => 'nl_primary_color_newsletter',
				'type'              => 'color_picker',
				'instructions'      => 'Il colore dello sfondo dei titoli',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
			),
			array(
				'key'               => 'field_5a9d936a3po9y',
				'label'             => 'Colore titoli',
				'name'              => 'nl_heading_color_newsletter',
				'type'              => 'color_picker',
				'instructions'      => 'Il colore del testo dei titoli ',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '34',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
			),
			array(
				'key'               => 'field_5a9d934f3ab3b',
				'label'             => 'Colore label',
				'name'              => 'nl_label_color_newsletter',
				'type'              => 'color_picker',
				'instructions'      => 'Colore della label delle categorie',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
			),
			// Font css
			array(
				'key'               => 'field_5a5777438iujhg',
				'label'             => 'URL Web Font',
				'name'              => 'nl_web_font',
				'type'              => 'url',
				'instructions'      => 'Inserire qui l\'url del webfont da utilizzare. Se non si inserisce nulla verrà utilizzato il font di default (Source Sans Pro). Esempio <em>https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700</em>',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_5a5773dsjYh54',
				'label'             => 'Font-family',
				'name'              => 'nl_font_family',
				'type'              => 'text',
				'instructions'      => 'Il font da utilizzare per la newsletter. Esempio <em>Source Sans Pro, Verdana, sans-serif</em>',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '50',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key' => 'field_6552a70d42f83',
				'label' => 'Dimensione del font del titolo',
				'name' => 'nl_font_title_multiplier',
				'aria-label' => '',
				'type' => 'number',
				'instructions' => 'Le dimensioni del font dei titoli possono essere modificate aumentando o diminuendo questo parametro',
				'required' => 0,
				'conditional_logic' => 0,
				'wrapper' => array(
					'width' => '50',
					'class' => '',
					'id' => '',
				),
				'default_value' => 1,
				'min' => '0.5',
				'max' => '1.5',
				'placeholder' => '',
				'step' => '0.1',
				'prepend' => '',
				'append' => '',
			),
			// Analytics UTM
			array(
				'key'               => 'field_5a5773dsju7yt5',
				'label'             => 'Campaign (UTM) Source',
				'name'              => 'nl_utm_source',
				'type'              => 'text',
				'instructions'      => 'Il valore del parametro <em>utm_source</em> (es. newsletter)',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_5a5773dsju7yt6',
				'label'             => 'Campaign (UTM) Medium',
				'name'              => 'nl_utm_medium',
				'type'              => 'text',
				'instructions'      => 'Il valore del parametro <em>utm_medium</em> (es. email)',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '34',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
			array(
				'key'               => 'field_5a5773dsju7yt7',
				'label'             => 'Campaign (UTM) Name',
				'name'              => 'nl_utm_campaign',
				'type'              => 'text',
				'instructions'      => 'Il valore del parametro <em>utm_campaign</em> (es. newsletter principale)',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '33',
					'class' => '',
					'id'    => '',
				),
				'default_value'     => '',
				'placeholder'       => '',
				'prepend'           => '',
				'append'            => '',
				'maxlength'         => '',
				'readonly'          => 0,
				'disabled'          => 0,
			),
		);

		/**
		 * Define blocks fields
		 */
		$blocchi_layouts_default = array(
			array(
				'key'        => '5a57773236329',
				'name'       => 'tbm_nl_html_code_group',
				'label'      => 'Codice HTML',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => '4b0c2dv67f154',
						'label'             => 'Codice HTML',
						'name'              => 'tbm_nl_html_code',
						'type'              => 'textarea',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => 'tbm_nl_html_code',
							'id'    => '',
						)
					),
				),
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a57773036329',
				'name'       => 'tbm_nl_title',
				'label'      => 'Titolo',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => 'field_5a5777437af64',
						'label'             => 'Titolo',
						'name'              => 'tbm_nl_block_title',
						'type'              => 'text',
						'instructions'      => '',
						'required'          => 1,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
						'prepend'           => '',
						'append'            => '',
						'maxlength'         => '',
						'readonly'          => 0,
						'disabled'          => 0,
					),
				),
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a5777u033hys',
				'name'       => 'tbm_nl_button',
				'label'      => 'Pulsante/CTA',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => 'field_5687hff5a34',
						'label'             => 'Titolo della sezione',
						'name'              => 'tbm_nl_block_title',
						'type'              => 'text',
						'instructions'      => 'Facoltativo: p.es. Scarica il PDF',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
						'prepend'           => '',
						'append'            => '',
						'maxlength'         => '',
						'readonly'          => 0,
						'disabled'          => 0,
					),
					array(
						'key'               => 'cta_5ced36bbfb269',
						'label'             => 'Url',
						'name'              => 'tbm_nl_button_url',
						'type'              => 'url',
						'instructions'      => 'Destinazione del clic sulla CTA',
						'required'          => 1,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '50',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
					),
					array(
						'key'               => 'cta_5a577345af64',
						'label'             => 'Cta',
						'name'              => 'tbm_nl_button_cta',
						'type'              => 'text',
						'instructions'      => 'Testo sul pulsante della CTA',
						'required'          => 1,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '50',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
					),
					array(
						'key'               => 'cta_5a5777437af64',
						'label'             => 'Testo',
						'name'              => 'tbm_nl_button_text',
						'type'              => 'textarea',
						'instructions'      => 'Il testo sarà pubblicato al di sopra della CTA',
						'required'          => 0,
						'conditional_logic' => 0,
						'rows'              => '3',
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
					),

				),
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a577796f3a5b',
				'name'       => 'tbm_nl_post',
				'label'      => 'Singolo Articolo - Grande',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => 'field_5aa6787ff5a68',
						'label'             => 'Titolo della sezione',
						'name'              => 'tbm_nl_block_title',
						'type'              => 'text',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
						'prepend'           => '',
						'append'            => '',
						'maxlength'         => '',
						'readonly'          => 0,
						'disabled'          => 0,
					),
					array(
						'key'               => 'field_5a8eb4c9d8e04',
						'label'             => 'Ultimo post inserito',
						'name'              => 'tbm_nl_last_post',
						'type'              => 'true_false',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'message'           => '',
						'default_value'     => 0,
						'ui'                => 1,
						'ui_on_text'        => '',
						'ui_off_text'       => '',
					),
					array(
						'key'               => 'field_5a57796e233cf',
						'label'             => 'Post',
						'name'              => 'tbm_nl_post',
						'type'              => 'post_object',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => array(
							array(
								array(
									'field'    => 'field_5a8eb4c9d8e04',
									'operator' => '!=',
									'value'    => '1',
								),
								array(
									'field'    => 'field_5aa18dbf612fc',
									'operator' => '!=',
									'value'    => '1',
								),
							),
						),
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'post_type'         => array(),
						'taxonomy'          => array(),
						'allow_null'        => 1,
						'multiple'          => 0,
						'return_format'     => 'object',
						'ui'                => 1,
					),
				),
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a577c8a2b618',
				'name'       => 'tbm_nl_posts',
				'label'      => 'Elenco Manuale',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => 'field_5aa6787ff5a34',
						'label'             => 'Titolo della sezione',
						'name'              => 'tbm_nl_block_title',
						'type'              => 'text',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
						'prepend'           => '',
						'append'            => '',
						'maxlength'         => '',
						'readonly'          => 0,
						'disabled'          => 0,
					),
					array(
						'key'               => 'field_5a577c9c2b619',
						'label'             => 'Posts',
						'name'              => 'tbm_nl_posts',
						'type'              => 'relationship',
						'instructions'      => '',
						'required'          => 1,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'post_type'         => array(),
						'taxonomy'          => array(),
						'filters'           => array(
							0 => 'search',
							1 => 'post_type'
						),
						'elements'          => '',
						'min'               => 1,
						'max'               => '',
						'return_format'     => 'object',
					),
					array(
						'key'               => 'field_4a58ae3649c24',
						'label'             => 'Utilizza immagine in primo piano',
						'name'              => 'tbm_nl_use_featured_img',
						'type'              => 'true_false',
						'instructions'      => 'Se non spuntato si userà, se presente nel template, l\'icona del post type specifico',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => 50,
							'class' => '',
							'id'    => '',
						),
						'message'           => '',
						'default_value'     => 1,
						'ui'                => 1,
						'ui_on_text'        => '',
						'ui_off_text'       => '',
					)
				),
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a577cf12e85d',
				'name'       => 'tbm_nl_categories',
				'label'      => 'Elenco post per Categoria',
				'display'    => 'block',
				'sub_fields' => $blocco_cat,
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a577ddb8379d',
				'name'       => 'tbm_nl_tags',
				'label'      => 'Elenco post per Tag',
				'display'    => 'block',
				'sub_fields' => $blocco_tag,
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a577ed7c17de',
				'name'       => 'tbm_nl_posttypes',
				'label'      => 'Elenco post per Tipologia',
				'display'    => 'block',
				'sub_fields' => $blocco_tipologia,
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a577c8a2h8738',
				'name'       => 'tbm_nl_banner',
				'label'      => 'Banner',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => 'field_5ced3533fb268',
						'label'             => 'Immagine',
						'name'              => 'tbm_nl_banner_img',
						'type'              => 'image',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '50',
							'class' => '',
							'id'    => '',
						),
						'return_format'     => 'url',
						'preview_size'      => 'thumbnail',
						'library'           => 'all',
						'min_width'         => '',
						'min_height'        => '',
						'min_size'          => '',
						'max_width'         => 580,
						'max_height'        => 580,
						'max_size'          => '0.5',
						'mime_types'        => '',
					),
					array(
						'key'               => 'field_5ced36bbfb269',
						'label'             => 'Url',
						'name'              => 'tbm_nl_banner_url',
						'type'              => 'url',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '50',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
					),

				),
				'min'        => '',
				'max'        => '',
			)
		);

		$blocchi_layouts = array(
			array(
				'key'        => '5a577796f3a5b',
				'name'       => 'tbm_nl_post',
				'label'      => 'Singolo Articolo - Grande',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => 'field_5aa6787ff5a68',
						'label'             => 'Titolo della sezione',
						'name'              => 'tbm_nl_block_title',
						'type'              => 'text',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
						'prepend'           => '',
						'append'            => '',
						'maxlength'         => '',
						'readonly'          => 0,
						'disabled'          => 0,
					),
					array(
						'key'               => 'field_5a8eb4c9d8e04',
						'label'             => 'Ultimo post inserito',
						'name'              => 'tbm_nl_last_post',
						'type'              => 'true_false',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'message'           => '',
						'default_value'     => 0,
						'ui'                => 1,
						'ui_on_text'        => '',
						'ui_off_text'       => '',
					),
					array(
						'key'               => 'field_5aa191cc62xer',
						'label'             => 'Ignora la data unica',
						'name'              => 'tbm_nl_ignore_post_date',
						'type'              => 'true_false',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'message'           => '',
						'default_value'     => 0,
						'ui'                => 1,
						'ui_on_text'        => '',
						'ui_off_text'       => '',
					),
					array(
						'key'               => 'field_5a57796e233cf',
						'label'             => 'Post',
						'name'              => 'tbm_nl_post',
						'type'              => 'post_object',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => array(
							array(
								array(
									'field'    => 'field_5a8eb4c9d8e04',
									'operator' => '!=',
									'value'    => '1',
								),
								array(
									'field'    => 'field_5aa18dbf612fc',
									'operator' => '!=',
									'value'    => '1',
								),
							),
						),
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'post_type'         => array(),
						'taxonomy'          => array(),
						'allow_null'        => 1,
						'multiple'          => 0,
						'return_format'     => 'object',
						'ui'                => 1,
					),
				),
				'min'        => '',
				'max'        => '',
			),
			array(
				'key'        => '5a577c8a2b618',
				'name'       => 'tbm_nl_posts',
				'label'      => 'Elenco Manuale',
				'display'    => 'block',
				'sub_fields' => array(
					array(
						'key'               => 'field_5aa6787ff5a34',
						'label'             => 'Titolo della sezione',
						'name'              => 'tbm_nl_block_title',
						'type'              => 'text',
						'instructions'      => '',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'default_value'     => '',
						'placeholder'       => '',
						'prepend'           => '',
						'append'            => '',
						'maxlength'         => '',
						'readonly'          => 0,
						'disabled'          => 0,
					),
					array(
						'key'               => 'field_5a577c9c2b619',
						'label'             => 'Posts',
						'name'              => 'tbm_nl_posts',
						'type'              => 'relationship',
						'instructions'      => '',
						'required'          => 1,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => '',
							'class' => '',
							'id'    => '',
						),
						'post_type'         => array(),
						'taxonomy'          => array(),
						'filters'           => array(
							0 => 'search',
							1 => 'post_type'
						),
						'elements'          => '',
						'min'               => 1,
						'max'               => '',
						'return_format'     => 'object',
					),
					array(
						'key'               => 'field_4a58ae3649c24',
						'label'             => 'Utilizza immagine in primo piano',
						'name'              => 'tbm_nl_use_featured_img',
						'type'              => 'true_false',
						'instructions'      => 'Se non spuntato si userà, se presente nel template, l\'icona del post type specifico',
						'required'          => 0,
						'conditional_logic' => 0,
						'wrapper'           => array(
							'width' => 50,
							'class' => '',
							'id'    => '',
						),
						'message'           => '',
						'default_value'     => 1,
						'ui'                => 1,
						'ui_on_text'        => '',
						'ui_off_text'       => '',
					)
				),
				'min'        => '',
				'max'        => '',
			),
		);

		$extra_blocks            = apply_filters( 'newsletter_custom_blocks', array() );
		$blocchi_layouts_default = array_merge( $blocchi_layouts_default, $extra_blocks );

		$blocchi = array(
			array(
				'key'               => 'field_5bb3957073ca4',
				'label'             => 'Contenuti',
				'name'              => '',
				'type'              => 'tab',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'placement'         => 'top',
				'endpoint'          => 0,
			),
			array(
				'key'               => 'field_5a5777197af63',
				'label'             => 'Selezione dei blocchi',
				'name'              => 'nl_blocchi_newsletter',
				'type'              => 'flexible_content',
				'instructions'      => '',
				'required'          => 0,
				'conditional_logic' => 0,
				'wrapper'           => array(
					'width' => '',
					'class' => '',
					'id'    => '',
				),
				'button_label'      => 'Aggiungi Blocco',
				'min'               => '',
				'max'               => '',
				'layouts'           => $blocchi_layouts_default,
			)
		);

		// $fields = array_merge( $configurazione, $blocchi, $spot );
		$fields = array_merge( $configurazione, $blocchi );

		acf_add_local_field_group( array(
			'key'                   => 'group_5c6448d28cb4c',
			'title'                 => ' ',
			'fields'                => array(
				array(
					'key'               => 'field_5bb4d3f7e3769',
					'label'             => 'Istruzioni',
					'name'              => '',
					'type'              => 'message',
					'instructions'      => '',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '',
						'class' => '',
						'id'    => '',
					),
					'message'           => 'Per impostare la newsletter compilare i campi che si trovano nella scheda <strong>Blocchi</strong> aggiungendo i layout desiderati. La newsletter è configurata per includere articoli pubblicati nello stesso giorno. Il giorno di pubblicazione viene determinato cercando, a ritroso, l’ultimo giorno in cui è stato pubblicato un articolo sul sito. Si possono modificare questi parametri in due modi: indicando da quale giorno partire per andare a ritroso (con il campo <em>Giorni da sottrarre ad oggi per la composizione</em> nella scheda <em>Configurazione</em>) e indicando quale tipologia di contenuto deve essere controllato per definire la data unica (si deve agire sulla lista <em>Post pilota</em>).',
					'new_lines'         => 'wpautop',
					'esc_html'          => 0,
				),
				array(
					'key'               => 'field_5ce48df9f2asc',
					'label'             => 'Composizione Newsletter',
					'name'              => 'tbm',
					'type'              => 'repeater',
					'instructions'      => '',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '',
						'class' => 'mb10',
						'id'    => '',
					),
					'collapsed'         => 'field_5a58ccc74da77',
					'min'               => 1,
					'max'               => 100,
					'layout'            => 'block',
					'button_label'      => 'Aggiungi un\'altra Newsletter',
					'sub_fields'        => $fields,
				),
			),
			'location'              => array(
				array(
					array(
						'param'    => 'options_page',
						'operator' => '==',
						'value'    => 'newsletter_blocks',
					),
				),
			),
			'menu_order'            => 0,
			'position'              => 'normal',
			'style'                 => 'seamless',
			'label_placement'       => 'top',
			'instruction_placement' => 'label',
			'hide_on_screen'        => '',
			'active'                => 1,
			'description'           => '',
		) );

		acf_add_local_field_group( array(
			'key'                   => 'group_653be7a99681e',
			'title'                 => 'Tagmailer',
			'fields'                => array(
				array(
					'key'               => 'field_653be7a9ca95e',
					'label'             => 'CTA',
					'name'              => 'tagmailer-form-cta',
					'aria-label'        => '',
					'type'              => 'textarea',
					'instructions'      => 'La CTA verrà mostrata al di sopra del form. Se non viene inserita, verrà mostrata la CTA predefinita. Puoi usare %%TITLE%% per far comparire la categoria o, se assente, il titolo dell\'articolo nel punto che desideri. Esempio: <em>Se vuoi avere più informazioni su %%TITLE%% registrati alla nostra newsletter</em>.',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '',
						'class' => '',
						'id'    => '',
					),
					'default_value'     => '',
					'new_lines'         => '',
					'maxlength'         => '',
					'placeholder'       => '',
					'rows'              => '3',
				),
				array(
					'key'               => 'field_653be7baca95f',
					'label'             => 'ID della lista',
					'name'              => 'tagmailer-form-containerid',
					'aria-label'        => '',
					'type'              => 'number',
					'instructions'      => 'L\'ID della lista su cui registrare gli utenti. Se non viene inserito, verrà usata la lista predefinita.',
					'required'          => 0,
					'conditional_logic' => 0,
					'wrapper'           => array(
						'width' => '',
						'class' => '',
						'id'    => '',
					),
					'default_value'     => '',
					'min'               => '',
					'max'               => '',
					'step'              => '',
					'placeholder'       => '',
					'prepend'           => '',
					'append'            => '',
				),
			),
			'location'              => array(
				array(
					array(
						'param'    => 'block',
						'operator' => '==',
						'value'    => 'tbm-tagmailer/tagmailer-form',
					),
				),
			),
			'menu_order'            => 0,
			'position'              => 'normal',
			'style'                 => 'default',
			'label_placement'       => 'top',
			'instruction_placement' => 'label',
			'hide_on_screen'        => '',
			'active'                => true,
			'description'           => '',
			'show_in_rest'          => 0,
		) );
	}

	public function acf_init_filters() {

		if ( ! $this->enabled_acf ) {
			return;
		}

		add_filter( 'acf/load_field/key=field_5a5c76639333d', function ( $fields ) {
			$fields['choices'] = $this->get_post_types();

			return $fields;
		} );

		add_filter( 'acf/load_field/key=field_5a577ed7c17df', function ( $fields ) {
			$fields['choices'] = $this->get_post_types();

			return $fields;
		} );

		add_filter( 'acf/load_field/key=field_5a577ed7cKJ7R', function ( $fields ) {
			$fields['choices'] = $this->get_post_types();

			return $fields;
		} );

		add_filter( 'acf/load_field/key=field_3a5cd786282rtg', function ( $fields ) {
			$fields['choices'] = $this->get_tagmailer_lists();

			return $fields;
		} );

		add_filter( 'acf/load_field/key=field_653cee9bf8c9c', function ( $fields ) {
			$fields['choices'] = $this->get_tagmailer_lists();

			return $fields;
		} );

		add_filter( 'acf/load_field/key=field_653ce9513e679', function ( $fields ) {
			$fields['choices'] = $this->get_post_types();

			return $fields;
		} );

		add_filter( 'acf/prepare_field/key=field_5a58ccc74da77', function ( $field ) {

			// Act only in admin
			if ( ! is_admin() ) {
				return $field;
			}

			$current_label = $field['label'];
			$current_id    = $field['id'];

			// Get the newsletter ID from the id field
			if ( ! preg_match( '#row-([0-9]{1,2})#', $current_id, $matches ) ) {
				return $field;
			}

			// Add one unit to newsletter ID to match the type preview
			$newsletter_id = (int) $matches[1] + 1;

			// Define home_url
			$home_url = home_url();

			// Create the html link
			$html_link_template = '[<a target="_blank" rel="noopener" href="%s">anteprima</a>]';
			$html_link          = sprintf( $html_link_template, add_query_arg( array(
				'newsletter' => 'html',
				'type'       => $newsletter_id
			), $home_url ) );

			// Create the xml link
			$xml_link_template = '[<a target="_blank" rel="noopener" href="%s">file xml</a>]';
			$xml_link          = sprintf( $xml_link_template, add_query_arg( array(
				'newsletter' => 'xml',
				'type'       => $newsletter_id
			), $home_url ) );

			$field['label'] = $current_label . " {$html_link} • {$xml_link}";

			return $field;
		} );

		add_filter( 'acf/fields/flexible_content/layout_title', function ( $title, $field, $layout, $i ) {
			if ( $field['label'] === 'Selezione dei blocchi' ) {
				$pre_title = get_sub_field( 'tbm_nl_block_title' ) ?: '<span style="color:#c0c0c0">Senza titolo</span>';

				return '<em style="font-size: 110%;font-weight: bold">' . $pre_title . '</em> - <small>' . $title . '</small>';
			}

			return $title;
		}, 10, 4 );
	}

	public function __return_false() {
		return false;
	}

	private function get_post_types() {
		$out = array();
		$post_types = get_post_types( array( 'public' => true ) );
		$exclude    = array(
			'attachment',
			'revision',
			'nav_menu_item',
			'custom_css',
			'customize_changeset',
			'oembed_cache',
			'user_request',
			'wp_block',
			'acf-field-group',
			'acf-field'
		);
		foreach ( $exclude as $ex ) {
			unset( $post_types[ $ex ] );
		}

		foreach ( $post_types as $post_type ) {
			$out[ $post_type ] = get_post_type_object( $post_type )->labels->singular_name;
		}

		return $out;
	}

	private function get_tagmailer_lists() {
		if ( ! $this->enabled_acf ) {
			return array();
		}

		if ( ! is_admin() ) {
			return array();
		}

		if ( ! get_field( 'tbl_tagmailer_api_key', 'option' ) ) {
			return array( '0' => 'No API key defined' );
		}

		$api_key = get_field( 'tbl_tagmailer_api_key', 'option' );
		$out     = get_option( 'tbm_nl_lists' );

		// If exists, and we are not in admin, return saved data
		if ( $out && ! is_admin() ) {
			return $out;
		}

		// Define array
		$out = array();

		// Regenerate data
		$response = wp_remote_get( TBM_TAGMAILER_V5_API . '/containers?limit=150',
			array( 'timeout' => 60, 'headers' => array( 'accept' => 'application/json', 'Api-Key' => $api_key ) )
		);

		if ( is_wp_error( $response ) ) {
			return array( '0' => 'No list available (' . $response->get_error_message() . ')' );
		}

		if ( 403 === wp_remote_retrieve_response_code( $response ) ) {
			return array( '0' => 'Invalid Api Key' );
		}

		if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
			return array( '0' => 'No list available (' . wp_remote_retrieve_response_code( $response ) . ')' );
		}

		$body = $response['body'];
		if ( $body == "false" ) {
			return array();
		}

		$lists = json_decode( $body, true );

		if ( ! isset( $lists['items'] ) || ! is_array( $lists['items'] ) ) {
			return array();
		}

		foreach ( $lists['items'] as $item ) {
			$out[ $item['containerId'] ] = $item['name'];
		}

		add_option( 'tbm_nl_lists', $out );

		return $out;
	}

}
