my-calendar-templating.php
15.8 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
// Display the admin configuration page
function edit_mc_templates() {
$templates = get_option( 'mc_templates' );
if ( ! empty( $_POST ) ) {
$nonce = $_REQUEST['_wpnonce'];
if ( ! wp_verify_nonce( $nonce, 'my-calendar-nonce' ) ) {
die( "Security check failed" );
}
}
if ( isset( $_POST['mc_template_key'] ) ) {
$key = $_POST['mc_template_key'];
} else {
$key = ( isset( $_GET['mc_template'] ) ) ? $_GET['mc_template'] : 'grid';
}
if ( isset( $_POST['delete'] ) ) {
delete_option( 'mc_ctemplate_' . $key );
echo "<div class=\"updated\"><p>" . __( 'Custom template deleted', 'my-calendar' ) . "</p></div>";
$key = 'grid';
} else {
if ( mc_is_core_template( $key ) && isset( $_POST['add-new'] ) ) {
echo "<div class=\"updated\"><p>" . __( 'Custom templates cannot have the same key as a core template', 'my-calendar' ) . "</p></div>";
} else {
if ( mc_is_core_template( $key ) && isset( $_POST['mc_template'] ) ) {
$template = $_POST['mc_template'];
$templates[$key] = $template;
update_option( 'mc_templates', $templates );
update_option( 'mc_use_' . $key . '_template', ( empty( $_POST['mc_use_template'] ) ? 0 : 1 ) );
echo "<div class=\"updated\"><p>" . sprintf( __( '%s Template saved', 'my-calendar' ), ucfirst( $key ) ) . "</p></div>";
} else if ( isset( $_POST['mc_template'] ) ) {
$template = $_POST['mc_template'];
if ( mc_key_exists( $key ) ) {
$key = mc_update_template( $key, $template );
} else {
$key = mc_create_template( $template );
}
echo "<div class='updated'><p>" . __( 'Custom Template saved', 'my-calendar' ) . "</p></div>";
}
}
}
// TODO: create UI for managing additional templates
// TODO: create admin system for modifying shortcodes
global $grid_template, $list_template, $mini_template, $single_template, $rss_template;
$mc_grid_template = ( $templates['grid'] != '' ) ? $templates['grid'] : $grid_template;
$mc_rss_template = ( $templates['rss'] != '' ) ? $templates['rss'] : $rss_template;
$mc_list_template = ( $templates['list'] != '' ) ? $templates['list'] : $list_template;
$mc_mini_template = ( $templates['mini'] != '' ) ? $templates['mini'] : $mini_template;
$mc_details_template = ( $templates['details'] != '' ) ? $templates['details'] : $single_template;
$template = ( mc_is_core_template( $key ) ) ? ${"mc_" . $key . "_template"} : mc_get_custom_template( $key );
$template = stripslashes( $template );
$core = mc_template_description( $key );
?>
<div class="wrap jd-my-calendar">
<?php my_calendar_check_db(); ?>
<h1 class="wp-heading-inline"><?php _e( 'My Calendar Templates', 'my-calendar' ); ?></h1>
<a href="<?php echo add_query_arg( 'mc_template', 'add-new', admin_url( "admin.php?page=my-calendar-templates" ) ); ?>" class="page-title-action"><?php _e( 'Add New', 'my-calendar' ); ?></a>
<hr class="wp-header-end">
<div class="postbox-container jcd-wide">
<div class="metabox-holder">
<div class="ui-sortable meta-box-sortables">
<div class="postbox">
<h2><?php _e( 'Edit Template', 'my-calendar' ); ?></h2>
<div class="inside">
<p>
<a href="<?php echo admin_url( "admin.php?page=my-calendar-help#templates" ); ?>"><?php _e( "Templates Help", 'my-calendar' ); ?></a> »
</p>
<?php if ( $core != '' ) { echo "<p class='template-description'>$core</p>"; } ?>
<?php
if ( $key == 'add-new' ) {
?>
<form method="post" action="<?php echo add_query_arg( 'mc_template', $key, admin_url( "admin.php?page=my-calendar-templates" ) ); ?>">
<div>
<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'my-calendar-nonce' ); ?>"/>
</div>
<p>
<label for="mc_template_key"><?php _e( 'Template Description (required)', 'my-calendar' ); ?></label><br />
<input type="text" class="widefat" name="mc_template_key" id="mc_template_key" value="" required />
</p>
<p>
<label for="mc_template"><?php _e( 'Custom Template', 'my-calendar' ); ?></label><br/>
<textarea id="mc_template" name="mc_template" class="template-editor widefat" rows="32" cols="76"></textarea>
</p>
<p>
<input type="submit" name="save" class="button-primary" value="<?php _e( 'Add Template', 'my-calendar' ); ?>" />
</p>
</form>
<?php
} else {
?>
<form method="post" action="<?php echo add_query_arg( 'mc_template', $key, admin_url( "admin.php?page=my-calendar-templates" ) ); ?>">
<div>
<input type="hidden" name="_wpnonce" value="<?php echo wp_create_nonce( 'my-calendar-nonce' ); ?>"/>
<input type="hidden" name="mc_template_key" value="<?php esc_attr_e( $key ); ?>"/>
</div>
<?php if ( mc_is_core_template( $key ) ) { ?>
<p>
<input type="checkbox" id="mc_use_template" name="mc_use_template" value="1" <?php mc_is_checked( "mc_use_".$key."_template", 1 ); ?> /> <label for="mc_use_template"><?php _e( 'Use this template', 'my-calendar' ); ?></label>
</p>
<?php } ?>
<p>
<label for="mc_template"><?php _e( 'Custom Template', 'my-calendar' ); ?></label><br/>
<textarea id="mc_template" name="mc_template" class="template-editor widefat" rows="32" cols="76"><?php echo $template; ?></textarea>
</p>
<p>
<input type="submit" name="save" class="button-primary" value="<?php _e( 'Update Template', 'my-calendar' ); ?>" />
<?php if ( ! mc_is_core_template( $key ) ) { ?>
<input type="submit" name="delete" class="button-secondary" value=<?php _e( 'Delete Template', 'my-calendar' ); ?>" />
<?php } ?>
</p>
</form>
<?php } ?>
</div>
</div>
</div>
</div>
<div class="metabox-holder">
<div class="ui-sortable meta-box-sortables">
<div class="postbox">
<h2><?php _e( 'Templates', 'my-calendar' ); ?></h2>
<div class="inside">
<?php mc_list_templates(); ?>
<p>
<a href="<?php echo add_query_arg( 'mc_template', 'add-new', admin_url( "admin.php?page=my-calendar-templates" ) ); ?>"><?php _e( 'Add New Template', 'my-calendar' ); ?></a>
</p>
</div>
</div>
</div>
</div>
<div class="metabox-holder">
<div class="ui-sortable meta-box-sortables">
<div class="postbox">
<h2 class='hndle'><?php _e( 'Event Template Tags', 'my-calendar' ); ?></h2>
<div class='mc_template_tags inside'>
<p>
<a href="<?php echo admin_url( 'admin.php?page=my-calendar-help#templates' ); ?>"><?php _e( 'All Template Tags »', 'my-calendar' ); ?></a>
</p>
<dl>
<dt><code>{title}</code></dt>
<dd><?php _e( 'Title of the event.', 'my-calendar' ); ?></dd>
<dt><code>{link_title}</code></dt>
<dd><?php _e( 'Title of the event as a link if a URL is present, or the title alone if not.', 'my-calendar' ); ?></dd>
<dt><code>{time}</code></dt>
<dd><?php _e( 'Start time for the event.', 'my-calendar' ); ?></dd>
<dt><code>{date}</code></dt>
<dd><?php _e( 'Date on which the event begins.', 'my-calendar' ); ?></dd>
<dt><code>{daterange}</code></dt>
<dd><?php _e( 'Beginning date to end date; excludes end date if same as beginning.', 'my-calendar' ); ?></dd>
<dt><code>{multidate}</code></dt>
<dd><?php _e( 'Multi-day events: an unordered list of dates/times. Otherwise, beginning date/time.', 'my-calendar' ); ?></dd>
<dt><code>{author}</code></dt>
<dd><?php _e( 'Author who posted the event.', 'my-calendar' ); ?></dd>
<dt><code>{host}</code></dt>
<dd><?php _e( 'Name of the assigned host for the event.', 'my-calendar' ); ?></dd>
<dt><code>{shortdesc}</code></dt>
<dd><?php _e( 'Short event description.', 'my-calendar' ); ?></dd>
<dt><code>{description}</code></dt>
<dd><?php _e( 'Description of the event.', 'my-calendar' ); ?></dd>
<dt><code>{image}</code></dt>
<dd><?php _e( 'Image associated with the event.', 'my-calendar' ); ?></dd>
<dt><code>{link}</code></dt>
<dd><?php _e( 'URL provided for the event.', 'my-calendar' ); ?></dd>
<dt><code>{details}</code></dt>
<dd><?php _e( 'Link to an auto-generated page containing information about the event.', 'my-calendar' ); ?>
<dt><code>{event_open}</code></dt>
<dd><?php _e( 'Whether event is currently open for registration.', 'my-calendar' ); ?></dd>
<dt><code>{event_status}</code></dt>
<dd><?php _e( 'Current status of event: either "Published" or "Reserved."', 'my-calendar' ); ?></dd>
</dl>
<h3><?php _e( 'Location Template Tags', 'my-calendar' ); ?></h3>
<dl>
<dt><code>{location}</code></dt>
<dd><?php _e( 'Name of the location of the event.', 'my-calendar' ); ?></dd>
<dt><code>{street}</code></dt>
<dd><?php _e( 'First line of the site address.', 'my-calendar' ); ?></dd>
<dt><code>{street2}</code></dt>
<dd><?php _e( 'Second line of the site address.', 'my-calendar' ); ?></dd>
<dt><code>{city}</code></dt>
<dd><?php _e( 'City', 'my-calendar' ); ?></dd>
<dt><code>{state}</code></dt>
<dd><?php _e( 'State', 'my-calendar' ); ?></dd>
<dt><code>{postcode}</code></dt>
<dd><?php _e( 'Postal Code', 'my-calendar' ); ?></dd>
<dt><code>{region}</code></dt>
<dd><?php _e( 'Custom region.', 'my-calendar' ); ?></dd>
<dt><code>{country}</code></dt>
<dd><?php _e( 'Country for the event location.', 'my-calendar' ); ?></dd>
<dt><code>{sitelink}</code></dt>
<dd><?php _e( 'Output the URL for the location.', 'my-calendar' ); ?></dd>
<dt><code>{hcard}</code></dt>
<dd><?php _e( 'Event address in <a href="http://microformats.org/wiki/hcard">hcard</a> format.', 'my-calendar' ); ?></dd>
<dt><code>{link_map}</code></dt>
<dd><?php _e( 'Link to Google Map to the event, if address information is available.', 'my-calendar' ); ?></dd>
</dl>
<h3><?php _e( 'Category Template Tags', 'my-calendar' ); ?></h3>
<dl>
<dt><code>{category}</code></dt>
<dd><?php _e( 'Name of the category of the event.', 'my-calendar' ); ?></dd>
<dt><code>{icon}</code></dt>
<dd><?php _e( 'URL for the event\'s category icon.', 'my-calendar' ); ?></dd>
<dt><code>{color}</code></dt>
<dd><?php _e( 'Hex code for the event\'s category color.', 'my-calendar' ); ?></dd>
<dt><code>{cat_id}</code></dt>
<dd><?php _e( 'ID of the category of the event.', 'my-calendar' ); ?></dd>
</dl>
</div>
</div>
</div>
</div>
</div>
</div>
<?php mc_show_sidebar();
}
function mc_is_core_template( $key ) {
switch( $key ) {
case 'grid':
case 'details':
case 'list':
case 'rss':
case 'mini':
return true;
break;
default:
return false;
}
}
function mc_get_custom_template( $key ) {
$return = get_option( "mc_ctemplate_$key" );
return $return;
}
function mc_key_exists( $key ) {
if ( get_option( "mc_ctemplate_$key", 'missing' ) != 'missing' ) {
return true;
}
return false;
}
function mc_template_exists( $template ) {
$key = md5( $template );
if ( get_option( "mc_ctemplate_$key", 'missing' ) != 'missing' ) {
return true;
}
return false;
}
function mc_create_template( $template ) {
$key = md5( $template );
$description = $_POST['mc_template_key'];
update_option( "mc_template_desc_$key", $description );
update_option( "mc_ctemplate_$key", $template );
return $key;
}
function mc_update_template( $key, $template ) {
update_option( "mc_ctemplate_$key", $template );
return $key;
}
function mc_template_description( $key ) {
$return = sprintf( __( 'Custom template, keyword %s', 'my-calendar' ), "<code>$key</code>" );
$description = '';
switch( $key ) {
case 'grid':
$return = __( '<strong>Core Template:</strong> used in the details pop-up in the main calendar view.', 'my-calendar' );
break;
case 'details':
$return = __( '<strong>Core Template:</strong> used on the single event view.', 'my-calendar' );
break;
case 'list':
$return = __( '<strong>Core Template:</strong> used when viewing events in the main calendar list view.', 'my-calendar' );
break;
case 'rss':
$return = __( '<strong>Core Template:</strong> used for RSS feeds.', 'my-calendar' );
break;
case 'mini':
$return = __( '<strong>Core Template:</strong> used in pop-ups for the mini calendar.', 'my-calendar' );
break;
}
if ( ! mc_is_core_template( $key ) ) {
$description = strip_tags( stripslashes( get_option( "mc_template_desc_$key" ) ) );
}
$br = ( $description != '' ) ? '<br />' : '';
return $description . $br . $return;
}
function mc_list_templates() {
$check = "<span class='dashicons dashicons-yes' aria-hidden='true'></span><span>" . __( 'Enabled', 'my-calendar' ) . "</span>";
$uncheck = "<span class='dashicons dashicons-no' aria-hidden='true'></span><span>" . __( 'Not Enabled', 'my-calendar' ) . "</span>";
$grid_enabled = ( get_option( 'mc_use_grid_template' ) == 1 ) ? $check : $uncheck;
$list_enabled = ( get_option( 'mc_use_list_template' ) == 1 ) ? $check : $uncheck;
$mini_enabled = ( get_option( 'mc_use_mini_template' ) == 1 ) ? $check : $uncheck;
$details_enabled = ( get_option( 'mc_use_details_template' ) == 1 ) ? $check : $uncheck;
$rss_enabled = ( get_option( 'mc_use_rss_template' ) == 1 ) ? $check : $uncheck;
$list = "<table class='widefat'>
<thead>
<tr>
<th scope='col'>Template Key</th>
<th scope='col'>Description</th>
<th scope='col'>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href='" . add_query_arg( 'mc_template', 'grid', admin_url( "admin.php?page=my-calendar-templates" ) ) . "'>grid</a></td>
<td>" . mc_template_description( 'grid' ) . "</td>
<td>$grid_enabled</td>
</tr>
<tr>
<td><a href='" . add_query_arg( 'mc_template', 'list', admin_url( "admin.php?page=my-calendar-templates" ) ) . "'>list</a></td>
<td>" . mc_template_description( 'list' ) . "</td>
<td>$list_enabled</td>
</tr>
<tr>
<td><a href='" . add_query_arg( 'mc_template', 'mini', admin_url( "admin.php?page=my-calendar-templates" ) ) . "'>mini</a></td>
<td>" . mc_template_description( 'mini' ) . "</td>
<td>$mini_enabled</td>
</tr>
<tr>
<td><a href='" . add_query_arg( 'mc_template', 'details', admin_url( "admin.php?page=my-calendar-templates" ) ) . "'>details</a></td>
<td>" . mc_template_description( 'details' ) . "</td>
<td>$details_enabled</td>
</tr>
<tr>
<td><a href='" . add_query_arg( 'mc_template', 'rss', admin_url( "admin.php?page=my-calendar-templates" ) ) . "'>rss</a></td>
<td>" . mc_template_description( 'rss' ) . "</td>
<td>$rss_enabled</td>
</tr>";
global $wpdb;
$select = "SELECT * FROM " . $wpdb->prefix . "options WHERE option_name LIKE '%mc_ctemplate_%'";
$results = $wpdb->get_results( $select );
foreach( $results as $result ) {
$key = str_replace( 'mc_ctemplate_', '', $result->option_name );
$desc = mc_template_description( $key );
$list .= "<tr>
<td><a href='" . add_query_arg( 'mc_template', $key, admin_url( "admin.php?page=my-calendar-templates" ) ) . "'>$key</a></td>
<td>$desc</td>
<td> -- </td>
</tr>";
}
$list .= "</tbody>
</table>";
echo $list;
}