Trying to get logged-in user data inside php class

I am trying to get the current logged-in user data and use this in the parent class(I need it in several functions).

In the example below I can get the userID in the first function but it will give no output inside the next function.

Read More

This is my first wordpress plugin and the first time that I work with OOP, so who can help me out with this thing.

<?php

class WP_PM{

    public $currentId;

    public function __construct(){

        $this->addActions();
        $this->showId();
    }

    private function addActions(){
        add_action('wp_loaded', array(&$this, 'getId'));
    }

    public function getId(){
        global $current_user;

        $this->currentId = $current_user->ID;

        //$this->currentId = 'hello';//we have an output

        echo $this->currentId;//we have an output

        return $this->currentId;
    }

    public function showId(){
        echo $this->currentId;//no output
            echo $this->getId();//no output
    }

}

register_activation_hook(__FILE__, array('WP_PM', 'activate'));
register_deactivation_hook(__FILE__, array('WP_PM', 'deactivate'));

$wp_pm = new WP_PM();
?>

// new example based on G.M solution(still not working -> triggers wp error and no logged in text)

class WP_PM_Extend extends WP_User {

    function getID() {
        return $this->ID;
    }

}

class WP_PM{

    protected $user;

    public function __construct(WP_PM_Extend $user = NULL){

        if (!is_null( $user ) && $user->exists()){
            $this->user = $user;
        }

        $this->getUser();

    }

    public function getUser() {
        return $this->user;
    }

}


function getWPPM() {

    if(!did_action('wp_loaded')){
     $msg = 'Please call getCurrentUser after wp_loaded is fired.';
    return new WP_Error('to_early_for_user', $msg);
    }

    static $wp_powertour_core = NULL;

    if(is_null( $wp_powertour_core)){
        $wp_pM = new WP_PM( new WP_PM_Extend( get_current_user_id() ) );
    }

    return $wp_powertour_core;
}

function getCurrentUser(){

  $wppm = getWPPM();

    if(is_wp_error($wppm)){
        return $wppm;
     }

    $user = $wppm->getUser();

    if($user instanceof WP_PM_Extend){ 
        return $user;
    }
}

add_action( 'wp_loaded', 'getCurrentUser');

$current_user = getCurrentUser();
if($current_user instanceof WP_PM_Extend){
    echo 'Current user ID: ' . $current_user->getID();
}else{
    echo 'No one logged in';
}

Related posts

3 comments

  1. Once you said is you first time using OOP, I want to say: stop using & before $this: PHP4 died long time ago.

    Second tip, don’t use global variables, if you can. I know that function like wp_get_current_user use global variable internally, but I hope that in future it will not be so anymore, however not seeing that global word in my code, make me fill a little better.

    Third tip, try to make your plguin more solid using type hinting.

    Here I post how I had wrote that code.

    Firts of all I’d wrote 2 classes, probably in 2 different files named in same way of class:

    class WP_PM_User extends WP_User {
    
      function getID() {
        return $this->ID;
      }
    
    }
    
    class WP_PM {
    
      protected $user;
    
      function __construct ( WP_PM_User $user = NULL) {
        if ( ! is_null( $user ) && $user->exists() ) $this->user = $user;
      }
    
      function getUser() {
        return $this->user;
      }
    
    }
    

    After that, somewhere in plugin (maybe in main plugin file) I’d write two function like this:

    function getWPPM() {
    
      if ( ! did_action('wp_loaded') ) {
        $msg = 'Please call getCurrentUser after wp_loaded is fired.';
        return new WP_Error( 'to_early_for_user', $msg );
      }
    
      static $wp_pm = NULL;
    
      if ( is_null( $wp_pm ) ) {
        $wp_pm = new WP_PM( new WP_PM_User( get_current_user_id() ) );
      }
    
      return $wp_pm;
    }
    
    
    function getCurrentUser() {
    
      $wppm = getWPPM();
    
      if ( is_wp_error( $wppm ) ) return $wppm;
    
      $user = $wppm->getUser();
    
      if ( $user instanceof WP_PM_User ) return $user;
    }
    
    add_action( 'wp_loaded', 'getCurrentUser' );
    

    Doing so, everywhere in your plugin you can call getCurrentUser and retrieve the current user object, and if you want the ID, you can call getID() on the user returned by getCurrentUser.

    Usage example:

    add_action( 'wp_loaded', function() {
    
      $current_user = getCurrentUser();
      if ( $current_user instanceof WP_PM_User ) {
        echo 'Current user ID: ' . $current_user->getID();
      } else {
        echo 'No one logged in';
      }
    
    }, 30 );
    
  2. You are firing both $this->getId(); and $this->showId(); in the constructor. I am pretty sure that that is the problem.

    If this is a plugin, which it looks like it is since you are using activation hooks, you should be able to do this:

    public function getId(){
        global $current_user;
        get_currentuserinfo();
    
        $this->currentId = $current_user->ID;
    
        //$this->currentId = 'hello';//we have an output
    
        echo $this->currentId;//we have an output
    
        return $this->currentId;
    }
    

    And have things work after that.

    If you instantiate in other contexts, you need to be careful what hook it instantiates on. Too early and neither $current_user nor get_currentuserinfo() will be defined.

    For example,

    add_action(
      'muplugins_loaded',
      function() {
        global $wp_pm;
        $wp_pm = new WP_PM();
      }
    );
    

    … will give you a notice about an undefined function– get_currentuserinfo().

  3. You are definitely loading your class instance early.

    First solution could be loading the class this way –

    add_action( 'init', 'load_wp_pm' );
    function load_wp_pm(){
        global $wp_pm;
        $wp_pm = new WP_PM();
    }
    

    Another way it can be accomplished by modifying __contruct and adding another method –

    // modified the construct method
    public function __construct(){
        add_action( 'init', array( get_class(), '_init' ) );
    }
    
    // getting user id & setting the $currentId variable and 
    public function _init(){
        $this->addActions();
        $this->showId();
    }
    

    This would probably show you some output.

Comments are closed.