Project

General

Profile

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

Boone Gorges, 2022-08-30 10:31 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
			$query = new BP_Docs_Query();
76

    
77
			$source_doc_id = $source_item->get_source_item_id();
78
			$source_doc    = get_post( $source_item_doc_id );
79

    
80
			$doc_args = [
81
				'title'     => $source_doc->post_title,
82
				'content'   => $source_doc->post_content,
83
				'author_id' => $source_doc->post_author,
84
				'group_id'  => bp_docs_get_associated_group_id( $source_doc_id ),
85
				'settings'  => bp_docs_get_doc_settings( $source_doc_id ),
86
			];
87

    
88
			$query->save( $doc_args );
89

    
90
		break;
91

    
92
		default :
93

    
94
			$dest_item = new \CAC\GroupLibrary\LibraryItem\Item();
95
			$dest_item->set_group_id( $dest_group_id );
96
			$dest_item->set_source_item_id( $source_item->get_source_item_id() );
97
			$dest_item->set_item_type( $source_item->get_item_type() );
98
			$dest_item->set_file_type( $source_item->get_file_type() );
99
			$dest_item->set_user_id( $source_item->get_user_id() );
100
			$dest_item->set_title( $source_item->get_title() );
101
			$dest_item->set_date_modified( $source_item->get_date_modified() );
102
			$dest_item->set_description( $source_item->get_description() );
103
			$dest_item->set_url( $source_item->get_url() );
104
			$dest_item->set_folders( $source_item->get_folders() );
105

    
106
			$dest_item->save();
107

    
108
		break;
109
	}
110
	continue;
111
}
112

    
113
remove_filter( 'ass_block_group_activity_types', '__return_false' );
114

    
115
$progress_bar->finish();
(2-2/3)