Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save panoslyrakis/ac51c1c0668801d479426c6521765595 to your computer and use it in GitHub Desktop.

Select an option

Save panoslyrakis/ac51c1c0668801d479426c6521765595 to your computer and use it in GitHub Desktop.
Autoconfirm appointments paid with MarketPress Manual Payment
<?php
/*
Plugin Name: Autoconfirm appointments paid with MarketPress Manual Payment
Plugin URI: https://premium.wpmudev.org/
Description: Requires Appointments+ and MarketPress
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if( ! class_exists( 'WPMUDEV_App_Auto_Confirm_Manual_Paid_Apps' ) ){
class WPMUDEV_App_Auto_Confirm_Manual_Paid_Apps{
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_App_Auto_Confirm_Manual_Paid_Apps();
}
return self::$_instance;
}
private function __construct(){
add_action( 'mp_order/new_order', array( $this, 'maybe_autoconf' ), 10 );
}
public function maybe_autoconf( $order ){
$payment_method = mp_get_post_value( 'payment_method', '' );
if( $payment_method != 'manual_payments' ){
return;
}
$cart_info = is_object( $order ) && is_callable( array( $order, 'get_cart' ) )
? $order->get_cart()->get_items()
: ( isset( $order->mp_cart_info ) ? $order->mp_cart_info : array() )
;
if( !is_object( $cart_info ) || epmty( $cart_info ) ){
global $mp_cart;
$cart_info = $mp_cart->get_items();
}
$variation_type = MP_Product::get_variations_post_type();
$appointment_ids = array();
if ( is_array( $cart_info ) && count( $cart_info ) ) {
foreach ( $cart_info as $cart_id => $count ) {
$variation = get_post( $cart_id );
if ( ! empty( $variation->post_type ) && $variation_type === $variation->post_type && $this->_is_app_mp_page( $variation->post_parent ) ) {
$app_id = MP_Product::get_variation_meta( $variation->ID, 'name' );
if ( is_numeric( $app_id ) ) {
$appointment_ids[] = $app_id;
}
}
}
}
foreach ($appointment_ids as $aid) {
if ( ! appointments_update_appointment_status( $aid, 'paid' ) ) {
continue;
}
do_action('app_mp_order_paid', $aid, $order);
}
}
private function _is_app_mp_page ( $product ) {
$product = get_post($product);
$result = is_object( $product ) && strpos( $product->post_content, '[app_' ) !== false
? true
: false
;
// Maybe required for templates
return apply_filters( 'app_is_mp_page', $result, $product );
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_App_Auto_Confirm_Manual_Paid_Apps'] = WPMUDEV_App_Auto_Confirm_Manual_Paid_Apps::get_instance();
}, 10 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment