Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] trunk from ryanwelcher:trunk #19

Merged
merged 13 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"files": [
"includes/enqueues.php",
"includes/query-loop.php"
"includes/query-loop.php",
"includes/utilities.php"
]
},
"require-dev": {
Expand Down
8 changes: 6 additions & 2 deletions includes/Query_Params_Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Query_Params_Generator {
use Traits\Include_Posts;
use Traits\Meta_Query;
use Traits\Date_Query;
use Traits\Exclude_Taxonomies;
use Traits\Disable_Pagination;


/**
Expand All @@ -28,6 +30,8 @@ class Query_Params_Generator {
'include_posts',
'meta_query',
'date_query',
'exclude_taxonomies',
'disable_pagination',
);

/**
Expand Down Expand Up @@ -58,8 +62,8 @@ class Query_Params_Generator {
* @param array $custom_params Custom values from AQL.
*/
public function __construct( $default_params, $custom_params ) {
$this->default_params = is_array( $default_params ) ? $default_params : [];
$this->custom_params = is_array( $custom_params ) ? $custom_params : [];
$this->default_params = is_array( $default_params ) ? $default_params : array();
$this->custom_params = is_array( $custom_params ) ? $custom_params : array();
}

/**
Expand Down
13 changes: 13 additions & 0 deletions includes/Traits/Disable_Pagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Trait for managing pagination.
*/

namespace AdvancedQueryLoop\Traits;

trait Disable_Pagination {

public function process_disable_pagination() {
$this->custom_args['no_found_rows'] = $this->get_custom_param( 'disable_pagination' );
}
}
30 changes: 30 additions & 0 deletions includes/Traits/Exclude_Taxonomies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Exclude_Taxonomies
*/

namespace AdvancedQueryLoop\Traits;

/**
* Trait
*/
trait Exclude_Taxonomies {

/**
* Main processing function.
*/
public function process_exclude_taxonomies(): void {
$taxonomies_to_exclude = $this->custom_params['exclude_taxonomies'];
if( count( $taxonomies_to_exclude ) ) {
$tax_query = [];
foreach ( $taxonomies_to_exclude as $slug ) {
$tax_query[] = [
'taxonomy' => $slug,
'operator' => 'NOT EXISTS'
];
}
$this->custom_args['tax_query'] = $tax_query;
}
}
}

5 changes: 4 additions & 1 deletion includes/Traits/Include_Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public function process_include_posts() {
* @return array
*/
protected function get_include_ids( $include_posts ) {
return array_column( $include_posts, 'id' );
if ( is_array( $include_posts ) ) {
return array_column( $include_posts, 'id' );
}
return array();
}
}
21 changes: 21 additions & 0 deletions includes/enqueues.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace AdvancedQueryLoop;

use function AdvancedQueryLoop\Utils\{ is_gutenberg_plugin_version_or_higher,is_core_version_or_higher };


// Bail on unit tests.
if ( ! function_exists( 'add_action' ) ) {
return;
Expand All @@ -33,5 +36,23 @@ function () {
// Allow for translation.
wp_set_script_translations( 'advanced-query-loop', 'advanced-query-loop' );
}

// Per Page, Offset, and Max count controls where merged into GB 19.
if ( ! is_gutenberg_plugin_version_or_higher( '19' ) && ! is_core_version_or_higher( '6.7' ) ) {
// Enqueue the legacy controls.
$pre_gb_19_assets_file = BUILD_DIR_PATH . 'legacy-pre-gb-19.asset.php';

if ( file_exists( $pre_gb_19_assets_file ) ) {
$pre_gb_19_assets = include $pre_gb_19_assets_file;

\wp_enqueue_script(
'advanced-query-loop-legacy-pre-gb-19',
BUILD_DIR_URL . 'legacy-pre-gb-19.js',
array_merge( array( 'advanced-query-loop' ), $pre_gb_19_assets['dependencies'] ),
$pre_gb_19_assets['version'],
true
);
}
}
}
);
36 changes: 36 additions & 0 deletions includes/utilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Handles enqueueing of assets for the plugin.
*
* @package AdvancedQueryLoop/Utils
*/

namespace AdvancedQueryLoop\Utils;

/**
* Helper to determine if the Gutenberg plugin is installed and if so, if it is at or higher a given version.
*
* @param string $version The version to check for.
*
* @return boolean.
*/
function is_gutenberg_plugin_version_or_higher( string $version ) {
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && defined( 'GUTENBERG_VERSION' ) ) {
// This means the plugin is installed
return version_compare( GUTENBERG_VERSION, $version, '>=' );
}
return false;
}

/**
* Helper to determine is the current WP install is at or higher than a given version.
*
* @param string $version The version to check for.

* @return boolean.
*/
function is_core_version_or_higher( string $version ) {
$core = get_bloginfo( 'version' );
return version_compare( $core, $version, '>=' );
}

93 changes: 93 additions & 0 deletions src/components/exclude-taxonomies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* WordPress dependencies
*/
/**
* WordPress dependencies
*/
import { FormTokenField, BaseControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';

/**
* A helper to retrieve the correct items to display or save in the token field
*
* @param {Array} subSet
* @param {Array} fullSet
* @param {string} lookupProperty
* @param {string} returnProperty
* @return {Array} The correct items to display or save in the token field
*/
function prepDataFromTokenField(
subSet,
fullSet,
lookupProperty,
returnProperty
) {
const subsetFullObjects = fullSet.filter( ( item ) =>
subSet.includes( item[ lookupProperty ] )
);
return subsetFullObjects.map(
( { [ returnProperty ]: returnVal } ) => returnVal
);
}

export const ExcludeTaxonomies = ( { attributes, setAttributes } ) => {
const {
query: {
multiple_posts: multiplePosts = [],
postType,
exclude_taxonomies: excludeTaxonomies = [],
} = {},
} = attributes;

const taxonomies = useSelect(
( select ) => {
const knownTaxes = select( coreDataStore ).getTaxonomies();
return knownTaxes.filter(
( { types } ) =>
types.includes( postType ) ||
types.some( ( i ) => multiplePosts.includes( i ) )
);
},
[ multiplePosts, postType ]
);

return (
<BaseControl
help={ __(
'Choose taxonomies to exclude from the query.',
'advanced-query-loop'
) }
>
<FormTokenField
label={ __( 'Exclude Taxonomies', 'advanced-query-loop' ) }
value={
prepDataFromTokenField(
excludeTaxonomies,
taxonomies,
'slug',
'name'
) || []
}
suggestions={ [ ...taxonomies?.map( ( { name } ) => name ) ] }
onChange={ ( selectedTaxonomies ) => {
setAttributes( {
query: {
...attributes.query,
exclude_taxonomies:
prepDataFromTokenField(
selectedTaxonomies,
taxonomies,
'name',
'slug'
) || [],
},
} );
} }
__experimentalExpandOnFocus
__experimentalShowHowTo={ false }
/>
</BaseControl>
);
};
29 changes: 29 additions & 0 deletions src/components/pagination-toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* WordPress dependencies
*/
import { ToggleControl } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

export const PaginationToggle = ( { attributes, setAttributes } ) => {
const { query: { disable_pagination: disablePagination } = {} } =
attributes;

return (
<ToggleControl
label={ __( 'Disable pagination', 'advanced-query-loop' ) }
help={ __(
'Disabling pagination will not show any pagination controls on the front end. It can also provide a performance improvement for complicated queries.',
'advanced-query-loop'
) }
checked={ !! disablePagination }
onChange={ () => {
setAttributes( {
query: {
...attributes.query,
disable_pagination: ! disablePagination,
},
} );
} }
/>
);
};
28 changes: 28 additions & 0 deletions src/legacy-controls/pre-gb-19.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* WordPress dependencies
*/
import { registerPlugin } from '@wordpress/plugins';

/**
* Internal dependencies
*/
import AQLLegacyControls from '../slots/aql-legacy-controls';
import { PostCountControls } from '../components/post-count-controls';
import { PostOffsetControls } from '../components/post-offset-controls';

registerPlugin( 'aql-pre-gb-19-controls', {
render: () => {
return (
<>
<AQLLegacyControls>
{ ( props ) => (
<>
<PostCountControls { ...props } />
<PostOffsetControls { ...props } />
</>
) }
</AQLLegacyControls>
</>
);
},
} );
25 changes: 25 additions & 0 deletions src/slots/aql-legacy-controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { createSlotFill } from '@wordpress/components';

/**
* Create our Slot and Fill components
*/
const { Fill, Slot } = createSlotFill( 'AQLLegacyControls' );

/**
* This slot is not exposed and is used to try to maintain the same UI
*/

const AQLLegacyControls = ( { children } ) => <Fill>{ children }</Fill>;

AQLLegacyControls.Slot = ( { fillProps } ) => (
<Slot fillProps={ fillProps }>
{ ( fills ) => {
return fills.length ? fills : null;
} }
</Slot>
);

export default AQLLegacyControls;
12 changes: 8 additions & 4 deletions src/variations/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import { createBlock } from '@wordpress/blocks';
import { AQL } from '.';
import AQLControls from '../slots/aql-controls';
import AQLControlsInheritedQuery from '../slots/aql-controls-inherited-query';
import { PostCountControls } from '../components/post-count-controls';
import { PostOffsetControls } from '../components/post-offset-controls';
import AQLLegacyControls from '../slots/aql-legacy-controls';
import { PostMetaQueryControls } from '../components/post-meta-query-controls';
import { PostDateQueryControls } from '../components/post-date-query-controls';
import { MultiplePostSelect } from '../components/multiple-post-select';
import { PostOrderControls } from '../components/post-order-controls';
import { PostExcludeControls } from '../components/post-exclude-controls';
import { PostIncludeControls } from '../components/post-include-controls';
import { ExcludeTaxonomies } from '../components/exclude-taxonomies';
import { PaginationToggle } from '../components/pagination-toggle';

/**
* Determines if the active variation is this one
Expand Down Expand Up @@ -56,11 +57,14 @@ const withAdvancedQueryControls = ( BlockEdit ) => ( props ) => {
'advanced-query-loop'
) }
>
<AQLLegacyControls.Slot
fillProps={ { ...props } }
/>
<PaginationToggle { ...props } />
<MultiplePostSelect { ...props } />
<PostCountControls { ...props } />
<PostOffsetControls { ...props } />
<PostOrderControls { ...props } />
<PostExcludeControls { ...props } />
<ExcludeTaxonomies { ...props } />
<PostIncludeControls { ...props } />
<PostMetaQueryControls { ...props } />
<PostDateQueryControls { ...props } />
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
entry: {
...getWebpackEntryPoints(),
variations: './src/variations/index.js',
'legacy-pre-gb-19': './src/legacy-controls/pre-gb-19.js',
},
output: {
...defaultConfig.output,
Expand Down
Loading