Make WordPress Themes

source: hueman/3.1.3/functions/widgets/alx-posts.php

Last change on this file was 60080, checked in by themedropbox, 9 years ago

New version of Hueman - 3.1.3

File size: 8.5 KB
Line 
1<?php
2/*
3        AlxPosts Widget
4
5        License: GNU General Public License v3.0
6        License URI: http://www.gnu.org/licenses/gpl-3.0.html
7
8        Copyright: (c) 2013 Alexander "Alx" AgnarsCopyright: (c) 2013-2015 Alexander "Alx" Agnarson, 2015 Nicolas GUILLAUME (nikeo)
9
10                @package AlxPosts
11                @version 1.0
12*/
13
14class AlxPosts extends WP_Widget {
15
16/*  Constructor
17/* ------------------------------------ */
18        function __construct() {
19                parent::__construct(
20      'alxposts',
21      __('Hueman Posts', 'hueman'),
22      array(
23        'description' => __('Display posts from a category', 'hueman'),
24        'classname' => 'widget_hu_posts'
25      )
26    );
27        }
28
29  public function hu_get_defaults() {
30    return array(
31      'title'       => '',
32      // Posts
33      'posts_thumb'     => 1,
34      'posts_category'  => 1,
35      'posts_date'    => 1,
36      'posts_num'     => '4',
37      'posts_cat_id'    => '0',
38      'posts_orderby'   => 'date',
39      'posts_time'    => '0',
40    );
41  }
42
43
44  /*  Widget
45  /* ------------------------------------ */
46        public function widget($args, $instance) {
47                extract( $args );
48
49    $defaults = $this -> hu_get_defaults();
50
51    $instance = wp_parse_args( (array) $instance, $defaults );
52
53                $title = apply_filters('widget_title',$instance['title']);
54                $output = $before_widget."\n";
55                if($title)
56                        $output .= $before_title.$title.$after_title;
57                ob_start();
58
59?>
60
61        <?php
62                $posts = new WP_Query( array(
63                        'post_type'                             => array( 'post' ),
64                        'showposts'                             => $instance['posts_num'],
65                        'cat'                                   => $instance['posts_cat_id'],
66                        'ignore_sticky_posts'   => true,
67                        'orderby'                               => $instance['posts_orderby'],
68                        'order'                                 => 'dsc',
69                        'date_query' => array(
70                                array(
71                                        'after' => $instance['posts_time'],
72                                ),
73                        ),
74                ) );
75        ?>
76
77        <ul class="alx-posts group <?php if($instance['posts_thumb']) { echo 'thumbs-enabled'; } ?>">
78                <?php while ($posts->have_posts()): $posts->the_post(); ?>
79                <li>
80
81                        <?php if($instance['posts_thumb']) { // Thumbnails enabled? ?>
82                        <div class="post-item-thumbnail">
83                                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
84                                        <?php if ( has_post_thumbnail() ): ?>
85                                                <?php the_post_thumbnail('thumb-medium'); ?>
86                                        <?php else: ?>
87                                                <?php hu_print_placeholder_thumb(); ?>
88                                        <?php endif; ?>
89                                        <?php if ( has_post_format('video') && !is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-play"></i></span>'; ?>
90                                        <?php if ( has_post_format('audio') && !is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-volume-up"></i></span>'; ?>
91                                        <?php if ( is_sticky() ) echo'<span class="thumb-icon small"><i class="fa fa-star"></i></span>'; ?>
92                                </a>
93                        </div>
94                        <?php } ?>
95
96                        <div class="post-item-inner group">
97                                <?php if($instance['posts_category']) { ?><p class="post-item-category"><?php the_category(' / '); ?></p><?php } ?>
98                                <p class="post-item-title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
99                                <?php if($instance['posts_date']) { ?><p class="post-item-date"><?php the_time('j M, Y'); ?></p><?php } ?>
100                        </div>
101
102                </li>
103                <?php endwhile; ?>
104                <?php wp_reset_postdata(); ?>
105        </ul><!--/.alx-posts-->
106
107<?php
108                $output .= ob_get_clean();
109                $output .= $after_widget."\n";
110                echo $output;
111        }
112
113/*  Widget update
114/* ------------------------------------ */
115        public function update($new,$old) {
116                $instance = $old;
117                $instance['title'] = strip_tags($new['title']);
118        // Posts
119                $instance['posts_thumb'] = $new['posts_thumb']?1:0;
120                $instance['posts_category'] = $new['posts_category']?1:0;
121                $instance['posts_date'] = $new['posts_date']?1:0;
122                $instance['posts_num'] = strip_tags($new['posts_num']);
123                $instance['posts_cat_id'] = strip_tags($new['posts_cat_id']);
124                $instance['posts_orderby'] = strip_tags($new['posts_orderby']);
125                $instance['posts_time'] = strip_tags($new['posts_time']);
126                return $instance;
127        }
128
129/*  Widget form
130/* ------------------------------------ */
131        public function form($instance) {
132                // Default widget settings
133                $defaults = $this -> hu_get_defaults();
134                $instance = wp_parse_args( (array) $instance, $defaults );
135?>
136
137        <style>
138        .widget .widget-inside .alx-options-posts .postform { width: 100%; }
139        .widget .widget-inside .alx-options-posts p { margin: 3px 0; }
140        .widget .widget-inside .alx-options-posts hr { margin: 20px 0 10px; }
141        .widget .widget-inside .alx-options-posts h4 { margin-bottom: 10px; }
142        </style>
143
144        <div class="alx-options-posts">
145                <p>
146                        <label for="<?php echo esc_attr( $this->get_field_id('title') ); ?>">Title:</label>
147                        <input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_attr( $instance["title"] ); ?>" />
148                </p>
149
150                <h4>List Posts</h4>
151
152                <p>
153                        <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('posts_thumb') ); ?>" name="<?php echo esc_attr( $this->get_field_name('posts_thumb') ); ?>" <?php checked( (bool) $instance["posts_thumb"], true ); ?>>
154                        <label for="<?php echo esc_attr( $this->get_field_id('posts_thumb') ); ?>">Show thumbnails</label>
155                </p>
156                <p>
157                        <label style="width: 55%; display: inline-block;" for="<?php echo esc_attr( $this->get_field_id("posts_num") ); ?>">Items to show</label>
158                        <input style="width:20%;" id="<?php echo esc_attr( $this->get_field_id("posts_num") ); ?>" name="<?php echo esc_attr( $this->get_field_name("posts_num") ); ?>" type="text" value="<?php echo absint($instance["posts_num"]); ?>" size='3' />
159                </p>
160                <p>
161                        <label style="width: 100%; display: inline-block;" for="<?php echo esc_attr( $this->get_field_id("posts_cat_id") ); ?>">Category:</label>
162                        <?php wp_dropdown_categories( array( 'name' => $this->get_field_name("posts_cat_id"), 'selected' => $instance["posts_cat_id"], 'show_option_all' => 'All', 'show_count' => true ) ); ?>
163                </p>
164                <p style="padding-top: 0.3em;">
165                        <label style="width: 100%; display: inline-block;" for="<?php echo esc_attr( $this->get_field_id("posts_orderby") ); ?>">Order by:</label>
166                        <select style="width: 100%;" id="<?php echo esc_attr( $this->get_field_id("posts_orderby") ); ?>" name="<?php echo esc_attr( $this->get_field_name("posts_orderby") ); ?>">
167                          <option value="date"<?php selected( $instance["posts_orderby"], "date" ); ?>>Most recent</option>
168                          <option value="comment_count"<?php selected( $instance["posts_orderby"], "comment_count" ); ?>>Most commented</option>
169                          <option value="rand"<?php selected( $instance["posts_orderby"], "rand" ); ?>>Random</option>
170                        </select>
171                </p>
172                <p style="padding-top: 0.3em;">
173                        <label style="width: 100%; display: inline-block;" for="<?php echo esc_attr( $this->get_field_id("posts_time") ); ?>">Posts from:</label>
174                        <select style="width: 100%;" id="<?php echo esc_attr( $this->get_field_id("posts_time") ); ?>" name="<?php echo esc_attr( $this->get_field_name("posts_time") ); ?>">
175                          <option value="0"<?php selected( $instance["posts_time"], "0" ); ?>>All time</option>
176                          <option value="1 year ago"<?php selected( $instance["posts_time"], "1 year ago" ); ?>>This year</option>
177                          <option value="1 month ago"<?php selected( $instance["posts_time"], "1 month ago" ); ?>>This month</option>
178                          <option value="1 week ago"<?php selected( $instance["posts_time"], "1 week ago" ); ?>>This week</option>
179                          <option value="1 day ago"<?php selected( $instance["posts_time"], "1 day ago" ); ?>>Past 24 hours</option>
180                        </select>
181                </p>
182
183                <hr>
184                <h4>Post Info</h4>
185
186                <p>
187                        <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('posts_category') ); ?>" name="<?php echo esc_attr( $this->get_field_name('posts_category') ); ?>" <?php checked( (bool) $instance["posts_category"], true ); ?>>
188                        <label for="<?php echo esc_attr( $this->get_field_id('posts_category') ); ?>">Show categories</label>
189                </p>
190                <p>
191                        <input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('posts_date') ); ?>" name="<?php echo esc_attr( $this->get_field_name('posts_date') ); ?>" <?php checked( (bool) $instance["posts_date"], true ); ?>>
192                        <label for="<?php echo esc_attr( $this->get_field_id('posts_date') ); ?>">Show dates</label>
193                </p>
194
195                <hr>
196
197        </div>
198<?php
199
200}
201
202}
203
204/*  Register widget
205/* ------------------------------------ */
206if ( ! function_exists( 'hu_register_widget_posts' ) ) {
207
208        function hu_register_widget_posts() {
209                register_widget( 'AlxPosts' );
210        }
211
212}
213add_action( 'widgets_init', 'hu_register_widget_posts' );
Note: See TracBrowser for help on using the repository browser.