Skip to content

Series of fatal errors: missing get_default_import_options method, protected constructor, too few arguments passed to import method #5

@lgenter

Description

@lgenter

Hey team,

I'm attempting to implement a variation of this example add-on for RankMath fields. I've run into a series of fatal errors.

Error 1: On step 3, I get the same error about missing the get_default_import_options method mentioned in the previously closed issue. I added the method just to get around the error, and that worked, but obviously it would be good to have clearer documentation on this point as I haven't looked under the hood of the API.

Error 2: When running the import, it seems that the importer is trying to instantiate the add-on's class, but it can't because the constructor is protected. I'm sure I'm missing something here, but this leaves me confused. Here's the error:

PHP Fatal error: Uncaught Error: Call to protected WP_All_Import_RankMath_Add_On::__construct() from scope PMXI_Import_Record in {...plugins-path}/wp-all-import-pro/models/import/record.php:1650

Error 3: Not ideal, but I temporarily made the constructor public to test further, and got the following error:

PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_All_Import_RankMath_Add_On::import(), 1 passed in {...plugins-path}/wp-all-import-pro/models/import/record.php on line 3170 and exactly 3 expected in {...plugins-path}/wp-all-import-rankmath-add-on/wp-all-import-rankmath-add-on.php:79

Would greatly appreciate input here or, if applicable, a fix in the example code to reflect any updates.

I'm including my plugin's code just in case it's a me problem. Running WP All Import Pro 4.9.8 on WP 6.7.1.

Thanks in advance!

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

final class WP_All_Import_RankMath_Add_On {
    protected static $instance;
    protected $add_on;
    protected $addon_name = 'RankMath Add-On'; // Define the name of your add-on
    protected $addon_slug = 'wp_all_import_rankmath_add_on'; // Define a unique add-on slug

    // Singleton pattern to ensure only one instance of the add-on is loaded
    public static function get_instance() {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    // Constructor to add the init hook
    protected function __construct() {
        add_action('init', array($this, 'init'));
    }

    // Add static keyword and make it public
    public static function get_default_import_options() {
        return [
            'rankmath_title' => '',
            'rankmath_description' => '',
            'rankmath_focus_keyword' => '',
        ];
    }

    public function init() {
        // Check for required classes and display an admin notice if not found
        if (!class_exists('PMXI_Plugin') || !class_exists('PMXI_RapidAddon')) {
            add_action('admin_notices', function() {
                echo 'The RankMath Add-On requires WP All Import Pro to be installed and active.';
            });
            return;
        }

        // Include the 'is_plugin_active' function if it doesn't exist
        if (!function_exists('is_plugin_active')) {
            include_once(ABSPATH . 'wp-admin/includes/plugin.php');
        }

        // Check if the RankMath plugin is active
        if (!is_plugin_active('seo-by-rank-math/rank-math.php')) {
            add_action('admin_notices', function() {
                echo 'The RankMath Add-On requires the RankMath plugin to be installed and active.';
            });
            return;
        }

        // Initialize the add-on with its name and slug
        $this->add_on = new PMXI_RapidAddon($this->addon_name, $this->addon_slug);
        $this->wpai_setup_fields(); // Add fields to the add-on
        $this->add_on->set_import_function([$this, 'import']); // Set the import function
        $this->add_on->run(); // Run the add-on
    }

    // Define the fields for the import template
    public function wpai_setup_fields() {
        $this->add_on->add_field('rankmath_title', 'RankMath Meta Title', 'text');
        $this->add_on->add_field('rankmath_description', 'RankMath Meta Description', 'text');
        $this->add_on->add_field('rankmath_focus_keyword', 'RankMath Meta Keywords', 'text');
    }

    // Import function to handle the import of RankMath SEO data
    public function import($post_id, $data, $import_options) {
        $fields = ['rankmath_title', 'rankmath_description', 'rankmath_focus_keyword'];
        foreach ($fields as $field) {
            // Check if the field can be updated
            if (empty($data['ID']) || $this->add_on->can_update_meta($field, $import_options)) {
                // Update the custom field with the imported data
                update_post_meta($post_id, $field, $data[$field]);
            }
        }
    }
}

// Initialize the add-on
WP_All_Import_RankMath_Add_On::get_instance();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions