-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin-fields-renamer.php
270 lines (195 loc) · 6.79 KB
/
admin-fields-renamer.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
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
<?php
/*
Plugin Name: Admin Fields Renamer
Plugin URI: http://kateryna.blog
Description: This plugin renames fields in the admin area for posts
Author: Kateryna Kodonenko
Author URI: http://kateryna.blog
Text Domain: adminrenamer
Version: 2.0
*/
// ===================================================================================================
// SECTION 1
// Add menu settings page for the plugin
//Add menu page for the plugin
function renamer_settings_page() {
add_menu_page(
'Admin Renamer',
'Admin Renamer',
'manage_options',
'adminrenamer',
'adminrenamer_settings_page_callback',
'dashicons-wordpress-alt',
100
);
}
add_action( 'admin_menu', 'renamer_settings_page');
// ===================================================================================================
// SECTION 2
// Callback for the menu section page;
function adminrenamer_settings_page_callback(){
//Double check user capabilities
if ( !current_user_can('manage_options') ) {
return;
}
?>
<div class="wrap">
<!-- Form output on the settings page !-->
<h1><?php esc_html_e( get_admin_page_title() ); ?></h1>
<form method="post" action="options.php">
<!-- Display necessary hidden fields for settings -->
<?php settings_fields( 'renamer_settings' ); ?>
<!-- Display the settings sections for the page -->
<?php do_settings_sections( 'adminrenamer' ); ?>
<!-- Default Submit Button -->
<?php submit_button(); ?>
</form>
</div>
<?php
}
// ===================================================================================================
// SECTION 3
// Add settings page section
//Check if the plugin settings exist and if don't, then create them
function renamer_settings() {
//if( !get_options( 'renamer_settings') ) {
// add_option( 'renamer_settings' );
//}
// Define (at least) one section for our fields
add_settings_section(
// Unique identifier for the section
'renamer_settings_section',
// Section Title
__( 'Renamer Fields', 'adminrenamer' ),
// Callback for an optional description
'renamer_settings_section_callback',
// Admin page to add section to
'adminrenamer'
);
//Checkbox field for Column Remover Checkbox
add_settings_field(
'renamer_settings_checkbox',
__( 'Column Remover', 'adminrenamer'),
'renamer_settings_checkbox_callback',
'adminrenamer',
'renamer_settings_section',
[
'label' => 'Remove the Date Column'
]
);
// Checkbox field for Subscriptions activating checkbox
add_settings_field(
'renamer_settings_checkbox2',
__( 'Subscriptions Indicator', 'adminrenamer'),
'renamer_settings_checkbox_callback2',
'adminrenamer',
'renamer_settings_section',
[
'label' => 'Show subscriptions'
]
);
add_settings_field(
// Unique identifier for field
'renamer_settings_custom_text',
// Field Title
__( 'Custom Text', 'adminrenamer'),
// Callback for field markup
'renamer_settings_custom_text_callback',
// Page to go on
'adminrenamer',
// Section to go in
'renamer_settings_section'
);
register_setting(
'renamer_settings',
'renamer_settings'
);
}
add_action( 'admin_init', 'renamer_settings' );
// ===================================================================================================
// SECTION 4
// Callbacks for the settings section;
//callback for the settings sections
function renamer_settings_section_callback() {
esc_html_e( 'Enter your custom names for the admin area sections', 'adminrenamer' );
}
//callback for the checkbox field for Column Remover
function renamer_settings_checkbox_callback( $args ) {
$options = get_option( 'renamer_settings' );
error_log(print_r($options, true));
$checkbox1 = '';
if( isset( $options[ 'checkbox1' ] ) ) {
$checkbox1 = esc_html( $options['checkbox1'] );
}
$html = '<input type="checkbox" id="renamer_settings_checkbox" name="renamer_settings[checkbox1]" value="1"' . checked( 1, $checkbox1, false ) . '/>';
$html .= ' ';
$html .= '<label for="renamer_settings[checkbox1]">' . $args['label'] . '</label>';
echo $html;
}
//callback for the checkbox field for Subscriptions Activation
function renamer_settings_checkbox_callback2( $args ) {
$options = get_option( 'renamer_settings' );
$checkbox2 = '';
if( isset( $options[ 'checkbox2' ] ) ) {
$checkbox2 = esc_html( $options['checkbox2'] );
}
$html = '<input type="checkbox" id="renamer_settings_checkbox2" name="renamer_settings[checkbox2]" value="1"' . checked( 1, $checkbox2, false ) . '/>';
$html .= ' ';
$html .= '<label for="renamer_settings[checkbox2]">' . $args['label'] . '</label>';
echo $html;
}
//callback for the Column Renamer custom text field
function renamer_settings_custom_text_callback() {
$options = get_option( 'renamer_settings' );
$custom_text = '';
if( isset( $options[ 'custom_text' ] ) ) {
$custom_text = esc_html( $options['custom_text'] );
}
echo '<input type="text" id="renamer_customtext" name="renamer_settings[custom_text]" value="' . $custom_text . '" />';
}
// ===================================================================================================
// SECTION 5
// Settings section performing custom actions
// rename the author column in post admin field - tied to custom text field
function rename_columns ( $columns ){
$options = get_option( 'renamer_settings' );
$columns ['author'] = esc_html( $options['custom_text'] );
return $columns;
}
add_filter ('manage_post_posts_columns', 'rename_columns', 30 );
// unset the date column on the posts admin field tied to the checkbox field for Column Remover
function my_manage_columns( $columns ) {
unset($columns['date']);
return $columns;
}
function my_column_init() {
$options = get_option( 'renamer_settings' );
if( isset( $options[ 'checkbox1' ]) && $options['checkbox1'] == '1' ) {
add_filter( 'manage_post_posts_columns' , 'my_manage_columns' );
}
}
add_action( 'admin_init' , 'my_column_init' );
// Subscriptions Activation section
//adding a new column tied to WooCommerce Subscriptions
function add_product_column( $columns ) {
//add column
$columns['new_column'] = __( 'Simple Sub', 'woocommerce' );
return $columns;
}
add_filter( 'manage_edit-product_columns', 'add_product_column', 10, 1 );
// function to populate the products column
add_action( 'manage_product_posts_custom_column', 'subs_populate_admin', 10, 2 );
// Subscription activation checkbox that will enable the subscription indicator column
// Tied to checkbox Subscriptions activation
$options = get_option( 'renamer_settings' );
if( isset( $options[ 'checkbox2' ]) && $options['checkbox1'] == '1' ) {
function subs_populate_admin( $column_name ) {
if ( $column_name == 'new_column' ) {
$product = wc_get_product ();
if ( WC_Subscriptions_Product::is_subscription( $product) ) {
echo 'yes';
}
}
}
}
?>