-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample-plugin.php
More file actions
85 lines (71 loc) · 2.23 KB
/
example-plugin.php
File metadata and controls
85 lines (71 loc) · 2.23 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
<?php
/**
* @package Background Process
* @version 0.1
*/
/*
Plugin Name: Background Process
Plugin URI: http://techslides.com/
Description: Example Plugin that uses WP Background Processing to queue background tasks
Version: 0.1
Author URI: http://techslides.com/
*/
//https://codex.wordpress.org/Plugin_API/Action_Reference/plugins_loaded
add_action( 'plugins_loaded', 'saas_init' );
function saas_init() {
//require_once plugin_dir_path( __FILE__ ) . 'class-example-process.php';
class WP_Example_Process extends WP_Background_Process {
//use WP_Example_Logger;
/**
* @var string
*/
protected $action = 'example_process';
/**
* Task
*
* Override this method to perform any actions required on each
* queue item. Return the modified item for further processing
* in the next pass through. Or, return false to remove the
* item from the queue.
*
* @param mixed $item Queue item to iterate over
*
* @return mixed
*/
protected function task( $item ) {
backgroundProcess($item);
return false;
}
/**
* Complete
*
* Override if applicable, but ensure that the below actions are
* performed, or, call parent::complete().
*/
protected function complete() {
parent::complete();
// Show notice to user or perform some other arbitrary task...
}
}
$process_all = new WP_Example_Process();
}
//function to be called in the background: referenced in protected task function above
function backgroundProcess($str) {
error_log($str);
sleep(20);
file_put_contents('/Users/iwo/Sites/wordpress2/wp-content/plugins/background-process/log.txt', $str.PHP_EOL,FILE_APPEND);
}
//function to listen to ajax call, add to queue, and dispatch process
add_action( 'wp_ajax_admin_test', 'prefix_ajax_admin_test' );
function prefix_ajax_admin_test() {
$data = $_POST['data'];
//echo $data; //test
$process_all = new WP_Example_Process();
$process_all->push_to_queue( $data );
$process_all->push_to_queue( $data );
$process_all->save()->dispatch();
echo "success"; //return something
wp_die();
}
//initiate ajax call from front-end in js console
//jQuery.ajax({type: "POST",url: ajaxurl,data: {'action': 'admin_test','data': 'go pokeman go'},success: function(data) { console.log(data); }});