Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: CenCo WebServices
- Plugin URI:
- Description: Plugin fournissant des webservices destinés à renvoyer des données, en premier lieu pour l'application mobile d'OCS
- Version: 1.0
- Author: CenConnect
- Author URI:
- */
- class CencoWebServices{
- const URI = '/cenco-webservices';
- public function __construct(){
- add_action('parse_request', array($this, 'cencoWebservicesUrlHandler'));
- }
- /**
- * Interception de l'url
- */
- public function cencoWebservicesUrlHandler(){
- if(strpos($_SERVER["REQUEST_URI"], self::URI) === 0){
- // Récupèration de la méthode à executer
- $method = $this->getMethod();
- $response = array();
- // Si la méthode existe, on l'execute
- if(method_exists($this, $method)){
- $response['code'] = 202;
- $response['content'] = $this->$method();
- // Sinon on renvoie une 404
- }else{
- $response['code'] = 404;
- }
- // Renvoi de la réponse au format json
- echo json_encode($response);
- exit;
- }
- }
- /**
- * Renvoie la méthode à executer en fonction de l'action demandée
- *
- * Pour cela, on convertit le nom de l'action demandée (nom-action)
- * en nom de méthode (nomMethode)
- *
- * @return string Nom de la méthode à executer
- */
- public function getMethod(){
- if($_GET['action']){
- $words = explode('-', $_GET['action']);
- $capitalizedWords = array(
- array_shift($words),
- );
- foreach($words as $word){
- $capitalizedWords[] = ucfirst($word);
- }
- return implode('', $capitalizedWords);
- }
- }
- /**
- * Récupère la liste des intervenants
- * @return array
- */
- public function getSpeakers(){
- $args = array(
- 'post_type' => 'speaker',
- 'posts_per_page' => -1,
- 'tax_query' => array(
- array(
- 'taxonomy' => 'group',
- 'field' => 'slug',
- 'terms' => array('speak'),
- )
- ),
- );
- $my_query = new WP_Query($args);
- $speakers = array();
- foreach($my_query->get_posts() as $speaker){
- $speaker->post_content = htmlentities($speaker->post_content, ENT_QUOTES);
- $speakers[] = $speaker;
- }
- return $speakers;
- }
- }
- new CencoWebServices();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement