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