Wordpress ajax端点类控制器

时间:2018-06-07 17:46:16

标签: ajax wordpress class oop

我想为wordpress制作ajax验证器类。

我想要实现的是,不知何故add_endpoint为wordpress添加了操作,但添加的函数WPMonWayContentEditor->get_view()将被验证。

主要原因是我不想写每个函数$this->Ajax->validate();

将来我想在CodeIgniter中使用这个插件并为其进行ajax验证;

如何使这项工作?我没有oop的经验,我无法弄清楚

class WPMonWayContentEditor {
  private $Ajax;
  public $plugin_dir;

  private function __construct(){
    $this->plugin_dir = plugin_dir_path( __FILE__ );

    include($this->plugin_dir.'inc/MWCE_WP_Ajax_Support.php');
    $this->Ajax                 = new MWCE_WP_Ajax_Support($this);

    $this->Ajax->add_endpoint('get_view',array($this,'get_view'));
  }

  public function get_view(){

  }

<?php

class MWCE_WP_Ajax_Support {
    public $endpoints;

    private $MWCE;

    public function __construct( WPMonWayContentEditor $MWCE_Input){
        $this->MWCE = $MWCE_Input;
        $this->endpoints = array();
    }

    public function add_endpoint($endpoint='', callable $function){
        $this->endpoints[] = array(
            $endpoint => $function
        );

        add_action( 'wp_ajax_'.$endpoint ,array($this, 'validate') );
    }

    private function validate(){
        check_ajax_referer('mwce-ajax-security','security');

        $var = call_user_func(array($this->MWCE, $function);

        echo json_encode($vars);

    }
}

0 个答案:

没有答案