Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/jetpack/extensio.../plugins/image-st...
File: image-studio.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Block Editor & Media Library - Image Studio plugin feature.
[2] Fix | Delete
*
[3] Fix | Delete
* @package automattic/jetpack
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
namespace Automattic\Jetpack\Extensions\ImageStudio;
[7] Fix | Delete
[8] Fix | Delete
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
[9] Fix | Delete
use Automattic\Jetpack\Status;
[10] Fix | Delete
use Automattic\Jetpack\Status\Host;
[11] Fix | Delete
[12] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[13] Fix | Delete
exit( 0 );
[14] Fix | Delete
}
[15] Fix | Delete
[16] Fix | Delete
const FEATURE_NAME = 'image-studio';
[17] Fix | Delete
const ASSET_BASE_PATH = 'widgets.wp.com/agents-manager/';
[18] Fix | Delete
const ASSET_JS_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.min.js';
[19] Fix | Delete
const ASSET_CSS_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.css';
[20] Fix | Delete
const ASSET_RTL_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.rtl.css';
[21] Fix | Delete
const ASSET_JSON_URL = 'https://' . ASSET_BASE_PATH . 'image-studio.asset.json';
[22] Fix | Delete
const ASSET_JSON_PATH = ASSET_BASE_PATH . 'image-studio.asset.json';
[23] Fix | Delete
const ASSET_TRANSLATIONS_URL = 'https://' . ASSET_BASE_PATH . 'languages/';
[24] Fix | Delete
const ASSET_TRANSIENT = 'jetpack_image_studio_asset';
[25] Fix | Delete
[26] Fix | Delete
/**
[27] Fix | Delete
* Check if Image Studio is enabled.
[28] Fix | Delete
*
[29] Fix | Delete
* Requires AI features (Big Sky or AI Assistant) plus at least one of:
[30] Fix | Delete
* - The unified chat experience (agents_manager_use_unified_experience).
[31] Fix | Delete
* - The jetpack_image_studio_enabled filter.
[32] Fix | Delete
*
[33] Fix | Delete
* @return bool
[34] Fix | Delete
*/
[35] Fix | Delete
function is_image_studio_enabled() {
[36] Fix | Delete
if ( ! has_ai_features() ) {
[37] Fix | Delete
return false;
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
return apply_filters( 'agents_manager_use_unified_experience', false )
[41] Fix | Delete
|| apply_filters( 'jetpack_image_studio_enabled', false );
[42] Fix | Delete
}
[43] Fix | Delete
[44] Fix | Delete
/**
[45] Fix | Delete
* Check whether AI features are available.
[46] Fix | Delete
*
[47] Fix | Delete
* - wpcom simple: always available.
[48] Fix | Delete
* - Atomic: requires Big Sky or AI Assistant feature flags.
[49] Fix | Delete
* - Self-hosted: requires a connected owner with AI not disabled
[50] Fix | Delete
* (same conditions the AI Assistant plugin uses to register).
[51] Fix | Delete
*
[52] Fix | Delete
* @return bool
[53] Fix | Delete
*/
[54] Fix | Delete
function has_ai_features() {
[55] Fix | Delete
$host = new Host();
[56] Fix | Delete
[57] Fix | Delete
if ( $host->is_wpcom_simple() ) {
[58] Fix | Delete
return true;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
return ( new Connection_Manager( 'jetpack' ) )->has_connected_owner()
[62] Fix | Delete
&& ! ( new Status() )->is_offline_mode()
[63] Fix | Delete
&& apply_filters( 'jetpack_ai_enabled', true );
[64] Fix | Delete
}
[65] Fix | Delete
[66] Fix | Delete
/**
[67] Fix | Delete
* Check if the current screen is a block editor (Post Editor or Site Editor).
[68] Fix | Delete
*
[69] Fix | Delete
* @return bool
[70] Fix | Delete
*/
[71] Fix | Delete
function is_block_editor() {
[72] Fix | Delete
if ( ! function_exists( 'get_current_screen' ) ) {
[73] Fix | Delete
return false;
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
$screen = get_current_screen();
[77] Fix | Delete
return $screen && $screen->is_block_editor();
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
/**
[81] Fix | Delete
* Check if the current screen is the Media Library.
[82] Fix | Delete
*
[83] Fix | Delete
* @return bool
[84] Fix | Delete
*/
[85] Fix | Delete
function is_media_library() {
[86] Fix | Delete
if ( ! function_exists( 'get_current_screen' ) ) {
[87] Fix | Delete
return false;
[88] Fix | Delete
}
[89] Fix | Delete
[90] Fix | Delete
$screen = get_current_screen();
[91] Fix | Delete
return $screen && 'upload' === $screen->base;
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
/**
[95] Fix | Delete
* Determine if Image Studio should load on the current screen.
[96] Fix | Delete
*
[97] Fix | Delete
* - Media Library: load if either filter is true.
[98] Fix | Delete
* - Block editors (Post/Site Editor): load if either filter is true.
[99] Fix | Delete
* - Other screens: don't load.
[100] Fix | Delete
*
[101] Fix | Delete
* @return bool
[102] Fix | Delete
*/
[103] Fix | Delete
function should_load_on_current_screen() {
[104] Fix | Delete
return is_media_library() || is_block_editor();
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/**
[108] Fix | Delete
* Register the Image Studio plugin.
[109] Fix | Delete
*
[110] Fix | Delete
* Registers unconditionally when either filter is true. Screen-level gating
[111] Fix | Delete
* happens at enqueue time since get_current_screen() is not available here.
[112] Fix | Delete
*
[113] Fix | Delete
* @return void
[114] Fix | Delete
*/
[115] Fix | Delete
function register_plugin() {
[116] Fix | Delete
if ( ! is_image_studio_enabled() ) {
[117] Fix | Delete
return;
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
\Jetpack_Gutenberg::set_extension_available( FEATURE_NAME );
[121] Fix | Delete
}
[122] Fix | Delete
add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugin' );
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Fetch and cache the remote asset manifest.
[126] Fix | Delete
*
[127] Fix | Delete
* On WordPress.com, the asset file may be accessible on the local filesystem
[128] Fix | Delete
* (under ABSPATH). This avoids an HTTP round-trip and works on sandboxes where
[129] Fix | Delete
* outbound requests to widgets.wp.com may be blocked.
[130] Fix | Delete
*
[131] Fix | Delete
* @return array|false The decoded asset data, or false on failure.
[132] Fix | Delete
*/
[133] Fix | Delete
function get_asset_data() {
[134] Fix | Delete
$cached = get_transient( ASSET_TRANSIENT );
[135] Fix | Delete
if ( false !== $cached ) {
[136] Fix | Delete
return $cached;
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
$data = get_asset_data_from_file();
[140] Fix | Delete
if ( false === $data ) {
[141] Fix | Delete
$data = get_asset_data_from_remote();
[142] Fix | Delete
}
[143] Fix | Delete
[144] Fix | Delete
if ( false === $data ) {
[145] Fix | Delete
return false;
[146] Fix | Delete
}
[147] Fix | Delete
[148] Fix | Delete
if ( ! ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) {
[149] Fix | Delete
set_transient( ASSET_TRANSIENT, $data, HOUR_IN_SECONDS );
[150] Fix | Delete
}
[151] Fix | Delete
return $data;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Try to read the asset manifest from the local filesystem.
[156] Fix | Delete
*
[157] Fix | Delete
* On WordPress.com, widgets.wp.com assets are available at ABSPATH.
[158] Fix | Delete
*
[159] Fix | Delete
* @return array|false The decoded asset data, or false if not available locally.
[160] Fix | Delete
*/
[161] Fix | Delete
function get_asset_data_from_file() {
[162] Fix | Delete
$local_path = ABSPATH . ASSET_JSON_PATH;
[163] Fix | Delete
if ( ! file_exists( $local_path ) ) {
[164] Fix | Delete
return false;
[165] Fix | Delete
}
[166] Fix | Delete
[167] Fix | Delete
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading a local file, not a remote URL.
[168] Fix | Delete
$contents = file_get_contents( $local_path );
[169] Fix | Delete
if ( false === $contents ) {
[170] Fix | Delete
return false;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
$data = json_decode( $contents, true );
[174] Fix | Delete
if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $data ) ) {
[175] Fix | Delete
return false;
[176] Fix | Delete
}
[177] Fix | Delete
[178] Fix | Delete
return $data;
[179] Fix | Delete
}
[180] Fix | Delete
[181] Fix | Delete
/**
[182] Fix | Delete
* Fetch the asset manifest via HTTP.
[183] Fix | Delete
*
[184] Fix | Delete
* Used as a fallback when the file is not available locally (e.g. self-hosted sites).
[185] Fix | Delete
*
[186] Fix | Delete
* @return array|false The decoded asset data, or false on failure.
[187] Fix | Delete
*/
[188] Fix | Delete
function get_asset_data_from_remote() {
[189] Fix | Delete
$response = wp_safe_remote_get( ASSET_JSON_URL );
[190] Fix | Delete
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
[191] Fix | Delete
return false;
[192] Fix | Delete
}
[193] Fix | Delete
[194] Fix | Delete
$content_type = wp_remote_retrieve_header( $response, 'content-type' );
[195] Fix | Delete
if ( is_string( $content_type ) && false === stripos( $content_type, 'json' ) ) {
[196] Fix | Delete
return false;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
$body = wp_remote_retrieve_body( $response );
[200] Fix | Delete
$data = json_decode( $body, true );
[201] Fix | Delete
if ( json_last_error() !== JSON_ERROR_NONE || ! is_array( $data ) ) {
[202] Fix | Delete
return false;
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
return $data;
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Determine the ISO 639 locale code for the current user.
[210] Fix | Delete
*
[211] Fix | Delete
* @return string The ISO 639 language code, defaulting to 'en'.
[212] Fix | Delete
*/
[213] Fix | Delete
function determine_iso_639_locale() {
[214] Fix | Delete
$language = get_user_locale();
[215] Fix | Delete
$language = strtolower( $language );
[216] Fix | Delete
[217] Fix | Delete
if ( in_array( $language, array( 'pt_br', 'pt-br', 'zh_tw', 'zh-tw', 'zh_cn', 'zh-cn' ), true ) ) {
[218] Fix | Delete
$language = str_replace( '_', '-', $language );
[219] Fix | Delete
} else {
[220] Fix | Delete
$language = preg_replace( '/([-_].*)$/i', '', $language );
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
if ( empty( $language ) ) {
[224] Fix | Delete
return 'en';
[225] Fix | Delete
}
[226] Fix | Delete
[227] Fix | Delete
return $language;
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
/**
[231] Fix | Delete
* Enqueue Image Studio script and style assets.
[232] Fix | Delete
*
[233] Fix | Delete
* @return void
[234] Fix | Delete
*/
[235] Fix | Delete
function do_enqueue_assets() {
[236] Fix | Delete
if ( ! is_image_studio_enabled() ) {
[237] Fix | Delete
return;
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
$asset_data = get_asset_data();
[241] Fix | Delete
if ( ! $asset_data ) {
[242] Fix | Delete
return;
[243] Fix | Delete
}
[244] Fix | Delete
[245] Fix | Delete
$version = $asset_data['version'] ?? false;
[246] Fix | Delete
$dependencies = $asset_data['dependencies'] ?? array();
[247] Fix | Delete
$locale = determine_iso_639_locale();
[248] Fix | Delete
[249] Fix | Delete
if ( 'en' !== $locale ) {
[250] Fix | Delete
// Load translations from widgets.wp.com.
[251] Fix | Delete
wp_enqueue_script(
[252] Fix | Delete
'image-studio-translations',
[253] Fix | Delete
ASSET_TRANSLATIONS_URL . $locale . '-v1.js',
[254] Fix | Delete
array( 'wp-i18n' ),
[255] Fix | Delete
$version,
[256] Fix | Delete
true
[257] Fix | Delete
);
[258] Fix | Delete
[259] Fix | Delete
$dependencies[] = 'image-studio-translations';
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
wp_enqueue_script(
[263] Fix | Delete
FEATURE_NAME,
[264] Fix | Delete
ASSET_JS_URL,
[265] Fix | Delete
$dependencies,
[266] Fix | Delete
$version,
[267] Fix | Delete
true
[268] Fix | Delete
);
[269] Fix | Delete
[270] Fix | Delete
wp_add_inline_script(
[271] Fix | Delete
FEATURE_NAME,
[272] Fix | Delete
'if ( typeof window.imageStudioData === "undefined" ) { window.imageStudioData = ' . wp_json_encode( array( 'enabled' => true ), JSON_HEX_TAG | JSON_HEX_AMP ) . '; }',
[273] Fix | Delete
'before'
[274] Fix | Delete
);
[275] Fix | Delete
[276] Fix | Delete
wp_enqueue_style(
[277] Fix | Delete
FEATURE_NAME . '-style',
[278] Fix | Delete
is_rtl() ? ASSET_RTL_URL : ASSET_CSS_URL,
[279] Fix | Delete
array( 'wp-components' ),
[280] Fix | Delete
$version
[281] Fix | Delete
);
[282] Fix | Delete
}
[283] Fix | Delete
[284] Fix | Delete
/**
[285] Fix | Delete
* Enqueue Image Studio assets in the block editor.
[286] Fix | Delete
*
[287] Fix | Delete
* @return void
[288] Fix | Delete
*/
[289] Fix | Delete
function enqueue_image_studio() {
[290] Fix | Delete
if ( ! is_block_editor() ) {
[291] Fix | Delete
return;
[292] Fix | Delete
}
[293] Fix | Delete
[294] Fix | Delete
do_enqueue_assets();
[295] Fix | Delete
}
[296] Fix | Delete
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_image_studio' );
[297] Fix | Delete
[298] Fix | Delete
/**
[299] Fix | Delete
* Enqueue Image Studio assets on admin screens (Media Library).
[300] Fix | Delete
*
[301] Fix | Delete
* @return void
[302] Fix | Delete
*/
[303] Fix | Delete
function enqueue_image_studio_admin() {
[304] Fix | Delete
if ( ! is_media_library() ) {
[305] Fix | Delete
return;
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
do_enqueue_assets();
[309] Fix | Delete
}
[310] Fix | Delete
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_image_studio_admin' );
[311] Fix | Delete
[312] Fix | Delete
/**
[313] Fix | Delete
* Get the list of AI image extensions that conflict with Image Studio.
[314] Fix | Delete
*
[315] Fix | Delete
* @return array
[316] Fix | Delete
*/
[317] Fix | Delete
function get_ai_image_extensions() {
[318] Fix | Delete
return array(
[319] Fix | Delete
'ai-featured-image-generator',
[320] Fix | Delete
'ai-assistant-image-extension',
[321] Fix | Delete
'ai-general-purpose-image-generator',
[322] Fix | Delete
'ai-assistant-experimental-image-generation-support',
[323] Fix | Delete
);
[324] Fix | Delete
}
[325] Fix | Delete
[326] Fix | Delete
/**
[327] Fix | Delete
* Disable Jetpack AI image extensions when Image Studio is active on the current screen.
[328] Fix | Delete
*
[329] Fix | Delete
* This hook fires on `jetpack_register_gutenberg_extensions` which may run multiple
[330] Fix | Delete
* times: once during initial module load (before get_current_screen() is available)
[331] Fix | Delete
* and again inside Jetpack_Gutenberg::get_availability() during enqueue (where the
[332] Fix | Delete
* screen IS available).
[333] Fix | Delete
*
[334] Fix | Delete
* Only disables AI extensions when we can confirm Image Studio will actually load
[335] Fix | Delete
* on the current screen (i.e. screen is available and should_load_on_current_screen()
[336] Fix | Delete
* returns true). If the screen is not available or Image Studio won't load on this
[337] Fix | Delete
* screen, AI extensions remain enabled.
[338] Fix | Delete
*
[339] Fix | Delete
* This ensures AI extensions are available on screens where Image Studio won't load
[340] Fix | Delete
* (e.g. dashboard, other non-editor screens, or early initialization).
[341] Fix | Delete
*
[342] Fix | Delete
* @return void
[343] Fix | Delete
*/
[344] Fix | Delete
function disable_jetpack_ai_image_extensions() {
[345] Fix | Delete
if ( ! is_image_studio_enabled() ) {
[346] Fix | Delete
return;
[347] Fix | Delete
}
[348] Fix | Delete
[349] Fix | Delete
// Only disable if screen is available and Image Studio will actually load.
[350] Fix | Delete
if ( ! function_exists( 'get_current_screen' ) || ! get_current_screen() || ! should_load_on_current_screen() ) {
[351] Fix | Delete
return;
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
foreach ( get_ai_image_extensions() as $extension ) {
[355] Fix | Delete
\Jetpack_Gutenberg::set_extension_unavailable( $extension, 'image_studio_active' );
[356] Fix | Delete
}
[357] Fix | Delete
}
[358] Fix | Delete
// Priority 99 ensures this runs after all AI extensions are registered at default priority.
[359] Fix | Delete
add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\disable_jetpack_ai_image_extensions', 99 );
[360] Fix | Delete
[361] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function