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:
<?php
namespace EventPost;
$Children = new Children();
class Children{
var $POST_TYPE;
function __construct() {
$this->POST_TYPE = 'eventpost';
add_action('eventpost_getsettings_action', array(&$this, 'get_settings'), 1, 2);
add_action('eventpost_settings_form', array(&$this, 'settings_form'));
add_action('evenpost_init', array(&$this, 'init'));
add_action('wp_loaded', array(&$this, 'add_post_type'));
}
function Children() {
$this->__construct();
}
function init($EP) {
if (!$EP->settings['children_enabled']) {
return;
}
add_filter('eventpost_retreive', array(&$this, 'retreive'));
add_filter('eventpost_get_post_types', array(&$this, 'get_post_types'));
add_filter('eventpost_contentbar', array(&$this, 'display_single'), 3, 2);
add_action('eventpost_add_custom_box', array(&$this, 'add_custom_box'));
add_filter('eventpost_add_custom_box_position', array(&$this, 'add_custom_box_position'), 1, 2);
add_action('admin_notices', array(&$this, 'notice'));
add_action('save_post', array(&$this, 'save_postdata'), 100);
add_action('edit_form_top', array(&$this, 'edit_form_top'), 1);
add_action('admin_post_EventPostAddChild', array(&$this, 'add_child_admin_post'));
add_action('admin_post_EventPostDeleteChild', array(&$this, 'delete_child_admin_post'));
add_action('wp_ajax_EventPostAddChild', array(&$this, 'add_child_ajax'));
add_action('wp_ajax_EventPostDeleteChild', array(&$this, 'delete_child_ajax'));
add_filter('eventpost_columns_head', array(&$this, 'columns_head'));
add_action('eventpost_columns_content', array(&$this, 'columns_content'), 10, 2);
}
function add_post_type(){
register_post_type(
$this->POST_TYPE,
array(
'label' => __("Event post",'event-post'),
'description' => 'Child of event posts',
'public' => false,
'publicly_queryable'=>true,
'show_ui' => true,
'show_in_menu' => false,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => ''),
'taxonomies'=>get_taxonomies(),
'query_var' => true,
'has_archive' => false,
'supports' => array('page_attributes', 'author'),
'labels' => array (
'name' => __("Event post",'event-post'),
'singular_name' => __("Event post",'event-post'),
'menu_name' => __("Event post",'event-post'),
'add_new' => __('add','event-post'),
'add_new_item' => __('Add event child','event-post'),
'edit' => __('Edit','event-post'),
'edit_item' => __('Edit event child','event-post'),
'new_item' => __('New','event-post'),
'view' => __('View','event-post'),
'view_item' => __('View event child','event-post'),
'search_items' => __('Search event children','event-post'),
'not_found' => __('No event child Found','event-post'),
'parent' => __(' Event post Parent','event-post'),
)
)
);
}
function get_settings(&$ep_settings, &$reg_settings) {
if (!isset($ep_settings['children_enabled'])) {
$ep_settings['children_enabled'] = false;
$reg_settings = true;
}
elseif($ep_settings['children_enabled']==true && !in_array($this->POST_TYPE, $ep_settings['posttypes'])){
array_push($ep_settings['posttypes'], $this->POST_TYPE);
}
if (!isset($ep_settings['children_sync_tax'])) {
$ep_settings['children_sync_tax'] = false;
$reg_settings = true;
}
}
function add_custom_box($post_type){
global $EventPost;
if($post_type==$this->POST_TYPE){
return;
}
add_meta_box('event_post_children', __('Children events', 'event-post'), array(&$this, 'inner_custom_box_children'), $post_type, $EventPost->settings['adminpos'], 'core');
}
function add_custom_box_position($position, $post_type){
if($post_type==$this->POST_TYPE){
$position = 'advanced';
}
return $position;
}
function get_post_types($post_types=array()){
unset($post_types[$this->POST_TYPE]);
return $post_types;
}
function _delete_child($child_id){
return wp_delete_post( $child_id, true );
}
function _sync_child($child_id){
global $EventPost;
if (!$EventPost->settings['children_sync_tax']) {
return;
}
$child = get_post($child_id);
$taxonomies = get_object_taxonomies(get_post($child->post_parent));
foreach($taxonomies as $taxonomy){
wp_set_object_terms($child_id, wp_get_object_terms( $child->post_parent, $taxonomy, array('fields'=>'ids') ), $taxonomy);
}
}
function _add_child($post_id){
global $EventPost;
if(!is_numeric($post_id)){
return false;
}
$id = wp_insert_post(array(
'post_type'=>$this->POST_TYPE,
'post_parent'=>$post_id,
'post_status'=>'publish',
'post_mime_type'=>'text/calendar',
));
if(!is_numeric($id)){
return false;
}
if ($EventPost->settings['children_sync_tax']) {
$this->_sync_child($id);
}
return $id;
}
function get($post_id){
global $EventPost;
$param = array(
'post_type'=>$this->POST_TYPE,
'post_parent'=>$post_id,
'post_status'=>array('publish', 'draft'),
'posts_per_page'=>-1
);
$children=array();
$query = new \WP_Query($param);
foreach($query->posts as $post){
array_push($children, $EventPost->retreive($post) );
}
return $children;
}
public function save_postdata($post_id) {
global $EventPost;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){
return;
}
if (!$EventPost->settings['children_sync_tax']) {
return;
}
$children = $this->get($post_id);
if(count($children)){
foreach($children as $child){
$this->_sync_child($child->ID);
}
}
}
function retreive($event){
global $EventPost;
if($event->post_type == $this->POST_TYPE && $event->post_parent){
$parent = $EventPost->retreive($event->post_parent);
$event->root_ID = $event->post_parent;
$event->post_title = $parent->post_title;
$event->permalink = $parent->permalink;
$event->post_content = $parent->post_content;
$event->description = $parent->description;
}
return $event;
}
function notice(){
if(false === $notice = filter_input(INPUT_GET, 'eventpost_child_notice')){
return;
}
$notices = array(
'add_failed'=>array('warning', __( 'An error occured while creating a child event...', 'event-post' )),
'delete_success'=>array('success', __( 'Child event has been deleted', 'event-post' )),
'delete_failed'=>array('warning', __( 'An error occured while deleting a child event...', 'event-post' ))
);
if(!isset($notices[$notice])){
return;
}
?>
<div class="notice-<?php echo $notices[$notice][0]; ?>"><p><?php echo $notices[$notice][1]; ?></p></div>
<?php
}
function check_admin_legitimity(){
if (!wp_verify_nonce(filter_input(INPUT_GET, 'eventpost_children_nonce', FILTER_SANITIZE_STRING), 'eventpost_children_nonce') ){
wp_die(__('Invalid link', 'event-post'));
}
}
function add_child_admin_post(){
$this->check_admin_legitimity();
if(false === $post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT)){
wp_die(__('No post ID given...', 'event-post'));
}
if(false !== $child_id = $this->_add_child($post_id)){
wp_redirect(admin_url('post.php?post='.$child_id.'&action=edit&post_type=eventpost'));
}
else{
wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&eventpost_child_notice=add_failed'));
}
exit;
}
function add_child_ajax(){
if(false === $post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT)){
wp_die(__('No post ID given...', 'event-post'));
}
if(false !== $child_id = $this->_add_child($post_id)){
wp_send_json(array(
'success'=>true,
'child_id'=>$child_id,
'edit_url'=>admin_url('post.php?post='.$child_id.'&action=edit')
));
}
else{
wp_send_json(array(
'success'=>false
));
}
exit;
}
function delete_child_admin_post(){
$this->check_admin_legitimity();
if(false === $post_id = filter_input(INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT)){
wp_die(__('No post ID given...', 'event-post'));
}
if(false === $child_id = filter_input(INPUT_GET, 'child_id', FILTER_SANITIZE_NUMBER_INT)){
wp_die(__('No child ID given...', 'event-post'));
}
if(false !== $this->_delete_child($child_id)){
wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&eventpost_child_notice=delete_success'));
}
else{
wp_redirect(admin_url('post.php?post='.$post_id.'&action=edit&eventpost_child_notice=delete_failed'));
}
exit;
}
function settings_form($ep_settings) {
?>
<h2><?php _e('Multi dates', 'event-post'); ?></h2>
<table class="form-table" id="eventpost-settings-table-children">
<tbody>
<tr>
<th>
<?php _e('Enable children events', 'event-post') ?>
</th>
<td>
<label for="children_enabled">
<input type="checkbox" name="ep_settings[children_enabled]" id="children_enabled" <?php if ($ep_settings['children_enabled'] == '1') {
echo'checked';
} ?> value="1">
<?php _e('Allow event post to create children to events', 'event-post') ?>
</label>
</td>
</tr>
<tr>
<th>
<?php _e('Synchronize taxonomies', 'event-post') ?>
</th>
<td>
<label for="children_sync_tax">
<input type="checkbox" name="ep_settings[children_sync_tax]" id="children_sync_tax" <?php if ($ep_settings['children_sync_tax'] == '1') {
echo'checked';
} ?> value="1">
<?php _e('Make children herit all taxonomies (categories, tags, custom taxonomies...) from the original event.', 'event-post') ?>
</label>
</td>
</tr>
</tbody>
</table><!--
<?php
}
function edit_form_top($post){
if($post->post_type==$this->POST_TYPE){
$parent = get_post($post->post_parent);
?>
<a href="<?php echo admin_url('post.php?post='.$parent->ID.'&action=edit'); ?>" class="button button-default">
<i class="dashicons dashicons-arrow-left-alt"></i>
<?php
<strong><?php printf(__('Back to %s', 'event-post'), esc_attr($parent->post_title)); ?></strong>
</a>
<?php
}
}
function inner_custom_box_children() {
global $EventPost;
wp_nonce_field('eventpost_children_nonce', 'eventpost_children_nonce');
$post_id = get_the_ID();
$children = $this->get($post_id);
?>
<ul id="eventpost-children-list">
<?php foreach ($children as $child): ?>
<li>
<?php echo $EventPost->get_singledate($child); ?>
<?php echo $EventPost->get_singleloc($child); ?>
<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'); ?>">
<i class="dashicons dashicons-edit"></i>
</a>
<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'); ?>">
<i class="dashicons dashicons-trash"></i>
</a>
</li>
<?php endforeach; ?>
</ul>
<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'); ?>">
<i class="dashicons dashicons-plus"></i>
<?php _e('Add child event', 'event-post'); ?>
</a>
<?php
}
public function columns_head($defaults) {
$defaults['children_events'] = __('Children events', 'event-post');
return $defaults;
}
public function columns_content($column_name, $post_id) {
if ($column_name == 'children_events') {
$nb = count($this->get($post_id));
echo $nb ? '<p align="center">'.$nb.'</p>' : '';
}
}
function display_single($eventbar, $event){
remove_filter('eventpost_contentbar', array(&$this, 'display_single'), 3, 2);
global $EventPost;
$children = $this->get($event->ID);
if(0 !== $count = count($children)){
$eventbar.='<p class="eventpost-children-text-more">'.sprintf(_n('%d other date:', '%d other dates:', $count, 'event-post'), $count).'</p>';
foreach($children as $child){
$eventbar.=$EventPost->get_single($child, 'event_single', 'single');
}
}
add_filter('eventpost_contentbar', array(&$this, 'display_single'), 3, 2);
return $eventbar;
}
}