PluginProbe
Event Post / 5.11.0
Event Post v5.11.0
6.1.1 6.1.0 6.0.1 6.0.0 5.12.0 trunk 3.9 4.0 4.2 4.3 4.4 4.5 5.0 5.1 5.10.0 5.10.1 5.10.2 5.10.3 5.10.4 5.11.0 5.11.1 5.2 5.5 5.6 5.6.1 All 46 releases
event-post / inc / class-children.php

class-children.php in Event Post 5.11.0, at inc/class-children.php

529 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Manage sub events
4 *
5 * @package event-post
6 * @version 5.11.0
7 * @since 4.3
8 */
9
10 namespace EventPost;
11
12 $Children = new Children();
13
14 /**
15 * Manage child events
16 */
17 class Children{
18
19 var $POST_TYPE;
20
21 function __construct() {
22
23 $this->POST_TYPE = 'eventpost';
24
25 // Hook into the plugin
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 * PHP4 constructor
35 */
36 function Children() {
37 $this->__construct();
38 }
39
40
41 /**
42 * Initialize
43 *
44 * @param object $EP
45 *
46 * @return void
47 */
48 function init($EP) {
49 // Ensure EventPost\Children is required.
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 * Registers custom post-type
76 *
77 * @return void
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 * Alter settings from eventpost_getsettings_action filter
118 *
119 * @param array reference &$ep_settings
120 * @param boolean reference &$reg_settings
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 * Add custom box
139 *
140 * @param string $post_type
141 *
142 * @global \EventPost $EventPost
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 * Remove self post type form global list
161 *
162 * @param array $post_types
163 *
164 * @return array
165 */
166 function get_post_types($post_types=array()){
167 unset($post_types[$this->POST_TYPE]);
168 return $post_types;
169 }
170
171 /**
172 * Delete a child
173 *
174 * @param int $child_id
175 *
176 * @return bool
177 */
178 function _delete_child($child_id){
179 return wp_delete_post( $child_id, true );
180 }
181
182 /**
183 * Syncs a child
184 *
185 * @param int $child_id
186 *
187 * @global $EventPost
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 * Add a child
203 *
204 * @param int $post_id
205 *
206 * @global type $EventPost
207 *
208 * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure.
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 * Get a child
234 *
235 * @param int $post_id
236 *
237 * @global \EventPost $EventPost
238 *
239 * @return array
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 * Saves a child
260 *
261 * @param int $post_id
262 *
263 * @global type $EventPost
264 *
265 * @return void
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 * Retreive a child's details
285 *
286 * @param \WP_Post $event
287 *
288 * @return object
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 /* -------------------- VIEWS ----------------------- */
305
306 /**
307 * Shows notices
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 esc_attr($notices[$notice][0]); ?>"><p><?php echo wp_kses_post($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(esc_html__('Invalid link', 'event-post'));
331 }
332 }
333
334 /**
335 * Triggers child add on form submission
336 *
337 * @return void
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(esc_html__('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(esc_html__('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(esc_html__('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 * Settings form
394 *
395 * @param type $ep_settings
396 */
397 function settings_form($ep_settings) {
398 ?>
399 <h2><?php esc_html_e('Multi dates', 'event-post'); ?></h2>
400 <table class="form-table" id="eventpost-settings-table-children">
401 <tbody>
402 <tr>
403 <th>
404 <?php esc_html_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 esc_attr('checked');
410 } ?> value="1">
411 <?php esc_html_e('Allow event post to create children to events', 'event-post') ?>
412 </label>
413 </td>
414 </tr>
415 <tr>
416 <th>
417 <?php esc_html_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 esc_attr('checked');
423 } ?> value="1">
424 <?php esc_html_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 * Edit form
436 *
437 * @param type $post
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 // translators: %s represents the parent post title ?>
446 <strong><?php printf(esc_html__('Back to %s', 'event-post'), esc_html($parent->post_title)); ?></strong>
447 </a>
448 <?php
449 }
450 }
451
452 /**
453 * Displays the children custom box
454 *
455 * @global \EventPost $EventPost
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 wp_kses($EventPost->get_singledate($child), EventPost()->kses_tags); ?>
467 <?php echo wp_kses($EventPost->get_singleloc($child), EventPost()->kses_tags); ?>
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 esc_html_e('Add child event', 'event-post'); ?>
480 </a>
481 <?php
482 }
483
484 /**
485 * Alters columns
486 *
487 * @param array $defaults
488 *
489 * @return array
490 */
491 public function columns_head($defaults) {
492 $defaults['children_events'] = __('Children events', 'event-post');
493 return $defaults;
494 }
495
496 /**
497 * Echoes content of a row in a given column
498 *
499 * @param string $column_name
500 * @param int $post_id
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">'.esc_html($nb).'</p>' : '';
506 }
507 }
508
509
510 /* -------------------------------------- FRONT -------------------------------*/
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 // translators: %s other dates
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