daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Mar 21st, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class fiche_content{    
  2.     private $file_base;
  3.     private $name;
  4.  
  5.     function __construct($jenkins_url, $jenkins_username, $jenkins_token){
  6.         $this->file_base = plugin_dir_path( dirname( __FILE__ ) ) . 'fiche.php';
  7.         $this->init();
  8.     }  
  9.  
  10.     public function init() {
  11.         add_action('wp_enqueue_scripts', array($this,'scripts_js'));
  12.  
  13.         //link PHP functions to Ajax
  14.         add_action('wp_ajax_set_name', array( $this, 'set_name' ));
  15.         add_action('wp_ajax_nopriv_set_name', array( $this, 'set_name' ));
  16.  
  17.         //if page not exists, then create a new one
  18.         if ( !$this->check_page_exist() ) add_action( 'wp_loaded', array ( $this, 'add_page_fiche' ) );      
  19.     }
  20.  
  21.     public function scripts_js(){
  22.         wp_enqueue_script('fiche_scripts', plugins_url('JS/scripts.js', __FILE__), array('jquery'), '1.0', true);
  23.         wp_localize_script( 'fiche_scripts', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ));
  24.     }    
  25.  
  26.     public function check_page_exist(){
  27.         $page = get_page_by_path("fiche-projet");
  28.         if (isset($page)) return true;
  29.         else false;
  30.     }
  31.  
  32.     public function add_page_fiche() {
  33.         $new_page = array(
  34.             'post_title'    => wp_strip_all_tags( 'Fiche Projet' ),
  35.             'post_content'  => $this->content_fiche(),
  36.             'post_status'   => 'publish',
  37.             'post_author'   => 1,
  38.             'post_type'     => 'page',
  39.         );
  40.         wp_insert_post( $new_page );
  41.     }    
  42.  
  43.     public function content_fiche(){
  44.         $html = '<div>A lot of content and here its name: '. $this->name .'
  45.                     <p>If I want to change the name:</p>
  46.                     <input id="newName" type="text" placeholder="New name">
  47.                     <button id="createNewName">New</button>
  48.                 </div>';
  49.         return $html;
  50.     }
  51.  
  52.     public function set_name(){            
  53.         $this->name = $_POST['new_name'];
  54.         $this->update_content_page();
  55.         wp_die();
  56.     }
  57.  
  58.     public function update_content_page(){
  59.         $fiche = get_page_by_title(wp_strip_all_tags( 'Fiche Projet' ));
  60.         $new_fiche = array(
  61.             'ID'           => $fiche->ID,
  62.             'post_content' => $this->content_fiche(),
  63.         );
  64.         wp_update_post( $new_fiche );
  65.     }
  66. }
  67.      
  68. jQuery(document).ready(function($) {
  69.     $(document).on('click', '#createNewName',function(e){
  70.         var newName = document.getElementById('newName').value;
  71.         $.ajax({
  72.             type: "POST",
  73.             url: ajax_object.ajaxurl,
  74.             data: {
  75.                 action: 'set_name',
  76.                 new_name: newName
  77.             },
  78.             done: function (output) {
  79.                console.log(output);
  80.             }
  81.         });
  82.         e.preventDefault();
  83.     });
  84. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top