plugins - Action or Filter Hook for a Custom Importer

I'm trying to write a custom bulk importer for store locations but I am struggling with which action hook to use.I

I'm trying to write a custom bulk importer for store locations but I am struggling with which action hook to use.

I have written it and tested it on a clean WordPress installation and it works fine there but when I try to use it alongside WooCommerce I get an error on every admin page.

I've traced the fault back and I think it is being caused by the filter hook I am trying to use.

add_filter('pre_update_option',[$this,'importLocations'],10,3);

My import method looks like this:

public function importLocations($newvalue,$option,$oldvalue){
    if('stockist_settings'==$option && !empty($newvalue['fileimport'])){

        $file=get_attached_file($newvalue['fileimport'][0]);

        $locations=$this->parseCSVToArray($file);

        if(!empty($locations)){
            $prefix = $this->getAttributes()['slug']."_";
            foreach($locations as $location){

                // Checks that the longitude and latitude are filled out
                if(empty($location['Lat']) || empty($location['Lng'])){
                    $geocoder=new Geocoder();
                    $lnglat = $geocoder->geocodePostcode($location['Postcode']);
                    $location['Lat']=$lnglat['latitude'];
                    $location['Lng']=$lnglat['longitude'];
                }

                $social=$this->parseSocialMedia($location);

                $v = new Valitron\Validator($location);
                $v->rules($this->setImportRules());
                if($v->validate()) {

                    $payload=[
                        'post_title'    =>  $location['CompanyName'],
                        'post_type'     =>  $this->getAttributes()['slug'],
                        'post_status'   =>  'publish',
                        'meta_input'    =>  [
                            $prefix.'address1'      =>  $location['Address1'],
                            $prefix.'address2'      =>  $location['Address2'],
                            $prefix.'address3'      =>  $location['Address3'],
                            $prefix.'city'          =>  $location['City'],
                            $prefix.'postcode'      =>  $location['Postcode'],
                            $prefix.'phonenumber'   =>  $location['PhoneNumber'],
                            $prefix.'website'       =>  $location['Website'],
                            $prefix.'email'         =>  $location['Email'],
                            $prefix.'longitude'     =>  $location['Lng'],
                            $prefix.'latitude'      =>  $location['Lat'],
                            $prefix.'socialrepeat_group'=>$social
                        ]
                    ];

                    $id=wp_insert_post($payload);

                } else {
                    // Errors
                    print_r($v->errors());
                }
            }

            wp_redirect(admin_url('/edit.php?post_type=stockists'),301);
        }
    }
}

Is there a better filter hook this I should use or am I approaching the bulk import from the wrong direction entirely?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745077925a4609944.html

相关推荐

  • plugins - Action or Filter Hook for a Custom Importer

    I'm trying to write a custom bulk importer for store locations but I am struggling with which action hook to use.I

    16小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信