2017 © Pedro Peláez
 

wordpress-muplugin wp-fast-simple-import

Import things in WordPress made easy for developers.

image

sourcerer-mike/wp-fast-simple-import

Import things in WordPress made easy for developers.

  • Friday, December 9, 2016
  • by sourcerer-mike
  • Repository
  • 0 Watchers
  • 0 Stars
  • 34 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Easy import for WordPress

Writing an import couldn't be easier. Just fetch data and forward it to WP-FSI:, (*1)

$query = 'SELECT
            uid   AS _import_uid,
            title AS name
          FROM `typo3-db`.categories
          WHERE deleted = 0';

foreach ( fsi_query( $query ) as $item ) {
    fsi_term_import( $item['name'], 'my_taxonomy' );
}

Simple as that for posts, terms and attachments. Blazing fast thanks to yields which WordPress is not capable of., (*2)

Features

  • Posts
    • Import Posts with fsi_import_post.
    • fsi_post_add_term creates terms or just adds them if they exist already.
  • Media
    • fsi_import_thumbnail imports an image from filesystem or URL.
  • Term
    • fsi_term_import creates a term or just updates if it already exists.
    • fsi_term_meta_update eats an array and applies it as new term meta.
    • fsi_term_meta_replace like above but with deletion of all old data.

And some neat helpers:, (*3)

  • Use fsi_query if you want faster imports.
  • Do fsi_enable_all_caps() to supercharge your import and give it all kind of capabilities.
  • Or fsi_enable_caps for a subset of caps.

Mapping

There is a helper for mapping included. Basically it is an array with some magic:, (*4)

  • The key is where the data should go.
  • The value is where the data comes from.
  • So to speak data flows from the right side of [ 'target_field' => 'source_field' ] to the left

So it is like:, (*5)

$mapping = new WP_FSI\Mapping();

$mapping['target_column_name'] = 'source_column_name';
$mapping['_import_uid']        = 'uid';
$mapping['post_title']         = 'subject';
$mapping['post_excerpt']       = 'This is no field of the source, so the string / value will be stored.';
$mapping['i_am_meta']          = 42; // Also no field of the source? Then all those meta_fields " will have the value 42.

// And for now it is very easy piping all through.
$some_data_source = fetched_from_somewhere();
$post_data = $mapping( $some_data_source );
fsi_import_post( $post_data );

Callables can transform data on the value side. It receives the actual mapping (first arg), the source data (second arg) and the so far resulting target data (third arg). What you return is what will be stored in the target:, (*6)

$mapping = new WP_FSI\Mapping();

$mapping['post_excerpt'] = function( $mapping_object, $source_data, $target_data ) {

    // The FIRST ARGUMENT is the mapping itself so that you can delegate.
    if ( is_callable( $mapping_object['some_callable'] ) ) {
        return call_user_func_array( $mapping_object['some_callable'], func_get_args() );
    }

    // The SECOND ARGUMENT is where everything came from.
    if ( 'dog' == $source_data['animal'] ) {
        return 'Such fast. Very simple. Much wow!';
    }

    // The THIRD ARGUMENT is the target data that you still can manipulate.
    // It is an array object so the reference is given by default - nice, huh? ;)
    if ( 'cat' == $source_data['animal'] ) {
        shuffle( $target_data );
        $target_data['post_title'] = 'meow meow!';
    }

    // or imagine sub queries here, data manipulation and more
    return 'Last seen on ' . date( 'Y-m-d', $source_data['timestamp'] );
}

// Still the same and easy.
$some_data_source = fetched_from_somewhere();
$post_data = $mapping( $some_data_source );
fsi_import_post( $post_data );

Real life examples

Import tt_news from Typo

Here I had some typo instance and had to import all news as new posts. A simple query and forwarding to a function. That's all:, (*7)

$query = '
SELECT
  uid                                   AS _import_uid,
  title                                 AS post_title,
  "post"                                AS post_type,
  short                                 AS post_excerpt,
  bodytext                              AS post_content,
  CONCAT('http://example.org/', logo)   AS _thumbnail_id,
FROM `typo3-databse`.tt_news
';

foreach( fsi_query ( $query ) as $item ) {
    fsi_import_post( $item );
}

This does a lot:, (*8)

  • No duplicates - Run this several times without hurt.
  • Faster import - Using fsi_query will yield data through.
  • Downloads thumbnail only once - No duplicates there too.

You can also use the mapper for that:, (*9)

The Versions

09/12 2016

dev-master

9999999-dev

Import things in WordPress made easy for developers.

  Sources   Download

MIT