A static approach as a solution to WordPress cross cutting problems

Delete all callbacks that can be attached
*The
*@Param string$hook hook hook name
*\/The
Public function remove_all ($hook){
Global $wp\u filter;
If (isset ($wp\u filter[$hook])){
If (class\u exists (‘wp\u hook’){
$all= copy $wp\u filter[$hook];
}Another{
$all = $wp_filter[$hook];
}The
If (! Empty ($all){
$this->removed[$hook] = $all;
Remove\u all\u filters ($hook);
}The
}The
}The
\/**The
*Re add all callbacks removed from the hook.
*The
*You must use $this->remove\u all() to delete.
*The
*@Param string$hook hook hook name
*\/The
Public function re_add ($hook){
If (isset ($this-> deleted [$hook]){
$looked=$this-> deleted [$hook];
If (! Empty ($hooked)){
If (class\u exists (‘wp\u hook’){
$priority=$hooked-> callback;
}Another{
$priorities = $looked;
}The
Foreach ($priorities as $priority = > $callbacks){
Foreach ($callbacks as $callback){
Add\u filter ($hook, $callback[‘function’], $priority, $callback[‘accepted\u args’]);
}The
}The
}The
}The
}The
}In this case, you must create an instance of the object and then trace it throughout the application. This is important because each instance of the object has a different hook removed from the $remove attribute. The
That might be good. You can provide instances of all classes that need to delete the hook. These classes are options that you can put back in later. This means that an approach that requires multiple instances of the same class that exist throughout the application does not have much advantage. In fact, this is more useful than having an instance. Before we get to global variables or monochrome mode, let’s take a look at this class recreated using static methods and static properties. You must use maleremove\u all().
*The
*@Param string$hook hook hook name
*\/The
Public static function re_add ($hook){
If (isset (self:: $removed[$hook])){
$hooked = self:: $removed[$hook];
If (! Empty ($hooked)){
If (class\u exists (‘wp\u hook’){
$priority=$hooked-> callback;
}Another{
$priorities = $looked;
}The
Foreach ($priorities as $priority = > $callbacks){
Foreach ($callbacks as $callback){
Add\u filter ($hook, $callback[‘function’], $priority, $callback[‘accepted\u args’]);
}The
}The
}The
}The
}The
}The important thing to keep in mind here is that static methods and properties can access classes without instantiation. In other words, when using static $remove::remove\u all(), you don’t have to worry about instance tracking. You can use remov later
Er:: when re\u add() is used, the tracked hook has the same $remove attribute. The
Now, by default, there is a global system to track hook deletions without using global variables. You don’t have to worry about conflicts with other plug-ins or topics that use the global with the same name in different ways or misuse of the $remove attribute. Because access is controlled by my class. In the process of solving the problem, in this article, I introduced the concept of cross problem. We also discussed a class with all static methods, which is a solution to a cross cutting problem that requires more than simple functionality. Remember, the goal is not to stick to principles for the sake of principles, but to solve problems without creating unnecessary technical liabilities. The
Hopefully, this will improve the ability to design object-oriented PHP code in large projects. It is a challenge for me to deal with these problems in different locations of the project. While avoiding repetition, it has been difficult to maintain a curriculum that adheres to the principle of single responsibility. Understanding the concept of cross problem is helpful to understand the problem and find new and more useful solutions.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *