| | 1600 | /** |
| | 1601 | * Handles the plugin searching from the repository dynamically. |
| | 1602 | * |
| | 1603 | * @param {object} data $_POST data for ajax request 'search-install-plugins'. |
| | 1604 | * |
| | 1605 | */ |
| | 1606 | wp.updates.pluginInstallSearchRequest = function ( data ) { |
| | 1607 | var $searchTab = $( '.plugin-install-search' ), searchLocation, $pluginFilter = $( '#plugin-filter' ); |
| | 1608 | |
| | 1609 | $pluginFilter.empty(); |
| | 1610 | |
| | 1611 | if ( ! $searchTab.length ) { |
| | 1612 | |
| | 1613 | searchLocation = location.href.split( '?' )[ 0 ] + '?' + $.param( _.omit( data, [ '_ajax_nonce', 'pagenow' ] ) ); |
| | 1614 | |
| | 1615 | $searchTab = $( '<li class="plugin-install-search" />' ) |
| | 1616 | .append( $( '<a />', { |
| | 1617 | 'class': 'current', |
| | 1618 | 'href': searchLocation, |
| | 1619 | 'text': wp.updates.l10n.searchResultsLabel |
| | 1620 | } ) ); |
| | 1621 | |
| | 1622 | $( '.wp-filter .filter-links .current' ) |
| | 1623 | .removeClass( 'current' ) |
| | 1624 | .parents( '.filter-links' ) |
| | 1625 | .prepend( $searchTab ); |
| | 1626 | |
| | 1627 | $pluginFilter.prev( 'p' ).remove(); |
| | 1628 | |
| | 1629 | $( '.plugins-popular-tags-wrapper' ).remove(); |
| | 1630 | } |
| | 1631 | |
| | 1632 | if ( 'undefined' !== typeof wp.updates.searchRequest ) { |
| | 1633 | wp.updates.searchRequest.abort(); |
| | 1634 | } |
| | 1635 | |
| | 1636 | $( 'body' ).addClass( 'loading-content' ); |
| | 1637 | |
| | 1638 | wp.updates.searchRequest = wp.ajax.post( 'search-install-plugins', data ).done( function( response ) { |
| | 1639 | $( 'body' ).removeClass( 'loading-content' ); |
| | 1640 | $pluginFilter.append( response.items ); |
| | 1641 | delete wp.updates.searchRequest; |
| | 1642 | |
| | 1643 | if ( 0 === response.count ) { |
| | 1644 | wp.a11y.speak( wp.updates.l10n.noPluginsFound ); |
| | 1645 | } else { |
| | 1646 | wp.a11y.speak( wp.updates.l10n.pluginsFound.replace( '%d', response.count ) ); |
| | 1647 | } |
| | 1648 | } ); |
| | 1649 | }; |
| | 1650 | |
| | 1651 | /** |
| | 1652 | * Handles the plugin searching from install plugins. |
| | 1653 | * |
| | 1654 | * @param {object} data $_POST data for ajax request 'search-plugins'. |
| | 1655 | * |
| | 1656 | */ |
| | 1657 | wp.updates.pluginSearchRequest = function ( data ) { |
| | 1658 | var $bulkActionForm = $( '#bulk-action-form' ); |
| | 1659 | if ( 'undefined' !== typeof wp.updates.searchRequest ) { |
| | 1660 | wp.updates.searchRequest.abort(); |
| | 1661 | } |
| | 1662 | |
| | 1663 | $bulkActionForm.empty(); |
| | 1664 | |
| | 1665 | $( 'body' ).addClass( 'loading-content' ); |
| | 1666 | |
| | 1667 | wp.updates.searchRequest = wp.ajax.post( 'search-plugins', data ).done( function ( response ) { |
| | 1668 | |
| | 1669 | // Can we just ditch this whole subtitle business? |
| | 1670 | var $subTitle = $( '<span />' ).addClass( 'subtitle' ).html( wp.updates.l10n.searchResults.replace( '%s', _.escape( data.s ) ) ), $oldSubTitle = $( '.wrap .subtitle' ); |
| | 1671 | |
| | 1672 | if ( ! data.s.length ) { |
| | 1673 | $oldSubTitle.remove(); |
| | 1674 | } else if ( $oldSubTitle.length ) { |
| | 1675 | $oldSubTitle.replaceWith( $subTitle ); |
| | 1676 | } else { |
| | 1677 | $( '.wrap h1' ).append( $subTitle ); |
| | 1678 | } |
| | 1679 | |
| | 1680 | $( 'body' ).removeClass( 'loading-content' ); |
| | 1681 | $bulkActionForm.append( response.items ); |
| | 1682 | delete wp.updates.searchRequest; |
| | 1683 | |
| | 1684 | if ( 0 === response.count ) { |
| | 1685 | wp.a11y.speak( wp.updates.l10n.noPluginsFound ); |
| | 1686 | } else { |
| | 1687 | wp.a11y.speak( wp.updates.l10n.pluginsFound.replace( '%d', response.count ) ); |
| | 1688 | } |
| | 1689 | } ); |
| | 1690 | }; |
| | 1691 | |
| 2063 | | if ( ! $searchTab.length ) { |
| 2064 | | $searchTab = $( '<li class="plugin-install-search" />' ) |
| 2065 | | .append( $( '<a />', { |
| 2066 | | 'class': 'current', |
| 2067 | | 'href': searchLocation, |
| 2068 | | 'text': wp.updates.l10n.searchResultsLabel |
| 2069 | | } ) ); |
| 2070 | | |
| 2071 | | $( '.wp-filter .filter-links .current' ) |
| 2072 | | .removeClass( 'current' ) |
| 2073 | | .parents( '.filter-links' ) |
| 2074 | | .prepend( $searchTab ); |
| 2075 | | |
| 2076 | | $pluginFilter.prev( 'p' ).remove(); |
| 2077 | | $( '.plugins-popular-tags-wrapper' ).remove(); |
| 2078 | | } |
| 2079 | | |
| 2080 | | if ( 'undefined' !== typeof wp.updates.searchRequest ) { |
| 2081 | | wp.updates.searchRequest.abort(); |
| 2082 | | } |
| 2083 | | $( 'body' ).addClass( 'loading-content' ); |
| 2084 | | |
| 2085 | | wp.updates.searchRequest = wp.ajax.post( 'search-install-plugins', data ).done( function( response ) { |
| 2086 | | $( 'body' ).removeClass( 'loading-content' ); |
| 2087 | | $pluginFilter.append( response.items ); |
| 2088 | | delete wp.updates.searchRequest; |
| | 2155 | wp.updates.pluginInstallSearchRequest( data ); |
| 2130 | | if ( 'undefined' !== typeof wp.updates.searchRequest ) { |
| 2131 | | wp.updates.searchRequest.abort(); |
| 2132 | | } |
| 2133 | | |
| 2134 | | $bulkActionForm.empty(); |
| 2135 | | $( 'body' ).addClass( 'loading-content' ); |
| 2136 | | |
| 2137 | | wp.updates.searchRequest = wp.ajax.post( 'search-plugins', data ).done( function( response ) { |
| 2138 | | |
| 2139 | | // Can we just ditch this whole subtitle business? |
| 2140 | | var $subTitle = $( '<span />' ).addClass( 'subtitle' ).html( wp.updates.l10n.searchResults.replace( '%s', _.escape( data.s ) ) ), |
| 2141 | | $oldSubTitle = $( '.wrap .subtitle' ); |
| 2142 | | |
| 2143 | | if ( ! data.s.length ) { |
| 2144 | | $oldSubTitle.remove(); |
| 2145 | | } else if ( $oldSubTitle.length ) { |
| 2146 | | $oldSubTitle.replaceWith( $subTitle ); |
| 2147 | | } else { |
| 2148 | | $( '.wrap h1' ).append( $subTitle ); |
| 2149 | | } |
| 2150 | | |
| 2151 | | $( 'body' ).removeClass( 'loading-content' ); |
| 2152 | | $bulkActionForm.append( response.items ); |
| 2153 | | delete wp.updates.searchRequest; |
| | 2191 | wp.updates.pluginSearchRequest( data ); |
| | 2333 | |
| | 2334 | /** |
| | 2335 | * Window popstate for search plugins |
| | 2336 | */ |
| | 2337 | $( window ).on( 'popstate', function ( event ) { |
| | 2338 | |
| | 2339 | if ( 'plugin-install' === pagenow || 'plugins' === pagenow ) { |
| | 2340 | var data, currentLocation, urlSearchString, urlTypeString, urlTabString; |
| | 2341 | currentLocation = location.href; |
| | 2342 | urlSearchString = wp.urlParam( 's', currentLocation ); |
| | 2343 | |
| | 2344 | switch ( pagenow ) { |
| | 2345 | |
| | 2346 | case 'plugins': |
| | 2347 | |
| | 2348 | $pluginSearch.val( urlSearchString ); |
| | 2349 | |
| | 2350 | data = { |
| | 2351 | _ajax_nonce: wp.updates.ajaxNonce, |
| | 2352 | s: urlSearchString, |
| | 2353 | pagenow: pagenow |
| | 2354 | }; |
| | 2355 | |
| | 2356 | wp.updates.pluginSearchRequest( data ); |
| | 2357 | break; |
| | 2358 | |
| | 2359 | case 'plugin-install': |
| | 2360 | |
| | 2361 | urlTypeString = wp.urlParam( 'type', currentLocation ); |
| | 2362 | urlTabString = wp.urlParam( 'tab', currentLocation ); |
| | 2363 | |
| | 2364 | if ( urlTypeString == "" ) { |
| | 2365 | urlTypeString = "term"; |
| | 2366 | } |
| | 2367 | $pluginSearchType.val( urlTypeString ); |
| | 2368 | $pluginInstallSearch.val( urlSearchString ); |
| | 2369 | |
| | 2370 | data = { |
| | 2371 | _ajax_nonce: wp.updates.ajaxNonce, |
| | 2372 | s: urlSearchString, |
| | 2373 | tab: urlTabString, |
| | 2374 | type: urlTypeString, |
| | 2375 | pagenow: pagenow |
| | 2376 | }; |
| | 2377 | |
| | 2378 | wp.updates.pluginInstallSearchRequest( data ); |
| | 2379 | break; |
| | 2380 | |
| | 2381 | default: |
| | 2382 | return; |
| | 2383 | } |
| | 2384 | } |
| | 2385 | |
| | 2386 | } ); |