commands.php 7.45 KB
<?php

require_once( ABSPATH . 'wp-admin/includes/file.php' );

class SYNC_FILES {

    private static function cleanTerm($str) {
        return trim($str);
    }

    private static function setPostData($post_id, $extracted_data) {

        update_field('authors', $extracted_data['authors'], $post_id);
        update_field('document_year', $extracted_data['year'], $post_id);
        update_field('version', $extracted_data['version'], $post_id);
        update_field('msf_initiative', $extracted_data['initiative'], $post_id);
        update_field('document_link', $extracted_data['link'], $post_id);
        update_field('description', $extracted_data['description'], $post_id);

        update_field('wpml', $extracted_data['wmpl'], $post_id);
        update_field('exclude_from_search', 0, $post_id);
        
        $set_language_args = array(
            'element_id' => $post_id,
            'element_type'  => 'post_attachment',
            'language_code' => strtolower($extracted_data['wmpl'])
        );
        apply_filters( 'wpml_set_element_language_details', null, $set_language_args );
        
       wp_set_post_terms($post_id,implode(",",$extracted_data['tags']), 'post_tag');
       wp_set_post_terms($post_id,$extracted_data['category'], 'categories');
       wp_set_post_terms($post_id,$extracted_data['continent'], 'continent');
       wp_set_post_terms($post_id,$extracted_data['country'], 'country');
       wp_set_post_terms($post_id,$extracted_data['doc_cat'], 'document-category');
       wp_set_post_terms($post_id,$extracted_data['doc_format'], 'document-format');
       wp_set_post_terms($post_id,$extracted_data['language'], 'language');
       wp_set_post_terms($post_id,$extracted_data['office'], 'msf-office');
       wp_set_post_terms($post_id,$extracted_data['oc'], 'oc');
       wp_set_post_terms($post_id,$extracted_data['region'], 'region');

    }

    private static function extractData($data) {

        $tags = [self::cleanTerm($data[self::$COLUMN_MAP['key_1']]),self::cleanTerm($data[self::$COLUMN_MAP['key_2']])
            ,self::cleanTerm($data[self::$COLUMN_MAP['key_3']]),self::cleanTerm($data[self::$COLUMN_MAP['key_4']]),self::cleanTerm($data[self::$COLUMN_MAP['key_5']])];

        $category = self::cleanTerm($data[self::$COLUMN_MAP['cat']]);
        $continent = self::cleanTerm($data[self::$COLUMN_MAP['continent']]);
        $country = self::cleanTerm($data[self::$COLUMN_MAP['country']]);
        $doc_cat = self::cleanTerm($data[self::$COLUMN_MAP['category']]);
        $doc_format = self::cleanTerm($data[self::$COLUMN_MAP['format']]);
        $language = self::cleanTerm($data[self::$COLUMN_MAP['language']]);
        $office = self::cleanTerm($data[self::$COLUMN_MAP['office']]);
        $oc = self::cleanTerm($data[self::$COLUMN_MAP['oc']]);
        $region = self::cleanTerm($data[self::$COLUMN_MAP['region']]);
        $authors = self::cleanTerm($data[self::$COLUMN_MAP['author']]);
        $year = self::cleanTerm($data[self::$COLUMN_MAP['year']]);
        $version = self::cleanTerm($data[self::$COLUMN_MAP['version']]);
        $initiative = self::cleanTerm($data[self::$COLUMN_MAP['initiative']]);
        $link = self::cleanTerm($data[self::$COLUMN_MAP['link']]);
        $title = self::cleanTerm($data[self::$COLUMN_MAP['title']]);
        $description = self::cleanTerm($data[self::$COLUMN_MAP['description']]);
        $wmpl = self::cleanTerm($data[self::$COLUMN_MAP['wmpl']]);


        $safe_data = [
            'tags'=>$tags,
            'category'=>$category,
            'continent'=>$continent,
            'country'=>$country,
            'doc_cat'=>$doc_cat,
            'doc_format'=>$doc_format,
            'language'=>$language,
            'office'=>$office,
            'oc'=>$oc,
            'region'=>$region,
            'authors'=>$authors,
            'year'=>$year,
            'version'=>$version,
            'initiative'=>$initiative,
            'link'=>$link,
            'title'=>$title,
            'description'=>$description,
            'wmpl'=>$wmpl
        ];

        return $safe_data;

    }

    public static $COLUMN_MAP = [
        '',
        '',
        'ref_number'=>2,
        'title'=>3,
        'link'=>4,
        'author'=>5,
        'oc'=>6,
        'office'=>7,
        'initiative'=>8,
        'year'=>9,
        'format'=>10,
        'category'=>11,
        'version'=>12,
        'language'=>13,
        'continent'=>14,
        'region'=>15,
        'country'=>16,
        'multiple_countries'=>17,
        'key_1'=>18,
        'key_2'=>19,
        'key_3'=>20,
        'key_4'=>21,
        'key_5'=>22,
        'description'=>23,
        'cat'=>24,
        'wpml'=>25
    ];

    private static function createDocument($file) {

        $data = self::extractData($file);
        
        if(!post_exists($data['title'],null,null,'documents')) {
            $post = wp_insert_post([
                'post_title' => $data['title'],
                'post_status'=>'publish',
                'post_type'=>'documents'
            ]);
        } else {
            $post = get_posts([
                'post_type'=>'documents',
                'title' => $data['title']
            ]);
            if(count($post) > 0) {
                $post = $post[0];
            }
        }


        if($post) {
            self::setPostData($post->ID, $data);
        }

    }

    private static function uploadLocalFile($file) {

        $data = self::extractData($file);

        $link = $file[4];
        $title = $file[3];


        $filepath = wp_upload_dir()['basedir'] . "/document_list_files/" . $link;

        if(file_exists($filepath)) {

            if(!post_exists($title,null,null,'attachment')) {

                $wp_filetype = wp_check_filetype( $filepath, null );

                $attachment = array(
                  'post_mime_type' => $wp_filetype['type'],
                  'post_title' => $title,
                  'post_content' => '',
                  'post_status' => 'inherit'
                );
                
                $attach_id = wp_insert_attachment( $attachment, $filepath );
                require_once( ABSPATH . 'wp-admin/includes/image.php' );
                $attach_data = wp_generate_attachment_metadata( $attach_id, $filepath );
                wp_update_attachment_metadata( $attach_id, $attach_data );

            }

            self::setPostData($attach_id, $data);

        } else {
            WP_CLI::line( "FILE DOES NOT EXIST = " .  $filepath );
        }

    }

	public function sync_files() {
		// WP_CLI::line( 'Hello World!' );

        $external = [];
        $local = [];

        $store_search_file = fopen(wp_upload_dir()['basedir']."/documents_list.csv", "r+");
        $row = 1;
        
        while (($data = fgetcsv($store_search_file)) !== FALSE) {

            $is_external = false;
            $link =  $data[4];
            if(strpos($link,'http') !== false) {
                $is_external = true;
            }

            if($is_external) {
                $external[] = $data;
            } else {
                $local[] = $data;
            }

        }

        WP_CLI::line( "EXTERNAL COUNT = " .  count($external) );
        WP_CLI::line( "LOCAL COUNT = " .  count($local) );

        foreach($external as $ext) {
            self::createDocument($ext);
        }

        foreach($local as $file) {
            self::uploadLocalFile($file);
        }



	}

    

}

function sync_files_cli_register_commands() {
	WP_CLI::add_command( 'tz', 'SYNC_FILES' );
}

add_action( 'cli_init', 'sync_files_cli_register_commands' );