1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jetpack Compatibility File |
4
|
|
|
* See: https://jetpack.com/ |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
View Code Duplication |
function twentynineteen_jetpack_setup() { |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Add theme support for Infinite Scroll. |
11
|
|
|
*/ |
12
|
|
|
add_theme_support( 'infinite-scroll', array( |
13
|
|
|
'type' => 'click', |
14
|
|
|
'container' => 'main', |
15
|
|
|
'render' => 'twentynineteen_infinite_scroll_render', |
16
|
|
|
'footer' => 'page', |
17
|
|
|
) ); |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* Add theme support for Responsive Videos. |
21
|
|
|
*/ |
22
|
|
|
add_theme_support( 'jetpack-responsive-videos' ); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Add theme support for geo-location. |
26
|
|
|
*/ |
27
|
|
|
add_theme_support( 'jetpack-geo-location' ); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Add theme support for Content Options. |
31
|
|
|
*/ |
32
|
|
|
add_theme_support( 'jetpack-content-options', array( |
33
|
|
|
'blog-display' => array( 'content', 'excerpt' ), |
34
|
|
|
'post-details' => array( |
35
|
|
|
'stylesheet' => 'twentynineteen-style', |
36
|
|
|
'date' => '.posted-on', |
37
|
|
|
'categories' => '.cat-links', |
38
|
|
|
'tags' => '.tags-links', |
39
|
|
|
'author' => '.byline', |
40
|
|
|
'comment' => '.comments-link', |
41
|
|
|
), |
42
|
|
|
'featured-images' => array( |
43
|
|
|
'archive' => true, |
44
|
|
|
'post' => true, |
45
|
|
|
'page' => true, |
46
|
|
|
), |
47
|
|
|
) ); |
48
|
|
|
} |
49
|
|
|
add_action( 'after_setup_theme', 'twentynineteen_jetpack_setup' ); |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Custom render function for Infinite Scroll. |
53
|
|
|
*/ |
54
|
|
|
function twentynineteen_infinite_scroll_render() { |
55
|
|
|
while ( have_posts() ) { |
56
|
|
|
the_post(); |
57
|
|
|
get_template_part( 'template-parts/content/content' ); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
function twentynineteen_init_jetpack() { |
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add our compat CSS file for Infinite Scroll and custom widget stylings and such. |
64
|
|
|
* Set the version equal to filemtime for development builds, and the JETPACK__VERSION for production |
65
|
|
|
* or skip it entirely for wpcom. |
66
|
|
|
*/ |
67
|
|
|
if ( ! is_admin() ) { |
68
|
|
|
$version = false; |
69
|
|
|
if ( method_exists( 'Jetpack', 'is_development_version' ) ) { |
70
|
|
|
$version = Jetpack::is_development_version() ? filemtime( plugin_dir_path( __FILE__ ) . 'twentynineteen.css' ) : JETPACK__VERSION; |
71
|
|
|
} |
72
|
|
|
wp_enqueue_style( 'twentynineteen-jetpack', plugins_url( 'twentynineteen.css', __FILE__ ), array(), $version ); |
73
|
|
|
wp_style_add_data( 'twentynineteen-jetpack', 'rtl', 'replace' ); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
add_action( 'init', 'twentynineteen_init_jetpack' ); |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Alter gallery widget default width. |
80
|
|
|
*/ |
81
|
|
|
function twentynineteen_gallery_widget_content_width( $width ) { |
82
|
|
|
return 390; |
83
|
|
|
} |
84
|
|
|
add_filter( 'gallery_widget_content_width', 'twentynineteen_gallery_widget_content_width' ); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Alter featured-image default visibility for content-options. |
88
|
|
|
*/ |
89
|
|
|
function twentynineteen_override_post_thumbnail( $width ) { |
90
|
|
|
$options = get_theme_support( 'jetpack-content-options' ); |
91
|
|
|
$featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null; |
92
|
|
|
|
93
|
|
|
$settings = array( |
94
|
|
|
'post-default' => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1, |
95
|
|
|
'page-default' => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1, |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$settings = array_merge( $settings, array( |
99
|
|
|
'post-option' => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ), |
100
|
|
|
'page-option' => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ), |
101
|
|
|
) ); |
102
|
|
|
|
103
|
|
|
if ( ( ! $settings['post-option'] && is_single() ) |
104
|
|
|
|| ( ! $settings['page-option'] && is_singular() && is_page() ) ) { |
105
|
|
|
return false; |
106
|
|
|
} else { |
107
|
|
|
return ! post_password_required() && ! is_attachment() && has_post_thumbnail(); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
add_filter( 'twentynineteen_can_show_post_thumbnail', 'twentynineteen_override_post_thumbnail', 10, 2 ); |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Adds custom classes to the array of body classes. |
114
|
|
|
* |
115
|
|
|
* @param array $classes Classes for the body element. |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
function twentynineteen_jetpack_body_classes( $classes ) { |
119
|
|
|
// Adds a class if we're in the Customizer |
120
|
|
|
if ( is_customize_preview() ) : |
121
|
|
|
$classes[] = 'twentynineteen-customizer'; |
122
|
|
|
endif; |
123
|
|
|
|
124
|
|
|
return $classes; |
125
|
|
|
} |
126
|
|
|
add_filter( 'body_class', 'twentynineteen_jetpack_body_classes' ); |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Load AMP theme specific hooks for infinite scroll. |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
function amp_twentynineteen_infinite_scroll_render_hooks() { |
134
|
|
|
add_filter( 'jetpack_amp_infinite_footers', 'twentynineteen_amp_infinite_footers', 10, 2 ); |
135
|
|
|
add_filter( 'jetpack_amp_infinite_output', 'twentynineteen_amp_infinite_output' ); |
136
|
|
|
add_filter( 'jetpack_amp_infinite_older_posts', 'twentynineteen_amp_infinite_older_posts' ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get the theme specific footers. |
141
|
|
|
* |
142
|
|
|
* @param array $footers The footers of the themes. |
143
|
|
|
* @param string $buffer Contents of the output buffer. |
144
|
|
|
* |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
function twentynineteen_amp_infinite_footers( $footers, $buffer ) { |
148
|
|
|
// Collect the footer wrapper. |
149
|
|
|
preg_match( |
150
|
|
|
'/<footer id="colophon".*<!-- #colophon -->/s', |
151
|
|
|
$buffer, |
152
|
|
|
$footer |
153
|
|
|
); |
154
|
|
|
$footers[] = reset( $footer ); |
155
|
|
|
|
156
|
|
|
return $footers; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Hide and remove various elements from next page load. |
161
|
|
|
* |
162
|
|
|
* @param string $buffer Contents of the output buffer. |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
function twentynineteen_amp_infinite_output( $buffer ) { |
167
|
|
|
// Hide site header on next page load. |
168
|
|
|
$buffer = preg_replace( |
169
|
|
|
'/id="masthead"/', |
170
|
|
|
'$0 next-page-hide', |
171
|
|
|
$buffer |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
// Hide pagination on next page load. |
175
|
|
|
$buffer = preg_replace( |
176
|
|
|
'/class=".*navigation pagination.*"/', |
177
|
|
|
'$0 next-page-hide hidden', |
178
|
|
|
$buffer |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
// Remove the footer as it will be added back to amp next page footer. |
182
|
|
|
$buffer = preg_replace( |
183
|
|
|
'/<footer id="colophon".*<!-- #colophon -->/s', |
184
|
|
|
'', |
185
|
|
|
$buffer |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
return $buffer; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Filter the AMP infinite scroll older posts button |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
function twentynineteen_amp_infinite_older_posts() { |
197
|
|
|
ob_start(); |
198
|
|
|
?> |
199
|
|
|
<div id="infinite-handle" style="text-align: center;"> |
200
|
|
|
<span> |
201
|
|
|
<a href="{{url}}"> |
202
|
|
|
<button> |
203
|
|
|
<?php esc_html_e( 'Older posts', 'jetpack' ); ?> |
204
|
|
|
</button> |
205
|
|
|
</a> |
206
|
|
|
</span> |
207
|
|
|
</div> |
208
|
|
|
<?php |
209
|
|
|
return ob_get_clean(); |
210
|
|
|
} |
211
|
|
|
|
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.