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