Skip to content
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
15 changes: 9 additions & 6 deletions assets/src/admin/onboarding/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ interface NoticeState {
message: string;
}

// WordPress provides snake_case keys here. Using them intentionally.
// eslint-disable-next-line camelcase
const { nonce, setup_url, site_type } = window.OneSearchOnboarding;
// WordPress provides snake_case keys here. Rename to camelCase for local use.
const {
nonce,
setup_url: setupUrl,
site_type: initialSiteType,
} = window.OneSearchOnboarding;

/**
* Create NONCE middleware for apiFetch
Expand Down Expand Up @@ -66,7 +69,7 @@ const SiteTypeSelector = ( {

const OnboardingScreen = () => {
const [ siteType, setSiteType ] = useState< SiteType | '' >(
site_type || ''
initialSiteType || ''
);
const [ notice, setNotice ] = useState< NoticeState | null >( null );
const [ isSaving, setIsSaving ] = useState( false );
Expand Down Expand Up @@ -106,8 +109,8 @@ const OnboardingScreen = () => {
setSiteType( settings.onesearch_site_type );

// Redirect user to setup page.
if ( setup_url ) {
window.location.href = setup_url;
if ( setupUrl ) {
window.location.href = setupUrl;
}
} );
} catch {
Expand Down
3 changes: 2 additions & 1 deletion assets/src/admin/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,11 @@ const OneSearchSettingsPage = () => {
body: JSON.stringify( { sites_data: updated } ),
} );
if ( ! response.ok ) {
/* eslint-disable-next-line no-console */
console.error(
'Error saving Brand site:',
response.statusText
); // eslint-disable-line no-console
);
return response;
}

Expand Down
2 changes: 1 addition & 1 deletion assets/src/components/SiteIndexableEntities.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const SiteIndexableEntities = ( {
),
} );
}
}, [] );
}, [ setNotice ] );

useEffect( () => {
getIndexableEntities();
Expand Down
2 changes: 1 addition & 1 deletion assets/src/components/SiteModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const SiteModal = ( {
} );
setShowNotice( true );
}
} catch ( error ) {
} catch {
setErrors( {
...newErrors,
message: __(
Expand Down
20 changes: 12 additions & 8 deletions assets/src/components/SiteSearchSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { __ } from '@wordpress/i18n';
/**
* External dependencies
*/
import { useState, useEffect } from 'react';
import { useState, useEffect, useCallback } from 'react';

/**
* Internal dependencies
Expand Down Expand Up @@ -77,11 +77,14 @@ const SiteSearchSettings = ( {
];

// Check if site has indexable entities.
const siteHasEntities = ( url ) => {
const normalizedUrl = trailingslashit( url );
const entities = indexableEntities[ normalizedUrl ] || [];
return Array.isArray( entities ) && entities.length > 0;
};
const siteHasEntities = useCallback(
( url ) => {
const normalizedUrl = trailingslashit( url );
const entities = indexableEntities[ normalizedUrl ] || [];
return Array.isArray( entities ) && entities.length > 0;
},
[ indexableEntities ]
);

// Auto-save search setting when entities are removed.
useEffect( () => {
Expand Down Expand Up @@ -144,7 +147,8 @@ const SiteSearchSettings = ( {
} );
} );
}
}, [ indexableEntities ] );
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ indexableEntities, setNotice, siteHasEntities ] );

// Load existing search settings
useEffect( () => {
Expand Down Expand Up @@ -174,7 +178,7 @@ const SiteSearchSettings = ( {
setLoading( false );
setLocalNotice( null );
} );
}, [ reloadKey ] );
}, [ reloadKey, setNotice ] );

// Toggle Algolia for a site
const handleSiteToggle = ( url, enabled ) => {
Expand Down
8 changes: 4 additions & 4 deletions assets/src/components/SiteSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const SiteSettings = () => {
}
const data = await response.json();
setApiKey( data?.secret_key || '' );
} catch ( error ) {
} catch {
setNotice( {
type: 'error',
message: __(
Expand Down Expand Up @@ -96,7 +96,7 @@ const SiteSettings = () => {
),
} );
}
} catch ( error ) {
} catch {
setNotice( {
type: 'error',
message: __(
Expand Down Expand Up @@ -126,7 +126,7 @@ const SiteSettings = () => {
}
const data = await response.json();
setGoverningSite( data?.governing_site_url || '' );
} catch ( error ) {
} catch {
setNotice( {
type: 'error',
message: __(
Expand Down Expand Up @@ -160,7 +160,7 @@ const SiteSettings = () => {
'onesearch'
),
} );
} catch ( error ) {
} catch {
setNotice( {
type: 'error',
message: __(
Expand Down
2 changes: 1 addition & 1 deletion assets/src/js/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const isValidUrl = ( url: string ): boolean => {
try {
const parsedUrl = new URL( url );
return isURL( parsedUrl.href );
} catch ( e ) {
} catch {
return false;
}
};
Expand Down
4 changes: 1 addition & 3 deletions inc/Modules/Core/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ public function register_assets(): void {

/**
* Add scripts and styles to the page.
*
* @param string $hook_suffix Admin page name.
*/
public function enqueue_scripts( $hook_suffix ): void {
public function enqueue_scripts(): void {
// @todo Only enqueue on OneSearch admin pages.
wp_enqueue_style( self::ADMIN_STYLES_HANDLE );
}
Expand Down
5 changes: 2 additions & 3 deletions inc/Modules/Rest/Governing_Data_Controller.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

declare(strict_types = 1);

/**
* Routes for Search-related operations.
*
* @package OneSearch
*/

declare(strict_types = 1);

namespace OneSearch\Modules\Rest;

use OneSearch\Modules\Search\Settings as Search_Settings;
Expand Down
5 changes: 2 additions & 3 deletions inc/Modules/Rest/Governing_Data_Handler.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php

declare(strict_types = 1);

/**
* Handles cross-site requests for governing brand data.
*
Expand All @@ -10,6 +7,8 @@
* @package OneSearch\Modules\Rest
*/

declare(strict_types = 1);

namespace OneSearch\Modules\Rest;

use OneSearch\Encryptor;
Expand Down
5 changes: 2 additions & 3 deletions inc/Modules/Rest/Search_Controller.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

declare(strict_types = 1);

/**
* Routes for Search-related operations.
*
* @package OneSearch
*/

declare(strict_types = 1);

namespace OneSearch\Modules\Rest;

use OneSearch\Modules\Search\Index;
Expand Down
2 changes: 1 addition & 1 deletion inc/Modules/Search/Post_Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private function get_cleaned_post_content( \WP_Post $post ): string {
$removed_filter = remove_filter( 'the_content', 'wptexturize', 10 );

try {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHookname -- intentionally using the_content.
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- intentionally using the_content.
$content = (string) apply_filters( 'the_content', $post->post_content );
} catch ( \Throwable $e ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- @todo Surface this better with a Logger class.
Expand Down
15 changes: 6 additions & 9 deletions inc/Modules/Search/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class Search implements Registrable {
*
* @var bool|null
*/
private ?bool $is_search_enabled;
private ?bool $is_search_enabled = null;

/**
* {@inheritDoc}
Expand All @@ -56,7 +56,7 @@ public function register_hooks(): void {

// Author data.
add_filter( 'get_the_author_display_name', [ $this, 'get_post_author' ], 10 );
add_filter( 'author_link', [ $this, 'get_post_author_link' ], 10, 3 );
add_filter( 'author_link', [ $this, 'get_post_author_link' ], 10 );
add_filter( 'get_avatar_url', [ $this, 'get_post_author_avatar' ], 10 );

// Term and taxonomy link handling for remote objects.
Expand All @@ -67,7 +67,7 @@ public function register_hooks(): void {
add_filter( 'wp_get_post_terms', [ $this, 'get_post_terms' ], 10, 3 );

// Block-theme compatibility: fix remote permalinks/excerpts in rendered blocks.
add_filter( 'render_block', [ $this, 'filter_render_block' ], 10, 3 );
add_filter( 'render_block', [ $this, 'filter_render_block' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -166,13 +166,11 @@ public function get_post_author( $author_name ) {
/**
* Author link mapping for remote posts.
*
* @param string $author_link Default author link.
* @param int $author_id Author ID (negative for remote).
* @param string|null $author_nicename The author's nicename if provided by the filter.
* @param string $author_link Default author link.
*
* @return string
*/
public function get_post_author_link( $author_link, $author_id, $author_nicename = null ) {
public function get_post_author_link( $author_link ) {
global $wp_query, $post;

if ( ! $this->is_search_enabled() || ! $wp_query instanceof \WP_Query || ! $this->should_filter_query( $wp_query ) ) {
Expand Down Expand Up @@ -325,11 +323,10 @@ public function get_post_terms( $terms, $post_id, $taxonomy ) {
*
* @param string $block_content Rendered block HTML.
* @param array<string,mixed> $block Block data.
* @param \WP_Block $instance Block instance.
*
* @return string
*/
public function filter_render_block( $block_content, $block, $instance ) {
public function filter_render_block( $block_content, $block ) {
global $post;

if ( ! $this->is_search_enabled() || ! $post instanceof \WP_Post || (int) $post->ID >= 0 || empty( $post->guid ) ) {
Expand Down
8 changes: 6 additions & 2 deletions inc/Modules/Search/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,12 @@ public function register_settings(): void {
/**
* Deletes Algolia index when site type is changed to consumer.
*
* @internal Hook callback
*
* @param mixed $old_value The old value.
* @param mixed $new_value The new value.
*/
public function on_site_type_change( $old_value, $new_value ): void {
public function on_site_type_change( $old_value, $new_value ): void { // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
if ( Admin_Settings::SITE_TYPE_CONSUMER !== $new_value ) {
return;
}
Expand Down Expand Up @@ -252,11 +254,13 @@ static function ( $site_url ) {
/**
* Purges the Governing_Data_Handler cache when a setting update triggers.
*
* @internal Hook callback
*
* @param mixed $old_value The old value.
* @param mixed $new_value The new value.
* @param string $option The option name.
*/
public function purge_cache_on_update( $old_value, $new_value, $option ): void {
public function purge_cache_on_update( $old_value, $new_value, $option ): void { // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
match ( $option ) {
self::OPTION_GOVERNING_ALGOLIA_CREDENTIALS,
self::OPTION_GOVERNING_SEARCH_SETTINGS,
Expand Down
4 changes: 3 additions & 1 deletion inc/Modules/Search/Watcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public function register_hooks(): void {
/**
* Triggered when a post's status changes (e.g., publish, update, trash, etc.)
*
* @internal Hook callback
*
* @param string $new_status The new post status.
* @param string $old_status The previous post status.
* @param \WP_Post $post The post object.
*/
public function on_post_transition( $new_status, $old_status, $post ): void {
public function on_post_transition( $new_status, $old_status, $post ): void { // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
if ( ! $post instanceof \WP_Post || ! $this->is_post_type_indexable( (string) $post->post_type ) ) {
return;
}
Expand Down
4 changes: 3 additions & 1 deletion inc/Modules/Settings/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ public function register_settings(): void {
/**
* Ensures the API key is generated when the site type changes to 'consumer'.
*
* @internal Hook callback
*
* @param mixed $old_value The old value.
* @param mixed $new_value The new value.
*/
public function on_site_type_change( $old_value, $new_value ): void {
public function on_site_type_change( $old_value, $new_value ): void { // phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
if ( self::SITE_TYPE_CONSUMER !== $new_value ) {
return;
}
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ parameters:
max: 80500
bootstrapFiles:
- onesearch.php
stubFiles:
- phpstan/stubs/wordpress-extended.php
paths:
- onesearch.php
- uninstall.php
Expand Down
29 changes: 29 additions & 0 deletions phpstan/stubs/wordpress-extended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php // phpcs:disable -- This is a stub file
/**
* Extends WP_Query and WP_Post with OneSearch dynamic properties for PHPStan analysis.
*/

namespace {
/**
* Extended WP_Query with OneSearch dynamic properties.
*
* @property bool $is_algolia_search Whether this is an Algolia-powered search query.
*/
class WP_Query {
}

/**
* Extended WP_Post with OneSearch dynamic properties.
*
* @property array<string, string> $onesearch_algolia_highlights Highlighted search snippets from Algolia.
* @property string $onesearch_site_url URL of the site this post belongs to.
* @property string $onesearch_site_name Name of the site this post belongs to.
* @property string $onesearch_remote_post_author_display_name Remote author display name.
* @property string $onesearch_remote_post_author_link Remote author posts URL.
* @property string $onesearch_remote_post_author_gravatar Remote author avatar URL.
* @property int $onesearch_original_id Original post ID on remote site.
* @property array<string, mixed> $onesearch_remote_taxonomies Taxonomies from remote site.
*/
class WP_Post {
}
}
Loading