Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/tasty-recipes-lite/assets/dist/database-migration.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/database-migration.build.asset.php
@@ -1 +1,7 @@
-<?php return array('dependencies' => array('wp-dom-ready', 'wp-i18n'), 'version' => 'f57b32286b1f5530e02e');
+<?php return array(
+ 'dependencies' => array(
+ 'wp-dom-ready',
+ 'wp-i18n'
+ ),
+ 'version' => 'a5df2bb87f11ec0c644e'
+);
--- a/tasty-recipes-lite/assets/dist/explorer-page.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/explorer-page.build.asset.php
@@ -1 +1,11 @@
-<?php return array('dependencies' => array('react-jsx-runtime', 'wp-api-fetch', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '675843478870f9733a72');
+<?php return array(
+ 'dependencies' => array(
+ 'react-jsx-runtime',
+ 'wp-api-fetch',
+ 'wp-dom-ready',
+ 'wp-element',
+ 'wp-hooks',
+ 'wp-i18n'
+ ),
+ 'version' => '004f29b84662b4d2711e'
+);
--- a/tasty-recipes-lite/assets/dist/onboarding-wizard.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/onboarding-wizard.build.asset.php
@@ -1 +1,6 @@
-<?php return array('dependencies' => array('wp-dom-ready'), 'version' => '7fdbf077eb7e66e8882e');
+<?php return array(
+ 'dependencies' => array(
+ 'wp-dom-ready'
+ ),
+ 'version' => '546dd8219d77395364b6'
+);
--- a/tasty-recipes-lite/assets/dist/recipe-card-preview-js.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/recipe-card-preview-js.build.asset.php
@@ -1 +1,6 @@
-<?php return array('dependencies' => array(), 'version' => 'bb913293b3808a528c7f');
+<?php return array(
+ 'dependencies' => array(
+
+ ),
+ 'version' => 'bb913293b3808a528c7f'
+);
--- a/tasty-recipes-lite/assets/dist/recipe-editor-js.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/recipe-editor-js.build.asset.php
@@ -1 +1,13 @@
-<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-api-fetch', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-url'), 'version' => 'b0695d7df62a15d3a585');
+<?php return array(
+ 'dependencies' => array(
+ 'react',
+ 'react-dom',
+ 'react-jsx-runtime',
+ 'wp-api-fetch',
+ 'wp-element',
+ 'wp-hooks',
+ 'wp-i18n',
+ 'wp-url'
+ ),
+ 'version' => 'd4e955d06ee7f17797f5'
+);
--- a/tasty-recipes-lite/assets/dist/recipe-js.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/recipe-js.build.asset.php
@@ -1 +1,6 @@
-<?php return array('dependencies' => array(), 'version' => '975dd9a26e40a6121580');
+<?php return array(
+ 'dependencies' => array(
+
+ ),
+ 'version' => '5ddfea015bd8b6ab9191'
+);
--- a/tasty-recipes-lite/assets/dist/recipe-print-controls-js.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/recipe-print-controls-js.build.asset.php
@@ -1 +1,6 @@
-<?php return array('dependencies' => array(), 'version' => '2a72996b89e0eb34409a');
+<?php return array(
+ 'dependencies' => array(
+
+ ),
+ 'version' => '2a72996b89e0eb34409a'
+);
--- a/tasty-recipes-lite/assets/dist/recipes-block.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/recipes-block.build.asset.php
@@ -1 +1,14 @@
-<?php return array('dependencies' => array('react-jsx-runtime', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-plugins'), 'version' => 'e7bbd0b7eef525261620');
+<?php return array(
+ 'dependencies' => array(
+ 'react-jsx-runtime',
+ 'wp-block-editor',
+ 'wp-blocks',
+ 'wp-components',
+ 'wp-data',
+ 'wp-element',
+ 'wp-hooks',
+ 'wp-i18n',
+ 'wp-plugins'
+ ),
+ 'version' => 'a45dec4594ea3fae6e6e'
+);
--- a/tasty-recipes-lite/assets/dist/settings.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/settings.build.asset.php
@@ -1 +1,14 @@
-<?php return array('dependencies' => array('react', 'react-dom', 'react-jsx-runtime', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives'), 'version' => 'a37b07f8e440209c14e8');
+<?php return array(
+ 'dependencies' => array(
+ 'react',
+ 'react-dom',
+ 'react-jsx-runtime',
+ 'wp-api-fetch',
+ 'wp-dom-ready',
+ 'wp-element',
+ 'wp-hooks',
+ 'wp-i18n',
+ 'wp-primitives'
+ ),
+ 'version' => '9bb43e6ca4780e08aeb6'
+);
--- a/tasty-recipes-lite/assets/dist/usage-modal.build.asset.php
+++ b/tasty-recipes-lite/assets/dist/usage-modal.build.asset.php
@@ -1 +1,11 @@
-<?php return array('dependencies' => array('react-jsx-runtime', 'wp-api-fetch', 'wp-dom-ready', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => 'beadc6fdb49adcac2e17');
+<?php return array(
+ 'dependencies' => array(
+ 'react-jsx-runtime',
+ 'wp-api-fetch',
+ 'wp-dom-ready',
+ 'wp-element',
+ 'wp-hooks',
+ 'wp-i18n'
+ ),
+ 'version' => 'f9fd024072965163f1b5'
+);
--- a/tasty-recipes-lite/inc/class-assets.php
+++ b/tasty-recipes-lite/inc/class-assets.php
@@ -15,6 +15,7 @@
use Tasty_RecipesBlock_Editor;
use Tasty_RecipesSettings;
use Tasty_RecipesRecipe_Explorer;
+use Tasty_RecipesIntegrationsNutrifoxConnection as Nutrifox_Connection;
/**
* Registers all scripts and styles.
@@ -228,6 +229,7 @@
'settings' => self::get_settings_tab_options(),
'converters' => self::get_converters_data(),
'optionNames' => $option_names,
+ 'nutrifox' => Nutrifox_Connection::get_editor_data(),
)
);
}
@@ -592,6 +594,7 @@
'cooking' => $cooking_attributes,
'nutrition' => $nutrition_attributes,
),
+ 'nutrifox' => Nutrifox_Connection::get_editor_data(),
);
// Add additional keys needed by the editor.
@@ -653,6 +656,11 @@
return;
}
+ // Password-protected posts don't load the print template, so skip its assets too.
+ if ( post_password_required( get_queried_object() ) ) {
+ return;
+ }
+
$recipe_ids = Tasty_Recipes::get_recipe_ids_for_post( get_queried_object_id() );
if ( ! in_array( $recipe_id, $recipe_ids, true ) ) {
return;
@@ -761,7 +769,8 @@
}
/**
- * Gets the current post ID from query args when global $post isn't set.
+ * Gets the current post ID, using the `post` query arg only when the
+ * current user can edit that post.
*
* @return int
*/
@@ -778,6 +787,12 @@
return self::get_auto_draft_id();
}
+ // On init some post types aren't registered yet, so the capability can't be checked. Fall back
+ // in that case too; the editor screens localize this value again once the post is loaded.
+ if ( ! post_type_exists( (string) get_post_type( $param_id ) ) || ! current_user_can( 'edit_post', $param_id ) ) {
+ return $post ? $post->ID : self::get_auto_draft_id();
+ }
+
return $param_id;
}
@@ -858,6 +873,12 @@
}
}
+ $star_color = isset( $settings['star_color'] ) ? (string) $settings['star_color'] : '';
+ $comment_star_color = Ratings::get_comment_form_star_color( $star_color );
+ if ( '' !== $comment_star_color ) {
+ $css_vars['--tr-comment-star-color'] = $comment_star_color;
+ }
+
$css_vars = (array) apply_filters( 'tasty_recipes_css_vars', $css_vars, $settings );
$css_vars = array_filter(
$css_vars,
--- a/tasty-recipes-lite/inc/class-content-model.php
+++ b/tasty-recipes-lite/inc/class-content-model.php
@@ -40,8 +40,9 @@
// Prevent deletion of default taxonomy terms.
add_action( 'pre_delete_term', array( __CLASS__, 'filter_pre_delete_term' ), 10, 2 );
- // Filter taxonomy archive queries to only show recipes with parent posts.
+ // Filter taxonomy archive queries to only show recipes with published parent posts.
add_action( 'pre_get_posts', array( __CLASS__, 'filter_taxonomy_archive_query' ) );
+ add_filter( 'posts_where', array( __CLASS__, 'filter_taxonomy_archive_where' ), 10, 2 );
}
/**
@@ -408,7 +409,7 @@
}
/**
- * Fetches Nutrifox API data when the Nutrifox ID is updated.
+ * Fetches Nutrifox API data when the Nutrifox ID is updated, and skips the update when no such label exists.
*
* @param mixed $check Existing check value.
* @param int $object_id ID for the post being updated.
@@ -429,6 +430,10 @@
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get
$response = wp_remote_get( sprintf( '%s/api/recipes/%d', Tasty_Recipes::nutrifox_url(), $meta_value ) );
$response_code = wp_remote_retrieve_response_code( $response );
+ if ( ! is_wp_error( $response ) && 404 === $response_code ) {
+ // The label doesn't exist, so keep the recipe's current ID.
+ return false;
+ }
if ( ! is_wp_error( $response ) && 200 === $response_code ) {
$body = wp_remote_retrieve_body( $response );
$response_data = json_decode( $body, true );
@@ -785,7 +790,8 @@
public static function filter_template_include( $template ) {
$recipe_id = (int) get_query_var( self::get_print_query_var() );
- if ( is_singular() && $recipe_id ) {
+ // Password-protected posts fall back to the theme template, which shows the password form.
+ if ( is_singular() && $recipe_id && ! post_password_required( get_queried_object() ) ) {
$recipe_ids = Tasty_Recipes::get_recipe_ids_for_post( get_queried_object_id() );
if ( in_array( $recipe_id, $recipe_ids, true ) ) {
$template = Tasty_Recipes::get_template_path( 'recipe-print' );
@@ -796,7 +802,7 @@
}
/**
- * Modify the taxonomy archive query to only include recipes with parent posts.
+ * Modify the taxonomy archive query to only include recipes with a published parent post.
*
* @since 1.2
*
@@ -812,18 +818,64 @@
// Force the post type to tasty_recipe since it's not public.
$query->set( 'post_type', 'tasty_recipe' );
- // Only include recipes that have a parent post.
- $existing_meta_query = $query->get( 'meta_query' );
- if ( ! is_array( $existing_meta_query ) ) {
- $existing_meta_query = array();
+ // Only include recipes that have a published parent post. See filter_taxonomy_archive_where().
+ $query->set( 'tasty_recipes_published_parent', true );
+ }
+
+ /**
+ * Limit the recipe taxonomy archive query to recipes embedded in at least one
+ * published post that is not password protected.
+ *
+ * @since 1.2.12
+ *
+ * @param string $where The WHERE clause of the query.
+ * @param WP_Query $query The query object.
+ *
+ * @return string
+ */
+ public static function filter_taxonomy_archive_where( $where, $query ) {
+ if ( ! $query->get( 'tasty_recipes_published_parent' ) ) {
+ return $where;
}
- $existing_meta_query[] = array(
- 'key' => Meta_Keys::RECIPE_PARENTS,
- 'compare' => 'EXISTS',
+ global $wpdb;
+
+ $where .= $wpdb->prepare(
+ " AND EXISTS (
+ SELECT 1 FROM {$wpdb->postmeta} AS tr_parents
+ INNER JOIN {$wpdb->posts} AS tr_parent_posts ON tr_parent_posts.ID = CAST( tr_parents.meta_value AS UNSIGNED )
+ WHERE tr_parents.post_id = {$wpdb->posts}.ID
+ AND tr_parents.meta_key = %s
+ AND tr_parent_posts.post_status = %s
+ AND tr_parent_posts.post_password = ''
+ )",
+ Meta_Keys::RECIPE_PARENTS,
+ 'publish'
);
- $query->set( 'meta_query', $existing_meta_query );
+ return $where;
+ }
+
+ /**
+ * Get the first published, non password protected post that embeds a recipe.
+ *
+ * @since 1.2.12
+ *
+ * @param int $recipe_id The recipe post ID.
+ *
+ * @return WP_Post|null The parent post, or null when the recipe has no public parent.
+ */
+ private static function get_published_parent( $recipe_id ) {
+ $parent_ids = get_post_meta( $recipe_id, Meta_Keys::RECIPE_PARENTS, false );
+
+ foreach ( (array) $parent_ids as $parent_id ) {
+ $parent_post = get_post( (int) $parent_id );
+ if ( $parent_post && 'publish' === $parent_post->post_status && '' === $parent_post->post_password ) {
+ return $parent_post;
+ }
+ }
+
+ return null;
}
/**
@@ -855,15 +907,12 @@
* @return array|false Array with display data or false if no parent exists.
*/
public static function get_archive_item_data( $recipe_id ) {
- $parent_post_id = get_post_meta( $recipe_id, Meta_Keys::RECIPE_PARENTS, true );
- if ( empty( $parent_post_id ) ) {
+ $parent_post = self::get_published_parent( $recipe_id );
+ if ( ! $parent_post ) {
return false;
}
- $parent_post = get_post( $parent_post_id );
- if ( ! $parent_post || 'publish' !== $parent_post->post_status ) {
- return false;
- }
+ $parent_post_id = $parent_post->ID;
$recipe = ObjectsRecipe::get_by_id( $recipe_id );
if ( ! $recipe ) {
@@ -901,17 +950,12 @@
return $permalink;
}
- $parent_post_id = get_post_meta( $post->ID, Meta_Keys::RECIPE_PARENTS, true );
- if ( empty( $parent_post_id ) ) {
- return $permalink;
- }
-
- $parent_post = get_post( $parent_post_id );
- if ( ! $parent_post || 'publish' !== $parent_post->post_status ) {
+ $parent_post = self::get_published_parent( $post->ID );
+ if ( ! $parent_post ) {
return $permalink;
}
- return get_permalink( $parent_post_id );
+ return get_permalink( $parent_post );
}
/**
@@ -929,6 +973,11 @@
return $excerpt;
}
+ // Never show the description of a recipe that is not in a public post.
+ if ( ! self::get_published_parent( get_the_ID() ) ) {
+ return '';
+ }
+
$recipe = ObjectsRecipe::get_by_id( get_the_ID() );
if ( ! $recipe ) {
return $excerpt;
--- a/tasty-recipes-lite/inc/class-distribution-metadata.php
+++ b/tasty-recipes-lite/inc/class-distribution-metadata.php
@@ -92,6 +92,10 @@
* @return void
*/
public static function action_wp_head_google_schema() {
+ // Don't expose recipes of password-protected posts before the password is entered.
+ if ( post_password_required( get_queried_object() ) ) {
+ return;
+ }
$recipes = Tasty_Recipes::get_recipes_for_post(
0,
@@ -419,14 +423,10 @@
$value = $recipe_json[ $name ];
if ( false !== stripos( $name, '_time' ) ) {
- if ( 'prep_time' === $name
- && ! empty( $recipe_json['additional_time_value'] ) ) {
- $time = time();
- $value = self::strtotime( $value, $time ) - $time
- + self::strtotime( $recipe_json['additional_time_value'], $time ) - $time;
- $value = "{$value} seconds";
+ $value = self::duration_schema_value( $name, $value, $recipe_json );
+ if ( '' === $value ) {
+ continue;
}
- $value = self::get_duration_for_time( $value );
}
if ( $value && in_array( $name, array( 'cuisine', 'category', 'method' ), true ) ) {
$bits = explode( ',', $value );
@@ -1352,14 +1352,23 @@
$step_set_regex = '#^(s*)(<[a-z]+)([^>]*>)#';
$list_items = self::get_top_level_list_items( $component );
if ( $list_items ) {
- // Replace from the end so earlier byte offsets stay valid.
- foreach ( array_reverse( $list_items, true ) as $index => $list_item ) {
- $open_tag = preg_replace(
+ $open_tags = array();
+ foreach ( $list_items as $index => $list_item ) {
+ // Photos and videos without text belong to the step above, as in the schema.
+ if ( ! self::is_step_list_item( $list_item['inner'] ) ) {
+ $open_tags[ $index ] = self::add_class_to_tag( $list_item['tag'], 'tasty-recipes-instruction-media' );
+ continue;
+ }
+ ++$step;
+ $open_tags[ $index ] = preg_replace(
$step_set_regex,
- '$1$2 id="instruction-step-' . ( $index + 1 ) . '"$3',
+ '$1$2 id="instruction-step-' . $step . '"$3',
$list_item['tag']
);
- $component = substr_replace( $component, $open_tag, $list_item['offset'], strlen( $list_item['tag'] ) );
+ }
+ // Replace from the end so earlier byte offsets stay valid.
+ foreach ( array_reverse( $list_items, true ) as $index => $list_item ) {
+ $component = substr_replace( $component, $open_tags[ $index ], $list_item['offset'], strlen( $list_item['tag'] ) );
}
} elseif ( preg_match_all( self::UNSTRUCTURED_LIST_MATCH_REGEX, $component, $matches, PREG_OFFSET_CAPTURE ) > 1 ) {
// Intro text before the first number, so set that as the first step.
@@ -1407,6 +1416,82 @@
}
/**
+ * Whether a top-level instruction list item is a step of its own.
+ *
+ * @since 1.2.12
+ *
+ * @param string $html Inner HTML of the list item.
+ *
+ * @return bool
+ */
+ private static function is_step_list_item( $html ) {
+ $text = self::get_step_text( $html );
+ if ( '' === $text ) {
+ return false;
+ }
+
+ // The card embeds a bare URL before numbering, the schema doesn't. Linked URLs stay links.
+ if ( preg_match( '#^s*(?:<p(?: [^>]*)?>s*)?(https?://[^s<>"]+)s*(?:</p>s*)?$#i', $html, $matches ) ) {
+ return ! self::get_video_url_from_content( $html )
+ && ! _wp_oembed_get_object()->get_provider( $matches[1], array( 'discover' => false ) );
+ }
+
+ return true;
+ }
+
+ /**
+ * Gets the text of an instruction, ignoring captions and shortcode tags.
+ *
+ * @since 1.2.12
+ *
+ * @param string $html Instruction HTML.
+ *
+ * @return string
+ */
+ private static function get_step_text( $html ) {
+ $html = (string) preg_replace( '#<figcaptionb[^>]*>.*?</figcaption>|[captionb[^]]*].*?[/caption]#is', '', $html );
+ // Keep the text inside other enclosing shortcodes, since it shows once they run.
+ $html = (string) preg_replace_callback(
+ '/' . get_shortcode_regex() . '/',
+ function ( $matches ) {
+ return '[' === $matches[1] ? $matches[0] : $matches[5];
+ },
+ $html
+ );
+ $text = trim( wp_strip_all_tags( $html ) );
+ return self::is_empty( $text ) ? '' : $text;
+ }
+
+ /**
+ * Adds a class to an opening HTML tag.
+ *
+ * @since 1.2.12
+ *
+ * @param string $tag Opening tag.
+ * @param string $class_name Class to add.
+ *
+ * @return string
+ */
+ private static function add_class_to_tag( $tag, $class_name ) {
+ $count = 0;
+ $updated = preg_replace_callback(
+ '#sclasss*=s*(?:"([^"]*)"|'([^']*)'|([^s>]+))#i',
+ function ( $matches ) use ( $class_name ) {
+ $existing = trim( implode( '', array_slice( $matches, 1 ) ) );
+ return ' class="' . trim( $class_name . ' ' . $existing ) . '"';
+ },
+ $tag,
+ 1,
+ $count
+ );
+ if ( $count ) {
+ return (string) $updated;
+ }
+
+ return (string) preg_replace( '#^(s*<[a-z]+)#i', '$1 class="' . $class_name . '"', $tag, 1 );
+ }
+
+ /**
* Finds top-level list items, keeping nested lists inside their parent item.
*
* @since 1.2.11
@@ -1666,7 +1751,11 @@
$processed_component['name'] = $component_item['name'];
}
}
- $component_value = trim( wp_strip_all_tags( strip_shortcodes( $component_item['value'] ) ) );
+ $component_value = '';
+ // Instruction items not numbered as steps belong to the step above.
+ if ( ! preg_match( '#^s*<lib[^>]*bclass="[^"]*btasty-recipes-instruction-mediab#', $component_item['value'] ) ) {
+ $component_value = trim( wp_strip_all_tags( strip_shortcodes( $component_item['value'] ) ) );
+ }
// Don't add yet if it's an image or a video.
if ( empty( $component_value ) ) {
if ( ! isset( $processed_component['video_url'] )
@@ -1786,6 +1875,302 @@
/**
+ * ISO duration for a recipe time field.
+ *
+ * @since 1.2.12
+ *
+ * @param string $name Cooking attribute name.
+ * @param string $value Stored time value.
+ * @param array $recipe_json Recipe JSON data.
+ *
+ * @return string
+ */
+ private static function duration_schema_value( $name, $value, $recipe_json ) {
+ if ( 'prep_time' === $name && ! empty( $recipe_json['additional_time_value'] ) ) {
+ $value = self::sum_prep_and_additional_time( $value, $recipe_json['additional_time_value'] );
+ if ( '' === $value ) {
+ return '';
+ }
+ }
+ return self::get_duration_for_time( $value );
+ }
+
+ /**
+ * Sum prep and additional time as a seconds string.
+ *
+ * @since 1.2.12
+ *
+ * @param string $prep Prep time.
+ * @param string $additional Additional time.
+ *
+ * @return string Empty when the sum is not a positive duration.
+ */
+ private static function sum_prep_and_additional_time( $prep, $additional ) {
+ $sum = 0;
+ foreach ( array( $prep, $additional ) as $part ) {
+ $seconds = self::get_seconds_for_time( $part );
+ if ( null === $seconds ) {
+ continue;
+ }
+ $sum += $seconds;
+ }
+ if ( $sum <= 0 ) {
+ return '';
+ }
+ return $sum . ' seconds';
+ }
+
+ /**
+ * Convert a human-readable time into a duration in seconds.
+ *
+ * @since 1.2.12
+ *
+ * @param mixed $time Time to convert.
+ *
+ * @return int|null Seconds, or null when the time cannot be parsed.
+ */
+ public static function get_seconds_for_time( $time ) {
+ if ( ! is_string( $time ) && ! is_numeric( $time ) ) {
+ return null;
+ }
+
+ $time = trim( (string) $time );
+ if ( '' === $time ) {
+ return null;
+ }
+ if ( is_numeric( $time ) ) {
+ return (int) $time * MINUTE_IN_SECONDS;
+ }
+
+ $time = self::normalize_duration_time_string( $time );
+ if ( '' === $time ) {
+ return null;
+ }
+
+ $seconds = self::sum_duration_unit_pairs( $time );
+ if ( null !== $seconds ) {
+ return $seconds;
+ }
+ return self::get_seconds_from_strtotime( $time );
+ }
+
+ /**
+ * Strip notes and expand clock times before duration parsing.
+ *
+ * @since 1.2.12
+ *
+ * @param string $time Time string.
+ *
+ * @return string
+ */
+ private static function normalize_duration_time_string( $time ) {
+ $time = preg_replace( '/([^)]+)/', '', $time );
+ $time = preg_replace( '/(d+):(d+)/', '$1 hours $2 minutes', $time );
+ if ( ! is_string( $time ) ) {
+ return '';
+ }
+ return trim( $time );
+ }
+
+ /**
+ * Sum integer quantities that are each followed by a known unit.
+ *
+ * @since 1.2.12
+ *
+ * @param string $time Normalized time string.
+ *
+ * @return int|null Null when a quantity has no known unit, or when no quantity exists.
+ */
+ private static function sum_duration_unit_pairs( $time ) {
+ if ( ! preg_match_all( '/(?<![d.,])(d+)(?![d.,])/', $time, $matches, PREG_OFFSET_CAPTURE ) ) {
+ return null;
+ }
+
+ $units = self::get_duration_unit_seconds();
+ $total = 0;
+ foreach ( $matches[1] as $number_match ) {
+ $unit = self::duration_unit_after_number( $time, $number_match );
+ if ( null === $unit || ! isset( $units[ $unit ] ) ) {
+ return null;
+ }
+ $total += (int) $number_match[0] * $units[ $unit ];
+ }
+ return $total;
+ }
+
+ /**
+ * Unit word that follows a captured number.
+ *
+ * @since 1.2.12
+ *
+ * @param string $time Time string.
+ * @param array $number_match Number match from preg_match_all() with PREG_OFFSET_CAPTURE.
+ *
+ * @return string|null
+ */
+ private static function duration_unit_after_number( $time, $number_match ) {
+ $after = substr( $time, $number_match[1] + strlen( $number_match[0] ) );
+ if ( ! preg_match( '/^[s,]*([^sd,]+)/', $after, $unit_match ) ) {
+ return null;
+ }
+ $unit = self::normalize_duration_unit( $unit_match[1] );
+ if ( '' === $unit ) {
+ return null;
+ }
+ return $unit;
+ }
+
+ /**
+ * Normalize a unit word for lookup.
+ *
+ * @since 1.2.12
+ *
+ * @param string $unit Raw unit word.
+ *
+ * @return string
+ */
+ private static function normalize_duration_unit( $unit ) {
+ $unit = strtolower( remove_accents( $unit ) );
+ return trim( $unit, '.,:;!?' );
+ }
+
+ /**
+ * Map of duration unit words to seconds.
+ *
+ * @since 1.2.12
+ *
+ * @return array
+ */
+ private static function get_duration_unit_seconds() {
+ $units = array();
+ self::add_duration_units(
+ $units,
+ 1,
+ array( 'sec', 'secs', 'second', 'seconds', 'segundo', 'segundos', 'secondo', 'secondi', 'seconde', 'secondes', 'sekunde', 'sekunden' )
+ );
+ self::add_duration_units(
+ $units,
+ MINUTE_IN_SECONDS,
+ array( 'min', 'mins', 'minute', 'minutes', 'minuto', 'minutos', 'minuten', 'minuti' )
+ );
+ self::add_duration_units(
+ $units,
+ HOUR_IN_SECONDS,
+ array( 'hour', 'hours', 'hora', 'horas', 'heure', 'heures', 'stunde', 'stunden', 'ora', 'ore' )
+ );
+ self::add_duration_units(
+ $units,
+ DAY_IN_SECONDS,
+ array( 'day', 'days', 'dia', 'dias', 'jour', 'jours', 'tag', 'tage', 'giorno', 'giorni' )
+ );
+ self::add_duration_units(
+ $units,
+ WEEK_IN_SECONDS,
+ array( 'week', 'weeks', 'semana', 'semanas', 'semaine', 'semaines', 'woche', 'wochen', 'settimana', 'settimane' )
+ );
+ return self::add_translated_duration_units( $units );
+ }
+
+ /**
+ * Add unit names that share one duration length.
+ *
+ * @since 1.2.12
+ *
+ * @param array $units Unit map, updated in place.
+ * @param float|int $seconds Duration length.
+ * @param array $names Unit words.
+ *
+ * @return void
+ */
+ private static function add_duration_units( &$units, $seconds, $names ) {
+ $seconds = (int) $seconds;
+ foreach ( $names as $name ) {
+ $units[ $name ] = $seconds;
+ }
+ }
+
+ /**
+ * Add unit words from the current locale's simple time formats.
+ *
+ * @since 1.2.12
+ *
+ * @param array $units Unit map.
+ *
+ * @return array
+ */
+ private static function add_translated_duration_units( $units ) {
+ $strings = self::time_strings();
+ foreach ( self::simple_duration_string_keys() as $key => $seconds ) {
+ $format = isset( $strings[ $key ] ) ? $strings[ $key ] : '';
+ $word = self::duration_word_from_format( $format );
+ if ( '' === $word ) {
+ continue;
+ }
+ $units[ $word ] = $seconds;
+ }
+ return $units;
+ }
+
+ /**
+ * Simple time-format keys that name a single unit.
+ *
+ * @since 1.2.12
+ *
+ * @return array
+ */
+ private static function simple_duration_string_keys() {
+ return array(
+ 'individual_hour' => HOUR_IN_SECONDS,
+ 'multiple_hours' => HOUR_IN_SECONDS,
+ 'individual_minute' => MINUTE_IN_SECONDS,
+ 'multiple_minutes' => MINUTE_IN_SECONDS,
+ );
+ }
+
+ /**
+ * Unit word from a `%s unit` format string.
+ *
+ * @since 1.2.12
+ *
+ * @param mixed $format Format string.
+ *
+ * @return string
+ */
+ private static function duration_word_from_format( $format ) {
+ if ( ! is_string( $format ) || '' === $format ) {
+ return '';
+ }
+ $word = trim( str_replace( '%s', '', $format ) );
+ $word = self::normalize_duration_unit( $word );
+ if ( '' === $word || false !== strpos( $word, ' ' ) ) {
+ return '';
+ }
+ return $word;
+ }
+
+ /**
+ * Duration in seconds from strtotime(), or null when that result is unusable.
+ *
+ * @since 1.2.12
+ *
+ * @param string $time Normalized time string.
+ *
+ * @return int|null
+ */
+ private static function get_seconds_from_strtotime( $time ) {
+ $now = time();
+ $parsed = self::strtotime( $time, $now );
+ if ( false === $parsed ) {
+ return null;
+ }
+ $seconds = $parsed - $now;
+ if ( $seconds <= 0 ) {
+ return null;
+ }
+ return $seconds;
+ }
+
+ /**
* Get an ISO 8601 duration based on a human-readable time.
*
* @param string $time Time to get the duration of.
@@ -1793,14 +2178,11 @@
* @return string
*/
public static function get_duration_for_time( $time ) {
- // Assume a number is minutes.
- if ( is_numeric( $time ) ) {
- $time = "{$time} minutes";
+ $seconds = self::get_seconds_for_time( $time );
+ if ( null === $seconds || $seconds <= 0 ) {
+ return '';
}
- $now = time();
- $time = self::strtotime( $time, $now );
- $time = $time - $now;
- $calculated_times = self::convert_seconds_to_minutes_hours( $time );
+ $calculated_times = self::convert_seconds_to_minutes_hours( $seconds );
$duration = 'PT';
if ( ! empty( $calculated_times['hours'] ) ) {
$duration .= $calculated_times['hours'] . 'H';
--- a/tasty-recipes-lite/inc/class-editor.php
+++ b/tasty-recipes-lite/inc/class-editor.php
@@ -51,7 +51,7 @@
public static function action_admin_notices() {
$screen = get_current_screen();
$post_id = Utils::get_param( 'post', 'intval', 0 );
- if ( 'post' !== $screen->base || empty( $post_id ) ) {
+ if ( 'post' !== $screen->base || empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
@@ -872,6 +872,11 @@
return array();
}
+ // Only use the query arg for a post the current user can edit. The post type check avoids a notice when it isn't registered yet.
+ if ( ! $post && ( ! post_type_exists( (string) get_post_type( $post_id ) ) || ! current_user_can( 'edit_post', $post_id ) ) ) {
+ return array();
+ }
+
$all_converters = Tasty_Recipes::get_converters();
$output = array();
--- a/tasty-recipes-lite/inc/class-meta-keys.php
+++ b/tasty-recipes-lite/inc/class-meta-keys.php
@@ -44,6 +44,17 @@
public const NUTRIFOX_ERROR = 'nutrifox_error';
/**
+ * Nutrifox sync state for recipes built in Nutrifox from the recipe.
+ *
+ * Stores array( 'hash' => string ); its presence means the label syncs on save.
+ *
+ * @since 1.2.12
+ *
+ * @var string
+ */
+ public const NUTRIFOX_SYNC = '_tasty_recipes_nutrifox_sync';
+
+ /**
* Cached video URL provider response meta.
*
* @since 1.2.7
@@ -134,6 +145,15 @@
public const RATING_COMMENT_HASH = 'tasty-recipes-comment-hash';
/**
+ * Comment meta on a held rated comment with the ID of the earlier comment it replaces once approved.
+ *
+ * @since 1.2.12
+ *
+ * @var string
+ */
+ public const RATING_REPLACES_COMMENT = 'tasty-recipes-replaces-comment';
+
+ /**
* Imported Create ratings post meta.
*
* @since 1.2.7
--- a/tasty-recipes-lite/inc/class-options.php
+++ b/tasty-recipes-lite/inc/class-options.php
@@ -288,6 +288,15 @@
public const DEFAULT_TERMS_INSERTED = 'tasty_recipes_default_terms_inserted_v2';
/**
+ * Nutrifox OAuth connection option.
+ *
+ * @since 1.2.12
+ *
+ * @var string
+ */
+ public const NUTRIFOX_CONNECTION = 'tasty_recipes_nutrifox_connection';
+
+ /**
* Option names keyed for settings JavaScript consumption.
*
* @since 1.2.7
--- a/tasty-recipes-lite/inc/class-ratings.php
+++ b/tasty-recipes-lite/inc/class-ratings.php
@@ -70,6 +70,33 @@
private static $svg_loaded = false;
/**
+ * Cookie that holds the random token identifying a logged-out rater.
+ *
+ * @since 1.2.12
+ *
+ * @var string
+ */
+ const RATER_COOKIE = 'tasty_recipes_rater';
+
+ /**
+ * Rater token created during this request, before the browser sends it back.
+ *
+ * @since 1.2.12
+ *
+ * @var string
+ */
+ private static $rater_token = '';
+
+ /**
+ * Comment ID to remove once the new comment from the same rater is saved.
+ *
+ * @since 1.2.12
+ *
+ * @var int
+ */
+ private static $replaced_comment_id = 0;
+
+ /**
* Whether or not ratings are enabled. Optionally pass a comment status to
* check if comments are open for the current post.
*
@@ -168,7 +195,8 @@
// Some comments may have ER, Cookbook, and Simple Recipe Pro.
$comment_ratings = array();
foreach ( $ratings as $rating ) {
- if ( $rating->meta_value >= 1 ) {
+ // Ignore values outside the 1-5 star range (imported ratings may have decimals).
+ if ( is_numeric( $rating->meta_value ) && $rating->meta_value >= 1 && $rating->meta_value <= 5 ) {
$comment_ratings[ $rating->comment_id ] = $rating->meta_value;
}
}
@@ -219,27 +247,145 @@
* @return array
*/
public static function filter_preprocess_comment( $commentdata ) {
- // We need to remove the old quick rating that was added before from this user if found.
- list( $comment_hash, $comment_id ) = self::get_previous_comment( $commentdata['comment_post_ID'] );
- if ( $comment_id ) {
- wp_delete_comment( $comment_id );
- }
-
$rating = Utils::post_param( 'tasty-recipes-rating' );
- if ( empty( $rating ) ) {
+ if ( ! self::is_valid_rating( $rating ) ) {
return $commentdata;
}
+ $post_id = (int) $commentdata['comment_post_ID'];
+
+ // Replace the earlier rating from this rater, but only after the new comment is saved.
+ list( $comment_hash, $comment_id ) = self::get_previous_comment( $post_id );
+ if ( $comment_id ) {
+ self::$replaced_comment_id = $comment_id;
+ add_action( 'comment_post', array( __CLASS__, 'delete_replaced_comment' ), 10, 2 );
+ }
+
if ( ! isset( $commentdata['comment_meta'] ) ) {
$commentdata['comment_meta'] = array();
}
$commentdata['comment_meta'][ self::COMMENT_META_KEY ] = (int) $rating;
- $commentdata['comment_meta'][ self::RATING_COMMENT_HASH_META_KEY ] = $comment_hash;
+ $commentdata['comment_meta'][ self::RATING_COMMENT_HASH_META_KEY ] = self::get_hash_for_saving( $comment_hash, $post_id );
return $commentdata;
}
/**
+ * Remove the rater's earlier comment once their new rated comment is saved.
+ *
+ * @since 1.2.12
+ *
+ * @param int $comment_id ID of the new comment.
+ * @param int|string $comment_approved Approval status of the new comment.
+ *
+ * @return void
+ */
+ public static function delete_replaced_comment( $comment_id, $comment_approved ) {
+ remove_action( 'comment_post', array( __CLASS__, 'delete_replaced_comment' ), 10 );
+
+ $replaced = self::$replaced_comment_id;
+ self::$replaced_comment_id = 0;
+
+ if ( ! $replaced || (int) $comment_id === $replaced || in_array( (string) $comment_approved, array( 'spam', 'trash' ), true ) ) {
+ return;
+ }
+
+ if ( '1' !== (string) $comment_approved ) {
+ // Keep the earlier rating counted until the new comment is approved.
+ update_comment_meta( $comment_id, Meta_Keys::RATING_REPLACES_COMMENT, $replaced );
+ return;
+ }
+
+ self::replace_comment( $replaced, (int) $comment_id );
+ }
+
+ /**
+ * Remove the earlier comment a held rated comment replaces once it is approved.
+ *
+ * @since 1.2.12
+ *
+ * @param string $new_status New comment status.
+ * @param string $old_status Old comment status.
+ * @param object $comment Comment object.
+ *
+ * @return void
+ */
+ public static function action_transition_comment_status( $new_status, $old_status, $comment ) {
+ if ( 'approved' !== $new_status || ! $comment ) {
+ return;
+ }
+
+ $replaced = (int) get_comment_meta( $comment->comment_ID, Meta_Keys::RATING_REPLACES_COMMENT, true );
+ if ( $replaced ) {
+ self::replace_comment( $replaced, (int) $comment->comment_ID );
+ }
+ }
+
+ /**
+ * Delete an earlier comment from the same rater, and any it was waiting to replace.
+ *
+ * @since 1.2.12
+ *
+ * @param int $replaced ID of the earlier comment.
+ * @param int $comment_id ID of the approved comment that replaces it.
+ *
+ * @return void
+ */
+ private static function replace_comment( $replaced, $comment_id ) {
+ delete_comment_meta( $comment_id, Meta_Keys::RATING_REPLACES_COMMENT );
+
+ $hash = (string) get_comment_meta( $comment_id, self::RATING_COMMENT_HASH_META_KEY, true );
+
+ // Also remove any earlier comment a held one was waiting to replace.
+ $depth = 0;
+ while ( $depth < 5 && $replaced !== $comment_id && self::is_same_rater_comment( $replaced, $hash ) ) {
+ $next = (int) get_comment_meta( $replaced, Meta_Keys::RATING_REPLACES_COMMENT, true );
+ wp_delete_comment( $replaced );
+ $replaced = $next;
+ ++$depth;
+ }
+
+ self::action_modify_comment_update_recipe_ratings( $comment_id );
+ }
+
+ /**
+ * Whether a comment is a counted rating left by the rater with this hash.
+ *
+ * @since 1.2.12
+ *
+ * @param int $comment_id Comment ID.
+ * @param string $hash Rater hash.
+ *
+ * @return bool
+ */
+ private static function is_same_rater_comment( $comment_id, $hash ) {
+ $comment = $comment_id ? get_comment( $comment_id ) : null;
+ if ( ! $comment || ! $hash || in_array( $comment->comment_approved, array( 'spam', 'trash' ), true ) ) {
+ return false;
+ }
+
+ return hash_equals( (string) get_comment_meta( $comment_id, self::RATING_COMMENT_HASH_META_KEY, true ), $hash );
+ }
+
+ /**
+ * Whether a value is a whole star rating from 1 to 5.
+ *
+ * @since 1.2.12
+ *
+ * @param mixed $rating Rating value.
+ *
+ * @return bool
+ */
+ private static function is_valid_rating( $rating ) {
+ if ( ! is_numeric( $rating ) || (float) $rating !== (float) (int) $rating ) {
+ return false;
+ }
+
+ $rating = (int) $rating;
+ return $rating >= 1 && $rating <= 5;
+ }
+
+ /**
* Handles a REST API request to create a new comment.
*
* @param object $comment New comment object.
@@ -248,7 +394,7 @@
* @return void
*/
public static function action_rest_insert_comment( $comment, $request ) {
- if ( ! empty( $request['meta']['tasty-recipes-rating'] ) ) {
+ if ( isset( $request['meta']['tasty-recipes-rating'] ) && self::is_valid_rating( $request['meta']['tasty-recipes-rating'] ) ) {
update_comment_meta(
$comment->comment_ID,
self::COMMENT_META_KEY,
@@ -489,6 +635,94 @@
}
/**
+ * Star color for the comment form when the card color would disappear on white.
+ *
+ * Near-white card stars stay on the card. The comment form uses gold instead.
+ *
+ * @since 1.2.12
+ *
+ * @param string $star_color Card star color.
+ *
+ * @return string Gold hex when the color is too light, otherwise an empty string.
+ */
+ public static function get_comment_form_star_color( $star_color ) {
+ $rgb = self::hex_color_to_rgb( $star_color );
+ if ( null === $rgb ) {
+ return '';
+ }
+
+ if ( self::relative_luminance( $rgb ) < 0.85 ) {
+ return '';
+ }
+
+ return '#F2B955';
+ }
+
+ /**
+ * Convert a hex color to RGB channels.
+ *
+ * @since 1.2.12
+ *
+ * @param string $color Hex color (#RGB, #RRGGBB, or #RRGGBBAA).
+ *
+ * @return int[]|null Red, green, and blue from 0 to 255, or null when the color cannot be parsed.
+ */
+ private static function hex_color_to_rgb( $color ) {
+ $color = trim( $color );
+ if ( ! preg_match( '/^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $color, $matches ) ) {
+ return null;
+ }
+
+ $hex = strtolower( $matches[1] );
+ if ( 3 === strlen( $hex ) ) {
+ $hex = substr( $hex, 0, 1 ) . substr( $hex, 0, 1 )
+ . substr( $hex, 1, 1 ) . substr( $hex, 1, 1 )
+ . substr( $hex, 2, 1 ) . substr( $hex, 2, 1 );
+ }
+
+ return array(
+ hexdec( substr( $hex, 0, 2 ) ),
+ hexdec( substr( $hex, 2, 2 ) ),
+ hexdec( substr( $hex, 4, 2 ) ),
+ );
+ }
+
+ /**
+ * Relative luminance for an sRGB color.
+ *
+ * @since 1.2.12
+ *
+ * @param int[] $rgb Red, green, and blue channels from 0 to 255.
+ *
+ * @return float
+ */
+ private static function relative_luminance( $rgb ) {
+ $red = self::linear_channel( $rgb[0] );
+ $green = self::linear_channel( $rgb[1] );
+ $blue = self::linear_channel( $rgb[2] );
+
+ return ( 0.2126 * $red ) + ( 0.7152 * $green ) + ( 0.0722 * $blue );
+ }
+
+ /**
+ * Convert one sRGB channel to linear light.
+ *
+ * @since 1.2.12
+ *
+ * @param int $channel Channel from 0 to 255.
+ *
+ * @return float
+ */
+ private static function linear_channel( $channel ) {
+ $value = ( (int) $channel ) / 255;
+ if ( 0.04045 >= $value ) {
+ return $value / 12.92;
+ }
+
+ return ( ( $value + 0.055 ) / 1.055 ) ** 2.4;
+ }
+
+ /**
* Get rating stars HTML.
*
* @param bool $no_ratings Show no rating template without review yet.
@@ -577,8 +811,8 @@
}
$minimum_rating_without_comment = self::get_minimum_rating_without_comment( $comment_data );
- $post_rating = (int) Utils::get_post_value( 'tasty-recipes-rating' );
- return ! empty( $post_rating ) && $post_rating >= $minimum_rating_without_comment;
+ $post_rating = Utils::get_post_value( 'tasty-recipes-rating' );
+ return self::is_valid_rating( $post_rating ) && (int) $post_rating >= $minimum_rating_without_comment;
}
/**
@@ -628,7 +862,7 @@
$recipe_id = (int) Vars::post_param( 'recipe_id', 'intval' );
$recipe = ! empty( $recipe_id ) ? Recipe::get_by_id( $recipe_id ) : null;
- if ( ! $rating || ! $recipe || ! self::can_rate_recipe( $post_id, $recipe_id ) ) {
+ if ( ! self::is_valid_rating( $rating ) || ! $recipe || ! self::can_rate_recipe( $post_id, $recipe_id ) ) {
Utils::send_json_error( __( 'Invalid request', 'tasty-recipes-lite' ) );
}
@@ -650,7 +884,8 @@
}
if ( $creating ) {
- $comment_id = self::create_rating( $comment_hash );
+ $comment_hash = self::get_hash_for_saving( $comment_hash, $post_id );
+ $comment_id = self::create_rating( $comment_hash );
} else {
// Update the rating on existing comment.
update_comment_meta( $comment_id, self::COMMENT_META_KEY, $rating );
@@ -802,6 +1037,60 @@
}
/**
+ * Get the hash to store with a new rating, creating the rater token if needed.
+ *
+ * @since 1.2.12
+ *
+ * @param string $comment_hash Hash for the current rater, empty when there is none yet.
+ * @param int $post_id Post ID.
+ *
+ * @return string
+ */
+ private static function get_hash_for_saving( $comment_hash, $post_id ) {
+ if ( $comment_hash ) {
+ return $comment_hash;
+ }
+
+ self::set_rater_token();
+ return self::generate_comment_hash( $post_id );
+ }
+
+ /**
+ * Get the logged-out rater token from this request or the browser cookie.
+ *
+ * @since 1.2.12
+ *
+ * @return string Empty when the browser has no valid token.
+ */
+ private static function get_rater_token() {
+ if ( self::$rater_token ) {
+ return self::$rater_token;
+ }
+
+ // Only read while handling a rating AJAX or comment POST request, never on cached page views.
+ // phpcs:ignore WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___COOKIE
+ $token = isset( $_COOKIE[ self::RATER_COOKIE ] ) ? sanitize_text_field( wp_unslash( $_COOKIE[ self::RATER_COOKIE ] ) ) : '';
+ return preg_match( '/^[A-Za-z0-9]{32}$/', $token ) ? $token : '';
+ }
+
+ /**
+ * Create a random rater token and send it to the browser in an HttpOnly cookie.
+ *
+ * @since 1.2.12
+ *
+ * @return void
+ */
+ private static function set_rater_token() {
+ self::$rater_token = wp_generate_password( 32, false );
+
+ if ( ! headers_sent() ) {
+ // Only sent on the rating AJAX and comment POST responses, never on cached page views.
+ // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.cookies_setcookie
+ setcookie( self::RATER_COOKIE, self::$rater_token, time() + YEAR_IN_SECONDS, COOKIEPATH, (string) COOKIE_DOMAIN, is_ssl(), true );
+ }
+ }
+
+ /**
* Generate comment hash for specific post and the current user.
*
* @since 3.12
@@ -811,16 +1100,19 @@
* @return string
*/
private static function generate_comment_hash( $post_id ) {
+ if ( ! is_user_logged_in() ) {
+ // Logged-out raters are identified by a random token only their browser has.
+ $token = self::get_rater_token();
+ return $token ? hash_hmac( 'sha256', $post_id . '::' . $token, wp_salt( 'auth' ) ) : '';
+ }
+
$params = array(
$post_id,
Vars::server_param( 'REMOTE_ADDR' ),
Vars::server_param( 'HTTP_USER_AGENT' ),
+ get_current_user_id(),
);
- if ( is_user_logged_in() ) {
- $params[] = get_current_user_id();
- }
-
if ( defined( 'AUTH_SALT' ) && ! empty( AUTH_SALT ) ) {
$params[] = AUTH_SALT;
}
@@ -839,6 +1131,11 @@
* @return int
*/
private static function get_comment_id_by_hash( $hash, $post_id = 0 ) {
+ // An empty meta_value is ignored by the meta query and would match any rated comment.
+ if ( empty( $hash ) || ! is_string( $hash ) ) {
+ return 0;
+ }
+
$args = array(
'fields' => 'ids',
'meta_key' => self::RATING_COMMENT_HASH_META_KEY, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
@@ -852,8 +1149,12 @@
$comments_query = new WP_Comment_Query( $args );
$comments = $comments_query->get_comments();
- // Return the first comment ID.
- return $comments ? $comments[0] : 0;
+ if ( ! $comments ) {
+ return 0;
+ }
+
+ $stored_hash = (string) get_comment_meta( $comments[0], self::RATING_COMMENT_HASH_META_KEY, true );
+ return hash_equals( $stored_hash, $hash ) ? (int) $comments[0] : 0;
}
/**
@@ -972,11 +1273,12 @@
return $comment;
}
- $comment['content'] = $has_comment->comment_content;
- if ( ! is_user_logged_in() ) {
- $comment['email'] = $has_comment->comment_author_email;
- $comment['name'] = $has_comment->comment_author;
+ // Only approved (already public) content is sent back. Never the author's email.
+ if ( '1' !== (string) $has_comment->comment_approved ) {
+ return $comment;
}
+
+ $comment['content'] = $has_comment->comment_content;
return $comment;
}
--- a/tasty-recipes-lite/inc/class-tasty-recipes.php
+++ b/tasty-recipes-lite/inc/class-tasty-recipes.php
@@ -6,6 +6,8 @@
*/
use Tasty_RecipesBlock_Editor;
+use Tasty_RecipesIntegrationsNutrifoxConnection_Routes as Nutrifox_Connection_Routes;
+use Tasty_RecipesIntegrationsNutrifoxLabels as Nutrifox_Labels;
use Tasty_RecipesMetaBox;
use Tasty_RecipesOnboarding_Wizard;
use Tasty_RecipesOptions;
@@ -310,6 +312,8 @@
add_action( 'tasty_recipes_process_thumbnails_continue', array( 'Tasty_RecipesContent_Model', 'action_tasty_recipes_process_thumbnails' ) );
add_action( 'rest_api_init', array( 'Tasty_RecipesRecipe_Explorer', 'action_rest_api_init' ) );
add_action( 'rest_api_init', array( 'Tasty_RecipesOnboarding_Wizard', 'register_api_routes' ) );
+ Nutrifox_Connection_Routes::load_hooks();
+ Nutrifox_Labels::load_hooks();
// Frontend.
add_action( 'body_class', array( 'Tasty_RecipesContent_Model', 'filter_body_class' ) );
@@ -318,6 +322,7 @@
foreach ( array( 'wp_insert_comment', 'wp_update_comment', 'wp_set_comment_status' ) as $hook ) {
add_action( $hook, array( 'Tasty_RecipesRatings', 'action_modify_comment_update_recipe_ratings' ) );
}
+ add_action( 'transition_comment_status', array( 'Tasty_RecipesRatings', 'action_transition_comment_status' ), 10, 3 );
add_action( 'rest_insert_comment', array( 'Tasty_RecipesRatings', 'action_rest_insert_comment' ), 10, 2 );
add_action( 'admin_head', array( 'Tasty_RecipesRatings', 'action_admin_head' ) );
--- a/tasty-recipes-lite/inc/class-telemetry.php
+++ b/tasty-recipes-lite/inc/class-telemetry.php
@@ -153,7 +153,7 @@
* @return bool
*/
public static function should_load_posthog() {
- $should_load = self::has_usage_consent() && ! self::is_cypress_request();
+ $should_load = self::has_usage_consent() && ! self::is_excluded_environment();
/**
* Filters whether PostHog may load on recipe-related admin screens.
@@ -311,7 +311,18 @@
* @return bool
*/
public static function is_excluded_environment() {
- return self::is_cypress_request() || self::is_local_site();
+ $is_excluded = self::is_cypress_request() || self::is_local_site();
+
+ /**
+ * Filters whether the current environment is excluded from telemetry.
+ *
+ * Excluded environments never load PostHog or send usage snapshots.
+ *
+ * @since 1.2.12
+ *
+ * @param bool $is_excluded Whether telemetry is excluded.
+ */
+ return (bool) apply_filters( 'tasty_recipes_telemetry_excluded_environment', $is_excluded );
}
/**
--- a/tasty-recipes-lite/inc/converters/class-converter.php
+++ b/tasty-recipes-lite/inc/converters/class-converter.php
@@ -12,6 +12,7 @@
use Tasty_RecipesRatings;
use Tasty_RecipesShortcodes;
use Tasty_RecipesUtils;
+use WP_Post;
/**
* Base converter class.
@@ -257,14 +258,120 @@
}
/**
+ * Get the legacy recipe post referenced by the content being converted.
+ *
+ * Published recipes without a password can always be used. Anything else
+ * (private, draft, pending, password protected, etc.) is only used when
+ * both the current user and the author of the post being converted are
+ * allowed to access it.
+ *
+ * @since 1.2.12
+ *
+ * @param int|string $source_id ID of the legacy recipe post.
+ * @param string $post_type Post type the legacy recipe must have.
+ * @param int $post_id ID of the post being converted.
+ *
+ * @return WP_Post|null
+ */
+ protected static function get_source_post( $source_id, $post_type, $post_id ) {
+ $source_post = get_post( (int) $source_id );
+ if ( ! $source_post || $post_type !== $source_post->post_type ) {
+ return null;
+ }
+
+ if ( 'publish' === $source_post->post_status && '' === $source_post->post_password ) {
+ return $source_post;
+ }
+
+ $user_ids = array(
+ get_current_user_id(),
+ (int) get_post_field( 'post_author', $post_id ),
+ );
+ foreach ( $user_ids as $user_id ) {
+ if ( ! self::user_can_access_source_post( $user_id, $source_post ) ) {
+ return null;
+ }
+ }
+
+ return $source_post;
+ }
+
+ /**
+ * Check if a user owns a legacy recipe post or can edit other users' posts.
+ *
+ * Meta capabilities are not used because the legacy post type is usually
+ * not registered anymore.
+ *
+ * @since 1.2.12
+ *
+ * @param int $user_id User ID.
+ * @param WP_Post $source_post Legacy recipe post.
+ *
+ * @return bool
+ */
+ private static function user_can_access_source_post( $user_id, $source_post ) {
+ if ( ! $user_id ) {
+ return false;
+ }
+ return (int) $source_post->post_author === $user_id || user_can( $user_id, 'edit_others_posts' );
+ }
+
+ /**
+ * Check if converted data can be saved without sanitizing it.
+ *
+ * Only when both the current user and the author of the converted post
+ * are allowed to save unfiltered HTML.
+ *
+ * @since 1.2.12
+ *
+ * @param int $post_id ID of the post being converted.
+ *
+ * @return bool
+ */
+ private static function can_keep_unfiltered_html( $post_id ) {
+ if ( ! $post_id || ! current_user_can( 'unfiltered_html' ) ) {
+ return false;
+ }
+ $author_id = (int) get_post_field( 'post_author', $post_id );
+ return $author_id && user_can( $author_id, 'unfiltered_html' );
+ }
+
+ /**
+ * Sanitize converted data with the same callbacks as the recipe editor.
+ *
+ * @since 1.2.12
+ *
+ * @param array $converted_data Data to be saved.
+ *
+ * @return array
+ */
+ private static function sanitize_converted_data( $converted_data ) {
+ $attributes = Recipe::get_attributes();
+ foreach ( $converted_data as $key => $value ) {
+ if ( ! is_string( $value ) ) {
+ continue;
+ }
+ $sanitize_callback = ! empty( $attributes[ $key ]['sanitize_callback'] ) ? $attributes[ $key ]['sanitize_callback'] : 'sanitize_text_field';
+ if ( 'intval' === $sanitize_callback && '' === $value ) {
+ continue;
+ }
+ // wp_filter_post_kses() expects slashed data.
+ $converted_data[ $key ] = wp_unslash( call_user_func( $sanitize_callback, wp_slash( $value ) ) );
+ }
+ return $converted_data;
+ }
+
+ /**
* Apply a filter to converted data before saving to the database.
*
* @param array $converted_data Data to be converted.
* @param Recipe $recipe Created recipe object.
+ * @param int $post_id ID of the post being converted. Since x.x.
+ * Without it, the data is always sanitized.
*
* @return Recipe
*/
- protected static function save_converted_data_to_recipe( $converted_data, $recipe ) {
+ protected static function save_converted_data_to_recipe( $converted_data, $recipe, $post_id = 0 ) {
/**
* Filter converted data before it's applied to the recipe.
*
@@ -276,6 +383,9 @@
$type = array_pop( $bits );
$type = strtolower( $type );
$converted_data = apply_filters( 'tasty_recipes_convert_recipe', $converted_data, $type );
+ if ( ! self::can_keep_unfiltered_html( $post_id ) ) {
+ $converted_data = self::sanitize_converted_data( $converted_data );
+ }
foreach ( $converted_data as $key => $value ) {
$setter = "set_{$key}";
$recipe->$setter( $value );
@@ -392,6 +502,11 @@
return $retval;
}
+ if ( ! current_user_can( 'upload_files' ) ) {
+ do_action( 'tasty_recipes_imported_image', 0 );
+ return 0;
+ }
+
preg_match( '/[^?]+.(jpe?g|jpe|gif|png)b/i', $file, $matches );
if ( ! $matches ) {
do_action( 'tasty_recipes_imported_image', 0 );
--- a/tasty-recipes-lite/inc/converters/class-cookbook.php
+++ b/tasty-recipes-lite/inc/converters/class-cookbook.php
@@ -63,8 +63,7 @@
return false;
}
- $post_id = (int) $post_id; // Avoid UnusedVariable.
- $existing_post = get_post( $id );
+ $existing_post = self::get_source_post( $id, 'cookbook_recipe', $post_id );
if ( ! $existing_post ) {
return false;
}
@@ -161,6 +160,6 @@
}
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
}
--- a/tasty-recipes-lite/inc/converters/class-easyrecipe.php
+++ b/tasty-recipes-lite/inc/converters/class-easyrecipe.php
@@ -231,7 +231,7 @@
$value = html_entity_decode( $value, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
$converted_data[ $key ] = $value;
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
/**
--- a/tasty-recipes-lite/inc/converters/class-mealplannerpro.php
+++ b/tasty-recipes-lite/inc/converters/class-mealplannerpro.php
@@ -118,7 +118,7 @@
}
$converted_data[ $tr ] = $value;
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
/**
--- a/tasty-recipes-lite/inc/converters/class-mediavine-create.php
+++ b/tasty-recipes-lite/inc/converters/class-mediavine-create.php
@@ -547,7 +547,7 @@
);
}
- $recipe = self::save_converted_data_to_recipe( $converted_data, $recipe );
+ $recipe = self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
update_post_meta( $recipe->get_id(), self::CREATE_KEY_META, $create_id );
return $recipe;
--- a/tasty-recipes-lite/inc/converters/class-simple-recipe-pro.php
+++ b/tasty-recipes-lite/inc/converters/class-simple-recipe-pro.php
@@ -215,6 +215,6 @@
update_post_meta( $recipe->get_id(), 'srp_ratings', $ratings );
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
}
--- a/tasty-recipes-lite/inc/converters/class-wp-recipe-maker.php
+++ b/tasty-recipes-lite/inc/converters/class-wp-recipe-maker.php
@@ -96,7 +96,6 @@
* @return false|Recipe
*/
public static function create_recipe_from_existing( $existing, $post_id ) { // phpcs:ignore SlevomatCodingStandard
- $post_id = (int) $post_id; // Avoid UnusedVariable.
if ( preg_match( '/<!--WPRM Recipe (d+)-->/', $existing, $matches ) ) {
$id = $matches[1];
} elseif ( preg_match( '#<!-- wp:wp-recipe-maker/recipe(.+)/?-->#Us', $existing, $matches ) ) {
@@ -109,7 +108,7 @@
return false;
}
- $existing_post = get_post( $id );
+ $existing_post = self::get_source_post( $id, 'wprm_recipe', $post_id );
if ( ! $existing_post ) {
return false;
}
@@ -304,6 +303,6 @@
// Restore registered taxonomies.
$GLOBALS['wp_taxonomies'] = $backup_taxonomies; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
}
--- a/tasty-recipes-lite/inc/converters/class-wp-ultimate-recipe.php
+++ b/tasty-recipes-lite/inc/converters/class-wp-ultimate-recipe.php
@@ -54,13 +54,11 @@
return false;
}
$recipe_id = (int) $data['id'];
- $existing_post = get_post( $recipe_id );
+ $existing_post = self::get_source_post( $recipe_id, 'recipe', $post_id );
if ( ! $existing_post ) {
return false;
}
- $post_id = (int) $post_id; // Avoid UnusedVariable.
-
$recipe = Recipe::create();
$converted_data = array();
@@ -175,6 +173,6 @@
// Restore registered taxonomies.
$GLOBALS['wp_taxonomies'] = $backup_taxonomies; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
}
--- a/tasty-recipes-lite/inc/converters/class-wpcom.php
+++ b/tasty-recipes-lite/inc/converters/class-wpcom.php
@@ -105,7 +105,7 @@
return false;
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
/**
--- a/tasty-recipes-lite/inc/converters/class-yummly.php
+++ b/tasty-recipes-lite/inc/converters/class-yummly.php
@@ -119,6 +119,6 @@
}
$converted_data[ $tr ] = $value;
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
}
--- a/tasty-recipes-lite/inc/converters/class-yumprint.php
+++ b/tasty-recipes-lite/inc/converters/class-yumprint.php
@@ -147,7 +147,7 @@
$data = json_decode( $existing->nutrition );
if ( ! is_object( $data ) ) {
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
foreach ( $mapping_fields as $mpp => $tr ) {
@@ -163,6 +163,6 @@
$converted_data[ $tr ] = $value;
}
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
}
--- a/tasty-recipes-lite/inc/converters/class-ziplist.php
+++ b/tasty-recipes-lite/inc/converters/class-ziplist.php
@@ -154,6 +154,6 @@
update_post_meta( $recipe->get_id(), 'zrp_ratings', $ratings );
}
- return self::save_converted_data_to_recipe( $converted_data, $recipe );
+ return self::save_converted_data_to_recipe( $converted_data, $recipe, $post_id );
}
}
--- a/tasty-recipes-lite/inc/integrations/class-recipe-graph-piece-yoast.php
+++ b/tasty-recipes-lite/inc/integrations/class-recipe-graph-piece-yoast.php
@@ -67,6 +67,10 @@
if ( ! is_singular() ) {
return false;
}
+ // Don't expose recipes of password-protected posts before the password is entered.
+ if ( post_password_required( $this->context->id ) ) {
+ return false;
+ }
$recipes = Tasty_Recipes::get_recipes_for_post(
$this->context->id,
array(
--- a/tasty-recipes-lite/inc/integrations/class-recipe-graph-piece.php
+++ b/tasty-recipes-lite/inc/integrations/class-recipe-graph-piece.php
@@ -72,6 +72,10 @@
if ( ! is_singular() ) {
return false;
}
+ // Don't expose recipes of password-protected posts before the password is entered.
+ if ( post_password_required( $this->context->id ) ) {
+ return false;
+ }
$recipes = Tasty_Recipes::get_recipes_for_post(
$this->context->id,
array(
--- a/tasty-recipes-lite/inc/integrations/nutrifox/class-connection-routes.php
+++ b/tasty-recipes-lite/inc/integrations/nutrifox/class-connection-routes.php
@@ -0,0 +1,368 @@
+<?php
+/**
+ * REST routes and OAuth callback for the Nutrifox connection.
+ *
+ * @package Tasty_Recipes
+ */
+
+namespace Tasty_RecipesIntegrationsNutrifox;
+
+use Tasty_RecipesEditor;
+use Tasty_RecipesUtils;
+use WP_Error;
+use WP_REST_Request;
+use WP_REST_Response;
+use WP_REST_Server;
+
+/**
+ * REST routes and OAuth callback for the Nutrifox connection.
+ *
+ * @since 1.2.12
+ */
+class Connection_Routes {
+
+ /**
+ * REST namespace.
+ *
+ * @since 1.2.12
+ *
+ * @var string
+ */
+ public const REST_NAMESPACE = 'tasty-recipes/v1';
+
+ /**
+ * REST route base.
+ *
+ * @since 1.2.12
+ *
+ * @var string
+ */
+ public const REST_BASE = 'nutrifox';
+
+ /**
+ * Registers the hooks.
+ *
+ * @since 1.2.12
+ *
+ * @return void
+ */
+ public static function load_hooks() {
+ add_action( 'rest_api_init', array( __CLASS__, 'register_routes' ) );
+ add_action( 'admin_post_' . Connection::CALLBACK_ACTION, array( __CLASS__, 'handle_callback' ) );
+ add_action( 'admin_post_nopriv_' . Connection::CALLBACK_ACTION, array( __CLASS__, 'handle_callback' ) );
+ }
+
+ /**
+ * Registers the REST routes.
+ *
+ * @since 1.2.12
+ *
+ * @return void
+ */
+ public static function register_routes() {
+ register_rest_route(
+ self::REST_NAMESPACE,
+ '/' . self::REST_BASE . '/conn