web/wp-content/themes/selecta/ocmx/widgets/four-block-widget.php
author ymh <ymh.work@gmail.com>
Mon, 22 Mar 2010 16:36:28 +0100
changeset 5 ac511f1ccc8e
parent 1 0d28b7c10758
permissions -rw-r--r--
add hgignore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
0d28b7c10758 First commit
ymh
parents:
diff changeset
     1
<?php
0d28b7c10758 First commit
ymh
parents:
diff changeset
     2
class four_block_widget extends WP_Widget {
0d28b7c10758 First commit
ymh
parents:
diff changeset
     3
    /** constructor */
0d28b7c10758 First commit
ymh
parents:
diff changeset
     4
    function four_block_widget() {
0d28b7c10758 First commit
ymh
parents:
diff changeset
     5
        parent::WP_Widget(false, $name = "OCMX Thumbnail Widget", array("description" => "Four block thumbnail widget for the Home Page. Place in the 'Index Header Panel'."));	
0d28b7c10758 First commit
ymh
parents:
diff changeset
     6
    }
0d28b7c10758 First commit
ymh
parents:
diff changeset
     7
0d28b7c10758 First commit
ymh
parents:
diff changeset
     8
    /** @see WP_Widget::widget */
0d28b7c10758 First commit
ymh
parents:
diff changeset
     9
    function widget($args, $instance) {		
0d28b7c10758 First commit
ymh
parents:
diff changeset
    10
        extract( $args );
0d28b7c10758 First commit
ymh
parents:
diff changeset
    11
		$widget_title =  esc_attr($instance["widget_title"]);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    12
		
0d28b7c10758 First commit
ymh
parents:
diff changeset
    13
		if(!($instance["post_category"])) :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    14
			$use_catId = 0;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    15
            $cat_query = "";
0d28b7c10758 First commit
ymh
parents:
diff changeset
    16
		else :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    17
	        $use_category = $instance["post_category"];
0d28b7c10758 First commit
ymh
parents:
diff changeset
    18
			$use_catId = get_category_by_slug($use_category);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    19
            $cat_query = "&category_name=".$use_catId->cat_name;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    20
		endif;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    21
       //Fetch the category for the widget
0d28b7c10758 First commit
ymh
parents:
diff changeset
    22
		if(!($instance["post_offset"])) :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    23
	        $use_offset = 0;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    24
		else :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    25
    	    $use_offset = $instance["post_offset"];
0d28b7c10758 First commit
ymh
parents:
diff changeset
    26
		endif;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    27
		
0d28b7c10758 First commit
ymh
parents:
diff changeset
    28
       //Fetch the count for the widget
0d28b7c10758 First commit
ymh
parents:
diff changeset
    29
		if(!($instance["post_count"])) :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    30
	        $post_count = 4;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    31
		else :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    32
    	    $post_count = $instance["post_count"];
0d28b7c10758 First commit
ymh
parents:
diff changeset
    33
		endif;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    34
		
0d28b7c10758 First commit
ymh
parents:
diff changeset
    35
		$ocmx_featured = new WP_Query("showposts=$post_count&post_type=post".$cat_query);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    36
0d28b7c10758 First commit
ymh
parents:
diff changeset
    37
		//Set the post Aguments and Query accordingly
0d28b7c10758 First commit
ymh
parents:
diff changeset
    38
		$count = 0;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    39
?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    40
		<div class="post-slider clearfix">
0d28b7c10758 First commit
ymh
parents:
diff changeset
    41
            <h2><?php echo $widget_title; ?></h2>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    42
            <ul>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    43
				<?php while ($ocmx_featured->have_posts()) : $ocmx_featured->the_post();
0d28b7c10758 First commit
ymh
parents:
diff changeset
    44
						setup_postdata($post);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    45
						$count++;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    46
						if ($count == 5) : 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    47
						$count = 1; ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    48
							</ul>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    49
                            	</div>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    50
                                <div class="post-slider clearfix">
0d28b7c10758 First commit
ymh
parents:
diff changeset
    51
							<ul>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    52
				<?php endif;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    53
						// Fetch the PermaLink, Thumbnail and Video Metas
0d28b7c10758 First commit
ymh
parents:
diff changeset
    54
						$this_post = get_post($ocmx_featured->ID);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    55
						$get_post_video = get_post_meta($this_post->ID, "main_video", true);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    56
						$get_thumbnail = get_post_meta($this_post->ID, "other_media", true);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    57
						$get_video_thumbnail = get_post_meta($this_post->ID, "video_thumbnail", true);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    58
						$link = get_permalink($this_post->ID);
0d28b7c10758 First commit
ymh
parents:
diff changeset
    59
						// Set our category
0d28b7c10758 First commit
ymh
parents:
diff changeset
    60
						$category = get_the_category(); 			
0d28b7c10758 First commit
ymh
parents:
diff changeset
    61
						// If we haven't used our custom image input, search for the first image in the post
0d28b7c10758 First commit
ymh
parents:
diff changeset
    62
						$post_image = "";
0d28b7c10758 First commit
ymh
parents:
diff changeset
    63
						if($get_thumbnail == "" && (get_option("ocmx_auto_home_images") && get_option("ocmx_auto_home_images") !== "no")) :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    64
							$post_image =  fetch_post_image($ocmx_featured->ID, "300", "600");
0d28b7c10758 First commit
ymh
parents:
diff changeset
    65
						endif;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    66
				?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    67
                    <li<?php if($count == 4) : echo " class=\"last\""; endif;?>>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    68
                        <div class="container-header-light-normal"><span></span></div>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    69
                        <div class="container-light">
0d28b7c10758 First commit
ymh
parents:
diff changeset
    70
							<div class="thumbnail">
0d28b7c10758 First commit
ymh
parents:
diff changeset
    71
								<?php if ($get_video_thumbnail !== "") : 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    72
									/* Display Custom Video Image */
0d28b7c10758 First commit
ymh
parents:
diff changeset
    73
								?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    74
									<img src="<?php bloginfo('template_directory'); ?>/functions/timthumb.php?src=<?php echo $get_video_thumbnail ?>&amp;h=&amp;w=300&amp;zc=1" alt="<?php the_title(); ?>" />
0d28b7c10758 First commit
ymh
parents:
diff changeset
    75
								<?php elseif ($post_image !== "") :
0d28b7c10758 First commit
ymh
parents:
diff changeset
    76
									/* Display Post Image */
0d28b7c10758 First commit
ymh
parents:
diff changeset
    77
									echo $post_image;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    78
								elseif ($get_thumbnail !== "") : 
0d28b7c10758 First commit
ymh
parents:
diff changeset
    79
									/* Display Custom Post Image */
0d28b7c10758 First commit
ymh
parents:
diff changeset
    80
								?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    81
									<img src="<?php bloginfo('template_directory'); ?>/functions/timthumb.php?src=<?php echo $get_thumbnail ?>&amp;h=&amp;w=300&amp;zc=1" alt="<?php the_title(); ?>" />
0d28b7c10758 First commit
ymh
parents:
diff changeset
    82
								<?php endif; ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    83
							</div>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    84
                        </div>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    85
                        <div class="container-footer-light-normal"><span></span></div>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    86
                        <h3><a href="<?php echo $link; ?>"><?php echo $this_post->post_title; ?></a></h3>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    87
                    </li>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    88
                <?php endwhile; ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    89
            </ul>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    90
        </div>
0d28b7c10758 First commit
ymh
parents:
diff changeset
    91
<?php
0d28b7c10758 First commit
ymh
parents:
diff changeset
    92
    }
0d28b7c10758 First commit
ymh
parents:
diff changeset
    93
0d28b7c10758 First commit
ymh
parents:
diff changeset
    94
    /** @see WP_Widget::update */
0d28b7c10758 First commit
ymh
parents:
diff changeset
    95
    function update($new_instance, $old_instance) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
    96
        return $new_instance;
0d28b7c10758 First commit
ymh
parents:
diff changeset
    97
    }
0d28b7c10758 First commit
ymh
parents:
diff changeset
    98
0d28b7c10758 First commit
ymh
parents:
diff changeset
    99
    /** @see WP_Widget::form */
0d28b7c10758 First commit
ymh
parents:
diff changeset
   100
    function form($instance) {
0d28b7c10758 First commit
ymh
parents:
diff changeset
   101
        $widget_title = 	esc_attr($instance["widget_title"]);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   102
        $post_category = 	esc_attr($instance["post_category"]);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   103
        $post_count = 		esc_attr($instance["post_count"]);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   104
		$post_offset =  	esc_attr($instance["post_offset"]);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   105
		
0d28b7c10758 First commit
ymh
parents:
diff changeset
   106
        ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   107
        	<p>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   108
            	<label for="<?php echo $this->get_field_id('widget_title'); ?>">Title
0d28b7c10758 First commit
ymh
parents:
diff changeset
   109
                <input type="text" class="widefat" id="<?php echo $this->get_field_id('widget_title'); ?>" name="<?php echo $this->get_field_name('widget_title'); ?>" value="<?php echo $widget_title; ?>">
0d28b7c10758 First commit
ymh
parents:
diff changeset
   110
			</p>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   111
            <p>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   112
            	<label for="<?php echo $this->get_field_id('post_count'); ?>">Post Count
0d28b7c10758 First commit
ymh
parents:
diff changeset
   113
                <select size="1" class="widefat" id="<?php echo $this->get_field_id('post_count'); ?>" name="<?php echo $this->get_field_name('post_count'); ?>">
0d28b7c10758 First commit
ymh
parents:
diff changeset
   114
                	<?php for($i = 4; $i < 25; $i=$i+4) : ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   115
	                    <option <?php if($post_count == $i) : ?>selected="selected"<?php endif; ?> value="<?php echo $i; ?>"><?php echo $i; ?></option>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   116
                    <?php endfor; ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   117
                </select>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   118
			</p>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   119
            <p>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   120
            	<label for="<?php echo $this->get_field_id('post_offset'); ?>">Post Offset
0d28b7c10758 First commit
ymh
parents:
diff changeset
   121
                <select size="1" class="widefat" id="<?php echo $this->get_field_id('post_offset'); ?>" name="<?php echo $this->get_field_name('post_offset'); ?>">
0d28b7c10758 First commit
ymh
parents:
diff changeset
   122
	                    <option <?php if($post_offset == 0) : ?>selected="selected"<?php endif; ?> value="0">Latest Post</option>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   123
                	<?php for($i = 1; $i <= 8; $i++) : ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   124
	                    <option <?php if($post_offset == $i) : ?>selected="selected"<?php endif; ?> value="<?php echo $i; ?>"><?php echo $i; ?> Posts Old</option>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   125
                    <?php endfor; ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   126
                </select>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   127
			</p>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   128
        
0d28b7c10758 First commit
ymh
parents:
diff changeset
   129
            <p><label for="<?php echo $this->get_field_id('post_category'); ?>">Category
0d28b7c10758 First commit
ymh
parents:
diff changeset
   130
               <select size="1" class="widefat" id="<?php echo $this->get_field_id("post_category"); ?>" name="<?php echo $this->get_field_name("post_category"); ?>">
0d28b7c10758 First commit
ymh
parents:
diff changeset
   131
                    <option <?php if($post_count == 0){echo "selected=\"selected\"";} ?> value="0">All</option>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   132
                    <?php
0d28b7c10758 First commit
ymh
parents:
diff changeset
   133
							$category_args = array('hide_empty' => false);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   134
                            $option_loop = get_categories($category_args);
0d28b7c10758 First commit
ymh
parents:
diff changeset
   135
                            foreach($option_loop as $option_label => $value)
0d28b7c10758 First commit
ymh
parents:
diff changeset
   136
                                { 	
0d28b7c10758 First commit
ymh
parents:
diff changeset
   137
                                    // Set the $value and $label for the options
0d28b7c10758 First commit
ymh
parents:
diff changeset
   138
                                    $use_value =  $value->slug;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   139
                                    $label =  $value->cat_name;
0d28b7c10758 First commit
ymh
parents:
diff changeset
   140
                                    //If this option == the value we set above, select it
0d28b7c10758 First commit
ymh
parents:
diff changeset
   141
                                    if($use_value == $post_category)
0d28b7c10758 First commit
ymh
parents:
diff changeset
   142
                                        {$selected = " selected='selected' ";}
0d28b7c10758 First commit
ymh
parents:
diff changeset
   143
                                    else
0d28b7c10758 First commit
ymh
parents:
diff changeset
   144
                                        {$selected = " ";}
0d28b7c10758 First commit
ymh
parents:
diff changeset
   145
                    ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   146
                                    <option <?php echo $selected; ?> value="<?php echo $use_value; ?>"><?php echo $label; ?></option>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   147
                    <?php 
0d28b7c10758 First commit
ymh
parents:
diff changeset
   148
                                }
0d28b7c10758 First commit
ymh
parents:
diff changeset
   149
                    ?>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   150
                </select>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   151
			</p>
0d28b7c10758 First commit
ymh
parents:
diff changeset
   152
<?php 
0d28b7c10758 First commit
ymh
parents:
diff changeset
   153
	} // form
0d28b7c10758 First commit
ymh
parents:
diff changeset
   154
0d28b7c10758 First commit
ymh
parents:
diff changeset
   155
}// class
0d28b7c10758 First commit
ymh
parents:
diff changeset
   156
0d28b7c10758 First commit
ymh
parents:
diff changeset
   157
//This sample widget can then be registered in the widgets_init hook:
0d28b7c10758 First commit
ymh
parents:
diff changeset
   158
0d28b7c10758 First commit
ymh
parents:
diff changeset
   159
// register FooWidget widget
0d28b7c10758 First commit
ymh
parents:
diff changeset
   160
add_action('widgets_init', create_function('', 'return register_widget("four_block_widget");'));
0d28b7c10758 First commit
ymh
parents:
diff changeset
   161
0d28b7c10758 First commit
ymh
parents:
diff changeset
   162
?>