-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
200 lines (185 loc) · 7.33 KB
/
Copy pathfunctions.php
File metadata and controls
200 lines (185 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Microposting theme functions.
*
* @package microposting
*
* Copyright 2026 Pablo Postigo
*
* Microposting is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Microposting is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details: https://www.gnu.org/licenses/gpl-3.0.html
*/
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_style(
'microposting-style',
get_parent_theme_file_uri( 'style.css' ),
array(),
wp_get_theme()->get( 'Version' )
);
// Mobile: admin bar is 46px in-flow, scrolls away with the page.
if ( is_admin_bar_showing() ) {
wp_enqueue_script(
'microposting-topbar',
get_parent_theme_file_uri( 'assets/topbar.js' ),
array(),
wp_get_theme()->get( 'Version' ),
true
);
}
} );
add_action( 'after_setup_theme', function () {
add_editor_style( 'style.css' );
} );
/**
* Register every PHP file in /patterns/ as a block pattern.
*
* WordPress is supposed to auto-register these via _register_theme_block_patterns(),
* but that path is unreliable in some hosting environments — registering explicitly
* on init guarantees the patterns are available to templates.
*/
add_action( 'init', function () {
$dir = get_stylesheet_directory() . '/patterns/';
foreach ( (array) glob( $dir . '*.php' ) as $file ) {
$data = get_file_data( $file, array(
'title' => 'Title',
'slug' => 'Slug',
'inserter' => 'Inserter',
) );
if ( empty( $data['slug'] ) || empty( $data['title'] ) ) {
continue;
}
if ( WP_Block_Patterns_Registry::get_instance()->is_registered( $data['slug'] ) ) {
continue;
}
$inserter = ! ( ! empty( $data['inserter'] ) && in_array( strtolower( $data['inserter'] ), array( 'no', 'false' ), true ) );
ob_start();
include $file;
register_block_pattern( $data['slug'], array(
'title' => $data['title'],
'content' => ob_get_clean(),
'inserter' => $inserter,
) );
}
} );
/**
* Wrap pagination next/previous links with wp-element-button classes
* so they look identical to the "See More Posts" button on single posts.
*/
function microposting_wrap_pagination_button( $block_content, $type ) {
if ( empty( trim( $block_content ) ) ) return $block_content;
$block_content = preg_replace(
'/<a ([^>]*)class="([^"]*)' . preg_quote( $type, '/' ) . '([^"]*)"/',
'<a $1class="$2' . $type . ' wp-block-button__link wp-element-button$3"',
$block_content
);
return '<div class="wp-block-button pagination-btn is-style-fill">' . trim( $block_content ) . '</div>';
}
add_filter( 'render_block_core/query-pagination-next', function( $block_content ) {
return microposting_wrap_pagination_button( $block_content, 'wp-block-query-pagination-next' );
}, 10, 1 );
add_filter( 'render_block_core/query-pagination-previous', function( $block_content ) {
return microposting_wrap_pagination_button( $block_content, 'wp-block-query-pagination-previous' );
}, 10, 1 );
/**
* Translate query-pagination labels at render time.
*
* The labels live as English text in templates/index.html and templates/search.html
* because the query-pagination block must be a direct child of wp:query to inherit
* the URL page context — wrapping it in a wp:pattern reference would break that
* inheritance and make the Next link appear on the last page.
*
* Calling __() with a dynamic argument translates correctly at runtime; the
* explicit __() calls below ensure `wp i18n make-pot` picks up the source strings.
*/
add_filter( 'render_block_data', function ( $parsed_block ) {
$paginators = array( 'core/query-pagination-previous', 'core/query-pagination-next' );
if ( in_array( $parsed_block['blockName'] ?? '', $paginators, true ) && ! empty( $parsed_block['attrs']['label'] ) ) {
$parsed_block['attrs']['label'] = __( $parsed_block['attrs']['label'], 'microposting' ); // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
}
return $parsed_block;
} );
// Source strings for the dynamic __() call above — keep in sync with the
// labels in templates/index.html and templates/search.html so make-pot finds them.
if ( false ) {
__( 'See Previous Posts', 'microposting' );
__( 'See Next Posts', 'microposting' );
__( 'See Previous Results', 'microposting' );
__( 'See Next Results', 'microposting' );
}
/**
* Replace the `{search_query}` token in any block content with the current
* search term. Used by templates/search.html's no-results heading so the
* template can read `No results for “{search_query}”` literally — keeping
* the placeholder visible in the editor — and the swap happens at render.
*
* Scoped to is_search() so the token never leaks elsewhere.
*/
add_filter( 'render_block', function ( $block_content, $block ) {
if ( ! is_search() || strpos( $block_content, '{search_query}' ) === false ) {
return $block_content;
}
$term = trim( (string) get_search_query( false ) );
return str_replace( '{search_query}', esc_html( $term ), $block_content );
}, 10, 2 );
/**
* Hide the "See More Posts" buttons block on the search template when the
* current search yielded no results. The template marks the block with the
* `search-see-more-posts` className; this filter checks the main query on
* search pages and returns an empty string when there are zero posts so the
* button only appears alongside actual results.
*/
add_filter( 'render_block_core/buttons', function ( $block_content, $block ) {
$class_name = $block['attrs']['className'] ?? '';
if ( strpos( $class_name, 'search-see-more-posts' ) === false ) {
return $block_content;
}
if ( is_search() ) {
global $wp_query;
if ( empty( $wp_query->posts ) ) {
return '';
}
}
return $block_content;
}, 10, 2 );
/**
* Send wp:avatar links to the homepage instead of the author archive.
*
* The core/avatar block hard-codes `get_author_posts_url()` when isLink is true.
* For a single-author microblog the author archive is redundant with the home
* feed, so we rewrite the href to home_url() on the rendered output.
*/
add_filter( 'render_block_core/avatar', function ( $block_content, $block ) {
if ( empty( $block_content ) || empty( $block['attrs']['isLink'] ) ) {
return $block_content;
}
$processor = new WP_HTML_Tag_Processor( $block_content );
if ( $processor->next_tag( 'a' ) ) {
$processor->set_attribute( 'href', home_url( '/' ) );
return $processor->get_updated_html();
}
return $block_content;
}, 10, 2 );
/**
* Customise the <!--more--> teaser link in feed views.
* Replaces the default "(more…)" span with a translatable "Read More →" label
* and removes the `#more-N` fragment so the link points to the post itself —
* styling then comes from the .wp-block-post-content a rules in style.css,
* identical to any inline link.
*/
add_filter( 'the_content_more_link', function( $link, $more_link_text ) {
$href = get_permalink();
$title = get_the_title();
return sprintf(
'<a href="%1$s" class="more-link" aria-label="%2$s">%3$s</a>',
esc_url( $href ),
esc_attr( sprintf( /* translators: %s: post title */ __( 'Continue reading %s', 'microposting' ), $title ) ),
esc_html__( 'Read More →', 'microposting' )
);
}, 10, 2 );