array( 'valid', 'taxonomies' ), * 'custom_fields' => array( 'csv_key' => 'internal_key' * 'csv_key' => array( 'internal_key' => 'key', * 'default' => 'value' ) * ), * 'tax_meta' => array( array( 'tax' => array( 'csv_key' => 'tax_key' )) * */ public function __construct($post_type = 'post', $fields, $args = '') { $this -> blog_url = get_bloginfo('url'); $this -> domain_name = $this -> get_domain_name(); $this -> post_type = $post_type; $this -> fields = $fields; //set while upload file $this -> taxonomies = $args['taxonomies']; //ad_cat $this -> tax_meta = $args['tax_meta']; // Parse Custom Fields for default values $this -> custom_fields = array(); $this -> wp_custom_fields = $this -> get_custom_fields(); $this -> wp_post_terms = wp_get_post_terms(); $custom_fields = $args['custom_fields']; foreach($custom_fields as $csv_key => $data) { if(is_array($data)) { $this -> custom_fields[$csv_key] = $data; } else { $this -> custom_fields[$csv_key] = array("internal_key" => $data, "default" => ""); } } } function init_plugin() { add_option('classi-importer_free_apikey', 'a8ef6c7f7987552a40c729ea232f9a8a'); add_action('appthemes_add_submenu_page', array(&$this, 'app_importer_menu')); } function app_importer_menu() { add_submenu_page('app-dashboard', __('Classi Ads Importer'), __('Classi Ads Importer'), 'manage_options', 'appthemes-classi-importer', array(&$this, 'get_interface')); } function get_interface() { if(!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.')); } // form HTML {{{ $this -> dispatch(); } /** * Convert a comma separated file into an associated array. * The first row should contain the array keys. */ function app_csv_to_array($file = '', $delimiter = ',') { if(!file_exists($file) || !is_readable($file)) return false; $header = NULL; $data = array(); if(false !== $handle = fopen($file, 'r')) { while(false !== $row = fgetcsv($handle, 1000, $delimiter)) { if($header) $data[] = array_combine($header, $row); else $header = $row; } fclose($handle); } return $data; } /** * Display import page title */ function header() { echo '
' . __('NOTE: It will create new Category if there is no \'ad_cat\' before the import.', 'appthemes') . '
'; echo '' . __('NOTE: It will create new User if there is no \'user_email\' before the import.', 'appthemes') . '
'; echo '' . __('Sorry, there has been an error.', 'appthemes') . '
';
echo esc_html($file['error']) . '
Error.
', 'appthemes'), home_url()); echo ''; } } /** * */ function process($file) { $rows = $this->app_csv_to_array($file); foreach($rows as $row) { $post['post_title'] = sanitize_text_field($row['post_title']); $post['post_content'] = sanitize_text_field($row['post_content']); $post['post_status'] = sanitize_text_field($row['post_status']); $post['post_type'] = 'ad_listing'; if(isset($row['user_email'])) { $user_id = username_exists($row['user_email']); if(!$user_id) { $random_password = wp_generate_password(12, false); $user_id = wp_create_user($row['user_email'], $random_password, $row['user_email']); } } unset($row['user_email']); $post['post_author'] = $user_id; // check if tax exists term_exists( $term, $taxonomy, $parent ) wp_insert_term( $term, $taxonomy, $args = array() ); if(!($term = term_exists(sanitize_text_field($row['ad_cat']), 'ad_cat'))) $term = wp_insert_term(sanitize_text_field($row['ad_cat']), 'ad_cat'); //'tax_input' => [ array( 'taxonomy_name' => array( 'term', 'term2', 'term3' ) ) ] // support for custom taxonomies. //$post['tax_input'] = array( 'ad_cat' => array( sanitize_text_field($row['ad_cat'])) ); $post_id = wp_insert_post($post); //wp_set_object_terms( $object_id, $terms, $taxonomy, $append ); wp_set_object_terms( $post_id, array(intval($term['term_taxonomy_id'])), 'ad_cat'); add_post_meta($post_id, 'cp_sys_expire_date', date('Y-m-d H:i:s', time()+30*24*60*60), true); //print_r($post_id); foreach($row as $key => $val) { if(strstr($key, 'cp_')) { // /add_post_meta($post_id, $meta_key, $meta_value, $unique); add_post_meta($post_id, $key, sanitize_text_field($val), true); } } } return true; } function get_domain_name() { $host = 'nodomain'; preg_match('@^(?:http://)?([^/]+)@i', $this -> blog_url, $matches); $host = $matches[1]; return $host; } }?>