-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathermekeilkarree-functionality-plugin.php
385 lines (327 loc) · 13.8 KB
/
ermekeilkarree-functionality-plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/*
Plugin Name: Ermekeilkarree Functionality Plugin
Description: Plugin for site ermekeilkarree.de offering functionality that is independent from themes. Activate this plugin for the whole multisite network to make it an integral part of the network.
Version: 0.5.0
License: MIT
Author: Daniel Appelt
Author URI: https://github.com/danielappelt
See also: http://wpcandy.com/teaches/how-to-create-a-functionality-plugin/
*/
// function custom_get_bloginfo($output, $show) {
// if($show === 'name' && strrpos($output, "e.V.") !== false) {
// $output = substr($output, 0, -4) . "<span>e.V.</span>";
// }
// return $output;
// }
// add_filter('bloginfo', 'custom_get_bloginfo', 10, 2);
function get_global_title() {
$title = get_bloginfo();
if(strrpos($title, "e.V.") !== false) {
$title = substr($title, 0, -4) . "<span>e.V.</span>";
}
return $title;
}
/*
Register global menu locations for navigation and footer that appear across the network.
See http://wpmututorials.com/plugins/add-a-global-menu-to-your-network/
*/
register_nav_menu( 'global', 'Global Navigation Menu' );
register_nav_menu( 'footer', 'Global Footer Menu' );
function global_menu_init() {
if (!is_main_site()) {
// Remove the menu from menu screens on sub sites to avoid confusions.
unregister_nav_menu('global');
unregister_nav_menu('footer');
unregister_nav_menu('mobile-nav');
}
}
// Remove global menus from admin and customizing areas in sub sites.
add_action('admin_init', 'global_menu_init');
add_action('start_previewing_theme', 'global_menu_init');
/*
Inject menus defined for the network's main site above into other sites
on the network.
*/
function global_menu_filter($output, $args) {
// This is the current network's information; 'site' is old terminology.
global $current_site;
$sorted_menu_items = null;
if(!is_main_site()) {
if($args->theme_location == "global"
|| $args->theme_location == "footer"
|| $args->theme_location == "mobile-nav") {
// Retrieve the main site's global menu.
switch_to_blog($current_site->blog_id);
$sorted_menu_items = wp_nav_menu($args);
restore_current_blog();
}
}
return $sorted_menu_items;
}
// See http://codex.wordpress.org/Function_Reference/add_filter for priority and accepted_args.
add_filter('pre_wp_nav_menu', 'global_menu_filter', 10, 2);
// TODO: this does not work yet. Close offCanvas WHEN following #<id> links
function add_close_to_mobile_nav_anchors( $atts, $item, $args, $depth ) {
if($args->theme_location === 'mobile-nav') {
$atts['data-close'] = 'offCanvas';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_close_to_mobile_nav_anchors', 10, 4 );
/*
Register another menu which might be used on each blog to add sub site navigation.
*/
register_nav_menu( 'site', 'Site Menu' );
/*
Create a custom post type to handle custom alerts to be placed on the front page.
TODO: Maybe it's easier to just use a plugin for this. For example:
https://wordpress.org/plugins/custom-post-type-ui/
https://wordpress.org/plugins/pods/
https://github.com/wpmetabox/mb-custom-post-type
*/
function create_custom_post_alert() {
$labels = array(
'name' => _x( 'Alerts', 'post type general name' ),
'singular_name' => _x( 'Alert', 'post type singular name' ),
'add_new' => _x( 'Add New', 'post type alert' ),
'add_new_item' => __( 'Add New Alert' ),
'edit_item' => __( 'Edit Alert' ),
'new_item' => __( 'New Alert' ),
'all_items' => __( 'All Alerts' ),
'view_item' => __( 'View Alert' ),
'search_items' => __( 'Search Alerts' ),
'not_found' => __( 'No alerts found' ),
'not_found_in_trash' => __( 'No alerts found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Alerts'
);
$args = array(
'labels' => $labels,
'description' => 'Custom alerts to be placed on the front page',
'public' => true,
'menu_position' => 5,
'supports' => array( 'editor' ),
'has_archive' => true
);
register_post_type( 'alert', $args );
}
add_action( 'init', 'create_custom_post_alert' );
/*
Reduce alert columns to relevant entries.
*/
function edit_alert_columns( $columns ) {
$columns = array(
'cb' => '<input type="checkbox" />',
'date' => __( 'Date' ),
'title' => __( 'Alert' ),
'content' => __( 'Content' )
);
return $columns;
}
add_filter( 'manage_edit-alert_columns', 'edit_alert_columns' ) ;
# TODO: we would like to have the functionality of the 'title' column
# but with only the excerpt being displayed. This does not seem to be easily possible.
function manage_alert_columns( $column, $post_id ) {
global $post;
the_excerpt();
}
add_action( 'manage_alert_posts_custom_column', 'manage_alert_columns', 10, 2 );
/*
Retrieve garden opening times for the following 7 days from Events Manager plugin.
*/
function get_garden_opening() {
$args = array(
# Retrieve garden events for the next 7 days including today
'scope' => date('Y-m-d', strtotime('today')).','.date('Y-m-d', strtotime('today +6 days')),
'category' => '7', # TODO: garden category - should be customizable
'pagination' => false,
'order_by' => 'event_start_date,event_start_time,event_end_time',
);
$events = EM_Events::get( $args );
# echo EM_Events::output( $args );
# TODO: A sweep algorithm could be a better solution.
# Create a bucket for every day and collect opening times into each bucket
$buckets = array(
strtotime('today') => [],
strtotime('today +1 day') => [],
strtotime('today +2 days') => [],
strtotime('today +3 days') => [],
strtotime('today +4 days') => [],
strtotime('today +5 days') => [],
strtotime('today +6 days') => [],
);
# PHP REPL: https://repl.it/repls/
# http://www.the-art-of-web.com/php/strtotime/
# http://wp-events-plugin.com/documentation/event-search-attributes/
# https://github.com/bippo/events-manager-wordpress/blob/23cc0d4bbd9b9f3a8aacb1dc0428c1298dba5dbc/templates/templates/rss.php#L22
foreach ( $events as $event ) {
# Put all events belonging to a certain day into the same bucket
$bucket =& $buckets[strtotime($event->event_start_date)];
$last = count($bucket) - 1;
if($last > 0 && $event->event_start_time <= $bucket[$last]) {
if($event->event_end_time > $bucket[$last]) {
# Create a consecutive time span
$bucket[$last] = $event->event_end_time;
} // Otherwise $event is completely contained
} else {
# Create a new time span
array_push($bucket, $event->event_start_time, $event->event_end_time);
}
# The following statement is essential! See https://repl.it/repls/MistyModestNlp
unset($bucket);
}
# echo '<pre>'; var_dump($buckets); echo '</pre>';
$timeline = array();
foreach ( $buckets as $day => $bucket ) {
# Now each bucket contains a list of non-consecutive start and end times.
# Try to find commonalities in the days, i.e. compute the list of individual entries
$isMatch = false;
foreach($timeline as $refDay => $days) {
# Create a common output if consecutive days have the same opening times.
# Using count and equality comparison seems to yield better results than array_diff
if($day == end($days) + 86400 &&
count($bucket) == count($buckets[$refDay]) && $bucket == $buckets[$refDay]) {
array_push($timeline[$refDay], $day);
$isMatch = true;
break;
}
}
if(!$isMatch) {
$timeline[$day] = [$day];
}
}
# echo '<pre>'; var_dump($timeline); echo '</pre>';
# Create beautiful keys from $timeline, i.e. [Mo, Tu, We] => Mo-We
$result = array();
foreach( $timeline as $refDay => $days ) {
# TODO: respect l18n in date function
if(count($days) > 2) {
$result[date_i18n('D', $days[0]).'-'.date_i18n('D', end($days))] = $buckets[$refDay];
} else {
$result[join(', ', array_map(function($v) { return date_i18n('D', $v); }, $days))] = $buckets[$refDay];
}
}
return $result;
}
/*
Add long description and cover image settings for blogs. These will be
displayed on the respective front pages as well as on the network front
page's sub site selection.
TODO: Maybe it's easier to just use a plugin for this. For example:
https://github.com/CMB2/CMB2
https://metabox.io/
*/
function long_description_callback() {
$option = get_option('blog_long_description');
echo "<textarea id='blog_long_description' name='blog_long_description' ".
"rows='5' cols='80'>{$option}</textarea>";
}
function cover_image_callback() {
$option = get_option('blog_cover_image');
if($option) {
$img = wp_get_attachment_image($option);
echo "<div>{$img}</div>";
}
echo "<input id='blog_cover_image' name='blog_cover_image' size='40' type='text' value='{$option}' />";
if (!function_exists('media_buttons')) {
include(ABSPATH . 'wp-admin/includes/media.php');
}
echo '<span id="blog-cover-image-media-buttons" class="wp-media-buttons">';
/**
* Fires after the default media button(s) are displayed.
*
* @since 2.5.0
*
* @param string $editor_id Unique editor identifier, e.g. 'content'.
*/
do_action('media_buttons', 'blog_cover_image');
echo "</span>\n";
}
function cover_image_filter($html, $id, $attachment) {
if($_POST && array_key_exists('post_id', $_POST) && $_POST['post_id'] == 0) {
// The post_id will be zero if we are not editing a real post. We assume,
// this means that we are in our settings field for the cover image.
// TODO: see
// - https://github.com/WordPress/WordPress/blob/master/wp-admin/custom-background.php
// - https://github.com/WordPress/WordPress/blob/master/wp-admin/js/custom-background.js
// on how to use the media library in the "right" way.
return $id;
} else {
return $html;
}
}
add_filter('media_send_to_editor', 'cover_image_filter', 10, 3);
function init_blog_settings() {
// Add fields with name and function to use for our extra settings
add_settings_field(
'blog_long_description',
'Längere Beschreibung',
'long_description_callback',
'general' );
add_settings_field(
'blog_cover_image',
'Cover Bild',
'cover_image_callback',
'general' );
// Register our setting so that $_POST handling is done for us and our
// callback function just has to echo the form HTML
register_setting('general', 'blog_long_description');
register_setting('general', 'blog_cover_image');
}
add_action('admin_init', 'init_blog_settings');
// TODO: maybe use a transient to save the result
// See also https://github.com/wp-plugins/network-summary/blob/master/includes/class-network-summary.php
function get_posts_for_sites(array $sites, $limit) {
$result = array();
if ( empty( $sites ) ) {
return $result;
}
function sort_by_post_date( $a, $b ) {
return strtotime( $b->post_date_gmt ) - strtotime( $a->post_date_gmt );
}
# By default get_posts() will retrieve the 5 latest posts. In order to get
# correct results, we need to retrieve $limit posts from every blog.
$post_params = array('numberposts' => $limit);
foreach ( $sites as $site ) {
$site = (object)$site;
switch_to_blog( $site->blog_id );
foreach ( get_posts($post_params) as $post ) {
$post->site_id = $site->blog_id;
array_push( $result, $post );
}
restore_current_blog();
usort( $result, 'sort_by_post_date' );
$result = array_slice( $result, 0, $limit );
}
return $result;
}
// Use template multisite_front_page.php if available to display the network's front page!
function front_page_filter($template) {
if(is_main_site() && is_front_page()) {
$main_template = locate_template( array( 'multisite_front_page.php' ) );
if ( '' != $main_template ) {
return $main_template;
}
}
return $template;
}
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include
add_filter('template_include', 'front_page_filter');
# Add two log actions which allow us to temporarely block spammers via fail2ban
# See http://www.scottbrownconsulting.com/2014/09/countering-wordpress-xml-rpc-attacks-with-fail2ban/
function fail2ban_login_failed_hook($username) {
// openlog('wordpress('.$_SERVER['HTTP_HOST'].')', LOG_NDELAY|LOG_PID, LOG_AUTHPRIV);
openlog('wordpress', LOG_NDELAY|LOG_PID, LOG_AUTHPRIV);
syslog(LOG_NOTICE,"Authentication failure for ".$username." from ".$_SERVER['REMOTE_ADDR']);
}
add_action('wp_login_failed', 'fail2ban_login_failed_hook');
function fail2ban_pingback_error_hook($ixr_error) {
if ( $ixr_error->code === 48 ) return $ixr_error; // don't punish duplication
// openlog('wordpress('.$_SERVER['HTTP_HOST'].')', LOG_NDELAY|LOG_PID, LOG_AUTHPRIV);
openlog('wordpress', LOG_NDELAY|LOG_PID, LOG_AUTHPRIV);
syslog(LOG_NOTICE,"Pingback error ".$ixr_error->code." generated from ".$_SERVER['REMOTE_ADDR']);
return $ixr_error;
}
add_filter('xmlrpc_pingback_error', 'fail2ban_pingback_error_hook', 1);
?>