|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Infinite Scroll Theme Assets |
|
4
|
|
|
* |
|
5
|
|
|
* Register support for @Twenty Ten and enqueue relevant styles. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Add theme support for infinity scroll |
|
10
|
|
|
*/ |
|
11
|
|
|
function jetpack_twentyten_infinite_scroll_init() { |
|
12
|
|
|
add_theme_support( 'infinite-scroll', array( |
|
13
|
|
|
'container' => 'content', |
|
14
|
|
|
'render' => 'jetpack_twentyten_infinite_scroll_render', |
|
15
|
|
|
'footer' => 'wrapper', |
|
16
|
|
|
'footer_widgets' => jetpack_twentyten_has_footer_widgets(), |
|
17
|
|
|
) ); |
|
18
|
|
|
} |
|
19
|
|
|
add_action( 'init', 'jetpack_twentyten_infinite_scroll_init' ); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Set the code to be rendered on for calling posts, |
|
23
|
|
|
* hooked to template parts when possible. |
|
24
|
|
|
* |
|
25
|
|
|
* Note: must define a loop. |
|
26
|
|
|
*/ |
|
27
|
|
|
function jetpack_twentyten_infinite_scroll_render() { |
|
28
|
|
|
get_template_part( 'loop' ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Enqueue CSS stylesheet with theme styles for infinity. |
|
33
|
|
|
*/ |
|
34
|
|
|
function jetpack_twentyten_infinite_scroll_enqueue_styles() { |
|
35
|
|
|
if ( wp_script_is( 'the-neverending-homepage' ) ) { |
|
36
|
|
|
// Add theme specific styles. |
|
37
|
|
|
wp_enqueue_style( 'infinity-twentyten', plugins_url( 'twentyten.css', __FILE__ ), array( 'the-neverending-homepage' ), '20121002' ); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
add_action( 'wp_enqueue_scripts', 'jetpack_twentyten_infinite_scroll_enqueue_styles', 25 ); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Do we have footer widgets? |
|
44
|
|
|
*/ |
|
45
|
|
|
function jetpack_twentyten_has_footer_widgets() { |
|
46
|
|
|
if ( is_active_sidebar( 'first-footer-widget-area' ) || |
|
47
|
|
|
is_active_sidebar( 'second-footer-widget-area' ) || |
|
48
|
|
|
is_active_sidebar( 'third-footer-widget-area' ) || |
|
49
|
|
|
is_active_sidebar( 'fourth-footer-widget-area' ) ) { |
|
50
|
|
|
|
|
51
|
|
|
return true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Load AMP theme specific hooks for infinite scroll. |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
function amp_jetpack_twentyten_infinite_scroll_render_hooks() { |
|
63
|
|
|
add_filter( 'jetpack_amp_infinite_footers', 'twentyten_amp_infinite_footers', 10, 2 ); |
|
64
|
|
|
add_filter( 'jetpack_amp_infinite_output', 'twentyten_amp_infinite_output' ); |
|
65
|
|
|
add_filter( 'jetpack_amp_infinite_older_posts', 'twentyten_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
|
|
|
function twentyten_amp_infinite_footers( $footers, $buffer ) { |
|
77
|
|
|
// Collect the footer wrapper. |
|
78
|
|
|
preg_match( |
|
79
|
|
|
'/<div id="footer" role="contentinfo".*<!-- #footer -->/s', |
|
80
|
|
|
$buffer, |
|
81
|
|
|
$footer |
|
82
|
|
|
); |
|
83
|
|
|
$footers[] = '<div style="background: #fff; margin: 0 auto; padding: 0 20px; width: 940px;">' . reset( $footer ) . '</div>'; |
|
84
|
|
|
|
|
85
|
|
|
return $footers; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Hide and remove various elements from next page load. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $buffer Contents of the output buffer. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
View Code Duplication |
function twentyten_amp_infinite_output( $buffer ) { |
|
|
|
|
|
|
96
|
|
|
// Hide site header on next page load. |
|
97
|
|
|
$buffer = preg_replace( |
|
98
|
|
|
'/<div id="header"/', |
|
99
|
|
|
'$0 next-page-hide', |
|
100
|
|
|
$buffer |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
// Hide sidebar on next page load. |
|
104
|
|
|
$buffer = preg_replace( |
|
105
|
|
|
'/<div id="primary"/', |
|
106
|
|
|
'$0 next-page-hide', |
|
107
|
|
|
$buffer |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
// Hide pagination on next page load. |
|
111
|
|
|
$buffer = preg_replace( |
|
112
|
|
|
'/<div id="nav-above" class="navigation"/', |
|
113
|
|
|
'$0 next-page-hide hidden', |
|
114
|
|
|
$buffer |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
|
|
$buffer = preg_replace( |
|
118
|
|
|
'/<div id="nav-below" class="navigation"/', |
|
119
|
|
|
'$0 next-page-hide hidden', |
|
120
|
|
|
$buffer |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
// Remove the footer as it will be added back to amp next page footer. |
|
124
|
|
|
$buffer = preg_replace( |
|
125
|
|
|
'/<div id="footer" role="contentinfo".*<!-- #footer -->/s', |
|
126
|
|
|
'', |
|
127
|
|
|
$buffer |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
return $buffer; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Filter the AMP infinite scroll older posts button |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
function twentyten_amp_infinite_older_posts() { |
|
139
|
|
|
ob_start(); |
|
140
|
|
|
?> |
|
141
|
|
|
<div id="infinite-handle" style="background: #fff; margin: 0 auto; padding: 0 20px 20px; width: 940px;"> |
|
142
|
|
|
<span> |
|
143
|
|
|
<a href="{{url}}"> |
|
144
|
|
|
<button> |
|
145
|
|
|
{{title}} |
|
146
|
|
|
</button> |
|
147
|
|
|
</a> |
|
148
|
|
|
</span> |
|
149
|
|
|
</div> |
|
150
|
|
|
<?php |
|
151
|
|
|
return ob_get_clean(); |
|
152
|
|
|
} |
|
153
|
|
|
|
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.