Make WordPress Core

Ticket #50145: 50145.diff

File 50145.diff, 3.0 KB (added by SergeyBiryukov, 6 years ago)
  • src/js/_enqueues/admin/site-health.js

     
    6565                goodIssuesWrapper.toggleClass( 'hidden' );
    6666                $( this ).attr( 'aria-expanded', ! goodIssuesWrapper.hasClass( 'hidden' ) );
    6767        } );
     68       
     69        /*
     70         * Object.entries polyfill.
     71         *
     72         * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
     73         */
     74        if ( ! Object.entries ) {
     75                Object.entries = function( obj ) {
     76                        var ownProps = Object.keys( obj ),
     77                                ownValues = Object.values( obj ),
     78                                i = ownProps.length,
     79                                resArray = new Array( i ); // Preallocate the array.
    6880
     81                        while ( i-- ) {
     82                                resArray[ i ] = [ ownProps[ i ], ownValues[ i ] ];
     83                        }
     84
     85                        return resArray;
     86                };
     87        }
     88
    6989        /**
     90         * Validates the Site Health test result format.
     91         *
     92         * @since 5.5.0
     93         *
     94         * @param {Object} issue
     95         *
     96         * @return {boolean}
     97         */
     98        function validateIssueData( issue ) {
     99                // Expected minimum format of a valid SiteHealth test response.
     100                var minimumExpected = {
     101                        test: 'string',
     102                        label: 'string',
     103                        description: 'string'
     104                };
     105                var passed = true;
     106
     107                // Loop over expected data and match the data types.
     108                Object.entries( minimumExpected ).forEach( ( [ key, value ] ) => {
     109                        if ( 'object' === typeof( value ) ) {
     110                                Object.entries( value ).forEach( ( [ subKey, subValue ] ) => {
     111                                        if (
     112                                                'undefined' === typeof( issue[ key ] ) ||
     113                                                'undefined' === typeof( issue[ key ][ subKey ] ) ||
     114                                                subValue !== typeof( issue[ key ][ subKey ] )
     115                                        ) {
     116                                                passed = false;
     117                                        }
     118                                } );
     119                        } else {
     120                                if (
     121                                        'undefined' === typeof( issue[ key ] ) ||
     122                                        value !== typeof( issue[ key ] )
     123                                ) {
     124                                        passed = false;
     125                                }
     126                        }
     127                } );
     128
     129                return passed;
     130        }
     131
     132        /**
    70133         * Appends a new issue to the issue list.
    71134         *
    72135         * @since 5.2.0
     
    79142                        heading,
    80143                        count;
    81144               
     145                /*
     146                 * Validate the issue data format before using it.
     147                 * If the output is invalid, discard it.
     148                 */
     149                if ( ! validateIssueData( issue ) ) {
     150                        return false;
     151                }
     152
    82153                SiteHealth.site_status.issues[ issue.status ]++;
    83154
    84155                count = SiteHealth.site_status.issues[ issue.status ];
  • src/wp-admin/site-health.php

     
    144144        <h4 class="health-check-accordion-heading">
    145145                <button aria-expanded="false" class="health-check-accordion-trigger" aria-controls="health-check-accordion-block-{{ data.test }}" type="button">
    146146                        <span class="title">{{ data.label }}</span>
    147                         <span class="badge {{ data.badge.color }}">{{ data.badge.label }}</span>
     147                        <# if ( data.badge ) { #>
     148                                <span class="badge {{ data.badge.color }}">{{ data.badge.label }}</span>
     149                        <# } #>
    148150                        <span class="icon"></span>
    149151                </button>
    150152        </h4>