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
registerBlockType( 'rave/initiative-tracker', { | |
title: __( 'Initiative Tracker', 'initiative-tracker' ), | |
attributes: { | |
id: { | |
type: 'string', | |
default: '', | |
}, | |
}, | |
edit: ( props ) => { | |
const { |
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
{ null !== notice && ( | |
<span | |
className={ `notice ${notice.type}` } | |
role={ 'error' === notice.type ? 'alert' : 'status' } | |
> | |
{ notice.message } | |
</span> | |
) } |
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
const saveCharacterUpdates = async () => { | |
// Code from 4.2.2. here... | |
const response = await apiFetch( // Args defined in 4.2.2. ) | |
.then( ( success ) => { | |
// Update dataAttributes to reflect changes here... | |
// E.g., dataAttributes.someAttr = someAttr; | |
return { | |
type: 'success', | |
message: success, |
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
<?php | |
public function update_initiative( WP_REST_Request $request ) { | |
$nonce = $request->get_header( 'x_wp_nonce' ); | |
if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) { | |
return new WP_Error( 'invalid-nonce', __( 'Invalid nonce.', 'initiative-tracker' ) ); | |
} | |
// Code from 3.1.3. here... | |
} |
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
const saveCharacterUpdates = async () => { | |
// Code from 4.2.1. here... | |
setLoading( true ); | |
setNotice( null ); | |
const response = await apiFetch( { | |
path: `${path}/${ dataAttributes.post_id }`, | |
method: 'POST', | |
data: { | |
...attributes, |
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
const saveCharacterUpdates = async () => { | |
const path = initTracker.initiative, | |
nonce = initTracker.nonce; | |
if ( null === path || null === nonce ) { | |
return; | |
} | |
}; |
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
const FrontendTracker = ( props ) => { | |
// Code from 4.1.1. here... | |
const [ notice, setNotice ] = useState( null ); | |
useEffect( () => { | |
if ( null === notice ) { | |
return; | |
} | |
const timer = setTimeout( () => { |
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
const FrontendTracker = ( props ) => { | |
// Code from 2.1. here... | |
const [ isLoading, setLoading ] = useState( false ); | |
}; |
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
<?php | |
function localize_route() { | |
// Code from 3.2.1. here... | |
wp_localize_script( 'initiative-tracker-frontend-script', 'initTracker', [ | |
'nonce' => $nonce, | |
'initiative' => 'rave-initiative/v1/initiative', | |
] ); | |
} |
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
<?php | |
function localize_route() { | |
$nonce = wp_create_nonce( 'wp_rest' ); | |
} | |
add_action( 'wp_enqueue_scripts', 'localize_route' ); |