Project

General

Profile

Support #13243 » cac-group-library-copy.php

Boone Gorges, 2020-08-27 08:40 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
$items_count  = count( $source_items );
26
$progress_bar = WP_CLI\Utils\make_progress_bar( "Copying $items_count library items", $items_count );
27

    
28
$dest_bp_group_documents_dir = bp_core_avatar_upload_path() . '/group-documents/' . $dest_group_id;
29

    
30
add_filter( 'ass_block_group_activity_types', '__return_false' );
31

    
32
foreach ( $source_items as $source_item ) {
33
	$progress_bar->tick();
34

    
35
	foreach ( $source_item->get_folders() as $folder_name ) {
36
		// Will create if it doesn't exist.
37
		$folder_id = \CAC\GroupLibrary\Folder::get_group_folder_by_name( $dest_group_id, $folder_name );
38
	}
39

    
40
	switch ( $source_item->get_item_type() ) {
41
		case 'bp_group_document' :
42
			$source_doc = new BP_Group_Documents( $source_item->get_source_item_id() );
43

    
44
			if ( empty( $source_doc->file ) ) {
45
				continue 2;
46
			}
47

    
48
			$params = [
49
				'user_id'        => $source_doc->user_id,
50
				'group_id'       => $dest_group_id,
51
				'created_ts'     => $source_doc->created_ts,
52
				'modified_ts'    => $source_doc->modified_ts,
53
				'file'           => $source_doc->file,
54
				'name'           => $source_doc->name,
55
				'description'    => $source_doc->description,
56
				'featured'       => $source_doc->featured,
57
				'download_count' => $source_doc->download_count,
58
			];
59

    
60
			$dest_doc = new BP_Group_Documents();
61
			foreach ( $params as $key => $value ) {
62
				$dest_doc->{$key} = $value;
63
			}
64

    
65
			$dest_doc->save( false );
66

    
67
			// Move the file itself.
68
			$source_path = $source_doc->get_path( 0, 0 );
69
			$dest_path   = $dest_doc->get_path( 0, 1 );
70
			copy( $source_path, $dest_path );
71

    
72
		break;
73

    
74
		case 'bp_doc' :
75

    
76
		break;
77

    
78
		default :
79

    
80
			$dest_item = new \CAC\GroupLibrary\LibraryItem\Item();
81
			$dest_item->set_group_id( $dest_group_id );
82
			$dest_item->set_source_item_id( $source_item->get_source_item_id() );
83
			$dest_item->set_item_type( $source_item->get_item_type() );
84
			$dest_item->set_file_type( $source_item->get_file_type() );
85
			$dest_item->set_user_id( $source_item->get_user_id() );
86
			$dest_item->set_title( $source_item->get_title() );
87
			$dest_item->set_date_modified( $source_item->get_date_modified() );
88
			$dest_item->set_description( $source_item->get_description() );
89
			$dest_item->set_url( $source_item->get_url() );
90
			$dest_item->set_folders( $source_item->get_folders() );
91

    
92
			$dest_item->save();
93

    
94
		break;
95
	}
96
	continue;
97
}
98

    
99
remove_filter( 'ass_block_group_activity_types', '__return_false' );
100

    
101
$progress_bar->finish();
    (1-1/1)