Making WordPress.org

Changeset 11033


Ignore:
Timestamp:
06/17/2021 04:36:20 AM (4 years ago)
Author:
dd32
Message:

Theme Directory: Log theme uploads (Successful new themes, Failures, and Updates) to slack #themereview-firehose channel.

See #5702.

File:
1 edited

Legend:

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

    r10931 r11033  
    398398
    399399            if ( ! $result ) {
     400                // Log it to slack.
     401                $this->log_to_slack( 'blocked' );
     402
    400403                /* translators: 1: Theme Check Plugin URL, 2: make.wordpress.org/themes */
    401404                return sprintf( __( 'Your theme has failed the theme check. Please correct the problems with it and upload it again. You can also use the <a href="%1$s">Theme Check Plugin</a> to test your theme before uploading. If you have any questions about this please post them to %2$s.', 'wporg-themes' ),
     
    431434        }
    432435
     436        $this->trac_ticket->id = $ticket_id;
     437
    433438        // Add a or update the Theme Directory entry for this theme.
    434439        $this->create_or_update_theme_post( $ticket_id );
     
    438443
    439444        do_action( 'theme_upload', $this->theme, $this->theme_post );
     445
     446        // Log it to slack.
     447        $this->log_to_slack( 'allowed' );
    440448
    441449        // Initiate a GitHub actions run for the theme.
     
    748756        echo '<div class="notice notice-info"><p>' . __( 'Note: While the automated theme scan is based on the Theme Review Guidelines, it is not a complete review. A successful result from the scan does not guarantee that the theme will pass review. All submitted themes are reviewed manually before approval.', 'wporg-themes' ) . '</p></div>';
    749757
    750         // Override some of the upload checks for child themes.
    751         if ( !! $this->theme->parent() ) {
     758        // Override ALL of the upload checks for child themes.
     759        if ( $this->theme->parent() ) {
    752760            $result = true;
    753761        }
     
    14131421        return $stdout ? end( $output ) : '';
    14141422    }
     1423
     1424    /**
     1425     * Log a Theme Upload to the slack `#themereview-firehose` channel.
     1426     *
     1427     * @param string $status Whether the upload was 'allowed' or 'blocked'.
     1428     */
     1429    public function log_to_slack( $status = 'allowed' ) {
     1430        global $themechecks;
     1431
     1432        if ( ! defined( 'THEME_DIRECTORY_SLACK_WEBHOOK' ) || empty( $themechecks ) ) {
     1433            return;
     1434        }
     1435
     1436        $errors = array(
     1437            'required'    => [],
     1438            'recommended' => [],
     1439            'info'        => [],
     1440        );
     1441        foreach ( $themechecks as $check ) {
     1442            if ( $check instanceof themecheck ) {
     1443                $error = $check->getError();
     1444
     1445                $class = get_class( $check );
     1446
     1447                // Humanize the class name.
     1448                $class = preg_replace( '/([a-z])_?([A-Z][a-z])/', '$1 $2', $class );
     1449
     1450                foreach ( (array) $check->getError() as $e ) {
     1451                    $type = 'unknown';
     1452                    if ( preg_match( '!<span[^>]+(tc-(?P<code>info|required|recommended))!i', $e, $m ) ) {
     1453                        $type = $m['code'];
     1454                    }
     1455
     1456                    // Strip the span.
     1457                    $e = preg_replace( '!<span[^>]+tc-.+</span>:?\s*!i', '', $e );
     1458
     1459                    // First sentence only.
     1460                    if ( false !== ( $pos = strpos( $e, '. ', 10 ) ) ) {
     1461                        $e = substr( $e, 0, $pos + 1 );
     1462                    }
     1463
     1464                    // Strip any remaining tags.
     1465                    $e = wp_kses( $e, [ 'strong' => true, 'code' => true, 'em' => true ] );
     1466
     1467                    // Convert to markdown.
     1468                    $e = preg_replace( '!</?(strong)[^>]*>!i', '*', $e );
     1469                    $e = preg_replace( '!</?(code)[^>]*>!i', '`', $e );
     1470                    $e = preg_replace( '!</?(em)[^>]*>!i', '_', $e );
     1471
     1472                    if ( empty( $errors[ $type ][ $class ] ) ) {
     1473                        $errors[ $type ][ $class ] = [];
     1474                    }
     1475
     1476                    $errors[ $type ][ $class ][] = $e;
     1477                }
     1478            }
     1479        }
     1480
     1481        $blocks = [];
     1482
     1483        // Preamble / header
     1484        if ( $this->theme_post && 'allowed' === $status ) {
     1485            $blocks[] = [
     1486                'type' => 'section',
     1487                'text' => [
     1488                    'type' => 'mrkdwn',
     1489                    'text' => "*Theme Update: <" . get_permalink( $this->theme_post ) . "|{$this->theme_post->post_title}>*"
     1490                ],
     1491                'accessory' => [
     1492                    'type'      => 'image',
     1493                    'alt_text'  => 'Theme Screenshot',
     1494                    'image_url' => "https://themes.svn.wordpress.org/{$this->theme_slug}/{$this->theme->display( 'Version' )}/{$this->theme->screenshot}",
     1495                ],
     1496                'fields' => array_filter( [
     1497                    [
     1498                        'type' => 'mrkdwn',
     1499                        'text' => "*Version:*\n{$this->theme->get('Version')}",
     1500                    ],
     1501                    [
     1502                        'type' => 'mrkdwn',
     1503                        'text' => "*Priority:*\n{$this->trac_ticket->priority}",
     1504                    ],
     1505                    $this->trac_ticket->resolution ? [
     1506                        'type' => 'mrkdwn',
     1507                        'text' => "*Resolution:*\n{$this->trac_ticket->resolution}",
     1508                    ] : null,
     1509                    [
     1510                        'type' => 'mrkdwn',
     1511                        'text' => "*Ticket:*\n<https://themes.trac.wordpress.org/ticket/{$this->trac_ticket->id}|#{$this->trac_ticket->id}>",
     1512                    ]
     1513                ] )
     1514            ];
     1515        } elseif ( $this->theme_post ) {
     1516            $blocks[] = [
     1517                'type' => 'section',
     1518                'text' => [
     1519                    'type' => 'mrkdwn',
     1520                    'text' => "*Theme update blocked for {$this->theme_post->post_title}*"
     1521                ],
     1522            ];
     1523        } else {
     1524            $blocks[] = [
     1525                'type' => 'section',
     1526                'text' => [
     1527                    'type' => 'mrkdwn',
     1528                    'text' => "*New Theme upload blocked for {$this->theme->get('Name')} {$this->theme->get('Version')}*",
     1529                ]
     1530            ];
     1531        }
     1532
     1533        foreach ( $errors as $type => $error_classes ) {
     1534            if ( ! $error_classes ) {
     1535                continue;
     1536            }
     1537
     1538            $blocks[] = [
     1539                'type' => 'header',
     1540                'text' => [
     1541                    'type' => 'plain_text',
     1542                    'text' => 'Theme Check ' . ucwords( $type ),
     1543                ]
     1544            ];
     1545
     1546            foreach ( $error_classes as $class => $errors ) {
     1547                $section = [
     1548                    'type' => 'section',
     1549                    'text' => [
     1550                        'type' => 'mrkdwn',
     1551                        'text' => "*{$class}:*\n" . implode( "\n", $errors ),
     1552                    ]
     1553                ];
     1554
     1555                $blocks[] = $section;
     1556            }
     1557        }
     1558
     1559        require_once API_WPORGPATH . 'includes/slack-config.php';
     1560        $send = new \Dotorg\Slack\Send( THEME_DIRECTORY_SLACK_WEBHOOK );
     1561        $send->add_attachment( [ 'blocks' => $blocks ] );
     1562        $send->set_username( 'allowed' === $status ? 'Theme Upload' : 'Theme Check Blocked' );
     1563        $send->set_icon( ':themes:' );
     1564        $send->send( '#themereview-firehose' );
     1565    }
    14151566}
Note: See TracChangeset for help on using the changeset viewer.