Esempio n. 1
0
function wpv_change_ct_assigned_to_something_dialog_callback(){
    wpv_ajax_authenticate( 'work_view_template', array( 'parameter_source' => 'get', 'type_of_death' => 'data' ) );
	
    global $wpdb, $WPV_settings;
	$values_to_prepare = array();
	$exclude_loop_templates = '';
	$exclude_loop_templates_ids = wpv_get_loop_content_template_ids();
	if ( count( $exclude_loop_templates_ids ) > 0 ) {
		$exclude_loop_templates_ids_sanitized = array_map( 'esc_attr', $exclude_loop_templates_ids );
		$exclude_loop_templates_ids_sanitized = array_map( 'trim', $exclude_loop_templates_ids_sanitized );
		// is_numeric + intval does sanitization
		$exclude_loop_templates_ids_sanitized = array_filter( $exclude_loop_templates_ids_sanitized, 'is_numeric' );
		$exclude_loop_templates_ids_sanitized = array_map( 'intval', $exclude_loop_templates_ids_sanitized );
		if ( count( $exclude_loop_templates_ids_sanitized ) > 0 ) {
			$exclude_loop_templates = " AND p.ID NOT IN ('" . implode( "','" , $exclude_loop_templates_ids_sanitized ) . "') ";
		}
	}
	$values_to_prepare[] = 'view-template';
	$view_tempates_available = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT p.ID, p.post_title 
			FROM {$wpdb->posts} p
			WHERE p.post_status = 'publish' 
			AND p.post_type = %s 
			{$exclude_loop_templates}
			ORDER BY p.post_title",
			$values_to_prepare
		)
	);
	
    $post_type = sanitize_text_field( $_GET['pt'] );
	$selected = ( isset( $WPV_settings[$post_type] ) ) ? $WPV_settings[$post_type] : 0;
	ob_start();
    ?>
    <div class="wpv-dialog wpv-shortcode-gui-content-wrapper js-wpv-dialog-add-new-content-template wpv-dialog-add-new-content-template">
		<h3><?php _e( 'Select one of the following Content Templates.', 'wpv-views' ); ?></h3>
		<ul class="wpv-mightlong-list">
			<li>
				<input type="radio" id="wpv-content-template-name-0" class="js-wpv-content-template-name" name="wpv-content-template-name" <?php checked( 0, $selected ); ?> value="0" />
				<label for="wpv-content-template-name-0"><?php _e( 'Don’t use any Content Template', 'wpv-views' ); ?></label>
			</li>
			<?php
			if ( count( $view_tempates_available ) > 0 ) {
				foreach ( $view_tempates_available as $row ) {
					?>
						<li>
							<input type="radio" id="wpv-content-template-name-<?php echo esc_attr( $row->ID );?>" class="js-wpv-content-template-name" name="wpv-content-template-name" <?php checked( $row->ID, $selected ); ?> value="<?php echo esc_attr( $row->ID );?>" />
							<label for="wpv-content-template-name-<?php echo esc_attr( $row->ID );?>"><?php echo $row->post_title; ?></label>
						</li>
					<?php
				}
			}
			?>
		</ul>
    </div>
    <?php
	$response = ob_get_clean();
	$data = array(
		'dialog_content' => $response
	);
	wp_send_json_success( $data );
}
function wpv_suggest_wpv_post_body_view_template() {
	global $wpdb, $sitepress;
	$values_to_prepare = array();
	$wpml_join = $wpml_where = "";
	if (
		isset( $sitepress ) 
		&& function_exists( 'icl_object_id' )
	) {
		$content_templates_translatable = $sitepress->is_translated_post_type( 'view-template' );
		if ( $content_templates_translatable ) {
			$wpml_current_language = $sitepress->get_current_language();
			$wpml_join = " JOIN {$wpdb->prefix}icl_translations t ";
			$wpml_where = " AND p.ID = t.element_id AND t.language_code = %s ";
			$values_to_prepare[] = $wpml_current_language;
		}
	}
	
	$exclude_loop_templates = '';
	$exclude_loop_templates_ids = wpv_get_loop_content_template_ids();
	// Be sure not to include the current CT when editing one
	if ( isset( $_REQUEST['wpv_suggest_wpv_post_body_view_template_exclude'] ) ) {
		$exclude_loop_templates_ids[] = $_REQUEST['wpv_suggest_wpv_post_body_view_template_exclude'];
	}
	if ( count( $exclude_loop_templates_ids ) > 0 ) {
		$exclude_loop_templates_ids_sanitized = array_map( 'esc_attr', $exclude_loop_templates_ids );
		$exclude_loop_templates_ids_sanitized = array_map( 'trim', $exclude_loop_templates_ids_sanitized );
		// is_numeric + intval does sanitization
		$exclude_loop_templates_ids_sanitized = array_filter( $exclude_loop_templates_ids_sanitized, 'is_numeric' );
		$exclude_loop_templates_ids_sanitized = array_map( 'intval', $exclude_loop_templates_ids_sanitized );
		if ( count( $exclude_loop_templates_ids_sanitized ) > 0 ) {
			$exclude_loop_templates = " AND p.ID NOT IN ('" . implode( "','" , $exclude_loop_templates_ids_sanitized ) . "') ";
		}
	}
	$values_to_prepare[] = 'view-template';
	$values_to_prepare[] = '%' . wpv_esc_like( $_REQUEST['q'] ) . '%';
	$view_tempates_available = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT p.ID, p.post_name, p.post_title 
			FROM {$wpdb->posts} p {$wpml_join} 
			WHERE p.post_status = 'publish' 
			{$wpml_where} 
			AND p.post_type = %s 
			AND p.post_title LIKE %s
			{$exclude_loop_templates}
			ORDER BY p.post_title 
			LIMIT 5",
			$values_to_prepare
		)
	);
	foreach ( $view_tempates_available as $row ) {
		echo $row->post_title . "\n";
	}
	die();
}
/**
*
* Add the short codes to javascript so they can be added to the post visual editor toolbar.
*
* $types contains the type of items to add to the toolbar
*
* 	'post' means all wpv-post shortcodes but wpv-post-field in the Basic section.
*	'post-fields-grouped' means non-Types custom fields in the Post field section.
*	'post-extended' means wpv-post-field and wpv-for-each shortcodes in the Basic section.
* 
* 	@important To be used only on native post edit screens:
*		'types-post' means Types custom fields and usermeta fields in their own groups.
*		'types-post-usermeta' means Types usermeta in their own groups.
* 	@important Note that for generic Types items, you can use the wpcf_filter_force_include_types_fields_on_views_dialog filter.
*
* 	'user' means all wpv-user shortcodes with a UserID selector
* 
* 	'body-view-templates' means a CT section listing all available CT.
*
* 	'view' means all available Views. DEPRECATED, use the other ones.
* 	'user-view' means Views listing users.
* 	'taxonomy-view' means Views listing terms.
* 	'post-view' means Views listing posts.
*
* 	'archives' means all WPAs - where is this being used? Nowhere!!
*
* 	'wpml' means some WPML-related shortcodes.
*/
function add_short_codes_to_js($types, $editor)
{
    global $wpv_shortcodes, $wpdb, $WP_Views, $sitepress;
    $views_shortcodes_with_api_obj = apply_filters('wpv_filter_wpv_shortcodes_gui_data', array());
    $views_shortcodes_with_api = array_keys($views_shortcodes_with_api_obj);
    $index = 0;
    $nonce = wp_create_nonce('wpv_editor_callback');
    foreach ($wpv_shortcodes as $shortcode) {
        if (in_array($shortcode[0], $views_shortcodes_with_api)) {
            $shortcode[3] = "WPViews.shortcodes_gui.wpv_insert_popup('" . $shortcode[0] . "', '" . $shortcode[1] . "', {}, '" . $nonce . "', this )";
        }
        if (in_array('post', $types) && strpos($shortcode[0], 'wpv-post-') === 0 && $shortcode[0] != 'wpv-post-field' && function_exists($shortcode[2])) {
            // All wpv-post-*** shortcodes but wpv-post-field
            if (isset($shortcode[3])) {
                $editor->add_insert_shortcode_menu($shortcode[1], $shortcode[0], __('Basic', 'wpv-views'), $shortcode[3]);
            } else {
                $editor->add_insert_shortcode_menu($shortcode[1], $shortcode[0], __('Basic', 'wpv-views'));
            }
            $index += 1;
        }
    }
    if (in_array('post-fields-placeholder', $types)) {
        $menu = __('Post field', 'wpv-views');
        $editor->add_insert_shortcode_menu('<i class="icon-plus-sign"></i> ' . __('Load non-Types custom fields', 'wpv-views'), '', $menu, "WPViews.shortcodes_gui.load_post_field_section_on_demand( event, this )");
        $index += 1;
    }
    if (in_array('post-fields-grouped', $types)) {
        $cf_keys = $WP_Views->get_meta_keys();
        foreach ($cf_keys as $cf_key) {
            if (!wpv_is_types_custom_field($cf_key)) {
                // add to the javascript array (text, function, sub-menu)
                $function_name = 'wpv_field_' . $index;
                $menu = __('Post field', 'wpv-views');
                $editor->add_insert_shortcode_menu($cf_key, 'wpv-post-field name="' . $cf_key . '"', $menu);
                $index += 1;
            }
        }
    }
    if (in_array('post-extended', $types)) {
        // Add the wpv-post-field just in case
        $editor->add_insert_shortcode_menu(__('Post field', 'wpv-views'), 'wpv-post-field', __('Basic', 'wpv-views'), "WPViews.shortcodes_gui.wpv_insert_popup('wpv-post-field', '" . __('Post field', 'wpv-views') . "', {}, '" . $nonce . "', this )");
        $index += 1;
        // Add the wpv-for-each iterator
        $editor->add_insert_shortcode_menu(__('Post field iterator', 'wpv-views'), 'wpv-for-each', __('Basic', 'wpv-views'), "WPViews.shortcodes_gui.wpv_insert_popup('wpv-for-each', '" . __('Post field iterator', 'wpv-views') . "', {}, '" . $nonce . "', this )");
        $index += 1;
    }
    /*
     * Note that the following two actions only have callbacks from Types on post.php and post-new.php
     * On Views and WPA edit screens, postmeta and usermeta items are added automatically by Types without needing to call them
     */
    if (in_array('types-post', $types)) {
        do_action('wpv_action_wpv_add_types_postmeta_to_editor', $editor);
    }
    if (in_array('types-post-usermeta', $types)) {
        do_action('wpv_action_wpv_add_types_post_usermeta_to_editor', $editor);
    }
    if (in_array('user', $types)) {
        $user_shortcodes = array('ID' => array('label' => __('User ID', 'wpv-views'), 'code' => 'wpv-user field="ID"'), 'user_email' => array('label' => __('User Email', 'wpv-views'), 'code' => 'wpv-user field="user_email"'), 'user_login' => array('label' => __('User Login', 'wpv-views'), 'code' => 'wpv-user field="user_login"'), 'user_firstname' => array('label' => __('First Name', 'wpv-views'), 'code' => 'wpv-user field="user_firstname"'), 'user_lastname' => array('label' => __('Last Name', 'wpv-views'), 'code' => 'wpv-user field="user_lastname"'), 'nickname' => array('label' => __('Nickname', 'wpv-views'), 'code' => 'wpv-user field="nickname"'), 'display_name' => array('label' => __('Display Name', 'wpv-views'), 'code' => 'wpv-user field="display_name"'), 'description' => array('label' => __('Description', 'wpv-views'), 'code' => 'wpv-user field="description"'), 'yim' => array('label' => __('Yahoo IM', 'wpv-views'), 'code' => 'wpv-user field="yim"'), 'jabber' => array('label' => __('Jabber', 'wpv-views'), 'code' => 'wpv-user field="jabber"'), 'aim' => array('label' => __('AIM', 'wpv-views'), 'code' => 'wpv-user field="aim"'), 'user_url' => array('label' => __('User URL', 'wpv-views'), 'code' => 'wpv-user field="user_url"'), 'user_registered' => array('label' => __('Registration Date', 'wpv-views'), 'code' => 'wpv-user field="user_registered"'), 'user_status' => array('label' => __('User Status', 'wpv-views'), 'code' => 'wpv-user field="user_status"'), 'spam' => array('label' => __('User Spam Status', 'wpv-views'), 'code' => 'wpv-user field="spam"'));
        foreach ($user_shortcodes as $u_shortcode_slug => $u_shortcode_data) {
            $editor->add_insert_shortcode_menu($u_shortcode_data['label'], $u_shortcode_data['code'], __('User basic data', 'wpv-views'), "WPViews.shortcodes_gui.wpv_insert_popup('wpv-user', '" . esc_js($u_shortcode_data['label']) . "', {field:{value:'" . $u_shortcode_slug . "',label:'" . esc_js($u_shortcode_data['label']) . "'}}, '" . $nonce . "', this )");
            $index += 1;
        }
    }
    // Content Templates
    if (in_array('body-view-templates', $types)) {
        $ct_objects = get_transient('wpv_transient_ct_published');
        if ($ct_objects === false) {
            global $pagenow;
            $values_to_prepare = array();
            $values_to_prepare[] = 'view-template';
            $values_to_exclude = array();
            $values_to_exclude_string = '';
            if (in_array($pagenow, array('post.php')) && isset($_GET["post"]) && is_numeric($_GET["post"])) {
                $values_to_exclude[] = (int) $_GET["post"];
            }
            if (isset($_GET["page"]) && 'ct-editor' == $_GET["page"] && isset($_GET["ct_id"]) && is_numeric($_GET["ct_id"])) {
                $values_to_exclude[] = (int) $_GET["ct_id"];
            }
            $exclude_loop_templates_ids = wpv_get_loop_content_template_ids();
            if (count($exclude_loop_templates_ids) > 0) {
                $exclude_loop_templates_ids_sanitized = array_map('esc_attr', $exclude_loop_templates_ids);
                $exclude_loop_templates_ids_sanitized = array_map('trim', $exclude_loop_templates_ids_sanitized);
                // is_numeric + intval does sanitization
                $exclude_loop_templates_ids_sanitized = array_filter($exclude_loop_templates_ids_sanitized, 'is_numeric');
                $exclude_loop_templates_ids_sanitized = array_map('intval', $exclude_loop_templates_ids_sanitized);
                if (count($exclude_loop_templates_ids_sanitized) > 0) {
                    $values_to_exclude = array_merge($values_to_exclude, $exclude_loop_templates_ids_sanitized);
                }
            }
            if (count($values_to_exclude) > 0) {
                $values_to_exclude_string = " AND ID NOT IN ('" . implode("','", $values_to_exclude) . "') ";
            }
            $view_template_available = $wpdb->get_results($wpdb->prepare("SELECT ID, post_title FROM {$wpdb->posts}\r\n\t\t\t\t\tWHERE post_type = %s\r\n\t\t\t\t\tAND post_status in ('publish')\r\n\t\t\t\t\t{$values_to_exclude_string}", $values_to_prepare));
            set_transient('wpv_transient_ct_published', $view_template_available, WEEK_IN_SECONDS);
        } else {
            $view_template_available = $ct_objects;
        }
        if (is_array($view_template_available)) {
            foreach ($view_template_available as $view_template) {
                $editor->add_insert_shortcode_menu(esc_html($view_template->post_title), 'wpv-post-body view_template="' . esc_html($view_template->post_title) . '"', __('Content Template', 'wpv-views'));
                $index += 1;
            }
        }
    }
    if (in_array('view', $types) || in_array('archives', $types) || in_array('user-view', $types) || in_array('taxonomy-view', $types) || in_array('post-view', $types)) {
        $views_objects = get_transient('wpv_transient_view_published');
        $views_objects_transient_to_update = array('archive' => array(), 'posts' => array(), 'taxonomy' => array(), 'users' => array());
        global $pagenow;
        $current_id = 0;
        if (in_array($pagenow, array('post.php')) && isset($_GET["post"])) {
            $current_id = (int) $_GET["post"];
        } else {
            if (in_array($pagenow, array('post-new.php'))) {
                global $post;
                $current_id = $post->ID;
            }
        }
        if ($views_objects === false) {
            $view_available = $wpdb->get_results("SELECT ID, post_title FROM {$wpdb->posts}\r\n\t\t\t\tWHERE post_type='view'\r\n\t\t\t\tAND post_status in ('publish')");
            foreach ($view_available as $view) {
                if ($WP_Views->is_archive_view($view->ID)) {
                    // Archive Views - add only if in_array( 'archives', $types )
                    if (in_array('archives', $types)) {
                        $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('Archive', 'wpv-views'));
                        $index += 1;
                    }
                    $views_objects_transient_to_update['archive'][] = $view;
                } else {
                    $view_settings = get_post_meta($view->ID, '_wpv_settings', true);
                    $view_type = 'posts';
                    if (isset($view_settings['query_type'][0])) {
                        $view_type = $view_settings['query_type'][0];
                    }
                    if (in_array('user-view', $types) && $view_type == 'users') {
                        // Add Views listing users
                        $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('User View', 'wpv-views'), 'WPViews.shortcodes_gui.wpv_insert_view_shortcode_dialog(\'' . $view->ID . '\', \'' . esc_js($view->post_title) . '\', \'' . $current_id . '\', \'' . $nonce . '\')');
                        $index += 1;
                    }
                    if (in_array('taxonomy-view', $types) && $view_type == 'taxonomy') {
                        // Add Views listing taxonomies
                        $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('Taxonomy View', 'wpv-views'), 'WPViews.shortcodes_gui.wpv_insert_view_shortcode_dialog(\'' . $view->ID . '\', \'' . esc_js($view->post_title) . '\', \'' . $current_id . '\', \'' . $nonce . '\')');
                        $index += 1;
                    }
                    if (in_array('post-view', $types) && $view_type == 'posts') {
                        // Add Views listing posts
                        $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('Post View', 'wpv-views'), 'WPViews.shortcodes_gui.wpv_insert_view_shortcode_dialog(\'' . $view->ID . '\', \'' . esc_js($view->post_title) . '\', \'' . $current_id . '\', \'' . $nonce . '\')');
                    }
                    $views_objects_transient_to_update[$view_type][] = $view;
                }
            }
            set_transient('wpv_transient_view_published', $views_objects_transient_to_update, WEEK_IN_SECONDS);
        } else {
            if (in_array('archives', $types) && isset($views_objects['archive']) && is_array($views_objects['archive'])) {
                foreach ($views_objects['archive'] as $view) {
                    $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('Archive', 'wpv-views'));
                    $index += 1;
                }
            }
            if (in_array('post-view', $types) && isset($views_objects['posts']) && is_array($views_objects['posts'])) {
                foreach ($views_objects['posts'] as $view) {
                    $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('Post View', 'wpv-views'), 'WPViews.shortcodes_gui.wpv_insert_view_shortcode_dialog(\'' . $view->ID . '\', \'' . esc_js($view->post_title) . '\', \'' . $current_id . '\', \'' . $nonce . '\')');
                }
            }
            if (in_array('taxonomy-view', $types) && isset($views_objects['taxonomy']) && is_array($views_objects['taxonomy'])) {
                foreach ($views_objects['taxonomy'] as $view) {
                    $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('Taxonomy View', 'wpv-views'), 'WPViews.shortcodes_gui.wpv_insert_view_shortcode_dialog(\'' . $view->ID . '\', \'' . esc_js($view->post_title) . '\', \'' . $current_id . '\', \'' . $nonce . '\')');
                    $index += 1;
                }
            }
            if (in_array('user-view', $types) && isset($views_objects['users']) && is_array($views_objects['users'])) {
                foreach ($views_objects['users'] as $view) {
                    $editor->add_insert_shortcode_menu(esc_html($view->post_title), 'wpv-view name="' . esc_html($view->post_title) . '"', __('User View', 'wpv-views'), 'WPViews.shortcodes_gui.wpv_insert_view_shortcode_dialog(\'' . $view->ID . '\', \'' . esc_js($view->post_title) . '\', \'' . $current_id . '\', \'' . $nonce . '\')');
                    $index += 1;
                }
            }
        }
    }
    // @todo move this to the WPML file with dependency-free hooks
    if (in_array('wpml', $types)) {
        global $sitepress;
        if (isset($sitepress)) {
            $editor->add_insert_shortcode_menu(__('WPML lang switcher', 'wpv-views'), 'wpml-lang-switcher', 'WPML');
            $index += 1;
            global $icl_language_switcher;
            if (isset($icl_language_switcher)) {
                $editor->add_insert_shortcode_menu(__('WPML lang footer', 'wpv-views'), 'wpml-lang-footer', 'WPML');
                $index += 1;
            }
            global $iclCMSNavigation;
            if (isset($iclCMSNavigation)) {
                //$editor->add_insert_shortcode_menu('WPML breadcrumbs', 'wpml-breadcrumbs', 'WPML');
                //$index += 1;
                $editor->add_insert_shortcode_menu(__('WPML sidebar', 'wpv-views'), 'wpml-sidebar', 'WPML');
                $index += 1;
            }
        }
    }
    if (defined('WPSEO_VERSION')) {
        $editor->add_insert_shortcode_menu(__('Yoast SEO breadcrumbs', 'wpv-views'), 'yoast-breadcrumbs', 'Yoast SEO');
        $index += 1;
    }
    if (in_array('loop-wizard-for-posts', $types)) {
        do_action('wpv_action_wpv_add_field_on_loop_wizard_for_posts', $editor, $nonce);
    }
    if (in_array('loop-wizard-for-taxonomy', $types)) {
        do_action('wpv_action_wpv_add_field_on_loop_wizard_for_taxonomy', $editor, $nonce);
    }
    if (in_array('loop-wizard-for-users', $types)) {
        do_action('wpv_action_wpv_add_field_on_loop_wizard_for_users', $editor, $nonce);
    }
    return $index;
}
function wpv_assign_ct_to_view_callback() {
	if ( ! current_user_can( 'manage_options' ) ) {
		$data = array(
			'type' => 'capability',
			'message' => __( 'You do not have permissions for that.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
	if ( 
		! isset( $_POST["wpnonce"] )
		|| (
			! wp_verify_nonce( $_POST["wpnonce"], 'wpv_inline_content_template' )
			&& ! wp_verify_nonce( $_POST["wpnonce"], 'wpv-ct-inline-edit' )			
		)
	) {
		$data = array(
			'type' => 'nonce',
			'message' => __( 'Your security credentials have expired. Please reload the page to get new ones.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
	if (
		! isset( $_POST["view_id"] )
		|| ! is_numeric( $_POST["view_id"] )
		|| intval( $_POST['view_id'] ) < 1 
	) {
		$data = array(
			'type' => 'id',
			'message' => __( 'Wrong or missing ID.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}

	global $wpdb;
	$view_id = $_POST['view_id'];
	$layout_settings = get_post_meta( $view_id, '_wpv_layout_settings', true);
	$assigned_templates = array();
	if ( isset( $layout_settings['included_ct_ids'] ) && $layout_settings['included_ct_ids'] != '' ) {
		$assigned_templates = explode( ',', $layout_settings['included_ct_ids'] );
	}
	ob_start();
	?>
	<div class="wpv-dialog">
		<p>
			<?php
			_e( 'Use Content Templates as chunks of content that will be repeated in each element of the View loop.', 'wpv-views' );
			?>
		</p>
		<?php
		$not_in = '';
		$not_in_array = wpv_get_loop_content_template_ids();
		$query_args = array(
			'post_type' => 'view-template',
			'orderby' => 'title', 
			'order' => 'ASC',
			'posts_per_page' => '-1'
		);
		if ( count( $assigned_templates ) > 0 ) {
		?>
			<h4><?php _e( 'This View has some Content Templates already assigned', 'wpv-views' ); ?></h4>
			<div style="margin-left:20px;">
				<input type="radio" name="wpv-ct-type" value="already" class="js-wpv-inline-template-type" id="js-wpv-ct-type-existing-asigned">
				<label for="js-wpv-ct-type-existing-asigned"><?php _e( 'Insert a Content Template already assigned into the View', 'wpv-views' ) ?></label>
				<div class="js-wpv-assign-ct-already" style="margin-left:20px;">
					<select class="js-wpv-inline-template-assigned-select" id="js-wpv-ct-add-id-assigned">
						<option value="0"><?php _e( 'Select a Content Template','wpv-views' ) ?>&hellip;</option>
						<?php
						foreach ( $assigned_templates as $assigned_temp ) {
						 if ( is_numeric( $assigned_temp ) ) {
							// This is cached so it is OK to load the whole post
							$template_post = get_post( $assigned_temp );
							if ( 
								is_object( $template_post ) 
								&& $template_post->post_status  == 'publish'
								&& $template_post->post_type  == 'view-template'
							) {
								$not_in_array[] =  $template_post->ID;
								echo '<option value="' . esc_attr( $template_post->ID ) . '">' . $template_post->post_title . '</option>';
							}
						 }
						}
						?>
					</select>
				</div>
			</div>
			<h4><?php _e( 'Assign other Content Template to the View', 'wpv-views' ); ?></h4>
		<?php
		} else {
		?>
			<h4><?php _e( 'Assign a Content Template to the View', 'wpv-views' ); ?></h4>
		<?php
		}
		// @todo transform this in a suggest text input
		// limit the query to just one, as we are OK with just that
		// also, it should return just IDs for performance
		if ( ! empty( $not_in_array ) ) {
			$not_in = implode( ',', $not_in_array );
			$query_args['exclude'] = $not_in;
		}
		$query = get_posts( $query_args );
		if ( count( $query ) > 0 ) {
		?>
			<div style="margin:0 0 10px 20px;">
				<input type="radio" name="wpv-ct-type" class="js-wpv-inline-template-type" value="existing" id="js-wpv-ct-type-existing">
				<label for="js-wpv-ct-type-existing"><?php _e( 'Assign an existing Content template to this View','wpv-views' ) ?></label>
				<div class="js-wpv-assign-ct-existing" style="margin-left:20px;">
					<select class="js-wpv-inline-template-existing-select" id="js-wpv-ct-add-id">
						<option value="0"><?php _e( 'Select a Content Template','wpv-views' ) ?>&hellip;</option>
						<?php
						foreach( $query as $temp_post ) {
							echo '<option value="' . esc_attr( $temp_post->ID ) .'">' . $temp_post->post_title .'</option>';
						}
						?>
					</select>
				</div>
			</div>
		<?php
		}
		?>
		<div style="margin:0 0 10px 20px;">
			<input type="radio" name="wpv-ct-type" class="js-wpv-inline-template-type" value="new" id="js-wpv-ct-type-new">
			<label for="js-wpv-ct-type-new"><?php _e('Create a new Content Template and assign it to this View','wpv-views') ?></label>
			<div style="margin-left:20px;" class="js-wpv-assign-ct-new">
				<input type="text" class="js-wpv-inline-template-new-name" id="js-wpv-ct-type-new-name" placeholder="<?php echo esc_attr( __( 'Type a name', 'wpv-views' ) ); ?>">
				<div class="js-wpv-add-new-ct-name-error-container"></div>
			</div>
		</div>
		<div class="js-wpv-inline-template-insert" id="js-wpv-add-to-editor-line" style="margin:10px 0 10px 20px;">
			<hr />
			<input type="checkbox" class="js-wpv-add-to-editor-check" name="wpv-ct-add-to-editor" id="js-wpv-ct-add-to-editor-btn" checked="checked">
			<label for="js-wpv-ct-add-to-editor-btn"><?php _e('Insert the Content Template shortcode to editor','wpv-views') ?></label>
		</div>
	</div>
	<?php
	$response = ob_get_clean();
	$data = array(
		'dialog_content' => $response
	);
	wp_send_json_success( $data );
}
	/**
	 * Add a meta box to public and publicly_queryable post types to set the Content Template to be used for that single item being edited
	 *
	 * @param WP_Post $post the post being edited
	 *
	 * Echoes the meta box.
	 *
	 * @since unknown
     *
	 * @note 1.7 added a link to edit the Content Template
	 * @note 1.7 removed loop Templates from the dropdown
	 */
    function content_template_select_meta_box( $post ) {
        global $wpdb, $WP_Views, $sitepress;
		
		$values_to_prepare = array();
		
		$wpml_join = $wpml_where = "";
		if (
			isset( $sitepress ) 
			&& function_exists( 'icl_object_id' )
		) {
			$content_templates_translatable = $sitepress->is_translated_post_type( 'view-template' );
			if ( $content_templates_translatable ) {
				$wpml_current_language = $sitepress->get_current_language();
				$wpml_join = " JOIN {$wpdb->prefix}icl_translations t ";
				$wpml_where = " AND p.ID = t.element_id AND t.language_code = %s ";
				$values_to_prepare[] = $wpml_current_language;
			}
		}
		
		
		$exclude_loop_templates = '';
		$exclude_loop_templates_ids = wpv_get_loop_content_template_ids();
		if ( count( $exclude_loop_templates_ids ) > 0 ) {
			$exclude_loop_templates_ids_sanitized = array_map( 'esc_attr', $exclude_loop_templates_ids );
			$exclude_loop_templates_ids_sanitized = array_map( 'trim', $exclude_loop_templates_ids_sanitized );
			// is_numeric + intval does sanitization
			$exclude_loop_templates_ids_sanitized = array_filter( $exclude_loop_templates_ids_sanitized, 'is_numeric' );
			$exclude_loop_templates_ids_sanitized = array_map( 'intval', $exclude_loop_templates_ids_sanitized );
			if ( count( $exclude_loop_templates_ids_sanitized ) > 0 ) {
				$exclude_loop_templates = " AND p.ID NOT IN ('" . implode( "','" , $exclude_loop_templates_ids_sanitized ) . "') ";
			}
		}
		$values_to_prepare[] = 'view-template';
        $view_tempates_available = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT p.ID, p.post_name, p.post_title 
				FROM {$wpdb->posts} p {$wpml_join} 
				WHERE p.post_status = 'publish' 
				{$wpml_where} 
				AND p.post_type = %s 			
				{$exclude_loop_templates}
				ORDER BY p.post_title",
				$values_to_prepare
			)
		);
        if ( 
			isset( $_GET['post'] ) 
			&& intval( $_GET['post'] ) > 0
		) {
            $template_selected = get_post_meta( (int) $_GET['post'], '_views_template', true );
			if ( empty( $template_selected ) ) {
				$template_selected = 0;
			}
        } else {
            $template_selected = 0;
            global $pagenow, $post_type;
            if ( $pagenow == 'post-new.php' ) {
				if ( 
					isset( $_GET['trid'] ) 
					&& isset( $_GET['source_lang'] )
				) {
					$sp_trid = sanitize_text_field( $_GET['trid'] );
					$sp_source_lang = sanitize_text_field( $_GET['source_lang'] );
					// we are creating a translated post
					if ( isset( $sitepress ) && function_exists( 'icl_object_id' ) ) {
						$translations = $sitepress->get_element_translations( $sp_trid, 'post_' . $post->post_type );
						if ( isset( $translations[$sp_source_lang] ) ) {
							$template_selected = get_post_meta( $translations[$sp_source_lang]->element_id, '_views_template', true );
							if ( empty( $template_selected ) ) {
								$template_selected = 0;
							}
						}
					}
				}
				if ( $template_selected == 0 ) {
					// see if we have specified what template to use for this post type
					global $WPV_settings;
					if ( isset( $WPV_settings['views_template_for_' . $post_type] ) ) {
						$template_selected = $WPV_settings['views_template_for_' . $post_type];						
					}
				}
            }
        }
		?>
		<select name="views_template[<?php echo esc_attr( $post->ID ); ?>]" id="views_template" class="widefat js-wpv-edit-post-select-ct">
		<?php
        // Add a "None" type to the list.
        $none = new stdClass();
        $none->ID = '0';
        $none->post_title = __( 'None', 'wpv-views' );
        array_unshift( $view_tempates_available, $none );
		if ( $template_selected != 0 ) {
			// Adjust for WPML support
			$template_selected = apply_filters( 'translate_object_id', $template_selected, 'view-template', true, null );
		}
        foreach( $view_tempates_available as $template ) {
			if ( $template->post_title != '' ) {
				?>
				<option value="<?php echo esc_attr( $template->ID ); ?>" <?php selected( $template_selected, $template->ID ); ?>><?php echo $template->post_title; ?></option>
				<?php
			} else {
				?>
				<option value="<?php echo esc_attr( $template->ID ); ?>" <?php selected( $template_selected, $template->ID ); ?>><?php echo $template->post_name; ?></option>
				<?php
			}
        }
		?>
		</select>
		<?php
		if ( ! $WP_Views->is_embedded() ) {
			$edit_link = '';
			$edit_link_visible = ' hidden';
			if ( 
				! empty( $template_selected ) 
				&& $template_selected !== 0 
				&& get_post_status( $template_selected ) == 'publish'
			) {
				$edit_link = wpv_ct_editor_url( $template_selected );
				$edit_link_visible = '';
			}
			?>
			<div class="js-wpv-edit-post-edit-ct-link-container<?php echo esc_attr( $edit_link_visible ); ?>" style="margin-top:10px;padding-top:10px;border-top:solid 1px #ccc;">
				<a href="<?php echo esc_url( $edit_link ); ?>" class="button button-secondary js-wpv-edit-post-edit-ct-link" target="_blank"><?php _e( 'Edit this Content Template', 'wpv-views' ); ?> <i class="icon-chevron-right"></i></a>
			</div>
			<script type="text/javascript">
				jQuery( function( $ ) {
					// Warning! We need to take care of Layouts compatibility: if there is a select for Layouts AND has a value, we need to hide the thiz_wpv_ct_link_container
					var thiz_wpv_layout_select = $( document.getElementById( 'js-layout-template-name' ) ),
					thiz_wpv_ct_select = $( '.js-wpv-edit-post-select-ct' ),
					thiz_wpv_ct_link = $( '.js-wpv-edit-post-edit-ct-link' ),
					thiz_wpv_ct_link_container = $( '.js-wpv-edit-post-edit-ct-link-container' ),
					thiz_wpv_ct_select_manager = function() {
						if ( thiz_wpv_ct_select.val() != 0 ) {
							thiz_wpv_ct_link.attr(
                                'href',
                                'admin.php?page=' + <?php printf( '\'%s\'', WPV_CT_EDITOR_PAGE_NAME ); ?> + '&ct_id=' + thiz_wpv_ct_select.val() + '&action=edit'
                            );
							thiz_wpv_ct_link_container.fadeIn( 'fast' );
						} else {
							thiz_wpv_ct_link_container.hide();
						}
					};
					if ( thiz_wpv_layout_select.length > 0 ) {
						if ( thiz_wpv_layout_select.val() != '0' ) {
							thiz_wpv_ct_link_container.hide();
						} else {
							thiz_wpv_ct_select_manager();
						}
						thiz_wpv_layout_select.on( 'change', function() {
							if ( thiz_wpv_layout_select.val() != '0' ) {
								thiz_wpv_ct_link_container.hide();
							} else {
								thiz_wpv_ct_select_manager();
							}
						});
					}
					thiz_wpv_ct_select.on( 'change', thiz_wpv_ct_select_manager );
				});
			</script>
			<?php
		}
    }