WordPress.org

Forums

JSON API
exclude doesn't work while include does (5 posts)

  1. archon810
    Member
    Posted 4 years ago #

    The exclude option doesn't seem to work, while include does.

    For example, /api/get_recent_posts/?page=1&count=1&dev=1&include=date works fine and returns just the date for each object, but /api/get_recent_posts/?page=1&count=1&dev=1&exclude=date still returns date along with the rest of the fields.

    The problem lies in this code:

    function setup() {
        global $json_api;
        $this->include_values = array();
        if ($json_api->query->include) {
          $this->include_values = explode(',', $json_api->query->include);
        }
        if ($json_api->query->exclude) {
          $exclude = explode(',', $json_api->query->exclude);
          $this->include_values = array_diff($this->include_values, $exclude);
        }
      }

    For includes, it works fine, but for excludes, it tries to subtract from an empty array, getting nada, and then this code:

    function is_value_included($key) {
        if (empty($this->include_values)) {
          return true;
        } else {
          return in_array($key, $this->include_values);
        }
      }

    forces everything to be included.

    I've thought about the possible solutions and the only ones that come to mind are: either maintain a comprehensive list of includes and then remove whatever is in excludes (not so elegant), or maintain a separate exclude option and have the is_value_included() function check this list in addition to the inclusion list.

    http://wordpress.org/extend/plugins/json-api/

  2. ikesyo
    Member
    Posted 4 years ago #

    Hi,

    I worked around the problem.

    response.php:

    function setup() {
        global $json_api;
        $this->include_values = array();
        $this->exclude_values = array();
        if ($json_api->query->include) {
          $this->include_values = explode(',', $json_api->query->include);
        }
        if ($json_api->query->exclude) {
          $this->exclude_values = explode(',', $json_api->query->exclude);
          $this->include_values = array_diff($this->include_values, $this->exclude_values);
        }
      }
    function is_value_included($key) {
        if (empty($this->include_values) && empty($this->exclude_values)) {
          return true;
        } else {
          if (empty($this->exclude_values)) {
            return in_array($key, $this->include_values);
          } else {
            return !in_array($key, $this->exclude_values);
          }
        }
      }

    It seems to work properly.

  3. ikesyo
    Member
    Posted 4 years ago #

    Sorry, 2nd part of the cord is berrow.

    function is_value_included($key) {
        if (empty($this->include_values) && empty($this->exclude_values)) {
          return true;
        } else {
          if (!empty($this->include_values)) {
            return in_array($key, $this->include_values);
          } else {
            return !in_array($key, $this->exclude_values);
          }
        }
      }
  4. dphiffer
    Member
    Plugin Author

    Posted 4 years ago #

    Thanks guys, this will be fixed in the forthcoming release!

  5. archon810
    Member
    Posted 4 years ago #

    Thanks for fixing in the latest release - I just upgraded and will be checking back to see if the exclude option works soon.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • JSON API
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags