Skip to content
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

Add a field to capture alert title #31

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions includes/alerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ function register_meta() {
'type' => 'integer',
]
);

register_post_meta(
$post_type,
'_hp_alert_title',
[
'show_in_rest' => true,
'auth_callback' => '__return_true',
'single' => true,
'type' => 'string',
]
);
}
}

Expand Down
7 changes: 7 additions & 0 deletions includes/taxonomy/alert-level.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ function enqueue_block_editor_assets() {
$asset_data['version'],
true
);

wp_enqueue_style(
'alert-level-box',
HP_ALERTS_PLUGIN_URL . '/build/index.css',
array(),
$asset_data['version']
);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/editor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.hp-alert-settings > .components-base-control + * {
margin-top: 20px;
}
82 changes: 50 additions & 32 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import {
DateTimePicker,
RadioControl,
SelectControl,
TextareaControl,
} from '@wordpress/components';
import { store as coreStore, useEntityProp } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
import { addFilter } from '@wordpress/hooks';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';

import './editor.css';

const setAlertLevel = ( OriginalComponent ) => {
return ( props ) => {
const { slug } = props;
Expand Down Expand Up @@ -59,7 +62,10 @@ const setAlertLevel = ( OriginalComponent ) => {
slug
);
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );
const { _hp_alert_display_through: displayThrough } = meta;
const {
_hp_alert_display_through: displayThrough,
_hp_alert_title: title,
} = meta;

let displayThroughValue;

Expand All @@ -73,6 +79,7 @@ const setAlertLevel = ( OriginalComponent ) => {
if ( 0 === Number( termID ) ) {
setMeta( {
_hp_alert_display_through: 0,
_hp_alert_title: '',
} );
setAlertLevels( [] );
} else {
Expand All @@ -81,7 +88,7 @@ const setAlertLevel = ( OriginalComponent ) => {
};

return (
<>
<div className="hp-alert-settings">
<SelectControl
label={ __( 'Alert level', 'hp-alerts' ) }
multiple={ false }
Expand All @@ -90,36 +97,47 @@ const setAlertLevel = ( OriginalComponent ) => {
value={ alertLevels }
/>
{ 0 < alertLevels.length && (
<RadioControl
label={ __( 'Alert expires', 'hp-alerts' ) }
selected={ displayThrough ? 'yes' : 'no' }
options={ [
{
label: __( 'Yes', 'hp-alerts' ),
value: 'yes',
},
{
label: __( 'No', 'hp-alerts' ),
value: 'no',
},
] }
onChange={ ( value ) => {
if ( 'no' === value ) {
setMeta( {
_hp_alert_display_through: 0,
} );
} else {
// Convert to a unix timestamp before storing. Milliseconds!
const storeDate = Math.round(
new Date().getTime() / 1000
);

setMeta( {
_hp_alert_display_through: storeDate,
} );
<>
<TextareaControl
help={ __(
'Override the displayed title. The post title is displayed by default.',
'hp-alerts'
) }
label={ __( 'Title (optional)', 'hp-alerts' ) }
onChange={ ( value ) =>
setMeta( { _hp_alert_title: value } )
}
} }
/>
value={ title }
/>
<RadioControl
label={ __( 'Alert expires', 'hp-alerts' ) }
selected={ displayThrough ? 'yes' : 'no' }
options={ [
{
label: __( 'Yes', 'hp-alerts' ),
value: 'yes',
},
{
label: __( 'No', 'hp-alerts' ),
value: 'no',
},
] }
onChange={ ( value ) => {
if ( 'no' === value ) {
setMeta( { _hp_alert_display_through: 0 } );
} else {
// Convert to a unix timestamp before storing. Milliseconds!
const storeDate = Math.round(
new Date().getTime() / 1000
);

setMeta( {
_hp_alert_display_through: storeDate,
} );
}
} }
/>
</>
) }
{ 0 < alertLevels.length && 0 !== displayThrough && (
<DateTimePicker
Expand All @@ -137,7 +155,7 @@ const setAlertLevel = ( OriginalComponent ) => {
__nextRemoveResetButton={ true }
/>
) }
</>
</div>
);
};
};
Expand Down