-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from kuno1/feature/multiple-auhor-selector
Feature/multiple auhor selector
- Loading branch information
Showing
28 changed files
with
23,540 additions
and
254 deletions.
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,29 @@ | ||
name: Test Plugin | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
phpcs: | ||
name: PHP Syntax Check | ||
uses: tarosky/workflows/.github/workflows/phpcs.yml@main | ||
with: | ||
version: 7.4 | ||
|
||
assets: | ||
name: Stylelint & ESLint | ||
uses: tarosky/workflows/.github/workflows/npm.yml@main | ||
with: | ||
node-version: 20 | ||
|
||
status-check: | ||
name: Status Check | ||
runs-on: ubuntu-latest | ||
if: always() | ||
needs: [ phpcs, assets ] | ||
steps: | ||
- uses: re-actors/alls-green@release/v1 | ||
with: | ||
jobs: ${{ toJSON(needs) }} |
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 |
---|---|---|
@@ -1,6 +1,4 @@ | ||
/vendor/ | ||
/node_modules/ | ||
composer.lock | ||
package-lock.json | ||
/wordpress/ | ||
/dist/ |
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,35 @@ | ||
/*! | ||
* User selector for classic editor | ||
* | ||
* @handle kvm-user-selector-classic-editor | ||
* @deps kvm-user-selector, wp-i18n, wp-components, wp-element | ||
*/ | ||
|
||
const { createRoot, render, useState } = wp.element; | ||
const { UserSelectorComponent } = kvm; | ||
|
||
// virtual-author-id[] | ||
|
||
const UserSelectorClassic = ( props ) => { | ||
const [ currentUsers, setUsers ] = useState( [] ); | ||
return ( | ||
<div className="kvm-user-selector-classic"> | ||
<UserSelectorComponent post={ props.post } onUserChange={ ( users ) => setUsers( users ) } /> | ||
{ currentUsers.map( ( user ) => { | ||
return ( | ||
<input type="hidden" name="virtual-author-id[]" value={ user.id } key={ user.id } /> | ||
); | ||
} ) } | ||
</div> | ||
); | ||
}; | ||
|
||
document.addEventListener( 'DOMContentLoaded', () => { | ||
const container = document.getElementById( 'kvm-user-selector-classic' ); | ||
const id = container.dataset.postId; | ||
if ( createRoot ) { | ||
createRoot( container ).render( <UserSelectorClassic post={ id } /> ); | ||
} else { | ||
render( <UserSelectorClassic post={ id } />, container ); | ||
} | ||
} ); |
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,147 @@ | ||
/*! | ||
* User Selector | ||
* | ||
* @handle kvm-user-selector | ||
* | ||
* @deps wp-element, wp-api-fetch, wp-components, wp-i18n | ||
*/ | ||
|
||
/* global KvmUserSelector:false */ | ||
|
||
const { useState, useEffect } = wp.element; | ||
const { Button, Icon, ComboboxControl } = wp.components; | ||
const { apiFetch } = wp; | ||
|
||
const UserToken = ( { user, onUserDelete } ) => { | ||
return ( | ||
<Button className="kvm-user-token" onClick={ () => onUserDelete( user ) } | ||
iconPosition="right" variant="secondary"> | ||
{ user.name } | ||
<small>({ user.group.length > 0 ? user.group.map( ( g ) => g.name ).join( ', ' ) : '---' })</small> | ||
<Icon icon="no-alt" /> | ||
</Button> | ||
); | ||
}; | ||
|
||
const UserComboBox = ( { onUserSelected } ) => { | ||
const [ query, setQuery ] = useState( '' ); | ||
const [ users, setUsers ] = useState( [] ); | ||
const [ selectedOption, setSelectedOption ] = useState( null ); | ||
const [ timer, setTimer ] = useState( null ); | ||
|
||
// Do incremental search | ||
useEffect( () => { | ||
if ( timer ) { | ||
clearTimeout( timer ); | ||
} | ||
setTimer( setTimeout( () => { | ||
if ( query ) { | ||
// Search authros via API | ||
const fetchOptions = async () => { | ||
const response = await apiFetch( { path: `/kvm/v1/authors/search?s=${ query }` } ); | ||
setUsers( response ); | ||
}; | ||
try { | ||
fetchOptions(); | ||
} catch ( error ) { | ||
setUsers( [] ); | ||
} | ||
} else { | ||
setUsers( [] ); | ||
} | ||
}, 300 ) ); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ query ] ); | ||
|
||
return ( | ||
<ComboboxControl | ||
label={ KvmUserSelector.slabel } | ||
placeholder={ KvmUserSelector.search } | ||
value={ selectedOption } | ||
options={ users.map( ( user ) => { | ||
return { label: user.name, value: user.id }; | ||
} ) } | ||
onChange={ ( value ) => { | ||
// User is selected. | ||
users.forEach( ( user ) => { | ||
if ( user.id === value ) { | ||
onUserSelected( user ); | ||
} | ||
} ); | ||
setSelectedOption( '' ); | ||
} } | ||
onFilterValueChange={ ( newQuery ) => setQuery( newQuery ) } | ||
/> | ||
); | ||
}; | ||
|
||
/** | ||
* User Select components. | ||
*/ | ||
|
||
const UserSelectorComponent = ( { post, onUserChange } ) => { | ||
const [ users, setUsers ] = useState( [] ); | ||
|
||
useEffect( () => { | ||
const fetchUsers = async () => { | ||
try { | ||
const response = await apiFetch( { | ||
path: `/kvm/v1/authors/of/${ post }`, | ||
} ); | ||
setUsers( response ); | ||
} catch ( error ) { | ||
// eslint-disable-next-line no-undef | ||
alert( error.message || 'Error' ); | ||
} | ||
}; | ||
fetchUsers(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
onUserChange( users ); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [ users ] ); | ||
|
||
const removeUser = ( user ) => { | ||
const newUsers = []; | ||
users.map( ( u ) => { | ||
if ( u.id !== user.id ) { | ||
newUsers.push( u ); | ||
} | ||
return u; | ||
} ); | ||
setUsers( newUsers ); | ||
}; | ||
|
||
return ( | ||
<> | ||
{ users.length > 0 ? ( | ||
<div className="kvm-user-token-wrapper"> | ||
{ | ||
users.map( ( user ) => { | ||
return <UserToken key={ user.id } user={ user } | ||
onUserDelete={ ( u ) => removeUser( u ) } />; | ||
} ) | ||
} | ||
</div> | ||
) : ( | ||
<div className="notice notice-error notice-alt"> | ||
<p>{ KvmUserSelector.nouser }</p> | ||
</div> | ||
) } | ||
<UserComboBox onUserSelected={ ( user ) => { | ||
const userIds = users.map( ( u ) => u.id ); | ||
if ( userIds.includes( user.id ) ) { | ||
return; | ||
} | ||
setUsers( users.concat( [ user ] ) ); | ||
} } /> | ||
</> | ||
); | ||
}; | ||
|
||
// Export components. | ||
const kvm = window.kvm || {}; | ||
kvm.UserSelectorComponent = UserSelectorComponent; | ||
window.kvm = kvm; |
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 @@ | ||
/*! | ||
* User selector | ||
* | ||
* @handle kvm-user-selector | ||
* @deps wp-components | ||
*/ | ||
|
||
.kvm-user-token { | ||
display: inline-block; | ||
} | ||
|
||
.kvm-user-token-wrapper { | ||
margin: 1rem 0; | ||
display: flex; | ||
flex-wrap: wrap; | ||
gap: 1em; | ||
justify-content: flex-start; | ||
align-items: flex-start; | ||
} |
Oops, something went wrong.