/
metabox.php
115 lines (104 loc) · 3.25 KB
/
metabox.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
<?php
/**
* Generated by the WordPress Meta Box Generator at http://goo.gl/8nwllb
* User: jana
* Date: 12.12.17
* Time: 18:00
*/
namespace Postqueue;
class MetaBox {
/**
* @var Store
*/
public $store;
private $screens;
/**
* Post constructor.
*
* @param Plugin $plugin
*/
function __construct(Plugin $plugin) {
$this->plugin = $plugin;
$this->store = $plugin->store;
$this->screens = get_post_types( array('public' => true) ); //@todo could get a setting page where to choose for which post_types postqueues should be available
/**
* registers add_meta_boxes action that adds metaboxes to post edit
*/
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
/**
* register ajax callbacks for this metabox
*/
add_action( 'wp_ajax_postqueue_add_post', array( $this, 'ajax_callback_add_post' ) );
add_action( 'wp_ajax_postqueue_remove_post', array( $this, 'ajax_callback_remove_post' ) );
}
/**
* Hooks into WordPress' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
add_meta_box(
'postqueue',
__( 'Postqueue', 'postqueue' ),
array( $this, 'render' ),
$screen,
'advanced',
'default'
);
}
}
/**
* Generates the HTML for the meta box
*
* @param object $post WordPress post object
*/
public function render( $post ) {
/**
* Add css and javascript
*/
wp_enqueue_style(
'postqueue-metabox-css',
$this->plugin->url . 'css/postqueue-metabox.css',
array( ),
1,
'all'
);
wp_enqueue_script(
'postqueue-metabox',
$this->plugin->url . 'js/postqueue-metabox.js',
array( 'jquery' ),
1,
false
);
wp_localize_script( 'postqueue-metabox', 'objectL10n', array(
'postremoved' => esc_html__( 'Post successfully removed from postqueue.', Plugin::DOMAIN ),
'postadded' => esc_html__( 'Post successfully added to postqueue.', Plugin::DOMAIN ),
'pleasechoose' => esc_html__( 'Please choose a postqueue!', Plugin::DOMAIN ),
'erroroccured' => esc_html__( 'An error occured while sending the request. Please try again later.', Plugin::DOMAIN ),
'removepostfromthispostqueue' => esc_html__( 'Remove post from this postqueue.', Plugin::DOMAIN ),
'notstoredyet' => esc_html__( 'This post is not saved in any postqueue yet. You can add it to one below.', Plugin::DOMAIN )
) );
$store = $this->store;
require $this->plugin->dir .'partials/postqueue-metabox.tpl.php';
}
/**
* Callback function for the add post action
*/
function ajax_callback_add_post() {
$post_id = intval( $_POST['postid'] );
$queue_id = intval( $_POST['queueid'] );
$this->store->add_post_to_queue( $post_id, $queue_id );
echo "Postqueue ID: " . $queue_id;
wp_die(); // this is required to terminate immediately and return a proper response
}
/**
* Callback function for the remove post action
*/
function ajax_callback_remove_post() {
$post_id = intval( $_POST['postid'] );
$queue_id = intval( $_POST['queueid'] );
$this->store->remove_post_from_queue( $post_id, $queue_id );
echo "Postqueue ID: " . $queue_id;
wp_die(); // this is required to terminate immediately and return a proper response
}
}