Okay
  Print

Switching to an Object Oriented architecture, changes for child themes

Important

This article is outdated. Woffice has a new documentation here: 

https://alkaweb.atlassian.net/wiki/spaces/WOF/overview

If anything is missing, let us know and we will improve it. It was designed to be searchable, more organized and more complete. 

------



Starting at version 2.1.3 (https://woffice.io/changelog/) Woffice's code design will be focus on an object oriented design. 

That won't change anything to any user who isn't using a child theme. But if you do and if you're overriding some functions from the Woffice parent theme, then this article is for you. 

What does the O.O.P architecture change means? 

It means that all functions related to the Woffice core (not extensions) have been organized through several PHP classes. 

Which also means : 

  • Better code organization
  • All functions are documented 
  • Code has been optimized and can now be loaded in the right place at the right moment
  • It'll be much easier for you to extend Woffice features
  • It'll be much easier for us to update Woffice with bigger features

Note that the functions related to woffice/inc/frontend.php will be handled into version 2.1.4.

What's the issue then? 

If you've been using before this version and have been overriding some Woffice functions using a child theme (using this way : https://code.tutsplus.com/tutorials/a-guide-to-overriding-parent-theme-functions-in-your-child-theme--cms-22623). Then, if it's parent function has been moved to a class your overriding won't work anymore. 

There are many chances that your function hasn't been moved to a class, but if you see that you change is no longer working then you'll have to make a quick change. 

Example :  As you know, Wordpress deactivate the admin bar for any user who isn't an administrator. That function is very liked by our customers so we haven't created any option about it. Let's imagine that you want to remove it and show the admin bar for any Wordpress user. 

In the old way the functions was called in Woffice by : 

if(!function_exists('remove_admin_bar')){
    function remove_admin_bar() {
        if ( ! current_user_can( 'administrator' ) && ! is_admin() ) {
            show_admin_bar( false );
        }
    }
}

So if you wanted to remove that feature you'd have to copy/past it to your woffice-child/functions.php doing :

function remove_admin_bar() {
    return;
}

That'd have override the parent's function and removed that feature (could have been changed or improved by the way, this is just an example). 

Now, the function is declared within a class. So that won't work anymore because the function will be called anyway. 

Thus, we need to create a new class to extend the parent's class containing that function (aka method).

Following my example, the remove_admin_bar is now declared under woffice/inc/Woffice_Setup.php. 

We'll give you any function location through the support here, just ask us :)

So we'll just do : 

// We add our code after Woffice classes have been loaded : 
add_action('after_woffice_loaded', function() {
    // We declare a new class which extends the parent one :
    class Woffice_Setup_Child extends Woffice_Setup
    {
        // Same change and same name as the parent class
        public function remove_admin_bar() {
            return;
        }
    }
    // We start it :
    new Woffice_Setup_Child();
});

Please let us know if you need anymore detail, we'll be happy to help ! 

You can also have a look on the following links for more details : 

http://php.net/manual/en/language.oop5.inheritance.php

https://code.tutsplus.com/articles/object-oriented-programming-in-wordpress-inheritance-ii--cms-21457