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

twentyseventeen.php ➔ twentyseventeen_amp_infinite_output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38

Duplication

Lines 38
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 38
loc 38
rs 9.312
c 0
b 0
f 0
1
<?php
2
/**
3
 * Infinite Scroll Theme Assets
4
 *
5
 * Register support for Twenty Seventeen.
6
 */
7
8
/**
9
 * Add theme support for infinite scroll
10
 */
11
function jetpack_twentyseventeen_infinite_scroll_init() {
12
	add_theme_support( 'infinite-scroll', array(
13
		'container'      => 'main',
14
		'render'         => 'jetpack_twentyseventeen_infinite_scroll_render',
15
		'footer'         => 'content',
16
		'footer_widgets' => jetpack_twentyseventeen_has_footer_widgets(),
17
	) );
18
}
19
add_action( 'init', 'jetpack_twentyseventeen_infinite_scroll_init' );
20
21
/**
22
 * Custom render function for Infinite Scroll.
23
 */
24 View Code Duplication
function jetpack_twentyseventeen_infinite_scroll_render() {
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...
25
	while ( have_posts() ) {
26
		the_post();
27
		if ( is_search() ) {
28
			get_template_part( 'template-parts/post/content', 'search' );
29
		} else {
30
			get_template_part( 'template-parts/post/content', get_post_format() );
31
		}
32
	}
33
}
34
35
/**
36
 * Custom function to check for the presence of footer widgets or the social links menu
37
 */
38
function jetpack_twentyseventeen_has_footer_widgets() {
39
	if ( is_active_sidebar( 'sidebar-2' ) ||
40
		 is_active_sidebar( 'sidebar-3' ) ||
41
		 has_nav_menu( 'social' ) ) {
42
43
		return true;
44
	}
45
46
	return false;
47
}
48
49
/**
50
 * Enqueue CSS stylesheet with theme styles for Infinite Scroll.
51
 */
52 View Code Duplication
function jetpack_twentyseventeen_infinite_scroll_enqueue_styles() {
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...
53
	if ( wp_script_is( 'the-neverending-homepage' ) || class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
54
		$dep = wp_script_is( 'the-neverending-homepage' ) ? array( 'the-neverending-homepage' ) : array();
55
		wp_enqueue_style( 'infinity-twentyseventeen', plugins_url( 'twentyseventeen.css', __FILE__ ), $dep, '20161219' );
56
		wp_style_add_data( 'infinity-twentyseventeen', 'rtl', 'replace' );
57
	}
58
}
59
add_action( 'wp_enqueue_scripts', 'jetpack_twentyseventeen_infinite_scroll_enqueue_styles', 25 );
60
61
/**
62
 * Load AMP theme specific hooks for infinite scroll.
63
 *
64
 * @return void
65
 */
66
function amp_jetpack_twentyseventeen_infinite_scroll_render_hooks() {
67
	add_filter( 'jetpack_amp_infinite_footers', 'twentyseventeen_amp_infinite_footers', 10, 2 );
68
	add_filter( 'jetpack_amp_infinite_output', 'twentyseventeen_amp_infinite_output' );
69
	add_filter( 'jetpack_amp_infinite_older_posts', 'twentyseventeen_amp_infinite_older_posts' );
70
}
71
72
/**
73
 * Get the theme specific footers.
74
 *
75
 * @param array  $footers The footers of the themes.
76
 * @param string $buffer  Contents of the output buffer.
77
 *
78
 * @return mixed
79
 */
80 View Code Duplication
function twentyseventeen_amp_infinite_footers( $footers, $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...
81
	// Collect the sidebar wrapper.
82
	preg_match(
83
		'/<aside id="secondary".*<!-- #secondary -->/s',
84
		$buffer,
85
		$footer
86
	);
87
	$footers[] = reset( $footer );
88
89
	// Collect the footer wrapper.
90
	preg_match(
91
		'/<footer id="colophon".*<!-- #colophon -->/s',
92
		$buffer,
93
		$footer
94
	);
95
	$footers[] = reset( $footer );
96
97
	return $footers;
98
}
99
100
/**
101
 * Hide and remove various elements from next page load.
102
 *
103
 * @param string $buffer Contents of the output buffer.
104
 *
105
 * @return string
106
 */
107 View Code Duplication
function twentyseventeen_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...
108
	// Hide site header on next page load.
109
	$buffer = preg_replace(
110
		'/<header id="masthead"/',
111
		'$0 next-page-hide',
112
		$buffer
113
	);
114
115
	// Hide skip link.
116
	$buffer = preg_replace(
117
		'/<a class="skip-link screen-reader-text"/',
118
		'$0 next-page-hide hidden',
119
		$buffer
120
	);
121
122
	// Remove the sidebar as it will be added back to amp next page footer.
123
	$buffer = preg_replace(
124
		'/<aside id="secondary".*<!-- #secondary -->/s',
125
		'',
126
		$buffer
127
	);
128
129
	// Hide below nav bar.
130
	$buffer = preg_replace(
131
		'/<nav class="navigation pagination"/',
132
		'$0 next-page-hide hidden',
133
		$buffer
134
	);
135
136
	// Remove the footer as it will be added back to amp next page footer.
137
	$buffer = preg_replace(
138
		'/<footer id="colophon".*<!-- #colophon -->/s',
139
		'',
140
		$buffer
141
	);
142
143
	return $buffer;
144
}
145
146
/**
147
 * Filter the AMP infinite scroll older posts button
148
 *
149
 * @return string
150
 */
151
function twentyseventeen_amp_infinite_older_posts() {
152
	ob_start();
153
	?>
154
<div id="infinite-handle">
155
	<span>
156
		<a href="{{url}}">
157
			<button>
158
				{{title}}
159
			</button>
160
		</a>
161
	</span>
162
</div>
163
	<?php
164
	return ob_get_clean();
165
}
166