Skip to content

Commit

Permalink
update to master
Browse files Browse the repository at this point in the history
  • Loading branch information
rlankhorst committed Sep 1, 2023
1 parent 08ba788 commit 13b8387
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 101 deletions.
106 changes: 54 additions & 52 deletions RecipeTable/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,49 +113,51 @@
<button type="submit" class="button button-primary save"><?php _e('Save', 'zip-recipes') ?></button>
<input type="submit" class="button button-primary exit" value="<?php _e('Save and close', 'zip-recipes') ?>">
</div>
<div id="general" class="zrdn-tabcontent <?php if ($active_tab=='general') echo 'active'?>">
<?php
$preview_post_id = get_option('zrdn_preview_post_id');
if ( !$zrdn_popup && $recipe->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 {
?>
<a class="button button-default"
href="<?php echo add_query_arg(array('post' => $recipe->post_id, 'action' => 'edit'), admin_url('post.php')) ?>"><?php _e("Edit linked post", "zip-recipes") ?></a>
<a class="button button-default"
href="<?php echo add_query_arg(array('page' => 'zrdn-recipes', 'id' => $recipe->recipe_id, 'action' => 'unlink', 'nonce' => wp_create_nonce('zrdn_save_recipe') ), admin_url()) ?>"><?php _e("Unlink from post", "zip-recipes") ?></a>
<?php if (get_post_status($recipe->post_id)==='publish') { ?>
<a class="button button-default" target="_blank"
href="<?php echo get_permalink($recipe->post_id) ?>"><?php _e("View", "zip-recipes") ?></a>

<?php }
}
} ?>
<?php
if ($recipe->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"),
'<a href="'.add_query_arg(array('post' => $recipe->post_id, 'action' => 'edit'), admin_url('post.php')).'">','</a>')
, 'notice', true, false, false);
}

$fields = array(
<div id="general" class="zrdn-tabcontent <?php if ($active_tab == 'general') echo 'active' ?>">
<?php
$preview_post_id = get_option('zrdn_preview_post_id');

if (!$zrdn_popup && ! empty( $recipe->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
?>
<a class="button button-default"
href="<?php echo add_query_arg(array('post' => $recipe->post_id, 'action' => 'edit'), admin_url('post.php')) ?>"><?php _e("Edit linked post", "zip-recipes") ?></a>
<a class="button button-default"
href="<?php echo add_query_arg(array('page' => 'zrdn-recipes', 'id' => $recipe->recipe_id, 'action' => 'unlink', 'nonce' => wp_create_nonce('zrdn_save_recipe')), admin_url()) ?>"><?php _e("Unlink from post", "zip-recipes") ?></a>
<?php if ( get_post_status($recipe->post_id ) === 'publish') { ?>
<a class="button button-default" target="_blank"
href="<?php echo get_permalink($recipe->post_id) ?>"><?php _e("View", "zip-recipes") ?></a>
<?php
}
}
}
if (!empty($recipe->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"),
'<a href="' . add_query_arg(array('post' => $recipe->post_id, 'action' => 'edit'), admin_url('post.php')) . '">', '</a>'),
'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',
Expand Down Expand Up @@ -256,15 +258,15 @@
array(
'type' => 'editor',
'fieldname' => 'notes',
'value' => $recipe->notes,
'value' => $recipe->notes ?? '',
'label' => __("Notes", 'zip-recipes'),
'media' => false,
),

array(
'type' => 'editor',
'fieldname' => 'summary',
'value' => $recipe->summary,
'value' => $recipe->summary ?? '',
'label' => __("Summary", 'zip-recipes'),
'media' => false,
),
Expand All @@ -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'),'<a target="_blank" href="https://ziprecipes.net/prevent-author-warning-by-google-by-adding-an-author-to-your-recipe/">','</a>'),
'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'), '<a target="_blank" href="https://ziprecipes.net/prevent-author-warning-by-google-by-adding-an-author-to-your-recipe/">', '</a>'),
'media' => false,
);
}


$fields = apply_filters('zrdn_edit_fields', $fields, $recipe);
foreach ($fields as $field_args) {
Expand Down Expand Up @@ -530,4 +532,4 @@

</div><!-- container -->
</form>
</div>
</div>
17 changes: 8 additions & 9 deletions _inc/class.ziprecipes.util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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' ),
),
Expand Down
101 changes: 66 additions & 35 deletions models/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = '';
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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",
Expand Down Expand Up @@ -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) );
Expand Down Expand Up @@ -1577,15 +1601,22 @@ public function insert_breaks($text)
*/
public function richify_item($item, $type=false)
{
if ( empty( $item ) ) {
return $item;
}

$output = preg_replace('/\[([^\]\|\[]*)\|([^\]\|\[]*)\]/', '<a href="\\2" target="_blank">\\1</a>', $item);
$output = preg_replace('/(^|\s)\*([^\s\*][^\*]*[^\s\*]|[^\s\*])\*(\W|$)/', '\\1<span class="bold">\\2</span>\\3', $output);
$output = preg_replace('/(^|\s)_([^\s_][^_]*[^\s_]|[^\s_])_(\W|$)/', '\\1<span class="italic">\\2</span>\\3', $output);

if ($type === 'notes' || $type === 'summary') {
$output = $this->insert_breaks($output);
}

return $output;
}


/**
* Delete recipe from table
*
Expand Down
Loading

0 comments on commit 13b8387

Please sign in to comment.