class-learndash-admin-data-upgrades-group-post-meta.php
4.84 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
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* LearnDash Data Upgrades for Group Post Meta
*
* @since 3.4.1
* @package LearnDash\Data_Upgrades
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ( class_exists( 'Learndash_Admin_Data_Upgrades' ) ) && ( ! class_exists( 'Learndash_Admin_Data_Upgrades_Group_Post_Meta' ) ) ) {
/**
* Class to create the Data Upgrade for Group Post Meta.
*
* @since 3.4.1
*/
class Learndash_Admin_Data_Upgrades_Group_Post_Meta extends Learndash_Admin_Data_Upgrades {
/**
* DB Version. This is used to trigger future processing actions.
*
* @var string $db_version DB version.
*
* @since 3.4.1
*/
private $db_version = '3.4.1';
/**
* Protected constructor for class
*
* @since 3.4.1
*/
protected function __construct() {
$this->data_slug = 'group-post-meta';
parent::__construct();
parent::register_upgrade_action();
}
/**
* Initialize the Post Meta Processing.
*
* @param bool $force Force processing. Optional. Default false.
*
* @since 3.4.1
*/
public function process_post_meta( $force = false ) {
$run_processing = false;
if ( is_admin() ) {
$data_settings = $this->get_data_settings( $this->data_slug );
if ( true === $force ) {
$run_processing = true;
} elseif ( ( ! isset( $data_settings['version'] ) ) || ( version_compare( $data_settings['version'], $this->db_version, '<' ) ) ) {
$run_processing = true;
} elseif ( ( ! isset( $data_settings['process_status'] ) ) || ( 'complete' !== $data_settings['process_status'] ) ) {
$run_processing = true;
}
if ( true === $run_processing ) {
$ret = $this->process_post_meta_items();
if ( $ret ) {
$data_settings['process_status'] = 'complete';
} else {
$data_settings['process_status'] = 'in_progress';
}
$this->set_last_run_info( $data_settings );
}
}
}
/** This filter is documented in includes/admin/class-learndash-admin-data-upgrades.php */
// phpcs:ignore Squiz.Commenting.FunctionComment
public function set_last_run_info( $data = array() ) {
$data = array_merge( $data, array( 'version' => $this->db_version ) );
parent::set_last_run_info( $data );
}
/**
* Process Post Meta items.
*
* @since 3.4.1
*/
public function process_post_meta_items() {
global $wpdb;
$post_ids_all = array();
$post_ids_meta = array();
$post_ids_all_query_args = array(
'post_type' => learndash_get_post_type_slug( 'group' ),
'fields' => 'ids',
'nopaging' => true,
);
$post_ids_all_query = new WP_Query( $post_ids_all_query_args );
if ( property_exists( $post_ids_all_query, 'posts' ) ) {
$post_ids_all = $post_ids_all_query->posts;
}
if ( empty( $post_ids_all ) ) {
return true;
}
$post_ids_all = array_map( 'absint', $post_ids_all );
$post_ids_meta_query_args = array(
'post_type' => learndash_get_post_type_slug( 'group' ),
'fields' => 'ids',
'nopaging' => true,
'meta_key' => '_ld_certificate', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_compare' => 'EXISTS',
);
$post_ids_meta_query = new WP_Query( $post_ids_meta_query_args );
if ( property_exists( $post_ids_meta_query, 'posts' ) ) {
$post_ids_meta = $post_ids_meta_query->posts;
}
$post_ids_meta = array_map( 'absint', $post_ids_meta );
$post_ids_diff = array_diff( $post_ids_all, $post_ids_meta );
if ( ! empty( $post_ids_diff ) ) {
/**
* Filter the number of items to process.
*
* @since 3.4.1
*
* @param int $batch_size Number of items to process per run. Default 500.
* @param string $post_type Post Type slug bring processed.
*/
$processing_limit = apply_filters( 'learndash_post_meta_process_batch_size', 500, learndash_get_post_type_slug( 'quiz' ) );
$processing_limit = absint( $processing_limit );
if ( $processing_limit > 0 ) {
$post_ids_diff = array_slice( $post_ids_diff, 0, $processing_limit );
}
foreach ( $post_ids_diff as $post_id ) {
// Convert Certificate to post_meta.
$certificate = learndash_get_setting( $post_id, 'certificate' );
$certificate = absint( $certificate );
update_post_meta( $post_id, '_ld_certificate', $certificate );
// Convert Price type to post_meta.
$price_type = learndash_get_setting( $post_id, 'group_price_type' );
$price_type = esc_attr( $price_type );
if ( empty( $price_type ) ) {
if ( defined( 'LEARNDASH_DEFAULT_COURSE_PRICE_TYPE' ) ) {
$price_type = LEARNDASH_DEFAULT_COURSE_PRICE_TYPE;
} else {
$price_type = '';
}
}
update_post_meta( $post_id, '_ld_price_type', $price_type );
}
} else {
return true;
}
return false;
}
// End of functions.
}
}
add_action(
'learndash_data_upgrades_init',
function() {
Learndash_Admin_Data_Upgrades_Group_Post_Meta::add_instance();
}
);