This repository has been archived by the owner on Jul 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
492 lines (412 loc) · 13.9 KB
/
functions.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
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
<?php
/**
* @version 2.0
*/
class DS_wpGrafie_Theme {
/**
* The theme slug.
*
* @since 2.0
*
* @var string
*/
protected static $themeslug = 'wpgrafie-v2';
/**
* Debug flag
*
* @since 2.0
*
* @var boolean
*/
protected static $dev;
/**
* Additional classes which will be autoloaded.
*
* @since 2.0
*
* @var array
*/
private static $classes = array(
// Theme related
'DS_wpGrafie_Theme_Widgets',
'DS_wpGrafie_Comments',
'DS_wpGrafie_Theme_Script_Styles',
// Frontend stuff
'DS_wpGrafie_Plugin',
'DS_wpGrafie_Rewrite',
'DS_wpGrafie_Minify',
'DS_WP_Breadcrumb',
// Backend stuff
'DS_wpGrafie_WP_Planet_Feed',
'DS_wpGrafie_Theme_Schnipsel',
);
/**
* Initialize the main class.
*
* @since 0.1
*
* @return void
*/
public static function init() {
self::$dev = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
spl_autoload_register( array( __CLASS__, 'autoload' ) );
// Register actions
add_action( 'after_setup_theme', array( __CLASS__, 'init_actions' ) );
// Remove some actions
add_action( 'init', array( __CLASS__, 'remove_actions' ), 9999 );
// Register menus
add_action( 'after_setup_theme', array( __CLASS__, 'init_menus' ) );
// Register post images
add_action( 'after_setup_theme', array( __CLASS__, 'init_post_thumbnails' ) );
// Register post formats
add_action( 'after_setup_theme', array( __CLASS__, 'init_post_formats' ) );
// Register filters
add_action( 'after_setup_theme', array( __CLASS__, 'init_filters' ) );
}
/**
* Initialize post formats.
*
* @since 1.0
*
* @return void
*/
public static function init_post_formats() {
add_theme_support( 'post-formats', array(
'aside',
'gallery',
'image',
'link',
'quote',
'status',
'video',
) );
}
public static function init_actions() {
// Register the custom post type
add_action( 'init', array( 'DS_wpGrafie_Theme_Schnipsel', 'init' ), 1 );
// Register pagination base
add_action( 'init', array( 'DS_wpGrafie_Rewrite', 'set_pagination_base' ) );
// Register script and styles
add_action( 'wp_enqueue_scripts', array( 'DS_wpGrafie_Theme_Script_Styles', 'init_scripts' ) );
add_action( 'wp_enqueue_scripts', array( 'DS_wpGrafie_Theme_Script_Styles', 'init_styles' ) );
// Register the WP Planet Feed
add_action( 'init', array( 'DS_wpGrafie_WP_Planet_Feed', 'init') );
add_action( 'wp_head', array( __CLASS__, 'social_meta' ) );
}
public static function remove_actions() {
/* Clean up */
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'locale_stylesheet' );
remove_action( 'wp_head', 'noindex', 1 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action( 'wp_head', 'rel_canonical' );
// Facebook
remove_action( 'wp_head', 'fb_add_og_protocol' );
remove_action( 'wp_footer', 'fb_root' );
remove_action( 'wp_head', 'fb_js_sdk_setup' );
remove_action( 'wp_enqueue_scripts', 'fb_style' );
}
/**
* Initialize actions and filters.
*
* @access public
* @static
* @return void
*/
public static function init_filters() {
// Filter some queries
add_filter( 'pre_get_posts', array( __CLASS__, 'search_query' ) );
add_filter( 'pre_get_posts', array( __CLASS__, 'include_post_type_in_feed' ) );
// Add a custom class for the comment status
add_filter( 'comment_class', array( __CLASS__, 'add_custom_comment_class' ) );
// Be friendly
add_filter( 'get_comment_author_link', array( __CLASS__, 'remove_nofollow_from_comment_author_link' ) );
// More user info
add_filter( 'user_contactmethods', array( __CLASS__, 'user_contactmethods' ) );
// Replace sad HTML with HTML5
add_filter( 'img_caption_shortcode', array( __CLASS__, 'img_caption_shortcode' ), 10, 3 );
// add_filter( 'image_send_to_editor', array( __CLASS__, 'image_send_to_editor' ), 10, 8 );
// Remove p-tag around an image
add_filter( 'the_content', array( __CLASS__, 'unautop_for_images' ), 99 );
add_filter( 'post_class', array( __CLASS__, 'remove_hentry_microformat' ) );
// Init the max width of the content
if ( ! isset( $GLOBALS['content_width'] ) )
$GLOBALS['content_width'] = 714;
}
public static function social_meta() {
if ( is_feed() or is_trackback() )
return;
$meta = '';
$meta .= sprintf( '<meta property="http://ogp.me/ns#locale" content="%s">', 'de_DE' );
$meta .= sprintf( '<meta property="http://ogp.me/ns#site_name" content="%s">', 'wpGrafie.de' );
if( is_singular() ) {
$meta .= sprintf( '<meta property="http://ogp.me/ns#type" content="%s">', 'article' );
$meta .= sprintf( '<meta property="http://ogp.me/ns#url" content="%s">', esc_url( get_permalink() ) );
$meta .= sprintf( '<meta property="http://ogp.me/ns#title" content="%s">', esc_attr( get_the_title() ) ) ;
$meta .= sprintf( '<meta property="http://ogp.me/ns#description" content="%s">', esc_attr( get_post_meta( get_the_ID(), '_wpseo_edit_description', true ) ) );
$meta .= sprintf( '<meta property="http://ogp.me/ns/article#published_time" content="%s">', esc_attr( get_the_date( 'c' ) ) );
$meta .= sprintf( '<meta property="http://ogp.me/ns/article#modified_time" content="%s">', esc_attr( get_the_modified_date( 'c' ) ) );
$category = get_the_category();
if ( ! empty( $category[0] ) )
$meta .= sprintf( '<meta property="http://ogp.me/ns/article#section" content="%s">', esc_attr( $category[0]->cat_name ) );
$tags = get_the_tags();
if ( ! empty( $tags ) )
foreach ( $tags as $tag )
$meta .= sprintf( '<meta property="http://ogp.me/ns/article#tag" content="%s">', esc_attr( $tag->name ) );
if ( has_post_thumbnail() ) {
$image = wp_get_attachment_image_src(
get_post_thumbnail_id()
);
$meta .= sprintf( '<meta property="http://ogp.me/ns#image" content="%s">', esc_url( $image[0] ) );
$meta .= sprintf( '<meta property="http://ogp.me/ns#image:width" content="%s">', esc_attr( $image[1] ) );
$meta .= sprintf( '<meta property="http://ogp.me/ns#image:height" content="%s">', esc_attr( $image[2] ) );
// Google+
$meta .= sprintf( '<meta itemprop="image" content="%s">', esc_url( $image[0] ) ) ;
}
}
if ( ! has_post_thumbnail() || is_home() || is_archive() || is_search() ) {
$meta .= sprintf( '<meta property="http://ogp.me/ns#image" content="%s">', esc_url( get_stylesheet_directory_uri() . '/img/wpgrafie-logo.png' ) );
$meta .= sprintf( '<meta property="http://ogp.me/ns#image:width" content="%s">', '200' );
$meta .= sprintf( '<meta property="http://ogp.me/ns#image:height" content="%s">', '200' );
// Google+
$meta .= sprintf( '<meta itemprop="image" content="%s">', esc_url( get_stylesheet_directory_uri() . '/img/wpgrafie-logo.png' ) );
}
$meta .= sprintf( '<meta property="http://ogp.me/ns/fb#app_id" content="%s">', '177681565672418' );
if ( ! is_singular( 'post', 'schnipsel' ) )
$meta .= '<link rel="author" href="https://plus.google.com/101675293278434581718/">';
echo $meta;
}
public static function remove_hentry_microformat( $classes ) {
return array_diff( $classes, array( 'hentry' ) );
}
/**
* [unautop_for_images description]
* Thx Frank Bueltge. http://bueltge.de/p-tag-bei-bilden-in-wordpress-ersetzen/1306/
* @return [type] [description]
*/
public static function unautop_for_images( $content ) {
if ( is_feed() ) return $content;
$content = preg_replace(
'/<p>\\s*?(<a rel=\"attachment.*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s',
'<figure>$1</figure>',
$content
);
return $content;
}
/**
* Add comment status as a class.
*
* @access public
* @static
* @param mixed $classes
* @param mixed $c (default: null)
* @param mixed $comment_id (default: null)
* @return void
*/
public static function add_custom_comment_class( $classes , $c = null , $comment_id = null) {
if ( is_admin() )
return $classes;
$status = wp_get_comment_status( $comment_id );
$status ? ( $classes[] = 'comment-' . $status ) : '';
return $classes;
}
/**
* Remove rel="nofollow" from comment author link.
*
* @access public
* @static
* @param mixed $html
* @return void
*/
public static function remove_nofollow_from_comment_author_link( $html ) {
$html = str_replace( ' nofollow', '', $html );
return $html;
}
/**
* [search_query description]
* @param [type] $query [description]
* @return [type] [description]
*/
public static function search_query( $query ) {
if ( ! $query->is_search() || is_admin() )
return $query;
$query->set( 'post_type', array( 'post', 'schnipsel' ) );
$query->set( 'posts_per_page', -1 );
$query->set( 'no_found_rows', 1 );
return $query;
}
/**
* [include_post_type_in_feed description]
* @param [type] $query [description]
* @return [type] [description]
*/
public static function include_post_type_in_feed( $query ) {
if ( $query->is_feed( get_default_feed() ) )
$query->set( 'post_type', array( 'post', 'schnipsel' ) );
return $query;
}
public function image_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt ) {
// Not used.
return '<figure>' . $html . '</figure>';
}
public function img_caption_shortcode( $null, $attr, $content ) {
extract( shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr ) );
if ( 1 > (int) $width || empty( $caption ) )
return $content;
if ( $id )
$id = 'id="' . esc_attr( $id ) . '" ';
return '<figure ' . $id . 'class="wp-caption ' . esc_attr( $align ) . '" style="width: ' . (int) $width . 'px">' . do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $caption . '</figcaption></figure>';
}
/**
* Autoload function to load the classes.
*
* @since 1.0
*
* @param string $class Class name.
* @return void
*/
static function autoload( $class ) {
if ( in_array( $class, self::$classes ) ) {
require_once(
sprintf(
'%s/classes/class-%s.php',
get_stylesheet_directory(),
str_replace( '_', '-', strtolower( $class ) )
)
);
}
}
/**
* Initialize custom menus.
*
* @since 1.0
*
* @return void
*/
public static function init_menus() {
register_nav_menus(
array(
'menu_top' => 'Header Navigation',
//'menu_bottom' => 'Footer Navigation'
)
);
add_filter( 'wp_nav_menu_items', array( __CLASS__, 'add_custom_menu_items' ), 10, 2 );
}
/**
* Add custom menu items to header navigation.
*
* @since 1.0
*
* @param string $items Menu items in HTML
* @param object $args Menu arguments
* @return string Updated menu items in HTML
*/
public static function add_custom_menu_items( $items, $args ) {
if ( $args->theme_location != 'menu_top' )
return $items;
// Add RSS feed icon.
$items .=
'<li id="menu-item-feed" class="menu-item menu-item-feed"><a data-icon="r" class="icon" href="/feed/">Feed</a></li>';
return $items;
}
public static function breadcrumb() {
if ( is_home() )
return;
$templates = array(
'before' => '<nav id="breadcrumb-navigation" class="width-960"><ul data-icon="/" class="icon">',
'after' => '</ul></nav>',
'standard' => '<li itemscope itemtype="http://data-vocabulary.org/Breadcrumb">%s<span class="sep"> / </span></li>',
'current' => '<li class="current">%s</li>',
'link' => '<a href="%s" itemprop="url"><span itemprop="title">%s</span></a>'
);
$options = array(
'show_htfpt' => true,
'separator' => ''
);
$breadcrumb = new DS_WP_Breadcrumb( $templates, $options, array(), false );
$breadcrumb->generate();
//if ( ! is_search() )
// $breadcrumb->breadcrumb = array_slice( $breadcrumb->breadcrumb, 0, -1 );
if ( ! empty( $breadcrumb->breadcrumb ) )
echo $breadcrumb->output();
}
/**
* Artikelbilder
*
* @since 0.1
*
* @return void
*/
public static function init_post_thumbnails() {
add_theme_support( 'post-thumbnails' );
add_image_size( 'article-image-big', 960, 192, true );
add_image_size( 'article-image-middle', 635, 127, true );
add_image_size( 'article-image-small', 310, 62, true );
}
/**
* Paging bar.
* Thx Sergej Müller.
*
* @access public
* @static
* @param int $range (default: 4)
* @return void
*/
public static function paging_bar( $range = 4 ) {
/* Init */
$count = @$GLOBALS['wp_query']->max_num_pages;
$page = (int) get_query_var( 'paged' );
$ceil = ceil( $range / 2 );
/* Kein Paging? */
if ( $count <= 1 )
return false;
/* Erste Seite? */
if ( ! $page )
$page = 1;
/* Limit errechnen */
if ( $count > $range ) {
if ( $page <= $range ) {
$min = 1;
$max = $range + 1;
} else if ( $page >= ( $count - $ceil ) ) {
$min = $count - $range;
$max = $count;
} else if ( $page >= $range && $page < ( $count - $ceil ) ) {
$min = $page - $ceil;
$max = $page + $ceil;
}
} else {
$min = 1;
$max = $count;
}
/* Ausgabe der Links */
$links = '';
if ( ! empty( $min ) && ! empty( $max ) )
for($i = $min; $i <= $max; $i++)
$links .= ( $i == $page ) ? sprintf( '<span class="current">%d</span>', $i ) : sprintf( '<a href="%s">%d</a>', get_pagenum_link( $i ), $i );
echo $links;
}
public static function user_contactmethods( $user_contactmethods ) {
$user_contactmethods['googleplus'] = 'Google+';
$user_contactmethods['twitter'] = 'Twitter';
$user_contactmethods['facebook'] = 'Facebook';
unset( $user_contactmethods['yim'] );
unset( $user_contactmethods['jabber'] );
unset( $user_contactmethods['aim'] );
return $user_contactmethods;
}
}
add_action( 'after_setup_theme', array( 'DS_wpGrafie_Theme', 'init' ), 1 );