<?php

namespace Linksy\SearchAndReplace\Plugli\Standard\Helpers;

/**
 * Ajax
 */
class Ajax
{
    protected static function _prefix($p, $name, $nopriv = \false)
    {
        $prefix = $nopriv ? 'wp_ajax_nopriv' : 'wp_ajax';
        return $prefix . $p . $name;
    }
    public static function prefix($name, $nopriv = \false)
    {
        return self::_prefix('_plugli_', $name, $nopriv);
    }
    public static function success($data)
    {
        wp_send_json_success($data);
        wp_die();
    }
    public static function error($data, $code = 400)
    {
        wp_send_json_error($data, $code);
        wp_die();
    }
    public static function authorise($key, $nonce = null)
    {
        $user = wp_get_current_user();
        if (empty($nonce) || !wp_verify_nonce($nonce, $user->ID . $key)) {
            wp_send_json_error('There was an error in processing the data, please reload the page and try again.', 401);
            wp_die();
        }
    }
}
