1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace lsx_health_plan\classes\integrations\facetwp; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Contains the downloads functions post type |
6
|
|
|
* |
7
|
|
|
* @package lsx-health-plan |
8
|
|
|
*/ |
9
|
|
|
class Connected_Plans { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Holds class instance |
13
|
|
|
* |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
* |
16
|
|
|
* @var object \lsx_health_plan\classes\integrations\facetwp\Connected_Plans() |
17
|
|
|
*/ |
18
|
|
|
protected static $instance = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This hold the current plan IDS, in case they need to be used in additional functions. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public $current_plan_ids = array(); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
*/ |
30
|
|
|
public function __construct() { |
|
|
|
|
31
|
|
|
//add_filter( 'facetwp_index_row', array( $this, 'facetwp_index_row' ), 10, 2 ); |
|
|
|
|
32
|
|
|
add_filter( 'facetwp_indexer_post_facet', array( $this, 'facetwp_indexer_post_facet' ), 10, 2 ); |
33
|
|
|
} |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Return an instance of this class. |
37
|
|
|
* |
38
|
|
|
* @since 1.0.0 |
39
|
|
|
* |
40
|
|
|
* @return object \lsx_health_plan\classes\integration\facetwp\Connected_Plans() A single instance of this class. |
41
|
|
|
*/ |
42
|
|
|
public static function get_instance() { |
43
|
|
|
// If the single instance hasn't been set, set it now. |
44
|
|
|
if ( null === self::$instance ) { |
|
|
|
|
45
|
|
|
self::$instance = new self(); |
46
|
|
|
} |
|
|
|
|
47
|
|
|
return self::$instance; |
48
|
|
|
} |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Index the connected plan |
52
|
|
|
* |
53
|
|
|
* @param array $return |
|
|
|
|
54
|
|
|
* @param array $params |
|
|
|
|
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function facetwp_indexer_post_facet( $return, $params ) { |
58
|
|
|
$facet = $params['facet']; |
|
|
|
|
59
|
|
|
$source = isset( $facet['source'] ) ? $facet['source'] : ''; |
|
|
|
|
60
|
|
|
|
61
|
|
|
if ( 'lsx_hp/connected_plans' === $source ) { |
|
|
|
|
62
|
|
|
$post_type = get_post_type( $params['defaults']['post_id'] ); |
63
|
|
|
switch ( $post_type ) { |
|
|
|
|
64
|
|
|
case 'workout': |
65
|
|
|
$return = $this->index_connected_plans( $params['defaults'] ); |
66
|
|
|
$this->index_exercises( $params['defaults'] ); |
67
|
|
|
break; |
68
|
|
|
|
69
|
|
|
case 'recipe': |
70
|
|
|
$return = $this->index_connected_plans( $params['defaults'] ); |
71
|
|
|
break; |
72
|
|
|
|
73
|
|
|
case 'meal': |
74
|
|
|
$return = $this->index_connected_plans( $params['defaults'] ); |
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
default: |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Reset the current plan ids array. |
83
|
|
|
$this->current_plan_ids = array(); |
84
|
|
|
return $return; |
85
|
|
|
} |
|
|
|
|
86
|
|
|
|
87
|
|
|
/** |
|
|
|
|
88
|
|
|
* Adds the connected plan to the list of rows. |
89
|
|
|
* |
90
|
|
|
* @param array $rows |
|
|
|
|
91
|
|
|
* @param array $params |
|
|
|
|
92
|
|
|
* @return boolean |
93
|
|
|
*/ |
94
|
|
|
public function index_connected_plans( $row ) { |
95
|
|
|
$indexed = false; |
96
|
|
|
$top_level_plans = array(); |
97
|
|
|
// Get meals this exercise is connected to. |
98
|
|
|
$plans = get_post_meta( $row['post_id'], 'connected_plans', true ); |
99
|
|
|
|
100
|
|
|
if ( ! empty( $plans ) ) { |
|
|
|
|
101
|
|
|
$plan = end( $plans ); |
|
|
|
|
102
|
|
|
$has_parent = wp_get_post_parent_id( $plan ); |
103
|
|
|
if ( 0 === $has_parent ) { |
|
|
|
|
104
|
|
|
$top_level_plans[] = $plan; |
105
|
|
|
} elseif ( false !== $top_level_plans ) { |
|
|
|
|
106
|
|
|
$top_level_plans[] = $has_parent; |
107
|
|
|
} |
108
|
|
|
} |
|
|
|
|
109
|
|
|
if ( ! empty( $top_level_plans ) && ( '' !== $top_level_plans ) ) { |
|
|
|
|
110
|
|
|
$top_level_plans = array_unique( $top_level_plans ); |
111
|
|
|
$this->current_plan_ids = $top_level_plans; |
112
|
|
|
$indexed = true; |
113
|
|
|
foreach ( $top_level_plans as $plan_id ) { |
|
|
|
|
114
|
|
|
$row['facet_value'] = $plan_id; |
115
|
|
|
$row['facet_display_value'] = get_the_title( $plan_id ); |
116
|
|
|
FWP()->indexer->index_row( $row ); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
} |
|
|
|
|
119
|
|
|
return $indexed; |
120
|
|
|
} |
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
|
|
|
|
123
|
|
|
* We index the exercises from the workouts. |
124
|
|
|
* |
125
|
|
|
* @param array $rows |
|
|
|
|
126
|
|
|
* @param array $params |
|
|
|
|
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
|
|
public function index_exercises( $row ) { |
130
|
|
|
if ( empty( $this->current_plan_ids ) ) { |
|
|
|
|
131
|
|
|
return; |
132
|
|
|
} |
|
|
|
|
133
|
|
|
$i = 1; |
134
|
|
|
$section_counter = 6; |
135
|
|
|
$unique_connections = array(); |
136
|
|
|
|
137
|
|
|
while ( $i <= $section_counter ) { |
|
|
|
|
138
|
|
|
// Here we grab the exercises and we add them to the index with the plan IDS. |
139
|
|
|
$groups = get_post_meta( $row['post_id'], 'workout_section_' . $i, true ); |
140
|
|
|
if ( ! empty( $groups ) ) { |
|
|
|
|
141
|
|
|
foreach ( $groups as $group ) { |
|
|
|
|
142
|
|
|
if ( isset( $group['connected_exercises'] ) && '' !== $group['connected_exercises'] ) { |
|
|
|
|
143
|
|
|
|
144
|
|
|
if ( ! is_array( $group['connected_exercises'] ) ) { |
|
|
|
|
145
|
|
|
$group['connected_exercises'] = array( $group['connected_exercises'] ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Loop through each exercise and add it to the plan. |
149
|
|
|
foreach ( $group['connected_exercises'] as $eid ) { |
|
|
|
|
150
|
|
|
$exercise_default = $row; |
151
|
|
|
$exercise_default['post_id'] = $eid; |
152
|
|
|
|
153
|
|
|
foreach ( $this->current_plan_ids as $plan_id ) { |
|
|
|
|
154
|
|
|
// Check to see if this connection has been added already. |
155
|
|
|
if ( isset( $unique_connections[ $eid . '_' . $plan_id ] ) ) { |
|
|
|
|
156
|
|
|
continue; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$title = get_the_title( $plan_id ); |
160
|
|
|
if ( ! empty( $title ) ) { |
|
|
|
|
161
|
|
|
$exercise_default['facet_value'] = $plan_id; |
162
|
|
|
$exercise_default['facet_display_value'] = $title; |
163
|
|
|
$unique_connections[ $eid . '_' . $plan_id ] = $exercise_default; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
|
|
|
|
170
|
|
|
$i++; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// If we have some unique connections, we index them. |
174
|
|
|
if ( ! empty( $unique_connections ) ) { |
|
|
|
|
175
|
|
|
foreach ( $unique_connections as $unique_row ) { |
|
|
|
|
176
|
|
|
FWP()->indexer->index_row( $unique_row ); |
|
|
|
|
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
|
|
|
|
180
|
|
|
} |
181
|
|
|
|