/wp-content/plugins/google-analytics-for-wordpress/includes/deprecated.php

https://bitbucket.org/carloskikea/helpet · PHP · 236 lines · 40 code · 19 blank · 177 comment · 12 complexity · 08e6cda48a46266aa1e2e9bcb0fb1719 MD5 · raw file

  1. <?php
  2. /**
  3. * Deprecated functions.
  4. *
  5. * Contains the functions used to deprecate functions and
  6. * hooks in MonsterInsights, as well as the deprecated functions
  7. * and hooks themselves, where possible.
  8. *
  9. * @since 6.0.0
  10. *
  11. * @package MonsterInsights
  12. * @subpackage Deprecated
  13. * @author Chris Christoff
  14. */
  15. // Exit if accessed directly
  16. if ( ! defined( 'ABSPATH' ) ) {
  17. exit;
  18. }
  19. /**
  20. * Fires functions attached to a deprecated filter hook.
  21. *
  22. * When a filter hook is deprecated, the apply_filters() call is replaced with
  23. * apply_filters_deprecated(), which triggers a deprecation notice and then fires
  24. * the original filter hook. Note, this is a copy of WordPress core's _apply_filters_deprecated
  25. * function, that we've copied into MonsterInsights so that we can use it on WordPress
  26. * versions older than 6.0.0 (when it was introduced to core). If we ever bump our
  27. * minimum WP version requirements above 6.0.0, we'll remove this function.
  28. *
  29. * @since 6.0.0
  30. * @access private
  31. *
  32. * @see _apply_filters_deprecated()
  33. *
  34. * @param string $tag The name of the filter hook.
  35. * @param array $args Array of additional function arguments to be passed to apply_filters().
  36. * @param string $version The version of WordPress that deprecated the hook.
  37. * @param string $message Optional. A message regarding the change. Default null.
  38. */
  39. function _monsterinsights_apply_filters_deprecated( $tag, $args, $version, $message = null ) {
  40. if ( ! has_filter( $tag ) ) {
  41. return $args[0];
  42. }
  43. _monsterinsights_deprecated_hook( $tag, $version, $message );
  44. return apply_filters_ref_array( $tag, $args );
  45. }
  46. /**
  47. * Fires functions attached to a deprecated action hook.
  48. *
  49. * When an action hook is deprecated, the do_action() call is replaced with
  50. * do_action_deprecated(), which triggers a deprecation notice and then fires
  51. * the original hook. Note, this is a copy of WordPress core's _do_action_deprecated
  52. * function, that we've copied into MonsterInsights so that we can use it on WordPress
  53. * versions older than 6.0.0 (when it was introduced to core). If we ever bump our
  54. * minimum WP version requirements above 6.0.0, we'll remove this function.
  55. *
  56. * @since 6.0.0
  57. * @access private
  58. *
  59. * @see _do_action_deprecated()
  60. *
  61. * @param string $tag The name of the action hook.
  62. * @param array $args Array of additional function arguments to be passed to do_action().
  63. * @param string $version The version of WordPress that deprecated the hook.
  64. * @param string $message Optional. A message regarding the change.
  65. */
  66. function _monsterinsights_do_action_deprecated( $tag, $args, $version, $message = null ) {
  67. if ( ! has_action( $tag ) ) {
  68. return;
  69. }
  70. _monsterinsights_deprecated_hook( $tag, $version, $message );
  71. do_action_ref_array( $tag, $args );
  72. }
  73. /**
  74. * Marks a deprecated action or filter hook as deprecated and throws a notice.
  75. *
  76. * Use the {@see 'deprecated_hook_run'} action to get the backtrace describing where
  77. * the deprecated hook was called.
  78. *
  79. * Default behavior is to trigger a user error if `WP_DEBUG` is true.
  80. *
  81. * This function is called by the do_action_deprecated() and apply_filters_deprecated()
  82. * functions, and so generally does not need to be called directly.
  83. *
  84. * Note, this is a copy of WordPress core's _deprecated_hook
  85. * function, that we've copied into MonsterInsights so that we can use it on WordPress
  86. * versions older than 6.0.0 (when it was introduced to core). If we ever bump our
  87. * minimum WP version requirements above 6.0.0, we'll remove this function.
  88. *
  89. * @since 6.0.0
  90. * @access private
  91. *
  92. * @param string $hook The hook that was used.
  93. * @param string $version The version of WordPress that deprecated the hook.
  94. * @param string $message Optional. A message regarding the change.
  95. */
  96. function _monsterinsights_deprecated_hook( $hook, $version, $message = null ) {
  97. /**
  98. * Fires when a deprecated hook is called.
  99. *
  100. * @since 6.0.0
  101. *
  102. * @param string $hook The hook that was called.
  103. * @param string $version The version of MonsterInsights that deprecated the hook used.
  104. * @param string $message A message regarding the change.
  105. */
  106. do_action( 'deprecated_hook_run', $hook, $version, $message );
  107. /**
  108. * Filters whether to trigger deprecated hook errors.
  109. *
  110. * @since 6.0.0
  111. *
  112. * @param bool $trigger Whether to trigger deprecated hook errors. Requires
  113. * `WP_DEBUG` to be defined true.
  114. */
  115. if ( ( WP_DEBUG && apply_filters( 'deprecated_hook_trigger_error', true ) ) || monsterinsights_is_debug_mode() ) {
  116. $message = empty( $message ) ? '' : ' ' . $message;
  117. trigger_error( sprintf( esc_html__( '%1$s is %3$sdeprecated%4$s since MonsterInsights version %2$s!', 'google-analytics-for-wordpress' ), $hook, $version, '<strong>', '</strong>' ) . esc_html ( $message ) );
  118. }
  119. }
  120. /**
  121. * Marks a function as deprecated and informs when it has been used.
  122. *
  123. * There is a hook monsterinsights_deprecated_function_run that will be called that can be used
  124. * to get the backtrace up to what file and function called the deprecated
  125. * function. Based on the one in EDD core.
  126. *
  127. * The current behavior is to trigger a user error if WP_DEBUG is true.
  128. *
  129. * This function is to be used in every function that is deprecated.
  130. *
  131. * @since 6.0.0
  132. * @access private
  133. *
  134. * @uses do_action() Calls 'monsterinsights_deprecated_function_run' and passes the function name, what to use instead,
  135. * and the version the function was deprecated in.
  136. * @uses apply_filters() Calls 'monsterinsights_deprecated_function_trigger_error' and expects boolean value of true to do
  137. * trigger or false to not trigger error.
  138. *
  139. * @param string $function The function that was called
  140. * @param string $version The version of WordPress that deprecated the function
  141. * @param array $backtrace Optional. Contains stack backtrace of deprecated function
  142. * @return void
  143. */
  144. function _monsterinsights_deprecated_function( $function, $version, $backtrace = null ) {
  145. /**
  146. * Deprecated Function Action.
  147. *
  148. * Allow plugin run an action on the use of a
  149. * deprecated function. This could be used to
  150. * feed into an error logging program or file.
  151. *
  152. * @since 6.0.0
  153. *
  154. * @param string $function The function that was called.
  155. * @param string $version The version of WordPress that deprecated the function.
  156. * @param array $backtrace Optional. Contains stack backtrace of deprecated function.
  157. */
  158. do_action( 'deprecated_function_run', $function, $version, $backtrace );
  159. /**
  160. * Filters whether to trigger an error for deprecated functions.
  161. *
  162. * @since 6.0.0
  163. *
  164. * @param bool $trigger Whether to trigger the error for deprecated functions. Default true.
  165. */
  166. if ( ( WP_DEBUG && apply_filters( 'deprecated_function_trigger_error', true ) ) || monsterinsights_is_debug_mode() ) {
  167. trigger_error( sprintf( esc_html__( '%1$s is %3$sdeprecated%4$s since MonsterInsights version %2$s.', 'google-analytics-for-wordpress' ), $function, $version, '<strong>', '</strong>' ) );
  168. trigger_error( print_r( $backtrace, 1 ) );// Limited to previous 1028 characters, but since we only need to move back 1 in stack that should be fine.
  169. // Alternatively we could dump this to a file.
  170. }
  171. }
  172. /**
  173. * Marks something as deprecated.
  174. *
  175. * The current behavior is to trigger a user error if WP_DEBUG is true.
  176. *
  177. * @since 6.0.0
  178. * @access private
  179. *
  180. * @uses apply_filters() Calls 'monsterinsights_deprecated_trigger_error' and expects boolean value of true to do
  181. * trigger or false to not trigger error.
  182. *
  183. * @param string $message Deprecation message shown.
  184. * @return void
  185. */
  186. function _monsterinsights_deprecated( $message ) {
  187. /**
  188. * Deprecated Message Filter.
  189. *
  190. * Allow plugin to filter the deprecated message.
  191. *
  192. * @since 6.0.0
  193. *
  194. * @param string $message Error message.
  195. */
  196. do_action( 'monsterinsights_deprecated_run', $message );
  197. $show_errors = current_user_can( 'manage_options' );
  198. /**
  199. * Deprecated Error Trigger.
  200. *
  201. * Allow plugin to filter the output error trigger.
  202. *
  203. * @since 6.0.0
  204. *
  205. * @param bool $show_errors Whether to show errors.
  206. */
  207. $show_errors = apply_filters( 'monsterinsights_deprecated_trigger_error', $show_errors );
  208. if ( ( WP_DEBUG && $show_errors ) || monsterinsights_is_debug_mode() ) {
  209. trigger_error( esc_html( $message ) );
  210. }
  211. }
  212. /**
  213. * Start Deprecated Actions & Filters.
  214. *
  215. * These backwards compatibility fixes may be removed at any time.
  216. * Users/Developers are encouraged to update their code as soon as possible.
  217. */