PluginProbe
Statify Widget / 1.3.0
Statify Widget v1.3.0
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.0, at statify-widget.php

252 lines 9.7 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.0
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 define('DEFAULT_SUFFIX', '');
18
19 class StatifyWidget extends WP_Widget {
20
21 /*
22 * Register StatifyWidget to Wordpress
23 */
24 function __construct() {
25 $widget_ops = array('classname' => 'statify-widget', 'description' => __('Shows the most popular content. Based on Statify Plugin.','statify-widget'));
26 parent::__construct(
27 'StatifyWidget',
28 __('Statify Widget', 'plugin_name'),
29 $widget_ops
30 );
31
32 add_shortcode( "statify-count", "Statify_Posts::statify_count_shortcode");
33 add_shortcode( "statify-count-sum", "Statify_Posts::statify_count_sum_shortcode");
34 }
35
36 /*
37 * Generating a from for settings
38 */
39 function form($instance) {
40 wp_enqueue_script( 'statify-widget-js', plugins_url( '/js/statify_widget.js', __FILE__ ));
41
42 $instance = wp_parse_args( (array) $instance, array(
43 'title' => '',
44 'amount' => DEFAULT_AMOUNT,
45 'post_type' => DEFAULT_POST_TYPE,
46 'interval' => DEFAULT_INTERVAL,
47 'show_visits' => 0,
48 'list_style_type' => "ol",
49 'suffix' => __('%VIEWS% views','statify-widget'),
50 'post_category' => 0) );
51
52 $title = $instance['title'];
53 $amount = $instance['amount'];
54 $post_type = $instance['post_type'];
55 $interval = $instance['interval'];
56 $show_visits = $instance['show_visits'];
57 $suffix = $instance['suffix'];
58 $post_category = $instance['post_category'];
59 ?>
60 <p>
61 <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Widget title:','statify-widget'); ?>
62 <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); ?>" />
63 </label>
64 </p>
65 <p class="post_select">
66 <label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e( 'Post type:','statify-widget'); ?>
67 <select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>">
68 <option value="postpage"><?php _e( 'posts + pages','statify-widget'); ?></option>
69 <?php
70 $post_types = get_post_types( array('public'=>true, 'show_ui'=>true), 'objects' );
71 foreach ( $post_types as $type ) {
72 echo '<option value="'. esc_attr($type->name) . '" '. selected( $post_type, $type->name, false ) .'>' . esc_attr($type->labels->name) . '</option>';
73 }
74 ?>
75 </select>
76 </p>
77 <p class="category_select" style="display:none;">
78 <label for="<?php echo $this->get_field_id('post_category'); ?>"><?php _e( 'Post category:','statify-widget'); ?>
79 <select class="widefat" id="<?php echo $this->get_field_id('post_category'); ?>" name="<?php echo $this->get_field_name('post_category'); ?>">
80 <option value="0">Alle Kategorien</option>
81 <?php
82 $post_categories = get_categories( array('hide_empty'=>0 ),'objects' );
83 foreach ( $post_categories as $type ) {
84 echo '<option value="'. esc_attr($type->cat_ID) . '" '. selected( $post_category, $type->cat_ID, false ) .'>' . esc_attr($type->name) . '</option>';
85 }
86 ?>
87 </select>
88 </p>
89 <p>
90 <label for="<?php echo $this->get_field_id('interval'); ?>"><?php _e( 'Last ','statify-widget'); ?>
91 <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); ?>" />
92 </label><?php _e( ' days. (If enough stats exists)','statify-widget'); ?>
93 <br /><small><?php _e( '0 days = show all items','statify-widget'); ?></small>
94 </p>
95 <p>
96 <label for="<?php echo $this->get_field_id('amount'); ?>"><?php _e( 'Amounts:','statify-widget'); ?>
97 <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); ?>" />
98 </label>
99 <label for="<?php echo $this->get_field_id('show_visits'); ?>">
100 <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); ?>>
101 <?php _e( 'Show view counter?','statify-widget'); ?></label>
102 </p>
103 <p>
104 <label for="<?php echo $this->get_field_id('suffix'); ?>"><?php _e( 'Custom text:','statify-widget'); ?>
105 <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); ?>" />
106 </label>
107 <small><?php _e( '%VIEWS% = amount of views','statify-widget'); ?></small>
108 </p>
109 <?php
110 }
111
112 /*
113 * Override old instance with new instance.
114 */
115 function update($new_instance, $old_instance) {
116 $instance = array();
117 $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
118 $instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? $new_instance['post_type'] : DEFAULT_POST_TYPE;
119 if($new_instance['interval'] != $old_instance['interval']) {
120 delete_transient('statify_targets_'.$old_instance['interval']);
121 }
122 $instance['interval'] = ( ! empty( $new_instance['interval'] ) ) ? $new_instance['interval'] : DEFAULT_INTERVAL;
123 $instance['amount'] = ( ! empty( $new_instance['amount'] ) ) ? sanitize_text_field( $new_instance['amount'] ) : DEFAULT_AMOUNT;
124 $instance['show_visits'] = ( ! empty( $new_instance['show_visits'] ) ) ? $new_instance['show_visits'] : 0;
125 $instance['suffix'] = ( ! empty( $new_instance['suffix'] ) ) ? sanitize_text_field( $new_instance['suffix'] ) : DEFAULT_SUFFIX;
126 $instance['post_category'] = ( ! empty( $new_instance['post_category'] ) ) ? sanitize_text_field( $new_instance['post_category'] ) : 0;
127 return $instance;
128 }
129
130 /*
131 * Print the widget
132 */
133 function widget($args, $instance) {
134 extract($args, EXTR_SKIP);
135
136 echo $before_widget;
137
138 $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
139 $amount = empty($instance['amount']) ? DEFAULT_AMOUNT : $instance['amount'];
140 $post_type = empty($instance['post_type']) ? DEFAULT_POST_TYPE : $instance['post_type'];
141 $interval = empty($instance['interval']) ? DEFAULT_INTERVAL : $instance['interval'];
142 $show_visits = empty($instance['show_visits']) ? 0 : 1;
143 $suffix = empty($instance['suffix']) ? DEFAULT_SUFFIX : $instance['suffix'];
144 $post_category = empty($instance['post_category']) ? 0 : $instance['post_category'];
145
146 if (!empty($title)) echo $before_title . $title . $after_title;
147
148 $popular_content = Statify_Posts::get_posts($post_type, $post_category, $amount, $interval);
149 if (empty( $popular_content )) {
150 echo "<p><?php __( 'There are no posts yet.','statify-widget'); ?></p>";
151 } else {
152 echo '<ol class="statify-widget-list">'."\n";
153 foreach($popular_content as $post) {
154 $_suffix = ($show_visits) ? ' <span>' . str_replace(__("%VIEWS%","statify-widget"), intval($post['visits']), $suffix) . '</span>' : '';
155 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";
156 }
157 echo "</ol>"."\n";
158 }
159
160 echo $after_widget;
161 }
162
163 /*
164 * Return the statify widget class for the hook
165 * @since 1.1.8
166 */
167
168 function statify_widget_class_callback( $className ) {
169 $widgetClass = $className;
170 }
171
172 }
173
174 /*
175 * Print error message in admin interface
176 */
177 function showErrorMessages() {
178 $html = '<div class="error"><p>';
179 $html .= __( 'Please install <a target="_blank" href="http://wordpress.org/plugins/statify/">Statify</a> plugin first.','statify-widget');
180 $html .= '</p></div>';
181 echo $html;
182 }
183
184 /*
185 * Check if Statify is acitivated
186 */
187 function requires_statify_plugin() {
188 $plugin_bcd_plugin = 'statify/statify.php';
189 $plugin = plugin_basename( __FILE__ );
190 $plugin_data = get_plugin_data( __FILE__, false );
191
192 if ( !is_plugin_active( $plugin_bcd_plugin) ) {
193 deactivate_plugins ( $plugin );
194 add_action('admin_notices', 'showErrorMessages');
195 }
196 }
197
198 /**
199 * Register Statify-Widget
200 *
201 * @since 1.1.7
202 */
203 function register_statify_widget() {
204 register_widget( 'StatifyWidget' );
205 }
206 add_action( 'widgets_init', 'register_statify_widget' );
207 add_action( 'admin_init', 'requires_statify_plugin' );
208
209 /*
210 * Get statify count for post id in themes or shortcode
211 * @since 1.1.6
212 */
213
214 function statify_count($post_id) {
215 echo Statify_Posts::statify_count($post_id);
216 }
217
218 /*
219 * Echo statify count for post id in themes or shortcode
220 * @since 1.1.9
221 */
222 function get_statify_count($post_id) {
223 return Statify_Posts::statify_count($post_id);
224 }
225
226 /*
227 * Return sum of all visits for the site
228 * @since 1.2
229 */
230 function get_statify_count_sum() {
231 return Statify_Posts::statify_count_sum();
232 }
233
234 /*
235 * Echo sum of all visits for the site
236 * @since 1.2
237 */
238 function statify_count_sum() {
239 echo Statify_Posts::statify_count_sum();
240 }
241
242 /**
243 * Load plugin textdomain.
244 *
245 * @since 1.1.9
246 */
247 function statify_widget_load_textdomain() {
248 load_plugin_textdomain( 'statify-widget', false, basename( dirname( __FILE__ ) ) . '/languages' );
249 }
250
251 add_action( 'init', 'statify_widget_load_textdomain' );
252 ?>