add-to-css
3 years ago
build-css
6 years ago
check-block-version
4 years ago
compatible-render
2 years ago
filter-attributes
2 years ago
flexbox-alignment
6 years ago
get-attribute
1 year ago
get-background-image
4 years ago
get-background-image-url
4 years ago
get-content-attribute
2 years ago
get-device-type
3 years ago
get-dynamic-image
4 years ago
get-icon
1 year ago
get-image-sizes
4 years ago
get-media-url
4 years ago
get-responsive-placeholder
2 years ago
get-unique-block-names
2 years ago
has-numeric-value
4 years ago
hex-to-rgba
2 years ago
is-flex-item
2 years ago
is-numeric
2 years ago
sanitize-svg
5 years ago
shorthand-css
2 years ago
should-rebuild-css
4 years ago
sizingValue
3 years ago
value-with-unit
2 years ago
was-block-just-inserted
4 years ago
convertInlineStyleStringToObject.js
1 year ago
convertLegacyHtmlAttributes.js
1 year ago
get-editor-blocks.js
2 years ago
getBlockClasses.js
1 year ago
getInnerBlocks.js
1 year ago
index.js
1 year ago
legacyStyleUtils.js
1 year ago
loop-utils.js
1 year ago
more-design-options.js
1 year ago
noStyleAttributes.js
1 year ago
object-is-empty.js
4 years ago
sanitizeHtmlAttribute.js
1 year ago
selectorShortcuts.js
1 year ago
loop-utils.js
76 lines
| 1 | import { applyFilters } from '@wordpress/hooks'; |
| 2 | |
| 3 | export function removeEmpty( obj ) { |
| 4 | return Object.fromEntries( Object.entries( obj ).filter( ( [ idx, value ] ) => { |
| 5 | // Allow the image alt attribute to be empty. |
| 6 | if ( 'alt' === idx ) { |
| 7 | return true; |
| 8 | } |
| 9 | |
| 10 | return Array.isArray( value ) ? !! value.length : !! value; |
| 11 | } ) ); |
| 12 | } |
| 13 | |
| 14 | export function getTaxQueryParam( taxQuery, isExclude = false ) { |
| 15 | const paramKey = isExclude ? `${ taxQuery.rest }_exclude` : taxQuery.rest; |
| 16 | return { [ paramKey ]: { |
| 17 | terms: taxQuery.terms, |
| 18 | include_children: taxQuery?.includeChildren, |
| 19 | } }; |
| 20 | } |
| 21 | |
| 22 | export function normalizeTaxQuery( taxQueryValue, isExclude = false ) { |
| 23 | return taxQueryValue.reduce( ( normalized, taxQuery ) => { |
| 24 | return Object.assign( {}, normalized, getTaxQueryParam( taxQuery, isExclude ) ); |
| 25 | }, {} ); |
| 26 | } |
| 27 | |
| 28 | export function normalizeRepeatableArgs( query ) { |
| 29 | let normalizedQuery = normalizeArgs( query ); |
| 30 | |
| 31 | if ( Array.isArray( normalizedQuery.tax_query ) ) { |
| 32 | const normalizedTaxQuery = normalizeTaxQuery( normalizedQuery.tax_query ); |
| 33 | |
| 34 | normalizedQuery = Object.assign( |
| 35 | {}, |
| 36 | normalizedQuery, |
| 37 | normalizedTaxQuery, |
| 38 | { tax_query: undefined } |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | if ( Array.isArray( normalizedQuery.tax_query_exclude ) ) { |
| 43 | const normalizedTaxQueryExclude = normalizeTaxQuery( normalizedQuery.tax_query_exclude, true ); |
| 44 | |
| 45 | normalizedQuery = Object.assign( |
| 46 | {}, |
| 47 | normalizedQuery, |
| 48 | normalizedTaxQueryExclude, |
| 49 | { tax_query_exclude: undefined } |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | return normalizedQuery; |
| 54 | } |
| 55 | |
| 56 | export function normalizeArgs( query ) { |
| 57 | const defaultPerPage = !! query.per_page ? query.per_page : 10; |
| 58 | |
| 59 | // In the editor we capped the posts. |
| 60 | const perPage = '-1' === query.per_page || parseInt( query.per_page ) > parseInt( generateBlocksInfo.queryLoopEditorPostsCap ) |
| 61 | ? generateBlocksInfo.queryLoopEditorPostsCap |
| 62 | : defaultPerPage; |
| 63 | |
| 64 | let sticky; |
| 65 | |
| 66 | if ( 'exclude' === query.stickyPosts ) { |
| 67 | sticky = false; |
| 68 | } else if ( 'only' === query.stickyPosts ) { |
| 69 | sticky = true; |
| 70 | } |
| 71 | |
| 72 | const normalizedQuery = Object.assign( {}, query, { per_page: perPage, sticky } ); |
| 73 | |
| 74 | return applyFilters( 'generateblocks.editor.query-loop.normalize-parameters', normalizedQuery ); |
| 75 | } |
| 76 |