PluginProbe
Event Post / 5.6.1
Event Post v5.6.1
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 / children.php

children.php in Event Post 5.6.1, at inc/children.php

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