0

Hy! In my plugin activation class i used wordpress functions (get_user_meta, update_user_meta)

<?php
/*
Plugin Name: Integração Pagarme Dokan
Description: Integrar os plugins pagarme para woocommerce e pix pagarme com o dokan
Author: Guilherme Marchesan
Version: 1.0.0
Text Domain: integracao-pagarme-dokan
*/

if(!defined('ABSPATH')){
    die('Invalid request.');
}

include "functions.php";
include "pagarme-functions.php";

class PagarmeDokan{

    private $functions;
    
    public function __construct(){
        $this->functions = new PagarmeDokanFunctions();
        $this->functions->check_pagarme_plugins();
        $sellers = $this->functions->get_sellers();
        foreach ($sellers as $sellerObj) {
            if(get_user_meta($sellerObj->ID, 'banco_saldo_disponivel', true) == null){
                update_user_meta($sellerObj->ID, "banco_saldo_disponivel", 0);
            }
            if(get_user_meta($sellerObj->ID, 'banco_saldo_futuro', true) == null){
                update_user_meta($sellerObj->ID, "banco_saldo_futuro", 0);
            } 
        }
        $this->create_options();
        flush_rewrite_rules();
    }
}

if(class_exists('PagarmeDokan')){ 
    $PagarmeDokanPlugin = new PagarmeDokan(); 
    register_activation_hook(__FILE__, array($PagarmeDokanPlugin, 'activate'));   
}

And this error showed up:

Fatal error: Uncaught Error: Call to undefined function wp_get_current_user() in /home/shotplacecom/public_html/wp-includes/capabilities.php:693 Stack trace: #0 /home/shotplacecom/public_html/wp-content/plugins/integracao-pagarme-dokan/functions.php(12): current_user_can('manage_options') #1 /home/shotplacecom/public_html/wp-content/plugins/integracao-pagarme-dokan/integracao-pagarme-dokan.php(24): PagarmeDokanFunctions->set_users_bank() #2 /home/shotplacecom/public_html/wp-content/plugins/integracao-pagarme-dokan/integracao-pagarme-dokan.php(63): PagarmeDokan->__construct() #3 /home/shotplacecom/public_html/wp-settings.php(409): include_once('/home/shotplace...') #4 /home/shotplacecom/public_html/wp-config.php(81): require_once('/home/shotplace...') #5 /home/shotplacecom/public_html/wp-load.php(50): require_once('/home/shotplace...') #6 /home/shotplacecom/public_html/wp-blog-header.php(13): require_once('/home/shotplace...') #7 /home/shotplacecom/public_html/index.php(17): require('/home/shotplace...') #8 {main} thrown in /home/shotplacecom/public_html/wp-includes/capabilities.php on line 693

I know I can use Wordpress base functions in my plugin class functions, as we can see in this question Access wordpress functions inside a plugin class, but for me its not working and I don't know why.

3
  • Where/how are you using this class? Oct 26, 2021 at 22:47
  • @JacobPeattie I edited my question with all the plugin code, please check it out. Oct 27, 2021 at 0:21
  • You're already calling a bunch of stuff in the constructor, which is not called from a hook but directly. Is this necessary? Have you considered instantiating the class only in a (later) hook?
    – kero
    Oct 27, 2021 at 7:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.