-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkntnt-block-editor-defaults.php
169 lines (130 loc) · 5.09 KB
/
kntnt-block-editor-defaults.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
<?php
/**
* @wordpress-plugin
* Plugin Name: Kntnt Block Editor Defaults
* Plugin URI: https://github.com/Kntnt/kntnt-block-editor-defaults
* Description: Provides defaults for featured image, title and excerpt in the block editor.
* Version: 1.0.0
* Requires at least: 5.0
* Requires PHP: 7.4
* Author: Thomas Barregren
* Author URI: https://www.kntnt.com/
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
*/
declare( strict_types=1 );
namespace Kntnt\Block_Editor_Defaults;
defined( 'ABSPATH' ) && new Plugin;
class Plugin {
private array $text_blocks = [ 'core/paragraph' ];
private array $heading_blocks = [ 'core/heading' ];
private array $image_blocks = [ 'core/image', 'core/media-text', 'core/cover' ];
private int $title_max_len = 70;
private bool $trim_excerpt = true;
public function __construct() {
add_filter( 'wp_insert_post_data', $this->apply_filters( ... ), 9 );
add_filter( 'wp_insert_post_data', $this->update_title_on_save( ... ), 10, 2 );
add_filter( 'wp_insert_post_data', $this->update_excerpt_on_save( ... ), 10, 2 );
add_filter( 'wp_insert_post_data', $this->update_featured_image_on_save( ... ), 10, 2 );
}
public function apply_filters( array $data ): array {
$this->text_blocks = apply_filters( 'kntnt-block-editor-defaults-text-blocks', $this->text_blocks );
$this->heading_blocks = apply_filters( 'kntnt-block-editor-defaults-heading-blocks', $this->heading_blocks );
$this->image_blocks = apply_filters( 'kntnt-block-editor-defaults-image-blocks', $this->image_blocks );
$this->title_max_len = apply_filters( 'kntnt-block-editor-defaults-trim-excerpts', $this->title_max_len );
$this->trim_excerpt = apply_filters( 'kntnt-block-editor-defaults-trim-excerpts', $this->trim_excerpt );
return $data;
}
public function update_title_on_save( array $data, array $postarr ): array {
if ( empty( $data['post_title'] ) && isset( $data['post_content'] ) ) {
$blocks = parse_blocks( $data['post_content'] );
[ 'title' => $title ] = $this->find_title_in_blocks( $blocks, [ 'title' => '', 'level' => 8 ] );
if ( $title ) {
$data['post_title'] = $title;
}
}
return $data;
}
public function update_excerpt_on_save( array $data, array $postarr ): array {
if ( empty( $data['post_excerpt'] ) && isset( $data['post_content'] ) ) {
$text = $this->find_excerpt_in_blocks( parse_blocks( $data['post_content'] ) );
if ( $text ) {
if ( $this->trim_excerpt ) {
wp_trim_excerpt( $text );
}
$data['post_excerpt'] = $text;
}
}
return $data;
}
public function update_featured_image_on_save( array $data, array $postarr ): array {
if ( ! has_post_thumbnail( $postarr['ID'] ) && isset( $data['post_content'] ) ) {
$image_id = $this->find_featured_image_in_blocks( parse_blocks( $data['post_content'] ) );
if ( $image_id ) {
set_post_thumbnail( $postarr['ID'], $image_id );
}
}
return $data;
}
private function find_title_in_blocks( array $blocks, array $result ): array {
foreach ( $blocks as $block ) {
if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) ) {
$result = $this->find_title_in_blocks( $block['innerBlocks'], $result );
}
$innerHTML = $block['innerHTML'] ?? '';
$text = trim( wp_strip_all_tags( $innerHTML ) );
if ( empty( $text ) ) {
continue;
}
if ( $result['level'] === 8 && in_array( $block['blockName'], $this->text_blocks, true ) ) {
$result = [
'title' => strlen( $text ) > $this->title_max_len ? substr( $text, 0, $this->title_max_len ) . '…' : $text,
'level' => 7,
];
}
elseif ( in_array( $block['blockName'], $this->heading_blocks, true ) ) {
if ( preg_match( '/<h(?<level>[1-6])/', $innerHTML, $matches ) ) {
$level = (int) $matches['level'];
if ( $level < $result['level'] ) {
$result = [
'title' => $text,
'level' => $level,
];
}
}
}
}
return $result;
}
private function find_excerpt_in_blocks( array $blocks, string $text = '' ): string {
foreach ( $blocks as $block ) {
if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) ) {
$text = $this->find_excerpt_in_blocks( $block['innerBlocks'], $text );
}
if ( in_array( $block['blockName'], $this->text_blocks, true ) ) {
$newText = trim( wp_strip_all_tags( $block['innerHTML'] ?? '' ) );
if ( ! empty( $newText ) ) {
return $newText;
}
}
}
return $text;
}
private function find_featured_image_in_blocks( array $blocks, int $image_id = 0 ): int {
foreach ( $blocks as $block ) {
if ( isset( $block['innerBlocks'] ) && ! empty( $block['innerBlocks'] ) ) {
$image_id = $this->find_featured_image_in_blocks( $block['innerBlocks'], $image_id );
}
if ( in_array( $block['blockName'], $this->image_blocks, true ) ) {
preg_match( '/wp-image-(?<image_id>\d+)/', $block['innerHTML'] ?? '', $matches );
if ( isset( $matches['image_id'] ) ) {
return (int) $matches['image_id'];
}
}
if ( $image_id !== 0 ) {
break;
}
}
return $image_id;
}
}