| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | function wpcomsh_get_wpcom_themes_service_instance(): WPCom_Themes_Service { |
| 14 | static $instance; |
| 15 | if ( ! $instance ) { |
| 16 | |
| 17 | require_once __DIR__ . '/includes/class-wpcom-themes-merger.php'; |
| 18 | require_once __DIR__ . '/includes/class-wpcom-themes-service.php'; |
| 19 | |
| 20 | |
| 21 | $mapper = new WPCom_Themes_Mapper(); |
| 22 | $merger = new WPCom_Themes_Merger(); |
| 23 | $cache = new WPCom_Themes_Cache(); |
| 24 | $api = new WPCom_Themes_Api( $cache ); |
| 25 | $wpcom_themes_service = new WPCom_Themes_Service( $api, $mapper, $merger ); |
| 26 | |
| 27 | $instance = $wpcom_themes_service; |
| 28 | } |
| 29 | |
| 30 | return $instance; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | function wpcomsh_remove_dot_org_themes_based_on_plan( $res ) { |
| 41 | if ( wpcom_site_has_feature( WPCOM_Features::COMMUNITY_THEMES ) ) { |
| 42 | return $res; |
| 43 | } |
| 44 | |
| 45 | return wpcomsh_keep_only_wpcom_themes( $res ); |
| 46 | } |
| 47 | add_filter( 'themes_api_result', 'wpcomsh_remove_dot_org_themes_based_on_plan', 0, 1 ); |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | function wpcomsh_keep_only_wpcom_themes( $res ) { |
| 57 | |
| 58 | if ( ! isset( $res->themes ) || ! is_array( $res->themes ) ) { |
| 59 | return $res; |
| 60 | } |
| 61 | |
| 62 | $ours = array_values( |
| 63 | array_filter( |
| 64 | $res->themes, |
| 65 | function ( $theme ) { |
| 66 | return ! empty( $theme->is_wpcom_theme ); |
| 67 | } |
| 68 | ) |
| 69 | ); |
| 70 | |
| 71 | |
| 72 | |
| 73 | if ( count( $ours ) === count( $res->themes ) ) { |
| 74 | return $res; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | $res->themes = $ours; |
| 82 | |
| 83 | if ( isset( $res->info['results'] ) ) { |
| 84 | $res->info['results'] = count( $ours ); |
| 85 | } |
| 86 | |
| 87 | return $res; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | function wpcomsh_popular_wpcom_themes_api_result( $res, string $action, $args ) { |
| 100 | |
| 101 | $browse = $args->browse ?? ''; |
| 102 | if ( 'query_themes' !== $action || 'popular' !== $browse ) { |
| 103 | return $res; |
| 104 | } |
| 105 | |
| 106 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 107 | |
| 108 | |
| 109 | return $wpcom_themes_service->filter_themes_api_result_recommended( $res ); |
| 110 | } |
| 111 | add_filter( 'themes_api_result', 'wpcomsh_popular_wpcom_themes_api_result', 1, 3 ); |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | function wpcomsh_latest_wpcom_themes_api_result( $res, string $action, $args ) { |
| 123 | |
| 124 | $browse = $args->browse ?? ''; |
| 125 | if ( 'query_themes' !== $action || 'new' !== $browse ) { |
| 126 | return $res; |
| 127 | } |
| 128 | |
| 129 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 130 | |
| 131 | |
| 132 | return $wpcom_themes_service->filter_themes_api_result_latest( $res ); |
| 133 | } |
| 134 | add_filter( 'themes_api_result', 'wpcomsh_latest_wpcom_themes_api_result', 1, 3 ); |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | function wpcomsh_block_themes_wpcom_themes_api_result( $res, string $action, $args ) { |
| 146 | |
| 147 | $tag = $args->tag ?? ''; |
| 148 | if ( 'query_themes' !== $action || 'full-site-editing' !== $tag ) { |
| 149 | return $res; |
| 150 | } |
| 151 | |
| 152 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 153 | |
| 154 | |
| 155 | return $wpcom_themes_service->filter_themes_api_result_block_themes( $res ); |
| 156 | } |
| 157 | add_filter( 'themes_api_result', 'wpcomsh_block_themes_wpcom_themes_api_result', 1, 3 ); |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | function wpcomsh_search_wpcom_themes_api_result( $res, string $action, $args ) { |
| 169 | |
| 170 | $search = $args->search ?? ''; |
| 171 | if ( 'query_themes' !== $action || '' === $search ) { |
| 172 | return $res; |
| 173 | } |
| 174 | |
| 175 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 176 | |
| 177 | |
| 178 | return $wpcom_themes_service->filter_themes_api_result_search( $res, $search ); |
| 179 | } |
| 180 | add_filter( 'themes_api_result', 'wpcomsh_search_wpcom_themes_api_result', 1, 3 ); |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | function wpcomsh_feature_filter_wpcom_themes_api_result( $res, string $action, $args ) { |
| 192 | |
| 193 | $tags = $args->tag ?? array(); |
| 194 | if ( 'query_themes' !== $action || ! $tags ) { |
| 195 | return $res; |
| 196 | } |
| 197 | |
| 198 | if ( ! is_array( $tags ) ) { |
| 199 | $tags = array( $tags ); |
| 200 | } |
| 201 | |
| 202 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 203 | |
| 204 | |
| 205 | return $wpcom_themes_service->filter_themes_api_result_feature_filter( $res, $tags ); |
| 206 | } |
| 207 | add_filter( 'themes_api_result', 'wpcomsh_feature_filter_wpcom_themes_api_result', 1, 3 ); |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | function wpcomsh_theme_information_wpcom_themes_api_result( $res, string $action, $args ) { |
| 219 | |
| 220 | if ( 'theme_information' !== $action ) { |
| 221 | return $res; |
| 222 | } |
| 223 | |
| 224 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 225 | |
| 226 | return $wpcom_themes_service->get_theme( $args->slug ) ?? $res; |
| 227 | } |
| 228 | add_filter( 'themes_api_result', 'wpcomsh_theme_information_wpcom_themes_api_result', 1, 3 ); |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | function wpcomsh_remove_symlink_wp_error( $code, $message, $data, WP_Error $error ) { |
| 241 | if ( 'wpcomsh_theme_install_symlink' === $code ) { |
| 242 | $error->remove( 'wpcomsh_theme_install_symlink' ); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | function wpcomsh_theme_install_by_symlink( $reply, $package, $upgrader ) { |
| 256 | |
| 257 | if ( ! $package || ! is_string( $package ) || str_contains( $package, '/' ) || ! $upgrader instanceof Theme_Upgrader ) { |
| 258 | return $reply; |
| 259 | } |
| 260 | |
| 261 | $wpcom_themes_service = wpcomsh_get_wpcom_themes_service_instance(); |
| 262 | $wpcom_theme = $wpcom_themes_service->get_theme( $package ); |
| 263 | |
| 264 | if ( ! $wpcom_theme ) { |
| 265 | return $reply; |
| 266 | } |
| 267 | |
| 268 | $theme_type = wpcomsh_get_wpcom_theme_type( $wpcom_theme->slug ); |
| 269 | |
| 270 | |
| 271 | if ( ! $theme_type ) { |
| 272 | return $reply; |
| 273 | } |
| 274 | |
| 275 | $upgrader->skin->feedback( 'installing_package' ); |
| 276 | $result = wpcomsh_install_managed_theme( $wpcom_theme->slug, $theme_type ); |
| 277 | |
| 278 | if ( is_wp_error( $result ) && $result->get_error_code() !== 'wpcom_theme_already_installed' ) { |
| 279 | $upgrader->skin->feedback( 'process_failed' ); |
| 280 | |
| 281 | $upgrader->result = $result; |
| 282 | |
| 283 | return $result; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | $result = array( |
| 288 | 'destination_name' => $wpcom_theme->slug, |
| 289 | ); |
| 290 | $upgrader->result = $result; |
| 291 | |
| 292 | $upgrader->skin->result = $result; |
| 293 | $upgrader->skin->feedback( 'process_success' ); |
| 294 | |
| 295 | |
| 296 | add_action( 'wp_error_added', 'wpcomsh_remove_symlink_wp_error', 0, 4 ); |
| 297 | |
| 298 | |
| 299 | return new WP_Error( 'wpcomsh_theme_install_symlink' ); |
| 300 | } |
| 301 | add_filter( 'upgrader_pre_download', 'wpcomsh_theme_install_by_symlink', 0, 4 ); |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | function wpcomsh_theme_install_by_download( $reply, $package, $upgrader ) { |
| 313 | if ( false !== $reply || ! $package || ! is_string( $package ) || str_contains( $package, '/' ) || ! $upgrader instanceof Theme_Upgrader ) { |
| 314 | return $reply; |
| 315 | } |
| 316 | |
| 317 | $wpcom_theme = wpcomsh_get_wpcom_themes_service_instance()->get_theme( $package ); |
| 318 | if ( ! $wpcom_theme ) { |
| 319 | return $reply; |
| 320 | } |
| 321 | |
| 322 | $download_link = wpcomsh_get_marketplace_theme_download_link( $wpcom_theme->slug ); |
| 323 | if ( '' === $download_link ) { |
| 324 | return new WP_Error( 'wpcom_theme_not_installable', __( 'This theme could not be installed.', 'wpcomsh' ) ); |
| 325 | } |
| 326 | |
| 327 | return download_url( $download_link ); |
| 328 | } |
| 329 | add_filter( 'upgrader_pre_download', 'wpcomsh_theme_install_by_download', 1, 4 ); |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | function wpcomsh_get_marketplace_theme_download_link( string $slug ): string { |
| 339 | $response = Automattic\Jetpack\Connection\Client::wpcom_json_api_request_as_blog( '/marketplace/themes/updates', '2', array(), null, 'wpcom' ); |
| 340 | if ( is_wp_error( $response ) ) { |
| 341 | return ''; |
| 342 | } |
| 343 | |
| 344 | $body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 345 | foreach ( $body['updates'] ?? array() as $update ) { |
| 346 | if ( is_array( $update ) && ( $update['slug'] ?? '' ) === $slug && ! empty( $update['download_link'] ) ) { |
| 347 | return (string) $update['download_link']; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return ''; |
| 352 | } |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | function wpcomsh_delete_managed_wpcom_theme( string $stylesheet ) { |
| 362 | if ( wpcomsh_is_theme_symlinked( $stylesheet ) ) { |
| 363 | wpcomsh_jetpack_wpcom_theme_delete( false, $stylesheet ); |
| 364 | } |
| 365 | } |
| 366 | add_action( 'delete_theme', 'wpcomsh_delete_managed_wpcom_theme' ); |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | function wpcomsh_include_themes_creation_date( $args ) { |
| 376 | $args['fields']['creation_time'] = true; |
| 377 | |
| 378 | return $args; |
| 379 | } |
| 380 | add_filter( 'install_themes_table_api_args_new', 'wpcomsh_include_themes_creation_date' ); |
| 381 | add_filter( 'install_themes_table_api_args_search', 'wpcomsh_include_themes_creation_date' ); |