Skip to content

Instantly share code, notes, and snippets.

@dkalisch
Created July 21, 2015 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkalisch/161bb5251e827d59ebdc to your computer and use it in GitHub Desktop.
Save dkalisch/161bb5251e827d59ebdc to your computer and use it in GitHub Desktop.
Moesia_Projects modified file
<?php
class Moesia_Projects_HUI extends WP_Widget {
// constructor
function moesia_projects_hui() {
$widget_ops = array('classname' => 'moesia_projects_hui_widget', 'description' => __( 'Show your most intresting projects.', 'moesia') );
parent::WP_Widget(false, $name = __('Moesia FP: Projects', 'moesia'), $widget_ops);
$this->alt_option_name = 'moesia_projects_hui_widget';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
// widget form creation
function form($instance) {
// Check values
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$category = isset( $instance['category '] ) ? esc_attr( $instance['category '] ) : '';
$image_uri = isset( $instance['image_uri'] ) ? esc_url_raw( $instance['image_uri'] ) : '';
?>
<p><?php _e('In order to display this widget, you must first add some projects from the dashboard. Add as many as you want and the theme will automatically display them all.', 'moesia'); ?></p>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'moesia'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p><label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Enter the slug for your category or leave empty to show all projects.', 'moesia' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>" type="text" value="<?php echo $category; ?>" size="3" /></p>
<?php
if ( $image_uri != '' ) :
echo '<p><img class="custom_media_image" src="' . $image_uri . '" style="max-width:100px;" /></p>';
endif;
?>
<p><label for="<?php echo $this->get_field_id('image_uri'); ?>"><?php _e('Upload an image for the background if you want. It will get a parallax effect.', 'moesia'); ?></label></p>
<p><input type="button" class="button button-primary custom_media_button" id="custom_media_button" name="<?php echo $this->get_field_name('image_uri'); ?>" value="Upload Image" style="margin-top:5px;" /></p>
<p><input class="widefat custom_media_url" id="<?php echo $this->get_field_id( 'image_uri' ); ?>" name="<?php echo $this->get_field_name( 'image_uri' ); ?>" type="text" value="<?php echo $image_uri; ?>" size="3" /></p>
<?php
}
// update widget
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['image_uri'] = esc_url_raw( $new_instance['image_uri'] );
$instance['category'] = strip_tags($new_instance['category']);
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['moesia_projects_hui']) )
delete_option('moesia_projects_hui');
return $instance;
}
function flush_widget_cache() {
wp_cache_delete('moesia_projects_hui', 'widget');
}
// display widget
function widget($args, $instance) {
$cache = array();
if ( ! $this->is_preview() ) {
$cache = wp_cache_get( 'moesia_projects_hui', 'widget' );
}
if ( ! is_array( $cache ) ) {
$cache = array();
}
if ( ! isset( $args['widget_id'] ) ) {
$args['widget_id'] = $this->id;
}
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
ob_start();
extract($args);
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Our projects', 'moesia' );
/** This filter is documented in wp-includes/default-widgets.php */
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
$image_uri = isset( $instance['image_uri'] ) ? esc_url($instance['image_uri']) : '';
$category = isset( $instance['category'] ) ? esc_attr($instance['category']) : '';
/**
* Filter the arguments for the Recent Posts widget.
*
* @since 3.4.0
*
* @see WP_Query::get_posts()
*
* @param array $args An array of arguments used to retrieve the recent posts.
*/
$r = new WP_Query( apply_filters( 'widget_posts_args', array(
'no_found_rows' => true,
'post_status' => 'publish',
'post_type' => 'projects',
'posts_per_page' => -1,
'category_name' => $category
) ) );
if ($r->have_posts()) :
?>
<section id="projects" class="projects-area">
<div class="container">
<?php if ( $title ) echo $before_title . '<span class="wow bounce">' . $title . '</span>' . $after_title; ?>
<div class="textwidget col-md-12 col-sm-12 col-xs-12">
<p>
The following examples represent a selection of projects in which the participating researchers were involved. Please click on one of the miages to get a short description and a link to the project webpage.
</p>
</div>
<?php $c = 1; ?>
<?php while ( $r->have_posts() ) : $r->the_post(); ?>
<?php //Get the custom field values
$post_id = get_the_ID();
$start_date = get_post_meta( get_the_ID(), 'wpcf-project-start-date', true );
$end_date = get_post_meta( get_the_ID(), 'wpcf-project-end-date', true );
$project_url_extern = get_post_meta( get_the_ID(), 'wpcf-project-url-extern', true );
$funding_type = get_post_meta( get_the_ID(), 'wpcf-funding-type', true );
$funding_volume = get_post_meta( get_the_ID(), 'wpcf-funding-volume', true );
$research_project = get_post_meta( get_the_ID(), 'wpcf-research-project', true );
$project_url = get_post_meta( get_the_ID(), 'wpcf-project-link', true ); //Get the custom URL for this project ?>
<div class="project col-md-4 col-sm-6 col-xs-6 wow zoomIn">
<span class="su-lightbox" data-mfp-src="#<?php echo esc_html($post_id); ?>" data-mfp-type="inline">
<div class="project-image">
<?php the_post_thumbnail('project-image'); ?>
<a class="link-icon" data-mfp-src="#<?php echo esc_html($post_id); ?>" data-mfp-type="inline" href="#" title="<?php the_title(); ?>"><i class="fa fa-link" data-mfp-src="#<?php echo esc_html($post_id); ?>" data-mfp-type="inline"></i></a>
</div>
<!-- A hidden layer that contains the content you want to add-->
<div id="<?php echo esc_html($post_id); ?>" class="white-popup mfp-hide">
<?php the_title('<h3>','</h3>'); ?>
<?php echo the_content(); ?>
<?php if ($start_date != '') : ?>
<p><em>Start date:</em> <?php echo date('F Y', $start_date); ?></p>
<?php endif; ?>
<?php if ($end_date != '') : ?>
<p><em>End date:</em> <?php echo date('F Y', $end_date); ?></p>
<?php else: ?>
<p><em>End date:</em> Ongoing</p>
<?php endif; ?>
<?php if ($research_project == '1') : ?>
<?php if ($funding_type != '') : ?>
<p><em>Funding type:</em> <?php echo esc_html($funding_type); ?></p>
<?php endif; ?>
<?php if ($funding_volume != '') : ?>
<p><em>Funding Volume:</em> <?php echo esc_html($funding_volume); ?></p>
<?php endif; ?>
<?php endif; ?>
<?php if ($funding_type != '') : ?>
<p><em>Project URL:</em> <a href="<?php echo esc_url($project_url_extern); ?>" target="_blank"><?php echo esc_url($project_url_extern); ?></a></p>
<?php endif; ?>
</div>
</span>
</div>
<?php endwhile; ?>
</div>
<?php if ($image_uri != '') : ?>
<style type="text/css">
.projects-area {
display: block;
background: url(<?php echo $image_uri; ?>) no-repeat;
background-position: center top;
background-attachment: fixed;
background-size: cover;
z-index: -1;
}
</style>
<?php endif; ?>
</section>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_postdata();
endif;
if ( ! $this->is_preview() ) {
$cache[ $args['widget_id'] ] = ob_get_flush();
wp_cache_set( 'moesia_projects_hui', $cache, 'widget' );
} else {
ob_end_flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment