App-WordPressTools

 view release on metacpan or  search on metacpan

script/wp-tools  view on Meta::CPAN

    my $bre = shift || '$.*[\]^';
    $str =~ s/([\Q$bre\E])/\\$1/g;
    $str =~ s/\n/\\n/g;
    return $str;
}

# to work around the fact that `tar --transform' can't correctly escape the
# delimiter of a sed substitution expression in the replacement text, this
# subroutine finds a delimiter that can be used safely without escaping
sub _find_sed_delimiter {
    my $str = join('', @_);
    my $bre = '$.*[\]^';
    for my $n (1..255) {
        my $del = pack('C', $n);
        return $del if $str !~ /\Q$del\E/ && $del !~ /[\Q$bre\E]/;
    }
}

sub _run_wpcli {
    my $path    = shift;
    my $command = shift;

    _fix_wp_config($path);
    my $ret = `$wp_cli_path $command`;
    my $status = $?;
    _restore_wp_config();

    $? = $status;
    return $ret;
}

### fix wp-config.php to workaround wpcli bug
#   See https://github.com/wp-cli/wp-cli/issues/1631
sub _fix_wp_config {
    my $path = shift or die 'Path argument required';

    our $config_file    = "$path/wp-config.php";
    our $config_backup  = "$config_file.backup";

    my $config_fileq    = shell_quote($config_file);
    my $config_backupq  = shell_quote($config_backup);

    return if !-e $config_file;

    # make sure we are not generating from an auto-generated config
    my $config_content = `cat $config_fileq`;
    if ($config_content =~ m!WARNING: This config is auto-generated from .* for wpcli compatibility!) {
        return;
        #die "Refusing to generate wpcli-compatible wp-config.php because we already did";
    }

    # copy config to a backup location
    my $err_out = `cp $config_fileq $config_backupq 2>&1`;
    if ($?) {
        die "Could not copy $config_file to $config_backup: $err_out";
    }

    # generate the new config
    open(my $in,  '<', $config_backup) or die "Failed to open (cbf) $config_backup: $!";
    #temporarily set permissions to rw?
    $mode = (stat($config_file))[2] & 0777;
    my $modestr = sprintf qq{%04o}, $mode;
    if ($modestr !~ /^.[67]/) {
        chmod($mode | 0600, $config_file);
    }
    else {
        undef $mode;
    }
    open(my $out, '>', $config_file) or die "Failed to open (cfw) $config_file $!";
    print $out "<?php\n";
    print $out "/* WARNING: This config is auto-generated from $config_backup for wpcli compatibility! */\n";
    while (my $line = <$in>) {
        if ($line =~ /(?:define\((.+?),.+\)\s*;|\$\w+\s*=\s*['"].*['"]\s*;)/) {
            $line = "if (!defined($1)) {$definition_check_string\n$line\n}$definition_check_string\n" if $1;
            print $out $line;
        }
    }
    print $out "require_once(ABSPATH . 'wp-settings.php');\n";
    close($in);
    close($out);

    END {
        _restore_wp_config();
    }
}

sub _restore_wp_config {
    our $config_file;
    our $config_backup;

    # restore original wp-config.php
    if ($config_backup && -e $config_backup) {
        unlink($config_file);
        rename($config_backup, $config_file);
    }
    chmod($mode, $config_file) if $mode;
}



( run in 2.319 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )