forked from cloudflare/Cloudflare-WordPress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
84 lines (74 loc) · 3.28 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
<?php
if (!defined('ABSPATH')) { // Exit if accessed directly
exit;
}
$config = new CF\Integration\DefaultConfig(file_get_contents('config.js', true));
$logger = new CF\Integration\DefaultLogger($config->getValue('debug'));
$dataStore = new CF\WordPress\DataStore($logger);
$wordpressAPI = new CF\WordPress\WordPressAPI($dataStore);
$pluginData = get_plugin_data(CLOUDFLARE_PLUGIN_DIR.'cloudflare.php');
$pluginVersion = $pluginData['Version'];
wp_register_style('cf-corecss', plugins_url('stylesheets/cf.core.css', __FILE__), null, $pluginVersion);
wp_enqueue_style('cf-corecss');
wp_register_style('cf-componentscss', plugins_url('stylesheets/components.css', __FILE__), null, $pluginVersion);
wp_enqueue_style('cf-componentscss');
wp_register_style('cf-hackscss', plugins_url('stylesheets/hacks.css', __FILE__), null, $pluginVersion);
wp_enqueue_style('cf-hackscss');
wp_enqueue_script('cf-compiledjs', plugins_url('compiled.js', __FILE__), null, $pluginVersion);
?>
<div id="root" class="cloudflare-partners site-wrapper"></div>
<script>
//Set global absolute base url
window.absoluteUrlBase = '<?php echo plugins_url('/cloudflare/'); ?>';
cfCSRFToken = '<?php echo wp_create_nonce(\CF\WordPress\WordPressAPI::API_NONCE); ?>';
localStorage.cfEmail = '<?php echo $dataStore->getCloudFlareEmail(); ?>';
/*
* A callback for cf-util-http to proxy all calls to our backend
*
* @param {Object} [opts]
* @param {String} [opts.method] - GET/POST/PUT/PATCH/DELETE
* @param {String} [opts.url]
* @param {Object} [opts.parameters]
* @param {Object} [opts.headers]
* @param {Object} [opts.body]
* @param {Function} [opts.onSuccess]
* @param {Function} [opts.onError]
*/
window.RestProxyCallback = (opts) => {
// Only proxy external REST calls
if (opts.url.lastIndexOf('http', 0) === 0) {
if (!opts.parameters) {
opts.parameters = {};
}
// WordPress Ajax Action
opts.parameters['action'] = '<?php echo \CF\WordPress\Hooks::WP_AJAX_ACTION; ?>'
if (opts.method.toUpperCase() === 'GET') {
var clientAPIURL = '<?php echo \CF\API\Client::ENDPOINT; ?>';
var pluginAPIURL = '<?php echo \CF\API\Plugin::ENDPOINT; ?>';
// If opts.url begins with clientAPIURL or pluginAPIURL,
// remove the API URL and assign the rest to proxyURL
if (opts.url.substring(0, clientAPIURL.length) === clientAPIURL) {
opts.parameters['proxyURL'] = opts.url.substring(clientAPIURL.
length);
opts.parameters['proxyURLType'] = 'CLIENT';
} else if (opts.url.substring(0, pluginAPIURL.length) === pluginAPIURL) {
opts.parameters['proxyURL'] = opts.url.substring(pluginAPIURL.length);
opts.parameters['proxyURLType'] = 'PLUGIN';
}
} else {
if (!opts.body) {
opts.body = {};
}
opts.body['cfCSRFToken'] = cfCSRFToken;
opts.body['proxyURL'] = opts.url;
}
// WordPress Ajax Global
opts.url = ajaxurl;
} else {
// To avoid static files getting cached add the version number
// to the url
var versionNumber = '<?php echo $pluginVersion; ?>';
opts.url = absoluteUrlBase + opts.url + '?ver=' + versionNumber;
}
}
</script>