Completed
Push — update/invite-user-sync-events ( 1f15ba...aaac64 )
by
unknown
08:25
created

Jetpack_Search_Helpers   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A get_search_url() 0 15 3
A add_query_arg() 0 7 2
A remove_query_arg() 0 3 1
A get_widget_option_name() 0 3 1
A get_widgets_from_option() 0 10 3
A build_widget_id() 0 3 1
A is_active_widget() 0 3 1
C get_filters_from_widgets() 0 27 7
1
<?php
2
3
class Jetpack_Search_Helpers {
4
	const FILTER_WIDGET_BASE = 'jetpack-search-filters';
5
6
	static function get_search_url() {
7
		$query_args = $_GET;
8
9
		// Handle the case where a permastruct is being used, such as /search/{$query}
10
		if ( ! isset( $query_args['s'] ) ) {
11
			$query_args['s'] = get_search_query();
12
		}
13
14
		if ( isset( $query_args['paged'] ) ) {
15
			unset( $query_args['paged'] );
16
		}
17
18
		$query = http_build_query( $query_args );
19
		return home_url( "?{$query}" );
20
	}
21
22
	static function add_query_arg( $key, $value = false ) {
23
		if ( is_array( $key ) ) {
24
			return add_query_arg( $key, self::get_search_url() );
25
		}
26
27
		return add_query_arg( $key, $value, self::get_search_url() );
28
	}
29
30
	static function remove_query_arg( $key ) {
31
		return remove_query_arg( $key, self::get_search_url() );
32
	}
33
34
	static function get_widget_option_name() {
35
		return sprintf( 'widget_%s', self::FILTER_WIDGET_BASE );
36
	}
37
38
	static function get_widgets_from_option() {
39
		$widget_options = get_option( self::get_widget_option_name(), array() );
40
41
		// We don't need this
42
		if ( ! empty( $widget_options ) && isset( $widget_options['_multiwidget'] ) ) {
43
			unset( $widget_options['_multiwidget'] );
44
		}
45
46
		return $widget_options;
47
	}
48
49
	static function build_widget_id( $number ) {
50
		return sprintf( '%s-%d', self::FILTER_WIDGET_BASE, $number );
51
	}
52
53
	static function is_active_widget( $widget_id ) {
54
		return (bool) is_active_widget( false, $widget_id, self::FILTER_WIDGET_BASE );
55
	}
56
57
	static function get_filters_from_widgets() {
58
		$filters = array();
59
60
		$widget_options = self::get_widgets_from_option();
61
		if ( empty( $widget_options ) ) {
62
			return $filters;
63
		}
64
65
		foreach ( (array) $widget_options as $number => $settings ) {
66
			$widget_id = self::build_widget_id( $number );
67
			if ( ! self::is_active_widget( $widget_id ) || empty( $settings['filters'] ) ) {
68
				continue;
69
			}
70
71
			if ( empty( $settings['use_filters'] ) ) {
72
				continue;
73
			}
74
75
			foreach ( (array) $settings['filters'] as $widget_filter ) {
76
				$widget_filter['widget_id'] = $widget_id;
77
				$key = sprintf( '%s_%d', $widget_filter['type'], count( $filters ) );
78
				$filters[ $key ] = $widget_filter;
79
			}
80
		}
81
82
		return $filters;
83
	}
84
}
85