-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
536 lines (439 loc) · 17.1 KB
/
functions.php
File metadata and controls
536 lines (439 loc) · 17.1 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
<?php
/**
* Piratpartied WordPress Theme
* @version 1.0
* @author Rickard Andersson <rickard@0x539.se>
*/
/**
* Main class for the theme
* @since 1.0
*/
class Piratpartiet {
/**
* Theme name and directory name
* @var string
*/
private $plugin_name = "piratpartiet";
/**
* URL to the site which holds the remote sidebar
* @var string
*/
private $remote_sidebar_url = 'http://www.piratpartiet.se/';
/**
* Initializes the theme functionality
*/
function __construct() {
// Initializing functions
add_action('init', array( $this, 'init_assets') );
add_action('init', array( $this, 'init_menus') );
add_action('init', array( $this, 'init_sidebars') );
// Enable post thumbnails and set the size to 190x190
add_theme_support('post-thumbnails');
set_post_thumbnail_size( 190, 190 );
// This theme has custom header support, this function will enable it
$this->custom_header_support();
// Change some excerpt settings
add_filter('excerpt_more', array($this, 'excerpt_more'));
add_filter('excerpt_length', array($this, 'excerpt_length'), 999);
add_filter('wp_trim_words', array($this, 'wp_trim_words'), 999, 3);
// Filters for adding the featured image to the rss feed, only applies to sub-blogs. The main blog
// is fetching the content from RSS and the images are already included there.
if ( !class_exists('PP_Ettan')) {
add_filter('the_excerpt_rss', array($this, 'featured_image_rss') );
add_filter('the_content_feed', array($this, 'featured_image_rss') );
}
// Action to fetch remote sidebar
add_action('pp_remote_sidebar', array($this, 'pp_remote_sidebar'));
}
/**
* Fetches and prints the remote sidebar
*/
public function pp_remote_sidebar() {
$sidebar = get_transient('pp_remote_sidebar');
if ($sidebar === false) {
$sidebar = file_get_contents($this->remote_sidebar_url . '?pp_remote_sidebar=1');
// if doctype is in the response, we've probably got the whole page which we don't want
if (preg_match('/<!doctype/i', $sidebar)) {
$sidebar = '';
}
set_transient('pp_remote_sidebar', $sidebar, 60*15);
}
echo $sidebar;
}
/**
* Adds the featured image to the rss feed
* @param $content
* @return string
* @since 1.0
*/
function featured_image_rss($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ) {
$thumbnail = '<figure class="alignleft">';
$thumbnail .= get_the_post_thumbnail( $post->ID, 'thumbnail' );
$thumbnail .= '</figure>';
$content = $thumbnail . $content;
}
return $content;
}
/**
* Attached to the filter excerpt_length
* @param $length
* @return int
* @since 1.0
*/
function excerpt_length( $length ) {
return 85;
}
/**
* Attached to the filter excerpt_more
* @param $more
* @return string
* @since 1.0
*/
function excerpt_more($more) {
global $post;
return '... <a href="'. get_permalink($post->ID) .'">Läs mer</a>';
}
/**
* ATtached to the filter wp_trim_words
*
* @param string $text
* @param string $num_words
* @param string $more
*
* @return string
*/
function wp_trim_words($text, $num_words, $more) {
global $post;
$moreLength = mb_strlen($more);
$moreAdded = mb_substr($text, -1 * $moreLength) == $more;
if (!$moreAdded) {
$text .= ' <a href="'. get_permalink($post->ID) .'">Läs mer</a>';
}
return $text;
}
/**
* Adds custom header support for this theme
* @since 1.0
* @return void
*/
function custom_header_support() {
$args = array(
'default-image' => get_template_directory_uri() . '/images/default_header.png',
'header-text' => false,
'default-text-color' => '',
'width' => 1000,
'height' => 180,
'random-default' => false,
'wp-head-callback' => array( $this, 'custom_header_style' ),
);
add_theme_support('custom-header', $args);
}
/**
* Needed function for the custom header
* @since 1.0
* @return void
*/
function custom_header_style() {
?><style type="text/css">
#banner { background-image: url(<?php header_image(); ?>); }
</style><?php
}
/**
* Initialize the sidebars
* @since 1.0
* @return void
*/
function init_sidebars() {
$args = array(
'name' => 'Sidospalt sektion A',
'id' => 'sidebar-section-a',
'description' => 'Överst i sidospalten, full bredd',
'before_widget' => '<section>',
'after_widget' => '</section>',
'before_title' => '<h1>',
'after_title' => '</h1>'
);
register_sidebar($args);
$args['name'] = 'Sidospalt sektion B';
$args['id'] = 'sidebar-section-b';
$args['description'] = "Mitten av sidospalten, halv bredd, till vänster";
register_sidebar($args);
$args['name'] = "Sidospalt sektion C";
$args['id'] = 'sidebar-section-c';
$args['description'] = "Mitten av sidospalten, halv bredd, till höger";
register_sidebar($args);
$args['name'] = "Sidospalt sektion D";
$args['id'] = 'sidebar-section-d';
$args['description'] = "Underst i sidospalten, full bredd";
register_sidebar($args);
$args['name'] = "Sidfoten (vänster)";
$args['id'] = 'footer-section-a';
$args['description'] = "Vänstra delen av sidfoten";
register_sidebar($args);
$args['name'] = "Sidfoten (mitten)";
$args['id'] = 'footer-section-b';
$args['description'] = "Mellersta delen av sidfoten";
register_sidebar($args);
$args['name'] = "Sidfoten (höger)";
$args['id'] = 'footer-section-c';
$args['description'] = "Högra delen av sidfoten";
register_sidebar($args);
$args['name'] = "Överst i innehållsspalten";
$args['id'] = 'sticky';
$args['description'] = 'Visas under sidans namn/rubrik men ovanför inlägg eller sida';
register_sidebar($args);
}
/**
* Initialize the menus
* @since 1.0
* @return void
*/
function init_menus() {
register_nav_menu('main', __('PP Meny'));
}
/**
* Initialize javascript and stylesheets
* @since 1.0
* @return void
*/
function init_assets() {
// Only enqueue files on the public part of the page
if ( !is_admin() ) {
$protocol = isset($_SERVER['HTTPS']) ? "https" : "http";
wp_enqueue_style($this->plugin_name . '-style', get_bloginfo( 'stylesheet_directory') . "/style.css", array(), 15);
wp_enqueue_script('modernizr', get_bloginfo("stylesheet_directory") . "/js/libs/modernizr-2.5.3.min.js");
// wp_enqueue_script($this->plugin_name . '-plugins', get_bloginfo("stylesheet_directory") . "/js/plugins.js", array('jquery'), false, true);
wp_enqueue_script($this->plugin_name . '-script', get_bloginfo("stylesheet_directory") . "/js/script.js", array('jquery'), false, true);
wp_register_script('flattr', $protocol . '://api.flattr.com/js/0.6/load.js?mode=auto&language=sv_SE&category=text', array(), false, true);
wp_register_script('facebook', $protocol . '://connect.facebook.net/sv_SE/all.js#xfbml=1', array(), false, true);
wp_register_script('gplusone', $protocol . '://apis.google.com/js/plusone.js', array(), false, true);
wp_register_script('twitter', $protocol . '://platform.twitter.com/widgets.js', array(), false, true);
} else if (!class_exists('PP_Ettan')) {
// Don't force images on main site
wp_enqueue_script($this->plugin_name . '-force-image-script', get_bloginfo("stylesheet_directory") . '/js/force-image.js', array('jquery') );
}
}
/**
* Handle requests to send notices about 404 pages
*
* @return bool
* @since 1.0
*/
function handle_404_form() {
$nonce = isset($_POST['_wpnonce']) ? $_POST['_wpnonce'] : false;
if ($nonce && wp_verify_nonce($_POST['_wpnonce'], '_404_notice')) {
$recipients = array(
'rickard.andersson@piratpartiet.se',
'ledningen@piratpartiet.se',
);
$url = $_SERVER['HTTP_ORIGIN'] . $_SERVER['REQUEST_URI'];
$host = $_SERVER['HTTP_HOST'];
$subject = 'Anmälan om sida som inte kunde hittas';
$template = 'En besökare på %s har anmält en sida som inte kunde hittas. <br><br>Adressen som besökaren försökte nå var: <a href="%s">%s</a>.<br><br>Mvh<br>Internet';
$headers = 'Content-Type:text/html';
$message = sprintf($template, $host, $url, $url);
@wp_mail($recipients, $subject, $message, $headers);
return true;
}
return false;
}
/**
* Wrapper function for $wp_query->max_num_pages when using PP ettan
* @static
* @return int
* @since 1.0
*/
static function get_max_num_pages() {
global $wp_query, $ettan;
$ppp = get_option('posts_per_page');
if ( class_exists('PP_Ettan') ) {
return intval( ceil( $ettan->get_max_posts_count() / $ppp ) );
} else {
return $wp_query->max_num_pages;
}
}
}
// Load the theme
$pp = new Piratpartiet();
/**
* Get the swedish month name
* @param int $month
* @return string
* @since 1.0
*/
function month_sv($month) {
$month = intval($month);
$m = array(null, "Jan", "Feb", "Mar", "Apr", "Maj", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dec");
return $m[ $month ];
}
/**
* HTML comment list class.
*
* @package WordPress
* @uses Walker
* @since 2.7.0
*/
class Walker_Comment_HTML5 extends Walker_Comment {
/**
* @see Walker::start_lvl()
* @since 2.7.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of comment.
* @param array $args Uses 'style' argument for type of HTML list.
*/
function start_lvl(&$output, $depth, $args) {
$GLOBALS['comment_depth'] = $depth + 1;
}
/**
* @see Walker::end_lvl()
* @since 2.7.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of comment.
* @param array $args Will only append content if style argument value is 'ol' or 'ul'.
*/
function end_lvl(&$output, $depth, $args) {
$GLOBALS['comment_depth'] = $depth + 1;
}
/**
* @see Walker::start_el()
* @since 2.7.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $comment Comment data object.
* @param int $depth Depth of comment in reference to parents.
* @param array $args
*/
function start_el(&$output, $comment, $depth, $args) {
$depth++;
$GLOBALS['comment_depth'] = $depth;
if ( !empty($args['callback']) ) {
call_user_func($args['callback'], $comment, $args, $depth);
return;
}
$GLOBALS['comment'] = $comment;
extract($args, EXTR_SKIP);
?>
<article <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
<header>
<span class="comment-author vcard">
<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
<?php printf('<cite class="fn">%s</cite> <span class="says">skrev</span>', get_comment_author_link()) ?>
</span>
<span class="comment-meta commentmetadata">
<a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><time class="published updated" pubdate datetime="<?php echo date("c", strtotime( get_comment_date() . " " . get_comment_time() ) )?>"><?php printf('den %1$s kl. %2$s', get_comment_date("j/n Y"), get_comment_time()) ?></time></a>:
<?php edit_comment_link(__('(Edit)'),' ','' ); ?>
</span>
<?php if ($comment->comment_approved == '0') : ?>
<em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
<br />
<?php endif; ?>
</header>
<?php comment_text() ?>
<footer>
<?php comment_reply_link(array_merge( $args, array('add_below' => 'comment', 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</footer>
<?php
}
/**
* @see Walker::end_el()
* @since 2.7.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $comment
* @param int $depth Depth of comment.
* @param array $args
*/
function end_el(&$output, $comment, $depth, $args) {
if ( !empty($args['end-callback']) ) {
call_user_func($args['end-callback'], $comment, $args, $depth);
return;
}
echo '</article>';
}
}
/**
* Improved comment form
* @param array $args
* @param int $post_id
* @since 1.0
* @return void
*/
function pp_comment_form( $args = array(), $post_id = null ) {
global $id;
if ( null === $post_id )
$post_id = $id;
else
$id = $post_id;
$commenter = wp_get_current_commenter();
$user = wp_get_current_user();
$user_identity = ! empty( $user->ID ) ? $user->display_name : '';
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields = array(
'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ' '. ( $req ? '<span class="required">*</span>' : '' ) .'</label> ' .
'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>',
'email' => '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ' ' . ( $req ? '<span class="required">*</span>' : '' ) . '</label> ' .
'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>',
'url' => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label>' .
'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>',
);
$required_text = sprintf( ' ' . __('Required fields are marked %s'), '<span class="required">*</span>' );
$defaults = array(
'fields' => apply_filters( 'comment_form_default_fields', $fields ),
'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>',
'must_log_in' => '<p class="must-log-in">' . sprintf( __( 'You must be <a href="%s">logged in</a> to post a comment.' ), wp_login_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
'logged_in_as' => '<p class="logged-in-as">' . sprintf( __( 'Logged in as <a href="%1$s">%2$s</a>. <a href="%3$s" title="Log out of this account">Log out?</a>' ), admin_url( 'profile.php' ), $user_identity, wp_logout_url( apply_filters( 'the_permalink', get_permalink( $post_id ) ) ) ) . '</p>',
'comment_notes_before' => '<p class="comment-notes">' . __( 'Your email address will not be published.' ) . ( $req ? $required_text : '' ) . '</p>',
'comment_notes_after' => '<p class="form-allowed-tags">' . sprintf( __( 'You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: %s' ), ' <code>' . allowed_tags() . '</code>' ) . '</p>',
'id_form' => 'commentform',
'id_submit' => 'submit',
'title_reply' => __( 'Leave a Reply' ),
'title_reply_to' => __( 'Leave a Reply to %s' ),
'cancel_reply_link' => __( 'Cancel reply' ),
'label_submit' => __( 'Post Comment' ),
);
$args = wp_parse_args( $args, apply_filters( 'comment_form_defaults', $defaults ) );
?>
<?php if ( comments_open() ) : ?>
<?php do_action( 'comment_form_before' ); ?>
<div id="respond">
<h2 id="reply-title"><?php comment_form_title( $args['title_reply'], $args['title_reply_to'] ); ?> <small><?php cancel_comment_reply_link( $args['cancel_reply_link'] ); ?></small></h2>
<?php if ( get_option( 'comment_registration' ) && !is_user_logged_in() ) : ?>
<?php echo $args['must_log_in']; ?>
<?php do_action( 'comment_form_must_log_in_after' ); ?>
<?php else : ?>
<form role="form" action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post" id="<?php echo esc_attr( $args['id_form'] ); ?>">
<?php do_action( 'comment_form_top' ); ?>
<?php if ( is_user_logged_in() ) : ?>
<?php echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity ); ?>
<?php do_action( 'comment_form_logged_in_after', $commenter, $user_identity ); ?>
<?php else : ?>
<?php echo $args['comment_notes_before']; ?>
<?php
do_action( 'comment_form_before_fields' );
foreach ( (array) $args['fields'] as $name => $field ) {
echo apply_filters( "comment_form_field_{$name}", $field ) . "\n";
}
do_action( 'comment_form_after_fields' );
?>
<?php endif; ?>
<?php echo apply_filters( 'comment_form_field_comment', $args['comment_field'] ); ?>
<?php echo $args['comment_notes_after']; ?>
<p class="form-submit">
<input name="submit" type="submit" id="<?php echo esc_attr( $args['id_submit'] ); ?>" value="<?php echo esc_attr( $args['label_submit'] ); ?>" />
<?php comment_id_fields( $post_id ); ?>
</p>
<?php do_action( 'comment_form', $post_id ); ?>
</form>
<?php endif; ?>
</div><!-- #respond -->
<?php do_action( 'comment_form_after' ); ?>
<?php else : ?>
<?php do_action( 'comment_form_comments_closed' ); ?>
<?php endif; ?>
<?php
}