Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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 35 .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 5 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 7 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 50 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,
{},
);
});
});
Morty Proxy This is a proxified and sanitized view of the page, visit original site.