Skip to content

Commit

Permalink
Downloader working. initial all-working v2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
IORoot committed Apr 28, 2020
1 parent 6d004b3 commit f81b87f
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 22 deletions.
45 changes: 44 additions & 1 deletion src/import/attach.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,47 @@

class attach
{
}
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;
}

}
7 changes: 5 additions & 2 deletions src/import/create_meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class meta

public $args;

public $result;
public $result = 0;


public function __construct()
Expand All @@ -30,7 +30,10 @@ public function add()

public function result()
{
return $this->result;

return;
}



}
15 changes: 7 additions & 8 deletions src/import/downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -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);
Expand Down Expand Up @@ -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
//
Expand All @@ -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%'" ) );
}

}
24 changes: 13 additions & 11 deletions src/import/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use \yt\import\post;
use \yt\import\image;
use \yt\import\meta;
use \yt\import\attach;

class import
{
Expand All @@ -20,6 +21,8 @@ class import

public $returned_ids;

public $attach;




Expand All @@ -33,6 +36,8 @@ public function __construct()

$this->meta = new meta;

$this->attach = new attach;

return $this;
}

Expand Down Expand Up @@ -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;
}



Expand All @@ -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;
}
}
9 changes: 9 additions & 0 deletions src/mapper/mapper_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -193,4 +200,6 @@ public function transform_value($source_value)
}




}
32 changes: 32 additions & 0 deletions src/transform/transforms/field_as_string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace yt\transform;

use yt\interfaces\transformInterface;

class field_as_string implements transformInterface
{

public $description = "Use value of field source as a literal string rather than a reference.";

public $parameters = 'None';

public $input;

public function config($config)
{
return;
}

public function in($input)
{
$this->input = $input;
return;
}

public function out()
{
return (string) $this->input;
}

}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit f81b87f

Please sign in to comment.