-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpojo-schedule-zoninator.php
More file actions
320 lines (289 loc) · 8.87 KB
/
pojo-schedule-zoninator.php
File metadata and controls
320 lines (289 loc) · 8.87 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/*
Plugin Name: Pojo Schedule post into Zoninator zone
Depends: Zone Manager (Zoninator)
Description: Inserts scheduled posts into the Zoninator zones on publication
Author: Vladimir Smotesko, Boyle Software, Pojo Team
Version: 1.0.0
Author URI: http://pojo.me/
Text Domain: schedule-zoninator
Domain Path: /languages/
License: GPLv2 or later
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
final class Pojo_Schedule_Zoninator {
const nonce_field = 'schedule_zoninator_nonce';
const zone_id_key = 'schedule_zone_zone_id';
const position_key = 'schedule_zone_position';
/**
* @var Zoninator instance of Zoninator plugin
*/
private $zoninator;
private $zone_posts = array(); // local cache variable
private static $_instance;
private function __construct() {
add_action( 'init', array( $this, 'check_zoninator' ) );
}
/**
* Throw error on object clone
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @since 1.0.1
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'schedule-zoninator' ), '1.0.1' );
}
/**
* Disable unserializing of the class
*
* @since 1.0.1
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'schedule-zoninator' ), '1.0.1' );
}
/**
* @return Pojo_Schedule_Zoninator
*/
public static function get_instance() {
if ( is_null( self::$_instance ) )
self::$_instance = new Pojo_Schedule_Zoninator();
return self::$_instance;
}
public function check_zoninator() {
global $zoninator;
if (
isset( $zoninator ) &&
$zoninator instanceof Zoninator
) {
$this->zoninator = $zoninator;
$this->init_plugin();
} else {
add_action( 'admin_notices', array( $this, 'zoninator_not_found_notice' ) );
}
load_plugin_textdomain( 'schedule-zoninator', false, basename( dirname( __FILE__ ) ) . '/languages' );
}
private function init_plugin() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ) );
add_action( 'publish_future_post', array( $this, 'publish_future_post' ) );
}
public function zoninator_not_found_notice() {
?>
<div class="error">
<p>
<?php _e(
'Zoninator plugin is not installed or not activated! Zoninator schedule will not work.',
'schedule-zoninator'
);
?>
</p>
</div>
<?php
}
public function add_meta_boxes() {
add_meta_box(
'schedule_zone',
__( 'Schedule post into zone', 'schedule-zoninator' ),
array( $this, 'metaboxes_zone' ),
'post',
'side'
);
}
public function metaboxes_zone( $post ) {
$available_zones = $this->zoninator->get_zones();
$selected_zone_id = get_post_meta( $post->ID, self::zone_id_key, true );
$current_position = get_post_meta( $post->ID, self::position_key, true );
wp_nonce_field( self::nonce_field, self::nonce_field );
?>
<p>
<label for="<?php echo self::zone_id_key; ?>"><?php _e( 'Zone', 'schedule-zoninator' ); ?>
</label><br />
<select name="<?php echo self::zone_id_key; ?>"
id="<?php echo self::zone_id_key; ?>">
<option value=""><?php _e( 'Select zone', 'schedule-zoninator' ); ?></option>
<?php foreach ( $available_zones as $available_zone ): ?>
<option value="<?php echo $available_zone->term_id; ?>"
<?php
echo ( $selected_zone_id == $available_zone->term_id ) ?
'selected="selected"' :
'';
?>
>
<?php echo $available_zone->name; ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo self::position_key; ?>"><?php _e( 'Position', 'schedule-zoninator' ); ?>
</label><br />
<input type="number"
value="<?php echo $current_position; ?>"
name="<?php echo self::position_key; ?>"
id="<?php echo self::position_key; ?>" />
</p>
<?php if (
( $selected_zone_id && ! $current_position ) ||
( ! $selected_zone_id && $current_position )
): ?>
<p>
<?php _e( 'You have selected only one setting. Post will not be put into zone without both settings.', 'schedule-zoninator' ); ?>
</p>
<?php endif; ?>
<p class="description">
<?php _e( 'Your scheduled post will be put into the selected zone.', 'schedule-zoninator' ); ?>
</p>
<p class="description">
<?php _e( 'Position is a number, usually from 1 to 10.', 'schedule-zoninator' ); ?>
</p>
<?php
}
public function save_post( $post_id ) {
if ( wp_is_post_revision( $post_id ) )
return;
if ( ! isset( $_POST[ self::nonce_field ] ) || ! wp_verify_nonce( $_POST[ self::nonce_field ], self::nonce_field ) )
return;
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
if ( isset( $_POST[ self::zone_id_key ] ) && $_POST[ self::zone_id_key ] ) {
$zone_id = absint( $_POST[ self::zone_id_key ] );
if ( $this->zoninator->zone_exists( $zone_id ) )
update_metadata( 'post', $post_id, self::zone_id_key, $zone_id );
} else {
delete_metadata( 'post', $post_id, self::zone_id_key );
}
if ( isset( $_POST[ self::position_key ] ) && $_POST[ self::position_key ] ) {
$position = absint( $_POST[ self::position_key ] );
if ( $position > 0 )
update_metadata( 'post', $post_id, self::position_key, $position );
} else {
delete_metadata( 'post', $post_id, self::position_key );
}
}
public function publish_future_post( $post_id ) {
$zone_id = intval( get_metadata( 'post', $post_id, self::zone_id_key, true ) );
$position = intval( get_metadata( 'post', $post_id, self::position_key, true ) );
delete_metadata( 'post', $post_id, self::zone_id_key );
delete_metadata( 'post', $post_id, self::position_key );
if ( ! $zone_id ) {
return;
}
if ( ! $position ) {
return;
}
if ( ! $this->zone_exists( $zone_id ) ) {
return;
}
$posts = $this->get_zone_posts( $zone_id );
if ( count( $posts ) < $position ) {
$posts[] = $post_id;
} else {
array_splice( $posts, ( $position - 1 ), 0, array( $post_id ) );
}
$this->update_zone_posts( $zone_id, $posts );
}
/*
* I had to create the following four functions because the Zoninator's ones
* are not working during the publish_future_post hook execution:
* zone_exists()
* get_zone_posts()
* update_zone_posts()
* clear_zone_posts()
*/
/**
* Check whether the Zoninator zone exists
*
* @param int $zone_id
*
* @return boolean
*/
private function zone_exists( $zone_id ) {
global $wpdb;
return ( $wpdb->get_var(
"SELECT COUNT(term_taxonomy_id)
FROM {$wpdb->term_taxonomy}
WHERE term_id = $zone_id
AND taxonomy = '{$this->zoninator->zone_taxonomy}'"
) > 0 );
}
/**
* Get the Zoninator zone's posts
*
* @param int $zone_id
*
* @return array posts IDs
*/
private function get_zone_posts( $zone_id ) {
if (
isset( $this->zone_posts[ $zone_id ] ) &&
is_array( $this->zone_posts[ $zone_id ] )
) {
return $this->zone_posts[ $zone_id ];
}
global $wpdb;
$this->zone_posts[ $zone_id ] = $wpdb->get_col(
"SELECT {$wpdb->postmeta}.post_id
FROM {$wpdb->postmeta}
WHERE {$wpdb->postmeta}.meta_key =
'{$this->zoninator->zone_meta_prefix}$zone_id'
ORDER BY {$wpdb->postmeta}.meta_value ASC"
);
return $this->zone_posts[ $zone_id ];
}
/**
* Overwrite zone posts
*
* @param int $zone_id
* @param array $posts
*
* @return boolean
*/
private function update_zone_posts( $zone_id, $posts ) {
$this->clear_zone_posts( $zone_id );
foreach ( $posts as $n => $post_id ) {
update_metadata(
'post',
$post_id,
$this->zoninator->zone_meta_prefix . $zone_id,
$n + 1
);
}
clean_term_cache( $zone_id, $this->zoninator->zone_taxonomy );
return true;
}
/**
* Clear zone from posts
*
* @param int $zone_id
*
* @return boolean
*/
private function clear_zone_posts( $zone_id ) {
foreach ( $this->get_zone_posts( $zone_id ) as $post_id ) {
delete_post_meta(
$post_id,
$this->zoninator->zone_meta_prefix . $zone_id
);
}
$this->zone_posts[ $zone_id ] = null;
return true;
}
}
Pojo_Schedule_Zoninator::get_instance();