| 1 |
<?php |
| 2 |
/** |
| 3 |
* A helper class for logging deprecated arguments, functions and methods. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Deprecated |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @version 1.4.0 |
| 10 |
* @version 1.5.9 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Deprecated' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Deprecated |
| 22 |
* |
| 23 |
* @since 1.4.0 |
| 24 |
*/ |
| 25 |
class Charitable_Deprecated { |
| 26 |
|
| 27 |
/** |
| 28 |
* One true class object. |
| 29 |
* |
| 30 |
* @since 1.4.0 |
| 31 |
* |
| 32 |
* @var Charitable_Deprecated |
| 33 |
*/ |
| 34 |
private static $instance = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether logging is enabled. |
| 38 |
* |
| 39 |
* @since 1.4.0 |
| 40 |
* |
| 41 |
* @var $logging |
| 42 |
*/ |
| 43 |
private static $logging; |
| 44 |
|
| 45 |
/** |
| 46 |
* Plugin that this is being called for. |
| 47 |
* |
| 48 |
* @since 1.5.9 |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
protected $context; |
| 53 |
|
| 54 |
/** |
| 55 |
* Create class object. Private constructor. |
| 56 |
* |
| 57 |
* @since 1.4.0 |
| 58 |
*/ |
| 59 |
private function __construct() { |
| 60 |
$this->context = 'Charitable'; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Create and return the class object. |
| 65 |
* |
| 66 |
* @since 1.4.0 |
| 67 |
* |
| 68 |
* @return Charitable_Deprecated |
| 69 |
*/ |
| 70 |
public static function get_instance() { |
| 71 |
if ( is_null( self::$instance ) ) { |
| 72 |
self::$instance = new self(); |
| 73 |
} |
| 74 |
|
| 75 |
return self::$instance; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Log a deprecated argument. |
| 80 |
* |
| 81 |
* @since 1.4.0 |
| 82 |
* |
| 83 |
* @param string $function The deprecated function. |
| 84 |
* @param string $version The version when this argument became deprecated. |
| 85 |
* @param string|null $extra_message An extra message to include for the notice. |
| 86 |
* @return boolean Whether the notice was logged. |
| 87 |
*/ |
| 88 |
public function deprecated_argument( $function, $version, $extra_message = null ) { |
| 89 |
if ( ! $this->is_logging_enabled() ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! is_null( $extra_message ) ) { |
| 94 |
$message = sprintf( __( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s of %4$s! %3$s', 'charitable' ), $function, $version, $extra_message, $this->context ); |
| 95 |
} else { |
| 96 |
$message = sprintf( __( '%1$s was called with an argument that is <strong>deprecated</strong> since version %2$s of %3$s with no alternatives available.', 'charitable' ), $function, $version, $this->context ); |
| 97 |
} |
| 98 |
|
| 99 |
trigger_error( $message ); |
| 100 |
|
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Log a deprecated function. |
| 106 |
* |
| 107 |
* @since 1.4.0 |
| 108 |
* |
| 109 |
* @param string $function The function that has been deprecated. |
| 110 |
* @param string $version The version of Charitable where the function was deprecated. |
| 111 |
* @param string|null $replacement Optional. The function to use instead. |
| 112 |
* @return boolean Whether the notice was logged. |
| 113 |
*/ |
| 114 |
public function deprecated_function( $function, $version, $replacement = null ) { |
| 115 |
if ( ! $this->is_logging_enabled() ) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! is_null( $replacement ) ) { |
| 120 |
$message = sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s of %4$s! Use %3$s instead.', 'charitable' ), $function, $version, $replacement, $this->context ); |
| 121 |
} else { |
| 122 |
$message = sprintf( __( '%1$s is <strong>deprecated</strong> since version %2$s of %3$s with no alternatives available.', 'charitable' ), $function, $version, $this->context ); |
| 123 |
} |
| 124 |
|
| 125 |
trigger_error( $message ); |
| 126 |
|
| 127 |
return true; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Log a general "doing it wrong" notice. |
| 132 |
* |
| 133 |
* @since 1.4.0 |
| 134 |
* |
| 135 |
* @param string $function The function with the problem. |
| 136 |
* @param string $message An error message. |
| 137 |
* @param string $version The version in which this error message was addd. |
| 138 |
* @return boolean Whether the notice was logged. |
| 139 |
*/ |
| 140 |
public function doing_it_wrong( $function, $message, $version ) { |
| 141 |
if ( ! $this->is_logging_enabled() ) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
$version = is_null( $version ) ? '' : sprintf( __( '(This message was added in %s version %s.)', 'charitable' ), $this->context, $version ); |
| 146 |
|
| 147 |
$message = sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', 'charitable' ), $function, $message, $version ); |
| 148 |
|
| 149 |
trigger_error( $message ); |
| 150 |
|
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Returns whether logging is enabled. |
| 156 |
* |
| 157 |
* @since 1.4.0 |
| 158 |
* |
| 159 |
* @return boolean |
| 160 |
*/ |
| 161 |
private function is_logging_enabled() { |
| 162 |
if ( ! isset( self::$logging ) ) { |
| 163 |
self::$logging = WP_DEBUG && apply_filters( 'charitable_log_deprecated_notices', true ); |
| 164 |
} |
| 165 |
|
| 166 |
return self::$logging; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
endif; |
| 171 |
|