Skip to content

Commit

Permalink
Merging latest and resolving conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Oct 27, 2023
2 parents f8e6a0e + cdfe4e5 commit 76864c6
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 30 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

All notable changes to `WP Curate` will be documented in this file.

## 0.1.0 - 202X-XX-XX
## 1.3.0 - 2023-10-26

- Only show the blocks and register the meta on supported post types.
- Supported post types defaults to all Block Editor post types, but can be filtered by filtering `wp_curate_supported_post_types`.

## 1.2.0 - 2023-10-26

- Adds support for AND/OR operators in the Query Parameters, giving more control over what posts to show.

## 1.1.0 - 2023-09-21

- Bug fix: prevents error if post type does not support meta.

## 1.0.0 - 2023-09-19

- Initial release
7 changes: 7 additions & 0 deletions blocks/post/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@
* @package wp-curate
*/

use Alley\WP\WP_Curate\Supported_Post_Types;

/**
* Registers the wp-curate/post block using the metadata loaded from the `block.json` file.
*/
function wp_curate_post_block_init(): void {
$supported_post_types = new Supported_Post_Types();
if ( ! in_array( $supported_post_types->get_current_post_type(), $supported_post_types->get_supported_post_types(), true ) ) {
return;
}

// Register the block by passing the location of block.json.
register_block_type(
__DIR__
Expand Down
20 changes: 20 additions & 0 deletions blocks/query/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@
"type": "array"
},
"type": "object"
},
"termRelations": {
"default": {},
"items": {
"default": "AND",
"enum": [
"AND",
"OR"
],
"type": "string"
},
"type": "object"
},
"taxRelation": {
"default": "AND",
"enum": [
"AND",
"OR"
],
"type": "string"
}
}
}
80 changes: 63 additions & 17 deletions blocks/query/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PanelRow,
RadioControl,
RangeControl,
SelectControl,
TextControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
Expand All @@ -35,6 +36,8 @@ import {
mainDedupe,
} from '../../services/deduplicate';

import buildTermQueryArgs from '../../services/buildTermQueryArgs';

import './index.scss';

interface Window {
Expand Down Expand Up @@ -64,6 +67,8 @@ export default function Edit({
postTypes = ['post'],
searchTerm = '',
terms = {},
termRelations = {},
taxRelation = 'AND',
},
setAttributes,
}: EditProps) {
Expand All @@ -74,6 +79,17 @@ export default function Edit({
} = {},
} = (window as any as Window);

const andOrOptions = [
{
label: __('AND', 'wp-curate'),
value: 'AND',
},
{
label: __('OR', 'wp-curate'),
value: 'OR',
},
];

// @ts-ignore
const [isPostDeduplicating, postTypeObject] = useSelect(
(select) => {
Expand All @@ -98,17 +114,15 @@ export default function Edit({
const [availableTaxonomies, setAvailableTaxonomies] = useState<Taxonomies>({});
const [availableTypes, setAvailableTypes] = useState<Types>({});

let termQueryArgs = '';
if (Object.keys(availableTaxonomies).length > 0) {
allowedTaxonomies.forEach((taxonomy) => {
if (terms[taxonomy]?.length > 0) {
const restBase = availableTaxonomies[taxonomy].rest_base;
if (restBase) {
termQueryArgs += `&${restBase}=${terms[taxonomy].map((term) => term.id).join(',')}`;
}
}
});
}
const taxCount = allowedTaxonomies.filter((taxonomy: string) => terms[taxonomy]?.length > 0).length; // eslint-disable-line max-len

const termQueryArgs = buildTermQueryArgs(
allowedTaxonomies,
terms,
availableTaxonomies,
termRelations,
taxRelation,
);

const manualPostIds = manualPosts.map((post) => (post ?? null)).join(',');
const postTypeString = postTypes.join(',');
Expand Down Expand Up @@ -148,7 +162,7 @@ export default function Edit({
per_page: 20,
},
);
path += termQueryArgs;
path += `&${termQueryArgs}`;

apiFetch({
path,
Expand Down Expand Up @@ -205,6 +219,14 @@ export default function Edit({
setAttributes({ terms: newTermAttrs });
});

const setTermRelation = ((type: string, relation: string) => {
const newTermRelationAttrs = {
...termRelations,
[type]: relation,
};
setAttributes({ termRelations: newTermRelationAttrs });
});

const setNumberOfPosts = (newValue?: number) => {
setAttributes({
numberOfPosts: newValue,
Expand Down Expand Up @@ -286,11 +308,13 @@ export default function Edit({
className="manual-posts"
>
{manualPosts.map((_post, index) => (
<PanelRow className={classnames(
'manual-posts__container',
{ 'manual-posts__container--selected': manualPosts[index] },
)}
key={index}
<PanelRow
// eslint-disable-next-line react/no-array-index-key
key={index}
className={classnames(
'manual-posts__container',
{ 'manual-posts__container--selected': manualPosts[index] },
)}
>
<span className="manual-posts__counter">{index + 1}</span>
<PostPicker
Expand Down Expand Up @@ -325,9 +349,31 @@ export default function Edit({
onSelect={(newCategories: Term[]) => setTerms(taxonomy, newCategories)}
multiple
/>
{terms[taxonomy]?.length > 1 ? (
<SelectControl
label={sprintf(
__('%s Relation', 'wp-curate'),
availableTaxonomies[taxonomy].name || taxonomy,
)}
help={__('AND: Posts must have all selected terms. OR: Posts may have one or more selected terms.', 'wp-curate')}
options={andOrOptions}
onChange={(newValue) => setTermRelation(taxonomy, newValue)}
value={termRelations[taxonomy] ?? 'OR'}
/>
) : null}
<hr />
</Fragment>
))
) : null}
{taxCount > 1 ? (
<SelectControl
label={__('Taxonomy Relation', 'wp-curate')}
help={__('AND: Posts must meet all selected taxonomy requirements. OR: Posts may have meet one or more selected taxonomy requirements.', 'wp-curate')}
options={andOrOptions}
onChange={(newValue) => setAttributes({ taxRelation: newValue })}
value={taxRelation}
/>
) : null }
<TextControl
label={__('Search Term', 'wp-curate')}
onChange={(next) => setAttributes({ searchTerm: next })}
Expand Down
7 changes: 7 additions & 0 deletions blocks/query/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package wp-curate
*/

use Alley\WP\WP_Curate\Supported_Post_Types;

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
Expand All @@ -13,6 +15,11 @@
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function wp_curate_query_block_init(): void {
$supported_post_types = new Supported_Post_Types();
if ( ! in_array( $supported_post_types->get_current_post_type(), $supported_post_types->get_supported_post_types(), true ) ) {
return;
}

// Register the block by passing the location of block.json.
register_block_type(
__DIR__,
Expand Down
4 changes: 4 additions & 0 deletions blocks/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface EditProps {
terms?: {
[key: string]: any[];
};
termRelations?: {
[key: string]: string;
};
taxRelation?: string;
};
setAttributes: (attributes: any) => void;
}
Expand Down
8 changes: 0 additions & 8 deletions config/post-meta.json
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
{
"wp_curate_deduplication": {
"post_types": [
"all"
],
"type": "boolean"
}
}
46 changes: 46 additions & 0 deletions services/buildTermQueryArgs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
interface Types {
[key: string]: {
name: string;
slug: string;
rest_base: string;
};
}

/**
* Builds the term query args for the WP REST API.
*
* @param string[] allowedTaxonomies The list of allowed taxonomies.
* @param { [key: string]: any[] } terms The selected terms.
* @param { [key: string]: any[] } availableTaxonomies The available taxonomies.
* @param { [key: string]: string } termRelations The AND/OR relation used for each taxonomy.
* @param string taxRelation The AND/OR relation used for all the terms.
* @returns string The term query args.
*/
export default function buildTermQueryArgs(
allowedTaxonomies: string[],
terms: { [key: string]: any[] },
availableTaxonomies: Types,
termRelations: { [key: string]: string },
taxRelation: string,
): string {
const taxCount = allowedTaxonomies.filter((taxonomy: string) => terms[taxonomy]?.length > 0).length; // eslint-disable-line max-len

const termQueryArgs: string[] = [];
if (Object.keys(availableTaxonomies).length > 0) {
allowedTaxonomies.forEach((taxonomy) => {
if (terms[taxonomy]?.length > 0) {
const restBase = availableTaxonomies[taxonomy].rest_base;
if (restBase) {
termQueryArgs.push(`${restBase}[terms]=${terms[taxonomy].map((term) => term.id).join(',')}`);
if (termRelations[taxonomy] !== '' && typeof termRelations[taxonomy] !== 'undefined') {
termQueryArgs.push(`${restBase}[operator]=${termRelations[taxonomy]}`);
}
}
}
});
if (taxCount > 1) {
termQueryArgs.push(`tax_relation=${taxRelation}`);
}
}
return termQueryArgs.join('&');
}
4 changes: 2 additions & 2 deletions services/deduplicate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ export function mainDedupe() {
queryBlocks.forEach((queryBlock) => {
const { attributes } = queryBlock;
const {
backfillPosts = [],
backfillPosts = null,
deduplication = 'inherit',
posts = [],
numberOfPosts = 5,
postTypes = ['post'],
} = attributes;
if (!backfillPosts.length) {
if (!backfillPosts) {
return;
}
const postTypeString = postTypes.join(',');
Expand Down
3 changes: 2 additions & 1 deletion src/class-plugin-curated-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ public function with_query_context( array $context, array $attributes, WP_Block_

if ( isset( $attributes['terms'] ) && is_array( $attributes['terms'] ) && count( $attributes['terms'] ) > 0 ) {
$args['tax_query'] = [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
'relation' => 'AND',
'relation' => $attributes['taxRelation'] ?? 'AND',
];

foreach ( $attributes['terms'] as $taxonomy => $terms ) {
if ( taxonomy_exists( $taxonomy ) && is_array( $terms ) && count( $terms ) > 0 ) {
$args['tax_query'][] = [
'taxonomy' => $taxonomy,
'terms' => array_column( $terms, 'id' ),
'operator' => is_array( $attributes['termRelations'] ) ? $attributes['termRelations'][ $taxonomy ] ?? 'AND' : 'AND',
];
}
}
Expand Down
Loading

0 comments on commit 76864c6

Please sign in to comment.