forked from 10up/distributor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributor.php
More file actions
228 lines (207 loc) · 6.46 KB
/
distributor.php
File metadata and controls
228 lines (207 loc) · 6.46 KB
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
<?php
/**
* Plugin Name: Distributor
* Description: Makes it easy to distribute and reuse content across your websites, whether inside of a multisite or across the web.
* Version: 1.6.3-dev
* Author: 10up Inc.
* Author URI: https://distributorplugin.com
* License: GPLv2 or later
* Text Domain: distributor
* Domain Path: /lang/
* GitHub Plugin URI: https://github.com/10up/distributor
*
* @package distributor
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
define( 'DT_VERSION', '1.6.2-dev' );
define( 'DT_PLUGIN_FILE', preg_replace( '#^.*plugins/(.*)$#i', '$1', __FILE__ ) );
define( 'DT_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
// Define a constant if we're network activated to allow plugin to respond accordingly.
$active_plugins = get_site_option( 'active_sitewide_plugins' );
if ( is_multisite() && isset( $active_plugins[ plugin_basename( __FILE__ ) ] ) ) {
define( 'DT_IS_NETWORK', true );
} else {
define( 'DT_IS_NETWORK', false );
}
/**
* PSR-4 autoloading
*/
spl_autoload_register(
function( $class ) {
// Project-specific namespace prefix.
$prefix = 'Distributor\\';
// Base directory for the namespace prefix.
$base_dir = __DIR__ . '/includes/classes/';
// Does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
return;
}
$relative_class = substr( $class, $len );
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
// If the file exists, require it.
if ( file_exists( $file ) ) {
require $file;
}
}
);
/**
* Require PHP version 5.6 - throw an error if the plugin is activated on an older version.
*/
register_activation_hook(
__FILE__,
function() {
if ( version_compare( PHP_VERSION, '5.6.0', '<' ) ) {
wp_die(
esc_html__( 'Distributor requires PHP version 5.6.', 'distributor' ),
esc_html__( 'Error Activating', 'distributor' )
);
}
}
);
/**
* Tell the world this site supports Distributor. We need this for external connections.
*/
add_action(
'send_headers',
function() {
if ( ! headers_sent() ) {
header( 'X-Distributor: yes' );
}
}
);
/**
* Set Distributor header in all API responses.
*/
add_filter(
'rest_post_dispatch',
function( $response ) {
$response->header( 'X-Distributor', 'yes' );
return $response;
}
);
\Distributor\Connections::factory();
// Include in case we have composer issues.
require_once __DIR__ . '/vendor/yahnis-elsts/plugin-update-checker/plugin-update-checker.php';
require_once __DIR__ . '/includes/utils.php';
require_once __DIR__ . '/includes/external-connection-cpt.php';
require_once __DIR__ . '/includes/push-ui.php';
require_once __DIR__ . '/includes/pull-ui.php';
require_once __DIR__ . '/includes/rest-api.php';
require_once __DIR__ . '/includes/subscriptions.php';
require_once __DIR__ . '/includes/syndicated-post-ui.php';
require_once __DIR__ . '/includes/distributed-post-ui.php';
require_once __DIR__ . '/includes/settings.php';
require_once __DIR__ . '/includes/template-tags.php';
require_once __DIR__ . '/includes/debug-info.php';
// Include application passwords.
add_action(
'plugins_loaded',
function() {
if ( function_exists( 'wp_is_application_passwords_available' ) ) {
if ( ! wp_is_application_passwords_available() ) {
add_action(
'admin_notices',
function() {
if ( get_current_screen()->id !== 'toplevel_page_distributor' ) {
return;
}
?>
<div class="notice notice-warning">
<p>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s is the URL to the guide to enable Application Passwords for non HTTPS sites. */
__( 'Your site is not using HTTPS or is a local environment. Follow this <a href="%s">guide</a> to enable Application Passwords.', 'distributor' ),
'https://github.com/10up/distributor#application-passwords-and-wordpress-56'
)
);
?>
</p>
</div>
<?php
}
);
}
} elseif ( ! class_exists( 'Application_Passwords' ) ) {
require_once __DIR__ . '/vendor/georgestephanis/application-passwords/application-passwords.php';
}
}
);
// Override some styles for application passwords until we can get these changes upstream.
add_action(
'admin_enqueue_scripts',
function() {
wp_enqueue_style( 'dt-admin-settings', plugins_url( '/dist/css/admin.min.css', __FILE__ ), array(), DT_VERSION );
}
);
if ( class_exists( 'Puc_v4_Factory' ) ) {
/**
* Enable updates if we have a valid license
*/
$valid_license = false;
if ( ! DT_IS_NETWORK ) {
$valid_license = \Distributor\Utils\get_settings()['valid_license'];
} else {
$valid_license = \Distributor\Utils\get_network_settings()['valid_license'];
}
if ( $valid_license ) {
// @codingStandardsIgnoreStart
$updateChecker = Puc_v4_Factory::buildUpdateChecker(
'https://github.com/10up/distributor/',
__FILE__,
'distributor'
);
$updateChecker->getVcsApi()->enableReleaseAssets();
$updateChecker->addResultFilter(
function( $plugin_info, $http_response = null ) {
$plugin_info->icons = array(
'svg' => plugins_url( '/assets/img/icon.svg', __FILE__ ),
);
return $plugin_info;
}
);
// @codingStandardsIgnoreEnd
}
}
/**
* Register connections
*/
add_action(
'init',
function() {
\Distributor\Connections::factory()->register( '\Distributor\ExternalConnections\WordPressExternalConnection' );
\Distributor\Connections::factory()->register( '\Distributor\ExternalConnections\WordPressDotcomExternalConnection' );
if (
/**
* Filter whether the network connection type is enabled. Enabled by default, return false to disable.
*
* @since 1.0.0
* @hook dt_network_site_connection_enabled
*
* @param {bool} true Whether the network connection should be enabled.
*
* @return {bool} Whether the network connection should be enabled.
*/
apply_filters( 'dt_network_site_connection_enabled', true )
) {
\Distributor\Connections::factory()->register( '\Distributor\InternalConnections\NetworkSiteConnection' );
}
},
1
);
/**
* We use setup functions to avoid unit testing WP_Mock strict mode errors.
*/
\Distributor\ExternalConnectionCPT\setup();
\Distributor\PushUI\setup();
\Distributor\PullUI\setup();
\Distributor\RestApi\setup();
\Distributor\Subscriptions\setup();
\Distributor\SyndicatedPostUI\setup();
\Distributor\DistributedPostUI\setup();
\Distributor\Settings\setup();
\Distributor\DebugInfo\setup();