WordPress abstraction plugins
03.12.2021 12:33 1.187 Displayed

WordPress abstraction plugins

WordPress is an ancient CMS, but it is also the most widely used. Due to its history of supporting old versions of PHP and legacy code, it still lacks the implementation of modern coding practices-abstract WordPress is an example.

For example, it would be better if you split the WordPress source code into packages run by Composer. Or, download the WordPress course automatically from the file path.

This article will teach you how to manually edit WordPress code and use the features of abstract WordPress plugins.

contents

1. WordPress and PHP tool integration issues

2. What is abstract code?

3. Coding for intermediates rather than implementations

4. Encode content in WordPress

5. Packaging production and distribution

6. Extract the WordPress code in the package

7. Use dependency injection

8. Dema by Abstract

9. Best Practices

10. Benefits of abstract code

11. The problem of abstract code

12. Abstract WordPress plugin options

WordPress and PHP tool integration issues

Due to its traditional architecture, we sometimes encounter problems when integrating WordPress with PHP code base tools (such as PHPS static analyzer, PHPUnit unit test library, and PHP-Scoper namespace library). For example, consider the following situation:

• Prior to WordPress 5.6 supporting PHP 8.0, a Yoast report outlined how running a WordPress-based PHPS would solve thousands of problems.

• Because it still supports PHP 5.6, the WordPress test package now only supports PHPUnit until version 7.5 has reached its life cycle.

• Using PHP-Scoper to determine the scope of a WordPress plugin is very difficult.

The WordPress code in our project is only a small part of the whole; the project will also have a basic CMS agnostic business code. However, with just the WordPress code, the project may not integrate properly with the tool.

Therefore, it may make sense to split the project into multiple packages, some of which contain WordPress code, while others contain only business code using "vanilla" PHP and no WordPress code. In this way, these latter packages will not be affected by the above-mentioned problems, but can be perfectly integrated with the device.

What is abstract code?

Deleting the code removes the permalink from the code, creating packages that interact with each other through contracts. These packages can then be added to different columns in different applications to maximize their use. The result of code abstraction is based on the following refined code base:

1. The code for the interval, not the implementation.

2. Create packages and distribute them through Composer.

3. Use connecting pins to glue all parts together.

Want to learn more about WordPress code abstraction?

Coding for intermediates instead of implementations

Coding for intermediaries is the practice of using contracts to exchange code fragments with each other. A contract is just a PHP tool (or a different language), which specifies which functions and their signatures they have, that is, which ones to enter and exit them.

The interval specifies the intent of the function, but does not explain how the function will be implemented. By accessing this functionality through an intermediary, our applications can rely on automated code fragments that can be used for specific purposes without knowing or paying attention to how they are executed. Therefore, the application does not need to be adjusted to transfer to another code for the same purpose—for example, from a different server.

Example contract

The following code uses Symfony's CacheInterface protocol and PHP Standard Prevention Protocol (PSR) CacheItemInterface to perform the caching function:

use Psr\Cache\CacheItemInterface;

use Symfony\Contracts\Cache\CacheInterface;

 

$value = $cache->get('my_cache_key', function (CacheItemInterface $item) {

$item->expiresAfter(3600);

return 'foobar';

});

$cache implements CacheInterface, which specifies the method of restoring from the cache. By accessing this function through the contract, the application will not forget where the cache is. Whether in memory, disk, database, network or anywhere else. Nevertheless, it still needs to perform this function. CacheItemInterface finishes this method after specifying how long the object remains in the cache. Applicants can run this method without paying attention to what the object is; what matters is how long it should be held.

Coding for intervals in WordPress

Since we removed the WordPress code, the result will be that the application will not directly access the WordPress code, but will always link to a medium. For example, the WordPress function get_posts has the following signature:

/**

* @param array $ args

* @return WP_Post [] | int [] The line of the post item or post ID.

* /

get_posts function ($args = null)

Instead of calling this method directly, we can access it through the Owner\App\Contracts\PostsAPIIinterface protocol:

Owner's name\application\contract;



PostAPII interface interface

{

General function get_posts(array $args = null): Intermediate Post[]|int[];

}

Please note that the get_posts WordPress function can return objects of the WP_Post class, which is unique to WordPress. When deleting the code, we need to delete the fixed connection in this way. The get_posts method in the contract returns an item of type PostInterface, which allows you to refer to the WP_Post class without having to specify it. The PostInterface class needs to access all the methods and functions of WP_Post:

Owner's name\application\contract;



PostInterface interface

{

General function get_ID(): int;

General function get_post_author(): string;

General function get_post_date(): string;

// ...

}

Implementing this strategy can change our understanding of the position of WordPress in our column. Instead of thinking of WordPress as an application itself (on which we build themes and plugins), we can simply think of it as another link in the application, which can be modified like any other component. (Although we will not translate WordPress in practice, it can be transferred through concepts.)

Packaging production and distribution

Composer is a package manager for PHP. It allows PHP applications to retrieve packages (or code snippets) from the repository and install them as accessories. In order to separate the application from WordPress, we need to distribute the code in two different packages: those with WordPress code and those that perform business logic (that is, without WordPress code).

Finally, we add all packages as links in the application and install them through Composer. Since the tool will be applied to business code packages, it must contain most of the application code; the higher the percentage, the better. Running about 90% of their common code is a good goal.

Extract the WordPress code in the package

Following the previous example, the PostAPIInterface and PostInterface protocols will be added to the package containing the business code, and another package will take over the WordPress implementation of these protocols. In order to satisfy PostInterface, we created a PostWrapper class, which will get all functions from the WP_Post object:

namespace Owner\MyAppForWP\ContractImplementations;

 

use Owner\MyApp\Contracts\PostInterface;

use WP_Post;

 

class PostWrapper implements PostInterface

{

private WP_Post $post;

public function __construct(WP_Post $post)

{

$this->post = $post;

}

 

public function get_ID(): int

{

return $this->post->ID;

}

 

public function get_post_author(): string

{

return $this->post->post_author;

}

 

public function get_post_date(): string

{

return $this->post->post_date;

}

 

// ...

}

When implementing PostAPI, since the get_posts method returns PostInterface[], we need to move things from WP_Post to PostWrapper:

namespace Owner\MyAppForWP\ContractImplementations;

 

use Owner\MyApp\Contracts\PostAPIInterface;

use WP_Post;

 

class PostAPI implements PostAPIInterface

{

public function get_posts(array $args = null): PostInterface[]|int[]

{

// This var will contain WP_Post[] or int[]

$wpPosts = \get_posts($args);

 

// Convert WP_Post[] to PostWrapper[]

return array_map(

function (WP_Post|int $post) {

if ($post instanceof WP_Post) {

<span style="color: #0077aa;">return new<span style="color: #000000;"> PostWrapper($post);

}

return $post

},

$wpPosts

);

}

}

Dependency Injection

The connecting pin is a design style that allows you to glue all parts of the application together perfectly. Through the link binding, the application reaches the work through its contract, and the contract realization is medically "injected" into the application.

By simply changing the configuration, we can easily move from one contract provider to another. There are many attached vaccination libraries for us to choose from. We recommend that you choose a library that complies with the PHP standard recommendations (usually called "PSR") so that we can easily switch the library to another library when needed. In terms of communication injection, the library needs to comply with PSR-11, which provides "inter-container" features. Among them, the following libraries are compatible with PSR-11:

• Symfony's dependency injection

• PHP-DI

• Aura.Di

• Container (inject dependencies)

• Yii dependency injection

Deliver work through work containers

The attached vaccination library will provide a "working container" that resolves a contract in its respective implementation class. The application must rely on a working container to access all functions. For example, when we usually call WordPress functions directly:

$posts = get_posts();

...Using the utility container, we must first obtain the utility that satisfies the PostAPIInterface, and realize the function through it:

use Owner\MyApp\Contracts\PostAPIInterface;

 

// Obtain the service container, as specified by the library we use

$serviceContainer = ContainerBuilderFactory<::getInstance();

 

// The obtained service will be of class Owner\MyAppForWP\ContractImplementations\PostAPI

$postAPI = $serviceContainer->get(PostAPIInterface::class);

 

// Now we can invoke the WordPress functionality

$posts = $postAPI->get_posts<span style="color: #999999;">();

Symfony’s DependencyInjection

The Symfony DependencyInjection component is now the most popular communication library. It allows you to customize the utility container using PHP, YAML, or XML code. For example, the PostAPI configuration in YAML is as follows, defining a PostAPIInterface protocol with sufficient classes:

service:

Between Owner\App\Contracts\PostAPII:

Class: \ Owner \ MyAppForWP \ ContractImplementations \ PostAPI

Symfony's DependencyInjection also allows samples from one job to be automatically injected (or "automated") into any other job related to it. In addition, it can easily define what a class is doing. For example, consider the following YAML configuration:

services:

_defaults:

public: true

autowire: true

 

GraphQLAPI\GraphQLAPI\Registries\UserAuthorizationSchemeRegistryInterface:

class: '\GraphQLAPI\GraphQLAPI\Registries\UserAuthorizationSchemeRegistry'

 

GraphQLAPI\GraphQLAPI\Security\UserAuthorizationInterface:

class: '\GraphQLAPI\GraphQLAPI\Security\UserAuthorization'

GraphQLAPI\GraphQLAPI\Security\UserAuthorizationSchemes\:

resource: '../src/Security/UserAuthorizationSchemes/*'

This configuration describes the following:

• UserAuthorizationSchemeRegistryInterface is approved by the UserAuthorizationSchemeRegistry class

• Meet the UserAuthorizationInterface protocol through the UserAuthorization class

• All classes are implemented under the UserAuthorizationSchemes/ folder

• The job should automatically inject each other (automatic: right)

Let us see how owiring works. The User Permission class is linked to the UserAuthorizationSchemeRegistryInterface protocol:

class UserAuthorization implements UserAuthorizationInterface

{

public function __construct(

protected UserAuthorizationSchemeRegistryInterface $userAuthorizationSchemeRegistry

) {

}

 

// ...

}

Because auto: true, the DependencyInjection component will automatically obtain the required UserAuthorization connection, which is an example of UserAuthorizationSchemeRegistry.

Dema by Abstract

Reverse coding can require a lot of time and effort, so we should only do this when the benefits outweigh the costs. Here are some suggestions on how to view or book code snippets. You can use the code snippets in this article or the WordPress plugin below to accomplish this.

Gain access to tools

As mentioned earlier, running PHP-Scoper on WordPress is difficult. By dividing the WordPress code into different packages, you can directly track WordPress plugins.

Reduce tool time and cost

Compared with not running the PHPUnit test group, WordPress takes longer to start and run. Less time can also translate into less testing costs-for example, GitHub Actions will spend them based on the usage time of the GitHub host browser.

Need a lot of refactoring

Existing projects may require extensive upgrades to require architecture (link unbinding, code splitting into packages, etc.), making it difficult to remove. When the project is built from scratch, it is very convenient to extract the code.

Multi-platform code generation

By extracting 90% of the code into a package unrelated to CMS, we can generate library versions suitable for different CMS or frameworks, which only account for 10% of the total code base.

Want to know how we increased traffic by 1000%?

Join more than 20,000 people to receive my weekly newsletter, which includes a built-in WordPress plugin!

If we need to transfer a project from Drupal to WordPress, from WordPress to Laravel, or any other compilation, then only 10% of the code needs to be rewritten-an important record.

Best Practices

When designing a contract to delete our code, we can make many improvements based on the code.

Connect to PSR-12

When defining the time interval for accessing WordPress methods, we must connect to PSR-12. The purpose of the latter function is to reduce the cognitive confusion when the code is typed by different authors. Compliance with PSR-12 means that the names of WordPress features can be changed.

WordPress uses snake_case to use the function name, while PSR-12 uses camelCase. Therefore, the get_posts function will be getPosts:

interface PostAPIInterface

{

public function getPosts(array $args = null): PostInterface[]|int[];

}

…and:

class PostAPI implements PostAPIInterface

{

public function getPosts(array $args = null): PostInterface[]|int[]

{

// This var will contain WP_Post[] or int[]

$wpPosts = \get_posts($args);

 

// Rest of the code

// ...

}

}

Split Methods

The method between the two does not need to be a copy of WordPress. We can replace them when it makes sense. For example, the WordPress function get_user_by($field, $value) knows how to return a user from the database through the $field parameter, which accepts the value of "id", "ID", "slug", "email", or "login". embankment. There are several problems with this design:

• If we pass the wrong line during compilation, it will not fail

• All different types of options must accept the $value parameter, although it gives an int hope when passing "ID", it can only receive a string when passing "e-mail".

We can improve this situation by dividing the function by several:

namespace Owner\MyApp\Contracts<;

 

interface UserAPIInterface

{

public function getUserById(int $id): ?UserInterface;

public function getUserByEmail(string $email): ?UserInterface;

public function getUserBySlug(string $slug): ?UserInterface;

public function getUserByLogin(string $login): ?UserInterface;

}

The contract is resolved for WordPress like this (assuming we have Create UserWrapper and UserInterface, as described above:

namespace Owner\MyAppForWP\ContractImplementations<span style="color: #999999;">;

 

use Owner\MyApp\Contracts\UserAPIInterface;

 

class UserAPI implements UserAPIInterface

{

public function getUserById<span style="color: #999999;">(int $id): ?<span style="color: #dd4a68;">UserInterface

{

return $this->getUserByProp('id'<span style="color: #999999;">, $id);

}

 

public function getUserByEmail(string $email<span style="color: #999999;">): ?UserInterface

{

return $this->getUserByProp('email', $email);

}

 

public function getUserBySlug(string $slug): ?UserInterface

{

return $this->getUserByProp('slug', $slug);

}

 

public function getUserByLogin(string $login): ?UserInterface

{

return $this->getUserByProp('login', $login);

}

 

private function getUserByProp(string $prop, int|string $value): ?UserInterface

{

if ($user <span style="color: #9a6e3a;">= \get_user_by($prop, $value)) {

return new UserWrapper($user);

}

return null;

}

}

Removing Implementation Details from Function Signature

Features in WordPress may provide information on how to implement these features in your signature. When evaluating the function from an abstract perspective, this information can be deleted. For example, in WordPress, getting the user password by calling get_the_author_meta means that the user password is stored as a "meta" value (on the wp_usermeta table):

$userLastname = get_the_author_meta("user_lastname", $user_id);

You do not need to provide this information to the contractor. Only focus on what, not how they are interested. Therefore, the contract may have a getUserLastname method, which does not provide any information about how it is implemented:

All Atakdomain hosting plans include 24/7 support from our old WordPress developers and engineers. Chat with the same team that supports our Fortune 500 customers. Check out our plan!

interface UserAPIInterface

{

public function getUserLastname(UserWrapper $userWrapper): string;

...

}

Add Stricter Types

Some WordPress functions can accept parameters in different ways, which can cause confusion. For example, the add_query_arg function can take a key and a value:

$url = add_query_arg('id', 5, <$url);

… or an array of key => value:

$url = add_query_arg(['id' => 5], $url);

Our relationship can define a more understandable goal by dividing these functions into several different functions, each of which accepts a unique login relationship:

public function addQueryArg<(string $key, string $value, string $url);

public function addQueryArgs(array $keyValues, string $url);

Clear technical debt

The WordPress function get_posts not only returns "posts", but also returns organizations of type "page" or "custom post" and does not modify these settings. Both the post and the page are custom posts, but the page is neither a post nor a page. Therefore, implement get_posts to return the page. This behavior is a conceptual contradiction.

To be correct, get_posts should be changed to get_customposts, but it has never been modified in WordPress. This is a common problem with most long-term software, called "technical debt"-code that is problematic but never corrected because it detects broken changes.

However, when creating contracts, we have the opportunity to avoid this type of technical debt. In this case, we can create a new ModelAPIInterface interface to handle various entities, and we have developed several methods, each of which handles different types:

interface ModelAPIInterface

{

public function getPosts(array $args): array;

public function getPages(array $args): array;

public function getCustomPosts(array $args): arrayspan style="font-size: small;">;

}

In this way, the conflict will no longer exist and you will see the following results:

• getPosts only returns posts

• getPages only returns pages

• getCustomPosts returns posts and pages

Benefits of abstract code

The main benefits of submitting the application code are:

• Tools that deal with packages containing only business codes are easier and require less time (and money) to operate.

• We can use tools that do not support WordPress, such as plugins with PHP Scope.

• The packaging we produce can be automated for easy use in other applications.

• Migrating applications to other platforms has become easier.

• We can change our thinking from WordPress thinking to thinking based on our business logic.

• The contract defines the intent of the application, it will be easier to understand.

• Applications are organized in packages to create a perfect application that contains the least amount and gradually increases as needed.

• We can clear technical debt.

The problem with abstract code

The disadvantages of using valid application code are:

• Contains important work initially.

• The code becomes more popular; additional layers of code are added to achieve the same result.

• You may generate dozens of packages that then need to be managed and maintained.

• You may need a monorail to manage all packages together.

• For simple applications (reduced return stroke), the connection pins can be greatly increased.

• Code deletion will never be completed, because there is usually a general preference in the CMS architecture.

Abstract WordPress plugin options

Although it is usually wise to remove the code from the local environment before processing it, some WordPress plugins can help you achieve your abstract goal. These are our main choices.

1.WPide

The popular WPide plug-in produced by WebFactory Ltd greatly expands the functionality of the custom WordPress code editor. It is used as an abstract plugin for WordPress, allowing you to find the right code to better highlight the content that needs attention.

 WPide plugin.

WPide also has search and change functions to quickly find outdated or outdated code and replace it with updated code.

In addition, WPide also provides many additional features, including:

• Syntax and block tags

• Automatic support

• Create files and folders

• Extended file tree browser

• Access to the WordPress file API

2. The ultimate DB administrator

WPHobby's Ultimate WP DB Manager plug-in provides you with a quick way to download and install the entire database.

 The ultimate database administrator plugin.

Of course, Atakdomain users don't need plugins like this, because Atakdomain provides direct access to the database for all customers. However, if your hosting provider does not have enough databases, Ultimate DB Administrator can be used as an abstract WordPress plugin.

3. Create your own WordPress Abstract plugin

In the end, the best choice for abstraction is always to make your own supplement. This may seem important, but if your ability to directly manage WordPress core files is limited, then this provides an abstract and friendly approach.

This has obvious benefits:

• Shorten the functions in the theme file

• Protect your code through theme changes and database updates

You can learn how to create your own abstract WordPress plugin with the help of the WordPress plugin server.

 

Should we delete the code in the application? Like all things, there is no predetermined "correct answer" because it depends on the underlying project. Projects that require a lot of time to analyze using PHPUnit or PHPS can get the most benefit, but the effort required to extract it is not always worth it.

You already know everything you need to start compiling WordPress code.

Do you plan to implement this strategy in your project? If so, would you use an abstract WordPress plugin? Please leave your comments in the evaluation section!

_______________________________________

Save time, money, and improve website performance by:

• Instant help from WordPress hosting experts, 24/7.

• Cloudflare Enterprise integration.

• Reach global audiences through data centers around the world.

• Use our built-in application performance monitor for optimization.

Check our plans and contact the seller to find a plan that suits you.

Buy WordPress Hosting at Best Prices!