【问题标题】:Conditionally load public and admin code with AJAX working on both sides on WordPress有条件地加载公共和管理代码,AJAX 在 WordPress 的两边都工作
【发布时间】:2020-03-03 08:45:24
【问题描述】:

我正在尝试在 WordPress 插件中有条件地加载我的前端和管理区域代码,因此创建管理区域的文件和类将在管理端加载,并且需要在前端运行的文件和类 - end 将仅在前端运行,不会触及管理区域的任何内容。

我尝试使用is_admin() 条件:

if (!is_admin()) {
    require_once(plugin_dir_path(dirname(__FILE__)) . 'public/class-public.php');
    $this->Public = new Public();
} else {
    require_once(plugin_dir_path(dirname(__FILE__)) . 'admin/class-admin.php');
    $this->Admin = new Admin();
}

代码加载很好,但 AJAX 在公共方面不起作用,因为绑定到 wp_ajax_wp_ajax_nopriv_ 操作的 AJAX 请求在 WP Admin 上下文中执行。所以我决定创建自己的isAdmin() 函数:

public static function isAdmin() {
    $currentUrl = set_url_scheme(
        sprintf(
            'http://%s%s',
            $_SERVER['HTTP_HOST'],
            $_SERVER['REQUEST_URI']
        )
    );
    $adminUrl = strtolower(admin_url());
    $referrer  = strtolower(wp_get_referer());

    if (strpos($currentUrl, $adminUrl) === 0) {
        if (strpos($referrer, $adminUrl) === 0) {
            return true;
        } else {
            if (function_exists('wp_doing_ajax')) {
                return !wp_doing_ajax();
            } else {
                return !(defined('DOING_AJAX') && DOING_AJAX);
            }
        }
    } else {
        if (!defined('REST_REQUEST') || !REST_REQUEST) {
            return false;
        }
        return (isset($_REQUEST['context']) && $_REQUEST['context'] === 'edit');
    }
}

代码加载仍然没问题,但现在 AJAX 在公共方面工作,而不是在管理方面工作。

那么,如何防止在管理区号上加载公共代码,反之亦然,同时 AJAX 在双方都工作?

【问题讨论】:

    标签: php ajax wordpress


    【解决方案1】:

    我还通过检查公共接口设法解决了这个问题。我创建了新函数isPublic() 来检查它是否是公开的。所以这是我的最终代码:

    if ($this->isPublic()) {
        require_once(plugin_dir_path(dirname(__FILE__)) . 'public/class-public.php');
        $this->Public = new Public();
    } elseif ($this->isAdmin()) {
        require_once(plugin_dir_path(dirname(__FILE__)) . 'admin/class-admin.php');
        $this->Admin = new Admin();
    }
    

    这里是帮助器 isPublic()isAdmin() 函数:

    public static function isAdmin() {
        if (function_exists('is_admin') && is_admin()) {
            return true;
        } else {
            if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    public static function isPublic() {
        if (function_exists('is_admin') && is_admin()) {
            if (function_exists('wp_doing_ajax') && wp_doing_ajax()) {
                return true;
            } else {
                return false;
            }
        } else {
            if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false) {
                if (strpos($_SERVER['REQUEST_URI'], 'admin-ajax.php') !== false) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 2019-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多