| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cooked Shortcodes |
| 4 |
* |
| 5 |
* @package Cooked |
| 6 |
* @subpackage Shortcodes |
| 7 |
* @since 1.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Cooked_Shortcodes Class |
| 15 |
* |
| 16 |
* This class handles the settings creation and contains functions for retreiving those settings. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
|
| 21 |
class Cooked_Shortcodes { |
| 22 |
|
| 23 |
function __construct() { |
| 24 |
// Allow shortcodes in widgets |
| 25 |
add_filter( 'widget_text', 'do_shortcode' ); |
| 26 |
|
| 27 |
// Site-Wide |
| 28 |
add_shortcode('cooked-browse', [$this, 'cooked_browse_shortcode'] ); |
| 29 |
add_shortcode('cooked-search', [$this, 'cooked_search_shortcode'] ); |
| 30 |
add_shortcode('cooked-recipe', [$this, 'cooked_recipe_shortcode'] ); |
| 31 |
add_shortcode('cooked-categories', [$this, 'cooked_categories_shortcode'] ); |
| 32 |
add_shortcode('cooked-recipe-list', [$this, 'cooked_recipe_list_shortcode'] ); |
| 33 |
add_shortcode('cooked-recipe-card', [$this, 'cooked_recipe_card_shortcode'] ); |
| 34 |
|
| 35 |
if ( shortcode_exists( 'timer' ) ): |
| 36 |
add_shortcode('cooked-timer', [$this, 'cooked_timer'] ); |
| 37 |
else: |
| 38 |
add_shortcode('cooked-timer', [$this, 'cooked_timer'] ); |
| 39 |
add_shortcode('timer', [$this, 'cooked_timer'] ); |
| 40 |
endif; |
| 41 |
|
| 42 |
// Recipe-Only |
| 43 |
add_shortcode('cooked-title', [$this, 'cooked_title_shortcode'] ); |
| 44 |
add_shortcode('cooked-gallery', [$this, 'cooked_gallery_shortcode'] ); |
| 45 |
add_shortcode('cooked-image', [$this, 'cooked_image_shortcode'] ); |
| 46 |
add_shortcode('cooked-info', [$this, 'cooked_info_shortcode'] ); |
| 47 |
add_shortcode('cooked-excerpt', [$this, 'cooked_excerpt_shortcode'] ); |
| 48 |
add_shortcode('cooked-notes', [$this, 'cooked_notes_shortcode'] ); |
| 49 |
add_shortcode('cooked-ingredients', [$this, 'cooked_ingredients_shortcode'] ); |
| 50 |
add_shortcode('cooked-directions', [$this, 'cooked_directions_shortcode'] ); |
| 51 |
add_shortcode('cooked-nutrition', [$this, 'cooked_nutrition_shortcode'] ); |
| 52 |
|
| 53 |
// Add preprocessing filter for shortcodes for Elementor, Divi, and other builders compatibility. |
| 54 |
add_filter('pre_do_shortcode_tag', [$this, 'preprocess_shortcode'], 10, 4); |
| 55 |
} |
| 56 |
|
| 57 |
public function preprocess_shortcode($output, $tag, $attr, $m) { |
| 58 |
// Tags to skip |
| 59 |
$skip_tags = [ |
| 60 |
'cooked-search', |
| 61 |
'cooked-browse', |
| 62 |
'cooked-timer', |
| 63 |
'cooked-recipe', |
| 64 |
'cooked-recipe-list', |
| 65 |
'cooked-recipe-card', |
| 66 |
]; |
| 67 |
|
| 68 |
// Only process for Cooked shortcodes |
| 69 |
if (is_front_page() || strpos($tag, 'cooked-') === false || in_array($tag, $skip_tags)) { |
| 70 |
return $output; |
| 71 |
} |
| 72 |
|
| 73 |
global $recipe_settings, $post; |
| 74 |
|
| 75 |
// If recipe settings are empty, try to get them. |
| 76 |
if (empty($recipe_settings)) { |
| 77 |
// Try to get recipe settings from current post. |
| 78 |
$post_id = isset($post->ID) ? $post->ID : false; |
| 79 |
if ($post_id && get_post_type( $post_id ) === 'cp_recipe') { |
| 80 |
$recipe_settings = Cooked_Recipes::get($post_id, true); |
| 81 |
} else { |
| 82 |
// We are in the editor but not on a recipe post type. Maybe a single recipe template? |
| 83 |
// Uses the first recipe found in the database as a sample. |
| 84 |
$recipe_settings = Cooked_Recipes::get(false, true, false, 1); |
| 85 |
} |
| 86 |
|
| 87 |
// If still empty and we have a specific recipe ID in attributes, try to get them. |
| 88 |
if (empty($recipe_settings) && isset($attr['id'])) { |
| 89 |
$recipe_settings = Cooked_Recipes::get(intval($attr['id']), true); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $output; |
| 94 |
} |
| 95 |
|
| 96 |
public function cooked_search_shortcode( $atts, $content = null ) { |
| 97 |
// Shortcode Attributes |
| 98 |
$options = shortcode_atts( |
| 99 |
[ |
| 100 |
'compact' => false, |
| 101 |
'hide_browse' => false, |
| 102 |
'hide_sorting' => false, |
| 103 |
'inline_browse' => false, |
| 104 |
], $atts |
| 105 |
); |
| 106 |
|
| 107 |
return Cooked_Recipes::recipe_search_box( $options ); |
| 108 |
} |
| 109 |
|
| 110 |
public function cooked_timer( $atts, $content = null ) { |
| 111 |
global $cooked_timer_identifier; |
| 112 |
|
| 113 |
// Shortcode Attributes |
| 114 |
$atts = shortcode_atts( |
| 115 |
[ |
| 116 |
'seconds' => 0, |
| 117 |
'minutes' => 0, |
| 118 |
'length' => 0, // Deprecated, left here for backwards compatibility with 2.x |
| 119 |
'hours' => 0, |
| 120 |
'desc' => '' |
| 121 |
], $atts |
| 122 |
); |
| 123 |
|
| 124 |
$desc = esc_attr($atts['desc']); |
| 125 |
$seconds = $atts['seconds']; |
| 126 |
$minutes = $atts['minutes'] ? $atts['minutes'] * 60 : $atts['length'] * 60; |
| 127 |
$hours = $atts['hours'] * 60 * 60; |
| 128 |
$seconds = $seconds + $minutes + $hours; |
| 129 |
|
| 130 |
if (!$cooked_timer_identifier) { |
| 131 |
$cooked_timer_identifier = 1; |
| 132 |
} else { |
| 133 |
$cooked_timer_identifier++; |
| 134 |
} |
| 135 |
|
| 136 |
$timer_id = md5( $seconds . $desc . $content ) . '_' . $cooked_timer_identifier; |
| 137 |
|
| 138 |
wp_enqueue_script( 'cooked-timer' ); |
| 139 |
|
| 140 |
return '<span class="cooked-timer"><a data-timer-id="' . esc_attr( $timer_id ) . '" data-seconds="' . esc_attr( $seconds ) . '" data-desc="' . ( $desc ? wp_strip_all_tags( $desc ) : wp_strip_all_tags( $content ) ) . '"><i class="cooked-icon cooked-icon-clock"></i> ' . wp_kses_post( $content ) . '</a></span>'; |
| 141 |
} |
| 142 |
|
| 143 |
public function cooked_browse_shortcode( $sc_atts, $content = null ) { |
| 144 |
global $_cooked_settings; |
| 145 |
|
| 146 |
if ( isset($_cooked_settings['advanced']) && !empty($_cooked_settings['advanced']) && in_array( 'disable_public_recipes', $_cooked_settings['advanced'] ) ) { |
| 147 |
/* translators: referring to the bottom of the Settings page. */ |
| 148 |
return current_user_can( 'edit_cooked_settings' ) ? wpautop( sprintf( __('Public recipes are currently disabled. You can change this at the bottom of the %s page.','cooked'), '<a href="' . trailingslashit( admin_url() ) . 'admin.php?page=cooked_settings" target="_blank">' . __( 'Settings', 'cooked' ) . '</a>' ) ) : false; |
| 149 |
} |
| 150 |
|
| 151 |
if ( is_admin() ) return false; |
| 152 |
|
| 153 |
$author_query_var = sanitize_key( get_query_var( 'recipe_author', false ) ); |
| 154 |
if ( !$author_query_var && isset( $_GET['recipe_author'] ) ) { |
| 155 |
$author_query_var = sanitize_key( $_GET['recipe_author'] ); |
| 156 |
} |
| 157 |
|
| 158 |
// Shortcode Attributes |
| 159 |
$atts = shortcode_atts( apply_filters( 'cooked_browse_shortcode_default_attributes', [ |
| 160 |
'category' => false, |
| 161 |
'order' => false, |
| 162 |
'orderby' => false, |
| 163 |
'show' => false, |
| 164 |
'search' => 'true', |
| 165 |
'pagination' => 'true', |
| 166 |
'columns' => 3, |
| 167 |
'layout' => false, |
| 168 |
'author' => $author_query_var, |
| 169 |
'compact' => false, |
| 170 |
'hide_browse' => false, |
| 171 |
'hide_sorting' => false, |
| 172 |
'exclude' => false, |
| 173 |
'inline_browse' => false, |
| 174 |
'hide_excerpt' => false, |
| 175 |
] ), $sc_atts); |
| 176 |
|
| 177 |
return Cooked_Recipes::list_view( $atts ); |
| 178 |
} |
| 179 |
|
| 180 |
public function cooked_recipe_card_shortcode( $atts, $content = null ) { |
| 181 |
if ( is_admin() ) return false; |
| 182 |
|
| 183 |
// Shortcode Attributes |
| 184 |
$atts = shortcode_atts([ |
| 185 |
'id' => false, |
| 186 |
'category' => false, |
| 187 |
'width' => false, |
| 188 |
'style' => false, |
| 189 |
'hide_total' => false, |
| 190 |
'hide_image' => false, |
| 191 |
'hide_title' => false, |
| 192 |
'hide_excerpt' => false, |
| 193 |
'hide_author' => false |
| 194 |
], $atts); |
| 195 |
|
| 196 |
$recipe_id = intval( $atts['id'] ); |
| 197 |
$category_id = intval( $atts['category'] ); |
| 198 |
$width = Cooked_Functions::sanitize_text_field( $atts['width'] ); |
| 199 |
$style = Cooked_Functions::sanitize_text_field( $atts['style'] ); |
| 200 |
$hide_image = Cooked_Functions::sanitize_text_field( $atts['hide_image'] ); |
| 201 |
$hide_total = Cooked_Functions::sanitize_text_field( $atts['hide_total'] ); |
| 202 |
$hide_title = Cooked_Functions::sanitize_text_field( $atts['hide_title'] ); |
| 203 |
$hide_excerpt = Cooked_Functions::sanitize_text_field( $atts['hide_excerpt'] ); |
| 204 |
$hide_author = Cooked_Functions::sanitize_text_field( $atts['hide_author'] ); |
| 205 |
|
| 206 |
ob_start(); |
| 207 |
|
| 208 |
if ( $recipe_id ) { |
| 209 |
echo Cooked_Recipes::card( $recipe_id, $width, $hide_image, $hide_title, $hide_excerpt, $hide_author, $style ); |
| 210 |
} elseif ( $category_id ) { |
| 211 |
echo Cooked_Taxonomies::card( $category_id, $width, $hide_image, $hide_total, $style ); |
| 212 |
} |
| 213 |
|
| 214 |
return ob_get_clean(); |
| 215 |
} |
| 216 |
|
| 217 |
public function cooked_categories_shortcode( $atts, $content = null ) { |
| 218 |
if ( is_admin() ) return false; |
| 219 |
|
| 220 |
// Shortcode Attributes |
| 221 |
$atts = shortcode_atts([ |
| 222 |
'hide_empty' => true, |
| 223 |
'child_of' => false, |
| 224 |
'style' => 'block' |
| 225 |
], $atts); |
| 226 |
|
| 227 |
$hide_empty = Cooked_Functions::sanitize_text_field( $atts['hide_empty'] ); |
| 228 |
$child_of = Cooked_Functions::sanitize_text_field( $atts['child_of'] ); |
| 229 |
$style = Cooked_Functions::sanitize_text_field( $atts['style'] ); |
| 230 |
$parents_only = $child_of ? false : true; |
| 231 |
|
| 232 |
ob_start(); |
| 233 |
|
| 234 |
$categories_array = Cooked_Settings::terms_array( 'cp_recipe_category', false, false, $hide_empty, $parents_only, $child_of ); |
| 235 |
if ( !empty($categories_array) ) { |
| 236 |
echo $style == 'list' ? '<div class="cooked-recipe-term-list">' : '<div class="cooked-recipe-term-grid cooked-clearfix">'; |
| 237 |
foreach ( $categories_array as $key => $val ) { |
| 238 |
Cooked_Taxonomies::single_taxonomy_block( $key, $style ); |
| 239 |
} |
| 240 |
echo '</div>'; |
| 241 |
} |
| 242 |
|
| 243 |
return ob_get_clean(); |
| 244 |
} |
| 245 |
|
| 246 |
public function cooked_recipe_list_shortcode( $atts, $content = null ) { |
| 247 |
if ( is_admin() ) return false; |
| 248 |
|
| 249 |
// Shortcode Attributes |
| 250 |
$atts = shortcode_atts( [ |
| 251 |
'orderby' => 'date', |
| 252 |
'width' => '100%', |
| 253 |
'recipes' => false, |
| 254 |
'show' => 5, |
| 255 |
'hide_image' => false, |
| 256 |
'hide_author' => false |
| 257 |
], $atts); |
| 258 |
|
| 259 |
$recipes = Cooked_Functions::sanitize_text_field( $atts['recipes'] ); |
| 260 |
$orderby = Cooked_Functions::sanitize_text_field( $atts['orderby'] ); |
| 261 |
$show = Cooked_Functions::sanitize_text_field( $atts['show'] ); |
| 262 |
$width = Cooked_Functions::sanitize_text_field( $atts['width'] ); |
| 263 |
$hide_image = Cooked_Functions::sanitize_text_field( $atts['hide_image'] ); |
| 264 |
$hide_author = Cooked_Functions::sanitize_text_field( $atts['hide_author'] ); |
| 265 |
|
| 266 |
if ( $recipes ) { |
| 267 |
$recipes = preg_replace( '/\s+/', '', $recipes ); |
| 268 |
$recipes = explode( ',', $recipes ); |
| 269 |
} |
| 270 |
|
| 271 |
ob_start(); |
| 272 |
|
| 273 |
Cooked_Recipes::recipe_list( $orderby, $show, $recipes, $width, $hide_image, $hide_author ); |
| 274 |
|
| 275 |
return ob_get_clean(); |
| 276 |
} |
| 277 |
|
| 278 |
public function cooked_recipe_shortcode( $atts, $content = null ) { |
| 279 |
if ( is_admin() ) return false; |
| 280 |
|
| 281 |
// Shortcode Attributes |
| 282 |
$atts = shortcode_atts([ |
| 283 |
'id' => false, |
| 284 |
], $atts); |
| 285 |
|
| 286 |
global $recipe_settings, $_cooked_content_unfiltered; |
| 287 |
|
| 288 |
ob_start(); |
| 289 |
|
| 290 |
$recipe_id = intval( $atts['id'] ); |
| 291 |
|
| 292 |
if ( $recipe_id ) { |
| 293 |
|
| 294 |
$recipe_settings = Cooked_Recipes::get( $recipe_id, true ); |
| 295 |
if ( !$recipe_settings || $recipe_settings && empty( $recipe_settings ) ) { |
| 296 |
return wpautop( '<strong>[cooked-recipe id="' . intval( $recipe_id ) . '"]</strong><br><em>' . __( '(recipe not found or in draft status)', 'cooked' ) . '</em>' ); |
| 297 |
} else { |
| 298 |
load_template( COOKED_DIR . 'templates/front/recipe.php', false ); |
| 299 |
} |
| 300 |
|
| 301 |
} |
| 302 |
|
| 303 |
return do_shortcode( ob_get_clean() ); |
| 304 |
} |
| 305 |
|
| 306 |
public function cooked_gallery_shortcode($atts, $content = null) { |
| 307 |
global $recipe_settings; |
| 308 |
|
| 309 |
if ( isset($recipe_settings['gallery']) && isset($recipe_settings['gallery']['type']) && $recipe_settings['gallery']['type'] == 'cooked' ) { |
| 310 |
$gallery_options = apply_filters( 'cooked_recipe_gallery_options', [ |
| 311 |
'data-fit' => 'cover', |
| 312 |
'data-nav' => 'dots', |
| 313 |
'data-width' => '100%', |
| 314 |
'data-loop' => 'true', |
| 315 |
'data-allowfullscreen' => 'true', |
| 316 |
'data-ratio' => '800/600', |
| 317 |
'data-thumbmargin' => '10', |
| 318 |
'data-thumbborderwidth' => '5', |
| 319 |
'data-swipe' => 'true', |
| 320 |
'data-thumbheight' => '75', |
| 321 |
'data-thumbwidth' => '75' |
| 322 |
]); |
| 323 |
|
| 324 |
// Generate the Shortcode Attributes |
| 325 |
foreach ( $gallery_options as $opt_name => $opt_value ) { |
| 326 |
$name_sans_data = str_replace( 'data-', '', $opt_name ); |
| 327 |
$gallery_atts[$name_sans_data] = $opt_value; |
| 328 |
} |
| 329 |
|
| 330 |
// Set the Shortcode Attributes |
| 331 |
$atts = shortcode_atts( |
| 332 |
$gallery_atts, $atts |
| 333 |
); |
| 334 |
|
| 335 |
// Check for Shortcode Attribute Customizations |
| 336 |
foreach ( $gallery_options as $opt_name => $opt_value ) { |
| 337 |
$name_sans_data = str_replace( 'data-', '', $opt_name ); |
| 338 |
if ( isset($atts[$name_sans_data]) && $atts[$name_sans_data] ) { |
| 339 |
$gallery_options[$opt_name] = $atts[$name_sans_data]; |
| 340 |
} |
| 341 |
} |
| 342 |
} else { |
| 343 |
$atts = []; |
| 344 |
} |
| 345 |
|
| 346 |
if ( isset($recipe_settings['gallery']) && isset($recipe_settings['gallery']['type']) ): |
| 347 |
|
| 348 |
switch( $recipe_settings['gallery']['type'] ): |
| 349 |
|
| 350 |
case 'cooked': |
| 351 |
|
| 352 |
if ( isset($recipe_settings['gallery']['items']) && !empty($recipe_settings['gallery']['items']) || isset($recipe_settings['gallery']['video_url']) && $recipe_settings['gallery']['video_url'] ): |
| 353 |
|
| 354 |
// Gallery Options |
| 355 |
// Developers: Filter these to change! |
| 356 |
// Full list here: http://fotorama.io/customize/options/ |
| 357 |
|
| 358 |
$gallery_options_array = []; |
| 359 |
|
| 360 |
foreach ( $gallery_options as $data_key => $data_val ): |
| 361 |
$gallery_options_array[] = $data_key . '="' . $data_val . '"'; |
| 362 |
endforeach; |
| 363 |
|
| 364 |
$gallery_html = '<div class="cooked-recipe-gallery"' . ( !empty($gallery_options_array) ? ' ' . implode( ' ', $gallery_options_array ) : '' ) . '>'; |
| 365 |
|
| 366 |
$cooked_gallery_video_last_option = apply_filters( 'cooked_gallery_video_last_option', false ); |
| 367 |
|
| 368 |
if ( !$cooked_gallery_video_last_option && isset($recipe_settings['gallery']['video_url']) && $recipe_settings['gallery']['video_url'] ): |
| 369 |
$gallery_html .= '<a href="' . esc_url($recipe_settings['gallery']['video_url']) . '" data-caption="' . esc_attr($recipe_settings['title']) . '">' . esc_html($recipe_settings['title']) . '</a>'; |
| 370 |
endif; |
| 371 |
|
| 372 |
$gallery_items = ( isset($recipe_settings['gallery']['items']) && !empty($recipe_settings['gallery']['items']) ? apply_filters( 'cooked_gallery_items_output', $recipe_settings['gallery']['items'] ) : [] ); |
| 373 |
|
| 374 |
if ( isset($gallery_items) && !empty($gallery_items) ): |
| 375 |
foreach( $gallery_items as $item ): |
| 376 |
$image_src = wp_get_attachment_image_src( $item, [900, 900] ); |
| 377 |
$image_title = get_the_title( $item ); |
| 378 |
$gallery_html .= '<a href="' . esc_url( $image_src[0] ) . '" data-alt="'.esc_attr( $image_title ).'" data-caption="'.esc_attr( $image_title ).'"><img alt="'.esc_attr( $image_title ).'" src="' . wp_get_attachment_image_url( $item, 'thumbnail' ) . '" /></a>'; |
| 379 |
endforeach; |
| 380 |
endif; |
| 381 |
|
| 382 |
if ( $cooked_gallery_video_last_option && isset($recipe_settings['gallery']['video_url']) && $recipe_settings['gallery']['video_url'] ): |
| 383 |
$gallery_html .= '<a href="' . esc_url($recipe_settings['gallery']['video_url']) . '" data-caption="' . esc_attr($recipe_settings['title']) . '">' . esc_html($recipe_settings['title']) . '</a>'; |
| 384 |
endif; |
| 385 |
|
| 386 |
// Enqueue Gallery Styles & Scripts |
| 387 |
wp_enqueue_style( 'cooked-fotorama' ); |
| 388 |
wp_enqueue_script( 'cooked-fotorama' ); |
| 389 |
|
| 390 |
$gallery_html .= '</div>'; |
| 391 |
return wp_kses_post( $gallery_html ); |
| 392 |
|
| 393 |
endif; |
| 394 |
|
| 395 |
break; |
| 396 |
|
| 397 |
case 'envira': |
| 398 |
return ( $recipe_settings['gallery']['envira'] ? '<div class="cooked-recipe-gallery-envira">' . do_shortcode('[envira-gallery id="' . intval( $recipe_settings['gallery']['envira'] ) . '"]') . '</div>' : '' ); |
| 399 |
break; |
| 400 |
|
| 401 |
case 'soliloquy': |
| 402 |
return ( $recipe_settings['gallery']['soliloquy'] ? '<div class="cooked-recipe-gallery-soliloquy">' . do_shortcode('[soliloquy id="' . intval( $recipe_settings['gallery']['soliloquy'] ) . '"]') . '</div>' : '' ); |
| 403 |
break; |
| 404 |
|
| 405 |
case 'revslider': |
| 406 |
return ( $recipe_settings['gallery']['revslider'] ? '<div class="cooked-recipe-gallery-revslider">' . do_shortcode('[rev_slider alias="' . intval( $recipe_settings['gallery']['revslider'] ) . '"]') . '</div>' : '' ); |
| 407 |
break; |
| 408 |
|
| 409 |
endswitch; |
| 410 |
|
| 411 |
endif; |
| 412 |
|
| 413 |
return false; |
| 414 |
} |
| 415 |
|
| 416 |
public function cooked_info_shortcode($atts, $content = null) { |
| 417 |
global $recipe, $recipe_settings, $_cooked_settings; |
| 418 |
|
| 419 |
if ( !isset($recipe_settings['id']) && isset($recipe->ID) ): |
| 420 |
$recipe_settings['id'] = $recipe->ID; |
| 421 |
elseif ( !isset($recipe_settings['id']) ): |
| 422 |
global $post; |
| 423 |
$recipe_settings['id'] = $post->ID; |
| 424 |
endif; |
| 425 |
|
| 426 |
// Shortcode Attributes |
| 427 |
$atts = shortcode_atts([ |
| 428 |
'left' => false, |
| 429 |
'right' => false, |
| 430 |
'include' => false, |
| 431 |
'exclude' => false, |
| 432 |
], $atts); |
| 433 |
|
| 434 |
$left = ( $atts['left'] ? array_map( 'trim', explode( ',', Cooked_Functions::sanitize_text_field($atts['left']) ) ) : false ); |
| 435 |
$right = ( $atts['right'] ? array_map( 'trim', explode( ',', Cooked_Functions::sanitize_text_field($atts['right']) ) ) : false ); |
| 436 |
$include = ( $atts['include'] ? array_map( 'trim', explode( ',', Cooked_Functions::sanitize_text_field($atts['include']) ) ) : false ); |
| 437 |
$exclude = ( $atts['exclude'] ? array_map( 'trim', explode( ',', Cooked_Functions::sanitize_text_field($atts['exclude']) ) ) : false ); |
| 438 |
|
| 439 |
$default_info_array = apply_filters( 'cooked_default_info_array', [ |
| 440 |
'author' => __('Author', 'cooked'), |
| 441 |
'difficulty_level' => __('Difficulty', 'cooked'), |
| 442 |
'servings' => __('Yields', 'cooked'), |
| 443 |
'prep_time' => __('Prep Time', 'cooked'), |
| 444 |
'cook_time' => __('Cook Time', 'cooked'), |
| 445 |
'total_time' => __('Total Time', 'cooked'), |
| 446 |
'taxonomies' => !empty($_cooked_settings['recipe_taxonomies']) ? $_cooked_settings['recipe_taxonomies'] : [] |
| 447 |
]); |
| 448 |
|
| 449 |
if ( $left ): |
| 450 |
// Left Content |
| 451 |
foreach( $left as $val ): |
| 452 |
$info_array['left'][$val] = isset( $default_info_array[$val] ) ? $default_info_array[$val] : ''; |
| 453 |
endforeach; |
| 454 |
if ( $right ): |
| 455 |
// Right Content |
| 456 |
foreach( $right as $val ): |
| 457 |
$info_array['right'][$val] = isset( $default_info_array[$val] ) ? $default_info_array[$val] : ''; |
| 458 |
endforeach; |
| 459 |
endif; |
| 460 |
elseif ( $right ): |
| 461 |
// Right Content |
| 462 |
foreach( $right as $val ): |
| 463 |
$info_array['right'][$val] = isset( $default_info_array[$val] ) ? $default_info_array[$val] : ''; |
| 464 |
endforeach; |
| 465 |
elseif ( $include ): |
| 466 |
// Include Content (left) |
| 467 |
foreach( $include as $val ): |
| 468 |
$info_array[$val] = isset( $default_info_array[$val] ) ? $default_info_array[$val] : ''; |
| 469 |
endforeach; |
| 470 |
elseif ( $exclude ): |
| 471 |
// Exclude Content (left) |
| 472 |
$info_array = $default_info_array; |
| 473 |
foreach( $exclude as $val ): |
| 474 |
unset($info_array[$val]); |
| 475 |
endforeach; |
| 476 |
else: |
| 477 |
$info_array = $default_info_array; |
| 478 |
endif; |
| 479 |
|
| 480 |
ob_start(); |
| 481 |
|
| 482 |
if ( !empty( $info_array ) ): |
| 483 |
|
| 484 |
$available_methods = apply_filters( 'cooked_available_info_shortcode_methods', [ |
| 485 |
'cooked_info_author' => 'Cooked_Recipes', |
| 486 |
'cooked_info_difficulty' => 'Cooked_Recipes', |
| 487 |
'cooked_info_servings' => 'Cooked_Recipes', |
| 488 |
'cooked_info_print' => 'Cooked_Recipes', |
| 489 |
'cooked_info_fullscreen' => 'Cooked_Recipes', |
| 490 |
'cooked_info_prep_time' => 'Cooked_Recipes', |
| 491 |
'cooked_info_cook_time' => 'Cooked_Recipes', |
| 492 |
'cooked_info_total_time' => 'Cooked_Recipes', |
| 493 |
'cooked_info_taxonomies' => 'Cooked_Recipes' |
| 494 |
]); |
| 495 |
|
| 496 |
if ( isset($info_array['left']) && !empty($info_array['left']) ): |
| 497 |
echo '<section class="cooked-left">'; |
| 498 |
foreach( $info_array['left'] as $name => $val ): |
| 499 |
$function = 'cooked_info_' . $name; |
| 500 |
if ( array_key_exists( $function, $available_methods ) ): |
| 501 |
$class = ( $available_methods[$function] == 'Cooked_Recipes' ? $this : $available_methods[$function] ); |
| 502 |
if ( method_exists( $class, $function ) ): |
| 503 |
$class::$function( $recipe_settings ); |
| 504 |
endif; |
| 505 |
endif; |
| 506 |
endforeach; |
| 507 |
echo '</section>'; |
| 508 |
|
| 509 |
if ( isset($info_array['right']) && !empty($info_array['right']) ): |
| 510 |
echo '<section class="cooked-right">'; |
| 511 |
foreach( $info_array['right'] as $name => $val ): |
| 512 |
$function = 'cooked_info_' . $name; |
| 513 |
if ( array_key_exists( $function, $available_methods ) ): |
| 514 |
$class = ( $available_methods[$function] == 'Cooked_Recipes' ? $this : $available_methods[$function] ); |
| 515 |
if ( method_exists( $class, $function ) ): |
| 516 |
$class::$function( $recipe_settings ); |
| 517 |
endif; |
| 518 |
endif; |
| 519 |
endforeach; |
| 520 |
echo '</section>'; |
| 521 |
endif; |
| 522 |
|
| 523 |
elseif ( isset($info_array['right']) && !empty($info_array['right']) ): |
| 524 |
|
| 525 |
echo '<section class="cooked-right">'; |
| 526 |
foreach ( $info_array['right'] as $name => $val ): |
| 527 |
$function = 'cooked_info_' . $name; |
| 528 |
if ( array_key_exists( $function, $available_methods ) ): |
| 529 |
$class = ( $available_methods[$function] == 'Cooked_Recipes' ? $this : $available_methods[$function] ); |
| 530 |
if ( method_exists( $class, $function ) ): |
| 531 |
$class::$function( $recipe_settings ); |
| 532 |
endif; |
| 533 |
endif; |
| 534 |
endforeach; |
| 535 |
echo '</section>'; |
| 536 |
|
| 537 |
else: |
| 538 |
|
| 539 |
foreach ( $info_array as $name => $val ): |
| 540 |
$function = 'cooked_info_' . $name; |
| 541 |
if ( array_key_exists( $function, $available_methods ) ): |
| 542 |
$class = ( $available_methods[$function] == 'Cooked_Recipes' ? $this : $available_methods[$function] ); |
| 543 |
if ( method_exists( $class, $function ) ): |
| 544 |
$class::$function( $recipe_settings ); |
| 545 |
endif; |
| 546 |
endif; |
| 547 |
endforeach; |
| 548 |
|
| 549 |
endif; |
| 550 |
|
| 551 |
endif; |
| 552 |
|
| 553 |
$cooked_info_html = ob_get_clean(); |
| 554 |
|
| 555 |
if ( $cooked_info_html ): |
| 556 |
add_filter('wp_kses_allowed_html', [$this, 'cooked_kses_servings_switcher']); |
| 557 |
add_filter('wp_kses_allowed_html', [$this, 'cooked_kses_cooked_donut']); |
| 558 |
return '<div class="cooked-recipe-info cooked-clearfix">' . wp_kses_post( $cooked_info_html ) . '</div>'; |
| 559 |
//return '<div class="cooked-recipe-info cooked-clearfix">' . $cooked_info_html . '</div>'; // @TODO: Fix this |
| 560 |
endif; |
| 561 |
} |
| 562 |
|
| 563 |
function cooked_kses_servings_switcher($tags) { |
| 564 |
$tags['select'] = [ |
| 565 |
'name' => true, |
| 566 |
'class' => true, |
| 567 |
]; |
| 568 |
$tags['option'] = [ |
| 569 |
'value' => true, |
| 570 |
'selected' => true |
| 571 |
]; |
| 572 |
return $tags; |
| 573 |
} |
| 574 |
|
| 575 |
function cooked_kses_cooked_donut($tags) { |
| 576 |
$tags['script'] = [ |
| 577 |
'type' => true |
| 578 |
]; |
| 579 |
return $tags; |
| 580 |
} |
| 581 |
|
| 582 |
public static function cooked_info_author() { |
| 583 |
global $recipe_settings, $_cooked_settings; |
| 584 |
|
| 585 |
if (in_array('author', $_cooked_settings['recipe_info_display_options'])) { |
| 586 |
$browse_page_id = isset($_cooked_settings['browse_page']) && $_cooked_settings['browse_page'] ? $_cooked_settings['browse_page'] : false; |
| 587 |
$front_page_id = get_option( 'page_on_front' ); |
| 588 |
$browse_page_url = $browse_page_id ? get_permalink( $browse_page_id ) : false; |
| 589 |
$author = !empty($recipe_settings['author']) ? $recipe_settings['author'] : false; |
| 590 |
|
| 591 |
if ( !empty($author['id']) ) { |
| 592 |
$author_slug = !empty($author['name']) ? sanitize_title($author['name']) : false; |
| 593 |
$permalink = $front_page_id != $browse_page_id && get_option('permalink_structure') ? esc_url( untrailingslashit( $browse_page_url ) . '/' . $_cooked_settings['recipe_author_permalink'] . '/' . $author['id'] . '/' . trailingslashit( $author_slug ) ) : esc_url( trailingslashit( get_home_url() ) . 'index.php?page_id=' . $_cooked_settings['browse_page'] . '&recipe_author=' . $author['id'] ); |
| 594 |
$permalink = apply_filters( 'cooked_author_permalink', $permalink, $author['id'] ); |
| 595 |
} else { |
| 596 |
$permalink = false; |
| 597 |
} |
| 598 |
|
| 599 |
$author_links = isset( $_cooked_settings['disable_author_links'][0] ) && $_cooked_settings['disable_author_links'][0] == 'disabled' ? false : true; |
| 600 |
$clickable = isset($_cooked_settings['advanced']) && !empty($_cooked_settings['advanced']) && in_array( 'disable_public_recipes', $_cooked_settings['advanced'] ) || !$author_links ? false : true; |
| 601 |
$hide_avatars = isset( $_cooked_settings['hide_author_avatars'][0] ) && $_cooked_settings['hide_author_avatars'][0] == 'hidden' ? true : false; |
| 602 |
|
| 603 |
echo '<span class="cooked-author' . ( $hide_avatars ? ' cooked-no-avatar' : '' ) . '">'; |
| 604 |
echo !$hide_avatars ? '<span class="cooked-author-avatar">' . (!empty($author) ? wp_kses_post( $author['profile_photo'] ) : '') . '</span>' : ''; |
| 605 |
echo '<strong class="cooked-meta-title">' . __('Author','cooked') . '</strong>' . ( $clickable && $permalink ? '<a href="' . esc_url( $permalink ) . '">' : '' ) . (!empty($author) ? esc_html( $author['name'] ) : '') . ( $clickable && $permalink ? '</a>' : '' ); |
| 606 |
echo '</span>'; |
| 607 |
|
| 608 |
wp_reset_postdata(); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
public static function cooked_info_difficulty( $recipe ) { |
| 613 |
global $_cooked_settings; |
| 614 |
|
| 615 |
if (in_array('difficulty_level', $_cooked_settings['recipe_info_display_options']) && isset($recipe['difficulty_level']) && $recipe['difficulty_level']) { |
| 616 |
$dl_html = '<span class="cooked-difficulty-level"><strong class="cooked-meta-title">' . __('Difficulty','cooked') . '</strong>' . Cooked_Recipes::difficulty_level( $recipe['difficulty_level'] ) . '</span>'; |
| 617 |
echo apply_filters( 'cooked_show_difficulty_level', $dl_html, $recipe['difficulty_level'] ); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
public static function cooked_info_servings( $recipe ) { |
| 622 |
global $_cooked_settings; |
| 623 |
|
| 624 |
if (in_array('servings', $_cooked_settings['recipe_info_display_options'])) { |
| 625 |
$servings = isset($recipe['nutrition']['servings']) && $recipe['nutrition']['servings'] ? $recipe['nutrition']['servings'] : 1; |
| 626 |
Cooked_Recipes::serving_size_switcher( $servings ); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
public static function cooked_info_print() { |
| 631 |
global $recipe_settings, $_cooked_settings; |
| 632 |
|
| 633 |
$recipe_post_url = get_permalink( $recipe_settings['id'] ); |
| 634 |
$query_args['print'] = 1; |
| 635 |
$query_args['servings'] = (float)esc_html( get_query_var( 'servings', false ) ); |
| 636 |
echo '<span class="cooked-print"><a target="_blank" rel="nofollow" href="' . add_query_arg( $query_args, $recipe_post_url ) . '" class="cooked-print-icon"><i class="cooked-icon cooked-icon-print"></i></a></span>'; |
| 637 |
} |
| 638 |
|
| 639 |
public static function cooked_info_fullscreen() { |
| 640 |
global $recipe_settings, $_cooked_settings; |
| 641 |
|
| 642 |
echo '<span class="cooked-fsm-button" data-recipe-id="' . esc_attr( $recipe_settings['id'] ) . '"><i class="cooked-icon cooked-icon-fullscreen"></i></span>'; |
| 643 |
wp_enqueue_script('cooked-nosleep'); |
| 644 |
} |
| 645 |
|
| 646 |
public static function cooked_info_prep_time( $recipe ) { |
| 647 |
global $_cooked_settings; |
| 648 |
|
| 649 |
if (!empty($_cooked_settings['recipe_info_display_options']) && in_array('timing_prep',$_cooked_settings['recipe_info_display_options'])) { |
| 650 |
$prep_time = isset($recipe['prep_time']) ? esc_html( $recipe['prep_time'] ) : 0; |
| 651 |
echo $prep_time ? '<span class="cooked-prep-time cooked-time"><span class="cooked-time-icon"><i class="cooked-icon cooked-icon-clock"></i></span><strong class="cooked-meta-title">' . __('Prep Time','cooked') . '</strong>' . Cooked_Measurements::time_format( $prep_time ) . '</span>' : ''; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
public static function cooked_info_cook_time( $recipe ) { |
| 656 |
global $_cooked_settings; |
| 657 |
|
| 658 |
if (!empty($_cooked_settings['recipe_info_display_options']) && in_array('timing_cook', $_cooked_settings['recipe_info_display_options'])) { |
| 659 |
$cook_time = isset($recipe['cook_time']) ? esc_html( $recipe['cook_time'] ) : 0; |
| 660 |
echo $cook_time ? '<span class="cooked-cook-time cooked-time"><span class="cooked-time-icon"><i class="cooked-icon cooked-icon-clock"></i></span><strong class="cooked-meta-title">' . __('Cook Time','cooked') . '</strong>' . Cooked_Measurements::time_format( $cook_time ) . '</span>' : ''; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
public static function cooked_info_total_time( $recipe ) { |
| 665 |
global $_cooked_settings; |
| 666 |
|
| 667 |
if (!empty($_cooked_settings['recipe_info_display_options']) && in_array('timing_total',$_cooked_settings['recipe_info_display_options'])) { |
| 668 |
$total_time = isset($recipe['total_time']) ? esc_html( $recipe['total_time'] ) : 0; |
| 669 |
|
| 670 |
if ( $total_time ) { |
| 671 |
echo $total_time ? '<span class="cooked-total-time cooked-time"><span class="cooked-time-icon"><i class="cooked-icon cooked-icon-clock"></i></span><strong class="cooked-meta-title">' . __('Total Time','cooked') . '</strong>' . Cooked_Measurements::time_format( $total_time ) . '</span>' : ''; |
| 672 |
} else { |
| 673 |
$prep_time = isset($recipe['prep_time']) ? esc_html( $recipe['prep_time'] ) : 0; |
| 674 |
$cook_time = isset($recipe['cook_time']) ? esc_html( $recipe['cook_time'] ) : 0; |
| 675 |
|
| 676 |
if ( $prep_time && $cook_time ) { |
| 677 |
$total_time = $prep_time + $cook_time; |
| 678 |
echo $total_time ? '<span class="cooked-total-time cooked-time"><span class="cooked-time-icon"><i class="cooked-icon cooked-icon-clock"></i></span><strong class="cooked-meta-title">' . __('Total Time','cooked') . '</strong>' . Cooked_Measurements::time_format( $total_time ) . '</span>' : ''; |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
public static function cooked_info_taxonomies() { |
| 685 |
global $recipe_settings, $_cooked_settings, $clickable; |
| 686 |
|
| 687 |
$clickable = isset($_cooked_settings['advanced']) && !empty($_cooked_settings['advanced']) && in_array( 'disable_public_recipes', $_cooked_settings['advanced'] ) ? false : true; |
| 688 |
|
| 689 |
if (!empty($_cooked_settings['recipe_info_display_options']) && in_array('taxonomies', $_cooked_settings['recipe_info_display_options'])): |
| 690 |
|
| 691 |
global $recipe_terms_list; |
| 692 |
$recipe_terms_list = ''; |
| 693 |
|
| 694 |
do_action( 'cooked_info_taxonomies_shortcode_before', $recipe_settings ); |
| 695 |
|
| 696 |
if (!empty($_cooked_settings['recipe_taxonomies']) && in_array('cp_recipe_category', $_cooked_settings['recipe_taxonomies'])): |
| 697 |
if ( $clickable ): |
| 698 |
$recipe_terms_list .= get_the_term_list( $recipe_settings['id'], 'cp_recipe_category', '<span class="cooked-taxonomy cooked-category"><strong class="cooked-meta-title">' . __('Category','cooked') . '</strong>', ', ', '</span>' ); |
| 699 |
else: |
| 700 |
$_recipe_terms_array = []; |
| 701 |
$recipe_terms_list .= '<span class="cooked-taxonomy cooked-category"><strong class="cooked-meta-title">' . __('Category','cooked') . '</strong>'; |
| 702 |
$recipe_terms_array = wp_get_object_terms( $recipe_settings['id'], 'cp_recipe_category' ); |
| 703 |
if ( !empty($recipe_terms_array) ): |
| 704 |
if ( ! is_wp_error( $recipe_terms_array ) ): |
| 705 |
foreach( $recipe_terms_array as $recipe_term ): |
| 706 |
$_recipe_terms_array[] = esc_html( $recipe_term->name ); |
| 707 |
endforeach; |
| 708 |
endif; |
| 709 |
endif; |
| 710 |
$recipe_terms_list .= implode( ', ', $_recipe_terms_array ); |
| 711 |
$recipe_terms_list .= '</span>'; |
| 712 |
endif; |
| 713 |
endif; |
| 714 |
|
| 715 |
do_action( 'cooked_info_taxonomies_shortcode_after', $recipe_settings ); |
| 716 |
|
| 717 |
if ( $recipe_terms_list ): |
| 718 |
echo wp_filter_post_kses( $recipe_terms_list ); |
| 719 |
endif; |
| 720 |
|
| 721 |
endif; |
| 722 |
} |
| 723 |
|
| 724 |
public function cooked_excerpt_shortcode($atts, $content = null) { |
| 725 |
global $_cooked_settings, $recipe_settings; |
| 726 |
|
| 727 |
if (isset($_cooked_settings['recipe_info_display_options']) && is_array($_cooked_settings['recipe_info_display_options']) && in_array('excerpt', $_cooked_settings['recipe_info_display_options'])) { |
| 728 |
ob_start(); |
| 729 |
|
| 730 |
if (isset($recipe_settings['excerpt']) && $recipe_settings['excerpt']) { |
| 731 |
$excerpt = Cooked_Recipes::format_content($recipe_settings['excerpt']); |
| 732 |
echo '<div class="cooked-recipe-excerpt cooked-clearfix">' . do_shortcode($excerpt) . '</div>'; |
| 733 |
} |
| 734 |
|
| 735 |
return ob_get_clean(); |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
public function cooked_notes_shortcode($atts, $content = null) { |
| 740 |
global $_cooked_settings, $recipe_settings; |
| 741 |
|
| 742 |
// Shortcode Attributes |
| 743 |
$atts = shortcode_atts([ |
| 744 |
'show_header' => false, |
| 745 |
], $atts); |
| 746 |
|
| 747 |
$show_header = Cooked_Functions::sanitize_text_field( $atts['show_header'] ); |
| 748 |
|
| 749 |
if (isset($_cooked_settings['recipe_info_display_options']) && is_array($_cooked_settings['recipe_info_display_options']) && in_array('notes', $_cooked_settings['recipe_info_display_options'])) { |
| 750 |
ob_start(); |
| 751 |
|
| 752 |
if (isset($recipe_settings['notes']) && !empty($recipe_settings['notes'])) { |
| 753 |
$notes = Cooked_Recipes::format_content($recipe_settings['notes']); |
| 754 |
$show_header = $show_header ? '<div class="cooked-heading">' . __('Notes', 'cooked') . '</div>' : ''; |
| 755 |
|
| 756 |
echo '<div class="cooked-recipe-notes cooked-clearfix">'; |
| 757 |
echo $show_header; |
| 758 |
echo do_shortcode($notes); |
| 759 |
echo '</div>'; |
| 760 |
} |
| 761 |
|
| 762 |
return ob_get_clean(); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
public function cooked_title_shortcode($atts, $content = null) { |
| 767 |
global $recipe, $recipe_settings; |
| 768 |
|
| 769 |
if (!isset($recipe_settings['id'])) { |
| 770 |
$recipe_settings['id'] = $recipe->ID; |
| 771 |
} |
| 772 |
|
| 773 |
return get_the_title($recipe_settings['id']); |
| 774 |
} |
| 775 |
|
| 776 |
public function cooked_image_shortcode($atts, $content = null) { |
| 777 |
global $recipe, $recipe_settings; |
| 778 |
|
| 779 |
if ( !isset($recipe_settings['id']) ): |
| 780 |
$recipe_settings['id'] = $recipe->ID; |
| 781 |
endif; |
| 782 |
|
| 783 |
$recipe = get_post( $recipe_settings['id'] ); |
| 784 |
wp_reset_postdata(); |
| 785 |
|
| 786 |
ob_start(); |
| 787 |
|
| 788 |
if (has_post_thumbnail($recipe)) : |
| 789 |
echo '<div class="cooked-post-featured-image">'; |
| 790 |
echo get_the_post_thumbnail( $recipe,'cooked-large' ); |
| 791 |
echo '</div>'; |
| 792 |
endif; |
| 793 |
|
| 794 |
return ob_get_clean(); |
| 795 |
} |
| 796 |
|
| 797 |
public function cooked_ingredients_shortcode($atts, $content = null) { |
| 798 |
global $recipe_settings; |
| 799 |
|
| 800 |
// Shortcode Attributes |
| 801 |
$atts = shortcode_atts([ |
| 802 |
'checkboxes' => true, |
| 803 |
], $atts); |
| 804 |
|
| 805 |
$checkboxes = Cooked_Functions::sanitize_text_field($atts['checkboxes']); |
| 806 |
$checkboxes = !$checkboxes || $checkboxes == 'false' ? false : $checkboxes; |
| 807 |
|
| 808 |
ob_start(); |
| 809 |
|
| 810 |
if ( isset($recipe_settings['ingredients']) && !empty($recipe_settings['ingredients']) ): |
| 811 |
echo '<div class="cooked-recipe-ingredients">'; |
| 812 |
foreach( $recipe_settings['ingredients'] as $ing ): |
| 813 |
|
| 814 |
Cooked_Recipes::single_ingredient( $ing, $checkboxes ); |
| 815 |
|
| 816 |
endforeach; |
| 817 |
echo '</div>'; |
| 818 |
endif; |
| 819 |
|
| 820 |
return ob_get_clean(); |
| 821 |
} |
| 822 |
|
| 823 |
public function cooked_directions_shortcode($atts, $content = null) { |
| 824 |
global $recipe_settings; |
| 825 |
|
| 826 |
// Shortcode Attributes |
| 827 |
$atts = shortcode_atts(apply_filters('cooked_directions_shortcode_atts', [ |
| 828 |
'numbers' => true, |
| 829 |
]), $atts); |
| 830 |
|
| 831 |
$step = 0; |
| 832 |
$numbers = Cooked_Functions::sanitize_text_field( $atts['numbers'] ); |
| 833 |
$numbers = !$numbers || $numbers == 'false' ? false : $numbers; |
| 834 |
|
| 835 |
ob_start(); |
| 836 |
|
| 837 |
if (isset($recipe_settings['directions']) && !empty($recipe_settings['directions'])) { |
| 838 |
echo '<div class="cooked-recipe-directions">'; |
| 839 |
foreach ($recipe_settings['directions'] as $dir) { |
| 840 |
if (!isset($dir['section_heading_name'])) { |
| 841 |
$step++; |
| 842 |
$number = $numbers ? $step : false; |
| 843 |
} else { |
| 844 |
$number = 0; |
| 845 |
} |
| 846 |
|
| 847 |
Cooked_Recipes::single_direction($dir, $number, false, $step, $atts); |
| 848 |
} |
| 849 |
echo '</div>'; |
| 850 |
} |
| 851 |
|
| 852 |
return ob_get_clean(); |
| 853 |
} |
| 854 |
|
| 855 |
public function cooked_nutrition_shortcode($atts, $content = null) { |
| 856 |
global $_cooked_settings, $recipe_settings; |
| 857 |
|
| 858 |
// Shortcode Attributes |
| 859 |
$atts = shortcode_atts([ |
| 860 |
'id' => false, |
| 861 |
'float' => false, |
| 862 |
], $atts); |
| 863 |
|
| 864 |
if ( isset( $atts['id'] ) && $atts['id'] ): |
| 865 |
$recipe_settings = Cooked_Recipes::get( intval( $atts['id'] ), true ); |
| 866 |
else: |
| 867 |
$recipe_settings = !empty($recipe_settings) ? $recipe_settings : false; |
| 868 |
endif; |
| 869 |
|
| 870 |
$float = Cooked_Functions::sanitize_text_field($atts['float']); |
| 871 |
|
| 872 |
$_nf_fields = Cooked_Measurements::nutrition_facts(); |
| 873 |
$nutrition_facts = isset($recipe_settings['nutrition']) && !empty($recipe_settings['nutrition']) ? $recipe_settings['nutrition'] : false; |
| 874 |
|
| 875 |
ob_start(); |
| 876 |
|
| 877 |
if ( $nutrition_facts ): |
| 878 |
|
| 879 |
$servings_change = (float)esc_html( get_query_var( 'servings', $recipe_settings['nutrition']['servings'] ) ); |
| 880 |
|
| 881 |
$top_facts = $_nf_fields['top']; |
| 882 |
if (!empty($top_facts)): |
| 883 |
|
| 884 |
// Start output buffer for top facts. |
| 885 |
ob_start(); |
| 886 |
|
| 887 |
echo '<dt class="cooked-nut-servings">'; |
| 888 |
foreach ( $top_facts as $slug => $nf ): |
| 889 |
if ( $slug === 'serving_size' ): |
| 890 |
echo '<dt class="cooked-serving-size"><strong>' . esc_html($nf['name']) . '</strong> '; |
| 891 |
echo '<p class="cooked-right"><strong class="cooked-nut-label" data-labeltype="' . esc_attr($slug) . '">' . esc_html( isset($nutrition_facts[$slug]) ? $nutrition_facts[$slug] : '' ) . '</strong></p>'; |
| 892 |
echo '</dt>'; |
| 893 |
else: |
| 894 |
echo '<p><strong class="cooked-nut-label" data-labeltype="' . esc_attr( $slug ) . '">' . $servings_change . '</strong> ' . esc_html(strtolower($nf['name'])) . '</p>'; |
| 895 |
endif; |
| 896 |
endforeach; |
| 897 |
echo '</dt>'; |
| 898 |
|
| 899 |
// Get top facts content from buffer. |
| 900 |
$top_facts_content = ob_get_clean(); |
| 901 |
|
| 902 |
endif; |
| 903 |
|
| 904 |
$mid_facts = $_nf_fields['mid']; |
| 905 |
if (!empty($mid_facts)): |
| 906 |
|
| 907 |
// Start output buffer for mid-facts. |
| 908 |
ob_start(); |
| 909 |
|
| 910 |
foreach ( $mid_facts as $slug => $nf ): |
| 911 |
if ( isset( $nutrition_facts[$slug] ) && $nutrition_facts[$slug] || isset( $nutrition_facts[$slug] ) && $nutrition_facts[$slug] === '0' ): |
| 912 |
echo '<dt class="cooked-calories no-after"><strong>' . esc_html($nf['name']) . '</strong>'; |
| 913 |
echo '<strong class="cooked-nut-label">' . esc_html( $nutrition_facts[$slug] ) . '</strong>'; |
| 914 |
echo '</dt>'; |
| 915 |
endif; |
| 916 |
endforeach; |
| 917 |
|
| 918 |
// Get mid facts content from buffer. |
| 919 |
$mid_facts_content = ob_get_clean(); |
| 920 |
|
| 921 |
endif; |
| 922 |
|
| 923 |
$main_facts = $_nf_fields['main']; |
| 924 |
$nut_loops = 0; |
| 925 |
|
| 926 |
if (!empty($main_facts)): |
| 927 |
|
| 928 |
// Start output buffer for main facts. |
| 929 |
ob_start(); |
| 930 |
|
| 931 |
foreach ( $main_facts as $slug => $nf ): |
| 932 |
|
| 933 |
if ( isset( $nutrition_facts[$slug] ) && $nutrition_facts[$slug] || isset( $nutrition_facts[$slug] ) && $nutrition_facts[$slug] === '0' ): |
| 934 |
|
| 935 |
echo '<dt>'; |
| 936 |
echo '<strong>' . $nf['name'] . '</strong> <strong class="cooked-nut-label">' . esc_html( $nutrition_facts[$slug] ) . '</strong>' . ( isset($nf['measurement']) ? '<strong class="cooked-nut-label cooked-nut-measurement">' . esc_html( $nf['measurement'] ) . '</strong>' : '' ); |
| 937 |
echo ( isset( $nf['pdv'] ) && $nutrition_facts[$slug] ? '<strong class="cooked-nut-right"><span class="cooked-nut-percent">' . ceil( ( esc_html( $nutrition_facts[$slug] ) / $nf['pdv'] ) * 100 ) . '</span>%</strong>' : '' ); |
| 938 |
|
| 939 |
if ( isset($nf['subs']) ): |
| 940 |
foreach( $nf['subs'] as $sub_slug => $sub_nf ): |
| 941 |
if ( isset( $nutrition_facts[$sub_slug] ) && $nutrition_facts[$sub_slug] || isset( $nutrition_facts[$sub_slug] ) && $nutrition_facts[$sub_slug] === '0' ): |
| 942 |
echo '<dl>'; |
| 943 |
if ($sub_slug === 'trans_fat'): |
| 944 |
echo '<dt>'; |
| 945 |
echo $sub_nf['nutrition_info_name'] . ' <strong class="cooked-nut-label" data-labeltype="' . esc_attr( $sub_slug ) . '">' . esc_html( $nutrition_facts[$sub_slug] ) . '</strong>' . ( isset($sub_nf['measurement']) ? '<strong class="cooked-nut-label" data-labeltype="' . esc_attr( $sub_slug ) . '_measurement">' . esc_html($sub_nf['measurement']) . '</strong>' : '' ); |
| 946 |
echo '</dt>'; |
| 947 |
elseif ($sub_slug === 'added_sugars'): |
| 948 |
echo '<dl><dt>'; |
| 949 |
echo __('Includes', 'cooked') . ' <strong class="cooked-nut-label" data-labeltype="' . esc_attr( $sub_slug ) . '">' . esc_html( $nutrition_facts[$sub_slug] ) . '</strong>' . ( isset($sub_nf['measurement']) ? '<strong class="cooked-nut-label" data-labeltype="' . esc_attr( $sub_slug ) . '_measurement">' . esc_html($sub_nf['measurement']) . '</strong>' : '' ) . ' ' . esc_html($sub_nf['name']); |
| 950 |
echo ( isset( $sub_nf['pdv'] ) ? '<strong class="cooked-nut-right"><span class="cooked-nut-percent" data-pdv="' . esc_attr($sub_nf['pdv']) . '" data-labeltype="' . esc_attr($sub_slug) . '">' . ceil( ( esc_html( $nutrition_facts[$sub_slug] ) / $sub_nf['pdv'] ) * 100 ) . '</span>%</strong>' : '' ); |
| 951 |
echo '</dt></dl>'; |
| 952 |
else: |
| 953 |
echo '<dt>'; |
| 954 |
echo esc_html( $sub_nf['name'] ) . ' <strong class="cooked-nut-label">' . esc_html( $nutrition_facts[$sub_slug] ) . '</strong>' . ( isset($sub_nf['measurement']) ? '<strong class="cooked-nut-label cooked-nut-measurement">' . esc_html( $sub_nf['measurement'] ) . '</strong>' : '' ); |
| 955 |
echo ( isset( $sub_nf['pdv'] ) && $nutrition_facts[$sub_slug] ? '<strong class="cooked-nut-right"><span class="cooked-nut-percent">' . ceil( ( esc_html( $nutrition_facts[$sub_slug] ) / $sub_nf['pdv'] ) * 100 ) . '</span>%</strong>' : '' ); |
| 956 |
echo '</dt>'; |
| 957 |
endif; |
| 958 |
echo '</dl>'; |
| 959 |
endif; |
| 960 |
endforeach; |
| 961 |
endif; |
| 962 |
|
| 963 |
echo '</dt>'; |
| 964 |
|
| 965 |
endif; |
| 966 |
|
| 967 |
endforeach; |
| 968 |
|
| 969 |
// Get main facts content from buffer. |
| 970 |
$main_facts_content = ob_get_clean(); |
| 971 |
|
| 972 |
endif; |
| 973 |
|
| 974 |
$bottom_facts = $_nf_fields['bottom']; |
| 975 |
|
| 976 |
if (!empty($bottom_facts)): |
| 977 |
|
| 978 |
// Start output buffer for bottom facts. |
| 979 |
ob_start(); |
| 980 |
|
| 981 |
foreach ( $bottom_facts as $slug => $nf ): |
| 982 |
if ( isset( $nutrition_facts[$slug] ) && $nutrition_facts[$slug] || isset( $nutrition_facts[$slug] ) && $nutrition_facts[$slug] === '0' ): |
| 983 |
echo '<dt>'; |
| 984 |
echo '<strong>' . esc_html($nf['name']) . ' <span class="cooked-nut-label" data-labeltype="' . esc_attr( $slug ) . '">' . esc_html( $nutrition_facts[$slug] ) . '</span>' . ( isset($nf['measurement']) ? '<span class="cooked-nut-label" data-labeltype="' . esc_attr( $slug ) . '_measurement">' . esc_html($nf['measurement']) . '</span>' : '' ); |
| 985 |
echo ( isset( $nf['pdv'] ) ? '<strong class="cooked-nut-right"><span class="cooked-nut-percent" data-pdv="' . esc_attr($nf['pdv']) . '" data-labeltype="' . esc_attr($slug) . '">' . ceil( ( esc_html( $nutrition_facts[$slug] ) / $nf['pdv'] ) * 100 ) . '</span>%</strong>' : '' ); |
| 986 |
echo '</dt>'; |
| 987 |
endif; |
| 988 |
endforeach; |
| 989 |
|
| 990 |
// Get bottom facts content from buffer. |
| 991 |
$bottom_facts_content = ob_get_clean(); |
| 992 |
|
| 993 |
endif; |
| 994 |
|
| 995 |
// Start a buffer for all nutrition facts content |
| 996 |
ob_start(); |
| 997 |
|
| 998 |
if ( isset($top_facts_content) && $top_facts_content ): |
| 999 |
echo wp_kses_post( $top_facts_content ); |
| 1000 |
endif; |
| 1001 |
|
| 1002 |
if ( isset($mid_facts_content) && $mid_facts_content || isset($main_facts_content) && $main_facts_content ): |
| 1003 |
|
| 1004 |
echo '<hr class="cooked-nut-hr" />'; |
| 1005 |
echo '<dl>'; |
| 1006 |
|
| 1007 |
echo '<dt><strong class="cooked-nut-heading">' . __('Amount per serving','cooked') . '</strong></dt>'; |
| 1008 |
|
| 1009 |
if ( isset($mid_facts_content) && $mid_facts_content ): |
| 1010 |
echo '<section class="cooked-clearfix">'; |
| 1011 |
echo wp_kses_post( $mid_facts_content ); |
| 1012 |
echo '</section>'; |
| 1013 |
endif; |
| 1014 |
|
| 1015 |
if ( isset($main_facts_content) && $main_facts_content ): |
| 1016 |
echo '<dt class="cooked-nut-spacer"></dt>'; |
| 1017 |
echo '<dt class="cooked-nut-no-border"><strong class="cooked-nut-heading cooked-nut-right">' . __('% Daily Value *','cooked'). '</strong></dt>'; |
| 1018 |
echo '<section class="cooked-clearfix">'; |
| 1019 |
echo wp_kses_post( $main_facts_content ); |
| 1020 |
echo '</section>'; |
| 1021 |
endif; |
| 1022 |
|
| 1023 |
echo '</dl>'; |
| 1024 |
echo '<hr class="cooked-nut-hr" />'; |
| 1025 |
|
| 1026 |
endif; |
| 1027 |
|
| 1028 |
if ( isset($bottom_facts_content) && $bottom_facts_content ): |
| 1029 |
echo '<dl class="cooked-nut-bottom cooked-clearfix">'; |
| 1030 |
echo wp_kses_post( $bottom_facts_content ); |
| 1031 |
echo '</dl>'; |
| 1032 |
endif; |
| 1033 |
|
| 1034 |
$nutrition_facts_content = ob_get_clean(); |
| 1035 |
|
| 1036 |
if ( isset($nutrition_facts_content) && $nutrition_facts_content ): |
| 1037 |
|
| 1038 |
echo '<div class="cooked-nutrition-label' . ( $float ? ' cooked-float-'.esc_attr( $float ) : '' ) . '">'; |
| 1039 |
echo '<div class="cooked-nutrition-title">' . __('Nutrition Facts', 'cooked') . '</div>'; |
| 1040 |
echo wp_kses_post( $nutrition_facts_content ); |
| 1041 |
if ( isset($main_facts_content) && $main_facts_content || isset($bottom_facts_content) && $bottom_facts_content ): |
| 1042 |
echo '<dt class="cooked-nut-spacer"></dt>'; |
| 1043 |
echo '<p class="cooked-daily-value-text">* ' . __('The % Daily Value (DV) tells you how much a nutrient in a serving of food contributes to a daily diet. 2,000 calories a day is used for general nutrition advice.','cooked') . '</p>'; |
| 1044 |
endif; |
| 1045 |
|
| 1046 |
// Add the Edamam attribution "Powered by Edamam" if the Edamam API is enabled. |
| 1047 |
// More Information: https://developer.edamam.com/attribution |
| 1048 |
if (!empty($_cooked_settings['enable_nutrition_api'])) : |
| 1049 |
echo '<div class="cooked-nutrition-edamam">'; |
| 1050 |
do_action( 'cooked_nutrition_facts_powered_by_edamam' ); |
| 1051 |
echo '</div>'; |
| 1052 |
endif; |
| 1053 |
|
| 1054 |
echo '</div>'; |
| 1055 |
|
| 1056 |
endif; |
| 1057 |
|
| 1058 |
endif; |
| 1059 |
|
| 1060 |
return ob_get_clean(); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
|