Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

mod/search/start.php (1 issue)

usage of deprecated functions.

Deprecated Code Minor
1
<?php
2
/**
3
 * Elgg search plugin
4
 *
5
 */
6
7
/**
8
 * Initialize search plugin
9
 *
10
 * @return void
11
 */
12
function search_init() {
13
14
	// page handler for search actions and results
15 32
	elgg_register_page_handler('search', 'search_page_handler');
0 ignored issues
show
Deprecated Code introduced by Cash Costello
The function elgg_register_page_handler() has been deprecated: 3.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

15
	/** @scrutinizer ignore-deprecated */ elgg_register_page_handler('search', 'search_page_handler');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
16
17
	// exclude /search routes from indexing
18 32
	elgg_register_plugin_hook_handler('robots.txt', 'site', 'search_exclude_robots');
19
20
	// add in CSS for search elements
21 32
	elgg_extend_view('elgg.css', 'search/search.css');
22
23 32
	elgg_register_plugin_hook_handler('search:format', 'entity', \Elgg\Search\FormatComentEntityHook::class);
24
25 32
	elgg_register_plugin_hook_handler('view_vars', 'output/tag', 'search_output_tag');
26 32
}
27
28
/**
29
 * Page handler for /search routes
30
 *
31
 * @param array $segments URL segments
32
 *
33
 * @return \Elgg\Http\OkResponse
34
 * @throws SecurityException
35
 */
36
function search_page_handler($segments) {
37
38 1
	if (!get_input('q') && isset($segments[0])) {
39
		set_input('q', $segments[0]);
40
	}
41
42 1
	$output = elgg_view_resource('search/index');
43 1
	return elgg_ok_response($output);
44
}
45
46
/**
47
 * Exclude robots from indexing search pages
48
 *
49
 * This is good for performance since search is slow and there are many pages all
50
 * with the same content.
51
 *
52
 * @elgg_plugin_hook robots.txt search
53
 *
54
 * @param \Elgg\Hook $hook Hook
55
 * @return string
56
 */
57
function search_exclude_robots(\Elgg\Hook $hook) {
58
	$rules = [
59
		'',
60
		'User-agent: *',
61
		'Disallow: /search/',
62
		''
63
	];
64
65
	$text = $hook->getValue();
66
	$text .= implode("\r\n", $rules);
67
	return $text;
68
}
69
70
/**
71
 * Adds search 'href' to output/tag view vars
72
 *
73
 * @elgg_plugin_hook view_vars output/tag
74
 *
75
 * @param \Elgg\Hook $hook Hook
76
 *
77
 * @return void|array
78
 */
79
function search_output_tag(\Elgg\Hook $hook) {
80
	$vars = $hook->getValue();
81
	if (isset($vars['href'])) {
82
		// leave unaltered
83
		return;
84
	}
85
86
	$query_params = [
87
		'q' => elgg_extract('value', $vars),
88
		'search_type' => 'tags',
89
		'type' => elgg_extract('type', $vars, null, false),
90
		'subtype' => elgg_extract('subtype', $vars, null, false),
91
	];
92
93
	$url = elgg_extract('base_url', $vars, 'search');
94
95
	unset($vars['base_url']);
96
	unset($vars['type']);
97
	unset($vars['subtype']);
98
99
	$vars['href'] = elgg_http_add_url_query_elements($url, $query_params);
100
101
	return $vars;
102
}
103
104
return function() {
105
	elgg_register_event_handler('init', 'system', 'search_init');
106
};