Making WordPress.org

Changeset 11230


Ignore:
Timestamp:
09/14/2021 06:38:51 AM (5 years ago)
Author:
dd32
Message:

Theme Directory: Move exec_with_notify() to a trait for use in other classes too.

See #5899.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/class-wporg-themes-upload.php

    r11229 r11230  
    11<?php
    22use WordPressdotorg\Theme_Directory\Lib\GitHub;
     3use WordPressdotorg\Theme_Directory\Lib\Exec_With_Logging;
    34
    45/**
     
    89 */
    910class WPORG_Themes_Upload {
     11    use Exec_With_Logging {
     12        Exec_With_Logging::exec as exec_with_notify;
     13    }
     14
    1015    /**
    1116     * Path to `svn` script.
     
    16231628    protected function sort_by_string_length( $a, $b ) {
    16241629        return strlen( $b ) - strlen( $a );
    1625     }
    1626 
    1627     /**
    1628      * Execute a shell process, same behaviour as `exec()` but with PHP Warnings/Notices generated on errors.
    1629      *
    1630      * @param string $command    Command to execute. Escape it.
    1631      * @param array  $output     Array to append program output to. Passed by reference.
    1632      * @param int    $return_var The commands return value. Passed by reference.
    1633      *
    1634      * @return false|string False on failure, last line of output on success, as per exec().
    1635      */
    1636     public function exec_with_notify( $command, &$output = null, &$return_var = null ) {
    1637         $proc = proc_open(
    1638             $command,
    1639             [
    1640                 1 => [ 'pipe', 'w' ], // STDOUT
    1641                 2 => [ 'pipe', 'w' ], // STDERR
    1642             ],
    1643             $pipes
    1644         );
    1645 
    1646         $stdout = stream_get_contents( $pipes[1] );
    1647         $stderr = stream_get_contents( $pipes[2] );
    1648         fclose( $pipes[1] );
    1649         fclose( $pipes[2] );
    1650 
    1651         $return_var = proc_close( $proc );
    1652 
    1653         // Append to $output, as `exec()` does.
    1654         if ( ! is_array( $output ) ) {
    1655             $output = [];
    1656         }
    1657         if ( $stdout ) {
    1658             $output = array_merge( $output, explode( "\n", rtrim( $stdout, "\r\n" ) ) );
    1659         }
    1660 
    1661         // Redact any passwords that might be in a command and included in logged errors.
    1662         $command = str_replace( [ THEME_TRACBOT_PASSWORD, THEME_DROPBOX_PASSWORD ], '[redacted]', $command );
    1663 
    1664         if ( $return_var > 0 ) {
    1665             trigger_error(
    1666                 "Command failed, `{$command}`\n" .
    1667                     "```\n" .
    1668                     "Return Value: {$return_var}\n" .
    1669                     "STDOUT: {$stdout}\n" .
    1670                     "STDERR: {$stderr}\n" .
    1671                     "```",
    1672                 E_USER_WARNING
    1673             );
    1674         } elseif ( $stderr ) {
    1675             trigger_error(
    1676                 "Command produced errors, `{$command}`\n" .
    1677                     "```\n" .
    1678                     "Return Value: {$return_var}\n" .
    1679                     "STDOUT: {$stdout}\n" .
    1680                     "STDERR: {$stderr}\n" .
    1681                     "```",
    1682                 E_USER_NOTICE
    1683             );
    1684         }
    1685 
    1686         // Execution failed.
    1687         if ( $return_var > 0 ) {
    1688             return false;
    1689         }
    1690 
    1691         // Successful, return the last output line.
    1692         return $stdout ? end( $output ) : '';
    16931630    }
    16941631
Note: See TracChangeset for help on using the changeset viewer.