Skip to content

Search within contents (wordpress plugin) #196

@sebastian-rink

Description

@sebastian-rink

Hello,

first: I'm no experienced developer ;) With my little rusty PHP knowledge I tried to find a way to search within contents (field "parameters" in the database) in the wordpress plugin, not only within titles and keywords. This code extension (admin/class-h5p-content-query.php) seems to solve it. Do you see any problems with this code? And would this be interesting for a coming update to the H5P wordpress plugin?

    $this->where = '';
    $this->where_args = array();

    if ($filters !== NULL) {
      foreach ($filters as $filter) {
        if (!isset($filter[0]) || !isset($filter[1])) {
          throw new Exception('Missing filter options.');
        }

		#################################################################
		// Additional search in field 'parameters'
		if ($filter[0] === 'title' && ( !isset($filter[2]) || $filter[2] === 'LIKE' )) {
			// Search in title OR in parameters
			$this->where .= ($this->where ? ' AND ' : ' WHERE ')
			. '('
			. "hc.title" . $this->valid_operators['LIKE']
			. " OR hc.parameters" . $this->valid_operators['LIKE']
			. ')';

			// Add both terms
			$this->where_args[] = $filter[1];
			$this->where_args[] = preg_quote(substr(json_encode($filter[1]), 1, -1));

			continue;
		}
		##################################################################
		  
        $field = $this->get_valid_field($filter[0]);

        // Add join
        $this->add_join($field[0]);

        // Add where
        $this->where .= ($this->where ? ' AND ' : ' WHERE ') . ($field[0] === 't' ? 'ct2.tag_id' : $field[0] . '.' . $field[1]);
        $this->where_args[] = $filter[1];

        // Check if operator is valid, if not use the first valid one.
        $operator = (isset($filter[2]) ? $filter[2] : '=');
        if (!isset($this->valid_operators[$operator])) {
          throw new Exception('Invalid operator: '. $operator);
        }
        $this->where .= $this->valid_operators[$operator];
      }
    }

Thanks for any help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions