ShortcodeHooks.php
1.64 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
<?php
namespace WPML\PB\GutenbergCleanup;
use WPML\FP\Fns;
use WPML\FP\Maybe;
use WPML\FP\Obj;
use WPML\LIB\WP\Gutenberg;
use function WPML\FP\partialRight;
use function WPML\FP\pipe;
use function WPML\FP\tap as tap;
class ShortcodeHooks implements \IWPML_Backend_Action {
public function add_hooks() {
add_action(
'wp_insert_post',
Fns::withoutRecursion( Fns::noop(), [ $this, 'removeGutenbergFootprint' ] ),
10, 2
);
}
/**
* @param int $post_ID
* @param \WP_Post|mixed $post
*/
public function removeGutenbergFootprint( $post_ID, $post ) {
// $isWpPost :: mixed -> bool (wpmlcore-8575)
$isWpPost = partialRight( 'is_a', \WP_Post::class );
// $isBuiltWithShortcodes :: \WP_Post -> bool
$isBuiltWithShortcodes = function( \WP_Post $post ) {
/**
* @since WPML 4.4.9
*
* @param bool false
* @param \WP_Post $post
*/
return apply_filters( 'wpml_pb_is_post_built_with_shortcodes', false, $post );
};
// $hasGutenbergMetaData :: \WP_Post -> bool
$hasGutenbergMetaData = pipe(
Obj::prop( 'post_content' ),
Gutenberg::hasBlock()
);
// $removeHtmlComments :: \WP_Post -> \WP_Post
$removeHtmlComments = tap( pipe(
Obj::over( Obj::lensProp( 'post_content' ), Gutenberg::stripBlockData() ),
'wp_update_post'
) );
// $deleteGutenbergPackage :: \WP_Post -> void
$deleteGutenbergPackage = pipe(
Obj::prop( 'ID' ),
[ Package::class, 'get' ],
[ Package::class, 'delete' ]
);
Maybe::of( $post )
->filter( $isWpPost )
->filter( $isBuiltWithShortcodes )
->filter( $hasGutenbergMetaData )
->map( $removeHtmlComments )
->map( $deleteGutenbergPackage );
}
}