Created
December 2, 2018 14:29
-
-
Save wpmudev-sls/3c0362b3646f3061192add8278e3786f to your computer and use it in GitHub Desktop.
[Events +] - Multiple Events in MarketPress product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Events +] - Multiple Events in MarketPress product | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Allows to rsvp to multiple Events after purchasing a MarketPress product. Requires MarketPress. | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Events_Bundle' ) ) { | |
class WPMUDEV_Events_Bundle { | |
private static $_instance = null; | |
private static $bundle = 10; | |
private static $event_type = 'incsub_event'; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Events_Bundle(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) ); | |
add_action( 'save_post', array( $this, 'product_save' ) ); | |
add_action( 'admin_head', array( $this, 'css' ) ); | |
add_action( 'mp_order_order_paid', array( $this, 'order_paid' ), 20 ); | |
} | |
public function add_meta_box( $post_type ) { | |
if ( ! class_exists( 'Marketpress' ) || MP_Product::get_post_type() != $post_type ) { | |
return; | |
} | |
add_meta_box( | |
'mp_product_events_list', | |
'Events list in bundle', | |
array( $this, 'render_meta_box_content' ), | |
$post_type, | |
'advanced', | |
'high' | |
); | |
} | |
public function render_meta_box_content( $post ) { | |
$event_ids = get_post_meta( $post->ID, 'mp-event-bundle-items', true ); | |
if ( ! $event_ids ) { | |
$event_ids = array(); | |
} | |
wp_nonce_field( 'mp_eab_events_bundle', 'mp_eab_events_bundle_nonce' ); | |
$events_list = $this->events_list( $event_ids ); | |
?> | |
<h3>Choose which events you want to include when this product is purchased</h3> | |
<?php | |
echo $events_list; | |
} | |
public function events_list( $event_ids= array()) { | |
$events = $this->get_events(); | |
if ( ! $events || empty( $events ) ) { | |
return "<div><strong>There are no Events available</strong></div>"; | |
} | |
$html = "<ul class='mp-events-list'>"; | |
foreach ( $events as $key => $event ) { | |
$checked = checked( in_array( $event->ID, $event_ids ) ); | |
$html .= " <li> | |
<label> | |
<input type='checkbox' name='mp-event-items[]' value='{$event->ID}' {$checked} /> | |
{$event->post_title} | |
</label> | |
</li>"; | |
} | |
$html .= "</ul>"; | |
return $html; | |
} | |
public function get_events( $args = array() ) { | |
$default_args = array( | |
'post_type' => self::$event_type, | |
'posts_per_page' => -1, | |
'meta_query' => array( | |
array( | |
'key' => 'incsub_event_status', | |
'value' => 'open', | |
'compare' => '=' | |
) | |
) | |
); | |
$args = wp_parse_args( $args, $default_args ); | |
return get_posts( $args ); | |
} | |
public function product_save( $post_id ) { | |
if ( wp_is_post_revision( $post_id ) || ! isset( $_POST['mp_eab_events_bundle_nonce'] ) ) { | |
return $post_id; | |
} | |
$nonce = $_POST['mp_eab_events_bundle_nonce']; | |
if ( ! wp_verify_nonce( $nonce, 'mp_eab_events_bundle' ) ) { | |
return $post_id; | |
} | |
if ( isset( $_POST['mp-event-items'] ) ) { | |
$event_ids = $_POST['mp-event-items']; | |
if ( is_array( $event_ids ) ) { | |
if ( ! empty( $event_ids ) ){ | |
$event_ids = array_map( function( $item ){ | |
return (int) $item; | |
}, | |
$event_ids ); | |
} | |
} | |
else { | |
$event_ids = (int) $event_ids; | |
} | |
update_post_meta( $post_id, 'mp-event-bundle-items', $event_ids ); | |
} | |
} | |
public function order_paid( $order ) { | |
$order_items = $order->get_meta( 'mp_cart_items' ); | |
$user_id = $order->post_author; | |
$events_hub = new Eab_EventsHub; | |
foreach ( $order_items as $product_id => $items ){ | |
$product_events = get_post_meta( $product_id, 'mp-event-bundle-items', true ); | |
if ( empty( $product_events ) ) { | |
continue; | |
} | |
foreach ( $product_events as $event_id ) { | |
$event = new Eab_EventModel( $event_id ); | |
if ( $event->is_recurring() ) { | |
$event_id = $this->fetch_recurring_event_id( $event ); | |
} | |
if ( ! $event_id ) { | |
return; | |
} | |
$events_hub->update_rsvp_per_event( $event_id, $user_id, 'yes' ); | |
do_action( 'incsub_event_booking_yes', $event_id, $user_id ); | |
$events_hub->recount_bookings( $event_id ); | |
} | |
} | |
} | |
public function fetch_recurring_event_id( $event ) { | |
if ( ! $event instanceof Eab_EventModel ) { | |
return false; | |
} | |
if ( ! $event->is_recurring() ) { | |
return $event_id; | |
} | |
$recurring_dates = array(); | |
$now = time(); | |
$children = Eab_CollectionFactory::get_all_recurring_children_events( $event ); | |
if ( ! $children || ! ( $children[0] ) instanceof Eab_EventModel) { | |
return false; | |
} | |
foreach( $children as $key => $child ) { | |
$child_id = $child->get_id(); | |
$start = $child->get_start_timestamp(); | |
if ( $start < $now ) { | |
continue; | |
} | |
$recurring_dates[ $child_id ] = ( $start - $now ); | |
} | |
if ( empty( $recurring_dates ) ) { | |
return false; | |
} | |
$event_id = array_keys( $recurring_dates, min( $recurring_dates ) )[0]; | |
return $event_id; | |
} | |
public function css() { | |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; | |
if ( ! $screen || 'post' != $screen->base || MP_Product::get_post_type() != $screen->post_type ) { | |
return; | |
} | |
?> | |
<style type="text/css"> | |
#mp_product_events_list ul.mp-events-list { | |
-moz-column-count: 3; | |
-moz-column-gap: 20px; | |
-webkit-column-count: 3; | |
-webkit-column-gap: 20px; | |
column-count: 3; | |
column-gap: 20px; | |
} | |
</style> | |
<?php | |
} | |
} | |
if ( ! function_exists( 'wpmudev_events_bundle' ) ) { | |
function wpmudev_events_bundle(){ | |
return WPMUDEV_Events_Bundle::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_events_bundle', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment