diff --git a/inc/admin/namespace.php b/inc/admin/namespace.php index 6990c80..d908061 100644 --- a/inc/admin/namespace.php +++ b/inc/admin/namespace.php @@ -21,6 +21,7 @@ use function HM\ACM\update_cloudfront_distribution_config; use function HM\ACM\unlink_certificate; use function HM\ACM\unlink_cloudfront_distribution; +use function HM\ACM\distribution_matches_certificate; function bootstrap() { add_submenu_page( 'tools.php', __( 'HTTPS Certificate', 'hm-acm' ), __( 'HTTPS Certificate', 'hm-acm' ), 'manage_options', 'hm-acm', __NAMESPACE__ . '\\admin_page' ); @@ -120,7 +121,68 @@ function on_unlink_cloudfront_distribution() { exit; } -function admin_page() { +/** + * Display details of the certificate in an accordion to aid debugging. + * + * @return void + */ +function display_certificate_details() : void { + + if( ! has_certificate() ) { + return; + } + + printf( + '
%s
%s

', + esc_html__( 'Certificate Details', 'hm-acm' ), + esc_html( print_r( get_certificate(), true ) ) + + ); +} + +/** + * Display details of the distribution in an accordion to aid debugging. + * + * @return void + */ +function display_cloudfront_distribution_details() : void { + + $distribution = get_cloudfront_distribution(); + + if( empty( $distribution ) ) { + return; + } + + printf( + '
%s
%s

', + esc_html__( 'Cloudfront Distribution Details', 'hm-acm' ), + esc_html( print_r( $distribution, true ) ) + + ); +} + +/** + * Display the admin page content to administer certificate setup. + * + * @return void + */ +function admin_page() : void { + + /** + * Determine whether or not to show the unlink certificate button. + * + * @param bool $show_unlink_certificate True if the unlink certificate button should be shown, otherwise false. + */ + $show_unlink_certificate = apply_filters( 'hm.acm.show_unlink_certificate', true ); + + /** + * Determine whether or not to show the unlink distribution button. + * + * @param bool $show_unlink_distribution True if the unlink distribution button should be shown, otherwise false. + */ + $show_unlink_distribution= apply_filters( 'hm.acm.show_unlink_distribution', true ); + + ?>

@@ -129,7 +191,13 @@ function admin_page() { $certificate = get_certificate(); ?>

- + + + + + + +

@@ -144,7 +212,16 @@ function admin_page() { $distribution = get_cloudfront_distribution(); ?>

- + + + + +

+ + + + + Update Config

@@ -167,6 +244,7 @@ function admin_page() {
+

@@ -177,7 +255,7 @@ function admin_page() {

- +

diff --git a/inc/namespace.php b/inc/namespace.php index c28c9a2..c38569f 100644 --- a/inc/namespace.php +++ b/inc/namespace.php @@ -4,18 +4,56 @@ use Exception; +/** + * Check whether the site has a certificate set as an option. + * + * @return boolean True if the certificate is set. + */ function has_certificate() : bool { return (bool) get_option( 'hm-acm-certificate' ); } +/** + * Check whether the site's certificate has been verified. + * + * @return boolean True if the certificate is verified. + */ function has_verified_certificate() { return get_certificate()['Status'] === 'ISSUED'; } +/** + * Get the certificate details for the site. + * + * @return array An array of certificate details, derived from \AWS\Result. + */ function get_certificate() : array { return get_option( 'hm-acm-certificate' ); } +/** + * Check whether the distribution is using the linked certificate. + * + * @return bool True if certificates match, else false. + */ +function distribution_matches_certificate() : bool { + + if( ! has_certificate() || ! has_cloudfront_distribution() ) { + return false; + } + + $certificate = get_certificate(); + $distribution = get_cloudfront_distribution(); + + return $certificate['CertificateArn'] === ( $distribution['DistributionConfig']['ViewerCertificate']['ACMCertificateArn'] ?? false ); + +} + +/** + * Refresh the certificate from AWS and update the site option to match, or remove it on failure. + * + * @return void + */ function refresh_certificate() { try { $certificate = get_aws_acm_client()->describeCertificate([ @@ -72,6 +110,12 @@ function create_certificate( array $domains ) : array { return $certificate; } +/** + * Unlink the certificate from the site by deleting the option. + * Note this does not delete the certificate from AWS. + * + * @return void + */ function unlink_certificate() { delete_option( 'hm-acm-certificate' ); } @@ -115,6 +159,11 @@ function create_cloudfront_distribution() { update_option( 'hm-cloudfront-distribution', $result['Distribution'] ); } +/** + * Update the existing Cloudfront distribution. + * + * @return void + */ function update_cloudfront_distribution_config() { $current_distribution = get_aws_cloudfront_client()->getDistribution([ 'Id' => get_cloudfront_distribution()['Id'], @@ -451,6 +500,11 @@ function get_aws_cloudfront_client() { return get_aws_sdk()->createCloudFront(); } +/** + * Get the AWS instance for the network. + * + * @return \AWS\Sdk AWS SDK class for the network. + */ function get_aws_sdk() { static $sdk; if ( $sdk ) {