Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 64 |
|
0.00% |
0 / 3 |
CRAP | n/a |
0 / 0 |
|
| wpcom_themes_tracks_get_theme_props | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
56 | |||
| wpcom_themes_tracks_switch_theme | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| wpcom_themes_tracks_install_theme | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
306 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WordPress.com Theme Tracking |
| 4 | * |
| 5 | * Add Tracks events to the wp-admin theme screens. |
| 6 | * |
| 7 | * @package automattic/jetpack-mu-wpcom |
| 8 | */ |
| 9 | |
| 10 | use Automattic\Jetpack\Jetpack_Mu_Wpcom\Common; |
| 11 | |
| 12 | /** |
| 13 | * Get theme properties for Tracks event. |
| 14 | * |
| 15 | * @param WP_Theme $theme Theme object. |
| 16 | * @param string $prefix Prefix for the property keys. |
| 17 | * @return array Associative array of theme properties. |
| 18 | */ |
| 19 | function wpcom_themes_tracks_get_theme_props( $theme, $prefix = '' ) { |
| 20 | if ( $prefix !== '' ) { |
| 21 | $prefix .= '_'; |
| 22 | } |
| 23 | |
| 24 | $props = array( |
| 25 | $prefix . 'theme' => $theme->get( 'Name' ), |
| 26 | $prefix . 'theme_stylesheet' => $theme->get_stylesheet(), |
| 27 | $prefix . 'theme_tier' => null, |
| 28 | $prefix . 'theme_is_block_theme' => $theme->is_block_theme(), |
| 29 | $prefix . 'theme_is_retired' => false, |
| 30 | ); |
| 31 | |
| 32 | // Simple sites |
| 33 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM && class_exists( 'WPCom_Themes' ) ) { |
| 34 | // @phan-suppress-next-line PhanUndeclaredClassMethod |
| 35 | $props[ $prefix . 'theme_tier' ] = WPCom_Themes::get_theme_tier( $theme->get_stylesheet() ); |
| 36 | // @phan-suppress-next-line PhanUndeclaredClassMethod |
| 37 | $props[ $prefix . 'theme_is_retired' ] = WPCom_Themes::is_retired( $theme->get_stylesheet() ); |
| 38 | |
| 39 | return $props; |
| 40 | } |
| 41 | |
| 42 | // Atomic sites |
| 43 | $props[ $prefix . 'theme_tier' ] = 'community'; |
| 44 | |
| 45 | if ( function_exists( 'wpcomsh_get_wpcom_themes_service_instance' ) ) { |
| 46 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 47 | $theme_data = $wpcom_themes_service->get_theme( $theme->get_stylesheet() ); |
| 48 | |
| 49 | if ( $theme_data !== null ) { |
| 50 | $props[ $prefix . 'theme_tier' ] = $theme_data->theme_tier ?? null; |
| 51 | $props[ $prefix . 'theme_is_retired' ] = $theme_data->is_retired ?? false; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return $props; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Record a theme switch. |
| 60 | * |
| 61 | * @todo There is already a theme switch event for Simple sites. It should be removed in favor of this one. |
| 62 | * |
| 63 | * @param string $new_theme_name New theme name. |
| 64 | * @param WP_Theme $new_theme New theme object. |
| 65 | * @param WP_Theme $old_theme Old theme object. |
| 66 | * @return void |
| 67 | */ |
| 68 | function wpcom_themes_tracks_switch_theme( $new_theme_name, $new_theme, $old_theme ) { |
| 69 | $old_theme_props = wpcom_themes_tracks_get_theme_props( $old_theme, 'old' ); |
| 70 | $new_theme_props = wpcom_themes_tracks_get_theme_props( $new_theme, 'new' ); |
| 71 | |
| 72 | $event_props = array_merge( |
| 73 | array( 'blog_id' => get_wpcom_blog_id() ), |
| 74 | $old_theme_props, |
| 75 | $new_theme_props |
| 76 | ); |
| 77 | |
| 78 | Common\wpcom_record_tracks_event( 'wpcom_theme_switch', $event_props ); |
| 79 | } |
| 80 | add_action( 'switch_theme', 'wpcom_themes_tracks_switch_theme', 12, 3 ); |
| 81 | |
| 82 | /** |
| 83 | * Record a theme install via wp-admin. |
| 84 | * |
| 85 | * @param Theme_Upgrader $upgrader Upgrader instance. |
| 86 | * @param array $hook_extra Upgrade data. |
| 87 | * @return void |
| 88 | */ |
| 89 | function wpcom_themes_tracks_install_theme( $upgrader, $hook_extra ) { |
| 90 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 91 | // Simple sites can't install themes with wp-admin so should never reach here, |
| 92 | // but just in case, we exit early. |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | if ( ! isset( $hook_extra['type'] ) || $hook_extra['type'] !== 'theme' || ! isset( $hook_extra['action'] ) ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $theme_slug = $upgrader->result['destination_name'] ?? ''; |
| 101 | |
| 102 | if ( ! $theme_slug ) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | $theme = wp_get_theme( $theme_slug ); |
| 107 | $theme_props = wpcom_themes_tracks_get_theme_props( $theme ); |
| 108 | $install_props = array( |
| 109 | 'source' => '', // Intentionally left empty for debugging purposes. |
| 110 | 'type' => 'upload', |
| 111 | ); |
| 112 | |
| 113 | // Trying to distinguish between a manual upload vs a standard install or upgrade. It's not security sensitive. |
| 114 | // phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 115 | $action = $_GET['action'] ?? ''; |
| 116 | $overwrite = $_GET['overwrite'] ?? ''; |
| 117 | // phpcs:enable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 118 | |
| 119 | if ( $hook_extra['action'] === 'install' && ! $action ) { |
| 120 | // Install theme via wp-admin UI |
| 121 | $install_props['source'] = 'wp-admin'; |
| 122 | $install_props['type'] = 'install'; |
| 123 | } elseif ( $hook_extra['action'] === 'install' && $action === 'upload-theme' && ! $overwrite ) { |
| 124 | // Upload a theme via wp-admin (new theme) |
| 125 | $install_props['source'] = 'wp-admin'; |
| 126 | $install_props['type'] = 'upload'; |
| 127 | } elseif ( $hook_extra['action'] === 'install' && $action === 'upload-theme' && $overwrite === 'update-theme' ) { |
| 128 | // Upload a theme via wp-admin (existing theme) |
| 129 | $install_props['source'] = 'wp-admin'; |
| 130 | $install_props['type'] = 'update'; |
| 131 | } elseif ( $hook_extra['action'] === 'update' && $action === 'do-theme-upgrade' ) { |
| 132 | // Update theme via update-core.php |
| 133 | $install_props['source'] = 'wp-admin'; |
| 134 | $install_props['type'] = 'bulk-update'; |
| 135 | } |
| 136 | |
| 137 | $event_props = array_merge( |
| 138 | array( 'blog_id' => get_wpcom_blog_id() ), |
| 139 | $theme_props, |
| 140 | $install_props |
| 141 | ); |
| 142 | |
| 143 | Common\wpcom_record_tracks_event( 'wpcom_theme_install', $event_props ); |
| 144 | } |
| 145 | add_action( 'upgrader_process_complete', 'wpcom_themes_tracks_install_theme', 10, 2 ); |