I am making a logging sdk. The code I used in my plugin main file where the sdk starts is
function rpt_wpb()
{
global $rpt_wpb;
if (!isset($rpt_wpb)) {
// Include SDK.
// print_r(RELATED_POSTS_THUMBNAILS_PLUGIN_DIR . '/lib/wpb-sdk/require.php');exit;
require_once RELATED_POSTS_THUMBNAILS_PLUGIN_DIR . '/lib/wpb-sdk/require.php';
$rpt_wpb = new Logger(
array(
'id' => 7,
'name' => 'Related Posts Thumbnails Plugin for WordPress',
'slug' => 'related-posts-thumbnails',
'type' => 'plugin',
'path' => __FILE__,
'version' => RELATED_POSTS_THUMBNAILS_VERSION,
'public_key' => '7e240de74fb1ed08fa08d38063f6a691462a815',
'is_premium' => false,
'has_addons' => false,
'has_paid_plans' => false,
'menu' => array()
)
);
}
return $rpt_wpb;
}
// Init .
rpt_wpb();
do_action('rpt_wpb_loaded');
}
Then is my logger class where I am using a constructor to get the data that is logged from the class initialization in plugin main file. And inside the constructor I am using a function that call up all the hooks (dynamic_init()).
public function __construct($product_data)
{
// print_r($product_data);exit;
self::$product_data = $product_data;
// echo "<pre>";print_r(self::$product_data);exit;
$this->dynamic_init();
}
/**
* Call wp hooks to initialize logger actions.
*
* @return void
*/
public function dynamic_init()
{
// $product_data = self::$product_data;
// echo "<pre> Product Data: ";print_r($product_data);exit;
add_action('init', array($this, 'set_logs_schedule'));
add_action('wpb_logger_cron_' . self::$product_data['slug'], array($this, 'log_plugin'));
add_action('admin_footer', array($this, 'deactivation_model'));
add_action('wp_ajax_wpb_sdk_' . self::$product_data['slug'] . '_deactivation', array($this, 'ajax_deactivation'));
register_activation_hook(self::$product_data['path'], array(__CLASS__, 'log_activation'));
register_deactivation_hook(self::$product_data['path'], array(__CLASS__, 'product_deactivation'));
register_uninstall_hook(self::$product_data['path'], array(__CLASS__, 'log_uninstallation'));
}
Now the main issue is that in below functions
public function set_logs_schedule()
{
// print_r(self::$product_data['slug']);exit;
if (!wp_next_scheduled('wpb_logger_cron_' . self::$product_data['slug'])) {
wp_schedule_event(time(), 'weekly', 'wpb_logger_cron_' . self::$product_data['slug']);
}
}
/**
* Add deactivation model.
*
* @return void
*/
public function deactivation_model()
{
// print_r((self::$product_data['slug']));exit;
if (function_exists('get_current_screen')) {
$screen = get_current_screen();
if ('plugins.php' === $screen->parent_file) {
$product_slug = self::$product_data['slug'];
$product_name = self::$product_data['name'];
$has_pro_version = (in_array($product_slug, self::$has_pro_version)) ? true : false;
include dirname(__DIR__) . '/views/wpb-sdk-deactivate-form.php';
}
}
}
The product_data of the plugin is not coming instead the data of last plugin in the list that contains sdk comes up.
I expect that the data of product_data should come up in set_logs_schedule() and deactivation_model(). Moreover I am new in wordpress plugin development so that's why I am struggling.
When you're new to creating classes in php you should avoid using self::
because it refers to static class variables and methods. The way they behave is intricate and non-intuitive. Instead use instance variables. That is, say things like
$this->product_data
rather than
self::$product_data
and your code will be more predictable in what it does.
And, use a decent IDE with code formatting and error detection. Code readability counts for a lot.