-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
161 lines (144 loc) · 4.93 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Plugin Name: Extension for Distributor - Multisite Cloner
* Plugin URI: https://wordpress.org/plugins/mc_wp_mu-dt-clone
* Description: Fixes integration between cloned sites and the Distributor plugin.
* Version: 1.3.0
* Author: milkycode GmbH
* Author URI: https://www.milkycode.com
* License: License: GPL2+
*/
defined( 'ABSPATH' ) || exit();
// Since an original site would not know about any Distributor connections in the duplicate of a duplicate,
// this plugin connects the original with the latest created site.
// Add settings menu to run fix manually.
add_action( 'network_admin_menu', 'mc_mudtcl_create_page' );
// Add hook for plugin MultiSite Clone Duplicator.
add_action( 'wpmu_new_blog', 'mc_mudtcl_fix_dt_connections', 2, 1 );
// Add hook for plugin WP Ultimo.
add_action( 'mucd_after_copy_data', 'mc_mudtcl_wp_ultimo', 10, 2 );
// Add hook for plugin NS Cloner - Site Copier.
add_action( 'ns_cloner_process_finish', function () {
$target_id = ns_cloner_request()->get( 'target_id' );
return mc_mudtcl_fix_dt_connections( $target_id );
} );
/**
* Add admin page for running fix manually.
*/
function mc_mudtcl_create_page() {
add_submenu_page(
'settings.php',
'Distributor Multisite cloning Fixer',
'Distributor Multisite cloning Fixer',
'manage_options',
'mc_wp_mu-dt-clone/index.php',
'mudtcl_admin_page'
);
}
/**
* Wrapper for mc_mudtcl_fix_dt_connections.
*
* @param int $template_id
* @param int $target_id
*
* @return bool
*/
function mc_mudtcl_wp_ultimo( $template_id, $target_id ) {
return mc_mudtcl_fix_dt_connections( $target_id );
}
/**
* Fix all missing distributor connections.
*
* @param int $target_id
*
* @return bool
*/
function mc_mudtcl_fix_dt_connections( $target_id ) {
switch_to_blog( $target_id );
// Search through site for distributed posts
$args = array(
'meta_key' => 'dt_original_blog_id',
'post_type' => 'any',
'posts_per_page' => - 1,
);
$posts_query = new WP_Query( $args );
$distributed_posts = $posts_query->posts;
// Getting the original blog ID and post ID from every distributed post.
$og_blog_and_post_ids = array();
foreach ( $distributed_posts as $post ) {
$original_blog_id = get_post_meta( $post->ID, 'dt_original_blog_id' )[0];
$original_post_id = get_post_meta( $post->ID, 'dt_original_post_id' )[0];
if ( ! isset( $og_blog_and_post_ids[ $original_blog_id ] ) ) {
$og_blog_and_post_ids[ $original_blog_id ] = array();
}
array_push( $og_blog_and_post_ids[ $original_blog_id ],
array(
'og_post_id' => $original_post_id,
'post_id' => $post->ID,
) );
}
foreach ( $og_blog_and_post_ids as $blog_id => $og_post_id ) {
// Switching to original blog to create and add new site connection.
switch_to_blog( $blog_id );
foreach ( $og_post_id as $post ) {
$dt_connection = get_post_meta( $post['og_post_id'], 'dt_connection_map' );
if ( isset( $dt_connection ) ) {
$dt_connection[0]['internal'][ $target_id ] = array(
'post_id' => $post['post_id'],
'time' => time(),
);
update_post_meta( $post['og_post_id'], 'dt_connection_map', $dt_connection[0] );
}
}
}
restore_current_blog();
return true;
}
/**
* Create admin page for running fix manually.
*/
function mudtcl_admin_page() {
$network_sites = get_sites();
$plugin_url = admin_url(
'network/settings.php?page=mc_wp_mu-dt-clone%2Findex.php' );
?>
<div class="wrap">
<h2>Distributor Multisite cloning fixer</h2>
<p>Since an original site would not know about any Distributor connections in the duplicate of a site
containing connections, <br> this fix connects the original with the latest created clone.</p>
<p>This manual fix is only needed, when installing this plugin after sites where already cloned.<br/>
New sites will be connected automatically after cloning (for all supported 3rd party plugins).</p>
<form action="<?php echo $plugin_url ?>" method="POST">
<table class="form-table">
<tr>
<th scope="row"><label for="latest">Site to fix:</label></th>
<td>
<select name="latest" id="latest">
<?php foreach ( $network_sites as $site ) {
echo '<option value="'
. $site->blog_id
. '" name="'
. $site->domain
. '"' . '>'
. $site->domain
. '</option>';
}
?>
</select>
</td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button button-primary" value="Fix connections">
</p>
</form>
</div>
<?php
if ( isset( $_POST['latest'] ) && $_SERVER['REQUEST_METHOD'] == "POST" ) {
if ( mc_mudtcl_fix_dt_connections( $_POST['latest'] ) ) {
echo "Fix successfully executed. All connections updated.";
} else {
echo "There was an error executing the fix.";
}
}
}