Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
wpcomsh_maybe_symlink_storefront
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
wpcomsh_site_has_woocommerce
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_is_symlinked_storefront_theme
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_jetpack_storefront_theme_delete
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
wpcomsh_symlink_storefront_parent_theme
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Storefront file.
4 *
5 * @package storefront
6 */
7
8/**
9 * Checks for the WooCommerce plugin and symlink storefront themes, if not yet done.
10 *
11 * @return void|bool|WP_Error
12 */
13function wpcomsh_maybe_symlink_storefront() {
14    $is_storefront_installed = (bool) get_option( 'at_storefront_installed' );
15
16    // Nothing to do if the storefront is already symlinked.
17    if ( $is_storefront_installed ) {
18        return;
19    }
20
21    // Symlink storefront themes if WooCommerce is present.
22    if ( wpcomsh_site_has_woocommerce() ) {
23
24        $was_storefront_symlinked = wpcomsh_symlink_storefront_parent_theme();
25
26        // Exit early if storefront parent theme was not symlinked.
27        if ( is_wp_error( $was_storefront_symlinked ) ) {
28            // phpcs:disable WordPress.PHP.DevelopmentFunctions
29            error_log(
30                "Can't symlink storefront parent theme. Error: "
31                . print_r( $was_storefront_symlinked, true )
32            );
33            // phpcs:enable
34
35            return $was_storefront_symlinked;
36        }
37
38        update_option( 'at_storefront_installed', true );
39    }
40}
41add_action( 'admin_init', 'wpcomsh_maybe_symlink_storefront' );
42
43/**
44 * Checks if the site has the WooCommerce plugin, either installed or active.
45 *
46 * @return bool
47 */
48function wpcomsh_site_has_woocommerce() {
49    return class_exists( 'WooCommerce' ) || file_exists( WP_PLUGIN_DIR . '/woocommerce/woocommerce.php' );
50}
51
52/**
53 * Checks if the theme is the symlinked storefront theme.
54 *
55 * @param string $theme_slug Theme slug.
56 * @return bool
57 */
58function wpcomsh_is_symlinked_storefront_theme( $theme_slug ) {
59    return 'storefront' === $theme_slug && is_link( get_theme_root() . '/' . $theme_slug );
60}
61
62/**
63 * Handles deletion of the storefront parent theme.
64 *
65 * @param bool   $result     Whether theme deletion was successful.
66 * @param string $theme_slug Theme slug.
67 * @return bool|WP_Error
68 */
69function wpcomsh_jetpack_storefront_theme_delete( $result, $theme_slug ) {
70    if ( wpcomsh_is_symlinked_storefront_theme( $theme_slug ) ) {
71        $result = wpcomsh_delete_symlinked_theme( $theme_slug );
72    }
73    return $result;
74}
75add_filter( 'jetpack_wpcom_theme_delete', 'wpcomsh_jetpack_storefront_theme_delete', 10, 2 );
76
77/**
78 * Handles symlinking of the storefront parent theme if not installed.
79 *
80 * @return bool|WP_Error
81 */
82function wpcomsh_symlink_storefront_parent_theme() {
83    if ( ! file_exists( WP_CONTENT_DIR . '/themes/storefront' ) ) {
84        $storefront_theme_path         = WPCOMSH_STOREFRONT_SYMLINK . '/latest';
85        $storefront_theme_symlink_path = get_theme_root() . '/storefront';
86
87        if ( ! symlink( $storefront_theme_path, $storefront_theme_symlink_path ) ) {
88            $error_message = sprintf(
89                "Can't symlink the storefront parent theme. Make sure it exists in the %s directory.",
90                WPCOMSH_STOREFRONT_PATH
91            );
92
93            error_log( 'WPComSH: ' . $error_message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
94
95            return new WP_Error( 'error_symlinking_theme', $error_message );
96        }
97    }
98
99    return true;
100}