PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/p/appointments/includes/default_filters.php

https://bitbucket.org/matthewselby/wpdev
PHP | 141 lines | 111 code | 11 blank | 19 comment | 29 complexity | bc53c01397cd8c03f7ae885baaa76b39 MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0, LGPL-3.0, LGPL-2.1, AGPL-1.0, BSD-3-Clause, MIT, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
  3. function _app_catch_wrong_pattern_debug_notices( $where ) {
  4. if ( apply_filters( 'doing_it_wrong_trigger_error', true ) ) { trigger_error( sprintf( 'Wrong usage pattern detected when calling %s', $where ) ); }
  5. }
  6. add_action( 'app-core-doing_it_wrong', '_app_catch_wrong_pattern_debug_notices' );
  7. }
  8. if ( ! (defined( 'APP_DISALLOW_META_WRAPPERS' ) && APP_DISALLOW_META_WRAPPERS) ) {
  9. function _app_wrap_meta_output( $out ) {
  10. if ( ! $out ) { return $out; }
  11. return '<div class="app-settings-column_meta_info">' .
  12. '<div class="app-settings-column_meta_info-content" style="display:none">' . $out . '</div>' .
  13. '<div>' .
  14. '<a href="#toggle" class="app-settings-column_meta_info-toggle" ' .
  15. 'data-off="' . esc_attr( __( 'More Info', 'appointments' ) ) . '" ' .
  16. 'data-on="' . esc_attr( __( 'Less Info', 'appointments' ) ) . '" ' .
  17. '>' . __( 'More Info', 'appointments' ) . '</a>' .
  18. '</div>' .
  19. '</div>';
  20. }
  21. add_filter( 'app-settings-services-service-name', '_app_wrap_meta_output', 9991 );
  22. add_filter( 'app-settings-workers-worker-name', '_app_wrap_meta_output', 9991 );
  23. }
  24. if ( defined( 'APP_GCAL_CLIENT_TEMP_DIR_AUTO_LOOKUP' ) && APP_GCAL_CLIENT_TEMP_DIR_AUTO_LOOKUP ) {
  25. /**
  26. * Wrapper for Google Client cache filepath + open_basedir restriction resolution.
  27. */
  28. function _app_gcal_client_temp_dir_lookup( $params ) {
  29. if ( ! function_exists( 'get_temp_dir' ) ) { return $params; }
  30. $params['ioFileCache_directory'] = get_temp_dir() . 'Google_Client';
  31. return $params;
  32. }
  33. add_filter( 'app-gcal-client_parameters', '_app_gcal_client_temp_dir_lookup' );
  34. }
  35. if ( ! (defined( 'APP_USE_LEGACY_MP_INTEGRATION' ) && APP_USE_LEGACY_MP_INTEGRATION) ) {
  36. /**
  37. * Appointment-to-order mapping getter.
  38. */
  39. function app_mp_get_order_for_appointment( $app_id ) {
  40. $query = new WP_Query(array(
  41. 'post_type' => 'mp_order',
  42. //'post_status' => 'order_paid', // Can't use this, at least for manual payments gateway because post status transition happens AFTER the paid hook (o.0 yeah)
  43. 'post_status' => array( 'order_paid', 'order_received' ), // This is much less appropriate IMHO, but we'll be narrowing it down with `mp_paid_time` meta
  44. 'meta_query' => array(
  45. 'relation' => 'AND',
  46. array(
  47. 'key' => '_appointment_purchased_via_mp',
  48. 'value' => $app_id,
  49. ),
  50. // Since we absolutely can't rely on order status, let's hack our way around
  51. array(
  52. 'key' => 'mp_paid_time',
  53. 'value' => 0,
  54. 'compare' => '>',
  55. ),
  56. ),
  57. 'fields' => 'ids',
  58. ));
  59. if ( empty( $query->posts ) || empty( $query->posts[0] ) ) { return false; // Can't map
  60. } $order_id = (int) $query->posts[0];
  61. global $mp;
  62. if ( ! method_exists( $mp, 'get_order' ) ) { return false; // Erm... no MarketPress
  63. }
  64. return $mp->get_order( $order_id );
  65. }
  66. /**
  67. * Order-to-appointments mapping.
  68. */
  69. function app_mp_get_appointment_ids_from_order( $order ) {
  70. $order_id = is_object( $order ) && ! empty( $order->ID )
  71. ? $order->ID
  72. : (is_numeric( $order ) ? $order : false)
  73. ;
  74. if ( ! $order_id ) { return false; }
  75. $purchased = get_post_meta( $order_id, '_appointment_purchased_via_mp' );
  76. if ( ! empty( $purchased ) ) { return $purchased; // Easy way out for already paid order
  77. }
  78. // No easy way? Oh well...
  79. if ( empty( $order->mp_cart_info ) ) { return false; }
  80. $app_ids = array();
  81. foreach ( $order->mp_cart_info as $product_id => $product_detail ) {
  82. foreach ( $product_detail as $var ) {
  83. list($product_name, $app_id) = split( ':', $var['name'] );
  84. if ( ! empty( $app_id ) && is_numeric( $app_id ) ) { $app_ids[] = $app_id; }
  85. }
  86. }
  87. return array_values( array_filter( array_map( 'intval', array_unique( $app_ids ) ) ) );
  88. }
  89. /**
  90. * This is where the link actually gets established.
  91. */
  92. function _app_mp_record_appointment_order_info( $app_id, $order ) {
  93. if ( empty( $order->ID ) || empty( $app_id ) ) { return false; }
  94. add_post_meta( $order->ID, '_appointment_purchased_via_mp', $app_id, false );
  95. wp_cache_delete( $order->ID, 'post_meta' );
  96. }
  97. add_filter( 'app_mp_order_paid', '_app_mp_record_appointment_order_info', 10, 2 );
  98. }
  99. /**
  100. * Triggered when a user cancels an appointment from email link
  101. */
  102. function appointments_maybe_cancel_appointment() {
  103. if ( ! isset( $_GET['action'] ) || 'cancel-app' !== $_GET['action'] ) {
  104. return;
  105. }
  106. $app_id = absint( $_GET['id'] );
  107. $token = rawurldecode( $_GET['t'] );
  108. $app = appointments_get_appointment( $app_id );
  109. if ( ! $app ) {
  110. wp_die( __( 'The appointment does not exist.', 'appointments' ), __( 'Error', 'appointments' ), array( 'response' => 400 ) );
  111. }
  112. if ( $token != $app->get_cancel_token() ) {
  113. wp_die( __( 'You are not allowed to perform this action.', 'appointments' ), __( 'Error', 'appointments' ), array( 'response' => 403 ) );
  114. }
  115. // Remove the gcal appointment
  116. $appointments = appointments();
  117. $gcal = $appointments->get_gcal_api();
  118. $args['status'] = 'removed';
  119. $gcal->on_update_appointment( $app_id, $args, $app );
  120. $options = appointments_get_options();
  121. appointments_cancel_appointment( $app_id );
  122. $url = get_permalink( $options['cancel_page'] );
  123. if ( $url ) {
  124. wp_redirect( $url );
  125. exit;
  126. } else {
  127. wp_die( __( 'Your appointment has been cancelled!', 'appointments' ), __( 'Appointment cancelled', 'appointments' ), array( 'response' => 410 ) );
  128. }
  129. }
  130. add_action( 'init', 'appointments_maybe_cancel_appointment' );