<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[39137] trunk: Customize: Combine Ajax requests for initial load of available nav menu items into a single request.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/39137">39137</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/39137","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-04 06:03:51 +0000 (Fri, 04 Nov 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Customize: Combine Ajax requests for initial load of available nav menu items into a single request.

When there are many post types registered, prevent Ajax requests from piling up and slamming WordPress with concurrent requests.

Props curdin, westonruter.
Fixes <a href="https://core.trac.wordpress.org/ticket/36697">#36697</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizenavmenusjs">trunk/src/wp-admin/js/customize-nav-menus.js</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizenavmenusphp">trunk/src/wp-includes/class-wp-customize-nav-menus.php</a></li>
<li><a href="#trunktestsphpunittestsajaxCustomizeMenusphp">trunk/tests/phpunit/tests/ajax/CustomizeMenus.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjscustomizenavmenusjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/customize-nav-menus.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/customize-nav-menus.js      2016-11-04 01:47:56 UTC (rev 39136)
+++ trunk/src/wp-admin/js/customize-nav-menus.js        2016-11-04 06:03:51 UTC (rev 39137)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -235,7 +235,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                        self.doSearch( self.pages.search );
</span><span class="cx" style="display: block; padding: 0 10px">                                                }
</span><span class="cx" style="display: block; padding: 0 10px">                                        } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                self.loadItems( type, object );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         self.loadItems( [
+                                                       { type: type, object: object }
+                                               ] );
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        });
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -360,53 +362,82 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        // Render the template for each item by type.
</span><span class="cx" style="display: block; padding: 0 10px">                        _.each( api.Menus.data.itemTypes, function( itemType ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                self.pages[ itemType.type + ':' + itemType.object ] = 0;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                self.loadItems( itemType.type, itemType.object ); // @todo we need to combine these Ajax requests.
</del><span class="cx" style="display: block; padding: 0 10px">                         } );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        self.loadItems( api.Menus.data.itemTypes );
</ins><span class="cx" style="display: block; padding: 0 10px">                 },
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Load available menu items.
-               loadItems: function( type, object ) {
-                       var self = this, params, request, itemTemplate, availableMenuItemContainer;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * Load available nav menu items.
+                *
+                * @since 4.3.0
+                * @since 4.7.0 Changed function signature to take list of item types instead of single type/object.
+                * @access private
+                *
+                * @param {Array.<object>} itemTypes List of objects containing type and key.
+                * @param {string} deprecated Formerly the object parameter.
+                * @returns {void}
+                */
+               loadItems: function( itemTypes, deprecated ) {
+                       var self = this, _itemTypes, requestItemTypes = [], request, itemTemplate, availableMenuItemContainers = {};
</ins><span class="cx" style="display: block; padding: 0 10px">                         itemTemplate = wp.template( 'available-menu-item' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( -1 === self.pages[ type + ':' + object ] ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( _.isString( itemTypes ) && _.isString( deprecated ) ) {
+                               _itemTypes = [ { type: itemTypes, object: deprecated } ];
+                       } else {
+                               _itemTypes = itemTypes;
+                       }
+
+                       _.each( _itemTypes, function( itemType ) {
+                               var container, name = itemType.type + ':' + itemType.object;
+                               if ( -1 === self.pages[ name ] ) {
+                                       return; // Skip types for which there are no more results.
+                               }
+                               container = $( '#available-menu-items-' + itemType.type + '-' + itemType.object );
+                               container.find( '.accordion-section-title' ).addClass( 'loading' );
+                               availableMenuItemContainers[ name ] = container;
+
+                               requestItemTypes.push( {
+                                       object: itemType.object,
+                                       type: itemType.type,
+                                       page: self.pages[ name ]
+                               } );
+                       } );
+
+                       if ( 0 === requestItemTypes.length ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 return;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        availableMenuItemContainer = $( '#available-menu-items-' + type + '-' + object );
-                       availableMenuItemContainer.find( '.accordion-section-title' ).addClass( 'loading' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                         self.loading = true;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        params = {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 request = wp.ajax.post( 'load-available-menu-items-customizer', {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'customize-menus-nonce': api.settings.nonce['customize-menus'],
</span><span class="cx" style="display: block; padding: 0 10px">                                'wp_customize': 'on',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'type': type,
-                               'object': object,
-                               'page': self.pages[ type + ':' + object ]
-                       };
-                       request = wp.ajax.post( 'load-available-menu-items-customizer', params );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'item_types': requestItemTypes
+                       } );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        request.done(function( data ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                var items, typeInner;
-                               items = data.items;
-                               if ( 0 === items.length ) {
-                                       if ( 0 === self.pages[ type + ':' + object ] ) {
-                                               availableMenuItemContainer
-                                                       .addClass( 'cannot-expand' )
-                                                       .removeClass( 'loading' )
-                                                       .find( '.accordion-section-title > button' )
-                                                       .prop( 'tabIndex', -1 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         var typeInner;
+                               _.each( data.items, function( typeItems, name ) {
+                                       if ( 0 === typeItems.length ) {
+                                               if ( 0 === self.pages[ name ] ) {
+                                                       availableMenuItemContainers[ name ].find( '.accordion-section-title' )
+                                                               .addClass( 'cannot-expand' )
+                                                               .removeClass( 'loading' )
+                                                               .find( '.accordion-section-title > button' )
+                                                               .prop( 'tabIndex', -1 );
+                                               }
+                                               self.pages[ name ] = -1;
+                                               return;
+                                       } else if ( ( 'post_type:page' === name ) && ( ! availableMenuItemContainers[ name ].hasClass( 'open' ) ) ) {
+                                               availableMenuItemContainers[ name ].find( '.accordion-section-title > button' ).click();
</ins><span class="cx" style="display: block; padding: 0 10px">                                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        self.pages[ type + ':' + object ] = -1;
-                                       return;
-                               } else if ( ( 'page' === object ) && ( ! availableMenuItemContainer.hasClass( 'open' ) ) ) {
-                                       availableMenuItemContainer.find( '.accordion-section-title > button' ).click();
-                               }
-                               items = new api.Menus.AvailableItemCollection( items ); // @todo Why is this collection created and then thrown away?
-                               self.collection.add( items.models );
-                               typeInner = availableMenuItemContainer.find( '.available-menu-items-list' );
-                               items.each(function( menuItem ) {
-                                       typeInner.append( itemTemplate( menuItem.attributes ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 typeItems = new api.Menus.AvailableItemCollection( typeItems ); // @todo Why is this collection created and then thrown away?
+                                       self.collection.add( typeItems.models );
+                                       typeInner = availableMenuItemContainers[ name ].find( '.available-menu-items-list' );
+                                       typeItems.each( function( menuItem ) {
+                                               typeInner.append( itemTemplate( menuItem.attributes ) );
+                                       } );
+                                       self.pages[ name ] += 1;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 });
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                self.pages[ type + ':' + object ] += 1;
</del><span class="cx" style="display: block; padding: 0 10px">                         });
</span><span class="cx" style="display: block; padding: 0 10px">                        request.fail(function( data ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( typeof console !== 'undefined' && console.error ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -414,7 +445,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                        });
</span><span class="cx" style="display: block; padding: 0 10px">                        request.always(function() {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                availableMenuItemContainer.find( '.accordion-section-title' ).removeClass( 'loading' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         _.each( availableMenuItemContainers, function( container ) {
+                                       container.find( '.accordion-section-title' ).removeClass( 'loading' );
+                               } );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 self.loading = false;
</span><span class="cx" style="display: block; padding: 0 10px">                        });
</span><span class="cx" style="display: block; padding: 0 10px">                },
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizenavmenusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-nav-menus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-nav-menus.php    2016-11-04 01:47:56 UTC (rev 39136)
+++ trunk/src/wp-includes/class-wp-customize-nav-menus.php      2016-11-04 06:03:51 UTC (rev 39137)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -100,20 +100,35 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_die( -1 );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( empty( $_POST['type'] ) || empty( $_POST['object'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $all_items = array();
+               $item_types = array();
+               if ( isset( $_POST['item_types'] ) && is_array( $_POST['item_types'] ) ) {
+                       $item_types = wp_unslash( $_POST['item_types'] );
+               } elseif ( isset( $_POST['type'] ) && isset( $_POST['object'] ) ) { // Back compat.
+                       $item_types[] = array(
+                               'type' => wp_unslash( $_POST['type'] ),
+                               'object' => wp_unslash( $_POST['object'] ),
+                               'page' => empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] ),
+                       );
+               } else {
</ins><span class="cx" style="display: block; padding: 0 10px">                         wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $type = sanitize_key( $_POST['type'] );
-               $object = sanitize_key( $_POST['object'] );
-               $page = empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] );
-               $items = $this->load_available_items_query( $type, $object, $page );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( $item_types as $item_type ) {
+                       if ( empty( $item_type['type'] ) || empty( $item_type['object'] ) ) {
+                               wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
+                       }
+                       $type = sanitize_key( $item_type['type'] );
+                       $object = sanitize_key( $item_type['object'] );
+                       $page = empty( $item_type['page'] ) ? 0 : absint( $item_type['page'] );
+                       $items = $this->load_available_items_query( $type, $object, $page );
+                       if ( is_wp_error( $items ) ) {
+                               wp_send_json_error( $items->get_error_code() );
+                       }
+                       $all_items[ $item_type['type'] . ':' . $item_type['object'] ] = $items;
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( is_wp_error( $items ) ) {
-                       wp_send_json_error( $items->get_error_code() );
-               } else {
-                       wp_send_json_success( array( 'items' => $items ) );
-               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_send_json_success( array( 'items' => $all_items ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestsajaxCustomizeMenusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/ajax/CustomizeMenus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/ajax/CustomizeMenus.php 2016-11-04 01:47:56 UTC (rev 39136)
+++ trunk/tests/phpunit/tests/ajax/CustomizeMenus.php   2016-11-04 06:03:51 UTC (rev 39137)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -174,8 +174,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        // Testing empty obj_type.
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'type'     => '',
-                                       'object'   => 'post',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'type'     => 'post_type',
+                                       'object'   => '',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                                array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'success'  => false,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -193,6 +193,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        'data'     => 'nav_menus_missing_type_or_object_parameter',
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Testing empty type of a bulk request.
+                       array(
+                               array(
+                                       'item_types' => array(
+                                               array(
+                                                       'type'     => 'post_type',
+                                                       'object'   => 'post',
+                                               ),
+                                               array(
+                                                       'type'     => 'post_type',
+                                                       'object'   => '',
+                                               ),
+                                       ),
+                               ),
+                               array(
+                                       'success'  => false,
+                                       'data'     => 'nav_menus_missing_type_or_object_parameter',
+                               ),
+                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Testing incorrect type option.
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><span class="cx" style="display: block; padding: 0 10px">                                array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -276,6 +295,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><span class="cx" style="display: block; padding: 0 10px">                                true,
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // Testing a bulk request.
+                       array(
+                               array(
+                                       'item_types' => array(
+                                               array(
+                                                       'type'     => 'post_type',
+                                                       'object'   => 'post',
+                                               ),
+                                               array(
+                                                       'type'     => 'post_type',
+                                                       'object'   => 'page',
+                                               ),
+                                       ),
+                               ),
+                               true,
+                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -313,10 +348,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Get the results.
</span><span class="cx" style="display: block; padding: 0 10px">                $response = json_decode( $this->_last_response, true );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertNotEmpty( $response['data']['items'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertNotEmpty( current( $response['data']['items'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Get the second index to avoid the home page edge case.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $test_item = $response['data']['items'][1];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $first_prop = current( $response['data']['items'] );
+               $test_item = $first_prop[1];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $expected_keys as $key ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->assertArrayHasKey( $key, $test_item );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -325,7 +361,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Special test for the home page.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'page' === $test_item['object'] ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $home = $response['data']['items'][0];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $first_prop = current( $response['data']['items'] );
+                       $home = $first_prop[0];
</ins><span class="cx" style="display: block; padding: 0 10px">                         foreach ( $expected_keys as $key ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( 'object_id' !== $key ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $this->assertArrayHasKey( $key, $home );
</span></span></pre>
</div>
</div>

</body>
</html>