posts-functions.php
3.74 KB
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
<?php
/**
* Post Functions.
*
* @package BadgeOS
* @subpackage Achievements
* @author LearningTimes, LLC
* @license http://www.gnu.org/licenses/agpl.txt GNU AGPL v3.0
* @link https://credly.com
*/
/**
* Add Child step posts to trash.
*
* @param int $post_id Post ID.
*/
function badgeos_before_trash_post( $post_id ) {
global $post_type;
$badge_post_types = badgeos_get_achievement_types_slugs();
if ( in_array( $post_type, $badge_post_types, true ) ) {
$children = badgeos_get_achievements(
array(
'children_of' => $post_id,
'post_status' => array(
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash',
),
)
);
if ( $children ) {
foreach ( $children as $child ) {
$post = array(
'ID' => $child->ID,
'post_status' => 'trash',
);
wp_update_post( $post );
}
}
}
}
add_action( 'wp_trash_post', 'badgeos_before_trash_post' );
/**
* Untrash child posts.
*
* @param int $post_id Post ID.
*/
function badgeos_restore_posts_from_trash( $post_id ) {
global $post_type;
$badge_post_types = badgeos_get_achievement_types_slugs();
if ( in_array( $post_type, $badge_post_types, true ) ) {
$children = badgeos_get_achievements(
array(
'children_of' => $post_id,
'post_status' => array(
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash',
),
)
);
if ( $children ) {
foreach ( $children as $child ) {
$post = array(
'ID' => $child->ID,
'post_status' => 'publish',
);
wp_update_post( $post );
}
}
}
}
add_action( 'untrash_post', 'badgeos_restore_posts_from_trash' );
/**
* Delete child posts permanently.
*
* @param int $post_id Post ID.
*/
function badgeos_delete_posts_permanently( $post_id ) {
global $post_type;
$badge_post_types = badgeos_get_achievement_types_slugs();
if ( in_array( $post_type, $badge_post_types, true ) ) {
$children = badgeos_get_achievements(
array(
'children_of' => $post_id,
'post_status' => array(
'publish',
'pending',
'draft',
'auto-draft',
'future',
'private',
'inherit',
'trash',
),
)
);
if ( $children ) {
foreach ( $children as $child ) {
wp_delete_post( absint( $child->ID ), true ) || die( 'Unable to delete' );
}
}
}
}
add_action( 'before_delete_post', 'badgeos_delete_posts_permanently' );
/**
* Helper function to get a post field.
* Important: On network wide installs, this function will return the post field from main site, so use only for points, achievements and ranks posts fields.
*
* @param string $field The post field.
* @param integer|WP_Post|null $post_id Post ID.
* @return false|string Post field on success, false on failure.
*/
function badgeos_get_post_field( $field, $post_id = null ) {
if ( empty( $post_id ) && isset( $GLOBALS['post'] ) ) {
$post_id = $GLOBALS['post'];
}
// If we got a post object, then return their field.
if ( $post_id instanceof WP_Post ) {
return $post_id->$field;
} elseif ( is_object( $post_id ) ) {
return $post_id->$field;
}
return get_post_field( $field, $post_id );
}
/**
* Helper function to get a post.
* Important: On network wide installs, this function will return the post from main site, so use only for points, achievements and ranks posts.
*
* @param int $post_id Post ID.
* @return false| WP_Post Post object on success, false on failure.
*/
function badgeos_get_post( $post_id ) {
// if we got a post object, then return their field.
if ( $post_id instanceof WP_Post ) {
return $post_id;
}
global $wpdb;
return badgeos_utilities::badgeos_get_post( $post_id );
}