add perevod to admin

This commit is contained in:
arlemp@selectel.ru
2021-12-09 02:03:37 +03:00
parent 58f63ff8b6
commit f989783b48
2 changed files with 82 additions and 29 deletions

View File

@@ -15,6 +15,7 @@ require ABSPATH.'/vendor/autoload.php';
require get_template_directory().'/src/calend-block.php'; require get_template_directory().'/src/calend-block.php';
require get_template_directory().'/src/calendar.php'; require get_template_directory().'/src/calendar.php';
require get_template_directory().'/src/perevod.php';
//require ABSPATH.'vendor/masterforweb/db_lite/db_lite.php'; //require ABSPATH.'vendor/masterforweb/db_lite/db_lite.php';

View File

@@ -1,22 +1,15 @@
<?php <?php
// добааить поле переводчика
/**add_action( 'init', function (){
register_post_meta( 'post', 'perevod', array( add_action( 'load-post.php', 'perevod_post_meta_boxes_setup' );
'show_in_rest' => true, add_action( 'load-post-new.php', 'perevod_post_meta_boxes_setup' );
'single' => true,
'type' => 'string'
));
});**/
function perevod_post_meta_boxes_setup() {
add_action( 'add_meta_boxes', function (){ add_action( 'add_meta_boxes', function (){
add_meta_box( add_meta_box(
'subscript-post', 'perevod-post',
'Переводчик', 'Перевод',
'perevod_post_class_meta_box', 'perevod_post_class_meta_box',
'post', 'post',
'side', 'side',
@@ -25,15 +18,74 @@ add_action( 'add_meta_boxes', function (){
}); });
add_action( 'save_post', function ($post_id, $post){
// проверяем, может ли текущий юзер редактировать пост
$post_type = get_post_type_object( $post->post_type );
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
return $post_id;
}
// ничего не делаем для автосохранений
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
}
$curr_id = 0;
if( isset( $_POST[ 'perevod_name' ] )) {
$curr_id = (int) $_POST[ 'perevod_name' ];
}
if ($curr_id == 0) {
$meta_value = get_post_meta( $post_id, $meta_key, true );
if ($meta_value !== false) {
delete_post_meta( $post_id, 'perevod_name' );
}
}
else {
update_post_meta( $post_id, 'perevod_name', sanitize_text_field( $_POST[ 'perevod_name' ] ) );
}
return $post_id;
}
, 10, 2 );
}
function perevod_post_class_meta_box( $post ) {?> function perevod_post_class_meta_box( $post ) {?>
<?$args = array(
'role__in' => array( administrator, author, editor ),
'orderby' => 'display_name',
'order' => 'ASC',
);
$authors = get_users( $args );
$perevod_name = get_post_meta( $post->ID, 'perevod_name', true );
?>
<div class="components-base-control editor-post-excerpt__textarea"> <div class="components-base-control editor-post-excerpt__textarea">
<div class="components-base-control__field"> <div class="components-base-control__field">
<p>
<label class="components-base-control__label" for="perevod-name">Переводчик</label>
<input type="text" name="perevod-name" id="perevod-name" class="edit-post-post-schedule" value="<?php echo esc_attr( get_post_meta( $post->ID, 'perevod-name', true ) ); ?>"> <div><label class="components-base-control__label" for="perevod_name">Выбрать переводчика</label></div>
</p> <div class="components-input-control__container css-ebmrdk-Container e1cr7zh11">
<select name="perevod_name" id="perevod_name" class="components-select-control__input">
<option value="0">-</option>
<?foreach ($authors as $author):?>
<option value="<?=$author->ID?>" <?if ($author->ID == $perevod_name):?> selected<?endif?>><?=$author->display_name?></option>
<?endforeach?>
</select>
</div>
</div> </div>
</div> </div>
<?php } <?}