Plugin Directory

source: statify-widget/trunk/statify-widget.php

Last change on this file was 3446457, checked in by bitnulleins, 2 months ago

Fix widget update function.

File size: 11.9 KB
Line 
1<?php
2/*
3Plugin Name: Statify Widget: Popular Posts
4Description: Widget for popular pages, posts and other custom content types – based on privacy conform statistic plugin "Statify" from pluginkollektiv.
5Text Domain: statify-widget
6Author: Finn Dohrn
7Author URI: http://www.bit01.de/
8Plugin URI: http://www.bit01.de/blog/statify-widget/
9Version: 1.4.9
10*/
11
12/* Quit */
13defined( 'ABSPATH' ) || exit;
14
15define('STATIFY_WIDGET_DEFAULT_AMOUNT', 5);
16define('STATIFY_WIDGET_DEFAULT_POST_TYPE','post');
17define('STATIFY_WIDGET_DEFAULT_INTERVAL', 0);
18define('STATIFY_WIDGET_DEFAULT_SUFFIX', '');
19define('STATIFY_WIDGET_DEFAULT_TRANSIENT_PREFIX', 'statify_targets_');
20define('STATIFY_WIDGET_DEFAULT_EXPIRATION', 60 * 4);
21
22require( 'Statify_Post.class.php' );
23require( 'Statify_Posts.class.php' );
24
25class StatifyWidget extends WP_Widget {
26
27        /*
28        * Register StatifyWidget to Wordpress
29        */
30        function __construct() {
31                $widget_ops = array('classname' => 'statify-widget', 'description' => __('Shows the most popular content. Based on Statify Plugin.','statify-widget'));
32                parent::__construct(
33                        'StatifyWidget',
34                        __('Statify Widget', 'plugin_name'),
35                        $widget_ops
36                );
37               
38                add_shortcode( "statify-count", "Statify_Posts::statify_count_shortcode");
39                add_shortcode( "statify-count-sum", "Statify_Posts::statify_count_sum_shortcode");
40        }
41
42        /*
43        * Generating a from for settings
44        */
45        function form($instance) {
46                $instance = wp_parse_args( (array) $instance, array(
47                        'title' => '',
48                        'amount' => STATIFY_WIDGET_DEFAULT_AMOUNT,
49                        'post_type' => STATIFY_WIDGET_DEFAULT_POST_TYPE,
50                        'interval' => STATIFY_WIDGET_DEFAULT_INTERVAL,
51                        'show_visits' => 0,
52                        'list_style_type' => "ol",
53                        'suffix' => __("%VIEWS% views",'statify-widget'),
54                        'post_category' => 0) );
55
56        $title = $instance['title'];
57                $amount = $instance['amount'];
58                $post_type = $instance['post_type'];
59                $interval = $instance['interval'];
60                $show_visits = $instance['show_visits'];
61                $suffix = $instance['suffix'];
62                $post_category = $instance['post_category'];
63?>
64        <p>
65          <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Widget title:','statify-widget'); ?>
66            <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); ?>" />
67          </label>
68        </p>
69        <p class="post_select">
70        <label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e( 'Post type:','statify-widget'); ?>
71        <select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>">
72                  <option value="postpage"><?php _e( 'posts + pages','statify-widget'); ?></option>
73          <?php
74                $post_types = get_post_types( array('public'=>true, 'show_ui'=>true), 'objects' );
75                foreach ( $post_types as $type ) {
76                    echo '<option value="'. esc_attr($type->name) . '" '. selected( $post_type, $type->name, false ) .'>' . esc_attr($type->labels->name) . '</option>';
77                }
78          ?>
79        </select>
80        </p>
81                <p class="category_select" style="display:none;">
82                <label for="<?php echo $this->get_field_id('post_category'); ?>"><?php _e( 'Post category:','statify-widget'); ?>
83      <select class="widefat" id="<?php echo $this->get_field_id('post_category'); ?>" name="<?php echo $this->get_field_name('post_category'); ?>">
84                        <option value="0">Alle Kategorien</option>
85                  <?php
86                                $post_categories = get_categories( array('hide_empty'=>0 ),'objects' );
87                foreach ( $post_categories as $type ) {
88                    echo '<option value="'. esc_attr($type->cat_ID) . '" '. selected( $post_category, $type->cat_ID, false ) .'>' . esc_attr($type->name) . '</option>';
89                }
90          ?>
91        </select>
92                </p>
93                <p>
94                        <label for="<?php echo $this->get_field_id('interval'); ?>"><?php _e( 'Amount of days:','statify-widget'); ?>
95                                <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); ?>" />
96                        </label>
97                        <small><?php _e( '0 days = show all items','statify-widget'); ?></small>
98                </p>
99        <p>
100          <label for="<?php echo $this->get_field_id('amount'); ?>"><?php _e( 'Amounts:','statify-widget'); ?>
101            <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); ?>" />
102          </label>
103                </p>
104                <p>
105          <label for="<?php echo $this->get_field_id('show_visits'); ?>">
106            <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); ?>>
107            <?php _e( 'Show view counter?','statify-widget'); ?></label>
108        </p>
109        <p>
110            <label for="<?php echo $this->get_field_id('suffix'); ?>"><?php _e( 'Custom text:','statify-widget'); ?>
111            <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); ?>" />
112                        </label>
113            <small><?php _e( '%VIEWS% = amount of views','statify-widget'); ?></small>
114        </p>
115<?php
116        }
117
118        /*
119        * Override old instance with new instance.
120        * @change 1.4.9
121        */
122        function update($new_instance, $old_instance) {
123                if(! empty( $new_instance['interval'] ) && $new_instance['interval'] != $old_instance['interval']) {
124                        delete_transient('statify_targets_'.$old_instance['interval']);
125                }
126               
127                $instance = array();
128                $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : NULL;
129                $instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? sanitize_key( $new_instance['post_type'] ) : STATIFY_WIDGET_DEFAULT_POST_TYPE;
130                $instance['interval'] = ( ! empty( $new_instance['interval'] ) ) ? intval( $new_instance['interval'] ) : STATIFY_WIDGET_DEFAULT_INTERVAL;
131                $instance['amount'] = ( ! empty( $new_instance['amount'] ) ) ? intval( $new_instance['amount'] ) : STATIFY_WIDGET_DEFAULT_AMOUNT;
132                $instance['show_visits'] = ! empty( $new_instance['show_visits'] ) ? 1 : 0;
133                $instance['post_category'] = ( ! empty( $new_instance['post_category'] ) ) ? intval( $new_instance['post_category'] ) : 0;
134                $instance['suffix'] = ( ! empty( $new_instance['suffix'] ) ) ? sanitize_text_field( $new_instance['suffix'] ) : STATIFY_WIDGET_DEFAULT_SUFFIX;
135                return $instance;
136        }
137       
138       
139        /*
140        * Post Layout
141        */
142        function statify_widget_template($posts) {
143        ?>
144                        <?php if ( empty($posts) ): ?>
145                        <p><?php _e( 'There are no posts yet.','statify-widget' ) ?></p>
146                        <?php else: ?>
147
148                        <ol class="statify-widget-list">
149                                <?php foreach ($posts as $post): ?>
150                                <li class="statify-widget-element">
151                                        <?php echo do_action( "statify_widget_before_link", $post); ?>
152                                        <a class="statify-widget-link"
153                                           title="<?php echo esc_attr( $post->post_title ); ?>"
154                                           href="<?php echo esc_url( $post->post_permalink ); ?>"><?php echo esc_html( $post->post_title ); ?></a>
155                                        <?php if ( isset( $post->post_suffix ) ) echo '<span>' . esc_html( $post->post_suffix ) . '</span>'; ?>
156                                        <?php echo do_action( "statify_widget_after_link", $post); ?>
157                                </li>
158                                <?php endforeach; ?>
159                        </ol>
160
161                        <?php endif; ?>
162        <?php
163        }
164
165        /*
166        * Print the widget
167        */
168        function widget($args, $instance) {
169                extract($args, EXTR_SKIP);
170
171                echo $before_widget;
172
173                $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']);
174                $amount = empty($instance['amount']) ? STATIFY_WIDGET_DEFAULT_AMOUNT : $instance['amount'];
175                $post_type = empty($instance['post_type']) ? STATIFY_WIDGET_DEFAULT_POST_TYPE : $instance['post_type'];
176                $interval = empty($instance['interval']) ? STATIFY_WIDGET_DEFAULT_INTERVAL : $instance['interval'];
177                $show_visits = empty($instance['show_visits']) ? 0 : 1;
178                $suffix_text =  empty($instance['suffix']) ? STATIFY_WIDGET_DEFAULT_SUFFIX : $instance['suffix'];
179                $post_category = empty($instance['post_category']) ? 0 : $instance['post_category'];
180
181                if ( ! empty( $title ) ) echo $before_title . esc_html( $title ) . $after_title;
182               
183                $posts = Statify_Posts::get_posts($post_type, $post_category, $amount, $interval);
184               
185                // Replace custom suffix text with views and add to WP_Post object
186                if ($show_visits) {
187                        foreach($posts as $post) {
188                                $post->post_suffix = str_replace(__("%VIEWS%","statify-widget"), intval($post->post_views), $suffix_text);
189                        }
190                }
191               
192                $this->statify_widget_template($posts);
193               
194                echo $after_widget;
195        }
196
197        /*
198        * Return the statify widget class for the hook
199        * @since 1.1.8
200        */
201
202        function statify_widget_class_callback( $className ) {
203                $widgetClass = $className;
204        }
205
206}
207
208/*
209* Print error message in admin interface
210* @change 1.4.8
211*/
212function showErrorMessages() {
213        $html = '<div class="error"><p>';
214        $html .= wp_kses_post( __('Please install <a target="_blank" href="http://wordpress.org/plugins/statify/">Statify</a> plugin first.','statify-widget') );
215        $html .= '</p></div>';
216        echo $html;
217}
218
219/*
220* Check if Statify is acitivated
221*/
222function requires_statify_plugin() {
223    $plugin_bcd_plugin = 'statify/statify.php';
224    $plugin = plugin_basename( __FILE__ );
225    $plugin_data = get_plugin_data( __FILE__, false );
226
227        if ( !is_plugin_active( $plugin_bcd_plugin) ) {
228        deactivate_plugins ( $plugin );
229                add_action('admin_notices', 'showErrorMessages');
230    }
231}
232
233/**
234 * Register Statify-Widget
235 *
236 * @since 1.1.7
237 */
238function register_statify_widget() {
239        register_widget( 'StatifyWidget' );
240}
241add_action( 'widgets_init', 'register_statify_widget' );
242add_action( 'admin_init', 'requires_statify_plugin' );
243
244/*
245 * Get statify count for post id in themes or shortcode
246 * @since 1.1.6
247 * @deprecated 1.3.3
248*/
249function statify_count($post_id, $days = 0) {
250        echo Statify_Posts::statify_count($post_id, $days);
251}
252
253/*
254 * Echo statify count for post id in themes or shortcode
255 * @since 1.1.9
256 */
257function get_statify_count($post_id, $days = 0) {
258        return Statify_Posts::statify_count($post_id, $days);
259}
260
261/*
262 * Return sum of all visits for the site
263 * @since 1.2
264 */
265function get_statify_count_sum($days = 0) {
266        return Statify_Posts::statify_count_sum($days);
267}
268
269/*
270 * Echo sum of all visits for the site
271 * @since 1.2
272 * @deprecated 1.3.3
273 */
274function statify_count_sum($days = 0) {
275        echo Statify_Posts::statify_count_sum($days);
276}
277
278/*
279 * Return all posts for type as array
280 * @since 1.3
281 */
282function statify_popular_posts($amount = 5, $days = 0, $post_type = 'post', $post_category = 0) {
283        return Statify_Posts::get_post_list($post_type, $post_category, $amount, $days);
284}
285
286/**
287 * Load plugin textdomain.
288 *
289 * @since 1.1.9
290 */
291function statify_widget_load_textdomain() {
292  load_plugin_textdomain( 'statify-widget', false, basename( dirname( __FILE__ ) ) . '/languages' );
293}
294
295add_action( 'init', 'statify_widget_load_textdomain' );
296
297/**
298 * Remove all Statify Widget transients, if deactivate plugin.
299 * @since 1.4.1
300 */
301function get_transient_keys_with_prefix( $prefix ) {
302        global $wpdb;
303
304        $prefix = $wpdb->esc_like( '_transient_' . $prefix );
305        $sql    = "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE '%s'";
306        $keys   = $wpdb->get_results( $wpdb->prepare( $sql, $prefix . '%' ), ARRAY_A );
307
308        if ( is_wp_error( $keys ) ) {
309                return [];
310        }
311
312        return array_map( function( $key ) {
313                return substr( $key['option_name'], strlen( '_transient_' ) );
314        }, $keys );
315}
316
317register_deactivation_hook(
318        __FILE__,
319        function() {
320                foreach ( get_transient_keys_with_prefix( STATIFY_WIDGET_DEFAULT_TRANSIENT_PREFIX ) as $key ) {
321                        delete_transient( $key );
322                }
323        }
324);
325
326/**
327 * Add JS to widget for dynamic category selection
328 * @since 1.4.6
329 */
330add_action( 'admin_enqueue_scripts', 'statify_widget_admin_scripts' );
331function statify_widget_admin_scripts( $hook ) {
332    if ( 'widgets.php' !== $hook && 'customize.php' !== $hook ) {
333        return;
334    }
335
336    wp_enqueue_script( 'statify-widget-js', plugins_url( '/js/statify_widget.js', __FILE__ ));
337}
338?>
Note: See TracBrowser for help on using the repository browser.