Project

General

Profile

cac-group-library-copy.php

Boone Gorges, 2020-08-03 11:02 AM

 
1
<?php
2

    
3
/**
4
 * Copy one group's library to another.
5
 */
6

    
7
if ( empty( $args ) || count( $args ) < 2 ) {
8
        WP_CLI::error( 'Usage: wp eval-file cac-group-library-copy.php [fromGroup] [toGroup]' );
9
}
10

    
11
$source_group_id = $args[0];
12
$dest_group_id   = $args[1];
13

    
14
$source_group = groups_get_group( $source_group_id );
15
if ( ! $source_group->id ) {
16
        WP_CLI::error( 'Source group cannot be found.' );
17
}
18

    
19
$dest_group = groups_get_group( $dest_group_id );
20
if ( ! $dest_group->id ) {
21
        WP_CLI::error( 'Destination group cannot be found.' );
22
}
23

    
24
$source_items = \CAC\GroupLibrary\LibraryItem\Query::get( [ 'group_id' => $source_group_id ] );
25
foreach ( $source_items as $source_item ) {
26
        foreach ( $source_item->get_folders() as $folder_name ) {
27
                // Will create if it doesn't exist.
28
                $folder_id = \CAC\GroupLibrary\Folder::get_group_folder_by_name( $dest_group_id, $folder_name );
29
        }
30

    
31
        $dest_item = new \CAC\GroupLibrary\LibraryItem\Item();
32
        $dest_item->set_group_id( $dest_group_id );
33
        $dest_item->set_source_item_id( $source_item->get_source_item_id() );
34
        $dest_item->set_item_type( $source_item->get_item_type() );
35
        $dest_item->set_file_type( $source_item->get_file_type() );
36
        $dest_item->set_user_id( $source_item->get_user_id() );
37
        $dest_item->set_title( $source_item->get_title() );
38
        $dest_item->set_date_modified( $source_item->get_date_modified() );
39
        $dest_item->set_description( $source_item->get_description() );
40
        $dest_item->set_url( $source_item->get_url() );
41
        $dest_item->set_folders( $source_item->get_folders() );
42

    
43
        $dest_item->save();
44
}
45

    
46

    
47
//print_r( $source_items );