generated from alleyinteractive/create-wordpress-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Issue-97: Remove dependency on Fieldmanager and manually create admin pages #121
Draft
nikkifurls
wants to merge
13
commits into
develop
Choose a base branch
from
feature/issue-97/remove-fieldmanager-dependency
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
656cb9c
WIP: issue-97
nikkifurls 7b5c4fd
Adds `ImagePicker` Gutenberg component
nikkifurls b6da62e
Changes the "General Settings" admin page from Fieldmanager to Gutenberg
nikkifurls fc63dfc
Modifies settings logic to remove `footer_settings` array
nikkifurls 8dcf1a4
Fixes linting issues in `<ImagePicker />`
nikkifurls f068694
Merge branch 'develop' into feature/issue-97/remove-fieldmanager-depe…
nikkifurls b692326
Updates `blocks/footer/edit.tsx` to match `develop`
nikkifurls 7fe9282
Fixes dependency array name in `Settings->register_scripts()`
nikkifurls 29b63ff
Updates submenu page slub in `Settings->register_submenu_page()`
nikkifurls 579f560
Updates admin settings page classes
nikkifurls 2bce75b
Fixes phpcs issues in `Settings` class
nikkifurls 6b37324
Updates "General Settings" page element class
nikkifurls 86ea32c
Changes the "Email Types" admin page from Fieldmanager to Gutenberg
nikkifurls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.image-picker { | ||
&__button-group { | ||
display: flex; | ||
gap: 5px; | ||
margin-bottom: 5px; | ||
} | ||
|
||
&__preview { | ||
max-height: 200px; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* eslint-disable camelcase */ | ||
import { __ } from '@wordpress/i18n'; | ||
import { BaseControl, Button, ButtonGroup } from '@wordpress/components'; | ||
import { useEffect, useRef, useState } from 'react'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import type { WP_REST_API_Attachment } from 'wp-types'; | ||
import './index.scss'; | ||
|
||
type ImagePickerProps = { | ||
label: string; | ||
onChange: (value: number) => void; | ||
value: number; | ||
}; | ||
|
||
type MediaLibraryOptions = { | ||
library: { | ||
type: string; | ||
}; | ||
}; | ||
|
||
type MediaLibrarySelection = { | ||
id: number; | ||
url: string; | ||
}; | ||
|
||
declare global { | ||
interface Window { | ||
wp: { | ||
media: (options: MediaLibraryOptions) => any; | ||
}; | ||
} | ||
} | ||
|
||
export default function ImagePicker({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have an image picker component available as part of |
||
label, | ||
onChange, | ||
value, | ||
}: ImagePickerProps) { | ||
const imagePreviewRef = useRef<HTMLImageElement>(null); | ||
const imagePreview = imagePreviewRef.current as HTMLImageElement; | ||
const [imageUrl, setImageUrl] = useState(''); | ||
|
||
/** | ||
* Handle the Media Library modal logic. | ||
* @returns Promise | ||
*/ | ||
const openMediaLibraryModal = () => new Promise((resolve, reject) => { | ||
// Create the Media Library object. Restrict to images only. | ||
const mediaLibrary = window?.wp?.media({ | ||
library: { | ||
type: 'image', | ||
}, | ||
}); | ||
|
||
if (!mediaLibrary) { | ||
reject(); | ||
} | ||
|
||
// Set up the select event listener. On success, returns a promise with the image ID and URL. | ||
mediaLibrary?.on('select', () => { | ||
const selectedImage = mediaLibrary?.state()?.get('selection')?.first(); | ||
|
||
if (!selectedImage) { | ||
reject(); | ||
} | ||
|
||
const { | ||
attributes: { | ||
id = 0, | ||
url = '', | ||
} = {}, | ||
} = selectedImage; | ||
|
||
resolve({ id, url }); | ||
}); | ||
|
||
// Open the Media Library modal. | ||
mediaLibrary?.open(); | ||
}); | ||
|
||
/** | ||
* Select an image. | ||
*/ | ||
const selectImage = async () => { | ||
const imageData = await openMediaLibraryModal() as MediaLibrarySelection; | ||
|
||
const { | ||
id = 0, | ||
url = '', | ||
} = imageData; | ||
|
||
// Pass the selected attachment ID to the onChange event. | ||
onChange(id); | ||
|
||
// Update the image URL state. | ||
setImageUrl(url); | ||
}; | ||
|
||
/** | ||
* Clear the selected image. | ||
*/ | ||
const clearImage = () => { | ||
onChange(0); | ||
setImageUrl(''); | ||
}; | ||
|
||
/** | ||
* Fetch the image URL from the REST API when the image ID changes. | ||
*/ | ||
useEffect(() => { | ||
if (!value) { | ||
return; | ||
} | ||
|
||
// Get the image url from the REST API and update the image preview. | ||
apiFetch({ path: `/wp/v2/media/${value}` }) | ||
.then((response) => { | ||
const { source_url: url = '' } = response as WP_REST_API_Attachment; | ||
setImageUrl(url); | ||
}) | ||
.catch(() => { | ||
setImageUrl(''); | ||
}); | ||
}, [value]); | ||
|
||
/** | ||
* Update the image preview when the image URL changes. | ||
*/ | ||
useEffect(() => { | ||
if (!imageUrl || !imagePreview) { | ||
return; | ||
} | ||
|
||
imagePreview.src = imageUrl; | ||
}, [imageUrl, imagePreview]); | ||
|
||
return ( | ||
<BaseControl label={label}> | ||
<ButtonGroup className="image-picker__button-group"> | ||
<Button | ||
onClick={selectImage} | ||
variant="secondary" | ||
> | ||
{__('Select an Image', 'wp-newsletter-builder')} | ||
</Button> | ||
<Button | ||
disabled={!imageUrl} | ||
onClick={clearImage} | ||
variant="secondary" | ||
> | ||
{__('Clear Image', 'wp-newsletter-builder')} | ||
</Button> | ||
</ButtonGroup> | ||
<img | ||
alt="" | ||
className="image-picker__preview" | ||
ref={imagePreviewRef} | ||
src={imageUrl} | ||
/> | ||
</BaseControl> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import '@/scss/admin-settings/notice.scss'; | ||
import '@/scss/admin-settings/sortable-item.scss'; | ||
import '@/scss/admin-settings/wrapper-group.scss'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { StrictMode } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
// Components. | ||
import AdminEmailTypes from './index'; | ||
|
||
const element = document.getElementById('wp-newsletter-builder-settings__page-email-types'); | ||
|
||
if (element) { | ||
const root = createRoot(element); | ||
|
||
if (root) { | ||
root.render( | ||
<StrictMode> | ||
<AdminEmailTypes /> | ||
</StrictMode>, | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recognize that you didn't change the underlying code here, but it would be good to validate that what comes back from the API is in the proper shape. What I usually do here is create a function that takes an
unknown
parameter and guarantees a return type, andthrow
s if it can't. Thezod
library is very good for this and I can help point you in the right direction for how to implement it (it's pretty straightforward but I have examples I can share).