/
locomotive.php
190 lines (162 loc) · 4.65 KB
/
locomotive.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
<?php
/**
* Plugin Name: Locomotive
* Version: 0.1.0
* Description: Run custom batch processes from the WP admin.
* Author: Reaktiv Studios
* Author URI: http://reaktivstudios.com/
* Text Domain: locomotive
* Domain Path: languages
* License: GPL
*
* @package Locomotive
*/
namespace Rkv\Locomotive;
use Rkv\Locomotive\Abstracts\Batch;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Plugin class to handle setting constants and loading files and static helper methods.
*/
final class Loader {
/**
* Define all the constants we need
*/
public function define_constants() {
define( 'LOCO_VERSION', '0.1.0-dev' );
define( 'LOCO_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'LOCO_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'LOCO_PLUGIN_FILE', __FILE__ );
}
/**
* Admin Dashboard.
*/
public function add_dashboard() {
add_management_page(
__( 'Batch Processes' ),
__( 'Batches' ),
'manage_options',
'locomotive',
array( $this, 'dashboard_display' )
);
}
/**
* Dashboard display.
*/
public function dashboard_display() {
$registered_batches = locomotive_get_all_batches();
include LOCO_PLUGIN_DIR . 'templates/dashboard.php';
}
/**
* Load in all the files we need.
*/
public function load_includes() {
require_once( LOCO_PLUGIN_DIR . 'includes/abstracts/abstract-batch.php' );
require_once( LOCO_PLUGIN_DIR . 'includes/batches/class-batch-posts.php' );
require_once( LOCO_PLUGIN_DIR . 'includes/batches/class-batch-users.php' );
require_once( LOCO_PLUGIN_DIR . 'includes/batches/class-batch-sites.php' );
require_once( LOCO_PLUGIN_DIR . 'includes/batches/class-batch-terms.php' );
require_once( LOCO_PLUGIN_DIR . 'includes/batches/class-batch-comments.php' );
require_once( LOCO_PLUGIN_DIR . 'includes/functions.php' );
}
/**
* Handle hooks.
*/
public function attach_hooks() {
add_action( 'admin_menu', array( $this, 'add_dashboard' ) );
add_action( 'after_setup_theme', array( $this, 'loaded' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'scripts' ) );
add_action( 'wp_ajax_run_batch', array( $this, 'run' ) );
add_action( 'wp_ajax_reset_batch', array( $this, 'reset' ) );
}
/**
* Plugin stylesheet and JavaScript.
*
* @param string $hook The current page loaded in the WP admin.
*/
public function scripts( $hook ) {
// Exclude our scripts and CSS files from loading globally.
if ( 'tools_page_locomotive' !== $hook ) {
return;
}
wp_enqueue_style( 'batch-process-styles', LOCO_PLUGIN_URL . 'assets/main.css' );
wp_enqueue_script( 'batch-js', LOCO_PLUGIN_URL . 'assets/dist/batch.min.js', array( 'jquery' ), '0.1.0', true );
wp_localize_script( 'batch-js', 'batch', array(
'nonce' => wp_create_nonce( 'run-batch-process' ),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'batches' => locomotive_get_all_batches(),
'page_title' => esc_html( get_admin_page_title() ),
) );
}
/**
* Let everyone know we are loaded and ready to go.
*/
public function loaded() {
if ( is_admin() ) {
locomotive_clear_existing_batches();
do_action( 'locomotive_init' );
}
}
/**
* AJAX handler for running a batch.
*
* @todo Move this to it's own AJAX class.
*/
public function run() {
$batch_process = '';
$step = 0;
$errors = array();
check_ajax_referer( 'run-batch-process', 'nonce' );
if ( empty( $_POST['batch_process'] ) ) {
$errors[] = __( 'Batch process not specified.', 'locomotive' );
} else {
$batch_process = sanitize_text_field( wp_unslash( $_POST['batch_process'] ) );
}
if ( empty( $_POST['step'] ) ) {
$errors[] = __( 'Step must be defined.', 'locomotive' );
} else {
$step = absint( $_POST['step'] );
}
if ( ! empty( $errors ) ) {
wp_send_json( array(
'success' => false,
'errors' => $errors,
) );
}
do_action( 'loco_batch_' . $batch_process, $step );
}
/**
* AJAX handler for running a batch.
*
* @todo Move this to it's own AJAX class.
*/
public function reset() {
$batch_process = '';
$errors = array();
check_ajax_referer( 'run-batch-process', 'nonce' );
if ( empty( $_POST['batch_process'] ) ) {
$errors[] = __( 'Batch process not specified.', 'locomotive' );
} else {
$batch_process = sanitize_text_field( wp_unslash( $_POST['batch_process'] ) );
}
if ( ! empty( $errors ) ) {
wp_send_json( array(
'success' => false,
'errors' => $errors,
) );
}
do_action( 'loco_batch_' . $batch_process . '_reset' );
wp_send_json( array( 'success' => true ) );
}
/**
* Init.
*/
public function init() {
$this->define_constants();
$this->load_includes();
$this->attach_hooks();
}
}
$batch_processing = new Loader();
$batch_processing->init();