PluginProbe
Statify Widget / 1.2.1
Statify Widget v1.2.1
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.1, at statify-widget.php

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