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

Adds wpAdminUrl hook #1897

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions .changeset/odd-tigers-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
'@faustwp/core': patch
---

Adds new hook wpAdminUrl that lets user customize the wp-admin url in toolbar

Usage:

```js
export class MyPlugin {
apply(hooks) {
const { addAction, addFilter } = hooks;

addFilter(
'wpAdminUrl',
'my-namespace',
(wpAdminUrl, context) => {
// replaces default wp-admin url at /wp-admin with /wp/wp-admin
return wpAdminUrl.replace('/wp-admin', '/wp/wp-admin')
},
);
}
}

/**
* @type {import('@faustwp/core').FaustConfig}
**/
export default setConfig({
templates,
plugins: [new MyPlugin()],
experimentalToolbar: true,
possibleTypes,
});

```
5 changes: 4 additions & 1 deletion packages/faustwp-core/src/lib/getAdminUrl.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getWpUrl } from './getWpUrl.js';
import { hooks } from '../wpHooks/index.js';

/**
* Retrieves the URL to the admin area for the current site.
Expand All @@ -8,7 +9,9 @@ import { getWpUrl } from './getWpUrl.js';
* @param {string} path Path relative to the admin URL.
*/
export function getAdminUrl(path = ''): string {
const adminUrl = getWpUrl('wp-admin');
let adminUrl = getWpUrl('wp-admin');

adminUrl = hooks.applyFilters('wpAdminUrl', adminUrl, {}) as string;

if (!path) {
return adminUrl;
Expand Down
7 changes: 7 additions & 0 deletions packages/faustwp-core/src/wpHooks/overloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ type FaustCoreFilters = {
priority?: number | undefined,
): void;

addFilter(
hookName: 'wpAdminUrl',
namespace: string,
callback: (wpAdminUrl: string, context: Record<string, never>) => string,
priority?: number | undefined,
): void;

addFilter(
hookName: 'toolbarNodes',
namespace: string,
Expand Down
50 changes: 50 additions & 0 deletions packages/faustwp-core/tests/lib/getAdminUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createHooks } from '@wordpress/hooks';
import { hooks } from '../../src/wpHooks/index';
import { getAdminUrl } from '../../src/lib/getAdminUrl';
import { getWpUrl } from '../../src/lib/getWpUrl';

jest.mock('@wordpress/hooks', () => ({
createHooks: jest.fn().mockReturnValue({
applyFilters: jest.fn(),
}),
}));

jest.mock('../../src/lib/getWpUrl', () => ({
getWpUrl: jest.fn().mockReturnValue('http://example.com/wp-admin'),
}));

describe('getAdminUrl', () => {
beforeEach(() => {
hooks.actions = Object.create( null );
hooks.filters = Object.create( null );
(createHooks().applyFilters as jest.Mock).mockClear();
(getWpUrl as jest.Mock).mockClear();
});

it('returns the admin URL without path when no path is provided', () => {
const mockUrl = 'http://example.com/wp-admin';
(createHooks().applyFilters as jest.Mock).mockReturnValue(mockUrl);

const result = getAdminUrl();
expect(result).toBe(mockUrl);
expect(createHooks().applyFilters).toHaveBeenCalledWith(
'wpAdminUrl',
mockUrl,
{},
);
});

it('returns the admin URL with path when path is provided', () => {
const mockUrl = 'http://example.com/wp-admin';
const path = 'test-path';
(createHooks().applyFilters as jest.Mock).mockReturnValue(mockUrl);

const result = getAdminUrl(path);
expect(result).toBe(`${mockUrl}/${path}`);
expect(createHooks().applyFilters).toHaveBeenCalledWith(
'wpAdminUrl',
mockUrl,
{},
);
});
});
Loading