PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.12.3
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.12.3
1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 All 211 releases
charitable / includes / deprecated / class-charitable-deprecated.php

class-charitable-deprecated.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.12.3, at includes/deprecated/class-charitable-deprecated.php

173 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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) 2023, 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 return self::$instance ??= new self();
72 }
73
74 /**
75 * Log a deprecated argument.
76 *
77 * @since 1.4.0
78 *
79 * @param string $function The deprecated function.
80 * @param string $version The version when this argument became deprecated.
81 * @param string|null $extra_message An extra message to include for the notice.
82 * @return boolean Whether the notice was logged.
83 */
84 public function deprecated_argument( $function, $version, $extra_message = null ) {
85 if ( ! $this->is_logging_enabled() ) {
86 return false;
87 }
88
89 if ( ! is_null( $extra_message ) ) {
90 // translators: 1: function name, 2: version number, 3: extra message, 4: plugin name.
91 $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 );
92 } else {
93 // translators: 1: function name, 2: version number, 3: plugin name.
94 $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 );
95 }
96
97 trigger_error( $message ); // phpcs:ignore
98
99 return true;
100 }
101
102 /**
103 * Log a deprecated function.
104 *
105 * @since 1.4.0
106 *
107 * @param string $function The function that has been deprecated.
108 * @param string $version The version of Charitable where the function was deprecated.
109 * @param string|null $replacement Optional. The function to use instead.
110 * @return boolean Whether the notice was logged.
111 */
112 public function deprecated_function( $function, $version, $replacement = null ) {
113 if ( ! $this->is_logging_enabled() ) {
114 return false;
115 }
116
117 if ( ! is_null( $replacement ) ) {
118 // translators: 1: function name, 2: version number, 3: replacement function name, 4: plugin name.
119 $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 );
120 } else {
121 // translators: 1: function name, 2: version number, 3: plugin name.
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 ); // phpcs:ignore
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 // translators: 1: function name, 2: error message, 3: version number.
146 $version = is_null( $version ) ? '' : sprintf( __( '(This message was added in %1$s version %2$s.)', 'charitable' ), $this->context, $version );
147
148 // translators: 1: function name, 2: error message, 3: version number.
149 $message = sprintf( __( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s', 'charitable' ), $function, $message, $version );
150
151 trigger_error( $message ); // phpcs:ignore
152
153 return true;
154 }
155
156 /**
157 * Returns whether logging is enabled.
158 *
159 * @since 1.4.0
160 *
161 * @return boolean
162 */
163 private function is_logging_enabled() {
164 if ( ! isset( self::$logging ) ) {
165 self::$logging = WP_DEBUG && apply_filters( 'charitable_log_deprecated_notices', true );
166 }
167
168 return self::$logging;
169 }
170 }
171
172 endif;
173