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