Advertisement
Guest User

cenco-webservices

a guest
Aug 24th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | None | 0 0
  1. <?php
  2. /*
  3.   Plugin Name: CenCo WebServices
  4.   Plugin URI:
  5.   Description: Plugin fournissant des webservices destinés à renvoyer des données, en premier lieu pour l'application mobile d'OCS
  6.   Version: 1.0
  7.   Author: CenConnect
  8.   Author URI:
  9. */
  10.  
  11. class CencoWebServices{
  12.     const URI = '/cenco-webservices';
  13.  
  14.     public function __construct(){
  15.         add_action('parse_request', array($this, 'cencoWebservicesUrlHandler'));
  16.     }
  17.  
  18.     /**
  19.      * Interception de l'url
  20.      */
  21.     public function cencoWebservicesUrlHandler(){
  22.         if(strpos($_SERVER["REQUEST_URI"], self::URI) === 0){
  23.             // Récupèration de la méthode à executer
  24.             $method = $this->getMethod();
  25.  
  26.             $response = array();
  27.  
  28.             // Si la méthode existe, on l'execute
  29.             if(method_exists($this, $method)){
  30.                 $response['code'] = 202;
  31.                 $response['content'] = $this->$method();
  32.  
  33.             // Sinon on renvoie une 404
  34.             }else{
  35.                 $response['code'] = 404;
  36.             }
  37.  
  38.             // Renvoi de la réponse au format json
  39.             echo json_encode($response);
  40.  
  41.             exit;
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * Renvoie la méthode à executer en fonction de l'action demandée
  47.      *
  48.      * Pour cela, on convertit le nom de l'action demandée (nom-action)
  49.      *  en nom de méthode (nomMethode)
  50.      *
  51.      * @return string Nom de la méthode à executer
  52.      */
  53.     public function getMethod(){
  54.         if($_GET['action']){
  55.             $words = explode('-', $_GET['action']);
  56.             $capitalizedWords = array(
  57.                 array_shift($words),
  58.             );
  59.             foreach($words as $word){
  60.                 $capitalizedWords[] = ucfirst($word);
  61.             }
  62.             return implode('', $capitalizedWords);
  63.         }
  64.     }
  65.  
  66.     /**
  67.      * Récupère la liste des intervenants
  68.      * @return array
  69.      */
  70.     public function getSpeakers(){
  71.         $args = array(
  72.             'post_type' => 'speaker',
  73.             'posts_per_page' => -1,
  74.             'tax_query' => array(
  75.                 array(
  76.                     'taxonomy' => 'group',
  77.                     'field' => 'slug',
  78.                     'terms' => array('speak'),
  79.                 )
  80.             ),
  81.         );
  82.  
  83.         $my_query = new WP_Query($args);
  84.  
  85.         $speakers = array();
  86.         foreach($my_query->get_posts() as $speaker){
  87.             $speaker->post_content = htmlentities($speaker->post_content, ENT_QUOTES);
  88.             $speakers[] = $speaker;
  89.         }
  90.  
  91.         return $speakers;
  92.     }
  93. }
  94.  
  95. new CencoWebServices();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement