Skip to content

Commit

Permalink
Merge pull request #2677 from Parsely/prepare/3.16.2
Browse files Browse the repository at this point in the history
Release wp-parsely 3.16.2
  • Loading branch information
vaurdan authored Jul 30, 2024
2 parents 24169fc + 8a6bf62 commit 0e44216
Show file tree
Hide file tree
Showing 19 changed files with 301 additions and 95 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.16.2](https://github.com/Parsely/wp-parsely/compare/3.16.1...3.16.2) - 2024-07-30

### Fixed

- PCH Smart Linking: Prevent suggesting links in unexpected blocks ([#2673](https://github.com/Parsely/wp-parsely/pull/2673))
- PCH Related Posts: Prevent Related Posts indefinite loading ([#2666](https://github.com/Parsely/wp-parsely/pull/2666))
- Permissions: Allow overriding of PCH feature permissions ([#2663](https://github.com/Parsely/wp-parsely/pull/2663))
- PCH Smart Linking: Fix bug causing ‘No smart links’ error despite valid API response ([#2662](https://github.com/Parsely/wp-parsely/pull/2662))

## [3.16.1](https://github.com/Parsely/wp-parsely/compare/3.16.0...3.16.1) - 2024-07-18

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Parse.ly

Stable tag: 3.16.1
Stable tag: 3.16.2
Requires at least: 5.2
Tested up to: 6.5
Requires PHP: 7.2
Expand Down
2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar-rtl.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => 'b8c0240b984732a6e310');
<?php return array('dependencies' => array('react', 'wp-api-fetch', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-core-data', 'wp-data', 'wp-dom-ready', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => '69283974093cc5f88771');
2 changes: 1 addition & 1 deletion build/content-helper/editor-sidebar.css

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions build/content-helper/editor-sidebar.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-parsely",
"version": "3.16.1",
"version": "3.16.2",
"private": true,
"description": "The Parse.ly plugin facilitates real-time and historical analytics to your content through a platform designed and built for digital publishing.",
"author": "parsely, hbbtstar, jblz, mikeyarce, GaryJ, parsely_mike, acicovic, mehmoodak, vaurdan",
Expand Down
1 change: 1 addition & 0 deletions src/@types/assets/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ declare global {
wpParselyPostsStatsResponse: string;
wpParselyPostUrl: string;
wpParselySiteId: string,
wpParselySmartLinkingAllowedBlocks: string[];

/**
* Jetpack Editor Initial State.
Expand Down
25 changes: 25 additions & 0 deletions src/class-permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ public static function current_user_can_use_pch_feature(
$current_user = wp_get_current_user();
$user_roles = $current_user->roles;

/**
* Filters whether the current user can use the specified Content Helper
* feature.
*
* This filter can be used to override the default permissions check.
*
* @since 3.16.2
*
* @param bool $current_user_can_use_pch_feature Whether the current user can use the feature.
* @param string $feature_name The feature's name.
* @param \WP_User $current_user The current user object.
* @param int|false $post_id The post ID, if the check is for a specific post.
*/
$filtered_current_user_can_use_pch_feature = apply_filters(
'wp_parsely_current_user_can_use_pch_feature',
false,
$feature_name,
$current_user,
$post_id
);

if ( true === $filtered_current_user_can_use_pch_feature ) {
return true;
}

// Current user's role is not yet set.
if ( 0 === count( $user_roles ) ) {
return false;
Expand Down
88 changes: 71 additions & 17 deletions src/content-helper/editor-sidebar/related-posts/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
// eslint-disable-next-line import/named
import { store as coreStore, Taxonomy, User } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { GutenbergFunction } from '../../../@types/gutenberg/types';
import { useEffect, useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { GutenbergFunction } from '../../../@types/gutenberg/types';

/**
* Defines the states of post data as handled in the usePostData() function.
Expand Down Expand Up @@ -36,23 +37,42 @@ interface PostData {
*
* @since 3.14.3
* @since 3.14.4 Implemented checks to reduce risk of invalid data being processed.
* @since 3.16.2 Added `isLoading` and `hasResolved` to check if the data is still being fetched or if it
* has been resolved.
*
* @see https://github.com/Parsely/wp-parsely/issues/2423
*
* @return {PostData} The post data for the current post.
*/
export function usePostData(): PostData {
const { authors, categories, tags, isReady } = useSelect( ( select ) => {
const { getEntityRecords } = select( coreStore );
let authorRecords: User[] | null | undefined;
let categoryRecords: Taxonomy[] | null | undefined;
let tagRecords: Taxonomy[] | null | undefined;
const [ postData, setPostData ] = useState<PostData>( {
authors: undefined,
categories: undefined,
tags: undefined,
isReady: false,
} );

/**
* Fetches the post attributes from the editor.
* This includes the author, categories, and tags, and if the data is not available, it will be set to `null`.
*
* @since 3.14.4
* @since 3.16.2 Added `isLoading` and `hasResolved` to check if the data is still being fetched or if it
* has been resolved.
*/
const postAttributes = useSelect( ( select ) => {
// @ts-ignore `hasFinishedResolution` and `isResolving` are not part of the core-data store type.
const { getEntityRecords, hasFinishedResolution, isResolving } = select( coreStore );

const editor = select( 'core/editor' ) as GutenbergFunction;
const authorId = editor.getEditedPostAttribute( 'author' );
const categoryIds = editor.getEditedPostAttribute( 'categories' );
const tagIds = editor.getEditedPostAttribute( 'tags' );

let authorRecords: User[] | null | undefined;
let categoryRecords: Taxonomy[] | null | undefined;
let tagRecords: Taxonomy[] | null | undefined;

if ( Number.isInteger( authorId ) ) {
authorRecords = getEntityRecords(
'root', 'user', { include: [ authorId ], context: 'view' }
Expand Down Expand Up @@ -81,19 +101,53 @@ export function usePostData(): PostData {
tagRecords = null;
}

const isPostDataReady: boolean = (
authorRecords !== undefined &&
categoryRecords !== undefined &&
tagRecords !== undefined
// Check if the data is still being fetched.
const isLoading = (
isResolving( 'getEntityRecords', [ 'root', 'user', { include: [ authorId ], context: 'view' } ] ) ||
isResolving( 'getEntityRecords', [ 'taxonomy', 'category', { include: categoryIds, context: 'view' } ] ) ||
isResolving( 'getEntityRecords', [ 'taxonomy', 'post_tag', { include: tagIds, context: 'view' } ] )
);

// Check if all the data has been resolved. If the data is not available, it will be set to `null`,
// so it is considered resolved.
const hasResolved = (
( hasFinishedResolution( 'getEntityRecords', [ 'root', 'user', { include: [ authorId ], context: 'view' } ] ) ||
authorRecords === null ) &&
( hasFinishedResolution( 'getEntityRecords', [ 'taxonomy', 'category', { include: categoryIds, context: 'view' } ] ) ||
categoryRecords === null ) &&
( hasFinishedResolution( 'getEntityRecords', [ 'taxonomy', 'post_tag', { include: tagIds, context: 'view' } ] ) ||
tagRecords === null )
);

return {
authors: authorRecords,
categories: categoryRecords,
tags: tagRecords,
isReady: isPostDataReady,
};
return { authorRecords, categoryRecords, tagRecords, isLoading, hasResolved };
}, [] );

return { authors, categories, tags, isReady };
/**
* Sets the post data when all the data is ready.
* This is done when all the data is fetched and resolved.
*
* @since 3.16.2
*/
useEffect( () => {
const {
authorRecords,
categoryRecords,
tagRecords,
isLoading,
hasResolved,
} = postAttributes;

const isPostDataReady: boolean = hasResolved && ! isLoading;

if ( isPostDataReady ) {
setPostData( {
authors: authorRecords,
categories: categoryRecords,
tags: tagRecords,
isReady: true,
} );
}
}, [ postAttributes ] );

return postData;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
*/
class Smart_Linking extends Content_Helper_Feature {

/**
* Allowed blocks for the Smart Linking feature.
*
* @since 3.16.2
*
* @var array $BLOCKS_ALLOWED
*/
private const BLOCKS_ALLOWED = array(
'core/paragraph',
);

/**
* Instance of Editor_Sidebar class.
*
Expand All @@ -33,6 +44,7 @@ public function __construct( Editor_Sidebar $editor_sidebar ) {
$this->parsely = $editor_sidebar->parsely;

add_action( 'delete_post', array( $this, 'purge_smart_links' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'add_inline_script' ) );
}

/**
Expand Down Expand Up @@ -171,6 +183,27 @@ private function register_taxonomy( string $taxonomy, string $singular_name ): v
);
}

/**
* Adds inline script with Smart Linking specific data.
*
* @since 3.16.2
*/
public function add_inline_script(): void {
/**
* Filters the allowed blocks for the Smart Linking feature.
*
* @since 3.16.2
*
* @param array $allowed_blocks The allowed blocks for the Smart Linking feature.
*/
$allowed_blocks = apply_filters( 'wp_parsely_smart_linking_allowed_blocks', self::BLOCKS_ALLOWED );
wp_add_inline_script(
Editor_Sidebar::get_script_id(),
'window.wpParselySmartLinkingAllowedBlocks = ' . wp_json_encode( $allowed_blocks ) . ';',
'before'
);
}

/**
* Purges all outbound smart links associated with a post when the post is deleted.
*
Expand Down
Loading

0 comments on commit 0e44216

Please sign in to comment.