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