| 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 |
Author: Finn Dohrn |
| 6 |
Author URI: http://www.bit01.de/ |
| 7 |
Plugin URI: http://www.bit01.de/blog/statify-widget/ |
| 8 |
Version: 1.1.7 |
| 9 |
*/ |
| 10 |
|
| 11 |
require( 'Statify_Posts.class.php' ); |
| 12 |
|
| 13 |
define('DEFAULT_AMOUNT', 5); |
| 14 |
define('DEFAULT_POST_TYPE','post'); |
| 15 |
define('DEFAULT_SUFFIX', '(%ANZAHL% Aufrufe)'); |
| 16 |
define('DEFAULT_INTERVAL', 0); |
| 17 |
|
| 18 |
class StatifyWidget extends WP_Widget { |
| 19 |
/* |
| 20 |
* Register StatifyWidget to Wordpress |
| 21 |
*/ |
| 22 |
function __construct() { |
| 23 |
$widget_ops = array('classname' => 'statify-widget', 'description' => 'Zeigt beliebte Inhalte auf der Grundlage von Statify.' ); |
| 24 |
parent::__construct( |
| 25 |
'StatifyWidget', |
| 26 |
__('Statify Widget', 'text_domain'), |
| 27 |
$widget_ops |
| 28 |
); |
| 29 |
|
| 30 |
add_shortcode( "statify-count", "Statify_Posts::statify_count_shortcode"); |
| 31 |
} |
| 32 |
|
| 33 |
/* |
| 34 |
* Generating a from for settings |
| 35 |
*/ |
| 36 |
function form($instance) { |
| 37 |
$instance = wp_parse_args( (array) $instance, array( |
| 38 |
'title' => '', |
| 39 |
'amount' => DEFAULT_AMOUNT, |
| 40 |
'post_type' => DEFAULT_POST_TYPE, |
| 41 |
'interval' => DEFAULT_INTERVAL, |
| 42 |
'show_visits' => 0, |
| 43 |
'suffix' => DEFAULT_SUFFIX) ); |
| 44 |
$title = $instance['title']; |
| 45 |
$amount = $instance['amount']; |
| 46 |
$post_type = $instance['post_type']; |
| 47 |
$interval = $instance['interval']; |
| 48 |
$show_visits = $instance['show_visits']; |
| 49 |
$suffix = $instance['suffix']; |
| 50 |
?> |
| 51 |
<p> |
| 52 |
<label for="<?php echo $this->get_field_id('title'); ?>">Titel: |
| 53 |
<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); ?>" /> |
| 54 |
</label> |
| 55 |
</p> |
| 56 |
<p> |
| 57 |
<label for="<?php echo $this->get_field_id('post_type'); ?>">Inhaltstyp: |
| 58 |
<select class="widefat" id="<?php echo $this->get_field_id('post_type'); ?>" name="<?php echo $this->get_field_name('post_type'); ?>"> |
| 59 |
<option value="postpage">Beiträge + Seiten</option> |
| 60 |
<?php |
| 61 |
$post_types = get_post_types( array('public'=>true, 'show_ui'=>true), 'objects' ); |
| 62 |
foreach ( $post_types as $type ) { |
| 63 |
echo '<option value="'. esc_attr($type->name) . '" '. selected( $post_type, $type->name ) .'>' . esc_attr($type->labels->name) . '</option>'; |
| 64 |
} |
| 65 |
?> |
| 66 |
</select> |
| 67 |
</p> |
| 68 |
<p> |
| 69 |
<label for="<?php echo $this->get_field_id('interval'); ?>">Letzten |
| 70 |
<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); ?>" /> |
| 71 |
</label> Tage |
| 72 |
<br /><small>0 Tage = Alles anzeigen</small> |
| 73 |
</p> |
| 74 |
<p> |
| 75 |
<label for="<?php echo $this->get_field_id('amount'); ?>">Anzahl: |
| 76 |
<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); ?>" /> |
| 77 |
</label> |
| 78 |
<label for="<?php echo $this->get_field_id('show_visits'); ?>"> |
| 79 |
<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); ?>> |
| 80 |
Aufrufe anzeigen?</label> |
| 81 |
</p> |
| 82 |
<p> |
| 83 |
<label for="<?php echo $this->get_field_id('suffix'); ?>">Benutzerdefinierter Text: |
| 84 |
<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); ?>" /> |
| 85 |
</label> |
| 86 |
<small>%ANZAHL% = Anzahl der Aufrufe</small> |
| 87 |
</p> |
| 88 |
<?php |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* Override old instance with new instance. |
| 93 |
*/ |
| 94 |
function update($new_instance, $old_instance) { |
| 95 |
$instance = array(); |
| 96 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : ''; |
| 97 |
$instance['post_type'] = ( ! empty( $new_instance['post_type'] ) ) ? $new_instance['post_type'] : DEFAULT_POST_TYPE; |
| 98 |
if($new_instance['interval'] != $old_instance['interval']) { |
| 99 |
delete_transient('statify_targets_'.$old_instance['interval']); |
| 100 |
} |
| 101 |
$instance['interval'] = ( ! empty( $new_instance['interval'] ) ) ? $new_instance['interval'] : DEFAULT_INTERVAL; |
| 102 |
$instance['amount'] = ( ! empty( $new_instance['amount'] ) ) ? sanitize_text_field( $new_instance['amount'] ) : DEFAULT_AMOUNT; |
| 103 |
$instance['show_visits'] = ( ! empty( $new_instance['show_visits'] ) ) ? $new_instance['show_visits'] : 0; |
| 104 |
$instance['suffix'] = ( ! empty( $new_instance['suffix'] ) ) ? sanitize_text_field( $new_instance['suffix'] ) : DEFAULT_SUFFIX; |
| 105 |
return $instance; |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* Print the widget |
| 110 |
*/ |
| 111 |
function widget($args, $instance) { |
| 112 |
extract($args, EXTR_SKIP); |
| 113 |
|
| 114 |
echo $before_widget; |
| 115 |
|
| 116 |
$title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); |
| 117 |
$amount = empty($instance['amount']) ? DEFAULT_AMOUNT : $instance['amount']; |
| 118 |
$post_type = empty($instance['post_type']) ? DEFAULT_POST_TYPE : $instance['post_type']; |
| 119 |
$interval = empty($instance['interval']) ? DEFAULT_INTERVAL : $instance['interval']; |
| 120 |
$show_visits = empty($instance['show_visits']) ? 0 : 1; |
| 121 |
$suffix = empty($instance['suffix']) ? DEFAULT_SUFFIX : $instance['suffix']; |
| 122 |
|
| 123 |
if (!empty($title)) echo $before_title . $title . $after_title; |
| 124 |
|
| 125 |
$popular_content = Statify_Posts::get_posts($post_type, $amount, $interval); |
| 126 |
if (empty( $popular_content )) { |
| 127 |
echo "<p>Es gibt noch keine Einträge.</p>"; |
| 128 |
} else { |
| 129 |
echo '<ul class="statify-widget">'."\n"; |
| 130 |
foreach($popular_content as $post) { |
| 131 |
$_suffix = ($show_visits) ? ' <span>' . str_replace("%ANZAHL%", intval($post['visits']), $suffix) . '</span>' : ''; |
| 132 |
echo '<li><a title="' . esc_html($post['title']) . '" href="' . esc_url($post['url']) . '">' . esc_html($post['title']) . '</a>'. $_suffix .'</li>'."\n"; |
| 133 |
} |
| 134 |
echo "</ul>"."\n"; |
| 135 |
} |
| 136 |
|
| 137 |
echo $after_widget; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
* Print error message in admin interface |
| 143 |
*/ |
| 144 |
function showErrorMessages() { |
| 145 |
$html = '<div class="error"><p>'; |
| 146 |
$html .= __( 'Bitte installieren und aktivieren Sie zuerst das <a target="_blank" href="http://wordpress.org/plugins/statify/">Statify</a> Plugin.', 'error-statify-widget' ); |
| 147 |
$html .= '</p></div>'; |
| 148 |
echo $html; |
| 149 |
} |
| 150 |
|
| 151 |
/* |
| 152 |
* Check if Statify is acitivated |
| 153 |
*/ |
| 154 |
function requires_statify_plugin() { |
| 155 |
$plugin_bcd_plugin = 'statify/statify.php'; |
| 156 |
$plugin = plugin_basename( __FILE__ ); |
| 157 |
$plugin_data = get_plugin_data( __FILE__, false ); |
| 158 |
|
| 159 |
if ( !is_plugin_active( $plugin_bcd_plugin) ) { |
| 160 |
deactivate_plugins ( $plugin ); |
| 161 |
add_action('admin_notices', 'showErrorMessages'); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Register Statify-Widget |
| 167 |
* |
| 168 |
* @since 1.1.7 |
| 169 |
*/ |
| 170 |
function register_statify_widget() { |
| 171 |
register_widget( 'StatifyWidget' ); |
| 172 |
} |
| 173 |
add_action( 'widgets_init', 'register_statify_widget' ); |
| 174 |
add_action( 'admin_init', 'requires_statify_plugin' ); |
| 175 |
|
| 176 |
function statify_count($post_id) { |
| 177 |
return Statify_Posts::statify_count($post_id); |
| 178 |
} |
| 179 |
|
| 180 |
function get_statify_count($post_id) { |
| 181 |
return Statify_Posts::get_statify_count($post_id); |
| 182 |
} |
| 183 |
?> |
| 184 |
|