function ct_change_types_callback()
{
    if (!isset($_GET["wpnonce"]) || !wp_verify_nonce($_GET["wpnonce"], 'work_view_template')) {
        die("Undefined Nonce.");
    }
    global $wpdb, $WP_Views;
    $options = $WP_Views->get_options();
    $post_types_array = wpv_get_pt_tax_array();
    $post_type_names = array('single_post' => __('Single pages', 'wpv-views'), 'taxonomy_post' => __('Taxonomy archives', 'wpv-views'), 'archive_post' => __('Post archives', 'wpv-views'));
    $id = $_GET['id'];
    $show_hidden = 0;
    ?>
    <div class="wpv-dialog js-wpv-dialog-add-new-content-template wpv-dialog-add-new-content-template">
        <form method="" id="wpv-add-new-content-template-form">
        <div class="wpv-dialog-header">
            <h2><?php 
    _e('Change Types', 'wpv-views');
    ?>
</h2>
            <i class="icon-remove js-dialog-close"></i>
        </div>
        <div class="wpv-dialog-content">
            <p><?php 
    _e('What content will this template be for?', 'wpv-views');
    ?>
</p>
            <div>
                <?php 
    foreach ($post_types_array as $post_type_name => $post_types) {
        $open_ul = 0;
        for ($i = 0; $i < count($post_types); $i++) {
            $show_hidden = 0;
            $type = $post_types[$i][0];
            $label = $post_types[$i][1];
            $input_name = 'views_template_for_' . $type;
            $type_used = false;
            $type_current = false;
            if ($post_type_name == 'single_post') {
                if (!isset($options['views_template_for_' . $type])) {
                    $options['views_template_for_' . $type] = 0;
                    $WP_Views->save_options($options);
                }
                if (isset($options['views_template_for_' . $type]) && $options['views_template_for_' . $type] != 0) {
                    $type_used = true;
                }
                if (isset($options['views_template_for_' . $type]) && $options['views_template_for_' . $type] == $id) {
                    $type_used = false;
                    $type_current = true;
                }
                if ($open_ul == 0 && !isset(${$post_type_name})) {
                    for ($j = 0; $j < count($post_types); $j++) {
                        $temp_type = $post_types[$j][0];
                        if (isset($options['views_template_for_' . $temp_type]) && $options['views_template_for_' . $temp_type] == $id) {
                            $show_hidden = 1;
                            $j = count($post_types) + 1;
                        }
                    }
                }
            }
            if ($post_type_name == 'taxonomy_post') {
                if (!isset($options['views_template_loop_' . $type])) {
                    $options['views_template_loop_' . $type] = 0;
                    $WP_Views->save_options($options);
                }
                if (isset($options['views_template_loop_' . $type]) && $options['views_template_loop_' . $type] != 0) {
                    $type_used = true;
                }
                if (isset($options['views_template_loop_' . $type]) && $options['views_template_loop_' . $type] == $id) {
                    $type_used = false;
                    $type_current = true;
                }
                $input_name = 'views_template_loop_' . $type;
                if ($open_ul == 0 && !isset(${$post_type_name})) {
                    for ($j = 0; $j < count($post_types); $j++) {
                        $temp_type = $post_types[$j][0];
                        if (isset($options['views_template_loop_' . $temp_type]) && $options['views_template_loop_' . $temp_type] == $id) {
                            $show_hidden = 1;
                            $j = count($post_types) + 1;
                        }
                    }
                }
            }
            if ($post_type_name == 'archive_post') {
                if (!isset($options['views_template_archive_for_' . $type])) {
                    $options['views_template_archive_for_' . $type] = 0;
                    $WP_Views->save_options($options);
                }
                if (isset($options['views_template_archive_for_' . $type]) && $options['views_template_archive_for_' . $type] != 0) {
                    $type_used = true;
                }
                if (isset($options['views_template_archive_for_' . $type]) && $options['views_template_archive_for_' . $type] == $id) {
                    $type_used = false;
                    $type_current = true;
                }
                $input_name = 'views_template_archive_for_' . $type;
                if ($open_ul == 0 && !isset(${$post_type_name})) {
                    for ($j = 0; $j < count($post_types); $j++) {
                        $temp_type = $post_types[$j][0];
                        if (isset($options['views_template_archive_for_' . $temp_type]) && $options['views_template_archive_for_' . $temp_type] == $id) {
                            $show_hidden = 1;
                            $j = count($post_types) + 1;
                        }
                    }
                }
            }
            if (!isset(${$post_type_name})) {
                ${$post_type_name} = 1;
                if ($open_ul == 0) {
                    $hide = '';
                    if ($post_type_name != 'single_post') {
                        ?>
                                        </ul>
                                    </div> <!-- .wpv-content-template-dropdown-list -->
                                    <?php 
                    }
                }
                ?>
                            <p>
                                <span class="js-wpv-content-template-open wpv-content-template-open" title="<?php 
                echo htmlentities(__("Click to toggle", 'wpv-views'), ENT_QUOTES);
                ?>
">
                                    <?php 
                echo $post_type_names[$post_type_name];
                ?>
:
                                    <i class="icon-caret-down"></i>
                                </span>
                            </p>
                            <?php 
                if ($open_ul == 0) {
                    $hide = '';
                    if ($show_hidden == 0) {
                        $hide = 'hidden';
                    }
                    ?>
                                <div class="js-wpv-content-template-dropdown-list wpv-content-template-dropdown-list <?php 
                    echo $hide;
                    ?>
">
                                    <ul>
                                <?php 
                    $open_ul = 1;
                }
            }
            ?>
                        <li>
                            <label<?php 
            echo !$type_used ? ' class="wpv-ct-post-type-used"' : '';
            ?>
>
                                <input type="checkbox" name="wpv-new-content-template-post-type[]"
                                <?php 
            echo $type_current ? ' checked="checked"' : '';
            ?>
                                data-title="<?php 
            echo $label;
            ?>
" value="<?php 
            echo $input_name;
            ?>
" />
                                <?php 
            echo $label;
            ?>
                            </label>
                        </li>
                    <?php 
        }
    }
    ?>
                    </ul>
                </div> <!-- .wpv-content-template-dropdown-list -->
            </div>

        </div>
        <div class="wpv-dialog-footer">
            <button class="button js-dialog-close"><?php 
    _e('Cancel', 'wpv-views');
    ?>
</button>
            <button class="button button-primary js-ct-change-type-process" data-id="<?php 
    echo $id;
    ?>
"><?php 
    _e('Change', 'wpv-views');
    ?>
</button>
        </div>
        </form>
         </div>
    <?php 
    die;
}
function wpv_admin_menu_content_template_listing_by_type_row( $sort, $page = 0 ) {
	global $WPV_settings, $post;

	$post_types_array = wpv_get_pt_tax_array();

	ob_start();
	if ( $sort == 'usage-single' ){

		$counter = count( $post_types_array['single_post'] );
		$alternate = '';
		for ( $i = 0; $i < $counter; ++$i ) {
			$type = $post_types_array['single_post'][ $i ][0];
			$label = $post_types_array['single_post'][ $i ][1];
			$alternate = ' alternate' == $alternate ? '' : ' alternate';

			?>
			<tr id="wpv_ct_list_row_<?php echo $type; ?>" class="js-wpv-ct-list-row<?php echo $alternate; ?>">

				<td class="wpv-admin-listing-col-usage post-title page-title column-title">
					<span class="row-title">
						<?php echo $label;?>
					</span>
					<?php
						$row_actions = array(
								"change_pt js-wpv-change-ct-assigned-to-something-popup" => sprintf( '<a href="#">%s</a>', __('Change Content Template','wpv-views') ) );

						echo wpv_admin_table_row_actions( $row_actions,	array(
								"data-pt" => 'views_template_for_' . $type ) );
					?>
				</td>

				<td class="wpv-admin-listing-col-used-title">
					<ul>
						<?php
							$add_button = wpv_ct_listing_render_create_ct_button(
                                sprintf( __( 'Create a Content Template for single %s', 'wpv-views' ), $label ),
                                $label,
                                array( 'single_post_types' => array( $type ) )
                            );


							if ( isset( $WPV_settings[ 'views_template_for_' . $type ] ) && $WPV_settings[ 'views_template_for_' . $type ] != 0 ) {

                                // There is a Content Template assigned for single posts of this type

                                $ct_id = $WPV_settings[ 'views_template_for_' . $type ];
                                $ct = WPV_Content_Template::get_instance( $ct_id );

                                if ( null != $ct ) {
                                    printf(
                                        '<a href="%s">%s</a>',
                                        esc_url(
                                            add_query_arg(
                                                array( 'page' => WPV_CT_EDITOR_PAGE_NAME, 'ct_id' => $ct->id, 'action' => 'edit' ),
                                                admin_url( 'admin.php' )
                                            )
                                        ),
                                        $ct->title
                                    );

									// @todo We do not need the exact number here, let's create a has_dissident_posts method instead with a LIMITed query
                                    $dissident_post_count = $ct->get_dissident_posts( $type, 'count' );

                                    if ( $dissident_post_count > 0 ) {
                                        ?>
                                        <span class="js-wpv-apply-ct-to-cpt-single-<?php echo $type; ?>">
                                            <?php
                                                printf(
                                                    '<a class="%s" data-type="%s" data-id="%s" data-nonce="%s"> %s</a>',
                                                    'button button-small button-leveled icon-warning-sign js-wpv-apply-ct-to-all-cpt-single-dialog',
													$type,
													$ct->id,
													wp_create_nonce( 'work_view_template' ),
                                                    sprintf( __( 'Bind %u %s ', 'wpv-views' ), $dissident_post_count, $label )
                                                );
                                            ?>
                                        </span>
                                        <?php
                                    }
                                    //}
                                } else {
                                    echo $add_button;
                                }
                            } else {

                                // Single posts of this type have no Content Template assigned

                                echo $add_button;

                                $assigned_posts_count = WPV_Content_Template_Embedded::get_posts_using_content_template_by_type( $type, 'count' );

                                if ( $assigned_posts_count > 0 ) {
                                    ?>
                                    <a class="button button-small js-wpv-clear-cpt-from-ct-popup" href="#"
                                            data-unclear="<?php echo $assigned_posts_count; ?>"
                                            data-slug="<?php echo $type; ?>"
                                            data-label="<?php echo htmlentities( $label, ENT_QUOTES ); ?>">
                                        <i class="icon-unlink"></i>
                                        <?php echo sprintf( __('Clear %d %s', 'wpv-views'), $assigned_posts_count, $label ); ?>
                                    </a>
                                    <?php
                                }

                            }
						?>
					</ul>
				</td>
			</tr>
			<?php
		}

	} else if ( $sort == 'usage-post-archives' ){

		$alternate = '';
		$counter = count( $post_types_array['archive_post'] );
		for ( $i = 0; $i < $counter; ++$i ) {

			$type = $post_types_array['archive_post'][ $i ][0];
			$label = $post_types_array['archive_post'][ $i ][1];

			$add_button = wpv_ct_listing_render_create_ct_button(
                __( 'Add a new Content Template for this post type', 'wpv-views' ),
                $label,
                array( 'post_archives' => array( $type ) )
            );

			$alternate = ' alternate' == $alternate ? '' : ' alternate';
			?>
			<tr id="wpv_ct_list_row_<?php echo $type; ?>" class="js-wpv-ct-list-row<?php echo $alternate; ?>">
				<td class="post-title page-title column-title">
					<span class="row-title">
						<?php echo $label; ?>
					</span>
					<?php
						$row_actions = array(
								"change_pt js-wpv-change-ct-assigned-to-something-popup" => sprintf( '<a href="#">%s</a>', __( 'Change Content Template', 'wpv-views' ) ) );

						echo wpv_admin_table_row_actions( $row_actions,	array(
								"data-pt" => 'views_template_archive_for_' . $type ) );
					?>
				</td>
				<td>
					<ul>
						<?php
							if ( isset( $WPV_settings[ 'views_template_archive_for_' . $type ] )
									&& $WPV_settings[ 'views_template_archive_for_' . $type ] != 0) {
								$post = get_post( $WPV_settings[ 'views_template_archive_for_' . $type ] );
								if ( is_object( $post ) ) {
                                    wpv_ct_editor_render_link( $post->ID, esc_html( $post->post_title ) );
								} else {
									echo $add_button;
								}
							} else {
								echo $add_button;
							}
						?>
					</ul>
				</td>
			</tr>
			<?php
		}

	} else if ( $sort == 'usage-taxonomy-archives' ){

		$counter = count( $post_types_array['taxonomy_post'] );
		$alternate = '';

		for ( $i = 0; $i < $counter; ++$i ) {
			$type = $post_types_array['taxonomy_post'][ $i ][0];
			$label = $post_types_array['taxonomy_post'][ $i ][1];

			$add_button = wpv_ct_listing_render_create_ct_button(
                __( 'Add a new Content Template for this taxonomy', 'wpv-views' ),
                $label,
                array( 'taxonomy_archives' => array( $type ) )
            );

			$alternate = ' alternate' == $alternate ? '' : ' alternate';

			?>
			<tr id="wpv_ct_list_row_<?php echo $type; ?>" class="js-wpv-ct-list-row<?php echo $alternate; ?>">
				<td class="post-title page-title column-title">
					<span class="row-title">
						<?php echo $label;?>
					</span>
					<?php
						$row_actions = array(
								"change_pt js-wpv-change-ct-assigned-to-something-popup" => sprintf( '<a href="#">%s</a>', __( 'Change Content Template', 'wpv-views' ) ) );

						echo wpv_admin_table_row_actions( $row_actions,	array(
								"data-pt" => 'views_template_loop_' . $type ) );
					?>
				</td>
				<td>
					<ul>
						<?php
							if ( isset( $WPV_settings[ 'views_template_loop_' . $type ] )
									&& $WPV_settings[ 'views_template_loop_' . $type ] != 0 ) {
								$post = get_post( $WPV_settings['views_template_loop_' . $type] );
								if ( is_object( $post ) ) {
                                    wpv_ct_editor_render_link( $post->ID, esc_html( $post->post_title ) );
								} else {
									echo $add_button;
								}
							} else {
								echo $add_button;
							}
						?>
					</ul>
				</td>
			</tr>
			<?php
		}
	}

	$row = ob_get_contents();
	ob_end_clean();

	return $row;
}
function wpv_admin_menu_content_template_listing_by_type_row($sort, $page = 0)
{
    // TODO check if the action URL parameter is needed when creating a CT
    global $WP_Views, $post, $wpdb;
    $options = $WP_Views->get_options();
    //	$post_types = get_post_types( array('public' => true), 'objects' );
    $post_types_array = wpv_get_pt_tax_array();
    ob_start();
    if ($sort == 'usage-single') {
        $counter = count($post_types_array['single_post']);
        $alternate = '';
        for ($i = 0; $i < $counter; $i++) {
            $type = $post_types_array['single_post'][$i][0];
            $label = $post_types_array['single_post'][$i][1];
            $alternate = ' alternate' == $alternate ? '' : ' alternate';
            ?>
			<tr id="wpv_ct_list_row_<?php 
            echo $type;
            ?>
" class="js-wpv-ct-list-row<?php 
            echo $alternate;
            ?>
">
				<td class="wpv-admin-listing-col-usage post-title page-title column-title">
					<h3 class="row-title">
						<?php 
            echo $label;
            ?>
					</h3>
				</td>
				<td class="wpv-admin-listing-col-used-title">
					<ul>
						<?php 
            $add_button = '<a class="button" data-disabled="1"
						href="post-new.php?post_type=view-template&action=wpv_ct_create_new&post_title=' . urlencode(__('Content template for ', 'wpv-views') . $label) . '&ct_selected=views_template_for_' . $type . '&toggle=1,0,0">
						<i class="icon-plus"></i>' . sprintf(__('Create a Content Template for single %s', 'wpv-views'), $label) . '</a>';
            $posts = $wpdb->get_col("SELECT {$wpdb->posts}.ID FROM {$wpdb->posts} WHERE post_type='{$type}' AND post_status!='auto-draft'");
            $count = sizeof($posts);
            $posts_ids = "'" . implode("','", $posts) . "'";
            if (isset($options['views_template_for_' . $type])) {
                if ($options['views_template_for_' . $type] != 0) {
                    $template = get_post($options['views_template_for_' . $type]);
                    if (is_object($template)) {
                        ?>
									<a href="post.php?post=<?php 
                        echo $template->ID;
                        ?>
&amp;action=edit"><?php 
                        echo $template->post_title;
                        ?>
</a>
									<?php 
                        if ($count > 0) {
                            $set_count = $wpdb->get_var("SELECT COUNT(post_id) FROM {$wpdb->postmeta} WHERE\r\n\t\t\t\t\t\t\t\t\t\tmeta_key='_views_template' AND meta_value='{$options['views_template_for_' . $type]}'\r\n\t\t\t\t\t\t\t\t\t\tAND post_id IN ({$posts_ids})");
                            if ($count - $set_count > 0) {
                                echo ' <span class="js-alret-icon-hide-' . $type . '"><a class="icon-warning-sign js-apply-for-all-posts"
											data-target="' . admin_url('admin-ajax.php') . '?action=wpv_ct_update_posts&type=' . $type . '&tid=' . $template->ID . '&wpnonce=' . wp_create_nonce('work_view_template') . '"></a> ' . ($count - $set_count) . ' ' . $label . ' use a different template ' . '<a class="js-apply-for-all-posts" href=""
											data-target="' . admin_url('admin-ajax.php') . '?action=wpv_ct_update_posts&type=' . $type . '&tid=' . $template->ID . '&wpnonce=' . wp_create_nonce('work_view_template') . '">' . __('Update now', 'wpv-views') . '</a></span>';
                            }
                        }
                    } else {
                        echo $add_button;
                    }
                } else {
                    $set_count = $wpdb->get_var("SELECT COUNT(post_id) FROM {$wpdb->postmeta} WHERE\r\n\t\t\t\t\t\t\t\t\t\tmeta_key='_views_template' AND meta_value!='0'\r\n\t\t\t\t\t\t\t\t\t\tAND post_id IN ({$posts_ids})");
                    echo $add_button;
                    if ($set_count > 0) {
                        ?>
									<a class="button js-single-unlink-template-open-dialog" href="#" data-unclear="<?php 
                        echo $set_count;
                        ?>
" data-slug="<?php 
                        echo $type;
                        ?>
" data-label="<?php 
                        echo htmlentities($label, ENT_QUOTES);
                        ?>
"><i class="icon-unlink"></i>
									<?php 
                        echo sprintf(__('Clear %d %s', 'wpv-views'), $set_count, $label);
                        ?>
									</a>
								<?php 
                    }
                }
            } else {
                $set_count = $wpdb->get_var("SELECT COUNT(post_id) FROM {$wpdb->postmeta} WHERE\r\n\t\t\t\t\t\t\t\t\t\tmeta_key='_views_template' AND meta_value!='0'\r\n\t\t\t\t\t\t\t\t\t\tAND post_id IN ({$posts_ids})");
                echo $add_button;
                if ($set_count > 0) {
                    ?>
								<a class="button js-single-unlink-template-open-dialog" href="#" data-unclear="<?php 
                    echo $set_count;
                    ?>
" data-slug="<?php 
                    echo $type;
                    ?>
" data-label="<?php 
                    echo htmlentities($label, ENT_QUOTES);
                    ?>
"><i class="icon-unlink"></i>
								<?php 
                    echo sprintf(__('Clear %d %s', 'wpv-views'), $set_count, $label);
                    ?>
								</a>
							<?php 
                }
            }
            ?>
					</ul>
				</td>
				<td class="wpv-admin-listing-col-action">
					<select class="js-list-ct-action">
						<option value="0"><?php 
            _e('Choose', 'wpv-views');
            ?>
&hellip;</option>
						<option value="change_pt" data-msg="1" data-sort="<?php 
            echo $sort;
            ?>
" data-pt="<?php 
            echo 'views_template_for_' . $type;
            ?>
"><?php 
            _e('Change Content Template', 'wpv-views');
            ?>
</option>
					</select>
				</td>

			</tr>
	<?php 
        }
    }
    ?>
  <?php 
    if ($sort == 'usage-post-archives') {
        $alternate = '';
        $counter = count($post_types_array['archive_post']);
        for ($i = 0; $i < $counter; $i++) {
            $type = $post_types_array['archive_post'][$i][0];
            $label = $post_types_array['archive_post'][$i][1];
            $add_button = '<a class="button" data-disabled="1"
						href="post-new.php?post_type=view-template&action=wpv_ct_create_new&post_title=' . urlencode(__('Content template for ', 'wpv-views') . $label) . '&ct_selected=views_template_archive_for_' . $type . '&toggle=0,1,0">
						<i class="icon-plus"></i>' . __('Add a new Content Template for this post type', 'wpv-views') . '</a>';
            $alternate = ' alternate' == $alternate ? '' : ' alternate';
            ?>
			<tr id="wpv_ct_list_row_<?php 
            echo $type;
            ?>
" class="js-wpv-ct-list-row<?php 
            echo $alternate;
            ?>
">
				<td class="post-title page-title column-title">
					<h3 class="row-title">
						<?php 
            echo $label;
            ?>
					</h3>
				</td>
				<td>
					<ul>
						<?php 
            if (isset($options['views_template_archive_for_' . $type]) && $options['views_template_archive_for_' . $type] != 0) {
                $post = get_post($options['views_template_archive_for_' . $type]);
                if (is_object($post)) {
                    ?>
								<a href="post.php?post=<?php 
                    echo $post->ID;
                    ?>
&action=edit"><?php 
                    echo $post->post_title;
                    ?>
</a>
								<?php 
                } else {
                    echo $add_button;
                }
            } else {
                echo $add_button;
            }
            ?>
					</ul>
				</td>
				<td>
					<select class="js-list-ct-action">
						<option value="0"><?php 
            _e('Choose', 'wpv-views');
            ?>
&hellip;</option>
						<option value="change_pt" data-msg="1" data-sort="<?php 
            echo $sort;
            ?>
" data-pt="<?php 
            echo 'views_template_archive_for_' . $type;
            ?>
"><?php 
            _e('Change Content Template', 'wpv-views');
            ?>
</option>
					</select>
				</td>

			</tr>
	<?php 
        }
    }
    if ($sort == 'usage-taxonomy-archives') {
        $counter = count($post_types_array['taxonomy_post']);
        $alternate = '';
        for ($i = 0; $i < $counter; $i++) {
            $type = $post_types_array['taxonomy_post'][$i][0];
            $label = $post_types_array['taxonomy_post'][$i][1];
            $add_button = '<a class="button js-wpv-ct-create-new-for-usage" data-disabled="1"
						data-title="' . urlencode(__('Content template for ', 'wpv-views') . $label) . '"
						data-usage="views_template_loop_' . $type . '"
						href="post-new.php?post_type=view-template&action=wpv_ct_create_new&post_title=' . urlencode(__('Content template for ', 'wpv-views') . $label) . '&ct_selected=views_template_loop_' . $type . '&toggle=0,0,1">
						<i class="icon-plus"></i>' . __('Add a new Content Template for this taxonomy', 'wpv-views') . '</a>';
            $alternate = ' alternate' == $alternate ? '' : ' alternate';
            ?>
			<tr id="wpv_ct_list_row_<?php 
            echo $type;
            ?>
" class="js-wpv-ct-list-row<?php 
            echo $alternate;
            ?>
">
				<td class="post-title page-title column-title">
					<h3 class="row-title">
						<?php 
            echo $label;
            ?>
					</h3>
				</td>
				<td>
					<ul>
						<?php 
            if (isset($options['views_template_loop_' . $type]) && $options['views_template_loop_' . $type] != 0) {
                $post = get_post($options['views_template_loop_' . $type]);
                if (is_object($post)) {
                    ?>
								<a href="post.php?post=<?php 
                    echo $post->ID;
                    ?>
&action=edit"><?php 
                    echo $post->post_title;
                    ?>
</a>
								<?php 
                } else {
                    echo $add_button;
                }
            } else {
                echo $add_button;
            }
            ?>
					</ul>
				</td>
				<td>
					<select class="js-list-ct-action">
						<option value="0"><?php 
            _e('Choose', 'wpv-views');
            ?>
&hellip;</option>
						<option value="change_pt" data-msg="2" data-sort="<?php 
            echo $sort;
            ?>
" data-pt="<?php 
            echo 'views_template_loop_' . $type;
            ?>
"><?php 
            _e('Change Content Template', 'wpv-views');
            ?>
</option>
					</select>
				</td>

			</tr>
	<?php 
        }
    }
    $row = ob_get_contents();
    ob_end_clean();
    return $row;
}
Пример #4
0
function ct_change_types_callback()
{
    if (!current_user_can('manage_options')) {
        die("Untrusted user");
    }
    if (!isset($_GET["wpnonce"]) || !wp_verify_nonce($_GET["wpnonce"], 'work_view_template')) {
        die("Undefined nonce");
    }
    global $WPV_settings;
    $post_types_array = wpv_get_pt_tax_array();
    $id = $_GET['id'];
    $asterisk = '<span style="color:red;">*</span>';
    $asterisk_explanation = __('<span style="color:red">*</span> A different Content Template is already assigned to this item', 'wpv-views');
    ?>
    <div class="wpv-dialog js-wpv-dialog-add-new-content-template wpv-dialog-add-new-content-template">
        <form method="" id="wpv-add-new-content-template-form">
        <div class="wpv-dialog-header">
            <h2><?php 
    _e('Change Types', 'wpv-views');
    ?>
</h2>
            <i class="icon-remove js-dialog-close"></i>
        </div>
        <div class="wpv-dialog-content">
            <p><?php 
    _e('What content will this template be for?', 'wpv-views');
    ?>
</p>
            <div>
                <p>
					<span class="js-wpv-content-template-open wpv-content-template-open" title="<?php 
    echo esc_attr(__("Click to toggle", 'wpv-views'));
    ?>
">
						<?php 
    echo __('Single pages', 'wpv-views');
    ?>
:
						<i class="icon-caret-down"></i>
					</span>
				</p>
                <?php 
    $single_posts = $post_types_array['single_post'];
    //key is views_template_for_
    $open_section = false;
    $show_asterisk_explanation = false;
    ob_start();
    if (count($single_posts) > 0) {
        ?>
					<ul>
					<?php 
        foreach ($single_posts as $s_post) {
            // $s_post is an array with each element being (name, label)
            $type = $s_post[0];
            $label = $s_post[1];
            $type_current = $type_used = false;
            if (isset($WPV_settings['views_template_for_' . $type]) && $WPV_settings['views_template_for_' . $type] != 0) {
                $type_used = true;
                $show_asterisk_explanation = true;
            }
            if (isset($WPV_settings['views_template_for_' . $type]) && $WPV_settings['views_template_for_' . $type] == $id) {
                $type_current = true;
                $type_used = false;
                $open_section = true;
            }
            ?>
						<li>
							<input id="<?php 
            echo 'views_template_for_' . esc_attr($type);
            ?>
" type="checkbox" name="wpv-new-content-template-post-type[]"<?php 
            echo $type_current ? ' checked="checked"' : '';
            ?>
 data-title="<?php 
            echo esc_attr($label);
            ?>
" value="<?php 
            echo 'views_template_for_' . esc_attr($type);
            ?>
" />
							<label for="<?php 
            echo 'views_template_for_' . esc_attr($type);
            ?>
"><?php 
            echo $label;
            echo $type_used ? $asterisk : '';
            ?>
</label>
						</li>
					<?php 
        }
        ?>
					</ul>
					<?php 
        if ($show_asterisk_explanation) {
            ?>
					<span class="wpv-asterisk-explanation">
						<?php 
            echo $asterisk_explanation;
            ?>
					</span>
					<?php 
        }
        ?>
					<?php 
    } else {
        _e('There are no single post types to assign Content Templates to', 'wpv-views');
    }
    $s_content = ob_get_clean();
    ?>
                <div class="js-wpv-content-template-dropdown-list wpv-content-template-dropdown-list<?php 
    echo $open_section ? '' : ' hidden';
    ?>
">
					<?php 
    echo $s_content;
    ?>
                </div>
                <p>
					<span class="js-wpv-content-template-open wpv-content-template-open" title="<?php 
    echo esc_attr(__("Click to toggle", 'wpv-views'));
    ?>
">
						<?php 
    echo __('Post type archives', 'wpv-views');
    ?>
:
						<i class="icon-caret-down"></i>
					</span>
				</p>
                <?php 
    $archive_posts = $post_types_array['archive_post'];
    //key is views_template_archive_for_
    $open_section = false;
    $show_asterisk_explanation = false;
    ob_start();
    if (count($archive_posts) > 0) {
        ?>
					<ul>
					<?php 
        foreach ($archive_posts as $s_post) {
            // $s_post is an array with each element being (name, label)
            $type = $s_post[0];
            $label = $s_post[1];
            $type_current = $type_used = false;
            if (isset($WPV_settings['views_template_archive_for_' . $type]) && $WPV_settings['views_template_archive_for_' . $type] != 0) {
                $type_used = true;
                $show_asterisk_explanation = true;
            }
            if (isset($WPV_settings['views_template_archive_for_' . $type]) && $WPV_settings['views_template_archive_for_' . $type] == $id) {
                $type_current = true;
                $type_used = false;
                $open_section = true;
            }
            ?>
						<li>
							<input id="<?php 
            echo 'views_template_archive_for_' . esc_attr($type);
            ?>
" type="checkbox" name="wpv-new-content-template-post-type[]"<?php 
            echo $type_current ? ' checked="checked"' : '';
            ?>
 data-title="<?php 
            echo esc_attr($label);
            ?>
" value="<?php 
            echo 'views_template_archive_for_' . esc_attr($type);
            ?>
" />
                            <label for="<?php 
            echo 'views_template_archive_for_' . esc_attr($type);
            ?>
"><?php 
            echo $label;
            echo $type_used ? $asterisk : '';
            ?>
</label>
                        </li>
						<?php 
        }
        ?>
					</ul>
					<?php 
        if ($show_asterisk_explanation) {
            ?>
					<span class="wpv-asterisk-explanation">
						<?php 
            echo $asterisk_explanation;
            ?>
					</span>
					<?php 
        }
        ?>
					<?php 
    } else {
        _e('There are no post type archives to assign Content Templates to', 'wpv-views');
    }
    $pta_content = ob_get_clean();
    ?>
				<div class="js-wpv-content-template-dropdown-list wpv-content-template-dropdown-list<?php 
    echo $open_section ? '' : ' hidden';
    ?>
">
					<?php 
    echo $pta_content;
    ?>
				</div>
				<p>
					<span class="js-wpv-content-template-open wpv-content-template-open" title="<?php 
    echo esc_attr(__("Click to toggle", 'wpv-views'));
    ?>
">
						<?php 
    echo __('Taxonomy archives', 'wpv-views');
    ?>
:
						<i class="icon-caret-down"></i>
					</span>
				</p>
                <?php 
    $archive_taxes = $post_types_array['taxonomy_post'];
    //key is views_template_loop_
    $open_section = false;
    $show_asterisk_explanation = false;
    ob_start();
    if (count($archive_taxes) > 0) {
        ?>
					<ul>
					<?php 
        foreach ($archive_taxes as $s_post) {
            // $s_post is an array with each element being (name, label)
            $type = $s_post[0];
            $label = $s_post[1];
            $type_current = $type_used = false;
            if (isset($WPV_settings['views_template_loop_' . $type]) && $WPV_settings['views_template_loop_' . $type] != 0) {
                $type_used = true;
                $show_asterisk_explanation = true;
            }
            if (isset($WPV_settings['views_template_loop_' . $type]) && $WPV_settings['views_template_loop_' . $type] == $id) {
                $type_current = true;
                $type_used = false;
                $open_section = true;
            }
            ?>
						<li>
							<input id="<?php 
            echo 'views_template_loop_' . esc_attr($type);
            ?>
" type="checkbox" name="wpv-new-content-template-post-type[]"<?php 
            echo $type_current ? ' checked="checked"' : '';
            ?>
 data-title="<?php 
            echo esc_attr($label);
            ?>
" value="<?php 
            echo 'views_template_loop_' . esc_attr($type);
            ?>
" />
                            <label for="<?php 
            echo 'views_template_loop_' . esc_attr($type);
            ?>
"><?php 
            echo $label;
            echo $type_used ? $asterisk : '';
            ?>
</label>
                        </li>
                        <?php 
        }
        ?>
					</ul>
					<?php 
        if ($show_asterisk_explanation) {
            ?>
					<span class="wpv-asterisk-explanation">
						<?php 
            echo $asterisk_explanation;
            ?>
					</span>
					<?php 
        }
        ?>
					<?php 
    } else {
        _e('There are no taxonomy archives to assign Content Templates to', 'wpv-views');
    }
    $tax_content = ob_get_clean();
    ?>
				<div class="js-wpv-content-template-dropdown-list wpv-content-template-dropdown-list<?php 
    echo $open_section ? '' : ' hidden';
    ?>
">
					<?php 
    echo $tax_content;
    ?>
				</div>
            </div>
        </div>
        <div class="wpv-dialog-footer">
            <button class="button js-dialog-close"><?php 
    _e('Cancel', 'wpv-views');
    ?>
</button>
            <button class="button button-primary js-ct-change-type-process" data-id="<?php 
    echo esc_attr($id);
    ?>
"><?php 
    _e('Change', 'wpv-views');
    ?>
</button>
        </div>
        </form>
		</div>
    <?php 
    die;
}
function wpv_admin_menu_content_template_listing_by_type_row($sort, $page = 0)
{
    global $WPV_settings, $post, $wpdb;
    // $post_types = get_post_types( array('public' => true), 'objects' );
    $post_types_array = wpv_get_pt_tax_array();
    ob_start();
    if ($sort == 'usage-single') {
        $counter = count($post_types_array['single_post']);
        $alternate = '';
        for ($i = 0; $i < $counter; ++$i) {
            $type = $post_types_array['single_post'][$i][0];
            $label = $post_types_array['single_post'][$i][1];
            $alternate = ' alternate' == $alternate ? '' : ' alternate';
            ?>
			<tr id="wpv_ct_list_row_<?php 
            echo $type;
            ?>
" class="js-wpv-ct-list-row<?php 
            echo $alternate;
            ?>
">

				<td class="wpv-admin-listing-col-usage post-title page-title column-title">
					<span class="row-title">
						<?php 
            echo $label;
            ?>
					</span>
					<?php 
            $row_actions = array("change_pt js-list-ct-action-change-pt" => sprintf('<a href="#">%s</a>', __('Change Content Template', 'wpv-views')));
            echo wpv_admin_table_row_actions($row_actions, array("data-msg" => 1, "data-sort" => $sort, "data-pt" => 'views_template_for_' . $type));
            ?>
				</td>

				<td class="wpv-admin-listing-col-used-title">
					<ul>
						<?php 
            $add_button = sprintf('<a class="button button-small" data-disabled="1"
											href="%s">
										<i class="icon-plus"></i>
										%s
									</a>', esc_url(add_query_arg(array('post_type' => 'view-template', 'action' => 'wpv_ct_create_new', 'post_title' => urlencode(__('Content template for ', 'wpv-views') . $label), 'ct_selected' => 'views_template_for_' . $type, 'toggle' => '1,0,0'), admin_url('post-new.php'))), sprintf(__('Create a Content Template for single %s', 'wpv-views'), $label));
            // TODO get_posts or explanation why is it done this way (optimalization?)
            $posts = $wpdb->get_col($wpdb->prepare("SELECT {$wpdb->posts}.ID FROM {$wpdb->posts} \n\t\t\t\t\t\t\t\t\tWHERE post_type = %s \n\t\t\t\t\t\t\t\t\tAND post_status != 'auto-draft'", $type));
            $count = sizeof($posts);
            $posts_ids = "'" . implode("','", $posts) . "'";
            if (isset($WPV_settings['views_template_for_' . $type])) {
                if ($WPV_settings['views_template_for_' . $type] != 0) {
                    $template = get_post($WPV_settings['views_template_for_' . $type]);
                    if (is_object($template)) {
                        printf('<a href="%s">%s</a>', esc_url(add_query_arg(array('post' => $template->ID, 'action' => 'edit'), admin_url('post.php'))), $template->post_title);
                        if ($count > 0) {
                            $set_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(post_id) FROM {$wpdb->postmeta}\n\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE meta_key = '_views_template'\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND meta_value = %s\n\t\t\t\t\t\t\t\t\t\t\t\t\tAND post_id IN ({$posts_ids}) \n\t\t\t\t\t\t\t\t\t\t\t\t\tLIMIT %d", $WPV_settings['views_template_for_' . $type], $count));
                            if ($count - $set_count > 0) {
                                ?>
												<span class="js-alret-icon-hide-<?php 
                                echo $type;
                                ?>
">
													<?php 
                                printf('<a class="%s" data-target="%s"> %s</a>', 'button button-small button-leveled icon-warning-sign js-apply-for-all-posts', esc_url(add_query_arg(array('action' => 'wpv_ct_update_posts', 'type' => $type, 'tid' => $template->ID, 'wpnonce' => wp_create_nonce('work_view_template')), admin_url('admin-ajax.php'))), sprintf(__('Bind %u %s ', 'wpv-views'), $count - $set_count, $label));
                                ?>
												</span>
												<?php 
                            }
                        }
                    } else {
                        echo $add_button;
                    }
                } else {
                    echo $add_button;
                    if ($count > 0) {
                        $set_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(post_id) FROM {$wpdb->postmeta}\n\t\t\t\t\t\t\t\t\t\t\t\tWHERE meta_key = '_views_template'\n\t\t\t\t\t\t\t\t\t\t\t\tAND meta_value != %s\n\t\t\t\t\t\t\t\t\t\t\t\tAND post_id IN ({$posts_ids}) \n\t\t\t\t\t\t\t\t\t\t\t\tLIMIT %d", '0', $count));
                        if ($set_count > 0) {
                            ?>
											<a class="button button-small js-single-unlink-template-open-dialog" href="#"
													data-unclear="<?php 
                            echo $set_count;
                            ?>
"
													data-slug="<?php 
                            echo $type;
                            ?>
"
													data-label="<?php 
                            echo htmlentities($label, ENT_QUOTES);
                            ?>
">
												<i class="icon-unlink"></i>
												<?php 
                            echo sprintf(__('Clear %d %s', 'wpv-views'), $set_count, $label);
                            ?>
											</a>
											<?php 
                        }
                    }
                }
            } else {
                echo $add_button;
                if ($count > 0) {
                    $set_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(post_id) FROM {$wpdb->postmeta}\n\t\t\t\t\t\t\t\t\t\t\tWHERE meta_key = '_views_template'\n\t\t\t\t\t\t\t\t\t\t\tAND meta_value != %s\n\t\t\t\t\t\t\t\t\t\t\tAND post_id IN ({$posts_ids}) \n\t\t\t\t\t\t\t\t\t\t\tLIMIT %d", '0', $count));
                    if ($set_count > 0) {
                        ?>
										<a class="button button-small js-single-unlink-template-open-dialog" href="#"
												data-unclear="<?php 
                        echo $set_count;
                        ?>
"
												data-slug="<?php 
                        echo $type;
                        ?>
"
												data-label="<?php 
                        echo htmlentities($label, ENT_QUOTES);
                        ?>
">
											<i class="icon-unlink"></i>
											<?php 
                        echo sprintf(__('Clear %d %s', 'wpv-views'), $set_count, $label);
                        ?>
										</a>
										<?php 
                    }
                }
            }
            ?>
					</ul>
				</td>
			</tr>
			<?php 
        }
    } else {
        if ($sort == 'usage-post-archives') {
            $alternate = '';
            $counter = count($post_types_array['archive_post']);
            for ($i = 0; $i < $counter; ++$i) {
                $type = $post_types_array['archive_post'][$i][0];
                $label = $post_types_array['archive_post'][$i][1];
                $add_button = sprintf('<a class="button button-small" data-disabled="1" href="%s"><i class="icon-plus"></i> %s</a>', esc_url(add_query_arg(array('post_type' => 'view-template', 'action' => 'wpv_ct_create_new', 'post_title' => urlencode(__('Content template for ', 'wpv-views') . $label), 'ct_selected' => 'views_template_archive_for_' . $type, 'toggle' => '0,1,0'), admin_url('post-new.php'))), __('Add a new Content Template for this post type', 'wpv-views'));
                $alternate = ' alternate' == $alternate ? '' : ' alternate';
                ?>
			<tr id="wpv_ct_list_row_<?php 
                echo $type;
                ?>
" class="js-wpv-ct-list-row<?php 
                echo $alternate;
                ?>
">
				<td class="post-title page-title column-title">
					<span class="row-title">
						<?php 
                echo $label;
                ?>
					</span>
					<?php 
                $row_actions = array("change_pt js-list-ct-action-change-pt" => sprintf('<a href="#">%s</a>', __('Change Content Template', 'wpv-views')));
                echo wpv_admin_table_row_actions($row_actions, array("data-msg" => 1, "data-sort" => $sort, "data-pt" => 'views_template_archive_for_' . $type));
                ?>
				</td>
				<td>
					<ul>
						<?php 
                if (isset($WPV_settings['views_template_archive_for_' . $type]) && $WPV_settings['views_template_archive_for_' . $type] != 0) {
                    $post = get_post($WPV_settings['views_template_archive_for_' . $type]);
                    if (is_object($post)) {
                        printf('<a href="%s">%s</a>', esc_url(add_query_arg(array('post' => $post->ID, 'action' => 'edit'), admin_url('post.php'))), $post->post_title);
                    } else {
                        echo $add_button;
                    }
                } else {
                    echo $add_button;
                }
                ?>
					</ul>
				</td>
			</tr>
			<?php 
            }
        } else {
            if ($sort == 'usage-taxonomy-archives') {
                $counter = count($post_types_array['taxonomy_post']);
                $alternate = '';
                for ($i = 0; $i < $counter; ++$i) {
                    $type = $post_types_array['taxonomy_post'][$i][0];
                    $label = $post_types_array['taxonomy_post'][$i][1];
                    $add_button = sprintf('<a class="button button-small js-wpv-ct-create-new-for-usage" data-disabled="1"
							data-title="%s" data-usage="%s" href="%s">
						<i class="icon-plus"></i>
						%s
					</a>', urlencode(__('Content template for ', 'wpv-views') . $label), 'views_template_loop_' . $type, esc_url(add_query_arg(array('post_type' => 'view-template', 'action' => 'wpv_ct_create_new', 'post_title' => urlencode(__('Content template for ', 'wpv-views') . $label), 'ct_selected' => 'views_template_loop_' . $type, 'toggle' => '0,0,1'), admin_url('post-new.php'))), __('Add a new Content Template for this taxonomy', 'wpv-views'));
                    $alternate = ' alternate' == $alternate ? '' : ' alternate';
                    ?>
			<tr id="wpv_ct_list_row_<?php 
                    echo $type;
                    ?>
" class="js-wpv-ct-list-row<?php 
                    echo $alternate;
                    ?>
">
				<td class="post-title page-title column-title">
					<span class="row-title">
						<?php 
                    echo $label;
                    ?>
					</span>
					<?php 
                    $row_actions = array("change_pt js-list-ct-action-change-pt" => sprintf('<a href="#">%s</a>', __('Change Content Template', 'wpv-views')));
                    echo wpv_admin_table_row_actions($row_actions, array("data-msg" => 2, "data-sort" => $sort, "data-pt" => 'views_template_loop_' . $type));
                    ?>
				</td>
				<td>
					<ul>
						<?php 
                    if (isset($WPV_settings['views_template_loop_' . $type]) && $WPV_settings['views_template_loop_' . $type] != 0) {
                        $post = get_post($WPV_settings['views_template_loop_' . $type]);
                        if (is_object($post)) {
                            printf('<a href="%s">%s</a>', esc_url(add_query_arg(array('post' => $post->ID, 'action' => 'edit'), admin_url('post.php'))), $post->post_title);
                        } else {
                            echo $add_button;
                        }
                    } else {
                        echo $add_button;
                    }
                    ?>
					</ul>
				</td>
			</tr>
			<?php 
                }
            }
        }
    }
    $row = ob_get_contents();
    ob_end_clean();
    return $row;
}