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

twentyfourteen.php ➔ twentyfourteen_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 Fourteen.
6
 */
7
8
/**
9
 * Add theme support for infinite scroll
10
 */
11
function jetpack_twentyfourteen_infinite_scroll_init() {
12
	add_theme_support( 'infinite-scroll', array(
13
		'container'      => 'content',
14
		'render'         => 'twentyfourteen_infinite_scroll_render',
15
		'footer'         => 'page',
16
		'footer_widgets' => jetpack_twentyfourteen_has_footer_widgets(),
17
	) );
18
}
19
add_action( 'after_setup_theme', 'jetpack_twentyfourteen_infinite_scroll_init' );
20
21
/**
22
 * Needs to be defined so AMP logic kicks in.
23
 */
24
function twentyfourteen_infinite_scroll_render() {}
25
26
/**
27
 * Switch to the "click to load" type IS with the following cases
28
 * 1. Viewed from iPad and the primary sidebar is active.
29
 * 2. Viewed from mobile and either the primary or the content sidebar is active.
30
 * 3. The footer widget is active.
31
 *
32
 * @return bool
33
 */
34 View Code Duplication
function jetpack_twentyfourteen_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...
35
	if ( function_exists( 'jetpack_is_mobile' ) ) {
36
		if ( ( Jetpack_User_Agent_Info::is_ipad() && is_active_sidebar( 'sidebar-1' ) )
37
			|| ( jetpack_is_mobile( '', true ) && ( is_active_sidebar( 'sidebar-1' ) || is_active_sidebar( 'sidebar-2' ) ) )
38
			|| is_active_sidebar( 'sidebar-3' ) )
39
40
			return true;
41
	}
42
43
	return false;
44
}
45
46
/**
47
 * Enqueue CSS stylesheet with theme styles for Infinite Scroll.
48
 */
49
function jetpack_twentyfourteen_infinite_scroll_enqueue_styles() {
50
	if ( wp_script_is( 'the-neverending-homepage' ) || class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
51
		$dep = wp_script_is( 'the-neverending-homepage' ) ? array( 'the-neverending-homepage' ) : array();
52
		wp_enqueue_style( 'infinity-twentyfourteen', plugins_url( 'twentyfourteen.css', __FILE__ ), $dep, '20131118' );
53
	}
54
}
55
add_action( 'wp_enqueue_scripts', 'jetpack_twentyfourteen_infinite_scroll_enqueue_styles', 25 );
56
57
/**
58
 * Load AMP theme specific hooks for infinite scroll.
59
 *
60
 * @return void
61
 */
62
function amp_twentyfourteen_infinite_scroll_render_hooks() {
63
	add_filter( 'jetpack_amp_infinite_footers', 'twentyfourteen_amp_infinite_footers', 10, 2 );
64
	add_filter( 'jetpack_amp_infinite_output', 'twentyfourteen_amp_infinite_output' );
65
	add_filter( 'jetpack_amp_infinite_older_posts', 'twentyfourteen_amp_infinite_older_posts' );
66
}
67
68
/**
69
 * Get the theme specific footers.
70
 *
71
 * @param array  $footers The footers of the themes.
72
 * @param string $buffer  Contents of the output buffer.
73
 *
74
 * @return mixed
75
 */
76 View Code Duplication
function twentyfourteen_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...
77
	// Collect the sidebar wrapper.
78
	preg_match(
79
		'/<div id="secondary".*<!-- #secondary -->/s',
80
		$buffer,
81
		$footer
82
	);
83
	$footers[] = reset( $footer );
84
85
	// Collect the footer wrapper.
86
	preg_match(
87
		'/<footer id="colophon".*<!-- #colophon -->/s',
88
		$buffer,
89
		$footer
90
	);
91
	$footers[] = reset( $footer );
92
93
	return $footers;
94
}
95
96
/**
97
 * Hide and remove various elements from next page load.
98
 *
99
 * @param string $buffer Contents of the output buffer.
100
 *
101
 * @return string
102
 */
103 View Code Duplication
function twentyfourteen_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...
104
	// Hide site header on next page load.
105
	$buffer = preg_replace(
106
		'/<header id="masthead"/',
107
		'$0 next-page-hide',
108
		$buffer
109
	);
110
111
	// Remove the sidebar as it will be added back to amp next page footer.
112
	$buffer = preg_replace(
113
		'/<div id="secondary".*<!-- #secondary -->/s',
114
		'',
115
		$buffer
116
	);
117
118
	// Hide below nav bar.
119
	$buffer = preg_replace(
120
		'/<nav class="navigation paging-navigation"/',
121
		'$0 next-page-hide hidden',
122
		$buffer
123
	);
124
125
	// Remove the footer as it will be added back to amp next page footer.
126
	$buffer = preg_replace(
127
		'/<footer id="colophon".*<!-- #colophon -->/s',
128
		'',
129
		$buffer
130
	);
131
132
	return $buffer;
133
}
134
135
/**
136
 * Filter the AMP infinite scroll older posts button
137
 *
138
 * @return string
139
 */
140
function twentyfourteen_amp_infinite_older_posts() {
141
	ob_start();
142
	?>
143
<div id="infinite-handle">
144
	<span>
145
		<a href="{{url}}">
146
			<button>
147
				{{title}}
148
			</button>
149
		</a>
150
	</span>
151
</div>
152
	<?php
153
	return ob_get_clean();
154
}
155