PluginProbe
Cooked – Recipe Management / 1.9.5
Cooked – Recipe Management v1.9.5
1.16.1 1.16.0 1.15.0 trunk 1.10.0 1.11.0 1.11.1 1.11.2 1.11.3 1.11.4 1.12.0 1.13.0 1.14.0 1.7.10 1.7.11 1.7.12 1.7.13 1.7.15.1 1.7.15.3 1.7.15.4 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 All 37 releases
cooked / includes / class.cooked-recipes.php

class.cooked-recipes.php in Cooked – Recipe Management 1.9.5, at includes/class.cooked-recipes.php

1,309 lines 64.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cooked Recipe-Specific Functions
4 *
5 * @package Cooked
6 * @subpackage Recipe-Specific Functions
7 * @since 1.0.0
8 */
9
10 // Exit if accessed directly
11 if ( ! defined( 'ABSPATH' ) ) exit;
12
13 /**
14 * Cooked_Recipe_Meta Class
15 *
16 * This class handles the Cooked Recipe Meta Box creation.
17 *
18 * @since 1.0.0
19 */
20 class Cooked_Recipes {
21
22 public function __construct() {
23 add_filter( 'cooked_recipe_content_filter', [&$this, 'vendor_checks'], 1, 1 );
24 add_filter( 'the_content', [&$this, 'recipe_template'], 10 );
25
26 add_filter( 'parse_query', [&$this, 'custom_taxonomy_in_query'], 10 );
27
28 add_action( 'template_redirect', [&$this, 'print_recipe_template'], 10 );
29 add_action( 'cooked_check_recipe_query', [&$this, 'check_recipe_query'], 10 );
30 add_action( 'pre_get_posts', [&$this, 'cooked_pre_get_posts'], 10, 1 );
31 add_action( 'restrict_manage_posts', [&$this, 'filter_recipes_by_taxonomy'], 10 );
32
33 add_filter('get_canonical_url', [&$this, 'modify_browse_page_canonical_url'], 20, 2);
34 }
35
36 public static function get( $args = false, $single = false, $ids_only = false, $limit = false ) {
37 $recipes = [];
38 $counter = 0;
39
40 // Get by Recipe ID
41 if ( $args && !is_array($args) ):
42
43 $recipe_id = $args;
44 $args = [
45 'post_type' => 'cp_recipe',
46 'post__in' => [$recipe_id],
47 'post_status' => 'publish'
48 ];
49
50 // Default Query
51 elseif ( !$args || !is_array($args) ):
52
53 $args = [
54 'post_type' => 'cp_recipe',
55 'posts_per_page' => -1,
56 'post_status' => 'publish',
57 'orderby' => 'name',
58 'order' => 'ASC'
59 ];
60
61 if ( $ids_only ):
62 $args['fields'] = 'ids';
63 endif;
64
65 if ( $limit ):
66 $args['limit'] = $limit;
67 endif;
68
69 // Search Query
70 elseif ( $args && isset($args['s']) && isset($args['meta_query']) ):
71
72 $meta_query = $args['meta_query'];
73 $recipe_ids = [];
74
75 $pre_search_args = $args;
76 $pre_search_args['posts_per_page'] = -1;
77 $pre_search_args['fields'] = 'ids';
78
79 unset($pre_search_args['meta_query']);
80
81 $recipes_pre_search = new WP_Query($pre_search_args);
82 if ( $recipes_pre_search->have_posts() ):
83 $recipe_ids = $recipes_pre_search->posts;
84 endif;
85
86 $pre_search_args['meta_query'] = $meta_query;
87 unset($pre_search_args['s']);
88
89 $recipes_pre_search = new WP_Query($pre_search_args);
90 if ( $recipes_pre_search->have_posts() ):
91 $rposts = $recipes_pre_search->posts;
92 $recipe_ids = ( !empty($recipe_ids) ? array_merge( $recipe_ids, $rposts ) : $rposts );
93 endif;
94
95 $recipe_ids = array_unique( $recipe_ids );
96
97 if ( !empty($recipe_ids) ):
98 unset($args['s']);
99 unset($args['meta_query']);
100 $args['post__in'] = $recipe_ids;
101 endif;
102
103 endif;
104
105 $recipes_results = new WP_Query($args);
106
107 if ( $recipes_results->have_posts() ):
108 if ( $ids_only ):
109 return $recipes_results->posts;
110 else:
111 while ( $recipes_results->have_posts() ):
112 $recipes_results->the_post();
113 $recipes[$counter]['id'] = $recipes_results->post->ID;
114 $recipes[$counter]['title'] = $recipes_results->post->post_title;
115 $recipe_settings = self::get_settings($recipes_results->post->ID);
116 foreach($recipe_settings as $key => $setting):
117 $recipes[$counter][$key] = $setting;
118 endforeach;
119 $counter++;
120 endwhile;
121 endif;
122 else:
123 wp_reset_postdata();
124 return;
125 endif;
126
127 $recipes['raw'] = $recipes_results;
128
129 if ( $single && isset( $recipes[0] ) ):
130 $recipes = $recipes[0];
131 endif;
132
133 wp_reset_postdata();
134
135 return $recipes;
136 }
137
138 public static function get_settings( $post_id, $bc = true ) {
139 if ( !$post_id ) return;
140
141 $recipe_settings = get_post_meta( $post_id, '_recipe_settings', true );
142
143 if ( !is_array( $recipe_settings ) || empty($recipe_settings) ) {
144 $recipe_settings = [];
145 }
146
147 $recipe_settings['title'] = get_the_title( $post_id );
148
149 $recipe_post = get_post($post_id);
150 $wp_excerpt = $recipe_post->post_excerpt;
151
152 // Check for excerpt/content
153 // if ( isset($recipe_settings['excerpt']) && !$recipe_settings['excerpt'] && !$wp_excerpt || !isset($recipe_settings['excerpt']) ) {
154 // wp_update_post(['ID' => $post_id, 'post_excerpt' => $recipe_settings['title']] );
155 // }
156
157 // Check for nutrition data
158 if ( !isset($recipe_settings['nutrition']) ):
159 $recipe_settings['nutrition'] = [];
160 $recipe_settings['nutrition']['servings'] = 1;
161 endif;
162
163 // Backwards Compatibility with Cooked 2.x
164 if ( !isset($recipe_settings['cooked_version']) && $bc ):
165 $c2_recipe_settings = self::get_c2_recipe_meta( $post_id );
166 $recipe_settings = self::sync_c2_recipe_settings( $c2_recipe_settings, $post_id );
167 endif;
168
169 // Get the author information
170 $recipe_settings['author'] = Cooked_Users::get( $recipe_post->post_author, true );
171
172 // Include the Post ID
173 $recipe_settings['id'] = $post_id;
174
175 // You're welcome developers!
176 $recipe_settings = apply_filters( 'cooked_single_recipe_settings', $recipe_settings, $post_id );
177
178 return $recipe_settings;
179 }
180
181 public function check_recipe_query() {
182 global $_cooked_settings, $recipe_query;
183
184 if ( !isset($recipe_query['cp_recipe_category']) ):
185 $recipe_query['cp_recipe_category'] = ( isset($_GET['cp_recipe_category']) && $_GET['cp_recipe_category'] ? intval($_GET['cp_recipe_category']) : ( isset($_cooked_settings['browse_default_cp_recipe_category']) && $_cooked_settings['browse_default_cp_recipe_category'] ? $_cooked_settings['browse_default_cp_recipe_category'] : false ) );
186 endif;
187 }
188
189 public static function cooked_pre_get_posts( $q ) {
190 if ( $title = $q->get( '_cooked_title' ) ):
191 add_filter( 'get_meta_sql', function( $sql ) use ( $title ) {
192
193 global $wpdb, $cooked_modified_where;
194
195 if ( $cooked_modified_where ) return $sql;
196 $cooked_modified_where = 1;
197
198 // Modified WHERE
199 $sql['where'] = sprintf(
200 " AND ( %s OR %s ) ",
201 apply_filters( 'cooked_query_where_filter', $wpdb->prepare( "{$wpdb->posts}.post_title like '%%%s%%'", $wpdb->esc_like( $title ) ) ),
202 mb_substr( $sql['where'], 5, mb_strlen( $sql['where'] ) )
203 );
204
205 return $sql;
206 });
207 endif;
208 }
209
210 public static function recipe_list( $orderby = 'date', $show = 5, $recipes = false, $width = false, $hide_image = false, $hide_author = false ) {
211 global $_cooked_settings;
212
213 $width = !$width ? '100%' : $width;
214 $pixel_width = stristr( $width, 'px', true );
215 $percent_width = stristr( $width, '%', true );
216 $width = $pixel_width ? $pixel_width . 'px' : ( $percent_width ? $percent_width . '%' : ( is_numeric( $width ) ? $width . 'px' : '100%' ) );
217
218 $args = [
219 'post_type' => 'cp_recipe',
220 'posts_per_page' => $show,
221 'post_status' => 'publish',
222 'orderby' => $orderby,
223 'order' => 'DESC'
224 ];
225
226 if ( !empty($recipes) ) {
227 $args['posts_per_page'] = -1;
228 $args['orderby'] = 'post__in';
229 $args['order'] = 'ASC';
230 $args['post__in'] = $recipes;
231 }
232
233 // Filter out the pending/draft recipes.
234 $args = apply_filters( 'cooked_recipe_public_query_filters', $args );
235
236 $recipes = Cooked_Recipes::get( $args );
237
238 if ( isset($recipes['raw']) ): unset( $recipes['raw'] ); endif;
239 $recipe_list = [];
240
241 if ( !empty($recipes) ):
242
243 echo '<div class="cooked-shortcode-recipe-list">';
244
245 foreach ( $recipes as $key => $recipe ):
246
247 $rid = $recipe['id'];
248 $has_image_class = has_post_thumbnail($rid) && !$hide_image ? ' cooked-srl-has-image' : '';
249
250 echo '<div class="cooked-srl-single' . esc_attr( $has_image_class ) . '" style="width:100%; max-width:' . esc_attr( $width ) . '">';
251
252 echo has_post_thumbnail($rid) && !$hide_image ? '<div class="cooked-srl-image"><a href="' . esc_url( get_permalink($rid) ) . '">' . get_the_post_thumbnail( $rid, 'thumbnail' ) . '</a></div>' : '';
253
254 echo '<div class="cooked-srl-content">';
255
256 echo '<div class="cooked-srl-title"><a href="' . esc_url( get_permalink($rid) ) . '">' . wp_kses_post( $recipe['title'] ) . '</a></div>';
257
258 if ( in_array('author', $_cooked_settings['recipe_info_display_options']) && !$hide_author ):
259 echo '<div class="cooked-srl-author">';
260 $author = $recipe['author'];
261 /* translators: stating the recipe author with a "By" in front of it. (ex: "By John Smith") */
262 echo sprintf( __( 'By %s', 'cooked' ), '<strong>' . wp_kses_post( $author['name'] ) . '</strong>' );
263 echo '</div>';
264 endif;
265
266 echo '</div>';
267
268 echo '</div>';
269 endforeach;
270
271 echo '</div>';
272
273 endif;
274
275 }
276
277 public static function card( $rid, $width = false, $hide_image = false, $hide_title = false, $hide_excerpt = false, $hide_author = false, $style = false ) {
278 global $_cooked_settings;
279
280 $recipe = self::get( $rid, true );
281 $style_class = $style ? ' cooked-recipe-card-' . esc_attr($style) : '';
282
283 $width = !$width ? '100%' : $width;
284 $pixel_width = stristr( $width, 'px', true );
285 $percent_width = stristr( $width, '%', true );
286 $width = $pixel_width ? $pixel_width . 'px' : ( $percent_width ? $percent_width . '%' : ( is_numeric( $width ) ? $width . 'px' : '100%' ) );
287
288 ob_start();
289
290 echo '<a href="' . esc_url( get_permalink($rid) ) . '" class="cooked-recipe-card' . esc_attr( $style_class ) . '" style="width:100%; max-width:' . esc_attr( $width ) . '">';
291
292 do_action( 'cooked_recipe_grid_before_recipe', $recipe );
293
294 do_action( 'cooked_recipe_grid_before_image', $recipe );
295
296 echo has_post_thumbnail($rid) && !$hide_image ? '<span class="cooked-recipe-card-image" style="background-image:url(' . get_the_post_thumbnail_url( $recipe['id'], 'cooked-medium' ) . ');"></span>' : '';
297
298 //do_action( 'cooked_recipe_grid_after_image', $recipe );
299
300 echo '<span class="cooked-recipe-card-content">';
301
302 do_action( 'cooked_recipe_grid_before_name', $recipe );
303
304 echo !$hide_title ? '<span class="cooked-recipe-card-title">' . esc_html( $recipe['title'] ) . '</span>' : '';
305
306 do_action( 'cooked_recipe_grid_after_name', $recipe );
307
308 if ( $hide_excerpt && !$hide_title && !$hide_author ): echo '<span class="cooked-recipe-card-sep"></span>'; endif;
309
310 do_action( 'cooked_recipe_grid_before_author', $recipe );
311
312 if ( in_array('author',$_cooked_settings['recipe_info_display_options']) && !$hide_author ):
313 echo '<span class="cooked-recipe-card-author">';
314 $author = $recipe['author'];
315 /* translators: stating the recipe author with a "By" in front of it. (ex: "By John Smith") */
316 echo sprintf( __( 'By %s', 'cooked' ), '<strong>' . wp_kses_post( $author['name'] ) . '</strong>' );
317 echo '</span>';
318 endif;
319
320 do_action( 'cooked_recipe_grid_after_author', $recipe );
321
322 if ( !$hide_excerpt && !$hide_title || !$hide_excerpt && !$hide_author ): echo '<span class="cooked-recipe-card-sep"></span>'; endif;
323
324 do_action( 'cooked_recipe_grid_before_excerpt', $recipe );
325
326 echo !$hide_excerpt ? '<span class="cooked-recipe-card-excerpt">' . wp_kses_post( $recipe['excerpt'] ) . '</span>' : '';
327
328 do_action( 'cooked_recipe_grid_after_excerpt', $recipe );
329
330 echo '</span>';
331
332 do_action( 'cooked_recipe_grid_after_recipe', $recipe );
333
334 echo '</a>';
335
336 return ob_get_clean();
337 }
338
339 public function print_recipe_template() {
340 if ( is_singular('cp_recipe') && isset($_GET['print']) ):
341 load_template( COOKED_DIR . 'templates/front/recipe-print.php', false);
342 exit;
343 endif;
344 }
345
346 public static function vendor_checks( $content ) {
347 global $wp_query, $post;
348
349 // WooCommerce Memberships
350 if ( function_exists('wc_memberships_user_can') && function_exists('wc_memberships_is_post_content_restricted') && function_exists('wc_memberships_user_can') ):
351
352 if ( !is_user_logged_in() && wc_memberships_is_post_content_restricted() ):
353 return WC_Memberships_User_Messages::get_message_html( 'content_restricted', ['post' => $post] );
354 elseif ( is_user_logged_in() ):
355 $_user = wp_get_current_user();
356 if ( !wc_memberships_user_can( $_user->ID, 'view', ['cp_recipe' => $post->ID] ) ):
357 return WC_Memberships_User_Messages::get_message_html( 'content_restricted', ['post' => $post] );
358 endif;
359 endif;
360
361 endif;
362
363 return $content;
364 }
365
366 public function filter_recipes_by_taxonomy() {
367 global $typenow, $cooked_taxonomies_shown;
368 $taxonomies = apply_filters( 'cooked_active_taxonomies', ['cp_recipe_category'] );
369 if ( $typenow == 'cp_recipe' ):
370 foreach ( $taxonomies as $taxonomy ):
371 if ( is_array($cooked_taxonomies_shown) && !in_array( $taxonomy, $cooked_taxonomies_shown ) || !is_array($cooked_taxonomies_shown) ):
372 $cooked_taxonomies_shown[] = $taxonomy;
373 $selected = isset($_GET[$taxonomy]) ? sanitize_title($_GET[$taxonomy]) : '';
374 $info_taxonomy = get_taxonomy($taxonomy);
375 $taxonomy_label = $info_taxonomy->label;
376
377 /* translators: For showing "All" of a taxonomy (ex: "All Burgers") */
378 $all_string = sprintf( __( "All %s", "cooked" ), $taxonomy_label );
379
380 wp_dropdown_categories([
381 'show_option_all' => $all_string,
382 'taxonomy' => $taxonomy,
383 'name' => $taxonomy,
384 'orderby' => 'name',
385 'selected' => $selected,
386 'show_count' => true,
387 'hide_empty' => true,
388 ]);
389 endif;
390 endforeach;
391 endif;
392 }
393
394 public function custom_taxonomy_in_query($query) {
395 global $pagenow;
396 $taxonomies = apply_filters( 'cooked_active_taxonomies', ['cp_recipe_category'] );
397 $q_vars = &$query->query_vars;
398
399 foreach ( $taxonomies as $taxonomy ):
400 if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'cp_recipe' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ):
401 $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
402 $q_vars[$taxonomy] = $term->slug;
403 endif;
404 endforeach;
405 }
406
407 public static function list_view( $list_atts = false ) {
408 global $wp_query, $recipe_query, $atts, $_cooked_settings, $recipes, $recipe_args, $current_recipe_page;
409
410 // Get the attributes for this view
411 $atts = array_change_key_case( (array) $list_atts, CASE_LOWER );;
412 $ls_method = 'list_style_grid';
413 $ls_class = 'Cooked_Recipes';
414
415 // Change the recipe layout
416 if ( $atts['layout'] ) {
417 $recipe_list_style = apply_filters( 'cooked_recipe_list_style', [ 'grid' => 'Cooked_Recipes' ], $atts['layout'] );
418 $list_style = esc_html( key( $recipe_list_style ) );
419 $ls_method = 'list_style_' . $list_style;
420 $ls_class = current( $recipe_list_style );
421
422 $_cooked_settings['recipe_list_style'] = $list_style;
423 }
424
425 $recipe_query = $wp_query->query;
426 $tax_query = [];
427
428 do_action( 'cooked_check_recipe_query' );
429
430 if ( isset($_cooked_settings['recipe_taxonomies']) && !empty($_cooked_settings['recipe_taxonomies']) ):
431 foreach ( $_cooked_settings['recipe_taxonomies'] as $taxonomy ):
432 if ( isset($recipe_query[$taxonomy]) && $recipe_query[$taxonomy] ):
433 $field_type = is_numeric($recipe_query[$taxonomy]) ? 'id' : 'slug';
434 $tax_query['relation'] = 'AND';
435 $tax_query[] = [
436 'taxonomy' => $taxonomy,
437 'field' => $field_type,
438 'terms' => array_map('trim', explode(',', esc_html($recipe_query[$taxonomy])))
439 ];
440 endif;
441 endforeach;
442
443 if ( empty($tax_query) ):
444 foreach ( $_cooked_settings['recipe_taxonomies'] as $taxonomy ):
445 if ( isset( $_cooked_settings['browse_default_' . $taxonomy] ) && $_cooked_settings['browse_default_' . $taxonomy] ):
446 $tax_query['relation'] = 'AND';
447 $tax_query[] = [
448 'taxonomy' => $taxonomy,
449 'field' => 'id',
450 'terms' => [esc_html($_cooked_settings['browse_default_' . $taxonomy])]
451 ];
452 endif;
453 endforeach;
454 endif;
455 endif;
456
457 if ( empty($tax_query) ):
458
459 if ( $atts['category'] ):
460 $tax_query['relation'] = 'AND';
461 $tax_query[] = [
462 'taxonomy' => 'cp_recipe_category',
463 'field' => (is_numeric($atts['category']) ? 'id' : 'slug'),
464 'terms' => array_map('trim', explode(',', esc_html($atts['category'])))
465 ];
466 endif;
467
468 $tax_query = apply_filters( 'cooked_tax_query_filter', $tax_query, $atts );
469
470 endif;
471
472 $sorting_type = get_query_var('cooked_browse_sort_by', isset($_cooked_settings['browse_default_sort']) && $_cooked_settings['browse_default_sort'] ? $_cooked_settings['browse_default_sort'] : 'date_desc' );
473 $sorting_type = sanitize_key($sorting_type);
474 $sorting_types = explode('_', $sorting_type);
475
476 $text_search = get_query_var('cooked_search_s', '');
477 $text_search = esc_html($text_search);
478 $recipes_per_page = ( $atts['show'] ? $atts['show'] : ( isset($_cooked_settings['recipes_per_page']) && $_cooked_settings['recipes_per_page'] ? $_cooked_settings['recipes_per_page'] : get_option( 'posts_per_page' ) ) );
479 $current_recipe_page = Cooked_Recipes::current_page();
480
481 $orderby = $atts['orderby'] ? esc_html( $atts['orderby'] ) : $sorting_types[0];
482 $meta_sort = false;
483
484 $recipe_args = [
485 'paged' => $current_recipe_page,
486 'post_type' => 'cp_recipe',
487 'posts_per_page' => $recipes_per_page,
488 'post_status' => 'publish',
489 'orderby' => $orderby,
490 'order' => ($atts['order'] ? esc_html($atts['order']) : $sorting_types[1])
491 ];
492
493 if ( isset($atts['exclude']) && $atts['exclude'] ):
494 $exclude = explode( ',', str_replace( ' ', '', $atts['exclude'] ) );
495 $recipe_args['post__not_in'] = $exclude;
496 endif;
497
498 if ( $text_search ):
499 // Replace [+] [,] [;] with spaces
500 $prep_text = str_replace(['+', ',', ';'], ' ', $text_search);
501
502 // Replace duplicate spaces
503 $prep_text = preg_replace('/\s+/', ' ', $prep_text);
504
505 // Explode into an array of search terms
506 $words = explode( ' ', $prep_text );
507
508 if ( !empty($words) ):
509 $meta_query['relation'] = 'AND';
510 foreach ( $words as $word ):
511 $meta_query[] = [
512 'key' => '_recipe_settings',
513 'value' => $word,
514 'compare' => 'LIKE'
515 ];
516 endforeach;
517 else:
518 $meta_query[] = [
519 'key' => '_recipe_settings',
520 'value' => $text_search,
521 'compare' => 'LIKE'
522 ];
523 endif;
524 $recipe_args['s'] = $prep_text;
525 $recipe_args['meta_query'] = $meta_query;
526
527 endif;
528
529 if ( !empty($tax_query) ):
530 $recipe_args['tax_query'] = $tax_query;
531 endif;
532
533 if ( $atts['author'] && is_numeric( $atts['author'] ) ):
534 $recipe_args['author'] = $atts['author'];
535 elseif ( $atts['author'] ):
536 $recipe_args['author_name'] = $atts['author'];
537 endif;
538
539 if ( isset( $atts['include'] ) && !empty($atts['include']) ):
540 $recipe_args['post__in'] = $atts['include'];
541 endif;
542
543 $recipe_args = apply_filters( 'cooked_recipe_query_args', $recipe_args, $atts, $sorting_types );
544 if ( !isset($atts['public_recipes']) || isset($atts['public_recipes']) && $atts['public_recipes'] ):
545 $recipe_args = apply_filters( 'cooked_recipe_public_query_filters', $recipe_args );
546 endif;
547
548 wp_suspend_cache_addition(true);
549
550 ob_start();
551 $recipes = Cooked_Recipes::get( $recipe_args );
552 ( $theme_template_override = locate_template( 'recipe-list.php')) ? load_template( $theme_template_override, false ) : load_template( COOKED_DIR . 'templates/front/recipe-list.php', false );
553 wp_reset_postdata();
554 Cooked_Settings::reset();
555
556 wp_suspend_cache_addition(false);
557
558 return ob_get_clean();
559 }
560
561 public static function list_style_grid($atts = []) {
562 load_template(COOKED_DIR . 'templates/front/recipe-single.php', false, $atts);
563 }
564
565 public static function current_page() {
566 return get_query_var( 'paged' ) ? max( 1, get_query_var('paged') ) : ( get_query_var( 'page' ) ? max( 1, get_query_var('page') ) : 1 );
567 }
568
569 public static function pagination( $recipe_query, $recipe_args ) {
570 global $_cooked_settings, $current_recipe_page, $paged, $atts;
571
572 $paged = self::current_page();
573 $total_recipe_pages = $recipe_query->max_num_pages;
574 $pagination = '';
575
576 if ( $total_recipe_pages > 1) {
577 do_action( 'cooked_init_pagination', $recipe_args, $current_recipe_page, $total_recipe_pages, $atts );
578
579 $pagination_style = apply_filters( 'cooked_pagination_style', ['numbered_pagination' => 'Cooked_Recipes'] );
580 $p_method = key( $pagination_style );
581 $p_class = current( $pagination_style );
582
583 $pagination = $p_class::$p_method( $current_recipe_page, $total_recipe_pages );
584 }
585
586 return $pagination;
587 }
588
589 public static function numbered_pagination( $current_recipe_page, $total_recipe_pages ) {
590 if ( get_option('permalink_structure') ) {
591 $format = '?paged=%#%';
592 } else {
593 $format = 'page/%#%/';
594 }
595
596 $big = 999999999;
597 $base = $format =='?paged=%#%' ? $base = str_replace( $big, '%#%', get_pagenum_link( $big ) ) : $base = @add_query_arg('paged','%#%');
598
599 $recipe_pagination = apply_filters( 'cooked_pagination_args', [
600 'base' => $base,
601 'format' => $format,
602 'mid-size' => 1,
603 'current' => $current_recipe_page,
604 'total' => $total_recipe_pages,
605 'prev_next' => true,
606 'prev_text' => '<i class="cooked-icon cooked-icon-angle-left"></i>',
607 'next_text' => '<i class="cooked-icon cooked-icon-angle-right"></i>',
608 ]);
609 return '<div class="cooked-pagination-numbered cooked-clearfix">' . paginate_links( $recipe_pagination ) . '</div>';
610 }
611
612 public static function default_content() {
613 return apply_filters( 'cooked_default_content', '<p>[cooked-info left="author,taxonomies,difficulty" right="print,fullscreen"]</p><p>[cooked-excerpt]</p><p>[cooked-image]</p><p>[cooked-info left="servings" right="prep_time,cook_time,total_time"]</p><p>[cooked-ingredients]</p><p>[cooked-directions]</p><p>[cooked-notes show_header=true]</p><p>[cooked-gallery]</p>' );
614 }
615
616 public static function print_content() {
617 return apply_filters( 'cooked_print_content', '<p>[cooked-info include="servings,prep_time,cook_time,total_time"]</p><p>[cooked-excerpt]</p><p>[cooked-image]</p><p>[cooked-ingredients]</p><p>[cooked-directions]</p><p>[cooked-notes show_header=true]</p><p>[cooked-nutrition]</p>' );
618 }
619
620 public static function fsm_content() {
621 return apply_filters( 'cooked_fsm_content', '
622 <div class="cooked-fsm-ingredients cooked-fsm-content cooked-active">
623 <div class="cooked-panel"><h2>' . __('Ingredients', 'cooked') . '</h2>[cooked-ingredients]</div>
624 </div>
625 <div class="cooked-fsm-directions-wrap cooked-fsm-content">
626 <div class="cooked-fsm-directions cooked-fsm-content">
627 <div class="cooked-panel"><h2>' . __('Directions', 'cooked') . '</h2>[cooked-directions]</div>
628 </div>
629 <div class="cooked-fsm-notes cooked-fsm-content">
630 <div class="cooked-panel"><h2>' . __('Notes', 'cooked') . '</h2>[cooked-notes]</div>
631 </div>
632 </div>
633 ' );
634 }
635
636 public static function difficulty_levels() {
637 return apply_filters( 'cooked_difficulty_levels', [
638 1 => __('Beginner', 'cooked'),
639 2 => __('Intermediate', 'cooked'),
640 3 => __('Advanced', 'cooked')
641 ]);
642 }
643
644 public static function get_by_slug($slug = false) {
645 if ($slug):
646 if (!function_exists('ctype_digit') || function_exists('ctype_digit') && !ctype_digit($slug)):
647 $recipe_query = new WP_Query(['name' => $slug, 'post_type' => 'cp_recipe'] );
648 if ($recipe_query->have_posts()):
649 $recipe_query->the_post();
650 return get_the_ID();
651 else:
652 return false;
653 endif;
654 else:
655 return $slug;
656 endif;
657 else:
658 return false;
659 endif;
660 }
661
662 public static function gallery_types() {
663
664 $gallery_types = apply_filters( 'cooked_gallery_types', [
665 'cooked' => [
666 'title' => __('Cooked Gallery', 'cooked'),
667 'required_class' => ''
668 ],
669 'envira' => [
670 'title' => __('Envira Gallery', 'cooked'),
671 'required_class' => 'Envira_Gallery'
672 ],
673 'soliloquy' => [
674 'title' => __('Soliloquy Slider', 'cooked'),
675 'required_class' => 'Soliloquy'
676 ],
677 'revslider' => [
678 'title' => __('Slider Revolution', 'cooked'),
679 'required_class' => 'RevSlider'
680 ]
681 ]);
682
683 foreach ( $gallery_types as $slug => $gtype ):
684
685 $results = [];
686
687 if ( $gtype['required_class'] && class_exists($gtype['required_class']) ):
688
689 if ( $slug == 'revslider' ):
690
691 $slider = new RevSlider();
692 $arrSliders = $slider->getArrSliders();
693 if (!empty($arrSliders)):
694 foreach($arrSliders as $slider):
695 $results[ $slider->getAlias() ] = $slider->getTitle();
696 endforeach;
697 endif;
698
699 else:
700
701 $args = apply_filters( 'cooked_gallery_type_' . $slug . '_query', [
702 'post_type' => $slug,
703 'post_status' => 'publish',
704 'posts_per_page' => -1
705 ]);
706 $gallery_query = new WP_Query( $args );
707 if ( $gallery_query->have_posts() ):
708 while( $gallery_query->have_posts() ):
709 $gallery_query->the_post();
710 $results[$gallery_query->post->ID] = get_the_title();
711 endwhile;
712 endif;
713
714 endif;
715
716 if ( !empty($results) ):
717 $gallery_types[$slug]['posts'] = $results;
718 else:
719 unset( $gallery_types[$slug] );
720 endif;
721
722 else:
723
724 if ( $slug != 'cooked' ): unset( $gallery_types[$slug] ); endif;
725
726 endif;
727
728 endforeach;
729
730 wp_reset_query();
731 wp_reset_postdata();
732
733 return $gallery_types;
734 }
735
736 public static function serving_size_switcher( $servings ) {
737 global $_cooked_settings, $post;
738 $switcher_disabled = ( isset( $_cooked_settings['advanced'] ) && in_array( 'disable_servings_switcher', $_cooked_settings['advanced'] ) ? true : false );
739 $printing = ( is_singular('cp_recipe') && isset($_GET['print']) );
740
741 if ( !$printing && !$switcher_disabled ):
742 $default = $servings;
743 $servings = (float)esc_html( get_query_var( 'servings', $servings ) );
744 $servings = ( !$servings ? $default : $servings );
745 $counter = 1;
746
747 $quarter = $default / 4;
748 $half = $default / 2;
749 $double = $default * 2;
750 $triple = $default * 3;
751
752 /* translators: singular and plural quarter "serving" size */
753 $quarter_string = sprintf( esc_html( _n('Quarter (%s Serving)','Quarter (%s Servings)',$quarter,'cooked')),$quarter );
754
755 /* translators: singular and plural quarter "serving" size */
756 $half_string = sprintf( esc_html( _n('Half (%s Serving)','Half (%s Servings)',$half,'cooked')),$half );
757
758 /* translators: singular and plural quarter "serving" size */
759 $default_string = sprintf( esc_html( _n('Default (%s Serving)','Default (%s Servings)',$default,'cooked')),$default );
760
761 /* translators: singular and plural quarter "serving" size */
762 $double_string = sprintf( __( 'Double (%s Servings)','cooked'),$double );
763
764 /* translators: singular and plural quarter "serving" size */
765 $triple_string = sprintf( __( 'Triple (%s Servings)','cooked'),$triple );
766
767 $servings_array = apply_filters( 'cooked_servings_switcher_options', [
768 'quarter' => ['name' => $quarter_string, 'value' => $quarter],
769 'half' => ['name' => $half_string, 'value' => $half],
770 'default' => ['name' => $default_string, 'value' => $default],
771 'double' => ['name' => $double_string, 'value' => $double],
772 'triple' => ['name' => $triple_string, 'value' => $triple],
773 ], $quarter,$half,$default,$double,$triple );
774 else:
775 $servings_array = [];
776 endif;
777
778 echo '<span class="cooked-servings"><span class="cooked-servings-icon"><i class="cooked-icon cooked-icon-recipe-icon"></i></span>';
779 echo '<strong class="cooked-meta-title">' . __('Yields','cooked') . '</strong>';
780 if ( !$printing && !$switcher_disabled ):
781
782 /* translators: singular and plural "serving" sizes */
783 $servings_string = sprintf( esc_html( _n( '%s Serving', '%s Servings', $servings, 'cooked' ) ), $servings );
784
785 echo '<a href="#">' . $servings_string . '</a>';
786 echo '<select name="servings" class="cooked-servings-changer">';
787 foreach ( $servings_array as $stype ):
788 echo '<option value="' . $stype['value'] . '"' . ( $stype['value'] == $servings ? ' selected' : '' ) . '>' . esc_attr( $stype['name'] ) . '</option>';
789 endforeach;
790 echo '</select>';
791 else:
792 /* translators: singular and plural "serving" sizes */
793 echo '<span>' . sprintf( esc_html( _n( '%s Serving', '%s Servings', $servings, 'cooked' ) ), $servings ) . '</span>';
794 endif;
795 echo '</span>';
796
797 }
798
799 public static function single_ingredient( $ing, $checkboxes = true, $plain_text = false ) {
800
801 global $recipe_settings;
802
803 $Cooked_Measurements = new Cooked_Measurements();
804 $measurements = $Cooked_Measurements->get();
805
806 ob_start();
807
808 if ( isset($ing['section_heading_name']) && $ing['section_heading_name'] ):
809
810 if ( $plain_text ):
811 return $ing['section_heading_name'];
812 else:
813 echo '<div class="cooked-single-ingredient cooked-heading">' . esc_html($ing['section_heading_name']) . '</div>';
814 endif;
815
816 elseif ( isset($ing['name']) && $ing['name'] ):
817
818 $default_serving_size = ( isset($recipe_settings['nutrition']['servings']) && $recipe_settings['nutrition']['servings'] ? $recipe_settings['nutrition']['servings'] : 1 );
819 $multiplier = (float)esc_html( get_query_var( 'servings', $recipe_settings['nutrition']['servings'] ) );
820 $multiplier = ( !$multiplier ? $default_serving_size : $multiplier );
821
822 if ( !$multiplier || $multiplier == $default_serving_size ):
823 $multiplier = 1;
824 else:
825 $multiplier = $multiplier / $default_serving_size;
826 endif;
827
828 if ($multiplier === 1):
829 $amount = ( isset($ing['amount']) && $ing['amount'] ? esc_html( $ing['amount'] ) : false );
830 $amount = $Cooked_Measurements->cleanup_amount($amount);
831 $format = ( strpos($amount, '/') === false ? ( strpos($amount, '.') !== false || strpos($amount, ',') !== false ? 'decimal' : 'fraction' ) : 'fraction' );
832 $float_amount = $Cooked_Measurements->calculate( $amount, 'decimal' );
833 $amount = $Cooked_Measurements->format_amount( $float_amount, $format );
834 else:
835 $amount = ( isset($ing['amount']) && $ing['amount'] ? esc_html( $ing['amount'] ) : false );
836 $amount = $Cooked_Measurements->cleanup_amount($amount);
837 $format = ( strpos($amount, '/') === false ? ( strpos($amount, '.') !== false || strpos($amount, ',') !== false ? 'decimal' : 'fraction' ) : 'fraction' );
838 $float_amount = $Cooked_Measurements->calculate( $amount, 'decimal' );
839 if ($float_amount):
840 $float_amount = $float_amount * $multiplier;
841 $amount = $Cooked_Measurements->format_amount( $float_amount, $format );
842 endif;
843 endif;
844
845 $measurement = ( isset($ing['measurement']) && $ing['measurement'] ? esc_html( $ing['measurement'] ) : false );
846 $measurement = ( $measurement && $float_amount ? $Cooked_Measurements->singular_plural( $measurements[ $measurement ]['singular_abbr'], $measurements[ $measurement ]['plural_abbr'], $float_amount ) : false );
847
848 $name = ( isset($ing['name']) && $ing['name'] ? apply_filters( 'cooked_ingredient_name', wp_kses_post( $ing['name'] ), $ing ) : false );
849
850 if ( $plain_text ):
851 return ( $amount ? $amount . ' ' : '' ) . ( $measurement ? $measurement . ' ' : '' ) . ( $name ? $name : '' );
852 else:
853 echo '<div itemprop="recipeIngredient" class="cooked-single-ingredient cooked-ingredient' . ( !$checkboxes ? ' cooked-ing-no-checkbox' : '' ) . '">';
854 echo ( $checkboxes ? '<span class="cooked-ingredient-checkbox">&nbsp;</span>' : '' );
855 do_action( 'cooked_ingredient_after_checkbox', $ing );
856 echo ( $amount ? '<span class="cooked-ing-amount" data-decimal="' . esc_html($float_amount) . '">' . wp_kses_post($amount) . '</span> <span class="cooked-ing-measurement">' . wp_kses_post( $measurement ) . '</span> ' : '' );
857 do_action( 'cooked_ingredient_after_amount', $ing );
858 echo ( $name ? '<span class="cooked-ing-name">' . wp_kses_post( $name ) . '</span>' : '' );
859 do_action( 'cooked_ingredient_after_name', $ing );
860 echo '</div>';
861 endif;
862
863 endif;
864
865 $ing_html = ob_get_clean();
866 echo apply_filters( 'cooked_single_ingredient_html', $ing_html, $ing, $checkboxes, $plain_text );
867
868 }
869
870 public static function single_direction($dir, $number = false, $plain_text = false, $step = false, $atts = false) {
871 global $recipe_settings;
872
873 if (isset($dir['section_heading_name']) && $dir['section_heading_name']) {
874
875 if ($plain_text) {
876 return $dir['section_heading_name'];
877 } else {
878 echo '<div class="cooked-single-direction cooked-heading">' . esc_html($dir['section_heading_name']) . '</div>';
879 }
880
881 } elseif (isset($dir['content']) && $dir['content'] || isset($dir['image']) && $dir['image']) {
882
883 $dir_image_size = apply_filters( 'cooked_direction_image_size', 'large' );
884 $image = isset($dir['image']) && $dir['image'] ? wp_get_attachment_image( $dir['image'], $dir_image_size ) : '';
885 $content = !empty($dir['content']) ? Cooked_Recipes::format_content($dir['content']) : '';
886
887 $image = apply_filters('cooked_direction_image_html', $image, $atts);
888
889 if ($plain_text) {
890 return $content;
891 } else {
892 /* translators: singular and plural "steps" */
893 $step_string = sprintf( __( 'Step %d', 'cooked' ), $step );
894
895 echo '<div id="cooked-single-direction-step-'. $number .'" class="cooked-single-direction cooked-direction' . ($image ? ' cooked-direction-has-image' : '') . ( $number ? ' cooked-direction-has-number' . ( $number > 9 ? '-wide' : '' ) : '' ) . '"' . ( $step ? ' data-step="' . $step_string . '"' : '' ) . '>';
896 echo $number ? '<span class="cooked-direction-number">' . esc_html($number) . '</span>' : '';
897 echo '<div class="cooked-dir-content">' . do_shortcode($content) . ($image ? wpautop($image) : '') . '</div>';
898 echo '</div>';
899 }
900 }
901 }
902
903 public static function format_content($content) {
904 return wpautop(wp_kses_post(html_entity_decode($content)));
905 }
906
907 public static function difficulty_level($level) {
908 $level_names = self::difficulty_levels();
909 $level_text = isset($level_names[$level]) ? $level_names[$level] : false;
910 return $level_text ? '<span class="cooked-difficulty-level-' . esc_attr($level) . '">' . wp_kses_post($level_text) . '</span>' : '';
911 }
912
913 public static function recipe_search_box( $options = false ) {
914 global $_cooked_settings, $recipe_args, $tax_col_count, $active_taxonomy;
915
916 $tax_col_count = 0;
917 $filters_set = [];
918 $taxonomy_search_fields = '';
919
920 if ( isset($recipe_args['tax_query']) ):
921 foreach ( $recipe_args['tax_query'] as $query ):
922 if ( isset($query['taxonomy']) && isset($query['terms']) ):
923 $filters_set[$query['taxonomy']] = implode( ',', $query['terms'] );
924 endif;
925 endforeach;
926
927 if ( isset($filters_set) ):
928 foreach ( $filters_set as $taxonomy => $filter ):
929 $this_tax = get_term_by( 'slug', $filter, $taxonomy );
930 $this_tax = ( $this_tax ? $this_tax : get_term_by( 'id', $filter, $taxonomy ) );
931 $filters_set[$taxonomy] = $this_tax->term_id;
932 $active_taxonomy = ( !isset($active_taxonomy) ? $this_tax->name : $active_taxonomy );
933 endforeach;
934 endif;
935 endif;
936
937 $total_taxonomies = 0;
938
939 $inline_browse = ( isset($options['inline_browse']) && $options['inline_browse'] ? true : false );
940
941 ob_start();
942 if ( !empty($_cooked_settings['recipe_taxonomies']) ):
943
944 if ( !$inline_browse ):
945
946 echo '<div class="cooked-field-wrap cooked-field-wrap-select' . ( isset($active_taxonomy) ? ' cooked-taxonomy-selected' : '' ) . '">';
947 echo '<span class="cooked-browse-select">';
948 echo '<span class="cooked-field-title">' . ( isset($active_taxonomy) ? esc_html( $active_taxonomy ) : __('Browse','cooked') ) . '</span>';
949 echo '<span class="cooked-browse-select-block cooked-clearfix">';
950
951 endif;
952
953 ob_start();
954
955 do_action( 'cooked_before_search_filter_columns' );
956
957 if ( isset($active_taxonomy) ):
958 $recipes_page_id = ( $_cooked_settings['browse_page'] ? $_cooked_settings['browse_page'] : get_the_ID() );
959 $view_all_recipes_url = get_permalink( $recipes_page_id );
960 else:
961 $view_all_recipes_url = false;
962 endif;
963
964 if ( in_array( 'cp_recipe_category', $_cooked_settings['recipe_taxonomies']) ):
965 $terms_array = Cooked_Settings::terms_array( 'cp_recipe_category', false, __('No categories','cooked'), true, true, false );
966 if ( !empty($terms_array) ):
967 echo '<span class="cooked-tax-column">';
968 echo '<span class="cooked-tax-column-title">' . __('Categories','cooked') . '</span>';
969 echo '<div class="cooked-tax-scrollable">';
970 echo ( $view_all_recipes_url ? '<a href="' . esc_url( $view_all_recipes_url ) . '">' . __( 'All Categories','cooked' ) . '</a>' : '' );
971 foreach ( $terms_array as $key => $val ):
972 if ( $key ):
973 $term = get_term( $key );
974 $term_link = ( !empty($term) ? get_term_link( $term ) : false );
975 $term_name = apply_filters( 'cooked_term_name', $term->name, $term->ID, $term->taxonomy );
976 echo ( $term_link ? ( isset($active_taxonomy) && $active_taxonomy == $val ? '<strong><i class="cooked-icon cooked-icon-angle-right"></i>&nbsp;&nbsp;' : '' ) . '<a href="' . esc_url($term_link) . '">' . esc_html($term_name) . '</a>' . ( isset($active_taxonomy) && $active_taxonomy == $val ? '</strong>' : '' ) : '' );
977 $total_taxonomies++;
978 $sub_terms_array = Cooked_Settings::terms_array( 'cp_recipe_category', false, false, true, false, $key );
979 if ( !empty($sub_terms_array) ):
980 foreach ( $sub_terms_array as $sub_key => $sub_val ):
981 if ( $sub_key ):
982 $sub_term = get_term( $sub_key );
983 $sub_term_link = ( !empty($sub_term) ? get_term_link( $sub_term ) : false );
984 $sub_term_name = apply_filters( 'cooked_term_name', $sub_term->name, $sub_term->ID, $sub_term->taxonomy );
985 echo ( $sub_term_link ? '<span class="cooked-tax-sub-item">' . ( isset($active_taxonomy) && $active_taxonomy == $sub_val ? '<strong><i class="cooked-icon cooked-icon-angle-right"></i>&nbsp;&nbsp;' : '' ) . '<a href="' . esc_url($sub_term_link) . '">' . esc_html($sub_term_name) . '</a>' . ( isset($active_taxonomy) && $active_taxonomy == $sub_val ? '</strong>' : '' ) . '</span>' : '' );
986 $total_taxonomies++;
987 endif;
988 endforeach;
989 endif;
990 endif;
991 endforeach;
992 $tax_col_count++;
993 echo '</div>';
994 echo '</span>';
995 endif;
996 endif;
997
998 do_action( 'cooked_after_search_filter_columns' );
999
1000 $browse_html = ob_get_clean();
1001
1002 if ( !$inline_browse ):
1003
1004 echo wp_kses_post( $browse_html );
1005 echo '</span></span></div>';
1006
1007 endif;
1008
1009 endif;
1010
1011 if ( $total_taxonomies ):
1012 $taxonomy_search_fields = ob_get_clean();
1013 else:
1014 ob_clean();
1015 $taxonomy_search_fields = false;
1016 endif;
1017
1018 $page_id = $_cooked_settings['browse_page'] ? $_cooked_settings['browse_page'] : get_the_ID();
1019 $form_redirect = get_permalink($page_id);
1020
1021 $cooked_search_s = get_query_var('cooked_search_s', '');
1022
1023 ob_start();
1024
1025 echo '<section class="cooked-recipe-search cooked-clearfix' . ( isset( $options['compact'] ) && $options['compact'] ? ' cooked-search-compact' : '' ) . ( isset( $options['hide_sorting'] ) && $options['hide_sorting'] ? ' cooked-search-no-sorting' : '' ) . ( isset( $options['hide_browse'] ) && $options['hide_browse'] ? ' cooked-search-no-browse' : '' ) . '">';
1026
1027 echo '<form action="' . esc_url( $form_redirect ) . '" method="get">';
1028
1029 echo '<input type="hidden" name="page_id" value="' . intval( $page_id ) . '">';
1030 if ( isset($recipe_args['tax_query'][0]['taxonomy']) ):
1031 echo '<input type="hidden" name="' . esc_attr( $recipe_args['tax_query'][0]['taxonomy'] ) . '" value="' . esc_attr( $recipe_args['tax_query'][0]['terms'][0] ) . '">';
1032 endif;
1033
1034 echo '<div class="cooked-fields-wrap cooked-' . esc_attr( $tax_col_count ) . '-search-fields">';
1035
1036 echo !$options['hide_browse'] && $taxonomy_search_fields ? $taxonomy_search_fields : '';
1037
1038 echo '<input class="cooked-browse-search" type="text" name="cooked_search_s" value="' . ( !empty($cooked_search_s) ? Cooked_Functions::sanitize_text_field( $cooked_search_s ) : '' ) . '" placeholder="' . __('Find a recipe...','cooked') . '" />';
1039
1040 echo '<a href="#" class="cooked-browse-search-button"><i class="cooked-icon cooked-icon-search"></i></a>';
1041
1042 echo '</div>';
1043
1044 if ( isset( $recipe_args['orderby'] ) && is_array( $recipe_args['orderby'] ) ):
1045 $sorting_type = key($recipe_args['orderby']) . '_' . current( $recipe_args['orderby'] );
1046 else:
1047 $sorting_type = ( isset( $recipe_args['orderby'] ) && isset( $recipe_args['order'] ) ? $recipe_args['orderby'] . '_' . $recipe_args['order'] : 'date_desc' );
1048 endif;
1049
1050 $sorting_types = apply_filters( 'cooked_browse_sorting_types', [
1051 'date_desc' => [
1052 'slug' => 'date_desc',
1053 'name' => __("Newest first", "cooked")
1054 ],
1055 'date_asc' => [
1056 'slug' => 'date_asc',
1057 'name' => __("Oldest first", "cooked")
1058 ],
1059 'title_asc' => [
1060 'slug' => 'title_asc',
1061 'name' => __("Alphabetical (A-Z)", "cooked")
1062 ],
1063 'title_desc' => [
1064 'slug' => 'title_desc',
1065 'name' => __("Alphabetical (Z-A)", "cooked")
1066 ]
1067 ], $sorting_type );
1068
1069 if ( !$options['hide_sorting'] ):
1070
1071 echo '<span class="cooked-sortby-wrap"><select class="cooked-sortby-select" name="cooked_browse_sort_by">';
1072 foreach ( $sorting_types as $value => $type ):
1073 echo '<option value="' . esc_attr( $value ) . '"' . ( $sorting_type == $type['slug'] ? ' selected' : '' ) . '>' . esc_attr( $type['name'] ) . '</option>';
1074 endforeach;
1075 echo '</select></span>';
1076
1077 endif;
1078
1079 echo '</form>';
1080
1081 echo '</section>';
1082
1083 if ( $inline_browse ):
1084 echo '<div class="cooked-browse-select-inline-block cooked-clearfix">';
1085 echo wp_kses_post( $browse_html );
1086 echo '</div>';
1087 endif;
1088
1089 return ob_get_clean();
1090 }
1091
1092 public function recipe_template( $content ) {
1093 global $wp_query, $post, $_cooked_content_unfiltered;
1094
1095 if ( post_password_required() ):
1096 return $content;
1097 endif;
1098
1099 if ( is_singular('cp_recipe') && is_main_query() && $_cooked_content_unfiltered == false ):
1100 ob_start();
1101
1102 include COOKED_DIR . 'templates/front/recipe.php';
1103 // load_template( COOKED_DIR . 'templates/front/recipe.php', false );
1104
1105 $recipe_content = ob_get_clean();
1106 return shortcode_unautop( apply_filters( 'cooked_recipe_content_filter', $recipe_content, $content, $post->ID ) );
1107
1108 endif;
1109
1110 return $content;
1111 }
1112
1113 /**
1114 * Cooked Classic 2.x Backwards Compatibility
1115 *
1116 * @since 1.0.0
1117 */
1118
1119 // Get and return the Cooked 2.x Classic recipe meta information
1120 public static function get_c2_recipe_meta( $post_id ) {
1121 $recipe_meta = [];
1122 $revised_array = [];
1123 $recipe_cs2_meta = get_post_meta($post_id);
1124
1125 if ( isset($recipe_cs2_meta['_cp_recipe_ingredients']) ):
1126
1127 foreach($recipe_cs2_meta as $key => $content):
1128 $revised_array[$key] = $content[0];
1129 endforeach;
1130
1131 $recipe_cs2_meta = $revised_array;
1132
1133 $recipe_meta['_cp_recipe_title'] = get_the_title( $post_id );
1134 $recipe_meta['_cp_recipe_ingredients'] = isset($recipe_cs2_meta['_cp_recipe_ingredients']) ? $recipe_cs2_meta['_cp_recipe_ingredients'] : false;
1135 $recipe_meta['_cp_recipe_detailed_ingredients'] = isset($recipe_cs2_meta['_cp_recipe_detailed_ingredients']) ? $recipe_cs2_meta['_cp_recipe_detailed_ingredients'] : false;
1136 $recipe_meta['_cp_recipe_directions'] = isset($recipe_cs2_meta['_cp_recipe_directions']) ? $recipe_cs2_meta['_cp_recipe_directions'] : false;
1137 $recipe_meta['_cp_recipe_detailed_directions'] = isset($recipe_cs2_meta['_cp_recipe_detailed_directions']) ? $recipe_cs2_meta['_cp_recipe_detailed_directions'] : false;
1138 $recipe_meta['_cp_recipe_external_video'] = isset($recipe_cs2_meta['_cp_recipe_external_video']) ? $recipe_cs2_meta['_cp_recipe_external_video'] : false;
1139 $recipe_meta['_cp_recipe_short_description'] = isset($recipe_cs2_meta['_cp_recipe_short_description']) ? $recipe_cs2_meta['_cp_recipe_short_description'] : false;
1140 $recipe_meta['_cp_recipe_excerpt'] = isset($recipe_cs2_meta['_cp_recipe_excerpt']) ? $recipe_cs2_meta['_cp_recipe_excerpt'] : false;
1141 $recipe_meta['_cp_recipe_difficulty_level'] = isset($recipe_cs2_meta['_cp_recipe_difficulty_level']) ? $recipe_cs2_meta['_cp_recipe_difficulty_level'] : false;
1142 $recipe_meta['_cp_recipe_prep_time'] = isset($recipe_cs2_meta['_cp_recipe_prep_time']) ? $recipe_cs2_meta['_cp_recipe_prep_time'] : false;
1143 $recipe_meta['_cp_recipe_cook_time'] = isset($recipe_cs2_meta['_cp_recipe_cook_time']) ? $recipe_cs2_meta['_cp_recipe_cook_time'] : false;
1144 $recipe_meta['_cp_recipe_additional_notes'] = isset($recipe_cs2_meta['_cp_recipe_additional_notes']) ? $recipe_cs2_meta['_cp_recipe_additional_notes'] : false;
1145 $recipe_meta['_cp_recipe_admin_rating'] = isset($recipe_cs2_meta['_cp_recipe_admin_rating']) ? $recipe_cs2_meta['_cp_recipe_admin_rating'] : false;
1146 $recipe_meta['_cp_recipe_yields'] = isset($recipe_cs2_meta['_cp_recipe_yields']) ? $recipe_cs2_meta['_cp_recipe_yields'] : false;
1147 $recipe_meta['_cp_recipe_nutrition_servingsize'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_servingsize']) ? $recipe_cs2_meta['_cp_recipe_nutrition_servingsize'] : false;
1148 $recipe_meta['_cp_recipe_nutrition_calories'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_calories']) ? $recipe_cs2_meta['_cp_recipe_nutrition_calories'] : false;
1149 $recipe_meta['_cp_recipe_nutrition_fat'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_fat']) ? $recipe_cs2_meta['_cp_recipe_nutrition_fat'] : false;
1150 $recipe_meta['_cp_recipe_nutrition_satfat'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_satfat']) ? $recipe_cs2_meta['_cp_recipe_nutrition_satfat'] : false;
1151 $recipe_meta['_cp_recipe_nutrition_transfat'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_transfat']) ? $recipe_cs2_meta['_cp_recipe_nutrition_transfat'] : false;
1152 $recipe_meta['_cp_recipe_nutrition_cholesterol'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_cholesterol']) ? $recipe_cs2_meta['_cp_recipe_nutrition_cholesterol'] : false;
1153 $recipe_meta['_cp_recipe_nutrition_sodium'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_sodium']) ? $recipe_cs2_meta['_cp_recipe_nutrition_sodium'] : false;
1154 $recipe_meta['_cp_recipe_nutrition_potassium'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_potassium']) ? $recipe_cs2_meta['_cp_recipe_nutrition_potassium'] : false;
1155 $recipe_meta['_cp_recipe_nutrition_carbs'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_carbs']) ? $recipe_cs2_meta['_cp_recipe_nutrition_carbs'] : false;
1156 $recipe_meta['_cp_recipe_nutrition_fiber'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_fiber']) ? $recipe_cs2_meta['_cp_recipe_nutrition_fiber'] : false;
1157 $recipe_meta['_cp_recipe_nutrition_sugar'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_sugar']) ? $recipe_cs2_meta['_cp_recipe_nutrition_sugar'] : false;
1158 $recipe_meta['_cp_recipe_nutrition_protein'] = isset($recipe_cs2_meta['_cp_recipe_nutrition_protein']) ? $recipe_cs2_meta['_cp_recipe_nutrition_protein'] : false;
1159
1160 if ( !$recipe_meta['_cp_recipe_excerpt'] ):
1161 $recipe_meta['_cp_recipe_excerpt'] = $recipe_meta['_cp_recipe_short_description'];
1162 $recipe_meta['_cp_recipe_short_description'] = false;
1163 endif;
1164
1165 return $recipe_meta;
1166
1167 endif;
1168
1169 return [];
1170 }
1171
1172 // Sync up the Cooked 2.x Classic recipe meta fields to the new Cooked 3.x meta fields
1173 public static function sync_c2_recipe_settings( $c2_recipe_settings, $recipe_id ) {
1174 $recipe_settings = [];
1175 $ingredients = [];
1176 $directions = [];
1177
1178 $recipe_settings['title'] = isset($c2_recipe_settings['_cp_recipe_title']) ? $c2_recipe_settings['_cp_recipe_title'] : '';
1179 $recipe_settings['content'] = isset($c2_recipe_settings['_cp_recipe_short_description']) ? wpautop( $c2_recipe_settings['_cp_recipe_short_description'] . ($c2_recipe_settings['_cp_recipe_short_description'] ? '<br><br>' : '') . Cooked_Recipes::default_content() . ($c2_recipe_settings['_cp_recipe_additional_notes'] ? '<br><br>' : '') . $c2_recipe_settings['_cp_recipe_additional_notes'] ) : '';
1180 $recipe_settings['excerpt'] = isset($c2_recipe_settings['_cp_recipe_excerpt']) ? $c2_recipe_settings['_cp_recipe_excerpt'] : '';
1181 $recipe_settings['difficulty_level'] = isset($c2_recipe_settings['_cp_recipe_difficulty_level']) ? $c2_recipe_settings['_cp_recipe_difficulty_level'] : '';
1182 $recipe_settings['prep_time'] = isset($c2_recipe_settings['_cp_recipe_prep_time']) ? $c2_recipe_settings['_cp_recipe_prep_time'] : '';
1183 $recipe_settings['cook_time'] = isset($c2_recipe_settings['_cp_recipe_cook_time']) ? $c2_recipe_settings['_cp_recipe_cook_time'] : '';
1184
1185 // Ingredients
1186 if ( !empty($c2_recipe_settings['_cp_recipe_ingredients']) && empty($c2_recipe_settings['_cp_recipe_detailed_ingredients']) ):
1187 $ingredients = explode("\n", $c2_recipe_settings['_cp_recipe_ingredients']);
1188 elseif ( !empty($c2_recipe_settings['_cp_recipe_detailed_ingredients']) ):
1189 $ingredients = unserialize( $c2_recipe_settings['_cp_recipe_detailed_ingredients'] );
1190 endif;
1191
1192 if ( !empty($ingredients) ):
1193 foreach( $ingredients as $ing ):
1194 $rand_id = wp_rand( 1000000,9999999 );
1195 if ( isset($ing['type']) && $ing['type'] == 'ingredient' ):
1196 $recipe_settings['ingredients'][$rand_id]['amount'] = $ing['amount'];
1197 $recipe_settings['ingredients'][$rand_id]['measurement'] = $ing['measurement'];
1198 $recipe_settings['ingredients'][$rand_id]['name'] = $ing['name'];
1199 elseif ( isset($ing['type']) && $ing['type'] == 'section' ):
1200 $recipe_settings['ingredients'][$rand_id]['section_heading_name'] = $ing['value'];
1201 else:
1202 if ( substr($ing, 0, 2) == '--' ):
1203 $recipe_settings['ingredients'][$rand_id]['section_heading_name'] = substr($ing, 2);
1204 else:
1205 $recipe_settings['ingredients'][$rand_id]['amount'] = false;
1206 $recipe_settings['ingredients'][$rand_id]['measurement'] = false;
1207 $recipe_settings['ingredients'][$rand_id]['name'] = $ing;
1208 endif;
1209 endif;
1210 endforeach;
1211 endif;
1212
1213 // Directions
1214 if ( !empty($c2_recipe_settings['_cp_recipe_directions']) && empty($c2_recipe_settings['_cp_recipe_detailed_directions']) ):
1215 $directions = explode("\n", $c2_recipe_settings['_cp_recipe_directions']);
1216 elseif ( !empty($c2_recipe_settings['_cp_recipe_detailed_directions']) ):
1217 $directions = unserialize( $c2_recipe_settings['_cp_recipe_detailed_directions'] );
1218 endif;
1219
1220 if ( !empty($directions) ):
1221
1222 foreach( $directions as $dir ):
1223 $rand_id = wp_rand( 1000000,9999999 );
1224 if ( isset($dir['type']) && $dir['type'] == 'direction' ):
1225 $recipe_settings['directions'][$rand_id]['image'] = $dir['image_id'];
1226 $recipe_settings['directions'][$rand_id]['content'] = $dir['value'];
1227 elseif ( isset($dir['type']) && $dir['type'] == 'section' ):
1228 $recipe_settings['directions'][$rand_id]['section_heading_name'] = $dir['value'];
1229 else:
1230 if ( substr($dir, 0, 2) == '--' ):
1231 $recipe_settings['directions'][$rand_id]['section_heading_name'] = substr($dir, 2);
1232 else:
1233 $recipe_settings['directions'][$rand_id]['image'] = false;
1234 $recipe_settings['directions'][$rand_id]['content'] = $dir;
1235 endif;
1236 endif;
1237 endforeach;
1238
1239 endif;
1240
1241 if ( isset($c2_recipe_settings['_cp_recipe_external_video']) && $c2_recipe_settings['_cp_recipe_external_video'] && wp_oembed_get( $c2_recipe_settings['_cp_recipe_external_video'] ) ):
1242 $recipe_settings['gallery']['video_url'] = $c2_recipe_settings['_cp_recipe_external_video'];
1243 else:
1244 $recipe_settings['gallery']['video_url'] = false;
1245 $recipe_settings['gallery']['type'] = 'cooked';
1246 endif;
1247
1248 $recipe_settings['nutrition']['serving_size'] = ( isset($c2_recipe_settings['_cp_recipe_yields']) && $c2_recipe_settings['_cp_recipe_yields'] ? $c2_recipe_settings['_cp_recipe_yields'] : false );
1249 $recipe_settings['nutrition']['servings'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_servingsize']) && $c2_recipe_settings['_cp_recipe_nutrition_servingsize'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_servingsize']) : false );
1250 $recipe_settings['nutrition']['calories'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_calories']) && $c2_recipe_settings['_cp_recipe_nutrition_calories'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_calories']) : false );
1251 $recipe_settings['nutrition']['fat'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_fat']) && $c2_recipe_settings['_cp_recipe_nutrition_fat'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_fat']) : false );
1252 $recipe_settings['nutrition']['sat_fat'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_satfat']) && $c2_recipe_settings['_cp_recipe_nutrition_satfat'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_satfat']) : false );
1253 $recipe_settings['nutrition']['trans_fat'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_transfat']) && $c2_recipe_settings['_cp_recipe_nutrition_transfat'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_transfat']) : false );
1254 $recipe_settings['nutrition']['cholesterol'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_cholesterol']) && $c2_recipe_settings['_cp_recipe_nutrition_cholesterol'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_cholesterol']) : false );
1255 $recipe_settings['nutrition']['sodium'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_sodium']) && $c2_recipe_settings['_cp_recipe_nutrition_sodium'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_sodium']) : false );
1256 $recipe_settings['nutrition']['potassium'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_potassium']) && $c2_recipe_settings['_cp_recipe_nutrition_potassium'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_potassium']) : false );
1257 $recipe_settings['nutrition']['carbs'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_carbs']) && $c2_recipe_settings['_cp_recipe_nutrition_carbs'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_carbs']) : false );
1258 $recipe_settings['nutrition']['fiber'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_fiber']) && $c2_recipe_settings['_cp_recipe_nutrition_fiber'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_fiber']) : false );
1259 $recipe_settings['nutrition']['sugars'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_sugar']) && $c2_recipe_settings['_cp_recipe_nutrition_sugar'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_sugar']) : false );
1260 $recipe_settings['nutrition']['protein'] = ( isset($c2_recipe_settings['_cp_recipe_nutrition_protein']) && $c2_recipe_settings['_cp_recipe_nutrition_protein'] ? preg_replace("/[^0-9]/","",$c2_recipe_settings['_cp_recipe_nutrition_protein']) : false );
1261
1262 $_nutrition_facts = Cooked_Measurements::nutrition_facts();
1263 foreach( $_nutrition_facts as $nutrition_facts ):
1264 foreach( $nutrition_facts as $slug => $nf ):
1265
1266 if ( !isset($recipe_settings[$slug]) ): $recipe_settings[$slug] = false; endif;
1267 if ( isset($nf['subs']) ):
1268 foreach( $nf['subs'] as $sub_slug => $sub_nf ):
1269 if ( !isset($recipe_settings[$sub_slug]) ): $recipe_settings[$sub_slug] = false; endif;
1270 endforeach;
1271 endif;
1272 endforeach;
1273 endforeach;
1274
1275 return apply_filters( 'cooked_sync_c2_recipe_settings', $recipe_settings, $recipe_id );
1276 }
1277
1278 public function modify_browse_page_canonical_url($canonical_url, $post = null) {
1279 global $_cooked_settings, $wp_query;
1280
1281 if (!is_page()) {
1282 return $canonical_url;
1283 }
1284
1285 $browse_page_id = !empty($_cooked_settings['browse_page']) ? $_cooked_settings['browse_page'] : false;
1286
1287 // Only modify for browse page with category.
1288 if (is_page($browse_page_id) &&
1289 isset($wp_query->query['cp_recipe_category']) &&
1290 taxonomy_exists('cp_recipe_category') &&
1291 term_exists($wp_query->query['cp_recipe_category'], 'cp_recipe_category')) {
1292
1293 // Build the canonical URL based on permalink structure.
1294 if (get_option('permalink_structure')) {
1295 $new_canonical = untrailingslashit(get_permalink($browse_page_id)) . '/' . $_cooked_settings['recipe_category_permalink'] . '/' . $wp_query->query['cp_recipe_category'];
1296 } else {
1297 $new_canonical = add_query_arg('cp_recipe_category', $wp_query->query['cp_recipe_category'], get_permalink($browse_page_id));
1298 }
1299
1300 return $new_canonical;
1301 }
1302
1303 return $canonical_url;
1304 }
1305 }
1306
1307 global $Cooked_Recipes;
1308 $Cooked_Recipes = new Cooked_Recipes();
1309