Created
November 7, 2023 06:02
-
-
Save dan-zakirov/6caf185298b5051b8e762b99a3677395 to your computer and use it in GitHub Desktop.
Дата создания поста в сайт баре Gutenberg
This file contains hidden or 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
add_action('enqueue_block_editor_assets', function () { | |
ob_start(); | |
?> | |
<script> | |
window.onload = function () { | |
const el = wp.element.createElement; | |
const { registerPlugin } = wp.plugins; | |
const { PluginSidebar, PluginSidebarMoreMenuItem } = wp.editPost; | |
const ulStyle = { | |
padding: '16px', | |
}; | |
const liStyle = { | |
listStyleType: 'none', | |
}; | |
registerPlugin('post-date-sidebar', { | |
render: function () { | |
const { getCurrentPost } = wp.data.select('core/editor'); | |
const post = getCurrentPost(); | |
if (post) { | |
const createdDate = post.date ? new Date(post.date).toLocaleString() : 'Дата не установлена'; | |
const modifiedDate = post.modified ? new Date(post.modified).toLocaleString() : sameDateMessage; | |
let sameDateMessage = ''; | |
if (createdDate !== 'Дата не установлена' && modifiedDate !== 'Дата не установлена' && createdDate === modifiedDate) { | |
sameDateMessage = 'нет обновления'; | |
} | |
return el(PluginSidebar, { | |
name: 'post-creation-date-sidebar', | |
icon: 'calendar-alt', | |
title: 'Дата создания поста', | |
}, | |
el('ul', { style: ulStyle }, | |
el('li', { style: liStyle }, `Дата создания: ${createdDate}`), | |
el('li', { style: liStyle }, `Дата обновления: ${sameDateMessage || modifiedDate}`) | |
) | |
); | |
} else { | |
return null; | |
} | |
}, | |
}); | |
// Добавляем элемент в меню "Дополнительно" | |
registerPlugin('post-creation-date-sidebar-more-menu-item', { | |
render: function () { | |
return el(PluginSidebarMoreMenuItem, { | |
target: 'post-creation-date-sidebar', | |
}, 'Дата создания поста'); | |
}, | |
}); | |
} | |
</script> | |
<?php | |
wp_add_inline_script('wp-blocks', str_replace(['<script>', '</script>'], '', ob_get_clean())); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment