class-batch.php
1.92 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
<?php
/**
* Runs batch processing jobs (like imports, exports, etc)
*/
class WPF_Batch {
/**
* Get things started
*
* @since 1.0
* @return void
*/
public function __construct() {
// Global handlers
add_action( 'wp_ajax_wpf_batch_init', array( $this, 'batch_init' ) );
add_action( 'wp_ajax_wpf_batch_step', array( $this, 'batch_step' ) );
// Built in batch processes
add_action( 'wpf_batch_users_init', array( $this, 'users_init' ) );
add_action( 'wpf_batch_users', array( $this, 'users_step' ) );
}
/**
* Initialize batch process and return count of objects to be processed
*
* @since 1.0
* @return int Count
*/
public function batch_init() {
$hook = $_POST['hook'];
do_action( 'wpf_batch_' . $hook . '_init' );
die();
}
/**
* Runs a single batch step
*
* @since 1.0
* @return void
*/
public function batch_step() {
$hook = $_POST['hook'];
$args = array(
'offset' => $_POST['offset'],
'number' => $_POST['number']
);
do_action( 'wpf_batch_' . $hook, $args );
die();
}
/**
* Users batch process init
*
* @since 1.0
* @return int Count
*/
public function users_init() {
$args = array(
'fields' => 'ID',
'meta_query' => array( array(
'key' => wp_fusion()->crm->slug . '_contact_id',
'compare' => 'NOT EXISTS'
) )
);
$user_query = new WP_User_Query( $args );
$total = $user_query->get_total();
echo $total;
}
/**
* Users batch process - single step
*
* @since 1.0
* @return void
*/
public function users_step( $args ) {
$args = array(
'fields' => 'ID',
'orderby' => 'ID',
'number' => $args['number'],
'meta_query' => array( array(
'key' => wp_fusion()->crm->slug . '_contact_id',
'compare' => 'NOT EXISTS'
) )
);
$users = get_users( $args );
$_POST = array();
foreach( $users as $user_id ) {
wp_fusion()->user->user_register( $user_id, null, true );
}
}
}