-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforce-localhost-ca.php
More file actions
72 lines (65 loc) · 2.57 KB
/
Copy pathforce-localhost-ca.php
File metadata and controls
72 lines (65 loc) · 2.57 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
<?php
/**
* Plugin Name: Force localhost CA certificate
* Version: 1.0
* Author: Square Candy Design
* Plugin URI: https://github.com/squarecandy/force-localhost-ca
* GitHub Plugin URI: https://github.com/squarecandy/force-localhost-ca
* Author URI: http://squarecandydesign.com
*
* Description: WordPress curl requests (used in wp_remote_get, etc) use a CA cert file in WP Core, not the system PHP path. Use this drop in plugin (easiest in /wp-content/mu-plugins) to fix the issue on localhost. Helpful for compatibility with local SSL certs generated by the mkcert command.
*/
/**
* Copyright (c) 2020. All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* **********************************************************************
*/
// please define your custom rootCA file path as CUSTOM_LOCAL_CA_CERT in your wp-config file
if ( ! defined( 'CUSTOM_LOCAL_CA_CERT' ) ) {
define( 'CUSTOM_LOCAL_CA_CERT', '/Users/exampleuser/Library/Application Support/mkcert/rootCA.pem' );
}
// if you need other local TLDs other than "localhost", "local" and "test", define them in your wp-config file.
if ( ! defined( 'CUSTOM_LOCAL_CA_TLDS' ) ) {
define( 'CUSTOM_LOCAL_CA_TLDS',
array(
'localhost',
'local',
'test',
)
);
}
/**
* Replace Localhost cURL Certificates
*
* If the URL for an HTTP request in WordPress contains a .test
* TLD, replace the WordPress local /certificates/ca-bundle.crt
* with our custom location
*/
function curl_patch_http_request_args( $parsed_args, $url ) {
// Get the URL's TLD
$test_url = wp_parse_url( $url, PHP_URL_HOST );
$test_tld = explode( '.', $test_url );
$test_tld = end( $test_tld );
// provide custom CA cert if we are on .localhost or .local URL
if ( in_array( $test_tld, CUSTOM_LOCAL_CA_TLDS ) ) {
$parsed_args['sslcertificates'] = CUSTOM_LOCAL_CA_CERT;
}
return $parsed_args;
}
add_filter( 'http_request_args', 'curl_patch_http_request_args', 99, 2 );