| 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.6 |
| 10 |
*/ |
| 11 |
|
| 12 |
require( 'Statify_Post.class.php' ); |
| 13 |
require( 'Statify_Posts.class.php' ); |
| 14 |
|
| 15 |
define('DEFAULT_AMOUNT', 5); |
| 16 |
define('DEFAULT_POST_TYPE','post'); |
| 17 |
define('DEFAULT_INTERVAL', 0); |
| 18 |
define('DEFAULT_SUFFIX', ''); |
| 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 |
wp_enqueue_script( 'statify-widget-js', plugins_url( '/js/statify_widget.js', __FILE__ )); |
| 42 |
|
| 43 |
$instance = wp_parse_args( (array) $instance, array( |
| 44 |
'title' => '', |
| 45 |
'amount' => DEFAULT_AMOUNT, |
| 46 |
'post_type' => DEFAULT_POST_TYPE, |
| 47 |
'interval' => DEFAULT_INTERVAL, |
| 48 |
'show_visits' => 0, |
| 49 |
'list_style_type' => "ol", |
| 50 |
'suffix' => __('%VIEWS% views','statify-widget'), |
| 51 |
'post_category' => 0) ); |
| 52 |
|
| 53 |
$title = $instance['title']; |
| 54 |
$amount = $instance['amount']; |
| 55 |
$post_type = $instance['post_type']; |
| 56 |
$interval = $instance['interval']; |
| 57 |
$show_visits = $instance['show_visits']; |
| 58 |
$suffix = $instance['suffix']; |
| 59 |
$post_category = $instance['post_category']; |
| 60 |
?> |
| 61 |
<p> |
| 62 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Widget title:','statify-widget'); ?> |
| 63 |
<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); ?>" /> |
| 64 |
</label> |
| 65 |
</p> |
| 66 |
<p class="post_select"> |
| 67 |
<label for="<?php echo $this->get_field_id('post_type'); ?>"><?php _e( 'Post type:','statify-widget'); ?> |
| 68 |
<select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>"> |
| 69 |
<option value="postpage"><?php _e( 'posts + pages','statify-widget'); ?></option> |
| 70 |
<?php |
| 71 |
$post_types = get_post_types( array('public'=>true, 'show_ui'=>true), 'objects' ); |
| 72 |
foreach ( $post_types as $type ) { |
| 73 |
echo '<option value="'. esc_attr($type->name) . '" '. selected( $post_type, $type->name, false ) .'>' . esc_attr($type->labels->name) . '</option>'; |
| 74 |
} |
| 75 |
?> |
| 76 |
</select> |
| 77 |
</p> |
| 78 |
<p class="category_select" style="display:none;"> |
| 79 |
<label for="<?php echo $this->get_field_id('post_category'); ?>"><?php _e( 'Post category:','statify-widget'); ?> |
| 80 |
<select class="widefat" id="<?php echo $this->get_field_id('post_category'); ?>" name="<?php echo $this->get_field_name('post_category'); ?>"> |
| 81 |
<option value="0">Alle Kategorien</option> |
| 82 |
<?php |
| 83 |
$post_categories = get_categories( array('hide_empty'=>0 ),'objects' ); |
| 84 |
foreach ( $post_categories as $type ) { |
| 85 |
echo '<option value="'. esc_attr($type->cat_ID) . '" '. selected( $post_category, $type->cat_ID, false ) .'>' . esc_attr($type->name) . '</option>'; |
| 86 |
} |
| 87 |
?> |
| 88 |
</select> |
| 89 |
</p> |
| 90 |
<p> |
| 91 |
<label for="<?php echo $this->get_field_id('interval'); ?>"><?php _e( 'Last ','statify-widget'); ?> |
| 92 |
<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); ?>" /> |
| 93 |
</label><?php _e( ' days. (If enough stats exists)','statify-widget'); ?> |
| 94 |
<br /><small><?php _e( '0 days = show all items','statify-widget'); ?></small> |
| 95 |
</p> |
| 96 |
<p> |
| 97 |
<label for="<?php echo $this->get_field_id('amount'); ?>"><?php _e( 'Amounts:','statify-widget'); ?> |
| 98 |
<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); ?>" /> |
| 99 |
</label> |
| 100 |
</p> |
| 101 |
<p> |
| 102 |
<label for="<?php echo $this->get_field_id('show_visits'); ?>"> |
| 103 |
<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); ?>> |
| 104 |
<?php _e( 'Show view counter?','statify-widget'); ?></label> |
| 105 |
</p> |
| 106 |
<p> |
| 107 |
<label for="<?php echo $this->get_field_id('suffix'); ?>"><?php _e( 'Custom text:','statify-widget'); ?> |
| 108 |
<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); ?>" /> |
| 109 |
</label> |
| 110 |
<small><?php _e( '%VIEWS% = amount of views','statify-widget'); ?></small> |
| 111 |
</p> |
| 112 |
<?php |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
* Override old instance with new instance. |
| 117 |
*/ |
| 118 |
function update($new_instance, $old_instance) { |
| 119 |
$instance = array(); |
| 120 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : ''; |
| 121 |
$instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? $new_instance['post_type'] : DEFAULT_POST_TYPE; |
| 122 |
if(! empty( $new_instance['interval'] ) && $new_instance['interval'] != $old_instance['interval']) { |
| 123 |
delete_transient('statify_targets_'.$old_instance['interval']); |
| 124 |
} |
| 125 |
$instance['interval'] = ( ! empty( $new_instance['interval'] ) ) ? $new_instance['interval'] : DEFAULT_INTERVAL; |
| 126 |
$instance['amount'] = ( ! empty( $new_instance['amount'] ) ) ? sanitize_text_field( $new_instance['amount'] ) : DEFAULT_AMOUNT; |
| 127 |
$instance['show_visits'] = ( ! empty( $new_instance['show_visits'] ) ) ? $new_instance['show_visits'] : 0; |
| 128 |
$instance['suffix'] = ( ! empty( $new_instance['suffix'] ) ) ? sanitize_text_field( $new_instance['suffix'] ) : DEFAULT_SUFFIX; |
| 129 |
$instance['post_category'] = ( ! empty( $new_instance['post_category'] ) ) ? sanitize_text_field( $new_instance['post_category'] ) : 0; |
| 130 |
return $instance; |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* Post Layout |
| 135 |
*/ |
| 136 |
function statify_widget__template($posts) { |
| 137 |
?> |
| 138 |
<?php if ( empty($posts) ): ?> |
| 139 |
<p><?php __( 'There are no posts yet.','statify-widget' ) ?></p> |
| 140 |
<?php else: ?> |
| 141 |
|
| 142 |
<ol class="statify-widget-list"> |
| 143 |
<?php foreach ($posts as $post): ?> |
| 144 |
<li class="statify-widget-element"> |
| 145 |
<a class="statify-widget-link" title="<?php echo $post->post_title ?>" href="<?php echo $post->post_permalink ?>"><?php echo $post->post_title ?></a> <span>(<?php echo $post->post_suffix ?>)</span> |
| 146 |
</li> |
| 147 |
<?php endforeach; ?> |
| 148 |
</ol> |
| 149 |
|
| 150 |
<?php endif; ?> |
| 151 |
<?php |
| 152 |
} |
| 153 |
|
| 154 |
/* |
| 155 |
* Print the widget |
| 156 |
*/ |
| 157 |
function widget($args, $instance) { |
| 158 |
extract($args, EXTR_SKIP); |
| 159 |
|
| 160 |
echo $before_widget; |
| 161 |
|
| 162 |
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); |
| 163 |
$amount = empty($instance['amount']) ? DEFAULT_AMOUNT : $instance['amount']; |
| 164 |
$post_type = empty($instance['post_type']) ? DEFAULT_POST_TYPE : $instance['post_type']; |
| 165 |
$interval = empty($instance['interval']) ? DEFAULT_INTERVAL : $instance['interval']; |
| 166 |
$show_visits = empty($instance['show_visits']) ? 0 : 1; |
| 167 |
$suffix_text = empty($instance['suffix']) ? DEFAULT_SUFFIX : $instance['suffix']; |
| 168 |
$post_category = empty($instance['post_category']) ? 0 : $instance['post_category']; |
| 169 |
|
| 170 |
if (!empty($title)) echo $before_title . $title . $after_title; |
| 171 |
|
| 172 |
$posts = Statify_Posts::get_posts($post_type, $post_category, $amount, $interval); |
| 173 |
|
| 174 |
// Replace custom suffix text with views and add to WP_Post object |
| 175 |
if ($show_visits) { |
| 176 |
foreach($posts as $post) { |
| 177 |
$post->post_suffix = str_replace(__("%VIEWS%","statify-widget"), intval($post->post_views), $suffix_text); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
apply_filters( 'statify_widget__template', $posts ); |
| 182 |
|
| 183 |
echo $after_widget; |
| 184 |
} |
| 185 |
|
| 186 |
/* |
| 187 |
* Return the statify widget class for the hook |
| 188 |
* @since 1.1.8 |
| 189 |
*/ |
| 190 |
|
| 191 |
function statify_widget_class_callback( $className ) { |
| 192 |
$widgetClass = $className; |
| 193 |
} |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
/* |
| 198 |
* Print error message in admin interface |
| 199 |
*/ |
| 200 |
function showErrorMessages() { |
| 201 |
$html = '<div class="error"><p>'; |
| 202 |
$html .= __( 'Please install <a target="_blank" href="http://wordpress.org/plugins/statify/">Statify</a> plugin first.','statify-widget'); |
| 203 |
$html .= '</p></div>'; |
| 204 |
echo $html; |
| 205 |
} |
| 206 |
|
| 207 |
/* |
| 208 |
* Check if Statify is acitivated |
| 209 |
*/ |
| 210 |
function requires_statify_plugin() { |
| 211 |
$plugin_bcd_plugin = 'statify/statify.php'; |
| 212 |
$plugin = plugin_basename( __FILE__ ); |
| 213 |
$plugin_data = get_plugin_data( __FILE__, false ); |
| 214 |
|
| 215 |
if ( !is_plugin_active( $plugin_bcd_plugin) ) { |
| 216 |
deactivate_plugins ( $plugin ); |
| 217 |
add_action('admin_notices', 'showErrorMessages'); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Register Statify-Widget |
| 223 |
* |
| 224 |
* @since 1.1.7 |
| 225 |
*/ |
| 226 |
function register_statify_widget() { |
| 227 |
register_widget( 'StatifyWidget' ); |
| 228 |
} |
| 229 |
add_action( 'widgets_init', 'register_statify_widget' ); |
| 230 |
add_action( 'admin_init', 'requires_statify_plugin' ); |
| 231 |
|
| 232 |
/* |
| 233 |
* Get statify count for post id in themes or shortcode |
| 234 |
* @since 1.1.6 |
| 235 |
* @deprecated 1.3.3 |
| 236 |
*/ |
| 237 |
function statify_count($post_id, $days = 0) { |
| 238 |
echo Statify_Posts::statify_count($post_id, $days); |
| 239 |
} |
| 240 |
|
| 241 |
/* |
| 242 |
* Echo statify count for post id in themes or shortcode |
| 243 |
* @since 1.1.9 |
| 244 |
*/ |
| 245 |
function get_statify_count($post_id, $days = 0) { |
| 246 |
return Statify_Posts::statify_count($post_id, $days); |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* Return sum of all visits for the site |
| 251 |
* @since 1.2 |
| 252 |
*/ |
| 253 |
function get_statify_count_sum($days = 0) { |
| 254 |
return Statify_Posts::statify_count_sum($days); |
| 255 |
} |
| 256 |
|
| 257 |
/* |
| 258 |
* Echo sum of all visits for the site |
| 259 |
* @since 1.2 |
| 260 |
* @deprecated 1.3.3 |
| 261 |
*/ |
| 262 |
function statify_count_sum($days = 0) { |
| 263 |
echo Statify_Posts::statify_count_sum($days); |
| 264 |
} |
| 265 |
|
| 266 |
/* |
| 267 |
* Return all posts for type as array |
| 268 |
* @since 1.3 |
| 269 |
*/ |
| 270 |
function statify_popular_posts($amount = 5, $days = 0, $post_type = 'post', $post_category = 0) { |
| 271 |
return Statify_Posts::get_posts($post_type, $post_category, $amount, $days); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Load plugin textdomain. |
| 276 |
* |
| 277 |
* @since 1.1.9 |
| 278 |
*/ |
| 279 |
function statify_widget_load_textdomain() { |
| 280 |
load_plugin_textdomain( 'statify-widget', false, basename( dirname( __FILE__ ) ) . '/languages' ); |
| 281 |
} |
| 282 |
|
| 283 |
add_action( 'init', 'statify_widget_load_textdomain' ); |
| 284 |
?> |