Example #1
0
/**
 * Callback function for the AJAX action wp_ajax_wpv_ct_update_posts used to count dissident posts that are not using
 * the Template assigned to its type. This is called on the Content Templates listing screen for single usage
 * and on the Template edit screen.
 *
 * @since 1.3.0
 */
function wpv_apply_ct_to_cpt_posts_popup_callback() {
	wpv_ajax_authenticate( 'work_view_template', array( 'parameter_source' => 'get', 'type_of_death' => 'data' ) );

    if ( 
		isset ( $_GET['type'] ) 
		&& isset( $_GET['id'] ) 
	) {
        $type = sanitize_text_field( $_GET['type'] );
        $id = intval( $_GET['id'] );
    } else {
		$data = array(
			'message' => __( 'Wrong data.', 'wpv-views' )
		);
		wp_send_json_error( $data );
    }
	
	try {
        $ct = new WPV_Content_Template_Embedded( $id );
    } catch( Exception $e ) {
        // well, we were not handling non-existent CTs before and I am not sure how to do that now...
        $data = array(
			'message' => __( 'Wrong data.', 'wpv-views' )
		);
		wp_send_json_error( $data );
    }
	
    $dissident_post_count = $ct->get_dissident_posts( $type, 'count' );
    if ( $dissident_post_count > 0 ) {
        $ptype = get_post_type_object( $type );

        if ( $dissident_post_count > 1 ){
            $type_label = $ptype->labels->name;
            $message = '<p>' . sprintf( __( '<strong>%d %s</strong> use a different Content Template.', 'wpv-views' ), $dissident_post_count , $type_label ) . '</p>';
        } else {
            $type_label = $ptype->labels->singular_name;
            $message = '<p>' . sprintf( __( '<strong>%d %s</strong> uses a different Content Template.', 'wpv-views' ), $dissident_post_count, $type_label ) . '</p>';
        }
		$message .= '<p>' 
			. __( 'Maybe you have manually set a different one on them.', 'wpv-views' ) 
			. WPV_MESSAGE_SPACE_CHAR
			. sprintf(
					__( 'Click <em>Update</em> to force this Content Template into all single %s', 'wpv-views' ),
					$type_label
				)
			. '</p>';
		$data = array(
			'dialog_content' => '<div class="wpv-dialog wpv-shortcode-gui-content-wrapper">' . $message . '</div>'
		);
		wp_send_json_success( $data );
        ?>
    <?php
    } else {
		$data = array(
			'message' => __( 'Wrong data.', 'wpv-views' )
		);
		wp_send_json_error( $data );
	}
}
/**
 * Counts the amount of posts of a given type that do not use a given Template and creates the HTML structure to notify about it
 * Used on the Views popups for the Content Templates listing page on single usage
 *
 * @param int $template_id the ID of the Content Template we want to check against
 * @param string $content_type the post type to check
 * @param string $message_header (optional) to override the default message on the HTML structure header "Do you want to apply to all?"
 *
 * @return void
 *
 * @since 1.5.1
 *
 * @deprecated since 1.10
 */
function wpv_count_dissident_posts_from_template( $template_id, $content_type, $message_header = null ) {
	
    try {
        $ct = new WPV_Content_Template_Embedded( $template_id );
    } catch( Exception $e ) {
        // well, we were not handling non-existent CTs before and I am not sure how to do that now...
        return;
    }

    $dissident_post_count = $ct->get_dissident_posts( $content_type, 'count' );

    if ( $dissident_post_count > 0 ) {
        $ptype = get_post_type_object( $content_type );

        if ( $dissident_post_count > 1 ){
            $type_label = $ptype->labels->name;
            $message = sprintf( __( '<strong>%d %s</strong> use a different Content Template.', 'wpv-views' ), $dissident_post_count , $type_label );
        } else {
            $type_label = $ptype->labels->singular_name;
            $message = sprintf(__('<strong>%d %s</strong> uses a different Content Template.', 'wpv-views'), $dissident_post_count, $type_label);
        }

        ?>
        <div class="wpv-dialog wpv-shortcode-gui-content-wrapper">
			<?php echo $message; ?>
        </div>
    <?php
    }

}