-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-duplicates.php
77 lines (62 loc) · 2.41 KB
/
remove-duplicates.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
<?php
namespace Polylang\The_Events_Calendar_Stubs;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
}
function remove_duplicates_and_fix() {
$rel_path = 'the-events-calendar-stubs.php';
$io = new SymfonyStyle( new ArgvInput(), new ConsoleOutput() );
$io->title( 'Replacements in stubs' );
$full_path = __DIR__ . '/' . $rel_path;
if ( ! file_exists( $full_path ) ) {
$io->error( "Failed to locate file $rel_path." );
return;
}
$to_remove = [
'@^\s*/\*\*\s+\*\s+Determine whether a post or content string has blocks\..+/\s*function has_blocks\(.+\)\s*{\s*}\s*$@msU' => [ // `has_blocks()` is a WP function that should be hidden behind `function_exists()`.
'replacement' => '',
'count' => 1,
],
'#@return Context The View current Context instance#' => [ // `use Tribe__Context as Context;`.
'replacement' => '@return \Tribe__Context The View current Context instance',
'count' => 1,
],
'@tad_DI52_Container@' => [ // class alias, see `vendor/the-events-calendar/the-events-calendar/common/src/functions/aliases.php`.
'replacement' => 'TEC\Common\lucatume\DI52\Container',
],
'@tad_DI52_ServiceProvider@' => [ // class alias, see `vendor/the-events-calendar/the-events-calendar/common/src/functions/aliases.php`.
'replacement' => 'TEC\Common\lucatume\DI52\ServiceProvider',
],
];
$replaced = false;
try {
$contents = file_get_contents( $full_path );
if ( ! is_string( $contents ) ) {
$io->error( "Failed to open file $rel_path." );
return;
}
} catch( Exception $e ) {
$io->error( "Failed to open file $rel_path." );
}
foreach ( $to_remove as $pattern => $replacement_atts ) {
$new_contents = preg_replace( $pattern, $replacement_atts['replacement'], $contents, $replacement_atts['count'] ?? -1 );
if ( $new_contents !== $contents && is_string( $new_contents ) ) {
$replaced = true;
$contents = $new_contents;
}
}
if ( ! $replaced ) {
$io->error( "No replacements done in file $rel_path." );
return;
}
$result = file_put_contents( $full_path, $contents );
if ( false === $result ) {
$io->error( "Failed to perform replacements in file $rel_path." );
} else {
$io->success( "Replacements done in file $rel_path." );
}
}
remove_duplicates_and_fix();