Passed
Push — add/multiplan ( 30efbe...e0cac4 )
by Warwick
04:26
created

generate_section_id()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 0
dl 0
loc 13
rs 9.9666
c 0
b 0
f 0
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
39
	$section_array = get_post_meta( $plan_id, 'plan_sections', true );
0 ignored issues
show
Bug introduced by
It seems like $plan_id can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept 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

39
	$section_array = get_post_meta( /** @scrutinizer ignore-type */ $plan_id, 'plan_sections', true );
Loading history...
40
41
	if ( ! empty( $section_array ) ) {
42
		$sections = true;
43
	}
44
	return $sections;
45
}
46
47
/**
48
 * Returns the sections for a plan
49
 *
50
 * @param  integer $plan_id
51
 * @param  boolean $group_sections
52
 * @return array
53
 */
54
function get_sections( $plan_id = 0, $group_sections = false ) {
55
	$sections = array();
56
	if ( 0 === $plan_id ) {
57
		$plan_id = get_the_ID();
58
	}
59
	$section_array = get_post_meta( $plan_id, 'plan_sections', true );
0 ignored issues
show
Bug introduced by
It seems like $plan_id can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept 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

59
	$section_array = get_post_meta( /** @scrutinizer ignore-type */ $plan_id, 'plan_sections', true );
Loading history...
60
	if ( ! empty( $section_array ) ) {
61
		$sections = $section_array;
62
		if ( false !== $group_sections ) {
63
			$sections = group_sections( $sections );
0 ignored issues
show
Bug introduced by
It seems like $sections can also be of type string; however, parameter $sections of lsx_health_plan\functions\plan\group_sections() does only seem to accept array, 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

63
			$sections = group_sections( /** @scrutinizer ignore-type */ $sections );
Loading history...
64
		}
65
	}
66
	return $sections;
67
}
68
69
/**
70
 * Gets the current sections array values.
71
 *
72
 * @param  string $section_key
73
 * @return array
74
 */
75
function get_section_info( $section_key = '' ) {
76
	$section_info = array();
77
78
	$sections = get_sections();
79
	if ( ! empty( $sections ) ) {
80
		foreach ( $sections as $key => $values ) {
81
			$current_key = sanitize_title( $values['title'] );
82
			if ( $current_key === $section_key ) {
83
				return $values;
84
			}
85
		}
86
	}
87
	return $section_info;
88
}
89
90
/**
91
 * This will group the sections by their "Group" field.
92
 *
93
 * @param  array $sections
94
 * @return array
95
 */
96
function group_sections( $sections = array() ) {
97
	$groups = array();
98
	if ( ! empty( $sections ) ) {
99
		foreach ( $sections as $section_key => $section_values ) {
100
			$group_key = apply_filters( 'lsx_hp_default_plan_group', __( 'Daily Plan', 'lsx-health-plan' ) );
101
			if ( isset( $section_values['group'] ) && '' !== $section_values['group'] ) {
102
				$group_key = $section_values['group'];
103
			}
104
			$group_key                            = sanitize_title( $group_key );
105
			$groups[ $group_key ][ $section_key ] = $section_values;
106
		}
107
	}
108
	return $groups;
109
}
110
111
/**
112
 * This will group title from the first section item.
113
 *
114
 * @param  array $sections
115
 * @return array
116
 */
117
function get_group_title( $sections = array() ) {
118
	$group_title = apply_filters( 'lsx_hp_default_plan_group', __( 'Daily Plan', 'lsx-health-plan' ) );
119
	if ( ! empty( $sections ) ) {
120
		$first_section = reset( $sections );
121
		if ( isset( $first_section['group'] ) && '' !== $first_section['group'] ) {
122
			$group_title = $first_section['group'];
123
		}
124
	}
125
	return $group_title;
126
}
127
128
/**
129
 * Returns the sections for a plan
130
 *
131
 * @param  integer $plan_id
132
 * @param  string  $title
133
 * @return array
134
 */
135
function get_permalink( $plan_id = 0, $title = '' ) {
136
	if ( 0 === $plan_id ) {
137
		$plan_id = get_the_ID();
138
	}
139
	$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

139
	$url = \get_permalink( /** @scrutinizer ignore-type */ $plan_id );
Loading history...
140
	if ( '' !== $title ) {
141
		$url .= sanitize_title( $title ) . '/';
142
	}
143
	return $url;
144
}
145
146
/**
147
 * Return a true or false if the search is enabled.
148
 *
149
 * @return boolean
150
 */
151
function is_filters_disabled( $disabled = false ) {
152
	$is_disabled = \lsx_health_plan\functions\get_option( 'plan_filters_disabled', false );
153
	if ( false !== $is_disabled ) {
154
		$disabled = true;
155
	}
156
	return $disabled;
157
}
158
159
/**
160
 * Generates a unique ID for the current section item you are viewing,  e.g Day 1 of Week 1.
161
 * Can only be used on the single plan pages and endpoints.
162
 *
163
 * @return string
164
 */
165
function generate_section_id() {
166
	$key = '';
167
	if ( is_singular( 'plan' ) ) {
168
		$key          = get_the_ID();
169
		$section_key  = get_query_var( 'section' );
170
		
171
		if ( '' !== $section_key && \lsx_health_plan\functions\plan\has_sections() ) {
172
			$group_title  = apply_filters( 'lsx_hp_default_plan_group', __( 'Daily Plan', 'lsx-health-plan' ) );
173
			$section_info = \lsx_health_plan\functions\plan\get_section_info( $section_key );
174
			$key         .= '_' . sanitize_key( $group_title ) . '_' . sanitize_key( $section_info['title'] );
175
		}
176
	}
177
	return $key;
178
}
179