|
7 | 7 | return; |
8 | 8 | } |
9 | 9 |
|
10 | | -$em_pb_removed = false; |
11 | | - |
12 | | -/** |
13 | | - * Disable Page Builder for Events Manager post types. |
14 | | - * |
15 | | - * This function checks if the current post is an Events Manager post type |
16 | | - * and if Page Builder is enabled for it. If both conditions are met, it |
17 | | - * disables Page Builder for the content. This is done to prevent Page Builder |
18 | | - * from interfering with the Events Manager content, and vice versa. |
19 | | - * |
20 | | - * `loop_start` is used due to when the Events Manager plugin sets up its |
21 | | - * content replacement. |
22 | | - * |
23 | | - * @return void |
24 | | - */ |
25 | | -function siteorigin_panels_event_manager_loop_start() { |
26 | | - $em_post_types = array( 'event-recurring', 'event' ); |
27 | | - |
28 | | - // Is the current post an $em_post_types post? |
29 | | - $post_type = get_post_type(); |
30 | | - if ( ! in_array( $post_type, $em_post_types ) ) { |
31 | | - return; |
| 10 | +class SiteOrigin_Panels_Compat_Events_Manager { |
| 11 | + private $is_pb_removed = false; |
| 12 | + private $is_duplicating = false; |
| 13 | + |
| 14 | + public static function single() { |
| 15 | + static $single; |
| 16 | + |
| 17 | + return empty( $single ) ? $single = new self() : $single; |
32 | 18 | } |
33 | 19 |
|
34 | | - // Is Page Builder enabled for Events Manager post types? |
35 | | - $pb_post_types = siteorigin_panels_setting( 'post-types' ); |
36 | | - if ( empty( $pb_post_types ) || ! array_intersect( $em_post_types, $pb_post_types ) ) { |
37 | | - return; |
| 20 | + public function __construct() { |
| 21 | + add_action( 'loop_start', array( $this, 'loop_start' ) ); |
| 22 | + add_action( 'loop_end', array( $this, 'loop_end' ) ); |
| 23 | + |
| 24 | + add_action( 'em_event_duplicate_pre', array( $this, 'duplicate_pre' ) ); |
| 25 | + add_filter( 'em_event_get_event_meta', array( $this, 'filter_duplicate_meta' ) ); |
| 26 | + add_filter( 'em_event_duplicate', array( $this, 'duplicate_copy_panels_data' ), 10, 2 ); |
38 | 27 | } |
39 | 28 |
|
40 | | - global $em_pb_removed; |
41 | | - $em_pb_removed = true; |
| 29 | + /** |
| 30 | + * Disable Page Builder for Events Manager post types. |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function loop_start() { |
| 35 | + $em_post_types = array( 'event-recurring', 'event' ); |
42 | 36 |
|
43 | | - add_filter( 'siteorigin_panels_filter_content_enabled', '__return_false' ); |
44 | | -} |
45 | | -add_action( 'loop_start', 'siteorigin_panels_event_manager_loop_start' ); |
| 37 | + $post_type = get_post_type(); |
| 38 | + if ( ! in_array( $post_type, $em_post_types ) ) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + $pb_post_types = siteorigin_panels_setting( 'post-types' ); |
| 43 | + if ( empty( $pb_post_types ) || ! array_intersect( $em_post_types, $pb_post_types ) ) { |
| 44 | + return; |
| 45 | + } |
46 | 46 |
|
47 | | -/** |
48 | | - * Re-enable Page Builder for `the_content` filter if it |
49 | | - * was disabled at the start of the loop. |
50 | | - */ |
51 | | -function siteorigin_panels_event_manager_loop_end() { |
52 | | - global $em_pb_removed; |
| 47 | + $this->is_pb_removed = true; |
| 48 | + add_filter( 'siteorigin_panels_filter_content_enabled', '__return_false' ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Re-enable Page Builder for `the_content` filter if it |
| 53 | + * was disabled at the start of the loop. |
| 54 | + * |
| 55 | + * @return void |
| 56 | + */ |
| 57 | + public function loop_end() { |
| 58 | + if ( $this->is_pb_removed ) { |
| 59 | + remove_filter( 'siteorigin_panels_filter_content_enabled', '__return_false' ); |
| 60 | + $this->is_pb_removed = false; |
| 61 | + } |
| 62 | + } |
53 | 63 |
|
54 | | - if ( $em_pb_removed ) { |
55 | | - remove_filter( 'siteorigin_panels_filter_content_enabled', '__return_false' ); |
| 64 | + /** |
| 65 | + * Flag Events Manager duplication so we can avoid unsafe SQL inserts for panels_data. |
| 66 | + * |
| 67 | + * @return void |
| 68 | + */ |
| 69 | + public function duplicate_pre() { |
| 70 | + $this->is_duplicating = true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Remove Page Builder data from Events Manager's raw SQL duplication payload. |
| 75 | + * |
| 76 | + * @param array $event_meta Event post meta. |
| 77 | + * |
| 78 | + * @return array |
| 79 | + */ |
| 80 | + public function filter_duplicate_meta( $event_meta ) { |
| 81 | + if ( ! $this->is_duplicating || ! is_array( $event_meta ) ) { |
| 82 | + return $event_meta; |
| 83 | + } |
| 84 | + |
| 85 | + unset( $event_meta['panels_data'] ); |
| 86 | + |
| 87 | + return $event_meta; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Copy Page Builder data to the duplicated event using safe WordPress APIs. |
| 92 | + * |
| 93 | + * @param mixed $duplicated_event The duplicated event object, or false on failure. |
| 94 | + * @param mixed $source_event The original source event object. |
| 95 | + * |
| 96 | + * @return mixed |
| 97 | + */ |
| 98 | + public function duplicate_copy_panels_data( $duplicated_event, $source_event ) { |
| 99 | + $this->is_duplicating = false; |
| 100 | + |
| 101 | + if ( |
| 102 | + empty( $duplicated_event ) || |
| 103 | + ! is_object( $duplicated_event ) || |
| 104 | + ! is_object( $source_event ) || |
| 105 | + empty( $duplicated_event->post_id ) || |
| 106 | + empty( $source_event->post_id ) |
| 107 | + ) { |
| 108 | + return $duplicated_event; |
| 109 | + } |
| 110 | + |
| 111 | + $source_panels_data = get_post_meta( (int) $source_event->post_id, 'panels_data', true ); |
| 112 | + if ( empty( $source_panels_data ) ) { |
| 113 | + return $duplicated_event; |
| 114 | + } |
| 115 | + |
| 116 | + $source_panels_data = map_deep( $source_panels_data, array( 'SiteOrigin_Panels_Admin', 'double_slash_string' ) ); |
| 117 | + update_post_meta( (int) $duplicated_event->post_id, 'panels_data', $source_panels_data ); |
| 118 | + |
| 119 | + return $duplicated_event; |
56 | 120 | } |
57 | 121 | } |
58 | | -add_action( 'loop_end', 'siteorigin_panels_event_manager_loop_end' ); |
| 122 | + |
| 123 | +SiteOrigin_Panels_Compat_Events_Manager::single(); |
0 commit comments