Plugin Directory

Changeset 2000668


Ignore:
Timestamp:
12/23/2018 05:41:38 PM (7 years ago)
Author:
airesvsg
Message:

1.1.0

Location:
wp-primary-category
Files:
25 added
5 edited

Legend:

Unmodified
Added
Removed
  • wp-primary-category/trunk/includes/classes/class-wp-primary-category.php

    r1994274 r2000668  
    1616            add_action( 'wp_loaded', array( 'WP_Primary_Category_Settings', 'init' ) );
    1717            add_action( 'init', array( 'WP_Primary_Category_Shortcode', 'init' ) );
    18 
     18            add_action( 'rest_api_init', array( 'WP_Primary_Category_REST_API', 'init' ) );
    1919        }
    2020
     
    2323            require_once WP_PC_PLUGIN_PATH . 'includes/classes/class-wp-primary-category-settings.php';
    2424            require_once WP_PC_PLUGIN_PATH . 'includes/classes/class-wp-primary-category-shortcode.php';
     25            require_once WP_PC_PLUGIN_PATH . 'includes/classes/class-wp-primary-category-rest-api.php';
    2526            require_once WP_PC_PLUGIN_PATH . 'includes/functions.php';
     27        }
     28
     29        public static function get_primary_categories( $post = null, $output = OBJECT ) {
     30            $post_id = self::_ge_post_id( $post );
     31
     32            if ( ! $post_id ) {
     33                return false;
     34            }
     35
     36            $post_type  = get_post_type( $post_id );
     37            $taxonomies = WP_Primary_Category_Settings::get_option( $post_type );
     38
     39            $data = array();
     40            if ( $taxonomies ) {
     41                foreach ( $taxonomies as $taxonomy ) {
     42                    $category = get_primary_category( $taxonomy, $post_id, $output );
     43                    if ( $category ) {
     44                        $data[ $taxonomy ] = $category;
     45                    }
     46                }
     47            }
     48
     49            return $data;
    2650        }
    2751
  • wp-primary-category/trunk/includes/functions.php

    r1994274 r2000668  
    33if ( ! defined( 'ABSPATH' ) ) {
    44    exit;
     5}
     6
     7if ( ! function_exists( 'get_primary_categories' ) ) {
     8    function get_primary_categories( $post = null, $output = OBJECT ) {
     9        return WP_Primary_Category::get_primary_categories( $post, $output );
     10    }
    511}
    612
  • wp-primary-category/trunk/plugin.php

    r2000124 r2000668  
    55 * Author: Aires Gonçalves
    66 * Author URI: http://github.com/airesvsg
    7  * Version: 1.0.1
     7 * Version: 1.1.0
    88 * Plugin URI: http://github.com/airesvsg/wp-primary-category
    99 */
  • wp-primary-category/trunk/readme.md

    r1994274 r2000668  
    1313- [Shortcode](#shortcode)
    1414- [WP_Query](#wp_query)
     15- [REST API](#rest-api)
    1516- [To Do](#to-do)
    1617
     
    5253| Name | Argument(s) |
    5354|------|-------------|
     55| get_primary_categories | mixed ( int, WP_Post, NULL ) **$post**<br>*required*<br>--<br>string **$output**<br>*default value:* OBJECT<br>*others values:* OBJECT, ARRAY_A, ARRAY_N or "ID"<br>*optional* |
    5456| get_primary_category | mixed ( int, WP_Term, object, string ) **$taxonomy**<br>*required*<br>--<br>mixed ( int, WP_Post, NULL ) **$post**<br>*default value:* NULL<br>*optional*<br>--<br>string **$output**<br>*default value:* OBJECT<br>*others values:* OBJECT, ARRAY_A, ARRAY_N or "ID"<br>*optional* |
    5557| the_primary_category | mixed ( int, WP_Term, object, string ) **$taxonomy**<br>*required*<br>--<br>mixed ( int, WP_Post, NULL ) **$post**<br>*default value:* NULL<br>*optional*<br>--<br>string **$output**<br>*default value:* "link"<br>*others values:* "name"<br>*optional*--<br>string **$echo**<br>*default value:* true<br>*optional* |
     
    9496| wp_primary_category_taxonomies | mixed ( array, boolean ) **$taxonomies** |
    9597| wp_primary_category_html | string **$html**<br>mixed ( int, WP_Term, object, string ) **$taxonomy**<br>mixed ( int, WP_Post, NULL ) **$post**<br>string **$output** |
     98| wp_primary_category_rest_api_output | string ( OBJECT, ARRAY_A, ARRAY_N or "ID" ) **$output**<br> array **$request**  |
     99| wp_primary_category_rest_api_data | array **$data**<br> array **$request** |
    96100
    97101**How to use the filters?**
     
    165169https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
    166170
     171REST API
     172====
     173
     174**Endpoint**:
     175`/wp-json/wp/v2/posts/1`
     176
     177**Default answer:**
     178```JSON
     179{
     180    "id": 1,
     181    "title": {
     182        "rendered": "Hello world!"
     183    },
     184    ...
     185    "primary_categories": {
     186        "category": 1,
     187        "genre": 2
     188    },
     189    ...
     190}
     191```
     192
     193**Use the filter below to change the answer:**
     194```PHP
     195add_filter( 'wp_primary_category_rest_api_data', function( $data ) {
     196    if ( isset( $data['genre'] ) ) {
     197        unset( $data['genre'] );
     198    }
     199
     200    return $data;
     201} );
     202```
     203
     204**The new answer:**
     205```JSON
     206{
     207    "id": 1,
     208    "title": {
     209        "rendered": "Hello world!"
     210    },
     211    ...
     212    "primary_categories": {
     213        "category": 1
     214    },
     215    ...
     216}
     217```
     218
     219**Use the filter below to change the output:**
     220```PHP
     221add_filter( 'wp_primary_category_rest_api_output', function() {
     222    return OBJECT; // OBJECT, ARRAY_A, ARRAY_N or 'ID'
     223} );
     224```
     225
     226**The new answer:**
     227```JSON
     228{
     229    "id": 1,
     230    "title": {
     231        "rendered": "Hello world!"
     232    },
     233    ...
     234    "primary_categories": {
     235        "category": {
     236            "term_id": 1,
     237            "name": "My category",
     238            "slug": "my-category",
     239            "term_group": 0,
     240            "term_taxonomy_id": 2,
     241            "taxonomy": "category",
     242            "description": "",
     243            "parent": 0,
     244            "count": 1,
     245            "filter": "raw"
     246        }
     247    },
     248    ...
     249}
     250```
     251
    167252To Do
    168253====
  • wp-primary-category/trunk/readme.txt

    r2000124 r2000668  
    55Requires at least: 4.6
    66Tested up to: 4.9.8
    7 Stable tag: 1.0.1
     7Stable tag: 1.1.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    2222== Changelog ==
    2323
     24= 1.1.0 =
     25adding support to REST API
     26
    2427= 1.0.1 =
    2528bugfix imposible to remove primary category once it has been set - Thanks to Mirko
Note: See TracChangeset for help on using the changeset viewer.