Post Expirator - Version 1.2

Version Description

Download this release

Release Info

Developer axelseaa
Plugin Icon 128x128 Post Expirator
Version 1.2
Comparing to
See all releases

Version 1.2

Files changed (2) hide show
  1. post-expirator.php +364 -0
  2. readme.txt +52 -0
post-expirator.php ADDED
@@ -0,0 +1,364 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Post Expirator
4
+ Plugin URI: http://wordpress.org/extend/plugins/post-expirator/
5
+ Description: Allows you to add an expiration date (hourly) to posts which you can configure to either delete the post or change it to a draft.
6
+ Author: Aaron Axelsen
7
+ Version: 1.2
8
+ Author URI: http://www.frozenpc.net
9
+ */
10
+
11
+ // Default Values
12
+ $expirationdateDefaultDateFormat = 'l F jS, Y g:ia';
13
+
14
+ /**
15
+ * Function that does the actualy deleting - called by wp_cron
16
+ */
17
+ function expirationdate_delete_expired_posts() {
18
+ global $wpdb;
19
+ $result = $wpdb->get_results('select post_id, meta_value from ' . $wpdb->postmeta . ' where meta_key = "expiration-date" AND meta_value <= "' . mktime() . '"');
20
+ foreach ($result as $a) {
21
+ $post_result = $wpdb->get_var('select post_type from ' . $wpdb->posts .' where ID = '. $a->post_id);
22
+ if ($post_result == 'post') {
23
+ $expiredStatus = strtolower(get_option('expirationdateExpiredPostStatus'));
24
+ } else if ($post_result == 'page') {
25
+ $expiredStatus = strtolower(get_option('expirationdateExpiredPageStatus'));
26
+ } else {
27
+ $expiredStatus = 'draft';
28
+ }
29
+
30
+ if ($expiredStatus == 'delete')
31
+ wp_delete_post($a->post_id);
32
+ else {
33
+ wp_update_post(array('ID' => $a->post_id, 'post_status' => 'draft'));
34
+ delete_post_meta($a->post_id, 'expiration-date');
35
+ update_post_meta($a->post_id, 'expiration-date', $a->meta_value, true);
36
+ }
37
+ }
38
+ }
39
+ add_action ('expirationdate_delete_'.$current_blog->blog_id, 'expirationdate_delete_expired_posts');
40
+
41
+ /**
42
+ * Called at plugin activation
43
+ */
44
+ function expirationdate_activate () {
45
+ global $current_blog,$expirationdateDefaultDateFormat;
46
+ update_option('expirationdateExpiredPostStatus','Draft');
47
+ update_option('expirationdateExpiredPageStatus','Draft');
48
+ update_option('expirationdateDefaultDateFormat',$expirationdateDefaultDateFormat);
49
+ wp_schedule_event(mktime(date('H'),0,0,date('m'),date('d'),date('Y')), 'hourly', 'expirationdate_delete_'.$current_blog->blog_id);
50
+ }
51
+ register_activation_hook (__FILE__, 'expirationdate_activate');
52
+
53
+ /**
54
+ * Called at plugin deactivation
55
+ */
56
+ function expirationdate_deactivate () {
57
+ global $current_blog;
58
+ delete_option('expirationdateExpiredPostStatus');
59
+ delete_option('expirationdateExpiredPageStatus');
60
+ delete_option('expirationdateDefaultDateFormat');
61
+ wp_clear_scheduled_hook('expirationdate_delete_'.$current_blog->blog_id);
62
+ }
63
+ register_deactivation_hook (__FILE__, 'expirationdate_deactivate');
64
+
65
+ /**
66
+ * adds an 'Expires' column to the post display table.
67
+ */
68
+ function expirationdate_add_column ($columns) {
69
+ $columns['expirationdate'] = 'Expires <br/><span style="font-size: 0.8em; font-weight: normal;">(YYYY/MM/DD HH)</span>';
70
+ return $columns;
71
+ }
72
+ add_filter ('manage_posts_columns', 'expirationdate_add_column');
73
+ add_filter ('manage_pages_columns', 'expirationdate_add_column');
74
+
75
+ /**
76
+ * fills the 'Expires' column of the post display table.
77
+ */
78
+ function expirationdate_show_value ($column_name) {
79
+ global $wpdb, $post;
80
+ $id = $post->ID;
81
+ if ($column_name === 'expirationdate') {
82
+ $query = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = \"expiration-date\" AND post_id=$id";
83
+ $ed = $wpdb->get_var($query);
84
+ echo ($ed ? date('Y/m/d H',$ed) : "Never");
85
+ }
86
+ }
87
+ add_action ('manage_posts_custom_column', 'expirationdate_show_value');
88
+ add_action ('manage_pages_custom_column', 'expirationdate_show_value');
89
+
90
+ /**
91
+ * Add's hooks to get the meta box added to post
92
+ */
93
+ function expirationdate_meta_post() {
94
+ add_meta_box('expirationdatediv', __('Post Expirator'), 'expirationdate_meta_box', 'post', 'advanced', 'high');
95
+ }
96
+ add_action ('dbx_post_advanced','expirationdate_meta_post');
97
+
98
+ /**
99
+ * Add's hooks to get the meta box added to page
100
+ */
101
+ function expirationdate_meta_page() {
102
+ add_meta_box('expirationdatediv', __('Post Expirator'), 'expirationdate_meta_box', 'page', 'advanced', 'high');
103
+ }
104
+ add_action ('edit_page_form','expirationdate_meta_page');
105
+
106
+ /**
107
+ * Actually adds the meta box
108
+ */
109
+ function expirationdate_meta_box($post) {
110
+ // Get default month
111
+ $expirationdatets = get_post_meta($post->ID,'expiration-date',true);
112
+ if (empty($expirationdatets)) {
113
+ $defaultmonth = date('F');
114
+ $defaultday = date('d');
115
+ $defaulthour = date('H');
116
+ $defaultyear = date('Y');
117
+ $disabled = 'disabled="disabled"';
118
+ } else {
119
+ $defaultmonth = date('F',$expirationdatets);
120
+ $defaultday = date('d',$expirationdatets);
121
+ $defaultyear = date('Y',$expirationdatets);
122
+ $defaulthour = date('H',$expirationdatets);
123
+
124
+ $enabled = ' checked="checked"';
125
+ $disabled = '';
126
+ }
127
+
128
+ $rv = array();
129
+ $rv[] = '<p><input type="checkbox" name="enable-expirationdate" id="enable-expirationdate" value="checked"'.$enabled.' onclick="expirationdate_ajax_add_meta(\'enable-expirationdate\')" />';
130
+ $rv[] = '<label for="enable-expirationdate">Enable Post Expiration</label></p>';
131
+ $rv[] = '<table><tr>';
132
+ $rv[] = '<th style="text-align: left;">Month</th>';
133
+ $rv[] = '<th style="text-align: left;">Day</th>';
134
+ $rv[] = '<th style="text-align: left;">Year</th>';
135
+ $rv[] = '<th style="text-align: left;"></th>';
136
+ $rv[] = '<th style="text-align: left;">Hour (24 Hour Format)</th>';
137
+ $rv[] = '</tr><tr>';
138
+ $rv[] = '<td>';
139
+ $rv[] = '<select name="expirationdate_month" id="expirationdate_month"'.$disabled.'">';
140
+ for($i = 1; $i <= 12; $i++) {
141
+ if ($defaultmonth == date('F',mktime(0, 0, 0, $i, 1, date("Y"))))
142
+ $selected = ' selected="selected"';
143
+ else
144
+ $selected = '';
145
+ $rv[] = '<option value="'.date('m',mktime(0, 0, 0, $i, 1, date("Y"))).'"'.$selected.'>'.date('F',mktime(0, 0, 0, $i, 1, date("Y"))).'</option>';
146
+ }
147
+ $rv[] = '</select>';
148
+ $rv[] = '</td><td>';
149
+ $rv[] = '<input type="text" id="expirationdate_day" name="expirationdate_day" value="'.$defaultday.'" size="2"'.$disabled.'" />,';
150
+ $rv[] = '</td><td>';
151
+ $rv[] = '<select name="expirationdate_year" id="expirationdate_year"'.$disabled.'">';
152
+ $currentyear = date('Y');
153
+ if ($defaultyear < $currentyear)
154
+ $currentyear = $defaultyear;
155
+ for($i = $currentyear; $i < $currentyear + 8; $i++) {
156
+ if ($i == $defaultyear)
157
+ $selected = ' selected="selected"';
158
+ else
159
+ $selected = '';
160
+ $rv[] = '<option'.$selected.'>'.($i).'</option>';
161
+ }
162
+ $rv[] = '</select>';
163
+ $rv[] = '</td><td>@</td><td>';
164
+ $rv[] = '<input type="text" id="expirationdate_hour" name="expirationdate_hour" value="'.$defaulthour.'" size="2"'.$disabled.'" />';
165
+ $rv[] = '</td></tr></table>';
166
+
167
+ $rv[] = '<div id="expirationdate_ajax_result"></div>';
168
+
169
+ echo implode("\n",$rv);
170
+ }
171
+
172
+ /**
173
+ * PHP Code to be executed by ajax function call - currently nothing happens
174
+ */
175
+ function expirationdate_ajax_process() {
176
+ // Gather Values
177
+ $enable = $_POST['enable'];
178
+
179
+ die(0);
180
+ }
181
+ add_action ('wp_ajax_expirationdate_ajax','expirationdate_ajax_process');
182
+
183
+
184
+ /**
185
+ * Add's ajax javascript
186
+ */
187
+ function expirationdate_js_admin_header() {
188
+ // use JavaScript SACK library for Ajax
189
+ wp_print_scripts( array( 'sack' ));
190
+
191
+ // Define custom JavaScript function
192
+ ?>
193
+ <script type="text/javascript">
194
+ //<![CDATA[
195
+ function expirationdate_ajax_add_meta(expireenable) {
196
+ var mysack = new sack("<?php expirationdate_get_blog_url(); ?>wp-admin/admin-ajax.php");
197
+
198
+ var expire = document.getElementById(expireenable);
199
+
200
+ if (expire.checked == true) {
201
+ var enable = 'true';
202
+ document.getElementById('expirationdate_month').disabled = false;
203
+ document.getElementById('expirationdate_day').disabled = false;
204
+ document.getElementById('expirationdate_year').disabled = false;
205
+ document.getElementById('expirationdate_hour').disabled = false;
206
+ } else {
207
+ document.getElementById('expirationdate_month').disabled = true;
208
+ document.getElementById('expirationdate_day').disabled = true;
209
+ document.getElementById('expirationdate_year').disabled = true;
210
+ document.getElementById('expirationdate_hour').disabled = true;
211
+ var enable = 'false';
212
+ }
213
+
214
+ mysack.execute = 1;
215
+ mysack.method = 'POST';
216
+ mysack.setVar( "action", "expirationdate_ajax" );
217
+ mysack.setVar( "enable", enable );
218
+ mysack.encVar( "cookie", document.cookie, false );
219
+ mysack.onError = function() { alert('Ajax error in looking up elevation' )};
220
+ mysack.runAJAX();
221
+
222
+ return true;
223
+ }
224
+ //]]>
225
+ </script>
226
+ <?php
227
+ }
228
+ add_action('admin_print_scripts', 'expirationdate_js_admin_header' );
229
+
230
+ /**
231
+ * Get correct URL (HTTP or HTTPS)
232
+ */
233
+ function expirationdate_get_blog_url() {
234
+ global $current_blog;
235
+ $schema = ( isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ) ? 'https://' : 'http://';
236
+ echo $schema.$current_blog->domain.$current_blog->path;
237
+ }
238
+
239
+ /**
240
+ * Called when post is saved - stores expiration-date meta value
241
+ */
242
+ function expirationdate_update_post_meta($id) {
243
+ if ( 'autosave' == $_POST['action'] )
244
+ return;
245
+
246
+ $month = $_POST['expirationdate_month'];
247
+ $day = $_POST['expirationdate_day'];
248
+ $year = $_POST['expirationdate_year'];
249
+ $hour = $_POST['expirationdate_hour'];
250
+
251
+ if (isset($_POST['enable-expirationdate'])) {
252
+ // Format Date
253
+ $ts = mktime($hour,0,0,$month,$day,$year);
254
+ // Update Post Meta
255
+ delete_post_meta($id, 'expiration-date');
256
+ update_post_meta($id, 'expiration-date', $ts, true);
257
+ } else {
258
+ delete_post_meta($id, 'expiration-date');
259
+ }
260
+ }
261
+ add_action('save_post','expirationdate_update_post_meta');
262
+
263
+ /**
264
+ * Hook's to add plugin page menu
265
+ */
266
+ function expirationdate_plugin_menu() {
267
+ add_submenu_page('options-general.php','Post Expirator Options','Post Expirator',9,basename(__FILE__),'expirationdate_show_options');
268
+ }
269
+ add_action('admin_menu', 'expirationdate_plugin_menu');
270
+
271
+ /**
272
+ * Show the Expiration Date options page
273
+ */
274
+ function expirationdate_show_options() {
275
+
276
+ if ($_POST['expirationdateSave']) {
277
+ update_option('expirationdateExpiredPostStatus',$_POST['expired-post-status']);
278
+ update_option('expirationdateExpiredPageStatus',$_POST['expired-page-status']);
279
+ update_option('expirationdateDefaultDateFormat',$_POST['expired-default-date-format']);
280
+ echo "<div id='message' class='updated fade'><p>Saved Options!</p></div>";
281
+ }
282
+
283
+ // Get Option
284
+ $expirationdateExpiredPostStatus = get_option('expirationdateExpiredPostStatus');
285
+ if (empty($expirationdateExpiredPostStatus))
286
+ $expirationdateExpiredPostStatus = 'Draft';
287
+
288
+ $expirationdateExpiredPageStatus = get_option('expirationdateExpiredPageStatus');
289
+ if (empty($expirationdateExpiredPageStatus))
290
+ $expirationdateExpiredPageStatus = 'Draft';
291
+
292
+ $expirationdateDefaultDateFormat = get_option('expirationdateDefaultDateFormat');
293
+ if (empty($expirationdateDefaultDateFormat)) {
294
+ global $expirationdateDefaultDateFormat;
295
+ $expirationdateDefaultDateFormat = $expirationdateDefaultDateFormat;
296
+ }
297
+
298
+ ?>
299
+ <div class="wrap">
300
+ <h2><?php _e('Post Expirator Options'); ?></h2>
301
+ <p>
302
+ The post expirator plugin sets a custom meta value, and then optionally allows you to select if you want the post
303
+ changed to a draft status or deleted when it expires.
304
+ </p>
305
+ <form method="post" id="expirationdate_save_options">
306
+ <table class="form-table">
307
+ <tr valign-"top">
308
+ <th scope="row"><label for="expired-post-status">Set Post To:</label></th>
309
+ <td>
310
+ <select name="expired-post-status" id="expired-post-status">
311
+ <option<?php if ($expirationdateExpiredPostStatus == 'Draft'){ echo ' selected="selected"';}?>>Draft</option>
312
+ <option<?php if ($expirationdateExpiredPostStatus == 'Delete'){ echo ' selected="selected"';}?>>Delete</option>
313
+ </select>
314
+ <br/>
315
+ Select whether the post should be deleted or changed to a draft at expiration time.
316
+ </td>
317
+ </tr>
318
+ <tr valign-"top">
319
+ <th scope="row"><label for="expired-page-status">Set Page To:</label></th>
320
+ <td>
321
+ <select name="expired-page-status" id="expired-page-status">
322
+ <option<?php if ($expirationdateExpiredPageStatus == 'Draft'){ echo ' selected="selected"';}?>>Draft</option>
323
+ <option<?php if ($expirationdateExpiredPageStatus == 'Delete'){ echo ' selected="selected"';}?>>Delete</option>
324
+ </select>
325
+ <br/>
326
+ Select whether the page should be deleted or changed to a draft at expiration time.
327
+ </td>
328
+ </tr>
329
+ <tr valign-"top">
330
+ <th scope="row"><label for="expired-default-date-format">Default Date Format:</label></th>
331
+ <td>
332
+ <input type="text" name="expired-default-date-format" id="expired-default-date-format" value="<?php echo $expirationdateDefaultDateFormat ?>" size="25" /> (<?php echo date("$expirationdateDefaultDateFormat") ?>)
333
+ <br/>
334
+ The default format to use when displaying the expiration date within a post using the [postexpirator]
335
+ shortcode. For information on valid formatting options, see: <a href="http://us2.php.net/manual/en/function.date.php" target="_blank">PHP Date Function</a>.
336
+ </td>
337
+ </tr>
338
+ </table>
339
+ <p class="submit">
340
+ <input type="submit" name="expirationdateSave" value="Save" />
341
+ </p>
342
+ </form>
343
+ </div>
344
+ <?php
345
+ }
346
+
347
+ // [postexpirator format="l F jS, Y g:ia" tz="foo"]
348
+ function postexpirator_shortcode($atts) {
349
+ global $post;
350
+ $expirationdatets = get_post_meta($post->ID,'expiration-date',true);
351
+ if (empty($expirationdatets))
352
+ return false;
353
+ extract(shortcode_atts(array(
354
+ 'format' => get_option('expirationdateDefaultDateFormat'),
355
+ 'tz' => date('T'),
356
+ ), $atts));
357
+
358
+ $postexpirator_date_display = get_option('expirationdateDefaultDateFormat');
359
+ if (empty($format))
360
+ $format = $expirationdateDefaultDateFormat;
361
+
362
+ return date("$format",$expirationdatets);
363
+ }
364
+ add_shortcode('postexpirator', 'postexpirator_shortcode');
readme.txt ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Post Expirator ===
2
+ Contributors: axelseaa
3
+ Tags: expire, posts, pages
4
+ Requires at least: 2.5
5
+ Tested up to: 2.6
6
+ Stable tag: trunk
7
+
8
+ Allows you to add an expiration date (hourly) to posts which you can configure to either delete the post or change it to a draft.
9
+
10
+ == Description ==
11
+
12
+ The Post Expirator plugin allows the user to set expiration dates for both posts and pages. There is a configuration option page in the plugins
13
+ area that will allow you to seperataly control whether or not posts/pages are wither deleted or changed to draft status.
14
+
15
+ The plugin hooks into the wp cron processes and runs every hour.
16
+
17
+ The expiration date can be displayed within the actual post by using the [postexpirator] tag. The format attribute will override the plugin
18
+ default display format. See the [PHP Date Function](http://us2.php.net/manual/en/function.date.php) for valid format options.
19
+
20
+ == Wordpress MU ==
21
+
22
+ This plugin is compataibile with Wordpress MU 1.5+, however currently it will not work in the mu-plugins folder due to the plugin activation
23
+ functions.
24
+
25
+ == Credits ==
26
+
27
+ Plugin is based on the orginial [Expiration Date](http://www.hostscope.com/wordpress-plugins/the-expirationdate-wordpress-plugin/) plugin by jrrl.
28
+
29
+ == Installation ==
30
+
31
+ This section describes how to install the plugin and get it working.
32
+
33
+ 1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory
34
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
35
+
36
+ == Changelog ==
37
+
38
+ **Version 1.2**
39
+
40
+ * Changed wording from "Expiration Date" to "Post Expirator" and moved the configuration options to the "Settings" tab.
41
+ * Added shortcode tag [postexpirator] to display the post expiration date within the post
42
+ * Added new setting for the default date display format
43
+ * Fixed bug where expiration date was removed when a post was auto saved
44
+
45
+ **Version 1.1**
46
+
47
+ * Expired posts retain expiration date
48
+
49
+ **Version 1.0**
50
+
51
+ * Initial Release
52
+