WordPress Hooks不设置构造函数数据

x6h2sr28  于 8个月前  发布在  WordPress
关注(0)|答案(1)|浏览(97)

我正在做一个日志sdk。我在插件主文件中使用的代码是

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');
        }

字符串
然后是我的logger类,我使用一个构造函数来获取从插件主文件中的类初始化记录的数据。在构造函数中,我使用一个调用所有钩子的函数(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'));
    }


现在的主要问题是,在下面的功能

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';
            }
        }
    }


插件的product_data没有出现,而是出现了包含sdk的列表中最后一个插件的数据。
我希望product_data的数据应该出现在set_logs_schedule()和deactivation_model()中。此外,我是wordpress插件开发的新手,所以这就是为什么我在挣扎。

ct3nt3jp

ct3nt3jp1#

当你刚开始在php中创建类时,你应该避免使用self::,因为它引用了静态类变量和方法。它们的行为方式是复杂和不直观的。相反,使用示例变量。也就是说,比如

$this->product_data

字符串
而不是

self::$product_data


你的代码在做什么的时候会更容易预测。
而且,使用一个有代码格式化和错误检测功能的好IDE。代码的可读性很重要。

相关问题