This repository was archived by the owner on Mar 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-plugin-settings.php
188 lines (147 loc) · 3.95 KB
/
class-plugin-settings.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
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
<?php
/**
* class-plugin-settings.php
* @internal Description needed.
* @package Events Calendar
* @since 7.0
* @author Luke Howell <[email protected]>
* @copyright Copyright (c) 2007-2012 Luke Howell
* @license GPLv2 {@link http://www.gnu.org/licenses/gpl-2.0.html}
*/
/**
* Plugin settings
* @internal Complete description
* @package events-calendar
* @since 7.0
* @author Luke Howell <[email protected]>
*/
if( !class_exists( 'WPEC_Plugin_Settings' ) ) :
class WPEC_Plugin_Settings
{
/**
* Constructor
* Adds hooks and filters to get things started
* @internal Complete description
* @since 7.0
* @author Luke Howell <[email protected]>
*/
function __construct()
{
add_action( 'admin_menu', array( &$this, 'create_menu' ) );
add_action( 'admin_init', array( &$this, 'register_settings' ) );
add_action( 'update_option_wpec_options', array( &$this, 'options_updated' ), 10, 2 );
}
/**
* Create the menu
* @internal Complete description
* @since 7.0
* @author Luke Howell <[email protected]>
*/
function create_menu()
{
add_options_page( 'WP Events Calendar', 'Events Calendar', 'manage_options' ,'wpec', array( &$this, 'settings_page' ) );
}
/**
* Output the settings page
* @internal Complete description
* @since 7.0
* @author Luke Howell <[email protected]>
*/
function settings_page()
{
?>
<div class="wrap">
<h2>WP Events Calendar</h2>
<form method="post" action="options.php">
<?php settings_fields( 'wpec_options' ); ?>
<?php do_settings_sections( 'wpec' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Registers the settings
* @internal Complete description
* @since 1.0
*/
function register_settings()
{
// Register the setting
register_setting( 'wpec_options',
'wpec_options',
array( &$this, 'validate_options' ) );
// Front end settings section
add_settings_section( 'wpec-main',
__( 'Main Settings', WPEC_L10N ),
array( &$this, 'main_section' ),
'wpec' );
// Event post type slug
add_settings_field( 'post_type_slug',
__( 'Custom Post Slug for Events', WPEC_L10N ),
array( &$this, 'setting_post_type_slug' ),
'wpec',
'wpec-main' );
}
/**#@+
* Settings sections
* @internal Complete description
*/
/**
* Main Settings Section
* @internal Complete description
* @since 1.0
*/
function main_section()
{
?>
<p>
<?php _e( 'This section is used to edit the main functionality of the plugin.', WPEC_L10N ); ?>
</p>
<?php
}
/**#@-*/
/**
* Create setting to change the post type
* @internal Complete description
* @since 7.0
* @author Luke Howell <[email protected]>
*/
function setting_post_type_slug()
{
$options = get_option( 'wpec_options' );
echo "<input id='post_type_slug' name='wpec_options[post_type_slug]' size='40' type='text' value='{$options[ 'post_type_slug' ]}'>";
}
/**
* Validate the options
* @internal Complete description
* @since 7.0
* @author Luke Howell <[email protected]>
*/
function validate_options( $input )
{
// Strip slashes and make sure it is not empty
$input[ 'post_type_slug' ] = str_replace( array( '/', '\\' ), '', $input[ 'post_type_slug' ] );
$input[ 'post_type_slug' ] = empty( $input[ 'post_type_slug' ] ) ? 'event' : $input[ 'post_type_slug' ];
return $input;
}
/**
* Run when options are updated
* @internal Complete description
* @since 7.0
* @author Luke Howell <[email protected]>
*/
function options_updated( $old_options, $new_options )
{
if( $old_options[ 'post_type_slug' ] != $new_options[ 'post_type_slug' ] )
{
$posts = get_posts( array( 'post_type' => $old_options[ 'post_type_slug' ] ) );
foreach( $posts as $post )
{
set_post_type( $post->ID, $new_options[ 'post_type_slug' ] );
}
}
flush_rewrite_rules();
}
}
endif;