Passed
Push — add/multiplan ( be5784...8c4a89 )
by Warwick
04:27 queued 11s
created

group_sections()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 9.6111
1
<?php
2
/**
3
 * LSX Health Plan Plan specific functions.
4
 *
5
 * @package lsx-health-plan
6
 */
7
8
namespace lsx_health_plan\functions\plan;
9
10
11
/**
12
 * Return a true or false if the search is enabled.
13
 *
14
 * @return boolean
15
 */
16
function is_search_enabled() {
17
	$enabled = false;
18
	if ( function_exists( 'lsx_search' ) ) {
19
		$search_instance = \LSX_Search::get_instance();
0 ignored issues
show
Bug introduced by
The type LSX_Search was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
		if ( null !== $search_instance ) {
21
			$enabled = $search_instance->frontend->is_search_enabled();
22
		}
23
	}
24
	return $enabled;
25
}
26
27
/**
28
 * Return a true or false if the search if the plan has sections.
29
 *
30
 * @param  integer $plan_id
31
 * @return boolean
32
 */
33
function has_sections( $plan_id = 0 ) {
34
	$sections = false;
35
	if ( 0 === $plan_id ) {
36
		$plan_id = get_the_ID();
37
	}
38
	$lsx_hp   = lsx_health_plan();
39
	$sections = $lsx_hp->frontend->plan_query->query_sections( $plan_id );
40
	return $sections;
41
}
42
43
/**
44
 * Returns the sections for a plan
45
 *
46
 * @param  integer $plan_id
47
 * @param  boolean $group_sections
48
 * @return array
49
 */
50
function get_sections( $group_sections = false ) {
51
	$lsx_hp   = lsx_health_plan();
52
	$sections = $lsx_hp->frontend->plan_query->get_sections( $group_sections );
53
	return $sections;
54
}
55
56
/**
57
 * Gets the current sections array values.
58
 *
59
 * @param  string $section_key
60
 * @return array
61
 */
62
function get_section_info( $section_key = '' ) {
63
	$section_info = array();
64
65
	$sections = get_sections();
66
	if ( ! empty( $sections ) ) {
67
		foreach ( $sections as $key => $values ) {
68
			$current_key = sanitize_title( $values['title'] );
69
			if ( $current_key === $section_key ) {
70
				return $values;
71
			}
72
		}
73
	}
74
	return $section_info;
75
}
76
77
/**
78
 * This will group title from the first section item.
79
 *
80
 * @param  array $sections
81
 * @return array
82
 */
83
function get_group_title( $sections = array() ) {
84
	$group_title = apply_filters( 'lsx_hp_default_plan_group', __( 'Daily Plan', 'lsx-health-plan' ) );
85
	if ( ! empty( $sections ) ) {
86
		$first_section = reset( $sections );
87
		if ( isset( $first_section['group'] ) && '' !== $first_section['group'] ) {
88
			$group_title = $first_section['group'];
89
		}
90
	}
91
	return $group_title;
92
}
93
94
/**
95
 * Returns the sections for a plan
96
 *
97
 * @param  integer $plan_id
98
 * @param  string  $title
99
 * @return array
100
 */
101
function get_permalink( $plan_id = 0, $title = '' ) {
102
	if ( 0 === $plan_id ) {
103
		$plan_id = get_the_ID();
104
	}
105
	$url = \get_permalink( $plan_id );
0 ignored issues
show
Bug introduced by
It seems like $plan_id can also be of type false; however, parameter $post of get_permalink() does only seem to accept WP_Post|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

105
	$url = \get_permalink( /** @scrutinizer ignore-type */ $plan_id );
Loading history...
106
	if ( '' !== $title ) {
107
		$url .= sanitize_title( $title ) . '/';
108
	}
109
	return $url;
110
}
111
112
/**
113
 * Return a true or false if the search is enabled.
114
 *
115
 * @return boolean
116
 */
117
function is_filters_disabled( $disabled = false ) {
118
	$is_disabled = \lsx_health_plan\functions\get_option( 'plan_filters_disabled', false );
119
	if ( false !== $is_disabled ) {
120
		$disabled = true;
121
	}
122
	return $disabled;
123
}
124
125
/**
126
 * Generates a unique ID for the current section item you are viewing,  e.g Day 1 of Week 1.
127
 * Can only be used on the single plan pages and endpoints.
128
 *
129
 * @param  string  $section_key
130
 * @return string
131
 */
132
function generate_section_id( $section_key = '' ) {
133
	$key          = get_the_ID();
134
	if ( '' === $section_key ) {
135
		$section_key  = get_query_var( 'section' );
136
	}
137
138
	$section_key = sanitize_title( $section_key );
139
	
140
	if ( '' !== $section_key && \lsx_health_plan\functions\plan\has_sections() ) {
141
		$group_title  = apply_filters( 'lsx_hp_default_plan_group', __( 'Daily Plan', 'lsx-health-plan' ) );
142
		$section_info = \lsx_health_plan\functions\plan\get_section_info( $section_key );
143
		$key         .= '_' . sanitize_key( $group_title ) . '_' . sanitize_key( $section_info['title'] );
144
	}
145
	return $key;
146
}
147