PluginProbe
Statify Widget / 1.2.7
Statify Widget v1.2.7
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.2.7, at statify-widget.php

251 lines 9.6 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.2.7
10 */
11
12 require( 'Statify_Posts.class.php' );
13
14 define('DEFAULT_AMOUNT', 5);
15 define('DEFAULT_POST_TYPE','post');
16 define('DEFAULT_INTERVAL', 0);
17
18 class StatifyWidget extends WP_Widget {
19
20 /*
21 * Register StatifyWidget to Wordpress
22 */
23 function __construct() {
24 $widget_ops = array('classname' => 'statify-widget', 'description' => __('Shows the most popular content. Based on Statify Plugin.','statify-widget'));
25 parent::__construct(
26 'StatifyWidget',
27 __('Statify Widget', 'plugin_name'),
28 $widget_ops
29 );
30
31 add_shortcode( "statify-count", "Statify_Posts::statify_count_shortcode");
32 add_shortcode( "statify-count-sum", "Statify_Posts::statify_count_sum_shortcode");
33 }
34
35 /*
36 * Generating a from for settings
37 */
38 function form($instance) {
39 wp_enqueue_script( 'statify-widget-js', plugins_url( '/js/statify_widget.js', __FILE__ ));
40
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 <label for="<?php echo $this->get_field_id('show_visits'); ?>">
99 <input class="checkbox widget-description" style="margin-left:15px;" 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); ?>>
100 <?php _e( 'Show view counter?','statify-widget'); ?></label>
101 </p>
102 <p>
103 <label for="<?php echo $this->get_field_id('suffix'); ?>"><?php _e( 'Custom text:','statify-widget'); ?>
104 <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); ?>" />
105 </label>
106 <small><?php _e( '%VIEWS% = amount of views','statify-widget'); ?></small>
107 </p>
108 <?php
109 }
110
111 /*
112 * Override old instance with new instance.
113 */
114 function update($new_instance, $old_instance) {
115 $instance = array();
116 $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
117 $instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? $new_instance['post_type'] : DEFAULT_POST_TYPE;
118 if($new_instance['interval'] != $old_instance['interval']) {
119 delete_transient('statify_targets_'.$old_instance['interval']);
120 }
121 $instance['interval'] = ( ! empty( $new_instance['interval'] ) ) ? $new_instance['interval'] : DEFAULT_INTERVAL;
122 $instance['amount'] = ( ! empty( $new_instance['amount'] ) ) ? sanitize_text_field( $new_instance['amount'] ) : DEFAULT_AMOUNT;
123 $instance['show_visits'] = ( ! empty( $new_instance['show_visits'] ) ) ? $new_instance['show_visits'] : 0;
124 $instance['suffix'] = ( ! empty( $new_instance['suffix'] ) ) ? sanitize_text_field( $new_instance['suffix'] ) : DEFAULT_SUFFIX;
125 $instance['post_category'] = ( ! empty( $new_instance['post_category'] ) ) ? sanitize_text_field( $new_instance['post_category'] ) : 0;
126 return $instance;
127 }
128
129 /*
130 * Print the widget
131 */
132 function widget($args, $instance) {
133 extract($args, EXTR_SKIP);
134
135 echo $before_widget;
136
137 $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
138 $amount = empty($instance['amount']) ? DEFAULT_AMOUNT : $instance['amount'];
139 $post_type = empty($instance['post_type']) ? DEFAULT_POST_TYPE : $instance['post_type'];
140 $interval = empty($instance['interval']) ? DEFAULT_INTERVAL : $instance['interval'];
141 $show_visits = empty($instance['show_visits']) ? 0 : 1;
142 $suffix = empty($instance['suffix']) ? DEFAULT_SUFFIX : $instance['suffix'];
143 $post_category = empty($instance['post_category']) ? 0 : $instance['post_category'];
144
145 if (!empty($title)) echo $before_title . $title . $after_title;
146
147 $popular_content = Statify_Posts::get_posts($post_type, $post_category, $amount, $interval);
148 if (empty( $popular_content )) {
149 echo "<p><?php __( 'There are no posts yet.','statify-widget'); ?></p>";
150 } else {
151 echo '<ol class="statify-widget-list">'."\n";
152 foreach($popular_content as $post) {
153 $_suffix = ($show_visits) ? ' <span>' . str_replace(__("%VIEWS%","statify-widget"), intval($post['visits']), $suffix) . '</span>' : '';
154 echo '<li class="statify-widget-element"><a class="statify-widget-link" title="' . esc_html($post['title']) . '" href="' . esc_url($post['url']) . '">' . esc_html($post['title']) . '</a>'. $_suffix .'</li>'."\n";
155 }
156 echo "</ol>"."\n";
157 }
158
159 echo $after_widget;
160 }
161
162 /*
163 * Return the statify widget class for the hook
164 * @since 1.1.8
165 */
166
167 function statify_widget_class_callback( $className ) {
168 $widgetClass = $className;
169 }
170
171 }
172
173 /*
174 * Print error message in admin interface
175 */
176 function showErrorMessages() {
177 $html = '<div class="error"><p>';
178 $html .= __( 'Please install <a target="_blank" href="http://wordpress.org/plugins/statify/">Statify</a> plugin first.','statify-widget');
179 $html .= '</p></div>';
180 echo $html;
181 }
182
183 /*
184 * Check if Statify is acitivated
185 */
186 function requires_statify_plugin() {
187 $plugin_bcd_plugin = 'statify/statify.php';
188 $plugin = plugin_basename( __FILE__ );
189 $plugin_data = get_plugin_data( __FILE__, false );
190
191 if ( !is_plugin_active( $plugin_bcd_plugin) ) {
192 deactivate_plugins ( $plugin );
193 add_action('admin_notices', 'showErrorMessages');
194 }
195 }
196
197 /**
198 * Register Statify-Widget
199 *
200 * @since 1.1.7
201 */
202 function register_statify_widget() {
203 register_widget( 'StatifyWidget' );
204 }
205 add_action( 'widgets_init', 'register_statify_widget' );
206 add_action( 'admin_init', 'requires_statify_plugin' );
207
208 /*
209 * Get statify count for post id in themes or shortcode
210 * @since 1.1.6
211 */
212
213 function statify_count($post_id) {
214 echo Statify_Posts::statify_count($post_id);
215 }
216
217 /*
218 * Echo statify count for post id in themes or shortcode
219 * @since 1.1.9
220 */
221 function get_statify_count($post_id) {
222 return Statify_Posts::statify_count($post_id);
223 }
224
225 /*
226 * Return sum of all visits for the site
227 * @since 1.2
228 */
229 function get_statify_count_sum() {
230 return Statify_Posts::statify_count_sum();
231 }
232
233 /*
234 * Echo sum of all visits for the site
235 * @since 1.2
236 */
237 function statify_count_sum() {
238 echo Statify_Posts::statify_count_sum();
239 }
240
241 /**
242 * Load plugin textdomain.
243 *
244 * @since 1.1.9
245 */
246 function statify_widget_load_textdomain() {
247 load_plugin_textdomain( 'statify-widget', false, basename( dirname( __FILE__ ) ) . '/languages' );
248 }
249
250 add_action( 'init', 'statify_widget_load_textdomain' );
251 ?>