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.1.-2.1.2. here... | |
return ( | |
<div className={ className }> | |
<!-- Call components to display your block here... --> | |
</div> | |
); | |
}; |
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 update_initiative( WP_REST_Request $request ) { | |
$post_id = $request->get_param( 'id' ); | |
$block_id = $request->get_param( 'block_id' ); | |
$post_content = get_post_field( 'post_content', $post_id ); | |
$post_blocks = parse_blocks( $post_content ); | |
// Update usage count for target block. | |
$post_blocks = array_map( function( $block ) use ( $block_id ) { | |
if ( 'rave/initiative-tracker' !== ( $block['blockName'] ?? '' ) || ( $block['attrs']['id'] ?? 0 ) !== $block_id ) { |
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
const FrontendTracker = ( props ) => { | |
// Code from 2.1.1. here... | |
const [ attributes, setAttributes ] = useState( { | |
block_id: 0, | |
} ); | |
useEffect( () => { | |
setAttributes( { | |
...dataAttributes, | |
} ); |
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 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
<?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
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
<?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.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, |