This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathexample-plugin.php
154 lines (132 loc) · 3.25 KB
/
example-plugin.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
<?php
/*
Plugin Name: Example Background Processing
Plugin URI: https://github.com/A5hleyRich/wp-background-processing
Description: Background processing in WordPress.
Author: Ashley Rich
Version: 0.1
Author URI: https://deliciousbrains.com/
Text Domain: example-plugin
Domain Path: /languages/
*/
class Example_Background_Processing {
/**
* @var WP_Example_Request
*/
protected $process_single;
/**
* @var WP_Example_Process
*/
protected $process_all;
/**
* Example_Background_Processing constructor.
*/
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init' ) );
add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 100 );
add_action( 'init', array( $this, 'process_handler' ) );
}
/**
* Init
*/
public function init() {
require_once plugin_dir_path( __FILE__ ) . 'class-logger.php';
require_once plugin_dir_path( __FILE__ ) . 'async-requests/class-example-request.php';
require_once plugin_dir_path( __FILE__ ) . 'background-processes/class-example-process.php';
$this->process_single = new WP_Example_Request();
$this->process_all = new WP_Example_Process();
}
/**
* Admin bar
*
* @param WP_Admin_Bar $wp_admin_bar
*/
public function admin_bar( $wp_admin_bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$wp_admin_bar->add_menu( array(
'id' => 'example-plugin',
'title' => __( 'Process', 'example-plugin' ),
'href' => '#',
) );
$wp_admin_bar->add_menu( array(
'parent' => 'example-plugin',
'id' => 'example-plugin-single',
'title' => __( 'Single User', 'example-plugin' ),
'href' => wp_nonce_url( admin_url( '?process=single'), 'process' ),
) );
$wp_admin_bar->add_menu( array(
'parent' => 'example-plugin',
'id' => 'example-plugin-all',
'title' => __( 'All Users', 'example-plugin' ),
'href' => wp_nonce_url( admin_url( '?process=all'), 'process' ),
) );
}
/**
* Process handler
*/
public function process_handler() {
if ( ! isset( $_GET['process'] ) || ! isset( $_GET['_wpnonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'process') ) {
return;
}
if ( 'single' === $_GET['process'] ) {
$this->handle_single();
}
if ( 'all' === $_GET['process'] ) {
$this->handle_all();
}
}
/**
* Handle single
*/
protected function handle_single() {
$names = $this->get_names();
$rand = array_rand( $names, 1 );
$name = $names[ $rand ];
$this->process_single->data( array( 'name' => $name ) )->dispatch();
}
/**
* Handle all
*/
protected function handle_all() {
$names = $this->get_names();
foreach ( $names as $name ) {
$this->process_all->push_to_queue( $name );
}
$this->process_all->save()->dispatch();
}
/**
* Get names
*
* @return array
*/
protected function get_names() {
return array(
'Grant Buel',
'Bryon Pennywell',
'Jarred Mccuiston',
'Reynaldo Azcona',
'Jarrett Pelc',
'Blake Terrill',
'Romeo Tiernan',
'Marion Buckle',
'Theodore Barley',
'Carmine Hopple',
'Suzi Rodrique',
'Fran Velez',
'Sherly Bolten',
'Rossana Ohalloran',
'Sonya Water',
'Marget Bejarano',
'Leslee Mans',
'Fernanda Eldred',
'Terina Calvo',
'Dawn Partridge',
);
}
}
new Example_Background_Processing();