8. Lấy được kết quả form nhập vào (ok)

C:\xampp\htdocs\test\wp-content\plugins\plugin-name\admin\class-wpf-admin_ct.php

<?php
class WPF_Admin_CT {
  private $plugin_name;
  private $version;
  public function __construct($plugin_name, $version) {
    $this->plugin_name = $plugin_name;
    $this->version     = $version;
    add_action('admin_menu', array($this, 'add_plugin_admin_menu_ct'));
    add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'), 11);
    add_action('wp_ajax_wpf_add_ct', array($this, 'add_template'));
    add_action('wp_ajax_wpf_ct_ajax_themes_save',array($this,'save_themplate'));
  }
  public function add_plugin_admin_menu_ct() {
    add_menu_page(
      __('Product Filters', 'wpf_ct'), __('Lionel Product Filters', 'wpf_ct'), 'manage_options', 'wpf_search_ct', array($this, 'display_search_forms_ct'), 'dashicons-welcome-write-blog', '50'
    );
    $this->plugin_about_page();
  }
  public function plugin_about_page() {
    add_submenu_page(
      'wpf_search_ct',
      __('About', 'wpf'),
      __('About', 'wpf'),
      'manage_options',
      'wpf_about',
      array($this, 'create_about_page')
    );
  }
  public function display_search_forms_ct() {
    include_once 'partials/list_ct.php';
  }
  public function enqueue_scripts($hook) {
    $screen = get_current_screen();
    if ($screen->id != 'customize') {
      $plugin_dir = plugin_dir_url(__FILE__);
      wp_register_script($this->plugin_name, $plugin_dir . 'js/wpf-admin_ct.js', array('jquery'), $this->version, false);
      wp_enqueue_script($this->plugin_name);
      wp_enqueue_style($this->plugin_name, $plugin_dir . 'css/wpf-themplate.css', array(), $this->version, 'all');
    }
  }
  public function add_template() {
    check_ajax_referer($this->plugin_name . '_edit', 'nonce', true);
    if (current_user_can('manage_options')) {
      if ($_REQUEST['action'] === 'wpf_edit' && !empty($_REQUEST['slug'])) {
        global $cpt_id;
        $cpt_id = sanitize_key($_REQUEST['slug']);
      }
      include_once 'partials/form_ct.php';
    }
    wp_die();
  }
  public function save_themplate(){
    check_ajax_referer($this->plugin_name .'_them_ajax', $this->plugin_name .'_nonce', true);
    $form = new WPF_Form_CT($this->plugin_name,$this->version);
    $result = $form->save_themplate($_POST);
    if($result){
      echo  wp_json_encode($result);
    }
    wp_die();
  }
}
?>

C:\xampp\htdocs\test\wp-content\plugins\plugin-name\includes\class-wpf-form_ct.php

<?php
class WPF_Form_CT {
  protected $plugin_name;
  protected $version;
  protected $themplate_id = false;
  public function __construct($plugin_name, $version, $themplate_id = false) {
    $this->plugin_name  = $plugin_name;
    $this->version      = $version;
    $this->themplate_id = $themplate_id;
  }
  public function form() {
    $sort_cmb  = array_merge(WPF_Utils_CT::get_wc_attributes(), WPF_Utils_CT::get_default_fields());
    $languages = array(
      'en' => array(
        'name'     => '',
        'selected' => true,
      ),
    );
    natcasesort($sort_cmb);
    $layout = $data = array();
    $this->add_fields($data);
    ?>
    	<input type="hidden" value="" name="layout" id="wpf_layout" />
    	<input type="hidden" value="<?php echo $this->themplate_id ?>" id="wpf_themplate_id" name="themplate_id" />
    <?php
  }
  private function add_fields($data = array()) {
    $layouts = array(
      'vertical' => __('Vertical Layout', 'wpf'),
      'horizontal' => __('Horizontal Layout', 'wpf')
    );
    ?>
      <div class="wpf_lightbox_row">
        <div class="wpf_lightbox_label"><label for="wpf_name"><?php _e('Form Title', 'wpf'); ?></label></div>
        <div class="wpf_lightbox_input">
          <input id="wpf_name" class="wpf_towidth" type="text" value="<?php echo !empty($data['name']) ? $data['name'] : '' ?>" name="name" />
        </div>
      </div>
      <div class="wpf_lightbox_row ">
        <div class="wpf_lightbox_label"><?php _e('Layout', 'wpf'); ?></div>
        <div class="wpf_lightbox_input wpf_grid wpf_changed">
          <?php foreach ($layouts as $id => $ch): ?>
            <input id="wpf_<?php echo $id ?>" type="radio" value="<?php echo $id; ?>" name="type" <?php if ((!$data && $id === 'vertical' ) || ( isset($data['type']) && $data['type'] == $id )): ?>checked="checked"<?php endif; ?>/>
            <label title="<?php echo $ch ?>" for="wpf_<?php echo $id ?>" class="wpf_grid_<?php echo $id; ?>"></label>
          <?php endforeach; ?>
        </div>
      </div>
    <?php
  }
  public function save_themplate(array $post) {
    echo '<pre>';
      var_export($post);
    echo '</pre>';
    die();
  }
}
?>

Last updated