From 13b838768f12e9c2dfe78a803e7d45066de560a2 Mon Sep 17 00:00:00 2001 From: Rogier Lankhorst Date: Fri, 1 Sep 2023 12:53:01 +0200 Subject: [PATCH] update to master --- RecipeTable/edit.php | 106 ++++++++++++++-------------- _inc/class.ziprecipes.util.php | 17 +++-- models/Recipe.php | 101 +++++++++++++++++--------- plugins/RecipeGrid2/RecipeGrid2.php | 14 ++-- 4 files changed, 137 insertions(+), 101 deletions(-) diff --git a/RecipeTable/edit.php b/RecipeTable/edit.php index ad99957..6e0b8d7 100644 --- a/RecipeTable/edit.php +++ b/RecipeTable/edit.php @@ -50,7 +50,7 @@ if (isset($_POST['zrdn_recipe_id'])) $recipe_id = intval($_POST['zrdn_recipe_id']); $recipe = new Recipe($recipe_id); - if (strlen($recipe->recipe_title)==0) { + if (empty($recipe->recipe_title)) { $recipe->recipe_title = __("New recipe", "zip-recipes"); //when empty, we grab recipe title from post @@ -113,49 +113,51 @@ -
- post_id && $recipe->post_id !== $preview_post_id ){ - if ( get_post_type($recipe->post_id) === 'trash') { - zrdn_notice(__("This recipe is linked to a post, but this post has been trashed. You can untrash the post, or link the recipe to another post or page", "zip-recipes"), 'warning'); - } else { - ?> - - - post_id)==='publish') { ?> - - - - is_featured_post_image && Util::get_option('hide_on_duplicate_image') ){ - zrdn_notice(__("Your recipe image is the same as your post image. The image will be hidden on the front end.", "zip-recipes") ); - } - $tags = wp_get_post_tags( $recipe->post_id ); - if ($recipe->post_id && !$tags){ - zrdn_notice( - sprintf(__("You haven't added any tags to your post yet. In your post you can %sadd%s some tags relevant to this recipe. These will get added as keywords to your recipes microdata.", "zip-recipes"), - '','') - , 'notice', true, false, false); - } - - $fields = array( +
+ post_id ) && $recipe->post_id !== $preview_post_id ) { + if (get_post_type($recipe->post_id) === 'trash') { + zrdn_notice(__("This recipe is linked to a post, but this post has been trashed. You can untrash the post, or link the recipe to another post or page", "zip-recipes"), 'warning'); + } else { + // Only execute if $recipe->post_id is not empty + ?> + + + post_id ) === 'publish') { ?> + + is_featured_post_image) && Util::get_option('hide_on_duplicate_image')) { + zrdn_notice(__("Your recipe image is the same as your post image. The image will be hidden on the front end.", "zip-recipes")); + } + if (!empty($recipe->post_id)) { + $tags = wp_get_post_tags($recipe->post_id); + if (!$tags) { + zrdn_notice( + sprintf(__("You haven't added any tags to your post yet. In your post you can %sadd%s some tags relevant to this recipe. These will get added as keywords to your recipes microdata.", "zip-recipes"), + '', ''), + 'notice', true, false, false + ); + } + } + + $fields = array( array( 'type' => 'upload', 'fieldname' => 'recipe_image', - 'low_resolution_notice' => __( "Low resolution, please upload a better quality image.", - 'zip-recipes' ), + 'low_resolution_notice' => __( "Low resolution, please upload a better quality image.", 'zip-recipes' ), 'size' => 'zrdn_recipe_image', - 'value' => $recipe->recipe_image, - 'thumbnail_id' => $recipe->recipe_image_id, - 'label' => __( "Recipe image", - 'zip-recipes' ), - 'missing_value' => $recipe->missing_sharing_values['recipe_image_id'], + 'value' => $recipe->recipe_image ?? '', + 'thumbnail_id' => $recipe->recipe_image_id ?? 0, + 'label' => __( "Recipe image", 'zip-recipes' ), + 'missing_value' => $recipe->missing_sharing_values['recipe_image_id'] ?? 0, ), array( 'type' => 'text', @@ -256,7 +258,7 @@ array( 'type' => 'editor', 'fieldname' => 'notes', - 'value' => $recipe->notes, + 'value' => $recipe->notes ?? '', 'label' => __("Notes", 'zip-recipes'), 'media' => false, ), @@ -264,7 +266,7 @@ array( 'type' => 'editor', 'fieldname' => 'summary', - 'value' => $recipe->summary, + 'value' => $recipe->summary ?? '', 'label' => __("Summary", 'zip-recipes'), 'media' => false, ), @@ -280,15 +282,15 @@ /** * Category saved in recipe is deprecated, we move to wordpress categories */ - if (strlen($recipe->category)==0){ - $fields['categoryField']=array( - 'type' => 'notice', - 'fieldname' => 'categoryDeprecated', - 'label' => sprintf(__('The recipe category has been moved to the WordPress categories. You can now assign a category to your post in the WordPress post editor','zip-recipes'),'',''), - 'media' => false, - 'callback' => 'cmplzSelectedCategories' - ); - } + if (empty($recipe->category)) { + $fields['categoryField'] = array( + 'type' => 'notice', + 'fieldname' => 'categoryDeprecated', + 'label' => sprintf(__('The recipe category has been moved to the WordPress categories. You can now assign a category to your post in the WordPress post editor', 'zip-recipes'), '', ''), + 'media' => false, + ); + } + $fields = apply_filters('zrdn_edit_fields', $fields, $recipe); foreach ($fields as $field_args) { @@ -530,4 +532,4 @@
-
\ No newline at end of file + diff --git a/_inc/class.ziprecipes.util.php b/_inc/class.ziprecipes.util.php index 0b03836..8e46d35 100755 --- a/_inc/class.ziprecipes.util.php +++ b/_inc/class.ziprecipes.util.php @@ -215,14 +215,13 @@ public static function render_template($file, $recipe = false, $settings = false } if ($recipe) { - foreach ($recipe as $fieldname => $value) { - if (is_array($value)) { - continue; - } - $contents = strtr($contents, ['{' . $fieldname . '}' => $value]); - } - } - + foreach ($recipe as $fieldname => $value) { + if (is_array($value)) { + continue; + } + $contents = strtr($contents, ['{' . $fieldname . '}' => $value]); + } + } if (is_array($settings) && count($settings)>0){ foreach($settings as $placeholder => $value){ @@ -918,7 +917,7 @@ public static function get_fields( $type = false, $plugins_only = false ) { 'type' => 'checkbox', 'source' => 'plugins', 'is_plugin' => true, - 'disabled' => true, + 'disabled' => false, 'table' => false, 'label' => __( 'Recipe Gallery', 'zip-recipes' ), ), diff --git a/models/Recipe.php b/models/Recipe.php index 55e44bd..a314f07 100755 --- a/models/Recipe.php +++ b/models/Recipe.php @@ -562,7 +562,7 @@ public function load(){ } //backwards compatibility: update attachment id if it's not there - if (!$this->recipe_image_id && strlen($this->recipe_image)>0){ + if (!$this->recipe_image_id && !is_null($this->recipe_image) && strlen($this->recipe_image) > 0) { $recipe_image_id = attachment_url_to_postid($this->recipe_image); if (!$recipe_image_id) $recipe_image_id = get_post_thumbnail_id($this->post_id); $this->recipe_image_id = $recipe_image_id; @@ -600,22 +600,30 @@ public function load(){ $post_categories = wp_get_post_categories($this->post_id); $cats = array(); $all_from_post = true; - foreach ($post_categories as $c) { - $cat = get_category($c); - if (!term_exists($c) ) { - continue; - } + foreach ($post_categories as $c) { + $cat = get_category($c); + if (!term_exists($c)) { + continue; + } - $cats[] = $cat->name; - $this->categories[] = $cat->term_id; - //if all categories are loaded from wordpress, we leave the categories string blank. - if (strpos( $this->category, $cat->name) === FALSE) { - $all_from_post = false; - } - } - if (strlen($this->category)===0) $all_from_post = true; - $this->category = implode(', ',$cats); - if ($all_from_post){ + $cats[] = $cat->name; + $this->categories[] = $cat->term_id; + // if all categories are loaded from WordPress, we leave the categories string blank. + if (!empty($this->category) && strpos($this->category, $cat->name) === FALSE) { + $all_from_post = false; + } + } + $all_from_post = false; + + + // Check if $this->category is null or an empty string + if (empty($this->category)) { + $all_from_post = true; + } + + $this->category = implode(', ', $cats); + + if ($all_from_post) { $this->category = ''; } @@ -645,12 +653,15 @@ public function load(){ $this->has_nutrition_data = true; } - if ( strlen($this->video_url) ) { - //skip when on recipe overview page - if ( !Util::is_recipe_overview_page() && $this->single ) { - $this->video_url_output = ( strpos( $this->video_url, '_value' ) !== false ) ? $this->video_url : wp_oembed_get( $this->video_url ); - } - } + if (!empty($this->video_url)) { + //skip when on recipe overview page + if (!Util::is_recipe_overview_page() && $this->single) { + $this->video_url_output = (strpos($this->video_url, '_value') !== false) + ? $this->video_url + : wp_oembed_get($this->video_url); + } + } + $this->formatted_notes = $this->richify_item($this->zrdn_format_image($this->notes), 'notes'); $this->summary_rich = $this->richify_item($this->zrdn_format_image($this->summary), 'summary'); $this->nested_ingredients = $this->get_nested_items($this->ingredients); @@ -1152,17 +1163,18 @@ public function track_hit(){ * * @param string $type * @param string $value + * * @return string $daily_value in % */ public function calculate_daily_value($type, $value){ //in case of preview mode, return default value - if (strpos($value, '_value')!==FALSE){ - return str_replace("_value", "_value_daily", $value); - } + if ($value !== null && strpos($value, '_value') !== FALSE) { + return str_replace("_value", "_value_daily", $value); + } //get number value - $value = str_replace(array(' ', 'mg', 'g', 'µg', 'kcal'), '', $value); + $value = str_replace(array(' ', 'mg', 'g', 'µg', 'kcal'), '', (string) $value); $daily_values = array( 'fat' => 65, //g @@ -1281,16 +1293,21 @@ public function jsonld() //if it's not a food item, return empty if ($this->non_food) return array(); + $formattedIngredientsArray = array(); - foreach (explode("\n", $this->ingredients) as $item) { - $itemArray = $this->zrdn_format_item($item); - $formattedIngredientsArray[] = strip_tags( do_shortcode($itemArray['content']) ); + if (!empty($this->ingredients)) { + foreach (explode("\n", $this->ingredients) as $item) { + $itemArray = $this->zrdn_format_item($item); + $formattedIngredientsArray[] = strip_tags( do_shortcode($itemArray['content']) ); + } } $formattedInstructionsArray = array(); - foreach (explode("\n", $this->instructions) as $item) { - $itemArray = $this->zrdn_format_item($item); - $formattedInstructionsArray[] = strip_tags( do_shortcode($itemArray['content']) ); + if (!empty($this->instructions)) { + foreach (explode("\n", $this->instructions) as $item) { + $itemArray = $this->zrdn_format_item($item); + $formattedInstructionsArray[] = strip_tags( do_shortcode($itemArray['content']) ); + } } $keywords= false; @@ -1310,8 +1327,12 @@ public function jsonld() } $description = $this->summary; - if (strlen($description)===0) $description = $this->recipe_title; - $description = trim(preg_replace('/\s+/', ' ', strip_tags($description))); + if (!empty($description)) { + if (strlen($description)===0) $description = $this->recipe_title; + $description = trim(preg_replace('/\s+/', ' ', strip_tags($description))); + } else { + $description = $this->recipe_title; + } $recipe_json_ld = array( "@context" => "http://schema.org", @@ -1526,7 +1547,10 @@ public function zrdn_get_responsive_image_attributes( $url ) */ public function zrdn_format_image($item) { - preg_match_all('/(%http|%https):\/\/[^ ]+(\.gif|\.jpg|\.jpeg|\.png)/', $item, $matches); + if (!empty($item)) { + preg_match_all('/(%http|%https):\/\/[^ ]+(\.gif|\.jpg|\.jpeg|\.png)/', $item, $matches); + } + if (isset($matches[0]) && !empty($matches[0])) { foreach ($matches[0] as $image) { $attributes = $this->zrdn_get_responsive_image_attributes(str_replace('%', '', $image) ); @@ -1577,15 +1601,22 @@ public function insert_breaks($text) */ public function richify_item($item, $type=false) { + if ( empty( $item ) ) { + return $item; + } + $output = preg_replace('/\[([^\]\|\[]*)\|([^\]\|\[]*)\]/', '\\1', $item); $output = preg_replace('/(^|\s)\*([^\s\*][^\*]*[^\s\*]|[^\s\*])\*(\W|$)/', '\\1\\2\\3', $output); $output = preg_replace('/(^|\s)_([^\s_][^_]*[^\s_]|[^\s_])_(\W|$)/', '\\1\\2\\3', $output); + if ($type === 'notes' || $type === 'summary') { $output = $this->insert_breaks($output); } + return $output; } + /** * Delete recipe from table * diff --git a/plugins/RecipeGrid2/RecipeGrid2.php b/plugins/RecipeGrid2/RecipeGrid2.php index d19c382..fcb829d 100644 --- a/plugins/RecipeGrid2/RecipeGrid2.php +++ b/plugins/RecipeGrid2/RecipeGrid2.php @@ -20,7 +20,7 @@ class RecipeGrid2 extends PluginBase function __construct() { $this->dir = 'plugins/'.basename(dirname(__FILE__)).'/'; - add_shortcode('zrdn-grid', array($this, 'render_grid')); + add_shortcode('zrdn-grid', array($this, 'render_grid') ); //backward compatibility: add_shortcode('ziprecipes', array($this, 'render_grid')); @@ -30,7 +30,7 @@ function __construct() { add_action('admin_enqueue_scripts', array($this, 'tinymce_css')); } - add_action( 'enqueue_block_assets', array($this, 'enqueue_assets_gutenberg')); + add_action( 'enqueue_block_assets', array($this, 'enqueue_assets_gutenberg') ); add_action( 'wp_ajax_nopriv_zrdn_grid_load_more', array($this, 'zrdn_grid_load_more')); add_action( 'wp_ajax_zrdn_grid_load_more', array($this, 'zrdn_grid_load_more')); add_action( 'post_updated', array($this, 'clear_category_cache'), 10, 3 ); @@ -42,7 +42,6 @@ function __construct() { } - public function clear_category_cache($post_ID, $post_after=false, $post_before=false){ delete_transient('zrdn_recipe_categories'); } @@ -113,7 +112,7 @@ public function enqueue_assets() wp_enqueue_style('zrdn-grid-css'); wp_enqueue_script('zrdn-grid', ZRDN_PLUGIN_URL . $this->dir . "js/jquery.cubeportfolio.min.js", array('jquery'), ZRDN_VERSION_NUM, true); - wp_enqueue_script('zrdn-grid-custom', ZRDN_PLUGIN_URL . $this->dir . "js/main$min.js", array('jquery'), ZRDN_VERSION_NUM, true); + // wp_enqueue_script('zrdn-grid-custom', ZRDN_PLUGIN_URL . $this->dir . "js/main$min.js", array('jquery'), ZRDN_VERSION_NUM, true); } @@ -157,12 +156,13 @@ public function tinymce_plugin($plugin_array) public function enqueue_assets_gutenberg() { + $min = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min'; wp_register_style('zrdn-grid-css', ZRDN_PLUGIN_URL . $this->dir."css/recipegrid$min.css", false, ZRDN_VERSION_NUM); wp_enqueue_style('zrdn-grid-css'); wp_enqueue_script('zrdn-grid', ZRDN_PLUGIN_URL . $this->dir . "js/jquery.cubeportfolio.min.js", array('jquery'), ZRDN_VERSION_NUM, true); - //wp_enqueue_script('zrdn-grid-custom', ZRDN_PLUGIN_URL . $this->dir . "js/main$min.js", array('jquery'), ZRDN_VERSION_NUM, true); +// wp_enqueue_script('zrdn-grid-custom', ZRDN_PLUGIN_URL . $this->dir . "js/main$min.js", array('jquery'), ZRDN_VERSION_NUM, true); } @@ -254,6 +254,7 @@ public function render_grid($atts = array(), $content = null, $tag = '') //[ziprecipes grid id='1'] //[ziprecipes index id='1'] + ob_start(); /** @@ -356,6 +357,9 @@ public function render_grid($atts = array(), $content = null, $tag = '') } } + $min = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min'; + + wp_enqueue_script('zrdn-grid-custom', ZRDN_PLUGIN_URL . $this->dir . "js/main$min.js", array('jquery'), ZRDN_VERSION_NUM, true); wp_localize_script('zrdn-grid-custom', 'zrdn_grid_settings', $settings); $args = array( 'post_status'=>'publish',