| 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.4.3 |
| 10 |
*/ |
| 11 |
|
| 12 |
/* Quit */ |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
define('STATIFY_WIDGET_DEFAULT_AMOUNT', 5); |
| 16 |
define('STATIFY_WIDGET_DEFAULT_POST_TYPE','post'); |
| 17 |
define('STATIFY_WIDGET_DEFAULT_INTERVAL', 0); |
| 18 |
define('STATIFY_WIDGET_DEFAULT_SUFFIX', ''); |
| 19 |
define('STATIFY_WIDGET_DEFAULT_TRANSIENT_PREFIX', 'statify_targets_'); |
| 20 |
define('STATIFY_WIDGET_DEFAULT_EXPIRATION', 60 * 4); |
| 21 |
|
| 22 |
require( 'Statify_Post.class.php' ); |
| 23 |
require( 'Statify_Posts.class.php' ); |
| 24 |
|
| 25 |
class 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 |
*/ |
| 121 |
function update($new_instance, $old_instance) { |
| 122 |
$instance = array(); |
| 123 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : ''; |
| 124 |
$instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? $new_instance['post_type'] : STATIFY_WIDGET_DEFAULT_POST_TYPE; |
| 125 |
if(! empty( $new_instance['interval'] ) && $new_instance['interval'] != $old_instance['interval']) { |
| 126 |
delete_transient('statify_targets_'.$old_instance['interval']); |
| 127 |
} |
| 128 |
$instance['interval'] = ( ! empty( $new_instance['interval'] ) ) ? $new_instance['interval'] : STATIFY_WIDGET_DEFAULT_INTERVAL; |
| 129 |
$instance['amount'] = ( ! empty( $new_instance['amount'] ) ) ? sanitize_text_field( $new_instance['amount'] ) : STATIFY_WIDGET_DEFAULT_AMOUNT; |
| 130 |
$instance['show_visits'] = ( ! empty( $new_instance['show_visits'] ) ) ? $new_instance['show_visits'] : 0; |
| 131 |
$instance['suffix'] = ( ! empty( $new_instance['suffix'] ) ) ? sanitize_text_field( $new_instance['suffix'] ) : STATIFY_WIDGET_DEFAULT_SUFFIX; |
| 132 |
$instance['post_category'] = ( ! empty( $new_instance['post_category'] ) ) ? sanitize_text_field( $new_instance['post_category'] ) : 0; |
| 133 |
return $instance; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/* |
| 138 |
* Post Layout |
| 139 |
*/ |
| 140 |
function statify_widget_template($posts) { |
| 141 |
?> |
| 142 |
<?php if ( empty($posts) ): ?> |
| 143 |
<p><?php __( 'There are no posts yet.','statify-widget' ) ?></p> |
| 144 |
<?php else: ?> |
| 145 |
|
| 146 |
<ol class="statify-widget-list"> |
| 147 |
<?php foreach ($posts as $post): ?> |
| 148 |
<li class="statify-widget-element"> |
| 149 |
<?php echo do_action( "statify_widget_before_link", $post); ?> |
| 150 |
<a class='statify-widget-link' title='<?php echo $post->post_title ?>' href='<?php echo $post->post_permalink ?>'><?php echo $post->post_title ?></a> <?php if (isset($post->post_suffix)) echo "<span>". $post->post_suffix ."</span>" ?> |
| 151 |
<?php echo do_action( "statify_widget_after_link", $post); ?> |
| 152 |
</li> |
| 153 |
<?php endforeach; ?> |
| 154 |
</ol> |
| 155 |
|
| 156 |
<?php endif; ?> |
| 157 |
<?php |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
* Print the widget |
| 162 |
*/ |
| 163 |
function widget($args, $instance) { |
| 164 |
extract($args, EXTR_SKIP); |
| 165 |
|
| 166 |
echo $before_widget; |
| 167 |
|
| 168 |
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); |
| 169 |
$amount = empty($instance['amount']) ? STATIFY_WIDGET_DEFAULT_AMOUNT : $instance['amount']; |
| 170 |
$post_type = empty($instance['post_type']) ? STATIFY_WIDGET_DEFAULT_POST_TYPE : $instance['post_type']; |
| 171 |
$interval = empty($instance['interval']) ? STATIFY_WIDGET_DEFAULT_INTERVAL : $instance['interval']; |
| 172 |
$show_visits = empty($instance['show_visits']) ? 0 : 1; |
| 173 |
$suffix_text = empty($instance['suffix']) ? STATIFY_WIDGET_DEFAULT_SUFFIX : $instance['suffix']; |
| 174 |
$post_category = empty($instance['post_category']) ? 0 : $instance['post_category']; |
| 175 |
|
| 176 |
if (!empty($title)) echo $before_title . $title . $after_title; |
| 177 |
|
| 178 |
$posts = Statify_Posts::get_posts($post_type, $post_category, $amount, $interval); |
| 179 |
|
| 180 |
// Replace custom suffix text with views and add to WP_Post object |
| 181 |
if ($show_visits) { |
| 182 |
foreach($posts as $post) { |
| 183 |
$post->post_suffix = str_replace(__("%VIEWS%","statify-widget"), intval($post->post_views), $suffix_text); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
$this->statify_widget_template($posts); |
| 188 |
|
| 189 |
echo $after_widget; |
| 190 |
} |
| 191 |
|
| 192 |
/* |
| 193 |
* Return the statify widget class for the hook |
| 194 |
* @since 1.1.8 |
| 195 |
*/ |
| 196 |
|
| 197 |
function statify_widget_class_callback( $className ) { |
| 198 |
$widgetClass = $className; |
| 199 |
} |
| 200 |
|
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
* Print error message in admin interface |
| 205 |
*/ |
| 206 |
function showErrorMessages() { |
| 207 |
$html = '<div class="error"><p>'; |
| 208 |
$html .= __( 'Please install <a target="_blank" href="http://wordpress.org/plugins/statify/">Statify</a> plugin first.','statify-widget'); |
| 209 |
$html .= '</p></div>'; |
| 210 |
echo $html; |
| 211 |
} |
| 212 |
|
| 213 |
/* |
| 214 |
* Check if Statify is acitivated |
| 215 |
*/ |
| 216 |
function requires_statify_plugin() { |
| 217 |
$plugin_bcd_plugin = 'statify/statify.php'; |
| 218 |
$plugin = plugin_basename( __FILE__ ); |
| 219 |
$plugin_data = get_plugin_data( __FILE__, false ); |
| 220 |
|
| 221 |
if ( !is_plugin_active( $plugin_bcd_plugin) ) { |
| 222 |
deactivate_plugins ( $plugin ); |
| 223 |
add_action('admin_notices', 'showErrorMessages'); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Register Statify-Widget |
| 229 |
* |
| 230 |
* @since 1.1.7 |
| 231 |
*/ |
| 232 |
function register_statify_widget() { |
| 233 |
register_widget( 'StatifyWidget' ); |
| 234 |
} |
| 235 |
add_action( 'widgets_init', 'register_statify_widget' ); |
| 236 |
add_action( 'admin_init', 'requires_statify_plugin' ); |
| 237 |
|
| 238 |
/* |
| 239 |
* Get statify count for post id in themes or shortcode |
| 240 |
* @since 1.1.6 |
| 241 |
* @deprecated 1.3.3 |
| 242 |
*/ |
| 243 |
function statify_count($post_id, $days = 0) { |
| 244 |
echo Statify_Posts::statify_count($post_id, $days); |
| 245 |
} |
| 246 |
|
| 247 |
/* |
| 248 |
* Echo statify count for post id in themes or shortcode |
| 249 |
* @since 1.1.9 |
| 250 |
*/ |
| 251 |
function get_statify_count($post_id, $days = 0) { |
| 252 |
return Statify_Posts::statify_count($post_id, $days); |
| 253 |
} |
| 254 |
|
| 255 |
/* |
| 256 |
* Return sum of all visits for the site |
| 257 |
* @since 1.2 |
| 258 |
*/ |
| 259 |
function get_statify_count_sum($days = 0) { |
| 260 |
return Statify_Posts::statify_count_sum($days); |
| 261 |
} |
| 262 |
|
| 263 |
/* |
| 264 |
* Echo sum of all visits for the site |
| 265 |
* @since 1.2 |
| 266 |
* @deprecated 1.3.3 |
| 267 |
*/ |
| 268 |
function statify_count_sum($days = 0) { |
| 269 |
echo Statify_Posts::statify_count_sum($days); |
| 270 |
} |
| 271 |
|
| 272 |
/* |
| 273 |
* Return all posts for type as array |
| 274 |
* @since 1.3 |
| 275 |
*/ |
| 276 |
function statify_popular_posts($amount = 5, $days = 0, $post_type = 'post', $post_category = 0) { |
| 277 |
return Statify_Posts::get_post_list($post_type, $post_category, $amount, $days); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Load plugin textdomain. |
| 282 |
* |
| 283 |
* @since 1.1.9 |
| 284 |
*/ |
| 285 |
function statify_widget_load_textdomain() { |
| 286 |
load_plugin_textdomain( 'statify-widget', false, basename( dirname( __FILE__ ) ) . '/languages' ); |
| 287 |
} |
| 288 |
|
| 289 |
add_action( 'init', 'statify_widget_load_textdomain' ); |
| 290 |
|
| 291 |
/** |
| 292 |
* Remove all Statify Widget transients, if deactivate plugin. |
| 293 |
* @since 1.4.1 |
| 294 |
*/ |
| 295 |
function get_transient_keys_with_prefix( $prefix ) { |
| 296 |
global $wpdb; |
| 297 |
|
| 298 |
$prefix = $wpdb->esc_like( '_transient_' . $prefix ); |
| 299 |
$sql = "SELECT `option_name` FROM $wpdb->options WHERE `option_name` LIKE '%s'"; |
| 300 |
$keys = $wpdb->get_results( $wpdb->prepare( $sql, $prefix . '%' ), ARRAY_A ); |
| 301 |
|
| 302 |
if ( is_wp_error( $keys ) ) { |
| 303 |
return []; |
| 304 |
} |
| 305 |
|
| 306 |
return array_map( function( $key ) { |
| 307 |
return substr( $key['option_name'], strlen( '_transient_' ) ); |
| 308 |
}, $keys ); |
| 309 |
} |
| 310 |
|
| 311 |
register_deactivation_hook( |
| 312 |
__FILE__, |
| 313 |
function() { |
| 314 |
foreach ( get_transient_keys_with_prefix( STATIFY_WIDGET_DEFAULT_TRANSIENT_PREFIX ) as $key ) { |
| 315 |
delete_transient( $key ); |
| 316 |
} |
| 317 |
} |
| 318 |
); |
| 319 |
?> |