diff --git a/src/import/attach.php b/src/import/attach.php index 908bd16..7beab90 100644 --- a/src/import/attach.php +++ b/src/import/attach.php @@ -4,4 +4,47 @@ class attach { -} \ No newline at end of file + public function __construct() + { + return $this; + } + + + + public function image_to_post($image_id = null, $post_id = null) + { + if ($image_id == null || $post_id == null) { + return; + } + + return set_post_thumbnail($post_id, $image_id); + } + + + + + public function meta_to_post($metadata, $post_id) + { + if ($metadata == null || $post_id == null) { + return; + } + + foreach($metadata as $meta_key => $meta_value) + { + update_post_meta($post_id, $meta_key, $meta_value); + } + + return; + } + + + public function tax_to_post($tax_type, $tax_term, $post_id) + { + if (isset($tax_term) && isset($tax_type) && isset($post_id)) { + return wp_set_object_terms($post_id, $tax_term, $tax_type); + } + + return; + } + +} diff --git a/src/import/create_meta.php b/src/import/create_meta.php index 8f5bd1d..27e7c0d 100644 --- a/src/import/create_meta.php +++ b/src/import/create_meta.php @@ -7,7 +7,7 @@ class meta public $args; - public $result; + public $result = 0; public function __construct() @@ -30,7 +30,10 @@ public function add() public function result() { - return $this->result; + + return; } + + } \ No newline at end of file diff --git a/src/import/downloader.php b/src/import/downloader.php index f967a30..4ea504c 100644 --- a/src/import/downloader.php +++ b/src/import/downloader.php @@ -9,6 +9,11 @@ public function download($url = null, $post_data = array(), $alttext, $filename { if ( !$url ) return new \WP_Error('missing', "Need a valid URL to download image."); + // Does the image exist already? + if ($image_id = $this->does_image_exist($filename)) { + return $image_id; + } + require_once( ABSPATH . 'wp-admin/includes/file.php' ); $tmp = download_url( $url ); @@ -22,7 +27,7 @@ public function download($url = null, $post_data = array(), $alttext, $filename preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches); // fix file filename for query strings $url_filename = basename($matches[0]); // extract filename from url for title $url_type = wp_check_filetype($url_filename); // determine file type (ext and mime/type) - + // override filename if given, reconstruct server path if ( !empty( $filename ) ) { $filename = sanitize_file_name($filename); @@ -52,12 +57,6 @@ public function download($url = null, $post_data = array(), $alttext, $filename require_once(ABSPATH . 'wp-admin/includes/image.php'); - // Does the image exist already? - if ($image_id = $this->does_image_exist($file_array['name'])) - { - return $image_id; - } - // do the validation and storage stuff // $post_data can override the items saved to wp_posts table, like post_mime_type, guid, post_parent, post_title, post_content, post_status // @@ -82,7 +81,7 @@ public function download($url = null, $post_data = array(), $alttext, $filename public function does_image_exist($filename) { global $wpdb; - return intval( $wpdb->get_var( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%/$filename'" ) ); + return intval( $wpdb->get_var( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE '%/$filename%'" ) ); } } \ No newline at end of file diff --git a/src/import/import.php b/src/import/import.php index e7891b5..7366959 100644 --- a/src/import/import.php +++ b/src/import/import.php @@ -6,6 +6,7 @@ use \yt\import\post; use \yt\import\image; use \yt\import\meta; +use \yt\import\attach; class import { @@ -20,6 +21,8 @@ class import public $returned_ids; + public $attach; + @@ -33,6 +36,8 @@ public function __construct() $this->meta = new meta; + $this->attach = new attach; + return $this; } @@ -71,13 +76,20 @@ public function add_post($item) $this->returned_ids[$target_object] = $this->$target_object->result(); } - //$this->set_post_taxonomy($postID); + $this->combine(); return $this; } + public function combine() + { + $this->attach->image_to_post($this->returned_ids['image'], $this->returned_ids['post']); + $this->attach->meta_to_post($this->meta->args, $this->returned_ids['post']); + $this->attach->tax_to_post($this->taxonomy->taxonomy_type, $this->taxonomy->taxonomy_term, $this->returned_ids['post']); + return; + } @@ -102,14 +114,4 @@ public function add_term($taxonomy, $term, $description = '') } - public function set_post_taxonomy($postID) - { - if (isset($this->taxonomy->taxonomy_term) && isset($this->taxonomy->taxonomy_type)) { - $cat = $this->taxonomy->taxonomy_type; - $term = $this->taxonomy->taxonomy_term; - $result = wp_set_object_terms($postID, $term, $cat); - } - - return; - } } diff --git a/src/mapper/mapper_item.php b/src/mapper/mapper_item.php index 5fd65b3..a33a2d9 100644 --- a/src/mapper/mapper_item.php +++ b/src/mapper/mapper_item.php @@ -182,6 +182,13 @@ public function destination_field() public function transform_value($source_value) { + /** + * Special case - use source as string, not reference. + */ + if ($this->single_mapping['yt_mapper_transform'] == 'field_as_string'){ + return $this->single_mapping['yt_mapper_source']; + } + $transform_group = new transform_group; $transform_group->set_field($source_value); $transform_group->transform_group_to_run($this->single_mapping['yt_mapper_transform']); @@ -193,4 +200,6 @@ public function transform_value($source_value) } + + } diff --git a/src/transform/transforms/field_as_string.php b/src/transform/transforms/field_as_string.php new file mode 100644 index 0000000..9b1bf09 --- /dev/null +++ b/src/transform/transforms/field_as_string.php @@ -0,0 +1,32 @@ +input = $input; + return; + } + + public function out() + { + return (string) $this->input; + } + +} diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 773e557..b74c658 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -30,6 +30,7 @@ 'yt\\options' => $baseDir . '/src/acf/get_options.php', 'yt\\r' => $baseDir . '/src/error/r.php', 'yt\\scraper' => $baseDir . '/src/scraper.php', + 'yt\\transform\\field_as_string' => $baseDir . '/src/transform/transforms/field_as_string.php', 'yt\\transform\\none' => $baseDir . '/src/transform/transforms/none.php', 'yt\\transform\\regex_remove' => $baseDir . '/src/transform/transforms/regex_remove.php', 'yt\\transform\\string_remove' => $baseDir . '/src/transform/transforms/string_remove.php', diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 14d3ea3..112f5ea 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -31,6 +31,7 @@ class ComposerStaticInit30fd7ec9b8e2598fd2f686f91346eea7 'yt\\options' => __DIR__ . '/../..' . '/src/acf/get_options.php', 'yt\\r' => __DIR__ . '/../..' . '/src/error/r.php', 'yt\\scraper' => __DIR__ . '/../..' . '/src/scraper.php', + 'yt\\transform\\field_as_string' => __DIR__ . '/../..' . '/src/transform/transforms/field_as_string.php', 'yt\\transform\\none' => __DIR__ . '/../..' . '/src/transform/transforms/none.php', 'yt\\transform\\regex_remove' => __DIR__ . '/../..' . '/src/transform/transforms/regex_remove.php', 'yt\\transform\\string_remove' => __DIR__ . '/../..' . '/src/transform/transforms/string_remove.php',