Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/trunk' into update/sso-jetpack-d…
Browse files Browse the repository at this point in the history
…ependencies-1
  • Loading branch information
jeherve committed Apr 30, 2024
2 parents 024c01d + df039de commit 54cebb1
Show file tree
Hide file tree
Showing 184 changed files with 925 additions and 374 deletions.
23 changes: 23 additions & 0 deletions .github/files/lint-project-structure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,29 @@ for FILE in $(git -c core.quotepath=off ls-files 'projects/packages/**/block.jso
fi
done

# - In phpcs config, `<rule ref="Standard.Category.Sniff.Message"><severity>0</severity></rule>` doesn't do what you think.
debug "Checking for bad message exclusions in phpcs configs"
for FILE in $(git -c core.quotepath=off ls-files .phpcs.config.xml .phpcs.xml.dist .github/files/php-linting-phpcs.xml .github/files/phpcompatibility-dev-phpcs.xml '*/.phpcs.dir.xml' '*/.phpcs.dir.phpcompatibility.xml'); do
while IFS=$'\t' read -r LINE REF; do
EXIT=1
echo "::error file=$FILE,line=$LINE::PHPCS config attempts to set severity 0 for the sniff message \"$REF\". To exclude a single message from a sniff, use \`<rule ref=\"${REF%.*}\"><exclude name=\"$REF\"/></rule>\` instead."
done < <( php -- "$FILE" <<-'PHPDOC'
<?php
$doc = new DOMDocument();
$doc->load( $argv[1] );
$xpath = new DOMXPath( $doc );
function has_message( $v ) {
return count( explode(".", $v[0]->value) ) >= 4;
}
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPHPFunctions( "has_message" );
foreach ( $xpath->evaluate( "//rule[php:function(\"has_message\", @ref)][severity[normalize-space(.)=\"0\"]]" ) as $node ) {
echo "{$node->getLineNo()}\t{$node->getAttribute("ref")}\n";
}
PHPDOC
)
done

# - .nvmrc should match .github/versions.sh.
debug "Checking .nvmrc vs versions.sh"
if [[ "$(<.nvmrc)" != "$NODE_VERSION" ]]; then
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/update-phan-stubs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,24 @@ jobs:
id: changes
run: |
if git diff --exit-code; then
echo "No changes"
echo "::notice::No changes"
echo "needed=false" >> "$GITHUB_OUTPUT"
elif ! [[ "$(date +%e)" -le 7 && "$(date +%A)" == Thursday ]] && git diff --exit-code --ignore-matching-lines='^ \* Stubs automatically generated from' >/dev/null; then
echo "::notice::No changes (other than version bumps) and it's not the first Thursday"
echo "needed=false" >> "$GITHUB_OUTPUT"
elif git -c protocol.version=2 fetch --no-tags --prune --progress --no-recurse-submodules --depth=1 origin update/phan-custom-stubs >/dev/null; then
TMP=$( git -c core.quotepath=off diff --name-only )
mapfile -t FILES <<<"$TMP"
if git diff --quiet --exit-code origin/update/phan-custom-stubs "${FILES[@]}"; then
echo "Branch already exists and is up to date with these changes"
echo "::notice::Branch already exists and is up to date with these changes"
echo "needed=false" >> "$GITHUB_OUTPUT"
else
echo "::notice::Branch needs updating"
echo "has-branch=true" >> "$GITHUB_OUTPUT"
echo "needed=true" >> "$GITHUB_OUTPUT"
fi
else
echo "::notice::Branch needs creating"
echo "has-branch=false" >> "$GITHUB_OUTPUT"
echo "needed=true" >> "$GITHUB_OUTPUT"
fi
Expand Down
2 changes: 1 addition & 1 deletion .phan/stubs/woocommerce-internal-stubs.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Stubs automatically generated from WooCommerce 8.8.2
* Stubs automatically generated from WooCommerce 8.8.3
* using the definition file `tools/stubs/woocommerce-internal-stub-defs.php` in the Jetpack monorepo.
*
* Do not edit this directly! Run tools/stubs/update-stubs.sh to regenerate it.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

AI Featured Image: support custom user prompt on the image generation.
2 changes: 1 addition & 1 deletion projects/js-packages/ai-client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": false,
"name": "@automattic/jetpack-ai-client",
"version": "0.12.4",
"version": "0.13.0-alpha",
"description": "A JS client for consuming Jetpack AI services",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/ai-client/#readme",
"bugs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,86 @@ import requestJwt from '../../jwt/index.js';

const debug = debugFactory( 'ai-client:use-image-generator' );

/**
* Cut the post content on a given lenght so the total length of the prompt is not longer than 4000 characters.
* @param {string} content - the content to be truncated
* @param {number} currentPromptLength - the length of the prompt already in use
* @returns {string} a truncated version of the content respecting the prompt length limit
*/
const truncateContent = ( content: string, currentPromptLength: number ): string => {
const maxLength = 4000;
const remainingLength = maxLength - currentPromptLength;
// 6 is the length of the ellipsis and the space before it
return content.length > remainingLength
? content.substring( 0, remainingLength - 6 ) + ` [...]`
: content;
};

/**
* Create the prompt string based on the provided context.
* @param {string} postContent - the content of the post
* @param {string} userPrompt - the user prompt for the image generation, if provided. Max length is 1000 characters, will be truncated.
* @returns {string} the prompt string
*/
const getImageGenerationPrompt = ( postContent: string, userPrompt?: string ): string => {
/**
* If the user provide some custom prompt for the image generation,
* we will use it, add the post content as additional context and
* provide some guardrails for the generation.
*/
if ( userPrompt ) {
const imageGenerationPrompt = `I need a cover image for a blog post based on this user prompt:
${ userPrompt.length > 1000 ? userPrompt.substring( 0, 1000 ) : userPrompt }
Before creating the image, identify the main topic of the user prompt and relate it to the post content.
Do not represent the whole content in one image, keep it simple and just represent one single idea.
Do not add details, detailed explanations or highlights from the content, just represent the main idea as if it was a photograph.
Do not use collages or compositions with multiple elements or scenes. Stick to one single scene. Do not compose unrealistic scenes.
If the content describes facts, objects or concepts from the real world, represent them on a realistic style and do not make unreal compositions.
If the content is more abstract, use a more abstract style to represent the main idea.
Make sure the light and the style are visually appealing.
Do not add text to the image.
For additional context, this is the post content:
`;
// truncating the content so the whole prompt is not longer than 4000 characters, the model limit.
return imageGenerationPrompt + truncateContent( postContent, imageGenerationPrompt.length );
}

/**
* When the user does not provide a custom prompt, we will use the
* standard one, based solely on the post content.
*/
const imageGenerationPrompt = `I need a cover image for a blog post.
Before creating the image, identify the main topic of the content and only represent it.
Do not represent the whole content in one image, keep it simple and just represent one single idea.
Do not add details, detailed explanations or highlights from the content, just represent the main idea as if it was a photograph.
Do not use collages or compositions with multiple elements or scenes. Stick to one single scene. Do not compose unrealistic scenes.
If the content describes facts, objects or concepts from the real world, represent them on a realistic style and do not make unreal compositions.
If the content is more abstract, use a more abstract style to represent the main idea.
Make sure the light and the style are visually appealing.
Do not add text to the image.
This is the post content:
`;
// truncating the content so the whole prompt is not longer than 4000 characters, the model limit.
return imageGenerationPrompt + truncateContent( postContent, imageGenerationPrompt.length );
};

const useImageGenerator = () => {
const generateImage = async function ( {
feature,
postContent,
responseFormat = 'url',
userPrompt,
}: {
feature: string;
postContent: string;
responseFormat?: 'url' | 'b64_json';
userPrompt?: string;
} ): Promise< { data: Array< { [ key: string ]: string } > } > {
let token = '';

Expand All @@ -31,21 +102,7 @@ const useImageGenerator = () => {
try {
debug( 'Generating image' );

// TODO: fine tune the prompt as we move forward
const imageGenerationPrompt =
`I need a cover image for a blog post.
Before creating the image, identify the main topic of the content and only represent it.
Do not represent the whole content in one image, keep it simple and just represent one single idea.
Do not add details, detailed explanations or highlights from the content, just represent the main idea as if it was a photograph.
Do not use collages or compositions with multiple elements or scenes. Stick to one single scene. Do not compose unrealistic scenes.
If the content describes facts, objects or concepts from the real world, represent them on a realistic style and do not make unreal compositions.
If the content is more abstract, use a more abstract style to represent the main idea.
Make sure the light and the style are visually appealing.
Do not add text to the image.
This is the post content:
` + ( postContent.length > 3000 ? postContent.substring( 0, 3000 ) + ` [...]` : postContent ); // truncating the content so the whole prompt is not longer than 4000 characters, the model limit.
const imageGenerationPrompt = getImageGenerationPrompt( postContent, userPrompt );

const URL = 'https://public-api.wordpress.com/wpcom/v2/jetpack-ai-image';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Stop building broken storybook stories.


5 changes: 5 additions & 0 deletions projects/packages/phpcs-filter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ You can still exclude such a rule by setting its severity to zero. The 4-compone
<!-- This works too. -->
<exclude name="Standard.Category.Rule.Message" />
</rule>

<!-- P.S. Don't do this, it doesn't work like you'd expect (it implicitly sets severity 0 for Standard.Category.Rule too). Use `<exclude>` as above for individual messages. -->
<rule ref="Standard.Category.Rule.Message">
<severity>0</severity>
</rule>
```

### `<file>`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add a doc note warning against using `<severity>0</severity>` when excluding individual sniff messages.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Add Woocommerce event remove_order_items to Jetpack Sync
2 changes: 1 addition & 1 deletion projects/packages/sync/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"link-template": "https://github.com/Automattic/jetpack-sync/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-trunk": "2.14.x-dev"
"dev-trunk": "2.15.x-dev"
}
},
"config": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/sync/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '2.14.1-alpha';
const PACKAGE_VERSION = '2.15.0-alpha';

const PACKAGE_SLUG = 'sync';

Expand Down
24 changes: 24 additions & 0 deletions projects/packages/sync/src/modules/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Automattic\Jetpack\Sync\Modules;

use WC_Order;
use WP_Error;

/**
Expand Down Expand Up @@ -93,6 +94,9 @@ public function __construct() {

// Blacklist Action Scheduler comment types.
add_filter( 'jetpack_sync_prevent_sending_comment_data', array( $this, 'filter_action_scheduler_comments' ), 10, 2 );

// Preprocess action to be sent by Jetpack sync.
add_action( 'woocommerce_remove_order_items', array( $this, 'action_woocommerce_remove_order_items' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -128,6 +132,7 @@ public function init_listeners( $callable ) {
add_action( 'woocommerce_new_order_item', $callable, 10, 4 );
add_action( 'woocommerce_update_order_item', $callable, 10, 4 );
add_action( 'woocommerce_delete_order_item', $callable, 10, 1 );
add_action( 'woocommerce_remove_order_item_ids', $callable, 10, 1 );
$this->init_listeners_for_meta_type( 'order_item', $callable );

// Payment tokens.
Expand Down Expand Up @@ -197,6 +202,25 @@ public function filter_order_item( $args ) {
return $args;
}

/**
* Retrieve the order item ids to be removed and send them as one action
*
* @param WC_Order $order The order argument.
* @param string $type Order item type.
*/
public function action_woocommerce_remove_order_items( WC_Order $order, $type ) {
if ( $type ) {
$order_items = $order->get_items( $type );
} else {
$order_items = $order->get_items();
}
$order_item_ids = array_keys( $order_items );

if ( $order_item_ids ) {
do_action( 'woocommerce_remove_order_item_ids', $order_item_ids );
}
}

/**
* Expand order item IDs to order items and their meta.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Stop building broken storybook stories.


6 changes: 4 additions & 2 deletions projects/packages/waf/.phpcs.dir.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
<!-- Some code here runs outside of WordPress code. -->
<!-- TODO: Split that by directory or something so we can do this only for the non-WordPress code. -->
<rule ref="Jetpack-NoWP" />
<rule ref="WordPress.PHP.DevelopmentFunctions.error_log_var_export">
<severity>0</severity>
<rule ref="WordPress.PHP.DevelopmentFunctions">
<exclude name="WordPress.PHP.DevelopmentFunctions.error_log_error_log"/>
<exclude name="WordPress.PHP.DevelopmentFunctions.error_log_var_dump"/>
<exclude name="WordPress.PHP.DevelopmentFunctions.error_log_var_export"/>
</rule>

</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Fix phpcs config. No change to functionality.


4 changes: 2 additions & 2 deletions projects/packages/wp-js-data-sync/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
// PhanUndeclaredMethod : 3 occurrences
// PhanPluginDuplicateConditionalNullCoalescing : 2 occurrences
// PhanTypeMismatchArgumentProbablyReal : 2 occurrences
// PhanTypeMismatchReturn : 2 occurrences
// PhanImpossibleCondition : 1 occurrence
// PhanImpossibleTypeComparison : 1 occurrence
// PhanParamTooMany : 1 occurrence
// PhanPluginSimplifyExpressionBool : 1 occurrence
// PhanRedundantCondition : 1 occurrence
// PhanTypeMismatchArgumentNullable : 1 occurrence
// PhanTypeMismatchProperty : 1 occurrence
// PhanTypeMismatchReturn : 1 occurrence
// PhanUndeclaredTypeParameter : 1 occurrence
// PhanUnreferencedUseNormal : 1 occurrence

Expand All @@ -38,7 +38,7 @@
'src/schema/types/class-type-assoc-array.php' => ['PhanTypeMismatchArgumentNullable', 'PhanTypeMismatchReturn'],
'src/schema/types/class-type-string.php' => ['PhanImpossibleTypeComparison'],
'tests/php/schema/integration/test-integration-fallback-values.php' => ['PhanNonClassMethodCall'],
'tests/php/schema/integration/test-integration-parsing-errors.php' => ['PhanNonClassMethodCall', 'PhanParamTooFew', 'PhanTypeMismatchReturn'],
'tests/php/schema/integration/test-integration-parsing-errors.php' => ['PhanNonClassMethodCall', 'PhanParamTooFew'],
'tests/php/schema/type/test-type-assoc-array.php' => ['PhanTypeMismatchArgumentProbablyReal'],
],
// 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed.
Expand Down
41 changes: 18 additions & 23 deletions projects/packages/wp-js-data-sync/.phpcs.dir.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,30 @@
</properties>
</rule>

<rule ref="MediaWiki.Usage.ForbiddenFunctions.isset">
<severity>0</severity>
<rule ref="MediaWiki.Usage.ForbiddenFunctions">
<exclude name="MediaWiki.Usage.ForbiddenFunctions.isset"/>
</rule>

<rule ref="Squiz.Commenting.FunctionComment.MissingParamComment">
<severity>0</severity>
<rule ref="Squiz.Commenting.FunctionComment">
<exclude name="Squiz.Commenting.FunctionComment.MissingParamComment"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamName"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<exclude name="Squiz.Commenting.FunctionComment.MissingReturn"/>
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop"/>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.MissingParamName">
<severity>0</severity>
<rule ref="Generic.Commenting.DocComment">
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.MissingParamTag">
<severity>0</severity>
<rule ref="Squiz.Commenting.FunctionComment">
<exclude name="Squiz.Commenting.FunctionComment.Missing"/>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.MissingReturn">
<severity>0</severity>
<rule ref="Squiz.Commenting.ClassComment">
<exclude name="Squiz.Commenting.ClassComment.Missing"/>
</rule>
<rule ref="Generic.Commenting.DocComment.MissingShort">
<severity>0</severity>
<rule ref="Squiz.Commenting.FileComment">
<exclude name="Squiz.Commenting.FileComment.Missing"/>
</rule>
<rule ref="Squiz.Commenting.FunctionComment.Missing">
<severity>0</severity>
</rule>
<rule ref="Squiz.Commenting.ClassComment.Missing">
<severity>0</severity>
</rule>
<rule ref="Squiz.Commenting.FileComment.Missing">
<severity>0</severity>
</rule>
<rule ref="Squiz.Commenting.VariableComment.Missing">
<severity>0</severity>
<rule ref="Squiz.Commenting.VariableComment">
<exclude name="Squiz.Commenting.VariableComment.Missing"/>
</rule>
</ruleset>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix phpcs config, and phpdoc comments that were easy to fix.
Loading

0 comments on commit 54cebb1

Please sign in to comment.