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

twentytwelve.php ➔ twentytwelve_amp_infinite_output()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 31
loc 31
rs 9.424
c 0
b 0
f 0
1
<?php
2
/**
3
 * Infinite Scroll Theme Assets
4
 *
5
 * Register support for Twenty Twelve and enqueue relevant styles.
6
 */
7
8
/**
9
 * Add theme support for infinite scroll
10
 */
11
function jetpack_twentytwelve_infinite_scroll_init() {
12
	add_theme_support( 'infinite-scroll', array(
13
		'container'      => 'content',
14
		'render'         => 'twentytwelve_infinite_scroll_render',
15
		'footer'         => 'page',
16
		'footer_widgets' => jetpack_twentytwelve_has_footer_widgets(),
17
	) );
18
}
19
add_action( 'after_setup_theme', 'jetpack_twentytwelve_infinite_scroll_init' );
20
21
/**
22
 * Needs to be defined so AMP logic kicks in.
23
 */
24
function twentytwelve_infinite_scroll_render() {}
25
26
/**
27
 * Enqueue CSS stylesheet with theme styles for infinity.
28
 */
29 View Code Duplication
function jetpack_twentytwelve_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-twentytwelve', plugins_url( 'twentytwelve.css', __FILE__ ), $dep, '20120817' );
34
	}
35
}
36
add_action( 'wp_enqueue_scripts', 'jetpack_twentytwelve_infinite_scroll_enqueue_styles', 25 );
37
38
/**
39
 * Do we have footer widgets?
40
 */
41 View Code Duplication
function jetpack_twentytwelve_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
	if ( function_exists( 'jetpack_is_mobile' ) && jetpack_is_mobile() ) {
43
		if ( is_front_page() && ( is_active_sidebar( 'sidebar-2' ) || is_active_sidebar( 'sidebar-3' ) ) )
44
			return true;
45
		elseif ( is_active_sidebar( 'sidebar-1' ) )
46
			return true;
47
	}
48
49
	return false;
50
}
51
52
/**
53
 * Load AMP theme specific hooks for infinite scroll.
54
 *
55
 * @return void
56
 */
57
function amp_twentytwelve_infinite_scroll_render_hooks() {
58
	add_filter( 'jetpack_amp_infinite_footers', 'twentytwelve_amp_infinite_footers', 10, 2 );
59
	add_filter( 'jetpack_amp_infinite_output', 'twentytwelve_amp_infinite_output' );
60
	add_filter( 'jetpack_amp_infinite_older_posts', 'twentytwelve_amp_infinite_older_posts' );
61
}
62
63
/**
64
 * Get the theme specific footers.
65
 *
66
 * @param array  $footers The footers of the themes.
67
 * @param string $buffer  Contents of the output buffer.
68
 *
69
 * @return mixed
70
 */
71 View Code Duplication
function twentytwelve_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...
72
	// Collect the sidebar wrapper.
73
	preg_match(
74
		'/<div id="secondary".*<!-- #secondary -->/s',
75
		$buffer,
76
		$footer
77
	);
78
	$footers[] = reset( $footer );
79
80
	// Collect the footer wrapper.
81
	preg_match(
82
		'/<footer id="colophon".*<!-- #colophon -->/s',
83
		$buffer,
84
		$footer
85
	);
86
	$footers[] = reset( $footer );
87
88
	return $footers;
89
}
90
91
/**
92
 * Hide and remove various elements from next page load.
93
 *
94
 * @param string $buffer Contents of the output buffer.
95
 *
96
 * @return string
97
 */
98 View Code Duplication
function twentytwelve_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...
99
	// Hide site header on next page load.
100
	$buffer = preg_replace(
101
		'/<header id="masthead"/',
102
		'$0 next-page-hide',
103
		$buffer
104
	);
105
106
	// Hide below nav bar.
107
	$buffer = preg_replace(
108
		'/<nav id="nav-below"/',
109
		'$0 next-page-hide hidden',
110
		$buffer
111
	);
112
113
	// Remove the sidebar as it will be added back to amp next page footer.
114
	$buffer = preg_replace(
115
		'/<div id="secondary".*<!-- #secondary -->/s',
116
		'',
117
		$buffer
118
	);
119
120
	// Remove the footer as it will be added back to amp next page footer.
121
	$buffer = preg_replace(
122
		'/<footer id="colophon".*<!-- #colophon -->/s',
123
		'',
124
		$buffer
125
	);
126
127
	return $buffer;
128
}
129
130
/**
131
 * Filter the AMP infinite scroll older posts button
132
 *
133
 * @return string
134
 */
135
function twentytwelve_amp_infinite_older_posts() {
136
	ob_start();
137
	?>
138
<div id="infinite-handle" style="padding: 1.714285714rem 0; margin: 0 1.714285714rem;">
139
	<span>
140
		<a href="{{url}}">
141
			<button>
142
				{{title}}
143
			</button>
144
		</a>
145
	</span>
146
</div>
147
	<?php
148
	return ob_get_clean();
149
}
150