Skip to content

Commit

Permalink
automatic terms
Browse files Browse the repository at this point in the history
  • Loading branch information
torounit committed Apr 20, 2022
1 parent 7f7b319 commit 101f52b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 8 deletions.
3 changes: 2 additions & 1 deletion includes/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ class Plugin {
* Constructor.
*/
public function __construct() {
$meta_key = 'use_schedule';
$meta_key = 'schedule_terms_active';
new Assets();
new Term_UI( __DIR__, $meta_key );
new Term_Meta( $meta_key );
new Post_Meta( 'schedule_terms' );
new Term_Manager( 'schedule_terms' );
}

}
3 changes: 3 additions & 0 deletions includes/Post_Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function __construct( string $meta_key ) {
'term' => array(
'type' => 'string',
),
'taxonomy' => array(
'type' => 'string',
),
'type' => array(
'type' => 'string',
'enum' => array( 'attach', 'detach' ),
Expand Down
97 changes: 97 additions & 0 deletions includes/Term_Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Term Manager.
*
* @package Schedule_Terms
*/

namespace HAMWORKS\WP\Schedule_Terms;

/**
* Term attach or detach from post.
*/
class Term_Manager {

/**
* Post meta name for save term info.
*
* @var string
*/
private $post_meta_key;

/**
* Constructor.
*
* @param string $post_meta_key Post meta key.
*/
public function __construct( string $post_meta_key ) {
$this->post_meta_key = $post_meta_key;

add_action( 'wp_after_insert_post', array( $this, 'update_schedule' ), 100, 1 );
add_action( "${post_meta_key}_update_terms", array( $this, 'update_terms' ), 10, 4 );
}

/**
* Scheduled update.
*
* @param int $post_id post ID.
*/
public function update_schedule( int $post_id ) {
$post_meta_key = $this->post_meta_key;
$meta_values = get_post_meta( $post_id, $post_meta_key, false );
wp_clear_scheduled_hook( "${post_meta_key}_update_terms", array( $post_id ) );
foreach ( $meta_values as $meta_value ) {
if ( $meta_value ) {
try {
$date_time = new \DateTime( $meta_value['datetime'] );
} catch ( \Exception $e ) {
wp_die( esc_html( $e->getMessage() ) );
}
$time = $date_time->getTimestamp();
$term = get_term_by( 'slug', $meta_value['term'], $meta_value['taxonomy'] );

if ( time() > $time ) {
if ( 'attach' === $meta_value['type'] ) {
wp_set_post_terms( $post_id, array( $term->term_id ), $meta_value['taxonomy'], true );
} else {
$term = get_term_by( 'slug', $meta_value['term'], $meta_value['taxonomy'] );
wp_remove_object_terms( $post_id, $term->term_id, $meta_value['taxonomy'] );
}
} else {
wp_schedule_single_event( $time, "${post_meta_key}_update_terms", array( $post_id ) );
}
}
}
}

/**
* @param int $post_id post ID.
*
* @return void
*/
public function update_terms( int $post_id ) {
$post_meta_key = $this->post_meta_key;
$meta_values = get_post_meta( $post_id, $post_meta_key, false );
// TODO: attach から処理するように.
foreach ( $meta_values as $meta_value ) {
if ( $meta_value ) {
try {
$date_time = new \DateTime( $meta_value['datetime'] );
} catch ( \Exception $e ) {
wp_die( esc_html( $e->getMessage() ) );
}
$time = $date_time->getTimestamp();
$term = get_term_by( 'slug', $meta_value['term'], $meta_value['taxonomy'] );

if ( time() > $time ) {
if ( 'attach' === $meta_value['type'] ) {
wp_set_post_terms( $post_id, array( $term->term_id ), $meta_value['taxonomy'], true );
} else {
$term = get_term_by( 'slug', $meta_value['term'], $meta_value['taxonomy'] );
wp_remove_object_terms( $post_id, $term->term_id, $meta_value['taxonomy'] );
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion includes/Term_UI.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function enqueue_scripts() {
protected function format_output( $meta = '' ) {
if ( $meta ) {
?>
<span data-use-schedule><?php esc_html_e( 'Use Schedule', 'schedule-posts' ); ?></span>
<span data-schedule-terms-active><?php esc_html_e( 'Use Schedule', 'schedule-posts' ); ?></span>
<?php
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ document.addEventListener('click', function (e) {
const id = tr.id;
const checked = !!document
.getElementById(id)
?.querySelector('[data-use-schedule]');
?.querySelector('[data-schedule-terms-active]');
const checkbox = document.querySelector(
'.inline-edit-row input[name=term-use_schedule]'
) as HTMLInputElement;
Expand Down
3 changes: 3 additions & 0 deletions src/editor/components/DatetimeControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const TIMEZONELESS_FORMAT = 'YYYY-MM-DDTHH:mm:ss';
interface DatetimeControlProps {
label: string;
term: string;
taxonomy: string;
postType: string;
type: 'attach' | 'detach';
}

export const DatetimeControl = ( {
term,
taxonomy,
label,
postType,
type,
Expand Down Expand Up @@ -50,6 +52,7 @@ export const DatetimeControl = ( {
...otherItems,
datetime && {
term,
taxonomy,
type,
// convert to UTC.
datetime: moment(`${ datetime }${ getTimezoneOffsetString() }`).utc().format(),
Expand Down
12 changes: 7 additions & 5 deletions src/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ const ControlUI = ({ taxonomies, terms, currentPostType }: Props) => {
<DatetimeControl
label="Attach"
term={term.slug}
taxonomy={taxonomy.slug}
type="attach"
postType={currentPostType}
/>
<DatetimeControl
label="Detach"
term={term.slug}
taxonomy={taxonomy.slug}
type="detach"
postType={currentPostType}
/>
Expand All @@ -70,7 +72,7 @@ const PluginDocumentSetting = () => {
_taxonomies.map((taxonomy) => {
const terms = getEntityRecords('taxonomy', taxonomy.slug, {
per_page: -1,
})?.filter(({ meta: { use_schedule } }) => use_schedule);
})?.filter(({ meta: { schedule_terms_active } }) => schedule_terms_active);
return [taxonomy.slug, terms];
})
);
Expand All @@ -84,9 +86,9 @@ const PluginDocumentSetting = () => {

return (
<PluginDocumentSettingPanel
name="custom-panel"
title="Custom Panel"
className="custom-panel"
name="schedule-terms"
title="Schedule Terms"
className="schedule-terms"
>
<ControlUI
currentPostType={postType}
Expand All @@ -99,5 +101,5 @@ const PluginDocumentSetting = () => {

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

0 comments on commit 101f52b

Please sign in to comment.