forked from gocodebox/lifterlms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.llms.post.relationships.php
194 lines (154 loc) · 4.45 KB
/
class.llms.post.relationships.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
defined( 'ABSPATH' ) || exit;
/**
* Hooks and actions related to post relationships
* @since 3.16.12
* @version 3.24.0
*/
class LLMS_Post_Relationships {
/**
* Configure relationships
* @var array
*/
private $relationships = array(
'lesson' => array(
array(
'action' => 'unset',
'meta_key' => '_llms_prerequisite',
'meta_keys_additional' => array( '_llms_has_prerequisite' ),
'post_type' => 'lesson',
),
array(
'action' => 'unset',
'meta_key' => '_llms_lesson_id',
'post_type' => 'llms_quiz',
),
),
'llms_order' => array(
array(
'action' => 'delete',
'meta_key' => '_llms_order_id',
'post_type' => 'llms_transaction',
),
),
'llms_quiz' => array(
array(
'action' => 'delete', // delete = force delete; trash = move to trash
'meta_key' => '_llms_parent_id',
'post_type' => 'llms_question',
),
array(
'action' => 'unset',
'meta_key' => '_llms_quiz',
'meta_keys_additional' => array( '_llms_quiz_enabled' ),
'post_type' => 'lesson',
),
),
);
public function __construct() {
add_action( 'delete_post', array( $this, 'maybe_update_relationships' ) );
}
/**
* Delete / Trash posts related to the deleted post
* @param obj $post WP Post that's been deleted
* @param array $data relationship data array
* @return void
* @since 3.16.12
* @version 3.16.12
*/
private function delete_relationships( $post, $data ) {
$relationships = $this->get_related_posts( $post->ID, $data['post_type'], $data['meta_key'] );
$force = ( 'delete' === $data['action'] );
foreach ( $relationships as $id ) {
wp_delete_post( $id, $force );
}
}
/**
* Get a list of post types with relationships that should be checked
* @return array
* @since 3.16.12
* @version 3.16.12
*/
private function get_post_types() {
return array_keys( $this->get_relationships() );
}
/**
* Retrieve filtered LifterLMS post relationships array
* @return array
* @since 3.16.12
* @version 3.16.12
*/
private function get_relationships() {
return apply_filters( 'llms_get_post_relationships', $this->relationships );
}
/**
* Retrieve an array of post ids related to the deleted post by post type and meta key
* @param int $post_id WP Post ID of the deleted post
* @param string $post_type WP Post type of the related post(s)
* @param string $meta_key meta_key to check for relations by
* @return array
* @since 3.16.12
* @version 3.16.12
*/
private function get_related_posts( $post_id, $post_type, $meta_key ) {
global $wpdb;
return $wpdb->get_col( $wpdb->prepare(
"SELECT p.ID
FROM {$wpdb->posts} AS p
LEFT JOIN {$wpdb->postmeta} AS pm
ON p.ID = pm.post_id
AND pm.meta_key = %s
WHERE p.post_type = %s
AND pm.meta_value = %d",
$meta_key,
$post_type,
$post_id
) );
}
/**
* Check relationships and delete / update related posts when a post is deleted
* Called on `delete_post` hook (before a post is deleted)
* @param int $post_id WP Post ID of the deleted post
* @return void
* @since 3.16.12
* @version 3.24.0
*/
public function maybe_update_relationships( $post_id ) {
$post = get_post( $post_id );
if ( ! in_array( $post->post_type, $this->get_post_types() ) ) {
return;
}
foreach ( $this->get_relationships() as $post_type => $relationships ) {
if ( $post->post_type !== $post_type ) {
continue;
}
foreach ( $relationships as $data ) {
if ( in_array( $data['action'], array( 'delete', 'trash' ) ) ) {
$this->delete_relationships( $post, $data );
} elseif ( 'unset' === $data['action'] ) {
$this->unset_relationships( $post, $data );
}
}
}
}
/**
* Unsets relationship data from post_meta when a post is deleted
* @param obj $post WP Post that's been deleted
* @param array $data relationship data array
* @return void
* @since 3.16.12
* @version 3.24.0
*/
private function unset_relationships( $post, $data ) {
$relationships = $this->get_related_posts( $post->ID, $data['post_type'], $data['meta_key'] );
foreach ( $relationships as $id ) {
delete_post_meta( $id, $data['meta_key'], $post->ID );
if ( isset( $data['meta_keys_additional'] ) ) {
foreach ( $data['meta_keys_additional'] as $key ) {
delete_post_meta( $id, $key );
}
}
}
}
}
return new LLMS_Post_Relationships();