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

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

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