搜石者-mike/wp-fast-simple-import

让开发者轻松导入WordPress中的内容。

安装: 39

依赖: 0

建议: 0

安全: 0

星星: 0

关注者: 1

分支: 0

类型:wordpress-muplugin

dev-master 2016-12-09 08:48 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:31:15 UTC


README

编写导入操作非常简单。只需获取数据并将其转发给WP-FSI

$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' );
}

对于文章、术语和附件来说,就是这样简单。由于WordPress无法实现的好处,它速度极快。

特性

  • 文章
    • 使用 fsi_import_post 导入文章。
    • fsi_post_add_term 创建术语或如果它们已存在则添加它们。
  • 媒体
    • fsi_import_thumbnail 从文件系统或URL导入图像。
  • 术语
    • fsi_term_import 创建术语或如果它已存在则更新。
    • fsi_term_meta_update 接受一个数组并将其作为新的术语元数据应用。
    • fsi_term_meta_replace 如上所述,但删除所有旧数据。

还有一些实用的辅助工具

  • 如果你想加快导入速度,请使用 fsi_query
  • 使用 fsi_enable_all_caps() 为导入超级充电并赋予它所有类型的权限。
  • 或者使用 fsi_enable_caps 为权限子集。

映射

包括一个映射辅助工具。基本上它是一个带有一些魔法的数组

  • 是数据应该去的地方。
  • 是数据来自的地方。
  • 所以可以说,数据从 [ 'target_field' => 'source_field' ] 的右侧流向左侧

所以它就像

$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 );

可调用项可以在值侧转换数据。它接收实际的映射(第一个参数),源数据(第二个参数)以及迄今为止的结果目标数据(第三个参数)。你返回的内容将被存储在目标中

$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 );

实际示例

从Typo导入tt_news

这里我有一些Typo实例,必须导入所有新闻作为新文章。一个简单的查询并将它转发到函数。就是这样

$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 );
}

这做了很多

  • 无重复 - 可以多次运行而不会造成伤害。
  • 快速导入 - 使用 fsi_query 将通过。
  • 仅下载缩略图一次 - 这里也没有重复。

您也可以使用映射器来做这件事