Created
November 22, 2017 06:39
-
-
Save anonymous/c402e2e70ed6dc510190173cdaca8221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_once get_template_directory() . '/lib/widget-base/class-widget-base.php'; | |
if ( ! function_exists( 'university_hub_load_widgets' ) ) : | |
/** | |
* Load widgets. | |
* | |
* @since 1.0.0 | |
*/ | |
function child_university_hub_load_widgets() { | |
// Featured Page widget. | |
unregister_widget( 'University_Hub_Featured_Page_Widget' ); | |
register_widget( 'Child_University_Hub_Featured_Page_Widget' ); | |
} | |
endif; | |
add_action( 'widgets_init', 'child_university_hub_load_widgets',11 ); | |
if ( ! class_exists( 'Child_University_Hub_Featured_Page_Widget' ) ) : | |
/** | |
* Featured page widget Class. | |
* | |
* @since 1.0.0 | |
*/ | |
class Child_University_Hub_Featured_Page_Widget extends University_Hub_Widget_Base { | |
/** | |
* Sets up a new widget instance. | |
* | |
* @since 1.0.0 | |
*/ | |
function __construct() { | |
$opts = array( | |
'classname' => 'university_hub_widget_featured_page', | |
'description' => __( 'Displays single featured Page or Post.', 'university-hub-pro' ), | |
'customize_selective_refresh' => true, | |
); | |
$fields = array( | |
'title' => array( | |
'label' => __( 'Title:', 'university-hub-pro' ), | |
'type' => 'text', | |
'class' => 'widefat', | |
), | |
'use_page_title' => array( | |
'label' => __( 'Use Page/Post Title as Widget Title', 'university-hub-pro' ), | |
'type' => 'checkbox', | |
'default' => true, | |
), | |
'featured_page' => array( | |
'label' => __( 'Select Page:', 'university-hub-pro' ), | |
'type' => 'dropdown-pages', | |
'show_option_none' => __( '— Select —', 'university-hub-pro' ), | |
), | |
'id_message' => array( | |
'label' => '<strong>' . _x( 'OR', 'message', 'university-hub-pro' ) . '</strong>', | |
'type' => 'message', | |
), | |
'featured_post' => array( | |
'label' => __( 'Post ID:', 'university-hub-pro' ), | |
'placeholder' => __( 'Eg: 1234', 'university-hub-pro' ), | |
'type' => 'text', | |
'sanitize_callback' => 'university_hub_widget_sanitize_post_id', | |
), | |
'content_type' => array( | |
'label' => __( 'Show Content:', 'university-hub-pro' ), | |
'type' => 'select', | |
'default' => 'full', | |
'options' => array( | |
'excerpt' => __( 'Excerpt', 'university-hub-pro' ), | |
'full' => __( 'Full', 'university-hub-pro' ), | |
), | |
), | |
'excerpt_length' => array( | |
'label' => __( 'Excerpt Length:', 'university-hub-pro' ), | |
'description' => __( 'Applies when Excerpt is selected in Content option.', 'university-hub-pro' ), | |
'type' => 'number', | |
'css' => 'max-width:60px;', | |
'default' => 40, | |
'min' => 1, | |
'max' => 400, | |
), | |
'featured_image' => array( | |
'label' => __( 'Featured Image:', 'university-hub-pro' ), | |
'type' => 'select', | |
'options' => university_hub_get_image_sizes_options(), | |
), | |
'featured_image_alignment' => array( | |
'label' => __( 'Image Alignment:', 'university-hub-pro' ), | |
'type' => 'select', | |
'default' => 'center', | |
'options' => university_hub_get_image_alignment_options(), | |
), | |
); | |
parent::__construct( 'university-hub-featured-page', __( 'UH: Featured Page', 'university-hub-pro' ), $opts, array(), $fields ); | |
} | |
/** | |
* Outputs the content for the current widget instance. | |
* | |
* @since 1.0.0 | |
* | |
* @param array $args Display arguments. | |
* @param array $instance Settings for the current widget instance. | |
*/ | |
function widget( $args, $instance ) { | |
$params = $this->get_params( $instance ); | |
// ID validation. | |
$our_post_object = null; | |
$our_id = ''; | |
if ( absint( $params['featured_post'] ) > 0 ) { | |
$our_id = absint( $params['featured_post'] ); | |
} | |
if ( absint( $params['featured_page'] ) > 0 ) { | |
$our_id = absint( $params['featured_page'] ); | |
} | |
if ( absint( $our_id ) > 0 ) { | |
$raw_object = get_post( $our_id ); | |
if ( ! in_array( $raw_object->post_type, array( 'attachment', 'nav_menu_item', 'revision' ) ) ) { | |
$our_post_object = $raw_object; | |
} | |
} | |
if ( ! $our_post_object ) { | |
// No valid object; bail now! | |
return; | |
} | |
echo $args['before_widget']; | |
global $post; | |
// Setup global post. | |
$post = $our_post_object; | |
setup_postdata( $post ); | |
// Override title if checkbox is selected. | |
if ( true === $params['use_page_title'] ) { | |
$params['title'] = get_the_title( $post ); | |
$params['link'] = get_the_permalink( $post ); | |
} | |
?> | |
<div class="featured-page-widget entry-content"> | |
<?php if ( 'disable' !== $params['featured_image'] && has_post_thumbnail() ) : ?> | |
<?php the_post_thumbnail( esc_attr( $params['featured_image'] ), array( 'class' => 'align' . esc_attr( $params['featured_image_alignment'] ) ) ); ?> | |
<?php endif; ?> | |
<div class="fetured-page-content"> | |
<?php | |
if ( ! empty( $params['title'] ) && ! empty( $params['link']) ) { | |
echo $args['before_title'] . "<a href='".$params['link']."'>". $params['title'] .'</a>' . $args['after_title']; | |
} | |
?> | |
<?php if ( 'excerpt' === $params['content_type'] ) : ?> | |
<?php | |
$excerpt = university_hub_the_excerpt( absint( $params['excerpt_length'] ) ); | |
echo wp_kses_post( wpautop( $excerpt ) ); | |
?> | |
<?php else : ?> | |
<?php the_content(); ?> | |
<?php endif; ?> | |
</div> <!-- .fetured-page-content --> | |
</div><!-- .featured-page-widget --> | |
<?php | |
// Reset. | |
wp_reset_postdata(); | |
echo $args['after_widget']; | |
} | |
} | |
endif; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment