-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmainwp-hello-world-extension.php
232 lines (197 loc) · 8.52 KB
/
mainwp-hello-world-extension.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/*
Plugin Name: MainWP Hello World! Extension
Plugin URI: https://mainwp.com
Description: MainWP Hello World! Extension is a MainWP example extension
Version: 1.1
Author: MainWP
Author URI: https://mainwp.com
Icon URI:
*/
class MainWP_Hello_World_Extension {
public function __construct() {
add_filter( 'mainwp_getsubpages_sites', array( &$this, 'managesites_subpage' ), 10, 1 );
add_action( 'admin_init', array( &$this, 'admin_init' ) );
}
public function admin_init() {
}
public function managesites_subpage( $subPage ) {
$subPage[] = array(
'title' => 'Example Extension',
'slug' => 'ExampleExtension',
'sitetab' => true,
'menu_hidden' => true,
'callback' => array( static::class, 'renderPage' ),
);
return $subPage;
}
/*
* Create your extension page
*/
public static function renderPage() {
global $mainwpHelloExtensionActivator;
// Fetch all child-sites
$websites = apply_filters( 'mainwp_getsites', $mainwpHelloExtensionActivator->getChildFile(), $mainwpHelloExtensionActivator->getChildKey(), null );
// Location to open on child site
$location = 'admin.php?page=mainwp_child_tab';
if ( is_array( $websites ) ) {
?>
<div class="ui segment">
<div class="inside">
<p><?php _e( 'MainWP Hello World! Extension is an example extension. This extension provides two examples for calling MainWP Actions and Hooks. Purpose of this extension is to give you a start point in developing your first custom extension for the MainWP Plugin.' ); ?></p>
<p><?php echo __( 'For more details, please visit ' ) . '<a href="http://codex.mainwp.com" target="_blank">' . __( 'MainWP Codex' ) . '</a>' . ' website.'; ?></p>
</div>
</div>
<div class="ui segment">
<h3 class="mainwp_box_title"><?php _e( 'Get Child Sites' ); ?></h3>
<div class="inside">
<em><?php _e( 'List all child sites and link to open the child site' ); ?></em>
<?php
// Display number of your child sites
?>
<p><?php echo __( 'Number of Child Sites: ' ) . count( $websites ); ?></p>
<div id="mainwp_example_links">
<?php
// Display a list of your child site with a secure link to child site WP Admin (login not required)
foreach ( $websites as $site ) {
// Create link to open $location on child site
$open_url = 'admin.php?page=SiteOpen&newWindow=yes&websiteid=' . $site['id'] . '&location=' . base64_encode( $location ) . '&_opennonce=' . wp_create_nonce( 'mainwp-admin-nonce' );
echo $site['name'] . ': ';
?>
<a href="<?php echo $open_url; ?>" class="queue" target="_blank"><?php _e( 'Open location', 'mainwp' ); ?></a>.
<br/>
<?php
}
?>
</div>
</div>
</div>
<div class="ui segment">
<h3 class="mainwp_box_title"><?php _e( 'Get Posts From Child Sites' ); ?></h3>
<div class="inside">
<em><?php _e( 'List all posts from your child site' ); ?></em>
<?php
$rand_id = $websites ? wp_rand( 0, count( $websites ) - 1 ) : 0;
$site = $websites[ $rand_id ];
?>
<p><?php _e( 'Get posts from child site:' ); ?> <?php echo $site['url']; ?> <a href="admin.php?page=Extensions-Mainwp-Hello-World-Extension&siteid=<?php echo $site['id']; ?>" class="button button-primary"><?php _e( 'Click Here!' ); ?></a></p>
<?php
if ( isset( $_GET['siteid'] ) && ! empty( $_GET['siteid'] ) ) {
$websiteId = $_GET['siteid'];
// fetch information of one child-site
$website = apply_filters( 'mainwp_getsites', $mainwpHelloExtensionActivator->getChildFile(), $mainwpHelloExtensionActivator->getChildKey(), $websiteId );
if ( $website && is_array( $website ) ) {
$website = current( $website );
}
if ( ! $website ) {
echo '<p><strong>ERROR</strong>: ' . __( 'Child Site Not Found' ) . '</p>';
} else {
// Example to call function get_all_posts on child-plugin to get posts on child site
$post_data = array(
'status' => 'publish',
'maxRecords' => 10,
);
// hook to call the function get_all_posts
$information = apply_filters( 'mainwp_fetchurlauthed', $mainwpHelloExtensionActivator->getChildFile(), $mainwpHelloExtensionActivator->getChildKey(), $websiteId, 'get_all_posts', $post_data );
if ( is_array( $information ) ) {
if ( isset( $information['error'] ) ) {
echo '<p><strong>ERROR</strong>: ' . $information['error'] . '</p>';
} else {
echo '<h3>' . _( 'List of posts: ' ) . '</h3>';
echo '<ol>';
foreach ( $information as $post ) {
echo '<li>';
echo $post['title'];
?>
<a href="<?php echo $website['url'] . ( substr( $website['url'], -1 ) != '/' ? '/' : '' ) . '?p=' . $post['id']; ?>"
target="_blank"
title="View '<?php echo $post['title']; ?>'"
rel="permalink"><?php _e( 'View Post' ); ?></a>
<?php
echo '</li>';
}
echo '</ol>';
}
}
}
}
} else {
echo 'Child Sites Not Found';
}
?>
</div>
</div>
<?php
}
}
/*
* Activator Class is used for extension activation and deactivation
*/
class MainWP_Hello_World_Activator {
protected $mainwpHelloExtensionActivated = false;
protected $childEnabled = false;
protected $childKey = false;
protected $childFile;
protected $plugin_handle = 'mainwp-hello-world-extension';
public function __construct() {
$this->childFile = __FILE__;
add_filter( 'mainwp_getextensions', array( &$this, 'get_this_extension' ) );
// This filter will return true if the main plugin is activated
$this->mainwpHelloExtensionActivated = apply_filters( 'mainwp_activated_check', false );
if ( $this->mainwpHelloExtensionActivated !== false ) {
$this->activate_this_plugin();
} else {
// Because sometimes our main plugin is activated after the extension plugin is activated we also have a second step,
// listening to the 'mainwp_activated' action. This action is triggered by MainWP after initialisation.
add_action( 'mainwp_activated', array( &$this, 'activate_this_plugin' ) );
}
add_action( 'admin_notices', array( &$this, 'mainwp_error_notice' ) );
}
function get_this_extension( $pArray ) {
$pArray[] = array(
'plugin' => __FILE__,
'api' => $this->plugin_handle,
'mainwp' => false,
'callback' => array( &$this, 'settings' ),
);
return $pArray;
}
function settings() {
// The "mainwp_pageheader_extensions" action is used to render the tabs on the Extensions screen.
// It's used together with mainwp_pagefooter_extensions and mainwp_getextensions
do_action( 'mainwp_pageheader_extensions', __FILE__ );
if ( $this->childEnabled ) {
MainWP_Hello_World_Extension::renderPage();
} else {
?>
<div class="mainwp_info-box-yellow"><?php _e( 'The Extension has to be enabled to change the settings.' ); ?></div>
<?php
}
do_action( 'mainwp_pagefooter_extensions', __FILE__ );
}
// The function "activate_this_plugin" is called when the main is initialized.
function activate_this_plugin() {
// Checking if the MainWP plugin is enabled. This filter will return true if the main plugin is activated.
$this->mainwpHelloExtensionActivated = apply_filters( 'mainwp_activated_check', $this->mainwpHelloExtensionActivated );
// The 'mainwp_extension_enabled_check' hook. If the plugin is not enabled this will return false,
// if the plugin is enabled, an array will be returned containing a key.
// This key is used for some data requests to our main
$this->childEnabled = apply_filters( 'mainwp_extension_enabled_check', __FILE__ );
$this->childKey = $this->childEnabled['key'];
new MainWP_Hello_World_Extension();
}
function mainwp_error_notice() {
global $current_screen;
if ( $current_screen->parent_base == 'plugins' && $this->mainwpHelloExtensionActivated == false ) {
echo '<div class="error"><p>MainWP Hello World! Extension ' . __( 'requires ' ) . '<a href="http://mainwp.com/" target="_blank">MainWP</a>' . __( ' Plugin to be activated in order to work. Please install and activate' ) . '<a href="http://mainwp.com/" target="_blank">MainWP</a> ' . __( 'first.' ) . '</p></div>';
}
}
public function getChildKey() {
return $this->childKey;
}
public function getChildFile() {
return $this->childFile;
}
}
global $mainwpHelloExtensionActivator;
$mainwpHelloExtensionActivator = new MainWP_Hello_World_Activator();