-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
215 lines (195 loc) · 5.57 KB
/
index.php
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/*
Plugin Name: Format enabler
Plugin URI: https://github.com/draganescu/wp-format-enabler
Description: Enables post formats for block themes and adds admin entries for each format.
Version: 1.0
Author: Draganescu Stefan Andrei
Author URI: https://www.andrei.draganescu.info
*/
// Enable post formats for block themes.
function block_theme_post_formats() {
if ( wp_is_block_theme() ) {
add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );
}
}
add_action( 'after_setup_theme', 'block_theme_post_formats' );
// add the has-format class to the editor body if the GET param is set
function block_theme_add_format_class( $classes ) {
if ( ! wp_is_block_theme() ) {
return $classes;
}
if ( isset( $_GET['post_format'] ) ) {
$classes .= ' has-format ';
}
return $classes;
}
add_filter( 'admin_body_class', 'block_theme_add_format_class' );
// enqueue editor style to hide .wp-block-post-title if has-format exists on body
function block_theme_hide_title() {
if ( ! wp_is_block_theme() ) {
return;
}
if ( isset( $_GET['post_format'] ) ) {
wp_enqueue_style( 'block-theme-hide-title', plugin_dir_url( __FILE__ ) . 'hide-title.css' );
}
}
add_action( 'enqueue_block_editor_assets', 'block_theme_hide_title', 1 );
// Add admin entries for each format.
function block_theme_post_format_admin_menu() {
if ( wp_is_block_theme() ) {
foreach ( get_post_format_strings() as $format => $label ) {
if ( $format === 'standard' ) {
continue;
}
add_submenu_page( 'edit.php', 'New ' . $label, 'New ' . $label, 'edit_posts', 'post-new.php?post_format=' . $format );
}
}
}
add_action( 'admin_menu', 'block_theme_post_format_admin_menu' );
// Add a template to the post type object on init
function block_theme_setup_format_block_template() {
if ( ! wp_is_block_theme() ) {
return;
}
// if current page is not the post editor page in admin do nothing
if ( ! is_admin() || ! isset( $_GET['post_format'] ) ) {
return;
}
$template = null;
switch( $_GET['post_format'] ):
case 'aside':
$template = array(
array( 'core/paragraph', array(
'placeholder' => 'Write an aside...',
) ),
);
break;
case 'gallery':
$template = array(
array( 'core/gallery', array(
'columns' => 3,
) ),
);
break;
case 'link':
$template = array(
array( 'core/paragraph', array(
'placeholder' => 'Write a link...',
) ),
);
break;
case 'image':
$template = array(
array( 'core/image', array(
'align' => 'center',
) ),
);
break;
case 'quote':
$template = array(
array( 'core/quote', array(
'placeholder' => 'Write a quote...',
) ),
);
break;
case 'status':
$template = array(
array( 'core/paragraph', array(
'placeholder' => 'Write a status...',
) ),
);
break;
case 'video':
$template = array(
array( 'core/video', array(
'align' => 'center',
) ),
);
break;
case 'audio':
$template = array(
array( 'core/audio', array(
'align' => 'center',
) ),
);
break;
case 'chat':
$template = array(
array( 'core/paragraph', array(
'placeholder' => 'Write a chat...',
) ),
);
break;
endswitch;
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template_lock = 'all';
$post_type_object->template = $template;
add_filter( 'option_default_post_format', function() {
return $_GET['post_format'];
} );
}
add_action( 'init', 'block_theme_setup_format_block_template',20 );
// filter admin post title in listing to be name of format
function block_theme_post_format_admin_title( $title, $post_id ) {
if ( ! wp_is_block_theme() ) {
return $title;
}
$format = get_post_format( $post_id );
if ( $format === false ) {
return $title;
}
return get_post_format_string( $format );
}
add_filter( 'the_title', 'block_theme_post_format_admin_title', 10, 2 );
// Hook into the rendering of the query loop block.
function block_theme_post_format_post_template( $block_content ) {
// get the currently rendered post
$post = get_post();
// get permalink
$permalink = get_permalink( $post );
// get format name of post
$format = get_post_format( $post );
// check if post has any post format
if ( get_post_format( $post ) === false ) {
return $block_content;
}
$blocks = parse_blocks($post->post_content);
$content = '';
foreach ($blocks as $block) {
$content .= render_block( $block );
}
$format_classes = implode( ' ', array(
'wp-format-enabler',
'is-' . $format
) );
return '<div class="'. $format_classes .'">'
. $content . '
<a href="'. $permalink . '">'. __( 'Read more' ) .'</a>
</div>';
}
add_action( 'render_block_core/null', 'block_theme_post_format_post_template' );
// register a dashboard widget to show buttons to create new posts for each format
function block_theme_post_format_dashboard_widget() {
if ( ! wp_is_block_theme() ) {
return;
}
add_meta_box(
'block_theme_post_format_dashboard_widget',
__( 'New Post' ),
'block_theme_post_format_dashboard_widget_content',
'dashboard', 'side', 'high'
);
}
function block_theme_post_format_dashboard_widget_content() {
$formats = get_post_format_strings();
unset( $formats['standard'] );
?>
<div class="post-format-buttons">
<?php foreach ( $formats as $format => $label ): ?>
<a href="<?php echo admin_url( 'post-new.php?post_format=' . $format ); ?>" class="button button-primary"><?php echo $label; ?></a>
<?php endforeach; ?>
</div>
<?php
}
add_action( 'wp_dashboard_setup', 'block_theme_post_format_dashboard_widget' );