Plugin Directory

Changeset 2054167


Ignore:
Timestamp:
03/20/2019 06:10:59 PM (7 years ago)
Author:
warriorrocker
Message:

Add new terms api endpoint

Location:
xo-for-angular/trunk
Files:
2 added
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • xo-for-angular/trunk/Includes/Api/Abstract/Objects/Term.class.php

    r2042983 r2054167  
    9999
    100100    /**
     101     * Optional collection of ACF fields set for the given term.
     102     *
     103     * @since 1.0.7
     104     *
     105     * @var array
     106     */
     107    public $fields;
     108
     109    /**
    101110     * Generate a fully formed term object.
    102111     *
     
    105114     * @param WP_Term $term The base term object.
    106115     * @param bool $meta Optionally include meta in term object.
     116     * @param bool $fields Optionally include ACF fields in post object.
    107117     */
    108     function __construct(WP_Term $term, $meta = false) {
     118    function __construct(WP_Term $term, $meta = false, $fields = false) {
    109119        // Map base term object properties
    110120        $this->id = $term->term_id;
     
    119129        // Set the relative URL of the term using get_term_link and wp_make_link_relative
    120130        $this->url = wp_make_link_relative(get_term_link($term));
     131
     132        // Optionally set the post fields
     133        if ($fields)
     134            $this->SetFields();
    121135
    122136        // Optionally set the term meta
     
    138152
    139153        // Iterate over the retrieved meta options
    140         foreach ($meta as $key => $value)
    141             if ((is_array($value)) && (count($value) == 1) && (substr($key, 0, 1) != '_'))
     154        foreach ($meta as $key => $value) {
     155            // Skip the option if name not starts with an underscore indicating private or internal data
     156            if (substr($key, 0, 1) == '_')
     157                continue;
     158
     159            $addMeta = true;
     160
     161            // Check if meta option should be excluded
     162            if (!empty($this->fields)) {
     163                foreach ($this->fields as $exclude => $excludeValue) {
     164                    if (substr($key, 0, strlen($exclude)) == $exclude) {
     165                        $addMeta = false;
     166                        break;
     167                    }
     168                }
     169            }
     170
     171            if (!$addMeta)
     172                continue;
     173
     174            if ((is_array($value)) && (count($value) == 1))
    142175                $this->meta[$key] = $value[0];
     176        }
     177    }
     178
     179    /**
     180     * Set the ACF fields that are set for the given post.
     181     *
     182     * @since 1.0.7
     183     *
     184     * @return void
     185     */
     186    public function SetFields() {
     187        // Return empty array if ACF is unavailable
     188        if (!function_exists('get_fields'))
     189            return;
     190
     191        // Get collection of ACF fields for the post
     192        $this->fields = get_fields($this->taxonomy . '_' . $this->id);
    143193    }
    144194}
  • xo-for-angular/trunk/Includes/Api/Api.class.php

    r2051900 r2054167  
    6868        $this->Xo->RequireOnce('Includes/Api/Abstract/Responses/PostsConfigResponse.class.php');
    6969        $this->Xo->RequireOnce('Includes/Api/Abstract/Responses/MenusResponse.class.php');
    70         $this->Xo->RequireOnce('Includes/Api/Abstract/Responses/TermsResponse.class.php');
     70        $this->Xo->RequireOnce('Includes/Api/Abstract/Responses/TermsGetResponse.class.php');
     71        $this->Xo->RequireOnce('Includes/Api/Abstract/Responses/TermsFilterResponse.class.php');
    7172        $this->Xo->RequireOnce('Includes/Api/Abstract/Responses/OptionsResponse.class.php');
    7273    }
  • xo-for-angular/trunk/Includes/Api/Controllers/TermsController.class.php

    r2042983 r2054167  
    88class XoApiControllerTerms extends XoApiAbstractIndexController
    99{
     10    /**
     11     * Get a taxonomy and term by url.
     12     *
     13     * @since 1.0.7
     14     *
     15     * @param mixed $params Request object
     16     * @return XoApiAbstractTermsGetResponse
     17     */
     18    public function Get($params) {
     19        // Return an error if the url is missing
     20        if (empty($params['url']))
     21            return new XoApiAbstractTermsGetResponse(false, __('Missing category url.', 'xo'));
     22
     23        // Get the taxonomy by parsing the requested url
     24        $taxonomy = $this->GetTaxonomyByUrl($params['url']);
     25
     26        // Return an error if the taxonomy was not found
     27        if (!$taxonomy)
     28            return new XoApiAbstractTermsGetResponse(false, __('Unable to locate taxonomy.', 'xo'));
     29
     30        // Get the term within the found taxonomy by comparing the requested url
     31        $term = $this->GetTermByTaxonomyAndUrl($taxonomy, $params['url']);
     32
     33        // Return an error if the term was not found
     34        if (!$term)
     35            return new XoApiAbstractTermsGetResponse(false, __('Unable to locate term.', 'xo'));
     36
     37        // Return success and the taxonomy and term objects
     38        return new XoApiAbstractTermsGetResponse(
     39            true, __('Successfully located term.', 'xo'),
     40            new XoApiAbstractTerm($term, true, true)
     41        );
     42    }
     43
    1044    /**
    1145     * Filter, search, or list terms by various properties similar to get_terms().
     
    3771        $terms = array();
    3872        foreach ($taxonomyTerms as $term)
    39             $terms[] = new XoApiAbstractTerm($term, true);
     73            $terms[] = new XoApiAbstractTerm($term, true, true);
    4074
    4175        // Sort the terms by the given term meta key
     
    5387        );
    5488    }
     89
     90    /**
     91     * Summary of GetTaxonomyByUrl
     92     *
     93     * @since 1.0.7
     94     *
     95     * @param mixed $url
     96     * @return boolean|WP_Taxonomy
     97     */
     98    private function GetTaxonomyByUrl($url) {
     99        $taxonomies = get_taxonomies(array(), 'objects');
     100
     101        foreach ($taxonomies as $taxonomy_config) {
     102            if (!$taxonomy_config->public)
     103                continue;
     104
     105            if (empty($taxonomy_config->rewrite['slug']))
     106                continue;
     107
     108            $taxonomyUrl = '/' . $taxonomy_config->rewrite['slug'] . '/';
     109
     110            if (strpos($url, $taxonomyUrl) === 0)
     111                return $taxonomy_config;
     112        }
     113
     114        return false;
     115    }
     116
     117    /**
     118     * Summary of GetTermByTaxonomyAndUrl
     119     * @param WP_Taxonomy $taxonomy
     120     * @param mixed $url
     121     * @return boolean|WP_Term
     122     */
     123    private function GetTermByTaxonomyAndUrl(WP_Taxonomy $taxonomy, $url) {
     124        $url = trailingslashit($url);
     125
     126        $taxonomyTerms = get_terms(array(
     127            'taxonomy' => $taxonomy->name
     128        ));
     129
     130        if ((is_wp_error($taxonomyTerms)) || (empty($taxonomyTerms)))
     131            return false;
     132
     133        foreach ($taxonomyTerms as $term) {
     134            $termUrl = trailingslashit(wp_make_link_relative(get_term_link($term)));
     135
     136            if (strpos($url, $termUrl) === 0)
     137                return $term;
     138        }
     139
     140        return false;
     141    }
    55142}
  • xo-for-angular/trunk/Includes/Services/Classes/RouteGenerator.class.php

    r2051900 r2054167  
    9393
    9494            // Check if the post type has a rewrite base
    95             if (isset($post_type_config->rewrite['slug'])) {
     95            if (!empty($post_type_config->rewrite['slug'])) {
    9696                // Get the template of the rewrite base
    9797                if ((!$template = $this->Xo->Services->Options->GetOption('xo_' . $post_type . '_template', false))
     
    9999                    continue;
    100100
     101                // Remove any tags from rewrite slug
     102                $rewriteSlug = preg_replace('/(%)(.*?)(%)/', '', $post_type_config->rewrite['slug']);
     103                $rewriteSlug = untrailingslashit($rewriteSlug);
     104
    101105                // Generate route for a posts hub page which will handle individual post urls
    102                 $routes[] = new XoApiAbstractRoute($post_type_config->rewrite['slug'], $attrs['loadChildren'], 'prefix', array(
     106                $routes[] = new XoApiAbstractRoute($rewriteSlug, $attrs['loadChildren'], 'prefix', array(
    103107                    'postType' => $post_type
    104108                ));
  • xo-for-angular/trunk/xo-angular.php

    r2052267 r2054167  
    44Plugin URI: https://angularxo.io
    55Description: Angular theme development in WordPress with templates and a powerful API.
    6 Version: 1.0.6
     6Version: 1.0.7
    77Author: Travis Brown
    88Author URI: http://www.xodustech.com
Note: See TracChangeset for help on using the changeset viewer.