Why using Trait to transform a big Class in multiple parts

During the upgrade (and upgrade to the WP Code Standards) of a php source code sometimes of more than 10 years old, the file problem of a class of nearly 10,000 lines arose. Indeed, the phpcs process is very slow and it can be difficult to group the added functions over the years. Subclassing or specific class with new names is not easy and requires a lot of changes in the files. Searching through the docs and forums, few people cite the approach with “Trait” that look like a very simple php class.

Take the case of a method set that manages a page of admin settings and related functions in the meta boxes.

We create a file, the Trait is inside, we copy these said functions. No renaming, the pseudo variable $ this remains operative …

  • The call to the Trait is at the beginning of the class that is reorganized with “use”.

So it remains to set up the file containing the Trait (preferably only one per file)

It is required before the class containing the “use” otherwise a fatal error is caused.

And now, part by part, it is easier to change the code obviously on the admin side.

Cet article contient trois blocks en chaque langue

dans les trois blocs, trois textes dans chaque langue :

the block is edited in english.

Ce bloc paragraphe est en français.

Dieser Absatzblock ist in deutscher Sprache.

Si la langue secondaire est cochée, Search trouvera cet article ! ( une fonctionnalité unique de la dernière version du plugin xili-language ! ) Après avoir édité cet article complexe, il est facile de créer et d’éditer ceux dans d’autres langues avec le tableau de bord du plugin (meta-box).

This article contains three blocks in each language

in the three block below, three texts in each language :

the block is edited in english.

Ce bloc paragraphe est en français.

Dieser Absatzblock ist in deutscher Sprache.

If secondary language are checked, Search will find this article ! (an unique features of newest version of xili-language plugin ! After editing this complex article, it is easy to create and edit those in other languages with plugin dashboard (meta box).

What new with xili-language 2.23.11 ?

The latest version includes a better management of the metabox of translations in the new Gutenberg universe which considerably upsets the interface and its mode of dialogue with the database.

Of course, we can easily make columns (for example).

But, for the developer, we must reconsider all the interfaces that were added to assist the publisher, the editor.

Can not add a simple “input” with few lines of php:

Strong knowledge of JS (reactive) is needed. In addition, it is first of all a minimum block that must be added.

Example: it was easy to add the check box for the permalink to be adapted according to the modified title of the translated article. In classic mode, it was not far from the update button, put online. Now, this is no longer possible, so it is under the list of translations. Like the other checkboxes in the list, the gutenberg mode, detects a change (check, uncheck, …) and then triggers the process js, json and updates the contents (and links between the various translations).

checkbox is on bottom left…

Why coding with WP Coding Standards ?

When a code source is sometime more than ten years old, it is now time to rewrite it with good practices.

The first step is to use efficient tools. The result speaks for itself with these two snapshots :

php w/o wpcs
php w/o wpcs

php-with-format
php-with-format

Red points on first image indicate formatting errors !

I let you guess the mass of work and checks when there are nearly 30,000 lines in 4 plugins!

Where is wiki xili ?

The xiki wiki is for the moment suspended because its content had become obsolete given the evolution of WP and the xili-language trilogy.

PHP 7.2 : how to replace DEPRECATED create_function to pass a var ?

create_function is now obsolete with PHP 7.2. How to rewrite it with anonymous function to pass a var in add_action() at end of a function calculating this var ? In this case, ‘admin_notices’ action display a message when the theme is activated.

Replace

add_action( 'admin_notices', create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );

by

add_action(
    'admin_notices',
    function() use ( &$msg ) {
       echo $msg;
    }
);

Note that var used by references ( &$msg ) in function.
See the functions.php to this theme in GitHub ( line 148 ).

Enjoy