Completed
Push — feature/infinite-scroll-older-... ( 3c476c )
by
unknown
08:59
created

twentythirteen.php ➔ twentythirteen_amp_infinite_output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 24
loc 24
rs 9.536
c 0
b 0
f 0
1
<?php
2
/**
3
 * Infinite Scroll Theme Assets
4
 *
5
 * Register support for Twenty Thirteen.
6
 */
7
8
/**
9
 * Add theme support for infinite scroll
10
 */
11
function jetpack_twentythirteen_infinite_scroll_init() {
12
	add_theme_support( 'infinite-scroll', array(
13
		'container'      => 'content',
14
		'render'         => 'twentythirteen_infinite_scroll_render',
15
		'footer'         => 'page',
16
		'footer_widgets' => array( 'sidebar-1' ),
17
	) );
18
}
19
add_action( 'after_setup_theme', 'jetpack_twentythirteen_infinite_scroll_init' );
20
21
/**
22
 * Needs to be defined so AMP logic kicks in.
23
 */
24
function twentythirteen_infinite_scroll_render() {}
25
26
/**
27
 * Enqueue CSS stylesheet with theme styles for Infinite Scroll.
28
 */
29
function jetpack_twentythirteen_infinite_scroll_enqueue_styles() {
30
	if ( wp_script_is( 'the-neverending-homepage' ) || class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
31
		$dep = wp_script_is( 'the-neverending-homepage' ) ? array( 'the-neverending-homepage' ) : array();
32
		wp_enqueue_style( 'infinity-twentythirteen', plugins_url( 'twentythirteen.css', __FILE__ ), $dep, '20130409' );
33
	}
34
}
35
add_action( 'wp_enqueue_scripts', 'jetpack_twentythirteen_infinite_scroll_enqueue_styles', 25 );
36
37
/**
38
 * Load AMP theme specific hooks for infinite scroll.
39
 *
40
 * @return void
41
 */
42
function amp_twentythirteen_infinite_scroll_render_hooks() {
43
	add_filter( 'jetpack_amp_infinite_footers', 'twentythirteen_amp_infinite_footers', 10, 2 );
44
	add_filter( 'jetpack_amp_infinite_output', 'twentythirteen_amp_infinite_output' );
45
	add_filter( 'jetpack_amp_infinite_older_posts', 'twentythirteen_amp_infinite_older_posts' );
46
}
47
48
/**
49
 * Get the theme specific footers.
50
 *
51
 * @param array  $footers The footers of the themes.
52
 * @param string $buffer  Contents of the output buffer.
53
 *
54
 * @return mixed
55
 */
56
function twentythirteen_amp_infinite_footers( $footers, $buffer ) {
57
	// Collect the footer wrapper.
58
	preg_match(
59
		'/<footer id="colophon".*<!-- #colophon -->/s',
60
		$buffer,
61
		$footer
62
	);
63
	$footers[] = reset( $footer );
64
65
	return $footers;
66
}
67
68
/**
69
 * Hide and remove various elements from next page load.
70
 *
71
 * @param string $buffer Contents of the output buffer.
72
 *
73
 * @return string
74
 */
75 View Code Duplication
function twentythirteen_amp_infinite_output( $buffer ) {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
	// Hide site header on next page load.
77
	$buffer = preg_replace(
78
		'/<header id="masthead"/',
79
		'$0 next-page-hide',
80
		$buffer
81
	);
82
83
	// Hide below nav bar.
84
	$buffer = preg_replace(
85
		'/<nav class="navigation paging-navigation"/',
86
		'$0 next-page-hide hidden',
87
		$buffer
88
	);
89
90
	// Remove the footer as it will be added back to amp next page footer.
91
	$buffer = preg_replace(
92
		'/<footer id="colophon".*<!-- #colophon -->/s',
93
		'',
94
		$buffer
95
	);
96
97
	return $buffer;
98
}
99
100
/**
101
 * Filter the AMP infinite scroll older posts button
102
 *
103
 * @return string
104
 */
105
function twentythirteen_amp_infinite_older_posts() {
106
	ob_start();
107
	?>
108
<div id="infinite-handle">
109
	<span>
110
		<a href="{{url}}">
111
			<button>
112
				{{title}}
113
			</button>
114
		</a>
115
	</span>
116
</div>
117
	<?php
118
	return ob_get_clean();
119
}
120