Support » Plugin: JSON API » [Plugin: JSON API] exclude doesn't work while include does

  • 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/

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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.

    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);
          }
        }
      }

    Plugin Author dphiffer

    (@dphiffer)

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

    Thread Starter archon810

    (@archon810)

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

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Plugin: JSON API] exclude doesn't work while include does’ is closed to new replies.