0

I'm new to OOPs. I'm building a Plugin and I started with the "Wordpress Plugin Boilerplate with Namespace and AutoLoader Support."

I've even successfully added a section for a RESTful interface that I got working. However I have a a few functions that are common between Admin, the Front end and my REST interface. It would seem reasonable to put those common functions in the /inc/common folder. But I can not get it working.

Perhaps it is clear to a more experienced OOPs programmer, but I have found no guidance on the web. How should these functions/menthods? be packaged in /inc/common and how should they be called?

Plugin has folders:

    /inc/admin -contains class admin
    /inc/frontend -contains class frontend
    /inc/libraries -contains class autoloader
    /inc/core - contains class activator/deactivator/int/loader 
    /inc/common - empty folder
    /inc/rest (I added) 

I have tried just putting functions in a file in /int/common/common.php :

    function common(){
       do something
     }

the including them from class admin/frontend/rest - that does not work. I have tried wrapping the functions in a class common:

    class common{
       public function common(){
        do something
       }
      } 

no luck there. I've gotten it so hosed up I had to restore from a working back up to got back to where I was last week so I load my code attempt. Some clue as to how to approach this is what I am looking for because clearly I'm missing something major here.

As best as I can recreate what I had:

Function in /inc/rest/class-rest

    namespace PGC_Signoffs\Inc\Rest;
    class Rest {
        public function glider_club_update_signoff( \WP_REST_Request  $request) {
         include_once plugin_dir_path( __DIR__ ) . 'common/common.php';

         $date_expire = $common_function($a,$b,$c); 
         }
    }   

Function in inc/common/common.php

    public common_function( $a, $b, c){
        // function to calculate the expire date. 
       return($start_date); 
    }
  • I used a similar (if not the same, I can't remember) boilerplate a few months ago, and as far as I can tell being functions shared between a restricted area (admin) and a public one (REST, frontend) you should treat them as frontend functions, and hook them to admin hooks in the Init() class. But please tell us more: provide a minimal code example, explain us how you tried and cannot make it work, explain to us the project skeleton, etc. – Fabrizio Mele Feb 12 '19 at 21:40
  • There's no code in your question, how are the classes inside inc/common being loaded at the moment? Are you loading them? Or do you expect that if you create a class in that folder it will just load auto-magically? Where do you create those objects? – Tom J Nowell Feb 12 '19 at 22:01
  • (this is the boilerplate he's talking about) – Fabrizio Mele Feb 12 '19 at 22:02
  • I'm not trying to hook directly to these functions. These are common functions used by functions that are hooked in both admin/frontend and REST. – dsj Feb 12 '19 at 22:22
  • a couple of notes: $common_function($a,$b,$c) is wrong, there is a $ too much before the function name. While this could work, I suggest you wrap that common_function inside a class and autoload it, look at my answer below – Fabrizio Mele Feb 12 '19 at 22:41
0

That boilerplate uses autoloading, take advantage of it! I understand it's a bit overwhelming but with some training you will realize that autoloading and OOP are a superlative productivity boost.

Inside inc/common create your class (for example, class-commonclass.php)

<?php

namespace PGC_Signoffs\Inc\Common; \\declare the namespace for autoloading

class CommonClass {

  public function commonFunction($a,$b,$c){ //could also be 'public static function'
    //do your magic
    return $start_date;
  }

}

Note: the functions inside CommonClass could be static, it depends on what you want to do with the class. If it's just a collection of functions go with static.

Then instance the class inside other classes, for example Admin:

<?php
namespace PGC_Signoffs\Inc\Rest;

use PGC_Signoffs\Inc\Common\CommonClass; //declare you will be using CommonClass from the Inc\Common namespace

class Rest{
  ...

  public function glider_club_update_signoff( \WP_REST_Request  $request) {

    $commonClass = new CommonClass();
    $date_expire = $commonClass->commonFunction($a,$b,$c);
    //or, if you went with a static function:
    $date_expire = CommonClass::commonFunction($a,$b,$c);
  }
}

Here's the gist

  • I had pieces of this, but was not able to put it all together. I have my admin class broken into several sub-classes. I thought including "use PGC_Signoff\Inc|Common\CommonCass in the file where Class Admin is defined. But I had to added to the file where a class that extends Admin is (and uses the common function. – dsj Feb 13 '19 at 2:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.