You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
cosmopet-architecture/wp-content/themes/cosmopet/modules/profile/module-ajax-controller.php

133 lines
4.4 KiB

<?php
// include_module('profile');
// Сохранить значения полей
add_action('wp_ajax_edit_user', 'save_custom_user_profile_fields');
add_action('wp_ajax_nopriv_edit_user', 'save_custom_user_profile_fields'); //
function save_custom_user_profile_fields() {
// Проверка прав пользователя
wp_update_user( array(
'ID' => get_current_user_id(),
'first_name' => $_POST['name'],
'last_name' => $_POST['l_name'],
) );
$phone = update_user_meta( get_current_user_id(), 'billing_phone', $_POST['full'] );
}
function ajax_add_pet() {
$current_user = wp_get_current_user();
// Подготовка данных
$pet_name = $_POST['name'];
$old_type = $_POST['old_type'];
if ($old_type == 'ex'){
$old= $_POST['old'];
}
$weight= $_POST['weight'];
$activity = $_POST['activity'];
$pet = $_POST['pet'];
$breed = $_POST['breed'];
$sex = $_POST['sex'];
$user = $current_user->ID;
$post_data = array(
'post_title' => $pet_name,
'post_type' => 'pets',
'post_status' => 'publish',
);
$post_id = wp_insert_post($post_data);
var_dump($post_id);
if ($post_id) {
// Добавление мета-полей
if ($old_type == 'ex'){
update_field( 'old', $old, $post_id );
}
else{
update_field( 'day', $_POST['day'], $post_id );
update_field( 'month', $_POST['month'], $post_id );
update_field( 'year', $_POST['year'], $post_id );
}
update_field( 'weight', $weight, $post_id );
update_field( 'breed', $breed, $post_id );
update_field( 'sex', $sex, $post_id );
update_field( 'type', $pet, $post_id );
update_field( 'user', $user, $post_id );
update_field( 'activity', $activity, $post_id );
if($_POST['sterilized']=='1' && $pet=='cat'){
update_field( 'sterilized', true );
}
else{
update_field( 'sterilized', false );
}
wp_send_json_success('Питомец успешно добавлен!');
} else {
wp_send_json_error('Ошибка при добавлении питомца.');
}
wp_die(); // Завершение работы
}
add_action('wp_ajax_add_pet', 'ajax_add_pet');
add_action('wp_ajax_nopriv_add_pet', 'ajax_add_pet'); // Если нужно разрешить для незалогиненных пользователей
function ajax_edit_pet() {
$current_user = wp_get_current_user();
// Подготовка данных
$pet_name = $_POST['name'];
$old_type = $_POST['old_type'];
if ($old_type == 'ex'){
$old= $_POST['old'];
}
else{
$old_acc = $_POST['day'] . ' ' . $_POST['month'] . ' ' . $_POST['year'];
}
$weight= $_POST['weight'];
$activity = $_POST['activity'];
$pet = $_POST['pet'];
$breed = $_POST['breed'];
$sex = $_POST['sex'];
$user = $current_user->ID;
$post_id = intval($_POST['pet_id']);
if (get_field('user', $post_id) == $user) {
// Добавление мета-полей
if ($old_type == 'ex'){
update_field( 'old', $old, $post_id );
}
else{
update_field( 'old', '', $post_id );
update_field( 'day', $_POST['day'], $post_id );
update_field( 'month', $_POST['month'], $post_id );
update_field( 'year', $_POST['year'], $post_id );
}
update_field( 'weight', $weight, $post_id );
update_field( 'breed', $breed, $post_id );
update_field( 'sex', $sex, $post_id );
update_field( 'type', $pet, $post_id );
update_field( 'activity', $activity, $post_id );
if($_POST['sterilized']=='1' && $pet=='cat'){
update_field( 'sterilized', true );
}
else{
update_field( 'sterilized', false );
}
wp_send_json_success('Питомец успешно отредактирован!');
} else {
wp_send_json_error('Ошибка при редактировании питомца.');
}
wp_die(); // Завершение работы
}
add_action('wp_ajax_edit_pet', 'ajax_edit_pet');
add_action('wp_ajax_nopriv_edit_pet', 'ajax_edit_pet');