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

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