/**
     * Show information about how a CT is being used.
     *
     * @param $item WPV_Content_Template_Embedded Content template.
     *
     * @return string Content of the table cell.
     */
    public function column_used_on( $item ) {

        if( $item->is_owned_by_view ) {
            // This CT is used as a template for Loop Output in a View or WPA.

            // Get a View or WPA object. We'll be using only methods from their base, so it doesn't matter which one is it.
            $owner_view = WPV_View_Base::get_instance( $item->loop_output_id );

            if( $owner_view == null ) {
                // Something is wrong - most probably the owner doesn't exist.
                return '';
            }

            // Display the appropriate message.
            if( $owner_view->is_published ) {
				$edit_page = 'views-embedded';
				if ( WPV_View_Base::is_archive_view( $owner_view->id ) ) {
					$edit_page = 'view-archives-embedded';
				}
                return sprintf(
                    '<span>%s</span>',
                    sprintf( __( 'This Content Template is used as the loop block for the %s <a href="%s" target="_blank">%s</a>', 'wpv-views' ),
                        $owner_view->query_mode_display_name,
                        esc_url( add_query_arg(
                            array( 
								'page' => $edit_page, 
								'view_id' => $owner_view->id 
							),
                            admin_url( 'admin.php' ) ) ),
                        $owner_view->title ) );
            } else {
                return sprintf(
                    '<span>%s</span>',
                    sprintf(
                        __( 'This Content Template is used as the loop block for the trashed %s <strong>%s</strong>', 'wpv-views' ),
                        $owner_view->query_mode_display_name,
                        $owner_view->title ) );
            }

        } else {
            // This is a normal CT. Obtain information about assignments and display them in a tag-like list.
            $list = array();

            // "single posts"
            $assigned_single_pts = $item->get_assigned_single_post_types();
            foreach( $assigned_single_pts as $loop ) {
                $list[] = sprintf( '<li>%s%s</li>', $loop['display_name'], __(' (single)', 'wpv-views') );
            }

            // post type archives
            $assigned_pt_loops = $item->get_assigned_loops( 'post_type' );
            foreach( $assigned_pt_loops as $loop ) {
                $list[] = sprintf( '<li>%s%s</li>', $loop['display_name'], __(' (post type archive)', 'wpv-views') );
            }

            // taxonomy archives
            $assigned_ta_loops = $item->get_assigned_loops( 'taxonomy' );
            foreach( $assigned_ta_loops as $loop ) {
                $list[] = sprintf( '<li>%s%s</li>', $loop['display_name'], __(' (taxonomy archive)', 'wpv-views') );
            }

            if( !empty( $list ) ) {
                return sprintf( '<ul class="wpv-taglike-list">%s</ul>', implode( $list ) );
            } else {
                return sprintf( '<span>%s</span>', __( 'No Post types/Taxonomies assigned', 'wpv-views' ) );
            }
        }


    }
Пример #2
0
    /**
     * Create an appropriate wrapper for View or WPA post object.
     *
     * Decides by self::is_archive_view() if it's a WPA. Then it checks whether the full version of the wrapper exist,
     * and instantiates it or falls back to the embedded version.
     *
     * @param int|WP_Post $view Post ID or post object.
     *
     * @return null|WPV_View_Embedded|WPV_WordPress_Archive_Embedded|WPV_View|WPV_WordPress_Archive The appropriate wrapper or null on error.
     */
    public static function get_instance( $view ) {
        if( is_integer( $view ) ) {
            $post = WP_Post::get_instance( $view );
        } else {
            $post = $view;
        }

        if( ! WPV_View_Base::is_wppost_view( $post ) ) {
            return null;
        }

        try {
            if ( WPV_View_Base::is_archive_view( $post->ID ) ) {
                if( class_exists( 'WPV_WordPress_Archive' ) ) {
                    return new WPV_WordPress_Archive( $post );
                } else {
                    return new WPV_WordPress_Archive_Embedded( $post );
                }
            } else {
                if( class_exists( 'WPV_View' ) ) {
                    return new WPV_View( $post );
                } else {
                    return new WPV_View_Embedded( $post );
                }
            }
        } catch( Exception $ex ) {
            return null;
        }
    }
/**
 * Render list items with information about usage of this Content Template.
 *
 * Also render "Bind posts" buttons where applicable.
 * Different info shows when CT is a loop template of some View/WPA.
 *
 * @param int $ct_id Content template ID
 * @return string Rendered HTML code.
 *
 * @since unknown
 *
 * @todo this needs refactoring to get rid of wpv_get_pt_tax_array() etc.
 */
function wpv_content_template_used_for_list( $ct_id ) {
	global $WPV_settings;

    $list = '';

    $ct = WPV_Content_Template::get_instance( $ct_id );

    if( null == $ct ) {
        // this should never happen; still, there is a serious lack of error handling
        return '';
    }

	if ( ! $ct->is_owned_by_view ) {
		$post_types_array = wpv_get_pt_tax_array();
		$count_single_post = count( $post_types_array['single_post'] );
		$count_archive_post = count( $post_types_array['archive_post'] );
		$count_taxonomy_post = count( $post_types_array['taxonomy_post'] );

		for ( $i=0; $i<$count_single_post; $i++ ) {
			$type = $post_types_array['single_post'][$i][0];
			$label = $post_types_array['single_post'][$i][1];
			if ( isset( $WPV_settings['views_template_for_' . $type] ) && $WPV_settings['views_template_for_' . $type] == $ct_id ) {
                $list .= '<li>' . $label . __(' (single)', 'wpv-views');

				// @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 ) {
                    $list .= sprintf(
                        '<span class="%s"><a class="%s" data-type="%s" data-id="%s" data-nonce="%s"> %s</a></span>',
                        'js-wpv-apply-ct-to-cpt-single-' . $type,
                        '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 )
                    );
                }

				$list .= '</li>';
			}
		}

		for ( $i=0; $i < $count_archive_post; $i++ ) {
			$type = $post_types_array['archive_post'][$i][0];
			$label = $post_types_array['archive_post'][$i][1];
			if ( isset( $WPV_settings['views_template_archive_for_' . $type] ) && $WPV_settings['views_template_archive_for_' . $type] == $ct_id ) {
                $list .= '<li>' . $label . __(' (post type archive)','wpv-views') . '</li>';
			 }
		}

		for ( $i=0; $i < $count_taxonomy_post; $i++ ) {
			$type = $post_types_array['taxonomy_post'][$i][0];
			$label = $post_types_array['taxonomy_post'][$i][1];
			if ( isset( $WPV_settings['views_template_loop_' . $type] ) && $WPV_settings['views_template_loop_' . $type] == $ct_id ) {
                $list .= '<li>' . $label . __(' (taxonomy archive)','wpv-views') . '</li>';
			 }
		}
		
		if ( ! empty( $list ) ) {
			$list = '<ul class="wpv-taglike-list">' . $list . '</ul>';
		} else {
		   $list = '<span>' . __( 'No Post types/Taxonomies assigned', 'wpv-views' ) . '</span>';
		}
	} else {
        // This CT is owned by a View/WPA and used as a loop template

        $owner_view = WPV_View_Base::get_instance( $ct->loop_output_id );
        if( null == $owner_view ) {
            // again, there was no check for missing View before!
            return '';
        }

        // Show usage information depending on owner View post status.
		if ( $owner_view->is_published ) {
			$edit_page = 'views-editor';
			if ( WPV_View_Base::is_archive_view( $owner_view->id ) ) {
				$edit_page = 'view-archives-editor';
			}
			$list = sprintf(
                __( 'This Content Template is used as the loop block for the %s <a href="%s" target="_blank">%s</a>', 'wpv-views' ),
                $owner_view->query_mode_display_name,
                add_query_arg(
                    array(
                        'page' => $edit_page,
                        'view_id' => $owner_view->id
                    ),
                    admin_url( 'admin.php' )
                ),
                $owner_view->title
            );

		} else {

			$list = sprintf(
                __( 'This Content Template is used as the loop block for the trashed %s <strong>%s</strong>', 'wpv-views' ),
                $owner_view->query_mode_display_name,
                $owner_view->title
            );

		}
	}
	return "<span>$list</span>";
}
Пример #4
0
function wpv_ct_editor_usage_section( $ct ) {
	ob_start();

    $parent_view = null;
    if( $ct->is_owned_by_view ) {
        $parent_view = WPV_View_Base::get_instance($ct->loop_output_id);
    }

    if( null != $parent_view ) {

        if( $parent_view->is_published ) {
			$edit_page = 'views-editor';
			if ( WPV_View_Base::is_archive_view( $parent_view->id ) ) {
				$edit_page = 'view-archives-editor';
			}
            $loop_template_notice = sprintf(
                __( 'This Content Template is used as the loop block for the %s <a href="%s" target="_blank">%s</a>.', 'wpv-views' ),
                $parent_view->query_mode_display_name,
                esc_attr( add_query_arg(
                    array(
                        'page' => $edit_page,
                        'view_id' => $parent_view->id
                    ),
                    admin_url( 'admin.php' )
                ) ),
                $parent_view->title
            );

        } else {

            $loop_template_notice = sprintf(
                __( 'This Content Template is used as the loop block for the trashed %s %s.', 'wpv-views' ),
                $parent_view->query_mode_display_name,
                "<strong>{$parent_view->title}</strong>"
            );
        }

        printf( '<div class="wpv-advanced-setting"><p>%s</p></div>', $loop_template_notice );

    } else {

        $asterisk_explanation =
            '<span data-bind="fadeVisibility: isAsteriskExplanationVisible(\'%s\', \'%s\')"><span style="color:red">*</span> '
            . __('A different Content Template is already assigned to this item.', 'wpv-views')
            . '</span>';


        // Render checkboxes for each type of assignment.
        $single_post_types_with_other_ct = wpv_ct_editor_usage_section_single_pages($ct, $asterisk_explanation);
        $cpt_archives_with_other_ct = wpv_ct_editor_usage_section_post_archives($ct, $asterisk_explanation);
        $taxonomy_archives_with_other_ct = wpv_ct_editor_usage_section_taxonomy_archives($ct, $asterisk_explanation);

        // Print information about other CT assignments for JS
        $other_assignments = array(
            'single_posts' => $single_post_types_with_other_ct,
            'cpt_archives' => $cpt_archives_with_other_ct,
            'taxonomy_archives' => $taxonomy_archives_with_other_ct
        );


        printf(
            '<span style="visibility: hidden" class="js-wpv-usage-other-assignments" data-value="%s"></span>',
            htmlentities(json_encode($other_assignments))
        );
        ?>

        <p class="update-button-wrap">
            <span class="update-action-wrap">
                <span class="js-wpv-message-container"></span>
                <span class="spinner ajax-loader" data-bind="spinnerActive: isUsageSectionUpdating"></span>
            </span>
            <button data-bind="
                        enable: isUsageSectionUpdateNeeded,
                        attr: { class: isUsageSectionUpdateNeeded() ? 'button-primary' : 'button-secondary' },
                        click: usageSectionUpdate">
                <?php _e('Update', 'wpv-views'); ?>
            </button>
        </p>

    <?php
    }

	$content = ob_get_contents();
	ob_end_clean();

	wpv_ct_editor_render_section(
        __( 'Usage', 'wpv-views' ),
        'js-wpv-usage-section',
        $content,
        false,
        '',
        '',
        array( 'section' => 'usage_section', 'pointer_slug' => 'ptr_section' ) );
}