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

twentyeleven.php ➔ twentyeleven_infinite_scroll_render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Infinite Scroll Theme Assets
4
 *
5
 * Register support for @Twenty Eleven and enqueue relevant styles.
6
 */
7
8
/**
9
 * Add theme support for infinity scroll
10
 */
11
function jetpack_twentyeleven_infinite_scroll_init() {
12
	add_theme_support( 'infinite-scroll', array(
13
		'container'      => 'content',
14
		'render'         => 'twentyeleven_infinite_scroll_render',
15
		'footer'         => 'page',
16
		'footer_widgets' => jetpack_twentyeleven_has_footer_widgets(),
17
	) );
18
}
19
add_action( 'init', 'jetpack_twentyeleven_infinite_scroll_init' );
20
21
/**
22
 * Needs to be defined so AMP logic kicks in.
23
 */
24
function twentyeleven_infinite_scroll_render() {}
25
26
/**
27
 * Enqueue CSS stylesheet with theme styles for infinity.
28
 */
29 View Code Duplication
function jetpack_twentyeleven_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...
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
		// Add theme specific styles.
33
		wp_enqueue_style( 'infinity-twentyeleven', plugins_url( 'twentyeleven.css', __FILE__ ), $dep, '20121002' );
34
	}
35
}
36
add_action( 'wp_enqueue_scripts', 'jetpack_twentyeleven_infinite_scroll_enqueue_styles', 25 );
37
38
/**
39
 * Do we have footer widgets?
40
 */
41 View Code Duplication
function jetpack_twentyeleven_has_footer_widgets() {
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...
42
	// Are any of the "Footer Area" sidebars active?
43
	if ( is_active_sidebar( 'sidebar-3' ) || is_active_sidebar( 'sidebar-4' ) || is_active_sidebar( 'sidebar-5' ) )
44
		return true;
45
46
	// If we're on mobile and the Main Sidebar has widgets, it falls below the content, so we have footer widgets.
47
	if ( function_exists( 'jetpack_is_mobile' ) && jetpack_is_mobile() && is_active_sidebar( 'sidebar-1' ) )
48
		return true;
49
50
	return false;
51
}
52
53
/**
54
 * Load AMP theme specific hooks for infinite scroll.
55
 *
56
 * @return void
57
 */
58
function amp_twentyeleven_infinite_scroll_render_hooks() {
59
	add_filter( 'jetpack_amp_infinite_footers', 'twentyeleven_amp_infinite_footers', 10, 2 );
60
	add_filter( 'jetpack_amp_infinite_output', 'twentyeleven_amp_infinite_output' );
61
	add_filter( 'jetpack_amp_infinite_older_posts', 'twentyeleven_amp_infinite_older_posts' );
62
}
63
64
/**
65
 * Get the theme specific footers.
66
 *
67
 * @param array  $footers The footers of the themes.
68
 * @param string $buffer  Contents of the output buffer.
69
 *
70
 * @return mixed
71
 */
72 View Code Duplication
function twentyeleven_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...
73
	// Collect the sidebar wrapper.
74
	preg_match(
75
		'/<div id="secondary".*<!-- #secondary .widget-area -->/s',
76
		$buffer,
77
		$footer
78
	);
79
	$footers[] = reset( $footer );
80
81
	// Collect the footer wrapper.
82
	preg_match(
83
		'/<footer id="colophon".*<!-- #colophon -->/s',
84
		$buffer,
85
		$footer
86
	);
87
	$footers[] = reset( $footer );
88
89
	return $footers;
90
}
91
92
/**
93
 * Hide and remove various elements from next page load.
94
 *
95
 * @param string $buffer Contents of the output buffer.
96
 *
97
 * @return string
98
 */
99
function twentyeleven_amp_infinite_output( $buffer ) {
100
	// Hide site header on next page load.
101
	$buffer = preg_replace(
102
		'/<header id="branding"/',
103
		'$0 next-page-hide',
104
		$buffer
105
	);
106
107
	// Hide skip links on next page load.
108
	$buffer = preg_replace(
109
		'/<div class="skip-link"/',
110
		'$0 next-page-hide',
111
		$buffer
112
	);
113
114
	// Hide pagination on next page load.
115
	$buffer = preg_replace(
116
		'/<nav id="nav-above"/',
117
		'$0 next-page-hide hidden',
118
		$buffer
119
	);
120
121
	$buffer = preg_replace(
122
		'/<nav id="nav-below"/',
123
		'$0 next-page-hide hidden',
124
		$buffer
125
	);
126
127
	// Remove the sidebar as it will be added back to amp next page footer.
128
	$buffer = preg_replace(
129
		'/<div id="secondary".*<!-- #secondary .widget-area -->/s',
130
		'',
131
		$buffer
132
	);
133
134
	// Remove the footer as it will be added back to amp next page footer.
135
	$buffer = preg_replace(
136
		'/<footer id="colophon".*<!-- #colophon -->/s',
137
		'',
138
		$buffer
139
	);
140
141
	return $buffer;
142
}
143
144
/**
145
 * Filter the AMP infinite scroll older posts button
146
 *
147
 * @return string
148
 */
149
function twentyeleven_amp_infinite_older_posts() {
150
	ob_start();
151
	?>
152
<div id="infinite-handle" style="background-color: #fff; padding: 1em 7.6%">
153
	<span>
154
		<a href="{{url}}">
155
			<button>
156
				{{title}}
157
			</button>
158
		</a>
159
	</span>
160
</div>
161
	<?php
162
	return ob_get_clean();
163
}
164