1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | |
9: | |
10: | namespace EventPost; |
11: | |
12: | $Children = new Children(); |
13: | |
14: | |
15: | |
16: | |
17: | class Children{ |
18: | |
19: | var $POST_TYPE; |
20: | |
21: | function __construct() { |
22: | |
23: | $this->POST_TYPE = 'eventpost'; |
24: | |
25: | |
26: | |
27: | add_action('eventpost_getsettings_action', array(&$this, 'get_settings'), 1, 2); |
28: | add_action('eventpost_settings_form', array(&$this, 'settings_form')); |
29: | add_action('evenpost_init', array(&$this, 'init')); |
30: | add_action('wp_loaded', array(&$this, 'add_post_type')); |
31: | } |
32: | |
33: | |
34: | |
35: | |
36: | function Children() { |
37: | $this->__construct(); |
38: | } |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | |
45: | |
46: | |
47: | |
48: | function init($EP) { |
49: | |
50: | if (!$EP->settings['children_enabled']) { |
51: | return; |
52: | } |
53: | |
54: | add_filter('eventpost_retreive', array(&$this, 'retreive')); |
55: | add_filter('eventpost_get_post_types', array(&$this, 'get_post_types')); |
56: | add_filter('eventpost_contentbar', array(&$this, 'display_single'), 3, 2); |
57: | |
58: | add_action('eventpost_add_custom_box', array(&$this, 'add_custom_box')); |
59: | add_filter('eventpost_add_custom_box_position', array(&$this, 'add_custom_box_position'), 1, 2); |
60: | add_action('admin_notices', array(&$this, 'notice')); |
61: | add_action('save_post', array(&$this, 'save_postdata'), 100); |
62: | add_action('edit_form_top', array(&$this, 'edit_form_top'), 1); |
63: | |
64: | add_action('admin_post_EventPostAddChild', array(&$this, 'add_child_admin_post')); |
65: | add_action('admin_post_EventPostDeleteChild', array(&$this, 'delete_child_admin_post')); |
66: | |
67: | add_action('wp_ajax_EventPostAddChild', array(&$this, 'add_child_ajax')); |
68: | add_action('wp_ajax_EventPostDeleteChild', array(&$this, 'delete_child_ajax')); |
69: | |
70: | add_filter('eventpost_columns_head', array(&$this, 'columns_head')); |
71: | add_action('eventpost_columns_content', array(&$this, 'columns_content'), 10, 2); |
72: | } |
73: | |
74: | |
75: | |
76: | |
77: | |
78: | |
79: | function add_post_type(){ |
80: | register_post_type( |
81: | $this->POST_TYPE, |
82: | array( |
83: | 'label' => __("Event post",'event-post'), |
84: | 'description' => 'Child of event posts', |
85: | 'public' => false, |
86: | 'publicly_queryable'=>true, |
87: | 'show_ui' => true, |
88: | 'show_in_menu' => false, |
89: | 'capability_type' => 'post', |
90: | 'hierarchical' => false, |
91: | 'rewrite' => array('slug' => ''), |
92: | 'taxonomies'=>get_taxonomies(), |
93: | 'query_var' => true, |
94: | 'has_archive' => false, |
95: | 'supports' => array('page_attributes', 'author'), |
96: | 'labels' => array ( |
97: | 'name' => __("Event post",'event-post'), |
98: | 'singular_name' => __("Event post",'event-post'), |
99: | 'menu_name' => __("Event post",'event-post'), |
100: | 'add_new' => __('add','event-post'), |
101: | 'add_new_item' => __('Add event child','event-post'), |
102: | 'edit' => __('Edit','event-post'), |
103: | 'edit_item' => __('Edit event child','event-post'), |
104: | 'new_item' => __('New','event-post'), |
105: | 'view' => __('View','event-post'), |
106: | 'view_item' => __('View event child','event-post'), |
107: | 'search_items' => __('Search event children','event-post'), |
108: | 'not_found' => __('No event child Found','event-post'), |
109: | 'parent' => __(' Event post Parent','event-post'), |
110: | ) |
111: | ) |
112: | ); |
113: | |
114: | } |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | function get_settings(&$ep_settings, &$reg_settings) { |
123: | if (!isset($ep_settings['children_enabled'])) { |
124: | $ep_settings['children_enabled'] = false; |
125: | $reg_settings = true; |
126: | } |
127: | elseif($ep_settings['children_enabled']==true && !in_array($this->POST_TYPE, $ep_settings['posttypes'])){ |
128: | array_push($ep_settings['posttypes'], $this->POST_TYPE); |
129: | } |
130: | if (!isset($ep_settings['children_sync_tax'])) { |
131: | $ep_settings['children_sync_tax'] = false; |
132: | $reg_settings = true; |
133: | } |
134: | } |
135: | |
136: | |
137: | |
138: | |
139: | |
140: | |
141: | |
142: | |
143: | |
144: | function add_custom_box($post_type){ |
145: | global $EventPost; |
146: | if($post_type==$this->POST_TYPE){ |
147: | return; |
148: | } |
149: | add_meta_box('event_post_children', __('Children events', 'event-post'), array(&$this, 'inner_custom_box_children'), $post_type, $EventPost->settings['adminpos'], 'core'); |
150: | } |
151: | |
152: | function add_custom_box_position($position, $post_type){ |
153: | if($post_type==$this->POST_TYPE){ |
154: | $position = 'advanced'; |
155: | } |
156: | return $position; |
157: | } |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | |
164: | |
165: | |
166: | function get_post_types($post_types=array()){ |
167: | unset($post_types[$this->POST_TYPE]); |
168: | return $post_types; |
169: | } |
170: | |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | |
178: | function _delete_child($child_id){ |
179: | return wp_delete_post( $child_id, true ); |
180: | } |
181: | |
182: | |
183: | |
184: | |
185: | |
186: | |
187: | |
188: | |
189: | function _sync_child($child_id){ |
190: | global $EventPost; |
191: | if (!$EventPost->settings['children_sync_tax']) { |
192: | return; |
193: | } |
194: | $child = get_post($child_id); |
195: | $taxonomies = get_object_taxonomies(get_post($child->post_parent)); |
196: | foreach($taxonomies as $taxonomy){ |
197: | wp_set_object_terms($child_id, wp_get_object_terms( $child->post_parent, $taxonomy, array('fields'=>'ids') ), $taxonomy); |
198: | } |
199: | } |
200: | |
201: | |
202: | |
203: | |
204: | |
205: | |
206: | |
207: | |
208: | |
209: | |
210: | function _add_child($post_id){ |
211: | global $EventPost; |
212: | if(!is_numeric($post_id)){ |
213: | return false; |
214: | } |
215: | |
216: | $id = wp_insert_post(array( |
217: | 'post_type'=>$this->POST_TYPE, |
218: | 'post_parent'=>$post_id, |
219: | 'post_status'=>'publish', |
220: | 'post_mime_type'=>'text/calendar', |
221: | )); |
222: | |
223: | if(!is_numeric($id)){ |
224: | return false; |
225: | } |
226: | if ($EventPost->settings['children_sync_tax']) { |
227: | $this->_sync_child($id); |
228: | } |
229: | return $id; |
230: | } |
231: | |
232: | |
233: | |
234: | |
235: | |
236: | |
237: | |
238: | |
239: | |
240: | |
241: | function get($post_id){ |
242: | global $EventPost; |
243: | $param = array( |
244: | 'post_type'=>$this->POST_TYPE, |
245: | 'post_parent'=>$post_id, |
246: | 'post_status'=>array('publish', 'draft'), |
247: | 'posts_per_page'=>-1 |
248: | ); |
249: | |
250: | $children=array(); |
251: | $query = new \WP_Query($param); |
252: | foreach($query->posts as $post){ |
253: | array_push($children, $EventPost->retreive($post) ); |
254: | } |
255: | return $children; |
256: | } |
257: | |
258: | |
259: | |
260: | |
261: | |
262: | |
263: | |
264: | |
265: | |
266: | |
267: | public function save_postdata($post_id) { |
268: | global $EventPost; |
269: | if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ |
270: | return; |
271: | } |
272: | if (!$EventPost->settings['children_sync_tax']) { |
273: | return; |
274: | } |
275: | $children = $this->get($post_id); |
276: | if(count($children)){ |
277: | foreach($children as $child){ |
278: | $this->_sync_child($child->ID); |
279: | } |
280: | } |
281: | } |
282: | |
283: | |
284: | |
285: | |
286: | |
287: | |
288: | |
289: | |
290: | function retreive($event){ |
291: | global $EventPost; |
292: | if($event->post_type == $this->POST_TYPE && $event->post_parent){ |
293: | $parent = $EventPost->retreive($event->post_parent); |
294: | $event->root_ID = $event->post_parent; |
295: | $event->post_title = $parent->post_title; |
296: | $event->permalink = $parent->permalink; |
297: | $event->post_content = $parent->post_content; |
298: | $event->description = $parent->description; |
299: | } |
300: | return $event; |
301: | } |
302: | |
303: | |
304: | |
305: | |
306: | |
307: | |
308: | |
309: | function notice(){ |
310: | if(false === $notice = filter_input(INPUT_GET, 'eventpost_child_notice')){ |
311: | return; |
312: | } |
313: | $notices = array( |
314: | 'add_failed'=>array('warning', __( 'An error occured while creating a child event...', 'event-post' )), |
315: | 'delete_success'=>array('success', __( 'Child event has been deleted', 'event-post' )), |
316: | 'delete_failed'=>array('warning', __( 'An error occured while deleting a child event...', 'event-post' )) |
317: | ); |
318: | |
319: | if(!isset($notices[$notice])){ |
320: | return; |
321: | } |
322: | |
323: | ?> |
324: | <div class="notice-<?php echo $notices[$notice][0]; ?>"><p><?php echo $notices[$notice][1]; ?></p></div> |
325: | <?php |
326: | } |
327: | |
328: | function check_admin_legitimity(){ |
329: | if (!wp_verify_nonce(filter_input(INPUT_GET, 'eventpost_children_nonce'), 'eventpost_children_nonce') ){ |
330: | wp_die(__('Invalid link', 'event-post')); |
331: | } |
332: | } |
333: | |
334: | |
335: | |
336: | |
337: | |
338: | |
339: | function add_child_admin_post(){ |
340: | $this->check_admin_legitimity(); |
341: | |
342: | if(false === $post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT)){ |
343: | wp_die(__('No post ID given...', 'event-post')); |
344: | } |
345: | |
346: | if(false !== $child_id = $this->_add_child($post_id)){ |
347: | wp_redirect(admin_url('post.php?post='.$child_id.'&action=edit&post_type=eventpost')); |
348: | } |
349: | else{ |
350: | wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&eventpost_child_notice=add_failed')); |
351: | } |
352: | exit; |
353: | } |
354: | function add_child_ajax(){ |
355: | if(false === $post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT)){ |
356: | wp_die(__('No post ID given...', 'event-post')); |
357: | } |
358: | |
359: | if(false !== $child_id = $this->_add_child($post_id)){ |
360: | wp_send_json(array( |
361: | 'success'=>true, |
362: | 'child_id'=>$child_id, |
363: | 'edit_url'=>admin_url('post.php?post='.$child_id.'&action=edit') |
364: | )); |
365: | } |
366: | else{ |
367: | wp_send_json(array( |
368: | 'success'=>false |
369: | )); |
370: | } |
371: | exit; |
372: | } |
373: | |
374: | function delete_child_admin_post(){ |
375: | $this->check_admin_legitimity(); |
376: | if(false === $post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT)){ |
377: | wp_die(__('No post ID given...', 'event-post')); |
378: | } |
379: | if(false === $child_id = filter_input(INPUT_GET, 'child_id', FILTER_SANITIZE_NUMBER_INT)){ |
380: | wp_die(__('No child ID given...', 'event-post')); |
381: | } |
382: | |
383: | if(false !== $this->_delete_child($child_id)){ |
384: | wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&eventpost_child_notice=delete_success')); |
385: | } |
386: | else{ |
387: | wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&eventpost_child_notice=delete_failed')); |
388: | } |
389: | exit; |
390: | } |
391: | |
392: | |
393: | |
394: | |
395: | |
396: | |
397: | function settings_form($ep_settings) { |
398: | ?> |
399: | <h2><?php _e('Multi dates', 'event-post'); ?></h2> |
400: | <table class="form-table" id="eventpost-settings-table-children"> |
401: | <tbody> |
402: | <tr> |
403: | <th> |
404: | <?php _e('Enable children events', 'event-post') ?> |
405: | </th> |
406: | <td> |
407: | <label for="children_enabled"> |
408: | <input type="checkbox" name="ep_settings[children_enabled]" id="children_enabled" <?php if ($ep_settings['children_enabled'] == '1') { |
409: | echo'checked'; |
410: | } ?> value="1"> |
411: | <?php _e('Allow event post to create children to events', 'event-post') ?> |
412: | </label> |
413: | </td> |
414: | </tr> |
415: | <tr> |
416: | <th> |
417: | <?php _e('Synchronize taxonomies', 'event-post') ?> |
418: | </th> |
419: | <td> |
420: | <label for="children_sync_tax"> |
421: | <input type="checkbox" name="ep_settings[children_sync_tax]" id="children_sync_tax" <?php if ($ep_settings['children_sync_tax'] == '1') { |
422: | echo'checked'; |
423: | } ?> value="1"> |
424: | <?php _e('Make children herit all taxonomies (categories, tags, custom taxonomies...) from the original event.', 'event-post') ?> |
425: | </label> |
426: | </td> |
427: | </tr> |
428: | |
429: | </tbody> |
430: | </table><!-- #eventpost-settings-table-children --> |
431: | <?php |
432: | } |
433: | |
434: | |
435: | |
436: | |
437: | |
438: | |
439: | function edit_form_top($post){ |
440: | if($post->post_type==$this->POST_TYPE){ |
441: | $parent = get_post($post->post_parent); |
442: | ?> |
443: | <a href="<?php echo admin_url('post.php?post='.$parent->ID.'&action=edit'); ?>" class="button button-default"> |
444: | <i class="dashicons dashicons-arrow-left-alt"></i> |
445: | <?php ?> |
446: | <strong><?php printf(__('Back to %s', 'event-post'), esc_attr($parent->post_title)); ?></strong> |
447: | </a> |
448: | <?php |
449: | } |
450: | } |
451: | |
452: | |
453: | |
454: | |
455: | |
456: | |
457: | function inner_custom_box_children() { |
458: | global $EventPost; |
459: | wp_nonce_field('eventpost_children_nonce', 'eventpost_children_nonce'); |
460: | $post_id = get_the_ID(); |
461: | $children = $this->get($post_id); |
462: | ?> |
463: | <ul id="eventpost-children-list"> |
464: | <?php foreach ($children as $child): ?> |
465: | <li> |
466: | <?php echo $EventPost->get_singledate($child); ?> |
467: | <?php echo $EventPost->get_singleloc($child); ?> |
468: | <a class="button button-default eventpost-children-edit" href="<?php echo admin_url('post.php?post='.$child->ID.'&action=edit'); ?>" title="<?php _e('Edit child event', 'event-post'); ?>"> |
469: | <i class="dashicons dashicons-edit"></i> |
470: | </a> |
471: | <a class="button button-default eventpost-children-delete" href="<?php echo wp_nonce_url(admin_url('admin-post.php?action=EventPostDeleteChild&post_id='.$post_id.'&child_id='.$child->ID), 'eventpost_children_nonce', 'eventpost_children_nonce'); ?>" title="<?php _e('Delete child event', 'event-post'); ?>"> |
472: | <i class="dashicons dashicons-trash"></i> |
473: | </a> |
474: | </li> |
475: | <?php endforeach; ?> |
476: | </ul> |
477: | <a class="button button-default eventpost-children-add" href="<?php echo wp_nonce_url(admin_url('admin-post.php?action=EventPostAddChild&post_id='.$post_id), 'eventpost_children_nonce', 'eventpost_children_nonce'); ?>"> |
478: | <i class="dashicons dashicons-plus"></i> |
479: | <?php _e('Add child event', 'event-post'); ?> |
480: | </a> |
481: | <?php |
482: | } |
483: | |
484: | |
485: | |
486: | |
487: | |
488: | |
489: | |
490: | |
491: | public function columns_head($defaults) { |
492: | $defaults['children_events'] = __('Children events', 'event-post'); |
493: | return $defaults; |
494: | } |
495: | |
496: | |
497: | |
498: | |
499: | |
500: | |
501: | |
502: | public function columns_content($column_name, $post_id) { |
503: | if ($column_name == 'children_events') { |
504: | $nb = count($this->get($post_id)); |
505: | echo $nb ? '<p align="center">'.$nb.'</p>' : ''; |
506: | } |
507: | } |
508: | |
509: | |
510: | |
511: | |
512: | function display_single($eventbar, $event){ |
513: | remove_filter('eventpost_contentbar', array(&$this, 'display_single'), 3, 2); |
514: | global $EventPost; |
515: | $children = $this->get($event->ID); |
516: | if(0 !== $count = count($children)){ |
517: | |
518: | $eventbar.='<p class="eventpost-children-text-more">'.sprintf(_n('%d other date:', '%d other dates:', $count, 'event-post'), $count).'</p>'; |
519: | foreach($children as $child){ |
520: | $eventbar.=$EventPost->get_single($child, 'event_single', 'single'); |
521: | } |
522: | } |
523: | add_filter('eventpost_contentbar', array(&$this, 'display_single'), 3, 2); |
524: | return $eventbar; |
525: | } |
526: | |
527: | |
528: | } |
529: | |