# Webien - extended Wordpress search #

Integrates Relevanssi & Elementor on standard wordpress search.


There are some filters that can be hooked in this plugin. 
You can do a lot by adding Relevanssi filters as well -> https://www.relevanssi.com/user-manual/filter-hooks/

###Development
- Run composer install
- Gulp
- Compliled using NVM 10.10 but others should work

### Render
Input form can be rendered with settings array:
```php
$settingsArr = [
    'results_quantity' => 5,
    'post_types' => 'post,page,product',
    'post_types_templates' => {"post":43573,"page":43573,"product":43570}, // elementor template id:s
    'redirect' => '', // ' no-submit'
    'takeover_desktop' => '',
    'takeover_tablet' => '',
    'takeover_mobile' => '',
    'input_placeholder_text' => 'Sök..',
    'button_icon_position' => 'left,
    'button_icon' => 'fa fa-search',
    'button_text' => '',
    'min_input' => 3,
];
$echo = true;

\Webien\SearchAddon\SearchAddon::render_search_form($settingsArr, $echo);
```

### Filters
If using AWS and no env variables is present, update AWS credentials:
```php
add_filter( 'webien/search/aws_credentials', 'update_s3_credentials' );
function update_s3_credentials($credentials)
{
    // Remember not to include credentials in repo..
	$credentials['key'] = $accessKeyId;
	$credentials['secret'] = $secret;
	$credentials['region'] = 'eu-north-1';

	return $credentials;
}
```

Search can be extended from your theme with js variables such as localstorage values for filtering results

```js
$('.webien-search-addon-container')[0].dataset.variables = JSON.stringify({
   regionID: localStorage.getItem('regionID'),
   city: 'gbg'
});
```

Listen to those variables with a php hook in your theme:

```php
add_filter( 'webien/search/modify_query', [$this, 'filter_region_posts'], 10, 2 );
function filter_region_posts($query, $variables)
{

    if(isset($variables['regionID'])){

        // Get the taxonomy that has the correct connection to the post type region
        $terms = get_terms( array(
            'taxonomy' => 'region',
            'hide_empty' => false,
        ));
        $termIds = [];
        foreach($terms as $term){
            if(get_field('region', $term) == $variables['regionID'] ){
                array_push($termIds, $term->term_id);
                break;
            }
        }

        // also add Sverige id
        $sweden = get_term_by('name', 'sverige', 'region');
        array_push($termIds, $sweden->term_id);

        // Add tax query with correct region, we add the second array since we cant only filter posts with this
        $tax_query = [
            [
                'taxonomy' => 'region',
                'field'    => 'id',
                'terms'    => $termIds
            ],
            [
                // This is to get all other results from other post types
                'taxonomy' => 'region',
                'operator'  => 'NOT EXISTS'
            ],
            'relation' => 'OR',
        ];
        $query->set( 'tax_query', $tax_query );
    }

    return $query;
}
```


Modify cdn base url where cached files can be stored:
```php
add_filter( 'webien/search/cdn_url', 'update_s3_url', 10, 1 );
function update_s3_url($url)
{
    // Do stuff here
    return $url;
}
```


Possibility to supply external app with result suggestions as a js object instead of displaying the dropdown result. This could be nice sometimes when the search input and the suggestions have som serious UI awesomeness. Listen for suggestions with JS event below:
```php
add_filter( 'webien/search/suggestions_to_js', '__return_true' );
```
```js
window.addEventListener('webienSearchToJsEvent', (e) => { console.log(webienSearchSuggestions) }, false);
```


Possibility to ignore posts when printing. Try to use other filters first, this will just ignore post but still process them.. :
```php
add_filter( 'webien/search/post_filter', 'webien_search_post_filter', 10, 3 );
function webien_search_post_filter($save_post, $post_id, $variables)
{
    // Do stuff here
    return $save_post;
}
```

Modify post type labels:
```php
add_filter( 'webien/search/modify_post_type_title', 'webien_search_modify_post_type_title', 10, 2 );
function webien_search_modify_post_type_title($label, $posttype)
{
    return $label;
}

add_filter( 'webien/search/modify_post_type_array', 'webien_search_modify_post_type_array', 10, 1 );
function webien_search_modify_post_type_title($posttypeArray)
{
    return $posttypeArray;
}

```

Modify no result text:
```php
add_filter( 'webien/search/no_result_text', 'webien_search_no_result_text', 10, 1 );
function webien_search_no_result_text($text)
{
    $text = 'Something else..';
    return $text;
}

```


Modify standard markup if no elementor templates exists:
```php
add_filter( 'webien/search/standard_markup_suggestion', 'modify_standard_markup_suggestion', 10, 3 );
function webien_search_modify_post_type_title($html, $post, $searchword)
{
    return $html;
}

```

Modify the final resultlist:
```php
add_filter('webien/search/final_suggestions', 'add_more_suggestions', 10, 4);
function add_more_suggestions($return_posts, $searchword, $posttypes, $posts_to_parse){

    $result = $other_posts;

    foreach ($result as $res){
        $post_html = '';
        $post_html = apply_filters('webien/search/standard_markup_suggestion_custom', $post_html, $res->post_id, $res->question, $searchword);
        $return_posts[] = ['link' => get_the_permalink($res->post_id), 'html' => $post_html];
    }
    
    return $return_posts; 
}
```



Example of Relevanssi filter whrere you can filter query parameters before relevanssi starts it's query:

```php
add_filter('relevanssi_modify_wp_query', 'modify_query_before_relevanssi');
function modify_query_before_relevanssi($query){
    $query->query_vars( 'post_types', 'post' );
    $query->set( 'post_type', 'product );
    return $query;
}
```
Example of Relevanssi filter whrere you can edit what's added to the index on save;
<br>If using complex operations, you might want to force indexing in small chunks..

```php
add_filter( 'relevanssi_indexing_data', 'modify_index_data', 10, 2 );
function modify_index_data($data, $post){
    if($post->post_type == "product"){
        // Add stuff to data array..
    }
}
// Adjust indexing threshold for heavy jobs
add_filter('relevanssi_indexing_adjust', function(){ return false; });
add_filter('relevanssi_indexing_limit', function(){ return 2; });
```

More filters:

```php
// Search to execute on each ajax request
add_filter('webien/search/queries_per_request', function(){ return 2; });

// When building cache in admin, you can set it to also cache partial words.
// This will create alot more cache if set to true
// If this is set to true, you should set the queries per ajax request filter to 1 since each request generates search queries for all partial words.
add_filter('webien/search/build_cache_partial_words', function(){ return false; });
```

If you want to disable any type of Ajax search and only rely on cached results, you can disable the ajax call. This if for when you might have a cron job generating cached results and don't want any load on the server from ajax requests..
```
add_filter('webien/search/cached_suggestions_only', function(){ return false; });
```