Skip to content

Commit

Permalink
post meta. all datetime convert to utc.
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit committed Apr 20, 2022
1 parent d91bf54 commit 7f7b319
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 113 deletions.
45 changes: 1 addition & 44 deletions includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,50 +20,7 @@ public function __construct() {
new Assets();
new Term_UI( __DIR__, $meta_key );
new Term_Meta( $meta_key );

foreach ( get_post_types() as $post_type ) {
$schema = array(
'items' => array(
'type' => 'object',
'properties' => array(
'term' => array(
'type' => 'string',
),
'is_active' => array(
'type' => 'boolean',
),
'datetime' => array(
'type' => 'string',
'format' => 'datetime',
),
),
),
);
register_post_meta(
$post_type,
'use_schedule_set_attach_datetime',
array(
'single' => true,
'type' => 'array',
'show_in_rest' => array(
'schema' => $schema,
),
)
);

register_post_meta(
$post_type,
'use_schedule_set_detach_datetime',
array(

'single' => true,
'type' => 'array',
'show_in_rest' => array(
'schema' => $schema,
),
)
);
}
new Post_Meta( 'schedule_terms' );
}

}
53 changes: 53 additions & 0 deletions includes/Post_Meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Post_Meta Class.
*
* @package Schedule_Terms
*/

namespace HAMWORKS\WP\Schedule_Terms;

/**
* Register post meta.
*/
class Post_Meta {

/**
* Constructor.
*
* @param string $meta_key post meta key.
*/
public function __construct( string $meta_key ) {
foreach ( get_post_types() as $post_type ) {
$schema = array(
'type' => 'object',
'properties' => array(
'term' => array(
'type' => 'string',
),
'type' => array(
'type' => 'string',
'enum' => array( 'attach', 'detach' ),
),
'datetime' => array(
'type' => 'string',
'format' => 'datetime',
),
),
);

register_post_meta(
$post_type,
$meta_key,
array(

'single' => false,
'type' => 'object',
'show_in_rest' => array(
'schema' => $schema,
),
)
);
}
}
}
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
},
"dependencies": {
"@types/wordpress__components": "^19.3.1",
"@types/wordpress__core-data": "^2.4.5",
"@types/wordpress__data": "^6.0.0",
"@types/wordpress__edit-post": "^4.0.1",
"@types/wordpress__editor": "^11.0.0",
Expand Down
103 changes: 103 additions & 0 deletions src/editor/components/DatetimeControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// @ts-ignore
import { useEntityProp } from '@wordpress/core-data';
import { useRef } from '@wordpress/element';
import {
Button,
DateTimePicker,
Dropdown,
PanelRow,
} from '@wordpress/components';
import { __experimentalGetSettings as getSettings } from '@wordpress/date';
// @ts-ignore
import moment from 'moment';

const TIMEZONELESS_FORMAT = 'YYYY-MM-DDTHH:mm:ss';

interface DatetimeControlProps {
label: string;
term: string;
postType: string;
type: 'attach' | 'detach';
}

export const DatetimeControl = ( {
term,
label,
postType,
type,
}: DatetimeControlProps ) => {
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );
const anchorRef = useRef();

const getTimezoneOffsetString = () => {
// @ts-ignore
const { timezone } = getSettings();
const [ hour, time ] = timezone.offset.toString().split( '.' );
return `${ Number( hour ) > 0 ? '+' : '-' }${ String( Math.abs( hour ) ).padStart( 2, '0' ) }:${ String(
Math.floor( Number( `0.${ time || 0 }` ) * 60 )
).padStart( 2, '0' ) }`;
};

const updateDatetime = ( datetime: string ) => {
const otherItems = meta.schedule_terms?.filter( ( item ) => {
return !(
item.term === term && item.type === type
);
} );
setMeta( {
...meta,
schedule_terms: [
...otherItems,
datetime && {
term,
type,
// convert to UTC.
datetime: moment(`${ datetime }${ getTimezoneOffsetString() }`).utc().format(),
},
].filter( ( e ) => e ),
} );
};

const getDatetime = () => {
const val = meta.schedule_terms?.find( ( item ) => {
return item.term === term && item.type === type;
} );

if ( val?.datetime ) {
return moment(val.datetime).utcOffset( getTimezoneOffsetString() ).format( TIMEZONELESS_FORMAT );
}

return undefined;
};

return (
// @ts-ignore
<PanelRow ref={ anchorRef }>
<span>{ label }</span>
<Dropdown
// @ts-ignore
popoverProps={ { anchorRef: anchorRef.current } }
position="bottom left"
renderToggle={ ( { onToggle, isOpen } ) => (
<>
<Button
onClick={ onToggle }
aria-expanded={ isOpen }
variant="tertiary"
>
{ getDatetime() || 'none' }
</Button>
</>
) }
renderContent={ () => (
<div>
<DateTimePicker
currentDate={ getDatetime() }
onChange={ ( newDate ) => updateDatetime( newDate ) }
/>
</div>
) }
/>
</PanelRow>
);
};
96 changes: 27 additions & 69 deletions src/editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { registerPlugin } from "@wordpress/plugins";
import { useSelect } from "@wordpress/data";
import { registerPlugin } from '@wordpress/plugins';
import { useSelect } from '@wordpress/data';
// @ts-ignore
import { store as coreStore, useEntityProp } from "@wordpress/core-data";
import { store as coreStore, useEntityProp } from '@wordpress/core-data';
// @ts-ignore
import { store as editorStore } from "@wordpress/editor";
import { PluginDocumentSettingPanel } from "@wordpress/edit-post";
import { Button, DateTimePicker } from "@wordpress/components";
import { WP_Taxonomy_Name } from "wp-types"; // TODO: replace to stable api.
import { store as editorStore } from '@wordpress/editor';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { WP_Taxonomy_Name } from 'wp-types';
import { DatetimeControl } from './components/DatetimeControl';

interface Term {
id: number;
Expand All @@ -19,80 +19,38 @@ interface Term {
parent: number;
count: number;
}

interface Props {
currentPostType: string;
taxonomies: Term[];
terms: Record<string, Term[]>;
}

const ControlUI = ({ taxonomies, terms, currentPostType }: Props) => {
//const { timezone } = getDateSettings();
const [meta, setMeta] = useEntityProp("postType", currentPostType, "meta");

const updateDatetime = (key: string) => {
return (term: string, datetime: string) => {
setMeta({
...meta,
[key]: [
...meta[key]?.filter((item) => item.term !== term),
{
term,
datetime,
},
],
});
};
};

const updateAttachDateTime = updateDatetime(
"use_schedule_set_attach_datetime"
);
const updateDetachDateTime = updateDatetime(
"use_schedule_set_detach_datetime"
);

const getDatetime = (key: string) => {
return (term: string) => {
if (!Array.isArray(meta[key])) {
return "";
}
const { datetime } = meta[key].find((item) => item.term === term);
return datetime;
};
};

const getAttachDateTime = getDatetime("use_schedule_set_attach_datetime");
const getDetachDateTime = getDatetime("use_schedule_set_detach_datetime");

return (
<div>
{taxonomies?.map((taxonomy) => (
<div key={taxonomy.slug}>
{taxonomy.slug}
{terms[taxonomy.slug]?.map((term) => {
console.log(meta);
return (
{terms[taxonomy.slug] &&
terms[taxonomy.slug].length > 0 &&
terms[taxonomy.slug]?.map((term) => (
<div key={term.id}>
<h4>{term.name}</h4>
<h5>Attach Datetime</h5>
<Button>Attach Datetime</Button>
<DateTimePicker
currentDate={getAttachDateTime(term.slug)}
onChange={(newDate) =>
updateAttachDateTime(term.slug, newDate)
}
<h4>
{taxonomy.slug}: {term.name}
</h4>
<DatetimeControl
label="Attach"
term={term.slug}
type="attach"
postType={currentPostType}
/>
<h5>Detach DateTime</h5>
<DateTimePicker
currentDate={getDetachDateTime(term.slug)}
onChange={(newDate) =>
updateDetachDateTime(term.slug, newDate)
}
<DatetimeControl
label="Detach"
term={term.slug}
type="detach"
postType={currentPostType}
/>
</div>
);
})}
))}
</div>
))}
</div>
Expand All @@ -110,7 +68,7 @@ const PluginDocumentSetting = () => {
);
const _terms = Object.fromEntries(
_taxonomies.map((taxonomy) => {
const terms = getEntityRecords("taxonomy", taxonomy.slug, {
const terms = getEntityRecords('taxonomy', taxonomy.slug, {
per_page: -1,
})?.filter(({ meta: { use_schedule } }) => use_schedule);
return [taxonomy.slug, terms];
Expand Down Expand Up @@ -139,7 +97,7 @@ const PluginDocumentSetting = () => {
);
};

registerPlugin("schedule-terms", {
registerPlugin('schedule-terms', {
render: PluginDocumentSetting,
icon: "palmtree",
icon: 'palmtree',
});

0 comments on commit 7f7b319

Please sign in to comment.