| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
// First create the widget for the admin panel |
| 6 |
class custom_post_widget extends WP_Widget { |
| 7 |
function __construct() { |
| 8 |
$widget_ops = array( 'classname' => 'widget_custom_post_widget', 'description' => __( 'Displays custom post content in a widget', 'custom-post-widget' ) ); |
| 9 |
parent::__construct( 'custom_post_widget', __( 'Content Block', 'custom-post-widget' ), $widget_ops ); |
| 10 |
} |
| 11 |
|
| 12 |
function form( $instance ) { |
| 13 |
$custom_post_id = ''; // Initialize the variable |
| 14 |
if (isset($instance['custom_post_id'])) { |
| 15 |
$custom_post_id = esc_attr($instance['custom_post_id']); |
| 16 |
}; |
| 17 |
$show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : true; |
| 18 |
$show_featured_image = isset( $instance['show_featured_image'] ) ? $instance['show_featured_image'] : true; |
| 19 |
$apply_content_filters = isset( $instance['apply_content_filters'] ) ? $instance['apply_content_filters'] : true; |
| 20 |
?> |
| 21 |
|
| 22 |
<p> |
| 23 |
<label for="<?php echo esc_attr ( $this->get_field_id( 'custom_post_id' ) ); ?>"> <?php echo esc_html__( 'Content Block to Display:', 'custom-post-widget' ) ?> |
| 24 |
<select class="widefat" id="<?php echo esc_attr ( $this->get_field_id( 'custom_post_id' ) ); ?>" name="<?php echo esc_attr ( $this->get_field_name( 'custom_post_id' ) ); ?>"> |
| 25 |
<?php |
| 26 |
$args = array( 'post_type' => 'content_block', 'suppress_filters' => 0, 'numberposts' => -1, 'order' => 'ASC' ); |
| 27 |
$content_block = get_posts( $args ); |
| 28 |
if ($content_block) { |
| 29 |
foreach( $content_block as $content_block ) : setup_postdata( $content_block ); |
| 30 |
echo '<option value="' . esc_attr ( $content_block -> ID ) . '"'; |
| 31 |
if( $custom_post_id == $content_block -> ID ) { |
| 32 |
echo ' selected'; |
| 33 |
$widgetExtraTitle = $content_block -> post_title; |
| 34 |
}; |
| 35 |
echo '>' . esc_html ( $content_block -> post_title ) . '</option>'; |
| 36 |
endforeach; |
| 37 |
} else { |
| 38 |
echo '<option value="">' . esc_html__( 'No content blocks available', 'custom-post-widget' ) . '</option>'; |
| 39 |
}; |
| 40 |
?> |
| 41 |
</select> |
| 42 |
</label> |
| 43 |
</p> |
| 44 |
|
| 45 |
<input type="hidden" id="<?php echo esc_attr ($this -> get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr ( $this -> get_field_name( 'title' ) ); ?>" value="<?php if ( !empty( $widgetExtraTitle ) ) { echo esc_attr ( $widgetExtraTitle ); } ?>" /> |
| 46 |
|
| 47 |
<p> |
| 48 |
<?php |
| 49 |
echo '<a href="post.php?post=' . esc_attr ( $custom_post_id ) . '&action=edit">' . esc_html__( 'Edit Content Block', 'custom-post-widget' ) . '</a>' ; |
| 50 |
?> |
| 51 |
</p> |
| 52 |
|
| 53 |
<p> |
| 54 |
<input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['show_custom_post_title'] ), true ); ?> id="<?php echo esc_attr ( $this->get_field_id( 'show_custom_post_title' ) ); ?>" name="<?php echo esc_attr ( $this->get_field_name( 'show_custom_post_title' ) ); ?>" /> |
| 55 |
<label for="<?php echo esc_attr ( $this->get_field_id( 'show_custom_post_title' ) ); ?>"><?php echo esc_html__( 'Show post title', 'custom-post-widget' ) ?></label> |
| 56 |
</p> |
| 57 |
|
| 58 |
<p> |
| 59 |
<input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['show_featured_image'] ), true ); ?> id="<?php echo esc_attr ( $this->get_field_id( 'show_featured_image' ) ); ?>" name="<?php echo esc_attr ( $this->get_field_name( 'show_featured_image' ) ); ?>" /> |
| 60 |
<label for="<?php echo esc_attr ( $this->get_field_id( 'show_featured_image' ) ); ?>"><?php echo esc_html__( 'Show featured image', 'custom-post-widget' ) ?></label> |
| 61 |
</p> |
| 62 |
|
| 63 |
<p> |
| 64 |
<input class="checkbox" type="checkbox" <?php checked( (bool) isset( $instance['apply_content_filters'] ), true ); ?> id="<?php echo esc_attr ( $this->get_field_id( 'apply_content_filters' ) ); ?>" name="<?php echo esc_attr ( $this->get_field_name( 'apply_content_filters' ) ); ?>" /> |
| 65 |
<label for="<?php echo esc_attr ( $this->get_field_id( 'apply_content_filters' ) ); ?>"><?php echo esc_html__( 'Do not apply content filters', 'custom-post-widget' ) ?></label> |
| 66 |
</p> <?php |
| 67 |
} |
| 68 |
|
| 69 |
function update( $new_instance, $old_instance ) { |
| 70 |
$instance = $old_instance; |
| 71 |
$instance['custom_post_id'] = wp_strip_all_tags( $new_instance['custom_post_id'] ); |
| 72 |
$instance['show_custom_post_title'] = $new_instance['show_custom_post_title']; |
| 73 |
$instance['show_featured_image'] = $new_instance['show_featured_image']; |
| 74 |
$instance['apply_content_filters'] = $new_instance['apply_content_filters']; |
| 75 |
return $instance; |
| 76 |
} |
| 77 |
|
| 78 |
// Display the content block content in the widget area |
| 79 |
function widget($args, $instance) { |
| 80 |
extract($args); |
| 81 |
$custom_post_id = ( $instance['custom_post_id'] != '' ) ? esc_attr($instance['custom_post_id']) : esc_html__( 'Find', 'custom-post-widget' ); |
| 82 |
// Add support for WPML Plugin. |
| 83 |
if ( function_exists( 'icl_object_id' ) ){ |
| 84 |
$custom_post_id = icl_object_id( $custom_post_id, 'content_block', true ); |
| 85 |
} |
| 86 |
// Variables from the widget settings. |
| 87 |
$show_custom_post_title = isset( $instance['show_custom_post_title'] ) ? $instance['show_custom_post_title'] : false; |
| 88 |
$show_featured_image = isset($instance['show_featured_image']) ? $instance['show_featured_image'] : false; |
| 89 |
$apply_content_filters = isset($instance['apply_content_filters']) ? $instance['apply_content_filters'] : false; |
| 90 |
$content_post = get_post( $custom_post_id ); |
| 91 |
if ( ! $content_post ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
$post_status = get_post_status( $custom_post_id ); |
| 95 |
$content = $content_post->post_content; |
| 96 |
if ( $post_status == 'publish' ) { |
| 97 |
// Display custom widget frontend |
| 98 |
if ( $located = locate_template( 'custom-post-widget.php' ) ) { |
| 99 |
require $located; |
| 100 |
return; |
| 101 |
} |
| 102 |
if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected |
| 103 |
$content = apply_filters( 'the_content', $content); |
| 104 |
} |
| 105 |
echo $before_widget; |
| 106 |
if ( $show_custom_post_title ) { |
| 107 |
echo $before_title . apply_filters( 'widget_title',$content_post->post_title) . $after_title; // This is the line that displays the title (only if show title is set) |
| 108 |
} |
| 109 |
if ( $show_featured_image ) { |
| 110 |
echo get_the_post_thumbnail( $content_post -> ID ); |
| 111 |
} |
| 112 |
echo do_shortcode( $content ); // This is where the actual content of the custom post is being displayed |
| 113 |
echo $after_widget; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|