admin管理员组文章数量:1332377
I've noticed plugins using a singleton pattern that will use WordPress's _doing_it_wrong()
function in their clone()
methods, like so:
<?php
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'divlibrary' ), $this->version );
}
?>
But I also noticed this warning/notice on WordPress's official documentation:
It really doesn't matter when, how a developer is using it, WordPress is advocating that it shouldn't be used, so I'm wondering what is the correct way to do this within custom plugins? The method is used for deprecating code but in this instance the developer is just wanting to send a warning/alert out.
Reference: /
I've noticed plugins using a singleton pattern that will use WordPress's _doing_it_wrong()
function in their clone()
methods, like so:
<?php
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'divlibrary' ), $this->version );
}
?>
But I also noticed this warning/notice on WordPress's official documentation:
It really doesn't matter when, how a developer is using it, WordPress is advocating that it shouldn't be used, so I'm wondering what is the correct way to do this within custom plugins? The method is used for deprecating code but in this instance the developer is just wanting to send a warning/alert out.
Reference: https://developer.wordpress/reference/functions/_doing_it_wrong/
Share Improve this question asked Sep 9, 2016 at 5:27 XtremefaithXtremefaith 7882 gold badges5 silver badges21 bronze badges 3 |2 Answers
Reset to default 2What is an alternative method to the WordPress private _doing_it_wrong() function?
There's no way that WordPress is ever going to get rid of the _doing_it_wrong()
function, so it's perfectly safe to use it. But if for some reason you don't want to use it because it's marked private, then you could create a plugin that has a function called doing_it_wrong()
that is copy and pasted from _doing_it_wrong()
.
Another way would be to not copy code and instead use a class that handles deprecated code. Here's some sample code that basically does the same thing as _doing_it_wrong()
.
class deprecated {
protected $method;
protected $message;
protected $version;
public function method( $method ) {
$this->method = $method;
return $this;
}
public function message( $message ) {
$this->message = $message;
return $this;
}
public function version( $version ) {
$this->version = sprintf(
__( 'This message was added in version %1$s.' ),
$version
);
return $this;
}
public function trigger_error() {
do_action( 'doing_it_wrong_run', $this->method, $this->message, $this->version );
if ( WP_DEBUG && apply_filters( 'doing_it_wrong_trigger_error', true ) ) {
trigger_error( sprintf(
__( '%1$s was called <strong>incorrectly</strong>. %2$s %3$s' ),
isset( $this->method ) ? $this->method : '',
isset( $this->message ) ? $this->message : '',
isset( $this->version ) ? $this->version : ''
) );
}
}
}
Usage
class wpse_238672 {
public function some_deprecated_method() {
( new deprecated() )
->method( __METHOD__ )
->message( __(
'Deprecated Method: Use non_deprecated_method() instead.', 'wpse-238672'
) )
->version( '2.3.4' )
->trigger_error();
$this->non_deprecated_method();
}
public function non_deprecated_method() {
}
}
As of WordPress 5.4 _doing_it_wrong()
is no longer marked private, so it looks like we can just use it.
本文标签: plugin developmentWhat is an alternative method to the WordPress private doingitwrong() function
版权声明:本文标题:plugin development - What is an alternative method to the WordPress private _doing_it_wrong() function 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742306109a2449950.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
LogicException
as this error is caused by wrong design of the executing code. If you don't want to use exceptions, you can still always use trigger_error(). Also: Don't use singletons! – David Commented Sep 9, 2016 at 7:44trigger_error()
tip. As for "Don't use singletons" directive, there is a time and place to use them, so to declare that as a commandment is just as naive as the reasons most people use them. – Xtremefaith Commented Sep 9, 2016 at 16:52