PluginProbe
Statify Widget / 1.3.8
Statify Widget v1.3.8
trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 All 39 releases
statify-widget / statify-widget.php

statify-widget.php in Statify Widget 1.3.8, at statify-widget.php

285 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Statify Widget: Popular Posts
4 Description: Widget for popular pages, posts and other custom content types – based on privacy conform statistic plugin "Statify" from pluginkollektiv.
5 Text Domain: statify-widget
6 Author: Finn Dohrn
7 Author URI: http://www.bit01.de/
8 Plugin URI: http://www.bit01.de/blog/statify-widget/
9 Version: 1.3.8
10 */
11
12 require( 'Statify_Post.class.php' );
13 require( 'Statify_Posts.class.php' );
14
15 define('DEFAULT_AMOUNT', 5);
16 define('DEFAULT_POST_TYPE','post');
17 define('DEFAULT_INTERVAL', 0);
18 define('DEFAULT_SUFFIX', '');
19
20 class StatifyWidget extends WP_Widget {
21
22 /*
23 * Register StatifyWidget to Wordpress
24 */
25 function __construct() {
26 $widget_ops = array('classname' => 'statify-widget', 'description' => __('Shows the most popular content. Based on Statify Plugin.','statify-widget'));
27 parent::__construct(
28 'StatifyWidget',
29 __('Statify Widget', 'plugin_name'),
30 $widget_ops
31 );
32
33 add_shortcode( "statify-count", "Statify_Posts::statify_count_shortcode");
34 add_shortcode( "statify-count-sum", "Statify_Posts::statify_count_sum_shortcode");
35 }
36
37 /*
38 * Generating a from for settings
39 */
40 function form($instance) {
41 $instance = wp_parse_args( (array) $instance, array(
42 'title' => '',
43 'amount' => DEFAULT_AMOUNT,
44 'post_type' => DEFAULT_POST_TYPE,
45 'interval' => DEFAULT_INTERVAL,
46 'show_visits' => 0,
47 'list_style_type' => "ol",
48 'suffix' => __('%VIEWS% views','statify-widget'),
49 'post_category' => 0) );
50
51 $title = $instance['title'];
52 $amount = $instance['amount'];
53 $post_type = $instance['post_type'];
54 $interval = $instance['interval'];
55 $show_visits = $instance['show_visits'];
56 $suffix = $instance['suffix'];
57 $post_category = $instance['post_category'];
58 ?>
59 <p>
60 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Widget title:','statify-widget'); ?>
61 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
62 </label>
63 </p>
64 <p class="post_select">
65 <label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e( 'Post type:','statify-widget'); ?>
66 <select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>">
67 <option value="postpage"><?php _e( 'posts + pages','statify-widget'); ?></option>
68 <?php
69 $post_types = get_post_types( array('public'=>true, 'show_ui'=>true), 'objects' );
70 foreach ( $post_types as $type ) {
71 echo '<option value="'. esc_attr($type->name) . '" '. selected( $post_type, $type->name, false ) .'>' . esc_attr($type->labels->name) . '</option>';
72 }
73 ?>
74 </select>
75 </p>
76 <p class="category_select" style="display:none;">
77 <label for="<?php echo $this->get_field_id('post_category'); ?>"><?php _e( 'Post category:','statify-widget'); ?>
78 <select class="widefat" id="<?php echo $this->get_field_id('post_category'); ?>" name="<?php echo $this->get_field_name('post_category'); ?>">
79 <option value="0">Alle Kategorien</option>
80 <?php
81 $post_categories = get_categories( array('hide_empty'=>0 ),'objects' );
82 foreach ( $post_categories as $type ) {
83 echo '<option value="'. esc_attr($type->cat_ID) . '" '. selected( $post_category, $type->cat_ID, false ) .'>' . esc_attr($type->name) . '</option>';
84 }
85 ?>
86 </select>
87 </p>
88 <p>
89 <label for="<?php echo $this->get_field_id('interval'); ?>"><?php _e( 'Last ','statify-widget'); ?>
90 <input id="<?php echo $this->get_field_id('interval'); ?>" name="<?php echo $this->get_field_name('interval'); ?>" type="text" size="3" value="<?php echo esc_attr($interval); ?>" />
91 </label><?php _e( ' days. (If enough stats exists)','statify-widget'); ?>
92 <br /><small><?php _e( '0 days = show all items','statify-widget'); ?></small>
93 </p>
94 <p>
95 <label for="<?php echo $this->get_field_id('amount'); ?>"><?php _e( 'Amounts:','statify-widget'); ?>
96 <input id="<?php echo $this->get_field_id('amount'); ?>" name="<?php echo $this->get_field_name('amount'); ?>" type="text" size="3" value="<?php echo esc_attr($amount); ?>" />
97 </label>
98 </p>
99 <p>
100 <label for="<?php echo $this->get_field_id('show_visits'); ?>">
101 <input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id('show_visits'); ?>" name="<?php echo $this->get_field_name('show_visits'); ?>" value="1" <?php checked($show_visits,1); ?>>
102 <?php _e( 'Show view counter?','statify-widget'); ?></label>
103 </p>
104 <p>
105 <label for="<?php echo $this->get_field_id('suffix'); ?>"><?php _e( 'Custom text:','statify-widget'); ?>
106 <input id="<?php echo $this->get_field_id('suffix'); ?>" class="widefat" name="<?php echo $this->get_field_name('suffix'); ?>" type="text" value="<?php echo esc_attr($suffix); ?>" />
107 </label>
108 <small><?php _e( '%VIEWS% = amount of views','statify-widget'); ?></small>
109 </p>
110 <?php
111 }
112
113 /*
114 * Override old instance with new instance.
115 */
116 function update($new_instance, $old_instance) {
117 $instance = array();
118 $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
119 $instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? $new_instance['post_type'] : DEFAULT_POST_TYPE;
120 if(! empty( $new_instance['interval'] ) && $new_instance['interval'] != $old_instance['interval']) {
121 delete_transient('statify_targets_'.$old_instance['interval']);
122 }
123 $instance['interval'] = ( ! empty( $new_instance['interval'] ) ) ? $new_instance['interval'] : DEFAULT_INTERVAL;
124 $instance['amount'] = ( ! empty( $new_instance['amount'] ) ) ? sanitize_text_field( $new_instance['amount'] ) : DEFAULT_AMOUNT;
125 $instance['show_visits'] = ( ! empty( $new_instance['show_visits'] ) ) ? $new_instance['show_visits'] : 0;
126 $instance['suffix'] = ( ! empty( $new_instance['suffix'] ) ) ? sanitize_text_field( $new_instance['suffix'] ) : DEFAULT_SUFFIX;
127 $instance['post_category'] = ( ! empty( $new_instance['post_category'] ) ) ? sanitize_text_field( $new_instance['post_category'] ) : 0;
128 return $instance;
129 }
130
131
132 /*
133 * Post Layout
134 */
135 function statify_widget_template($posts) {
136 ?>
137 <?php if ( empty($posts) ): ?>
138 <p><?php __( 'There are no posts yet.','statify-widget' ) ?></p>
139 <?php else: ?>
140
141 <ol class="statify-widget-list">
142 <?php foreach ($posts as $post): ?>
143 <li class="statify-widget-element">
144 <?php echo do_action( "statify_widget_before_link", $post); ?>
145 <a class='statify-widget-link' title='<?php echo $post->post_title ?>' href='<?php echo $post->post_permalink ?>'><?php echo $post->post_title ?></a> <?php if (isset($post->post_suffix)) echo "<span>". $post->post_suffix ."</span>" ?>
146 <?php echo do_action( "statify_widget_after_link", $post); ?>
147 </li>
148 <?php endforeach; ?>
149 </ol>
150
151 <?php endif; ?>
152 <?php
153 }
154
155 /*
156 * Print the widget
157 */
158 function widget($args, $instance) {
159 extract($args, EXTR_SKIP);
160
161 echo $before_widget;
162
163 $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
164 $amount = empty($instance['amount']) ? DEFAULT_AMOUNT : $instance['amount'];
165 $post_type = empty($instance['post_type']) ? DEFAULT_POST_TYPE : $instance['post_type'];
166 $interval = empty($instance['interval']) ? DEFAULT_INTERVAL : $instance['interval'];
167 $show_visits = empty($instance['show_visits']) ? 0 : 1;
168 $suffix_text = empty($instance['suffix']) ? DEFAULT_SUFFIX : $instance['suffix'];
169 $post_category = empty($instance['post_category']) ? 0 : $instance['post_category'];
170
171 if (!empty($title)) echo $before_title . $title . $after_title;
172
173 $posts = Statify_Posts::get_posts($post_type, $post_category, $amount, $interval);
174
175 // Replace custom suffix text with views and add to WP_Post object
176 if ($show_visits) {
177 foreach($posts as $post) {
178 $post->post_suffix = str_replace(__("%VIEWS%","statify-widget"), intval($post->post_views), $suffix_text);
179 }
180 }
181
182 $this->statify_widget_template($posts);
183
184 echo $after_widget;
185 }
186
187 /*
188 * Return the statify widget class for the hook
189 * @since 1.1.8
190 */
191
192 function statify_widget_class_callback( $className ) {
193 $widgetClass = $className;
194 }
195
196 }
197
198 /*
199 * Print error message in admin interface
200 */
201 function showErrorMessages() {
202 $html = '<div class="error"><p>';
203 $html .= __( 'Please install <a target="_blank" href="http://wordpress.org/plugins/statify/">Statify</a> plugin first.','statify-widget');
204 $html .= '</p></div>';
205 echo $html;
206 }
207
208 /*
209 * Check if Statify is acitivated
210 */
211 function requires_statify_plugin() {
212 $plugin_bcd_plugin = 'statify/statify.php';
213 $plugin = plugin_basename( __FILE__ );
214 $plugin_data = get_plugin_data( __FILE__, false );
215
216 if ( !is_plugin_active( $plugin_bcd_plugin) ) {
217 deactivate_plugins ( $plugin );
218 add_action('admin_notices', 'showErrorMessages');
219 }
220 }
221
222 /**
223 * Register Statify-Widget
224 *
225 * @since 1.1.7
226 */
227 function register_statify_widget() {
228 register_widget( 'StatifyWidget' );
229 }
230 add_action( 'widgets_init', 'register_statify_widget' );
231 add_action( 'admin_init', 'requires_statify_plugin' );
232
233 /*
234 * Get statify count for post id in themes or shortcode
235 * @since 1.1.6
236 * @deprecated 1.3.3
237 */
238 function statify_count($post_id, $days = 0) {
239 echo Statify_Posts::statify_count($post_id, $days);
240 }
241
242 /*
243 * Echo statify count for post id in themes or shortcode
244 * @since 1.1.9
245 */
246 function get_statify_count($post_id, $days = 0) {
247 return Statify_Posts::statify_count($post_id, $days);
248 }
249
250 /*
251 * Return sum of all visits for the site
252 * @since 1.2
253 */
254 function get_statify_count_sum($days = 0) {
255 return Statify_Posts::statify_count_sum($days);
256 }
257
258 /*
259 * Echo sum of all visits for the site
260 * @since 1.2
261 * @deprecated 1.3.3
262 */
263 function statify_count_sum($days = 0) {
264 echo Statify_Posts::statify_count_sum($days);
265 }
266
267 /*
268 * Return all posts for type as array
269 * @since 1.3
270 */
271 function statify_popular_posts($amount = 5, $days = 0, $post_type = 'post', $post_category = 0) {
272 return Statify_Posts::get_post_list($post_type, $post_category, $amount, $days);
273 }
274
275 /**
276 * Load plugin textdomain.
277 *
278 * @since 1.1.9
279 */
280 function statify_widget_load_textdomain() {
281 load_plugin_textdomain( 'statify-widget', false, basename( dirname( __FILE__ ) ) . '/languages' );
282 }
283
284 add_action( 'init', 'statify_widget_load_textdomain' );
285 ?>