PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/error-tracking.php

https://github.com/keha76/Easy-Digital-Downloads
PHP | 133 lines | 50 code | 9 blank | 74 comment | 5 complexity | 8d735994a932745046b8213e85506071 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Error Tracking
  4. *
  5. * @package EDD
  6. * @subpackage Functions/Errors
  7. * @copyright Copyright (c) 2013, Pippin Williamson
  8. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  9. * @since 1.0
  10. */
  11. // Exit if accessed directly
  12. if ( ! defined( 'ABSPATH' ) ) exit;
  13. /**
  14. * Print Errors
  15. *
  16. * Prints all stored errors. For use during checkout.
  17. * If errors exist, they are returned.
  18. *
  19. * @since 1.0
  20. * @uses edd_get_errors()
  21. * @uses edd_clear_errors()
  22. * @return void
  23. */
  24. function edd_print_errors() {
  25. $errors = edd_get_errors();
  26. if ( $errors ) {
  27. $classes = apply_filters( 'edd_error_class', array(
  28. 'edd_errors'
  29. ) );
  30. echo '<div class="' . implode( ' ', $classes ) . '">';
  31. // Loop error codes and display errors
  32. foreach ( $errors as $error_id => $error ){
  33. echo '<p class="edd_error" id="edd_error_' . $error_id . '"><strong>' . __('Error', 'edd') . '</strong>: ' . $error . '</p>';
  34. }
  35. echo '</div>';
  36. edd_clear_errors();
  37. }
  38. }
  39. add_action( 'edd_purchase_form_before_submit', 'edd_print_errors' );
  40. add_action( 'edd_ajax_checkout_errors', 'edd_print_errors' );
  41. /**
  42. * Get Errors
  43. *
  44. * Retrieves all error messages stored during the checkout process.
  45. * If errors exist, they are returned.
  46. *
  47. * @since 1.0
  48. * @uses EDD_Session::get()
  49. * @return mixed array if errors are present, false if none found
  50. */
  51. function edd_get_errors() {
  52. return EDD()->session->get( 'edd_errors' );
  53. }
  54. /**
  55. * Set Error
  56. *
  57. * Stores an error in a session var.
  58. *
  59. * @since 1.0
  60. * @uses EDD_Session::get()
  61. * @param int $error_id ID of the error being set
  62. * @param string $error_message Message to store with the error
  63. * @return void
  64. */
  65. function edd_set_error( $error_id, $error_message ) {
  66. $errors = edd_get_errors();
  67. if ( ! $errors ) {
  68. $errors = array();
  69. }
  70. $errors[ $error_id ] = $error_message;
  71. EDD()->session->set( 'edd_errors', $errors );
  72. }
  73. /**
  74. * Clears all stored errors.
  75. *
  76. * @since 1.0
  77. * @uses EDD_Session::set()
  78. * @return void
  79. */
  80. function edd_clear_errors() {
  81. EDD()->session->set( 'edd_errors', null );
  82. }
  83. /**
  84. * Removes (unsets) a stored error
  85. *
  86. * @since 1.3.4
  87. * @uses EDD_Session::set()
  88. * @param int $error_id ID of the error being set
  89. * @return void
  90. */
  91. function edd_unset_error( $error_id ) {
  92. $errors = edd_get_errors();
  93. if ( $errors ) {
  94. unset( $errors[ $error_id ] );
  95. EDD()->session->set( 'edd_errors', $errors );
  96. }
  97. }
  98. /**
  99. * Register die handler for edd_die()
  100. *
  101. * @author Sunny Ratilal
  102. * @since 1.6
  103. * @return void
  104. */
  105. function _edd_die_handler() {
  106. if ( defined( 'EDD_UNIT_TESTS' ) )
  107. return '_edd_die_handler';
  108. else
  109. die();
  110. }
  111. /**
  112. * Wrapper function for wp_die(). This function adds filters for wp_die() which
  113. * kills execution of the script using wp_die(). This allows us to then to work
  114. * with functions using edd_die() in the unit tests.
  115. *
  116. * @author Sunny Ratilal
  117. * @since 1.6
  118. * @return void
  119. */
  120. function edd_die() {
  121. add_filter( 'wp_die_ajax_handler', '_edd_die_handler', 10, 3 );
  122. add_filter( 'wp_die_handler', '_edd_die_handler', 10, 3 );
  123. wp_die('');
  124. }