Skip to content

dev: backport refactor changes from One*#74

Merged
justlevine merged 6 commits intodevelopfrom
dev/refactor-first-pass
Dec 8, 2025
Merged

dev: backport refactor changes from One*#74
justlevine merged 6 commits intodevelopfrom
dev/refactor-first-pass

Conversation

@justlevine
Copy link
Collaborator

@justlevine justlevine commented Dec 4, 2025

Description

This provides the first round of OneDesign backports to this plugin.

Technical Details

Does not make any attempts to refactor legacy code or fix existing bugs. Specifically:

  • inc/classes/*
  • The internals of inc/Modules/Rest/*
  • inc/Utils

have been left untouched.

Checklist

Screenshots

To-do

Fixes/Covers issue

Fixes #

This comment was marked as outdated.

@justlevine justlevine requested a review from up1512001 December 4, 2025 21:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 58 out of 62 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@justlevine justlevine changed the title dev: refactor [first pass] dev: backport refactor changes from One* Dec 8, 2025
Copy link
Member

@up1512001 up1512001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added few comments & hoping some of them are from legacy code.


useEffect( () => {
const token = NONCE;
const token = ( NONCE );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why need to make const out of NONCE, i think its from old code base.

Copy link
Collaborator Author

@justlevine justlevine Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup legacy from OneDesign to keep the diff small. Since I want to rip this file out and replace it with tsx/apiFetch anyway, I'm leaving for now.

'X-OneSearch-Plugins-Token' => $public_key,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-OneSearch-Token' => $public_key,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just as remainder here public_key should be api_key

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're referring to the name of PHP variable used inside the class, or something else?

(either way, the plan is to refactor all this)


$base_size = strlen( wp_json_encode( $base_record ) ?: '' );
$available_space = 8000 - $base_size; // Per-chunk allowed size (left size).
$base_size = strlen( wp_json_encode( $base_record, JSON_INVALID_UTF8_SUBSTITUTE ) ?: '' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to add check since wp_json_encode could return false as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good eyes. Will make sure to include this in the followup PR.

[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'set_shared_sites' ],
'permission_callback' => static fn () => current_user_can( 'manage_options' ),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we create method into abstract class which needs to check for manage_options?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we want to abstract a one-line function call with a single parameter?
It's just extra boilerplate we need to context-switch, review and maintain.

I could see the argument to abstract the capability itself into a constant or filterable static function, on a long-term release roadmap. But at the same time the reason we're using manage_options is because they're wp_option, so really we should be getting rid of these endpoints entirely and replacing them with wp/v2/settings

}

// Check X-OneSearch-Token header.
$token = sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_ONESEARCH_TOKEN'] ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remainder as assuming cleanup is pending.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What specifically here needs cleanup?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usage of global vars instead of request.

*/
public function health_check(): WP_REST_Response|\WP_Error {
$requesting_origin = '';
if ( isset( $_SERVER['HTTP_X_ONESEARCH_REQUESTING_ORIGIN'] ) && ! empty( $_SERVER['HTTP_X_ONESEARCH_REQUESTING_ORIGIN'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remainder to cleanup using check_api_permissions permission callback.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, rn the plugin has separate endpoints for local/cross-site. This will be handled in a followup PR when I dedupe.

$index->delete()->wait();
} catch ( \Throwable $e ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- We need visibility.
error_log( 'Algolia Exception: ' . $e->getMessage() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

log in dev mode only.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imo, we need visibility that extends beyond dev-mode here.

Just for our own QA, we need these for our onepress-vip sites, and if we're using this on rtcamp-com then we definitely need this.

Possibly but unlikely that my Algolia refactor will get rid of these horrible try/catches that are hiding all bugs, and a logger class (shared or otherwise) is beyond the scope.

'format' => 'uri',
],
'publicKey' => [
'logo_id' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if logo is not required then can we remove it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your call.

I want to keep the diffs as close to each other, so it feels like keeping it in code but hiding it in js-land is the safest approach (especially since most the codebases arent defining or enforcing array shapes).

@justlevine justlevine requested a review from up1512001 December 8, 2025 16:31
Copy link
Member

@up1512001 up1512001 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, adding one suggestion which I think we can address in next round.

'X-OneSearch-Plugins-Token' => $our_public_key,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-OneSearch-Token' => $our_public_key,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion can we stick to one terminology either api_key or public_key.

@justlevine
Copy link
Collaborator Author

Merging. Leaving the comments open so I can track against the next PR.

@justlevine justlevine merged commit 015b400 into develop Dec 8, 2025
6 of 8 checks passed
@justlevine justlevine deleted the dev/refactor-first-pass branch December 8, 2025 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants