Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function register_post_type(){
- require( dirname(__FILE__).'/class-wp-rest-product-controller.php' );
- register_post_type( $this->_post_type ,
- array( 'labels' => array(
- 'name' => __( 'Produits' ), /* This is the Title of the Group */
- 'singular_name' => __( 'Produit' ), /* This is the individual type */
- 'all_items' => __( 'Lister' ), /* the all items menu item */
- 'add_new' => __( 'Ajouter' ), /* The add new menu item */
- 'add_new_item' => __( 'Ajouter' ), /* Add New Display Title */
- 'edit' => __( 'Modifier' ), /* Edit Dialog */
- 'edit_item' => __( 'Modifier' ), /* Edit Display Title */
- 'new_item' => __( 'Nouveau' ), /* New Display Title */
- 'view_item' => __( 'Voir' ), /* View Display Title */
- 'search_items' => __( 'Rechercher' ), /* Search Custom Type Title */
- 'not_found' => __( 'Aucun' ), /* This displays if there are no entries yet */
- 'not_found_in_trash' => __( 'Aucun dans la corbeille' ), /* This displays if there is nothing in the trash */
- 'parent_item_colon' => ''
- ), /* end of arrays */
- 'description' => __( 'Les produits' ), /* Custom Type Description */
- 'public' => true,
- 'publicly_queryable' => true,
- 'exclude_from_search' => false,
- 'show_ui' => true,
- 'menu_icon' => $this->_post_type_icon, /* the icon for the custom post type menu */
- 'query_var' => true,
- 'menu_position' => $this->_menu_order, /* this is what order you want it to appear in on the left hand side menu */
- 'rewrite' => array( 'slug' => $this->_post_type_archive_slug , 'with_front' => false ), /* you can specify its url slug */
- 'has_archive' => $this->_post_type_archive_slug , /* you can rename the slug here */
- 'capability_type' => 'post',
- 'hierarchical' => false,
- /* the next one is important, it tells what's enabled in the post editor */
- 'show_in_rest' => true,
- 'rest_base' => 'product',
- 'rest_controller_class' => 'WP_REST_Product_Controller',
- 'supports' => $this->_post_type_supports,
- ) /* end of options */
- ); /* end of register post type */
- }
- // la classe
- if(!class_exists('WP_REST_Posts_Controller'))
- return;
- class WP_REST_Product_Controller extends WP_REST_Posts_Controller {
- /**
- * Get the query params for collections.
- *
- * @return array
- */
- public function get_collection_params() {
- return array(
- 'context' => $this->get_context_param(),
- 'page' => array(
- 'description' => __( 'Current page of the collection.' ),
- 'type' => 'integer',
- 'default' => 1,
- 'sanitize_callback' => 'absint',
- 'validate_callback' => 'rest_validate_request_arg',
- 'minimum' => 1,
- ),
- 'per_page' => array(
- 'description' => __( 'Maximum number of items to be returned in result set.' ),
- 'type' => 'integer',
- 'default' => 10,
- 'minimum' => 1,
- 'maximum' => 1000,
- 'sanitize_callback' => 'absint',
- 'validate_callback' => 'rest_validate_request_arg',
- ),
- 'search' => array(
- 'description' => __( 'Limit results to those matching a string.' ),
- 'type' => 'string',
- 'sanitize_callback' => 'sanitize_text_field',
- 'validate_callback' => 'rest_validate_request_arg',
- ),
- );
- }
- /**
- * Prepare a single post output for response.
- *
- * @param WP_Post $post Post object.
- * @param WP_REST_Request $request Request object.
- * @return WP_REST_Response $data
- */
- public function prepare_item_for_response( $post, $request ) {
- $GLOBALS['post'] = $post;
- setup_postdata( $post );
- global $wpdb;
- // Base fields for every post.
- $data = array(
- 'id' => $post->ID,
- 'date' => $this->prepare_date_response( $post->post_date_gmt, $post->post_date ),
- 'date_gmt' => $this->prepare_date_response( $post->post_date_gmt ),
- 'guid' => array(
- /** This filter is documented in wp-includes/post-template.php */
- 'rendered' => apply_filters( 'get_the_guid', $post->guid ),
- 'raw' => $post->guid,
- ),
- 'modified' => $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ),
- 'date_display' => get_the_date( get_option('date_format'), $post->ID ),
- 'modified_gmt' => $this->prepare_date_response( $post->post_modified_gmt ),
- 'password' => $post->post_password,
- 'slug' => $post->post_name,
- 'status' => $post->post_status,
- 'type' => $post->post_type,
- 'link' => get_permalink( $post->ID ),
- 'codart' => get_post_meta( $post->ID, 'codart', true ),
- 'condit' => get_post_meta( $post->ID, 'condit', true ),
- 'minicde' => get_post_meta( $post->ID, 'minicde', true ),
- 'gencode' => get_post_meta( $post->ID, 'gencode', true ),
- 'memo_fr' => str_replace(array('<BR>','?'),array("\r\n"," • "),get_post_meta( $post->ID, 'memo_fr', true )),
- );
- $data['img'] = get_post_meta( $post->ID, 'thumb_url', true );
- if($data['condit']==0)
- $data['condit']='N/A';
- if($data['minicde']==0)
- $data['minicde']='N/A';
- $schema = $this->get_item_schema();
- if ( ! empty( $schema['properties']['title'] ) ) {
- $data['title'] = array(
- 'raw' => $post->post_title,
- 'rendered' => html_entity_decode(get_the_title( $post->ID )),
- );
- }
- if ( ! empty( $schema['properties']['excerpt'] ) ) {
- $data['excerpt'] = array(
- 'raw' => $post->post_excerpt,
- 'rendered' => $this->prepare_excerpt_response( $post->post_excerpt ),
- );
- }
- if ( ! empty( $schema['properties']['author'] ) ) {
- $data['author'] = (int) $post->post_author;
- }
- if ( ! empty( $schema['properties']['featured_media'] ) ) {
- $data['featured_media'] = (int) get_post_thumbnail_id( $post->ID );
- }
- if ( ! empty( $schema['properties']['parent'] ) ) {
- $data['parent'] = (int) $post->post_parent;
- }
- if ( ! empty( $schema['properties']['menu_order'] ) ) {
- $data['menu_order'] = (int) $post->menu_order;
- }
- if ( ! empty( $schema['properties']['comment_status'] ) ) {
- $data['comment_status'] = $post->comment_status;
- }
- if ( ! empty( $schema['properties']['ping_status'] ) ) {
- $data['ping_status'] = $post->ping_status;
- }
- if ( ! empty( $schema['properties']['sticky'] ) ) {
- $data['sticky'] = is_sticky( $post->ID );
- }
- if ( ! empty( $schema['properties']['template'] ) ) {
- if ( $template = get_page_template_slug( $post->ID ) ) {
- $data['template'] = $template;
- } else {
- $data['template'] = '';
- }
- }
- if ( ! empty( $schema['properties']['format'] ) ) {
- $data['format'] = get_post_format( $post->ID );
- // Fill in blank post format.
- if ( empty( $data['format'] ) ) {
- $data['format'] = 'standard';
- }
- }
- $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
- foreach ( $taxonomies as $taxonomy ) {
- $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
- $terms = get_the_terms( $post, $taxonomy->name );
- $data[ $base ] = $terms ? wp_list_pluck( $terms, 'name' ) : array();
- }
- $context = ! empty( $request['context'] ) ? $request['context'] : 'view';
- $data = $this->add_additional_fields_to_object( $data, $request );
- $data = $this->filter_response_by_context( $data, $context );
- // Wrap the data in a response object.
- $response = rest_ensure_response( $data );
- $response->add_links( $this->prepare_links( $post ) );
- /**
- * Filter the post data for a response.
- *
- * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
- * prepared for the response.
- *
- * @param WP_REST_Response $response The response object.
- * @param WP_Post $post Post object.
- * @param WP_REST_Request $request Request object.
- */
- return apply_filters( "rest_prepare_{$this->post_type}", $response, $post, $request );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment