inspector-controls
4 years ago
BlockControls.js
4 years ago
InspectorControls.js
4 years ago
LayoutSelector.js
4 years ago
LoopRenderer.js
4 years ago
QueryLoopRenderer.js
4 years ago
utils.js
4 years ago
utils.js
69 lines
| 1 | export function removeEmpty( obj ) { |
| 2 | return Object.fromEntries( Object.entries( obj ).filter( ( [ idx, value ] ) => { |
| 3 | // Allow the image alt attribute to be empty. |
| 4 | if ( 'alt' === idx ) { |
| 5 | return true; |
| 6 | } |
| 7 | |
| 8 | return Array.isArray( value ) ? !! value.length : !! value; |
| 9 | } ) ); |
| 10 | } |
| 11 | |
| 12 | function getTaxQueryParam( taxQuery, isExclude = false ) { |
| 13 | const paramKey = isExclude ? `${ taxQuery.rest }_exclude` : taxQuery.rest; |
| 14 | return { [ paramKey ]: taxQuery.terms }; |
| 15 | } |
| 16 | |
| 17 | function normalizeTaxQuery( taxQueryValue, isExclude = false ) { |
| 18 | return taxQueryValue.reduce( ( normalized, taxQuery ) => { |
| 19 | return Object.assign( {}, normalized, getTaxQueryParam( taxQuery, isExclude ) ); |
| 20 | }, {} ); |
| 21 | } |
| 22 | |
| 23 | export function normalizeRepeatableArgs( query ) { |
| 24 | let normalizedQuery = normalizeArgs( query ); |
| 25 | |
| 26 | if ( Array.isArray( query.tax_query ) ) { |
| 27 | const normalizedTaxQuery = normalizeTaxQuery( query.tax_query ); |
| 28 | |
| 29 | normalizedQuery = Object.assign( |
| 30 | {}, |
| 31 | normalizedQuery, |
| 32 | normalizedTaxQuery, |
| 33 | { tax_query: undefined } |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | if ( Array.isArray( query.tax_query_exclude ) ) { |
| 38 | const normalizedTaxQueryExclude = normalizeTaxQuery( query.tax_query_exclude, true ); |
| 39 | |
| 40 | normalizedQuery = Object.assign( |
| 41 | {}, |
| 42 | normalizedQuery, |
| 43 | normalizedTaxQueryExclude, |
| 44 | { tax_query_exclude: undefined } |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | return normalizedQuery; |
| 49 | } |
| 50 | |
| 51 | export function normalizeArgs( query ) { |
| 52 | const defaultPerPage = !! query.per_page ? query.per_page : 10; |
| 53 | |
| 54 | // In the editor we capped to 50 posts. |
| 55 | const perPage = '-1' === query.per_page || query.per_page > 50 |
| 56 | ? 50 |
| 57 | : defaultPerPage; |
| 58 | |
| 59 | let sticky; |
| 60 | |
| 61 | if ( 'exclude' === query.stickyPosts ) { |
| 62 | sticky = false; |
| 63 | } else if ( 'only' === query.stickyPosts ) { |
| 64 | sticky = true; |
| 65 | } |
| 66 | |
| 67 | return Object.assign( {}, query, { per_page: perPage, sticky } ); |
| 68 | } |
| 69 |