-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patheme-categories.php
602 lines (554 loc) · 23.4 KB
/
eme-categories.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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
function eme_new_category() {
$category = [
'category_name' => '',
'category_slug' => '',
'category_prefix' => '',
'description' => '',
];
return $category;
}
function eme_categories_page() {
global $wpdb;
if ( ! current_user_can( get_option( 'eme_cap_categories' ) ) && ( isset( $_GET['eme_admin_action'] ) || isset( $_POST['eme_admin_action'] ) ) ) {
$message = __( 'You have no right to update categories!', 'events-made-easy' );
eme_categories_table_layout( $message );
return;
}
if ( isset( $_GET['eme_admin_action'] ) && $_GET['eme_admin_action'] == 'edit_category' ) {
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
// edit category
eme_categories_edit_layout();
return;
}
if ( isset( $_POST['eme_admin_action'] ) && $_POST['eme_admin_action'] == 'add_category' ) {
// edit template
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
eme_categories_edit_layout();
return;
}
// Insert/Update/Delete Record
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
$message = '';
if ( isset( $_POST['eme_admin_action'] ) ) {
check_admin_referer( 'eme_admin', 'eme_admin_nonce' );
if ( $_POST['eme_admin_action'] == 'do_editcategory' ) {
// category update required
$category = [];
$category['category_name'] = eme_sanitize_request( $_POST['category_name'] );
$category['description'] = eme_kses( $_POST['description'] );
if ( ! empty( $_POST['category_prefix'] ) ) {
$category['category_prefix'] = eme_sanitize_request( $_POST['category_prefix'] );
}
if ( ! empty( $_POST['category_slug'] ) ) {
$category['category_slug'] = eme_permalink_convert_noslash( eme_sanitize_request( $_POST['category_slug'] ) );
} else {
$category['category_slug'] = eme_permalink_convert_noslash( $category['category_name'] );
}
if ( isset( $_POST['category_id'] ) && intval( $_POST['category_id'] ) > 0 ) {
$validation_result = $wpdb->update( $categories_table, $category, [ 'category_id' => intval( $_POST['category_id'] ) ] );
if ( $validation_result !== false ) {
$message = __( 'Successfully edited the category', 'events-made-easy' );
} else {
$message = __( 'There was a problem editing your category, please try again.', 'events-made-easy' );
}
} else {
$validation_result = $wpdb->insert( $categories_table, $category );
if ( $validation_result !== false ) {
$message = __( 'Successfully added the category', 'events-made-easy' );
} else {
$message = __( 'There was a problem adding your category, please try again.', 'events-made-easy' );
}
}
} elseif ( $_POST['eme_admin_action'] == 'do_deletecategory' && isset( $_POST['categories'] ) ) {
// Delete category or multiple
$categories = eme_sanitize_request( $_POST['categories']);
if ( ! empty( $categories ) && eme_is_numeric_array( $categories ) ) {
$ids_list = implode(',', $categories);
$validation_result = $wpdb->query( "DELETE FROM $categories_table WHERE category_id IN ( $ids_list )"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ( $validation_result !== false ) {
$message = __( 'Successfully deleted the selected categories.', 'events-made-easy' );
} else {
$message = __( 'There was a problem deleting the selected categories, please try again.', 'events-made-easy' );
}
} else {
$message = __( "Couldn't delete the categories. Incorrect category IDs supplied. Please try again.", 'events-made-easy' );
}
}
}
eme_categories_table_layout( $message );
}
function eme_categories_table_layout( $message = '' ) {
global $plugin_page;
$categories = eme_get_categories();
$destination = admin_url( "admin.php?page=$plugin_page" );
$nonce_field = wp_nonce_field( 'eme_admin', 'eme_admin_nonce', false, false );
$table = "
<div class='wrap nosubsub'>
<div id='poststuff'>
<div id='icon-edit' class='icon32'>
</div>
<h1>" . __( 'Manage categories', 'events-made-easy' ) . "</h1>\n ";
if ( $message != '' ) {
$table .= "
<div id='message' class='updated notice notice-success is-dismissible'>
<p>$message</p>
</div>";
}
$table .= "
<div class='wrap'>
<form id='categories-new' method='post' action='$destination'>
$nonce_field
<input type='hidden' name='eme_admin_action' value='add_category'>
<input type='submit' class='button-primary' name='submit' value='" . __( 'Add category', 'events-made-easy' ) . "'>
</form>
</div>
<br><br>
<form id='categories-form' method='post' action='$destination'>
<input type='hidden' name='eme_admin_action' value='do_deletecategory'>";
$table .= $nonce_field;
if ( count( $categories ) > 0 ) {
$table .= "<table class='widefat'>
<thead>
<tr>
<th class='manage-column column-cb check-column' scope='col'><input type='checkbox' class='select-all' value='1'></th>
<th>" . __( 'ID', 'events-made-easy' ) . '</th>
<th>' . __( 'Name', 'events-made-easy' ) . "</th>
</tr>
</thead>
<tbody>";
foreach ( $categories as $this_category ) {
if ( empty( $this_category['category_name'] ) ) {
$this_category['category_name'] = __( 'No name', 'events-made-easy' );
}
$table .= "
<tr>
<td><input type='checkbox' class ='row-selector' value='" . $this_category['category_id'] . "' name='categories[]'></td>
<td><a href='" . wp_nonce_url( admin_url( 'admin.php?page=eme-categories&eme_admin_action=edit_category&category_id=' . $this_category['category_id'] ), 'eme_admin', 'eme_admin_nonce' ) . "'>" . $this_category['category_id'] . "</a></td>
<td><a href='" . wp_nonce_url( admin_url( 'admin.php?page=eme-categories&eme_admin_action=edit_category&category_id=' . $this_category['category_id'] ), 'eme_admin', 'eme_admin_nonce' ) . "'>" . eme_trans_esc_html( $this_category['category_name'] ) . '</a></td>
</tr>
';
}
$delete_text = esc_html__( 'Are you sure you want to delete these categories?', 'events-made-easy' );
$delete_button_text = esc_html__( 'Delete', 'events-made-easy' );
$table .= "
</tbody>
<tfoot>
<tr>
<td class='manage-column column-cb check-column' scope='col'><input type='checkbox' class='select-all' value='1'></td>
<td>" . __( 'ID', 'events-made-easy' ) . '</td>
<td>' . __( 'Name', 'events-made-easy' ) . "</td>
</tr>
</tfoot>
</table>
<div class='tablenav'>
<div class='alignleft actions'>
<input class='button-primary action' type='submit' name='doaction' value='$delete_button_text' onclick=\"return areyousure('$delete_text');\">
<br class='clear'>
</div>
<br class='clear'>
</div>
";
} else {
$table .= '<p>' . __( 'No categories defined.', 'events-made-easy' );
}
$table .= '
</form>
</div>
</div>';
echo $table;
}
function eme_categories_edit_layout( $message = '' ) {
global $plugin_page;
if ( ! empty( $_GET['category_id'] ) ) {
$category_id = intval( $_GET['category_id'] );
$category = eme_get_category( $category_id );
$h1_string = __( 'Edit category', 'events-made-easy' );
$action_string = __( 'Update category', 'events-made-easy' );
$permalink_string = __( 'Permalink: ', 'events-made-easy' );
$action = 'edit';
} else {
$category_id = 0;
$category = eme_new_category();
$h1_string = __( 'Create category', 'events-made-easy' );
$action_string = __( 'Add category', 'events-made-easy' );
$permalink_string = __( 'Permalink prefix: ', 'events-made-easy' );
$action = 'add';
}
$nonce_field = wp_nonce_field( 'eme_admin', 'eme_admin_nonce', false, false );
?>
<div class='wrap'>
<div id='icon-edit' class='icon32'>
</div>
<h1><?php echo $h1_string; ?></h1>
<?php if ( $message != '' ) { ?>
<div id='message' class='updated notice notice-success is-dismissible'>
<p><?php echo $message; ?></p>
</div>
<?php } ?>
<div id='ajax-response'></div>
<form name='edit_category' id='edit_category' method='post' action='<?php echo admin_url( "admin.php?page=$plugin_page" ); ?>'>
<input type='hidden' name='eme_admin_action' value='do_editcategory'>
<input type='hidden' name='category_id' value='<?php echo $category_id; ?>'>
<?php echo $nonce_field; ?>
<table class='form-table'>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='category_name'><?php esc_html_e( 'Category name', 'events-made-easy' ); ?></label></th>
<td><input name='category_name' id='category_name' type='text' required='required' value='<?php echo esc_html( $category['category_name'] ); ?>' size='40'><br>
<?php esc_html_e( 'The name of the category', 'events-made-easy' ); ?></td>
</tr>
<tr class='form-field'>
<th scope='row' style='vertical-align:top'><label for='description'><?php esc_html_e( 'Category description', 'events-made-easy' ); ?></label></th>
<td><div id="<?php echo user_can_richedit() ? 'postdivrich' : 'postdiv'; ?>" class="postarea">
<!-- we need description for qtranslate as ID -->
<?php
eme_wysiwyg_textarea( 'description', $category['description'], 1, 1 );
?>
<br><?php esc_html_e( 'The description of the category', 'events-made-easy' ); ?>
</div>
</td>
</tr>
<tr>
<th scope='row' style='vertical-align:top'><label for='slug'><?php echo $permalink_string; ?></label></th>
<td>
<?php
echo trailingslashit( home_url() );
$categories_prefixes = get_option( 'eme_permalink_categories_prefix', '' );
if ( empty( $categories_prefixes ) ) {
$extra_prefix = 'cat/';
$categories_prefixes = get_option( 'eme_permalink_events_prefix', 'events' );
} else {
$extra_prefix = '';
}
if ( preg_match( '/,/', $categories_prefixes ) ) {
$categories_prefixes = explode( ',', $categories_prefixes );
$categories_prefixes_arr = [];
foreach ( $categories_prefixes as $categories_prefix ) {
$categories_prefixes_arr[ $categories_prefix ] = eme_permalink_convert( $categories_prefix );
}
$prefix = $category['category_prefix'] ? $category['category_prefix'] : '';
echo eme_ui_select( $prefix, 'category_prefix', $categories_prefixes_arr );
} else {
echo eme_permalink_convert( $categories_prefixes );
}
echo $extra_prefix;
if ( $action == 'edit' ) {
$slug = $category['category_slug'] ? $category['category_slug'] : $category['category_name'];
$slug = eme_permalink_convert_noslash( $slug );
?>
<input type="text" id="slug" name="category_slug" value="<?php echo $slug; ?>"><?php echo user_trailingslashit( '' ); ?>
<?php
}
?>
</td>
</tr>
</table>
<p class='submit'><input type='submit' class='button-primary' name='submit' value='<?php echo $action_string; ?>'></p>
</form>
</div>
<?php
}
function eme_get_cached_categories() {
$cats = wp_cache_get( 'eme_all_cats' );
if ( $cats === false ) {
$cats = eme_get_categories();
wp_cache_set( 'eme_all_cats', $cats, '', 60 );
}
return $cats;
}
function eme_get_categories( $eventful = false, $scope = 'future', $extra_conditions = '' ) {
global $wpdb;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
$categories = [];
$order_by = ' ORDER BY category_name ASC';
if ( $eventful ) {
$events = eme_get_events( scope: $scope, order: 'ASC' );
if ( $events ) {
foreach ( $events as $event ) {
if ( ! empty( $event['event_category_ids'] ) ) {
$event_cats = explode( ',', $event['event_category_ids'] );
if ( ! empty( $event_cats ) ) {
foreach ( $event_cats as $category_id ) {
$categories[ $category_id ] = $category_id;
}
}
}
}
}
if ( ! empty( $categories ) && eme_is_numeric_array( $categories ) ) {
$event_cats = join( ',', $categories );
if ( $extra_conditions != '' ) {
$extra_conditions = " AND ($extra_conditions)";
}
$result = $wpdb->get_results( "SELECT * FROM $categories_table WHERE category_id IN ( $event_cats ) $extra_conditions $order_by", ARRAY_A); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
} else {
if ( $extra_conditions != '' ) {
$extra_conditions = " WHERE ($extra_conditions)";
}
$result = $wpdb->get_results( "SELECT * FROM $categories_table $extra_conditions $order_by", ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
if ( has_filter( 'eme_categories_filter' ) ) {
$result = apply_filters( 'eme_categories_filter', $result );
}
return $result;
}
function eme_get_categories_filtered( $category_ids, $categories ) {
$cat_id_arr = explode( ',', $category_ids );
$new_arr = [];
foreach ( $categories as $cat ) {
if ( in_array( $cat['category_id'], $cat_id_arr ) ) {
$new_arr[] = $cat;
}
}
return $new_arr;
}
function eme_get_category( $category_id ) {
global $wpdb;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
$sql = $wpdb->prepare( "SELECT * FROM $categories_table WHERE category_id = %d", $category_id );
return $wpdb->get_row( $sql, ARRAY_A );
}
function eme_get_event_category_names( $event_id, $extra_conditions = '', $order_by = '' ) {
global $wpdb;
$event_table = EME_DB_PREFIX . EME_EVENTS_TBNAME;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
if ( $extra_conditions != '' ) {
$extra_conditions = " AND ($extra_conditions)";
}
if ( $order_by != '' ) {
$order_by = " ORDER BY $order_by";
}
$sql = $wpdb->prepare( "SELECT category_name FROM $categories_table, $event_table where event_id = %d AND FIND_IN_SET(category_id,event_category_ids) $extra_conditions $order_by", $event_id );
return $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
function eme_get_event_category_descriptions( $event_id, $extra_conditions = '', $order_by = '' ) {
global $wpdb;
$event_table = EME_DB_PREFIX . EME_EVENTS_TBNAME;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
if ( $extra_conditions != '' ) {
$extra_conditions = " AND ($extra_conditions)";
}
if ( $order_by != '' ) {
$order_by = " ORDER BY $order_by";
}
$sql = $wpdb->prepare( "SELECT description FROM $categories_table, $event_table where event_id = %d AND FIND_IN_SET(category_id,event_category_ids) $extra_conditions $order_by", $event_id );
return $wpdb->get_col( $sql ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
function eme_get_event_categories( $event_id, $extra_conditions = '', $order_by = '' ) {
global $wpdb;
$event_table = EME_DB_PREFIX . EME_EVENTS_TBNAME;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
if ( $extra_conditions != '' ) {
$extra_conditions = " AND ($extra_conditions)";
}
if ( $order_by != '' ) {
$order_by = " ORDER BY $order_by";
}
$sql = $wpdb->prepare( "SELECT $categories_table.* FROM $categories_table, $event_table where event_id = %d AND FIND_IN_SET(category_id,event_category_ids) $extra_conditions $order_by", $event_id );
return $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
function eme_get_category_eventids( $category_id, $future_only = 1 ) {
// similar to eme_get_recurrence_eventids
global $wpdb;
$events_table = EME_DB_PREFIX . EME_EVENTS_TBNAME;
$extra_condition = '';
if ( $future_only ) {
$eme_date_obj = new ExpressiveDate( 'now', EME_TIMEZONE );
$today = $eme_date_obj->getDateTime();
$extra_condition = "AND event_start > '$today'";
}
$cat_ids = explode( ',', $category_id );
$event_ids = [];
foreach ( $cat_ids as $cat_id ) {
$sql = $wpdb->prepare( "SELECT event_id FROM $events_table WHERE FIND_IN_SET(%d,event_category_ids) $extra_condition ORDER BY event_start ASC, event_name ASC", $cat_id );
if ( empty( $event_ids ) ) {
$event_ids = $wpdb->get_col( $sql );
} else {
$event_ids = array_unique( array_merge( $event_ids, $wpdb->get_col( $sql ) ) );
}
}
return $event_ids;
}
function eme_get_location_categories( $location_id, $extra_conditions = '', $order_by = '' ) {
global $wpdb;
$locations_table = EME_DB_PREFIX . EME_LOCATIONS_TBNAME;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
if ( $extra_conditions != '' ) {
$extra_conditions = " AND ($extra_conditions)";
}
if ( $order_by != '' ) {
$order_by = " ORDER BY $order_by";
}
$sql = $wpdb->prepare( "SELECT $categories_table.* FROM $categories_table, $locations_table where location_id = %d AND FIND_IN_SET(category_id,location_category_ids) $extra_conditions $order_by", $location_id );
return $wpdb->get_results( $sql, ARRAY_A );
}
function eme_get_location_category_names( $location_id, $extra_conditions = '', $order_by = '' ) {
global $wpdb;
$locations_table = EME_DB_PREFIX . EME_LOCATIONS_TBNAME;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
if ( $extra_conditions != '' ) {
$extra_conditions = " AND ($extra_conditions)";
}
if ( $order_by != '' ) {
$order_by = " ORDER BY $order_by";
}
$sql = $wpdb->prepare( "SELECT $categories_table.category_name FROM $categories_table, $locations_table WHERE location_id = %d AND FIND_IN_SET(category_id,location_category_ids) $extra_conditions $order_by", $location_id );
return $wpdb->get_col( $sql );
}
function eme_get_location_category_descriptions( $location_id, $extra_conditions = '', $order_by = '' ) {
global $wpdb;
$locations_table = EME_DB_PREFIX . EME_LOCATIONS_TBNAME;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
if ( $extra_conditions != '' ) {
$extra_conditions = " AND ($extra_conditions)";
}
if ( $order_by != '' ) {
$order_by = " ORDER BY $order_by";
}
$sql = $wpdb->prepare( "SELECT $categories_table.description FROM $categories_table, $locations_table WHERE location_id = %d AND FIND_IN_SET(category_id,location_category_ids) $extra_conditions $order_by", $location_id );
return $wpdb->get_col( $sql );
}
function eme_get_category_ids( $cat_slug = '' ) {
global $wpdb;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
$cat_ids = [];
if ( ! empty( $cat_slug ) ) {
$sql = $wpdb->prepare( "SELECT DISTINCT category_id FROM $categories_table WHERE category_slug = %s", $cat_slug );
} else {
$sql = "SELECT category_id FROM $categories_table ORDER BY category_id";
}
$cat_ids = $wpdb->get_col( $sql );
return $cat_ids;
}
function eme_get_category_id_by_name_slug ($cat_name ) {
global $wpdb;
$categories_table = EME_DB_PREFIX . EME_CATEGORIES_TBNAME;
$cat_name = eme_sanitize_request($cat_name);
$sql = $wpdb->prepare( "SELECT category_id FROM $categories_table WHERE category_name = %s OR category_slug = %s LIMIT 1", $cat_name, $cat_name );
return $wpdb->get_var( $sql );
}
function eme_get_categories_shortcode( $atts ) {
eme_enqueue_frontend();
// normalize attribute keys, lowercase
$atts = array_change_key_case( (array) $atts, CASE_LOWER );
$atts = shortcode_atts(
[
'event_id' => 0,
'eventful' => false,
'scope' => 'all',
'template_id' => 0,
'template_id_header' => 0,
'template_id_footer' => 0,
],
$atts
);
$event_id = $atts['event_id'];
$eventful = filter_var( $atts['eventful'], FILTER_VALIDATE_BOOLEAN );
$scope = $atts['scope'];
$template_id = $atts['template_id'];
$template_id_header = $atts['template_id_header'];
$template_id_footer = $atts['template_id_footer'];
if ( $event_id ) {
$categories = eme_get_event_categories( $event_id );
} else {
$categories = eme_get_categories( $eventful, $scope );
}
// Initialize format and templates for header/footer
$format = '';
$eme_format_header = '';
$eme_format_footer = '';
if ( $template_id ) {
$format = eme_get_template_format( $template_id );
}
if ( $template_id_header ) {
$format_header = eme_get_template_format( $template_id_header );
$eme_format_header = eme_replace_categories_placeholders( $format_header );
}
if ( $template_id_footer ) {
$format_footer = eme_get_template_format( $template_id_footer );
$eme_format_footer = eme_replace_categories_placeholders( $format_footer );
}
// Set default format if not defined
if (eme_is_empty_string( $format )) {
$format = '<li class="cat-#_CATEGORYFIELD{category_id}">#_CATEGORYFIELD{category_name}</li>';
}
if (eme_is_empty_string( $eme_format_header )) {
$eme_format_header = '<ul>';
}
if (eme_is_empty_string( $eme_format_footer )) {
$eme_format_footer = '</ul>';
}
// Build output using categories
$output = '';
foreach ($categories as $cat) {
$output .= eme_replace_categories_placeholders( $format, $cat );
}
$output = $eme_format_header . $output . $eme_format_footer;
return $output;
}
function eme_replace_categories_placeholders( $format, $cat = '', $target = 'html', $do_shortcode = 1, $lang = '' ) {
if ( $target == 'htmlmail' || $target == 'html_nohtml2br' ) {
$target = 'html';
}
$needle_offset = 0;
preg_match_all( '/#(ESC|URL)?@?_?[A-Za-z0-9_]+(\{(?>[^{}]+|(?2))*\})*+/', $format, $placeholders, PREG_OFFSET_CAPTURE );
foreach ( $placeholders[0] as $orig_result ) {
$result = $orig_result[0];
$orig_result_needle = $orig_result[1] - $needle_offset;
$orig_result_length = strlen( $orig_result[0] );
$need_escape = 0;
$need_urlencode = 0;
$found = 1;
if ( strstr( $result, '#ESC' ) ) {
$result = str_replace( '#ESC', '#', $result );
$need_escape = 1;
} elseif ( strstr( $result, '#URL' ) ) {
$result = str_replace( '#URL', '#', $result );
$need_urlencode = 1;
}
$replacement = '';
if ( preg_match( '/#_CATEGORYFIELD\{(.+)\}/', $result, $matches ) ) {
$tmp_attkey = $matches[1];
if ( isset( $cat[ $tmp_attkey ] ) && ! is_array( $cat[ $tmp_attkey ] ) ) {
$replacement = $cat[ $tmp_attkey ];
}
} elseif ( preg_match( '/#_CATEGORYURL/', $result ) ) {
$replacement = eme_category_url( $cat );
} else {
$found = 0;
}
if ( $found ) {
if ( $target == 'html' ) {
$replacement = eme_trans_esc_html( $replacement, $lang );
$replacement = apply_filters( 'eme_general', $replacement );
} elseif ( $target == 'rss' ) {
$replacement = eme_translate( $replacement, $lang );
$replacement = apply_filters( 'the_content_rss', $replacement );
} else {
$replacement = eme_translate( $replacement, $lang );
$replacement = apply_filters( 'eme_text', $replacement );
}
if ( $need_escape ) {
$replacement = eme_esc_html( preg_replace( '/\n|\r/', '', $replacement ) );
}
if ( $need_urlencode ) {
$replacement = rawurlencode( $replacement );
}
$format = substr_replace( $format, $replacement, $orig_result_needle, $orig_result_length );
$needle_offset += $orig_result_length - strlen( $replacement );
}
}
// now, replace any language tags found
$format = eme_translate( $format, $lang );
// and now replace any shortcodes, if wanted
if ( $do_shortcode ) {
return do_shortcode( $format );
} else {
return $format;
}
}
?>