Skip to content

Commit

Permalink
feat: Add campaign field to Donate block
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiulodro committed Jan 24, 2020
1 parent 2faed01 commit 8268836
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/blocks/donate/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Edit extends Component {
/* If block is in "manual" mode, override certain state properties with values stored in attributes */
blockData() {
const { attributes } = this.props;
const { manual } = attributes;
const { manual, campaign } = attributes;
const data = { ...this.state, ...( manual ? attributes : {} ) };
if ( manual ) {
data.customDonationAmounts = {
Expand All @@ -59,6 +59,7 @@ class Edit extends Component {
year: data.tiered ? 12 * data.suggestedAmounts[ 1 ] : 12 * data.suggestedAmountUntiered,
};
}
data.campaign = campaign;
return data;
}

Expand Down Expand Up @@ -437,7 +438,8 @@ class Edit extends Component {
}

render() {
const { manual } = this.blockData();
const { setAttributes } = this.props;
const { manual, campaign } = this.blockData();
return (
<Fragment>
{ this.renderPlaceholder() }
Expand Down Expand Up @@ -470,6 +472,17 @@ class Edit extends Component {
{ this.renderManualControls() }
</PanelBody>
) }
<PanelBody title={ __( 'Advanced', 'newspack-blocks' ) }>
<TextControl
label={ __( 'Campaign ID', 'newspack-blocks' ) }
value={ campaign ? campaign : '' }
onChange={ value =>
setAttributes( {
campaign: value,
} )
}
/>
</PanelBody>
</InspectorControls>
</Fragment>
);
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/donate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ export const settings = {
type: 'boolean',
default: true,
},
campaign: {
type: 'string',
},
},
supports: {
html: false,
Expand Down
11 changes: 11 additions & 0 deletions src/blocks/donate/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function newspack_blocks_render_block_donate( $attributes ) {
$selected_frequency = 'month';
$suggested_amounts = $settings['suggestedAmounts'];

$campaign = $attributes['campaign'] ?? false;

$uid = rand(); // Unique identifier to prevent labels colliding with other instances of Donate block.

ob_start();
Expand Down Expand Up @@ -102,6 +104,9 @@ class='donate-label'
<button type='submit'>
<?php echo esc_html__( 'Donate now!', 'newspack-blocks' ); ?>
</button>
<?php if ( $campaign ) : ?>
<input type='hidden' name='campaign' value='<?php echo esc_attr( $campaign ); ?>' />
<?php endif; ?>
</form>
</div>
<?php
Expand Down Expand Up @@ -199,6 +204,9 @@ class='other-donate-label'
<button type='submit'>
<?php echo esc_html__( 'Donate now!', 'newspack-blocks' ); ?>
</button>
<?php if ( $campaign ) : ?>
<input type='hidden' name='campaign' value='<?php echo esc_attr( $campaign ); ?>' />
<?php endif; ?>
</form>
</div>
<?php
Expand Down Expand Up @@ -233,6 +241,9 @@ function newspack_blocks_register_donate() {
'type' => 'boolean',
'default' => true,
],
'campaign' => [
'type' => 'string',
]
),
'render_callback' => 'newspack_blocks_render_block_donate',
)
Expand Down

0 comments on commit 8268836

Please sign in to comment.