| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace Automattic\Jetpack\Extensions\ImageStudio; |
| 9 | |
| 10 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 11 | use Automattic\Jetpack\Status; |
| 12 | use Automattic\Jetpack\Status\Host; |
| 13 | use Automattic\Jetpack\Status\Visitor; |
| 14 | use function Automattic\Jetpack\Extensions\Shared\determine_iso_639_locale; |
| 15 | |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 0 ); |
| 18 | } |
| 19 | |
| 20 | require_once __DIR__ . '/../../shared/cdn-locale.php'; |
| 21 | |
| 22 | |
| 23 | |
| 24 | require_once __DIR__ . '/../../../_inc/lib/class-jetpack-ai-settings.php'; |
| 25 | |
| 26 | const FEATURE_NAME = 'image-studio'; |
| 27 | const FEATURE_CLIP_META_KEY = '_jetpack_feature_clip_id'; |
| 28 | const ASSET_BASE_PATH = 'widgets.wp.com/agents-manager/'; |
| 29 | const ASSET_JS_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.min.js'; |
| 30 | const ASSET_CSS_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.css'; |
| 31 | const ASSET_RTL_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.rtl.css'; |
| 32 | const ASSET_JSON_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.asset.json'; |
| 33 | const ASSET_JSON_PATH = ASSET_BASE_PATH . 'image-studio.asset.json'; |
| 34 | const ASSET_TRANSLATIONS_URL = 'https://' . ASSET_BASE_PATH . 'languages/'; |
| 35 | const ASSET_TRANSIENT = 'jetpack_image_studio_asset'; |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | function is_image_studio_environment_available() { |
| 47 | |
| 48 | |
| 49 | if ( ! \Jetpack_AI_Settings::apply_master_gates( true ) ) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | if ( is_ciab_environment() || is_big_sky_enabled() ) { |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | return has_jetpack_ai_features(); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | function is_image_studio_enabled() { |
| 72 | |
| 73 | |
| 74 | |
| 75 | return \Jetpack_AI_Settings::is_feature_enabled( 'image_editor' ) |
| 76 | && is_image_studio_environment_available(); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | function is_current_user_connected() { |
| 92 | |
| 93 | |
| 94 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | return ( new Connection_Manager( 'jetpack' ) )->is_user_connected(); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | function is_big_sky_enabled() { |
| 107 | return class_exists( 'Big_Sky' ) && get_option( 'big_sky_enable', '1' ); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | function is_ciab_environment() { |
| 118 | return (bool) did_action( 'next_admin_init' ); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | function signal_image_studio_active() { |
| 131 | if ( is_image_studio_enabled() ) { |
| 132 | add_filter( 'jetpack_image_studio_enabled', '__return_true', 5 ); |
| 133 | } |
| 134 | } |
| 135 | add_action( 'init', __NAMESPACE__ . '\signal_image_studio_active' ); |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | function has_jetpack_ai_features() { |
| 147 | $host = new Host(); |
| 148 | |
| 149 | if ( $host->is_wpcom_simple() ) { |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | return ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() |
| 154 | && ! ( new Status() )->is_offline_mode() |
| 155 | && \Jetpack_AI_Settings::is_ai_enabled(); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | function image_studio_can_generate_video_clips() { |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | if ( ! is_image_studio_enabled() ) { |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | if ( ! \Jetpack_AI_Settings::is_feature_enabled( 'feature_clip' ) ) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | if ( function_exists( 'wpcom_site_can_upload_videos' ) && ! wpcom_site_can_upload_videos() ) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | $override = apply_filters( 'jetpack_image_studio_can_generate_video_clips', null ); |
| 199 | if ( null !== $override ) { |
| 200 | return (bool) $override; |
| 201 | } |
| 202 | |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | function is_block_editor() { |
| 212 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | $screen = get_current_screen(); |
| 217 | return $screen && $screen->is_block_editor(); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | function is_media_library() { |
| 226 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | $screen = get_current_screen(); |
| 231 | return $screen && 'upload' === $screen->base; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | function register_plugin() { |
| 243 | if ( ! is_image_studio_enabled() ) { |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | \Jetpack_Gutenberg::set_extension_available( FEATURE_NAME ); |
| 248 | } |
| 249 | add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugin' ); |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | function feature_clip_meta_auth_callback( $allowed, $meta_key, $object_id ) { |
| 262 | unset( $allowed, $meta_key ); |
| 263 | return current_user_can( 'edit_post', (int) $object_id ); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | function register_feature_clip_post_meta() { |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | if ( ! is_image_studio_environment_available() |
| 282 | || ! \Jetpack_AI_Settings::is_feature_enabled( 'feature_clip' ) |
| 283 | ) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | register_post_meta( |
| 288 | 'post', |
| 289 | FEATURE_CLIP_META_KEY, |
| 290 | array( |
| 291 | 'type' => 'integer', |
| 292 | 'description' => 'Attachment ID of the generated video clip designated as this post\'s feature clip.', |
| 293 | 'single' => true, |
| 294 | 'default' => 0, |
| 295 | 'show_in_rest' => true, |
| 296 | 'sanitize_callback' => 'absint', |
| 297 | 'auth_callback' => __NAMESPACE__ . '\feature_clip_meta_auth_callback', |
| 298 | ) |
| 299 | ); |
| 300 | } |
| 301 | add_action( 'init', __NAMESPACE__ . '\register_feature_clip_post_meta' ); |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | function get_asset_data() { |
| 313 | $cached = get_transient( ASSET_TRANSIENT ); |
| 314 | if ( false !== $cached ) { |
| 315 | return $cached; |
| 316 | } |
| 317 | |
| 318 | $data = get_asset_data_from_file(); |
| 319 | if ( false === $data ) { |
| 320 | $data = get_asset_data_from_remote(); |
| 321 | } |
| 322 | |
| 323 | if ( false === $data ) { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | if ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) { |
| 328 | set_transient( ASSET_TRANSIENT, $data, HOUR_IN_SECONDS ); |
| 329 | } |
| 330 | return $data; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | function get_asset_data_from_file() { |
| 341 | $local_path = ABSPATH . ASSET_JSON_PATH; |
| 342 | if ( ! file_exists( $local_path ) ) { |
| 343 | return false; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | $contents = file_get_contents( $local_path ); |
| 348 | if ( false === $contents ) { |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | $data = json_decode( $contents, true ); |
| 353 | if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $data ) ) { |
| 354 | return false; |
| 355 | } |
| 356 | |
| 357 | return $data; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | function get_asset_data_from_remote() { |
| 368 | $response = wp_safe_remote_get( ASSET_JSON_URL ); |
| 369 | if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | $content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
| 374 | if ( is_string( $content_type ) && false === stripos( $content_type, 'json' ) ) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | $body = wp_remote_retrieve_body( $response ); |
| 379 | $data = json_decode( $body, true ); |
| 380 | if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $data ) ) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | return $data; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | function get_tracking_blog_id() { |
| 393 | $blog_id = ( new Host() )->get_wpcom_site_id(); |
| 394 | |
| 395 | return $blog_id ? absint( $blog_id ) : 0; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | function get_tracking_site_type() { |
| 404 | $host = new Host(); |
| 405 | |
| 406 | if ( $host->is_wpcom_simple() ) { |
| 407 | return 'simple'; |
| 408 | } |
| 409 | |
| 410 | if ( $host->is_woa_site() ) { |
| 411 | return 'atomic'; |
| 412 | } |
| 413 | |
| 414 | return 'jetpack'; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | function is_tracking_automattician() { |
| 423 | $visitor = new Visitor(); |
| 424 | |
| 425 | |
| 426 | |
| 427 | if ( ! method_exists( $visitor, 'is_tracking_automattician' ) ) { |
| 428 | return ( function_exists( 'is_automattician' ) && \is_automattician() ) |
| 429 | || ( function_exists( 'wpcom_is_proxied_request' ) && \wpcom_is_proxied_request() ) |
| 430 | || ( defined( 'AT_PROXIED_REQUEST' ) && AT_PROXIED_REQUEST ); |
| 431 | } |
| 432 | |
| 433 | return $visitor->is_tracking_automattician(); |
| 434 | } |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | function do_enqueue_assets() { |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | if ( ! is_image_studio_enabled() || ! is_current_user_connected() ) { |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | $asset_data = get_asset_data(); |
| 451 | if ( ! $asset_data ) { |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | $version = $asset_data['version'] ?? false; |
| 456 | $dependencies = $asset_data['dependencies'] ?? array(); |
| 457 | $locale = determine_iso_639_locale(); |
| 458 | |
| 459 | if ( 'en' !== $locale ) { |
| 460 | |
| 461 | |
| 462 | |
| 463 | wp_enqueue_script( |
| 464 | 'agents-manager-translations', |
| 465 | ASSET_TRANSLATIONS_URL . $locale . '-v1.js', |
| 466 | array( 'wp-i18n' ), |
| 467 | $version, |
| 468 | true |
| 469 | ); |
| 470 | |
| 471 | $dependencies[] = 'agents-manager-translations'; |
| 472 | } |
| 473 | |
| 474 | wp_enqueue_script( |
| 475 | FEATURE_NAME, |
| 476 | ASSET_JS_URL, |
| 477 | $dependencies, |
| 478 | $version, |
| 479 | true |
| 480 | ); |
| 481 | |
| 482 | $image_studio_data = array( |
| 483 | 'enabled' => true, |
| 484 | 'version' => '1.0', |
| 485 | 'blogId' => get_tracking_blog_id(), |
| 486 | 'siteType' => get_tracking_site_type(), |
| 487 | 'isA11n' => is_tracking_automattician(), |
| 488 | 'isDevMode' => jetpack_is_internal_testing_environment(), |
| 489 | 'canGenerateVideoClips' => image_studio_can_generate_video_clips(), |
| 490 | ); |
| 491 | |
| 492 | wp_add_inline_script( |
| 493 | FEATURE_NAME, |
| 494 | 'if ( typeof window.imageStudioData === "undefined" ) { window.imageStudioData = ' . wp_json_encode( $image_studio_data, JSON_HEX_TAG | JSON_HEX_AMP ) . '; }', |
| 495 | 'before' |
| 496 | ); |
| 497 | |
| 498 | wp_enqueue_style( |
| 499 | FEATURE_NAME . '-style', |
| 500 | is_rtl() ? ASSET_RTL_URL : ASSET_CSS_URL, |
| 501 | array( 'wp-components' ), |
| 502 | $version |
| 503 | ); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | function enqueue_image_studio() { |
| 512 | if ( ! is_block_editor() ) { |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | do_enqueue_assets(); |
| 517 | } |
| 518 | add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_image_studio' ); |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | function enqueue_image_studio_admin() { |
| 526 | if ( ! is_media_library() ) { |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | do_enqueue_assets(); |
| 531 | } |
| 532 | add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_image_studio_admin' ); |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | |
| 541 | |
| 542 | |
| 543 | |
| 544 | function add_image_studio_row_action( $actions, $post ) { |
| 545 | |
| 546 | $supported_mime_types = array( |
| 547 | 'image/jpeg', |
| 548 | 'image/jpg', |
| 549 | 'image/png', |
| 550 | 'image/webp', |
| 551 | 'image/bmp', |
| 552 | 'image/tiff', |
| 553 | ); |
| 554 | |
| 555 | if ( ! in_array( $post->post_mime_type, $supported_mime_types, true ) ) { |
| 556 | return $actions; |
| 557 | } |
| 558 | |
| 559 | if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
| 560 | return $actions; |
| 561 | } |
| 562 | |
| 563 | $link = sprintf( |
| 564 | '<a href="#" class="big-sky-image-studio-link" data-attachment-id="%d">%s</a>', |
| 565 | absint( $post->ID ), |
| 566 | esc_html__( 'Edit with AI', 'jetpack' ) |
| 567 | ); |
| 568 | |
| 569 | |
| 570 | $new_actions = array(); |
| 571 | foreach ( $actions as $key => $value ) { |
| 572 | if ( 'edit' === $key ) { |
| 573 | $new_actions['edit-with-ai'] = $link; |
| 574 | } |
| 575 | $new_actions[ $key ] = $value; |
| 576 | } |
| 577 | |
| 578 | if ( ! isset( $new_actions['edit-with-ai'] ) ) { |
| 579 | $new_actions['edit-with-ai'] = $link; |
| 580 | } |
| 581 | |
| 582 | return $new_actions; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | function register_row_action() { |
| 591 | if ( ! is_image_studio_enabled() || ! is_current_user_connected() || ! is_media_library() ) { |
| 592 | return; |
| 593 | } |
| 594 | |
| 595 | add_filter( 'media_row_actions', __NAMESPACE__ . '\add_image_studio_row_action', 10, 2 ); |
| 596 | } |
| 597 | add_action( 'current_screen', __NAMESPACE__ . '\register_row_action' ); |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | function get_ai_image_extensions() { |
| 605 | return array( |
| 606 | 'ai-featured-image-generator', |
| 607 | 'ai-assistant-image-extension', |
| 608 | 'ai-general-purpose-image-generator', |
| 609 | 'ai-assistant-experimental-image-generation-support', |
| 610 | ); |
| 611 | } |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | function disable_jetpack_ai_image_extensions() { |
| 622 | if ( ! \Jetpack_Gutenberg::is_available( FEATURE_NAME ) ) { |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | foreach ( get_ai_image_extensions() as $extension ) { |
| 627 | \Jetpack_Gutenberg::set_extension_unavailable( $extension, 'image_studio_active' ); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\disable_jetpack_ai_image_extensions', 99 ); |