WordPress advanced OOP: customization of rest API endpoint to improve WordPress search

Let’s look at how to modify the allowed endpoint factors and how to create wp_query factors. It is two independent concerns, and the single responsibility principle means that each concern needs a class. However, both classes share common interests. For example, to allow querying different post types, you must know what is the public post type and what are the slug and rest_base parameters. This is all the information available to the get_post\u types function. The
The output of this function is not what we need. Now, let’s design a class that specifies the data format according to the requirements just listed and provides help methods that can access the data. Think of it as a common look and feel for all post type data required for usable containers. Male setposttypes ($posttypes);
}The
\/**The
*Import an array of ‘rest_base’ values for all public post types
*The
*@Return array
*\/The
Public function getposttyperestbases(): array
The{
Return! Empty ($this->posttypes)? Array_keys ($this->posttypes): [];
}The
\/**The
*Prepare article type
*The
*@Param array $posttypes
*\/The
Protected function setposttypes (array $posttypes)
The{
$this->posttypes = [];
\/**@var \wp\u post\u type $posttype*\/
Foreach (convert $posttype to $posttype){
If ($posttype->show_in_rest){
$this->posttype[$posttype->rest\u base]=$posttype-> name;
}The
}The
}The
\/**The
*Convert rest API based to post type slog
*The
*@Param string $restbase
*@Return string | null
*\/The
Public function restbasetoslug (string $restbase)
The{
If (in_array ($restbase, $this->getposttyperestbases())){
Return $this->posttype[$restbase];
}The
Returns null.
}The
}Class does not call get_post\u types (), but uses it as a dependency injected through the creator. As a result, the class can be tested without loading WordPress. The
This is why I describe this class as a \
. Instead of instantiating preparedposttypes, pass instances within the required class. This means that classes that use preparedposttype and preparedposttype will remain isolated and can be tested separately. In addition, dependency injection must be made possible and have the properties of the object, so that code can be reused. You can use cut and paste, or you can use PHP trail. PHP trait is a more extensible method for copying methods and properties between classes. The following are the properties that set the mode to inject preparedposttype objects into other classes: Male preparedposttypes = $preparedposttypes;
}The
}Our other concern is that we need to understand the types of posts in multiple locations. For example, a slug of type post. This is a cross cutting concern different from previous flavors. The last problem we solve is dynamic data. Now, you only need to change the string or two strings used in multiple locations in one location. Classes with class constants can easily solve this problem. Type of post
*The
*\/The
Class posttype
The{
\/**The
*Column slide
*The
*@Todo changes it to a post type slug.
*\/The
Const slug= \
\/**The
*Column type rest_base
*The
*@Todo changes it to the rest_base of the post type.
*\/The
Const restbase= \
}You can now keep these strings consistent throughout your code. This may seem like an unnecessary step. However, my sample code works in the post type. To change the type of post used, you must change the class without changing the others. This follows Tom McFarlin’s preferred definition of the single responsibility principle. Class a must have only one reason to change. To modify the rest API endpoint schema, you now need to modify the endpoint schema of the post type. In this way, WordPress will communicate with the rest API endpoint, which allows post\u type parameters and new endpoint parameters when parsing the request. The following are the classes that add the post\u type attribute: Use the userspreparedposttypes attribute just discussed. Male shouldfilter ($post\u type)){
$query_params[self:: argname] =[
The[
\
\
\
\/\/Limited to public post type, allowing query based on the rest of the content
\
The[
\
\
],
]The
];
}The
Return $query\u params;
}The
\/**The
*Should I filter patterns for this post type
*The
*@param \wp\u post\u type $wp\u post\u type
*@Carriage return Boolean
*\/The
Public function shouldfilter (\wp\u post\u type$wp\u post\u type): bool
The{
Return posttype::slug===$wp\u post\u type->name;
}The
}In the setting of this property, we inform WordPress that this property is an array property, and use the \
Add\u filter (‘posts\u pre\u query’, [filterwpquery:: class,’posts\u pre\u query’], 10, 2);
$args[‘post\u type’] = $this->restbasestoposttypeslugs ($request[modifyschema:: argname]);
}The
Return $args;
}The
\/**The
*Confirm that filter request factor is required
*The
*@param \wp\u rest\u request $request
*@Carriage return Boolean
*\/The
Public function shouldfilter (\wp\u rest\u request$request): bool
The{
$attributes = $request->get\u attributes();
If (isset ($attributes[‘args’][modifyschema:: argname]){
If ($request->get\u param (modifyschema:: argname)){
Returns true.
}The
}The
Return false.
}The
\/**The
*Convert array to post type slug
*The
*@Param array $posttypes
*@Return array
*\/The
Public function restbasestoposttypeslugs (array $posttypes): array
The{
$posttypeslugs = [];
Foreach (use $posttypes as $posttyperestbase){
If ($this->preparedposttypes->restbasetoslug ($posttyperestbase)){
$posttypeslugs[] = $this->preparedposttypes->restbasetoslug ($posttyper
\
],
]The
];
}The
Return $query\u params;
}The
\/**The
*Should I filter patterns for this post type
*The
*@param \wp\u post\u type $wp\u post\u type
*@Carriage return Boolean
*\/The
Public function shouldfilter (\wp\u post\u type$wp\u post\u type): bool
The{
Return posttype::slug===$wp\u post\u type->name;
}The
}It is also easy to create recursive rings when using this filter. Well, you can remove it as easily as this sample code. Another reason I choose to use static methods is that functions interact with other APIs. It can never be a real unit test. This pattern is a class with static methods, which can be easily mocked in the integration test, so as to minimize the impact of this part of the system without strong isolation. To make everything work together, the code we see is very separate from WordPress. This has many advantages. But that means you do nothing. It doesn’t matter. So far, we have only addressed business logic requirements. Now let’s look at integration. It’s not that hard. Just add a few hooks. What kind of hook? These two hooks are exactly the same as the modifyquery and modifyschema classes we designed. The desire to separate business logic does not mean that the actual reasons for writing code cannot be taken into account when designing a common interface. Otherwise, you will add complexity to your code for no reason. Generally speaking, I just want to add software complexity when it makes life easier. I used to leave this road. We should all practice forgiveness. The methods of the class we want to connect use exactly the same parameters and return types as those of the hook. Their task is to send the value to other components of the business. <? PHP
\/**The
*Start it all
*\/The
Add_action (\
$posttype = posttype:: slug;
$preparedposttype=new preparedpost_types ([], \
$modifyschema= new modifyschema ($preparedposttype);
Add\u filter (\
$modifyquery= new modifyqueryargs ($preparedposttype);
Add\u filter (\
}); Next step: test it close enough to jazz. Work. This is the best way to initialize because there is no formal system attached. It doesn't matter. We will discuss in a future post how to create a more refined and extensible boot strap process in the integration test with WordPress. This post describes how to create code to modify the schema, wp_query parameters, and the default wp\u query for the post type. It is recommended that you use the composer of the autoloader to convert this code into a plug-in. The following article describes unit tests that handle these classes.
good

Author:

Leave a Reply

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