mbcreation

WP REST API

Jun 22nd, 2016
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.05 KB | None | 0 0
  1. public function register_post_type(){
  2.  
  3.     require( dirname(__FILE__).'/class-wp-rest-product-controller.php' );
  4.  
  5.     register_post_type( $this->_post_type ,
  6.        
  7.         array( 'labels' => array(
  8.             'name' => __( 'Produits' ), /* This is the Title of the Group */
  9.             'singular_name' => __( 'Produit' ), /* This is the individual type */
  10.             'all_items' => __( 'Lister' ), /* the all items menu item */
  11.             'add_new' => __( 'Ajouter' ), /* The add new menu item */
  12.             'add_new_item' => __( 'Ajouter' ), /* Add New Display Title */
  13.             'edit' => __( 'Modifier' ), /* Edit Dialog */
  14.             'edit_item' => __( 'Modifier' ), /* Edit Display Title */
  15.             'new_item' => __( 'Nouveau' ), /* New Display Title */
  16.             'view_item' => __( 'Voir' ), /* View Display Title */
  17.             'search_items' => __( 'Rechercher' ), /* Search Custom Type Title */
  18.             'not_found' =>  __( 'Aucun' ), /* This displays if there are no entries yet */
  19.             'not_found_in_trash' => __( 'Aucun dans la corbeille' ), /* This displays if there is nothing in the trash */
  20.             'parent_item_colon' => ''
  21.             ), /* end of arrays */
  22.             'description' => __( 'Les produits' ), /* Custom Type Description */
  23.             'public' => true,
  24.             'publicly_queryable' => true,
  25.             'exclude_from_search' => false,
  26.             'show_ui' => true,
  27.             'menu_icon' => $this->_post_type_icon, /* the icon for the custom post type menu */
  28.             'query_var' => true,
  29.             'menu_position' => $this->_menu_order, /* this is what order you want it to appear in on the left hand side menu */
  30.             'rewrite'   => array( 'slug' => $this->_post_type_archive_slug , 'with_front' => false ), /* you can specify its url slug */
  31.             'has_archive' => $this->_post_type_archive_slug , /* you can rename the slug here */
  32.             'capability_type' => 'post',
  33.             'hierarchical' => false,
  34.             /* the next one is important, it tells what's enabled in the post editor */
  35.             'show_in_rest'       => true,
  36.             'rest_base'          => 'product',
  37.             'rest_controller_class' => 'WP_REST_Product_Controller',
  38.             'supports' => $this->_post_type_supports,
  39.         ) /* end of options */
  40.     ); /* end of register post type */
  41.    
  42. }
  43.  
  44.  
  45. // la classe
  46.  
  47. if(!class_exists('WP_REST_Posts_Controller'))
  48.     return;
  49.  
  50. class WP_REST_Product_Controller extends WP_REST_Posts_Controller {
  51.  
  52.     /**
  53.      * Get the query params for collections.
  54.      *
  55.      * @return array
  56.      */
  57.     public function get_collection_params() {
  58.         return array(
  59.             'context'                => $this->get_context_param(),
  60.             'page'                   => array(
  61.                 'description'        => __( 'Current page of the collection.' ),
  62.                 'type'               => 'integer',
  63.                 'default'            => 1,
  64.                 'sanitize_callback'  => 'absint',
  65.                 'validate_callback'  => 'rest_validate_request_arg',
  66.                 'minimum'            => 1,
  67.             ),
  68.             'per_page'               => array(
  69.                 'description'        => __( 'Maximum number of items to be returned in result set.' ),
  70.                 'type'               => 'integer',
  71.                 'default'            => 10,
  72.                 'minimum'            => 1,
  73.                 'maximum'            => 1000,
  74.                 'sanitize_callback'  => 'absint',
  75.                 'validate_callback'  => 'rest_validate_request_arg',
  76.             ),
  77.             'search'                 => array(
  78.                 'description'        => __( 'Limit results to those matching a string.' ),
  79.                 'type'               => 'string',
  80.                 'sanitize_callback'  => 'sanitize_text_field',
  81.                 'validate_callback'  => 'rest_validate_request_arg',
  82.             ),
  83.         );
  84.     }
  85.  
  86.  
  87.     /**
  88.      * Prepare a single post output for response.
  89.      *
  90.      * @param WP_Post $post Post object.
  91.      * @param WP_REST_Request $request Request object.
  92.      * @return WP_REST_Response $data
  93.      */
  94.  
  95.  
  96.     public function prepare_item_for_response( $post, $request ) {
  97.        
  98.         $GLOBALS['post'] = $post;
  99.         setup_postdata( $post );
  100.         global $wpdb;
  101.  
  102.         // Base fields for every post.
  103.         $data = array(
  104.             'id'           => $post->ID,
  105.             'date'         => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
  106.             'date_gmt'     => $this->prepare_date_response( $post->post_date_gmt ),
  107.             'guid'         => array(
  108.                 /** This filter is documented in wp-includes/post-template.php */
  109.                 'rendered' => apply_filters( 'get_the_guid', $post->guid ),
  110.                 'raw'      => $post->guid,
  111.             ),
  112.             'modified'     => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ),
  113.             'date_display' => get_the_date( get_option('date_format'), $post->ID ),
  114.             'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ),
  115.             'password'     => $post->post_password,
  116.             'slug'         => $post->post_name,
  117.             'status'       => $post->post_status,
  118.             'type'         => $post->post_type,
  119.             'link'         => get_permalink( $post->ID ),
  120.             'codart'       => get_post_meta( $post->ID, 'codart', true ),
  121.             'condit'       => get_post_meta( $post->ID, 'condit', true ),
  122.             'minicde'      => get_post_meta( $post->ID, 'minicde', true ),
  123.             'gencode'      => get_post_meta( $post->ID, 'gencode', true ),
  124.             'memo_fr'         => str_replace(array('<BR>','?'),array("\r\n"," • "),get_post_meta( $post->ID, 'memo_fr', true )),
  125.            
  126.         );
  127.  
  128.         $data['img'] = get_post_meta( $post->ID, 'thumb_url', true );
  129.  
  130.         if($data['condit']==0)
  131.                 $data['condit']='N/A';
  132.  
  133.         if($data['minicde']==0)
  134.                 $data['minicde']='N/A';
  135.  
  136.  
  137.         $schema = $this->get_item_schema();
  138.  
  139.         if ( ! empty( $schema['properties']['title'] ) ) {
  140.             $data['title'] = array(
  141.                 'raw'      => $post->post_title,
  142.                 'rendered' => html_entity_decode(get_the_title( $post->ID )),
  143.             );
  144.         }
  145.  
  146.         if ( ! empty( $schema['properties']['excerpt'] ) ) {
  147.             $data['excerpt'] = array(
  148.                 'raw'      => $post->post_excerpt,
  149.                 'rendered' => $this->prepare_excerpt_response( $post->post_excerpt ),
  150.             );
  151.         }
  152.  
  153.         if ( ! empty( $schema['properties']['author'] ) ) {
  154.             $data['author'] = (int) $post->post_author;
  155.         }
  156.  
  157.         if ( ! empty( $schema['properties']['featured_media'] ) ) {
  158.             $data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
  159.         }
  160.  
  161.         if ( ! empty( $schema['properties']['parent'] ) ) {
  162.             $data['parent'] = (int) $post->post_parent;
  163.         }
  164.  
  165.         if ( ! empty( $schema['properties']['menu_order'] ) ) {
  166.             $data['menu_order'] = (int) $post->menu_order;
  167.         }
  168.  
  169.         if ( ! empty( $schema['properties']['comment_status'] ) ) {
  170.             $data['comment_status'] = $post->comment_status;
  171.         }
  172.  
  173.         if ( ! empty( $schema['properties']['ping_status'] ) ) {
  174.             $data['ping_status'] = $post->ping_status;
  175.         }
  176.  
  177.         if ( ! empty( $schema['properties']['sticky'] ) ) {
  178.             $data['sticky'] = is_sticky( $post->ID );
  179.         }
  180.  
  181.         if ( ! empty( $schema['properties']['template'] ) ) {
  182.             if ( $template = get_page_template_slug( $post->ID ) ) {
  183.                 $data['template'] = $template;
  184.             } else {
  185.                 $data['template'] = '';
  186.             }
  187.         }
  188.  
  189.         if ( ! empty( $schema['properties']['format'] ) ) {
  190.             $data['format'] = get_post_format( $post->ID );
  191.             // Fill in blank post format.
  192.             if ( empty( $data['format'] ) ) {
  193.                 $data['format'] = 'standard';
  194.             }
  195.         }
  196.  
  197.         $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
  198.         foreach ( $taxonomies as $taxonomy ) {
  199.             $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
  200.             $terms = get_the_terms( $post, $taxonomy->name );
  201.             $data[ $base ] = $terms ? wp_list_pluck( $terms, 'name' ) : array();
  202.         }
  203.  
  204.         $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
  205.         $data = $this->add_additional_fields_to_object( $data, $request );
  206.         $data = $this->filter_response_by_context( $data, $context );
  207.  
  208.         // Wrap the data in a response object.
  209.         $response = rest_ensure_response( $data );
  210.  
  211.         $response->add_links( $this->prepare_links( $post ) );
  212.  
  213.         /**
  214.          * Filter the post data for a response.
  215.          *
  216.          * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
  217.          * prepared for the response.
  218.          *
  219.          * @param WP_REST_Response   $response   The response object.
  220.          * @param WP_Post            $post       Post object.
  221.          * @param WP_REST_Request    $request    Request object.
  222.          */
  223.         return apply_filters( "rest_prepare_{$this->post_type}", $response, $post, $request );
  224.     }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment