-
Notifications
You must be signed in to change notification settings - Fork 29
/
change-date-links.php
124 lines (102 loc) · 3.68 KB
/
change-date-links.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
<?php
/*
Plugin Name: Change Date Permalink Structure
Plugin URI: http://wordpress.stackexchange.com/q/39274/6035
Description: Give the data permalink structure a "base"
Author: Christopher Davis
Author URI: http://christopherdavis.me
License: GPL2
Copyright 2012 Christopher Davis
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
!defined('ABSPATH') && exit;
Custom_Date_Base::init();
class Custom_Date_Base
{
const SETTING = 'custom_date_base';
private static $ins = null;
public static function init()
{
add_action('plugins_loaded', array(self::instance(), '_setup'));
}
public static function instance()
{
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
public function _setup()
{
add_action('admin_init', array($this, 'settings'));
add_action('load-options-permalink.php', array($this, 'save'));
add_action('init', array($this, 'set_date_base'));
}
public function settings()
{
add_settings_field(
'custom-date-base',
__('Date Base', 'custom-date-base'),
array($this, 'field_cb'),
'permalink',
'optional',
array('label_for' => self::SETTING)
);
}
public function field_cb()
{
printf(
'<input type="text" class="regular-text" id="%1$s" name="%1$s" value="%2$s" />',
esc_attr(self::SETTING),
esc_attr(get_option(self::SETTING))
);
}
// apparently options-permalink only halfways uses the settings api?
public function save()
{
// make sure it's actually an update request.
if('POST' != $_SERVER['REQUEST_METHOD'])
return;
// since this fires before the actual update stuff,
// validate the permalink nonce.
check_admin_referer('update-permalink');
if(!empty($_POST[self::SETTING]))
{
update_option(
self::SETTING,
sanitize_title_with_dashes($_POST[self::SETTING])
);
}
else
{
// remove it.
delete_option(self::SETTING);
}
}
public function set_date_base()
{
if($db = get_option(self::SETTING))
{
global $wp_rewrite;
// current date permastruct
$date_s = $wp_rewrite->get_date_permastruct();
// get the "front" -- stuff before rewrite tags in post links
$front = isset($wp_rewrite->front) ? $wp_rewrite->front : '/';
// build some regex. We need to account for the global rewrite
// "front" as well as a possible "/date" that WP appends for
// %post_id% permalink structure where the numbers of a Post ID
// might conflict with with year/month/day numbers.
$regex = '#^' . preg_quote($front, '#') . '(/date)?#ui';
$wp_rewrite->date_structure = preg_replace($regex, "/{$db}/", $date_s);
// apprently we need to make sure this happens?
flush_rewrite_rules();
}
}
}