Jetpack_Recipes

Embed recipe 'cards' in post, with basic styling and print functionality.

Defined (1)

The class is defined in the following location(s).

/modules/shortcodes/recipe.php  
  1. class Jetpack_Recipes { 
  2.  
  3. private $scripts_and_style_included = false; 
  4.  
  5. function __construct() { 
  6. add_action( 'init', array( $this, 'action_init' ) ); 
  7.  
  8. function action_init() { 
  9. // Enqueue styles if [recipe] exists 
  10. add_action( 'wp_head', array( $this, 'add_scripts' ), 1 ); 
  11.  
  12. // Render [recipe] 
  13. add_shortcode( 'recipe', array( $this, 'recipe_shortcode' ) ); 
  14.  
  15. /** 
  16. * Enqueue scripts and styles 
  17. */ 
  18. function add_scripts() { 
  19. if ( empty( $GLOBALS['posts'] ) || ! is_array( $GLOBALS['posts'] ) ) { 
  20. return; 
  21.  
  22. foreach ( $GLOBALS['posts'] as $p ) { 
  23. if ( has_shortcode( $p->post_content, 'recipe' ) ) { 
  24. $this->scripts_and_style_included = true; 
  25. break; 
  26.  
  27. if ( ! $this->scripts_and_style_included ) { 
  28. return; 
  29.  
  30. if( is_rtl() ) { 
  31. wp_enqueue_style( 'jetpack-recipes-style', plugins_url( '/css/rtl/recipes-rtl.css', __FILE__ ), array(), '20130919' ); 
  32. } else { 
  33. wp_enqueue_style( 'jetpack-recipes-style', plugins_url( '/css/recipes.css', __FILE__ ), array(), '20130919' ); 
  34.  
  35.  
  36. wp_enqueue_script( 'jetpack-recipes-printthis', plugins_url( '/js/recipes-printthis.js', __FILE__ ), array( 'jquery' ), '20131230' ); 
  37. wp_enqueue_script( 'jetpack-recipes-js', plugins_url( '/js/recipes.js', __FILE__ ), array( 'jquery', 'jetpack-recipes-printthis' ), '20131230' ); 
  38.  
  39. $title_var = wp_title( '|', false, 'right' ); 
  40. $print_css_var = plugins_url( '/css/recipes-print.css', __FILE__ ); 
  41.  
  42. wp_localize_script( 'jetpack-recipes-js', 'jetpack_recipes_vars', array( 
  43. 'pageTitle' => $title_var,  
  44. 'loadCSS' => $print_css_var 
  45. ) ); 
  46.  
  47. /** 
  48. * Our [recipe] shortcode. 
  49. * Prints recipe data styled to look good on *any* theme. 
  50. * @return resume_shortcode_html 
  51. */ 
  52. static function recipe_shortcode( $atts, $content = '' ) { 
  53. $atts = shortcode_atts( array( 
  54. 'title' => '', //string 
  55. 'servings' => '', //intval 
  56. 'time' => '', //string 
  57. 'difficulty' => '', //string 
  58. 'print' => '', //string 
  59. ), $atts, 'recipe' ); 
  60.  
  61. return self::recipe_shortcode_html( $atts, $content ); 
  62.  
  63. /** 
  64. * The recipe output 
  65. * @return Html 
  66. */ 
  67. static function recipe_shortcode_html( $atts, $content = '' ) { 
  68. $html = false; 
  69.  
  70. $html = '<div class="hrecipe jetpack-recipe" itemscope itemtype="http://schema.org/Recipe">'; 
  71.  
  72. // Print the recipe title if exists 
  73. if ( '' != $atts['title'] ) { 
  74. $html .= '<h3 class="jetpack-recipe-title" itemprop="name">' . esc_html( $atts['title'] ) . '</h3>'; 
  75.  
  76. // Print the recipe meta if exists 
  77. if ( '' != $atts['servings'] || '' != $atts['time'] || '' != $atts['difficulty'] || '' != $atts['print'] ) { 
  78. $html .= '<ul class="jetpack-recipe-meta">'; 
  79.  
  80. if ( '' != $atts['servings'] ) { 
  81. $html .= sprintf( '<li class="jetpack-recipe-servings" itemprop="recipeYield"><strong>%1$s: </strong>%2$s</li>',  
  82. __( 'Servings', 'jetpack' ),  
  83. esc_html( $atts['servings'] ) 
  84. ); 
  85.  
  86. if ( '' != $atts['time'] ) { 
  87. $html .= sprintf( '<li class="jetpack-recipe-time" itemprop="totalTime"><strong>%1$s: </strong>%2$s</li>',  
  88. __( 'Time', 'jetpack' ),  
  89. esc_html( $atts['time'] ) 
  90. ); 
  91.  
  92. if ( '' != $atts['difficulty'] ) { 
  93. $html .= sprintf( '<li class="jetpack-recipe-difficulty"><strong>%1$s: </strong>%2$s</li>',  
  94. __( 'Difficulty', 'jetpack' ),  
  95. esc_html( $atts['difficulty'] ) 
  96. ); 
  97.  
  98. if ( 'false' != $atts['print'] ) { 
  99. $html .= sprintf( '<li class="jetpack-recipe-print"><a href="#">%s</a></li>',  
  100. __( 'Print', 'jetpack' ) 
  101. ); 
  102.  
  103. $html .= '</ul>'; 
  104.  
  105. // Print content between codes 
  106. $html .= '<div class="jetpack-recipe-content">' . do_shortcode( $content ) . '</div>'; 
  107.  
  108. // Close it up 
  109. $html .= '</div>'; 
  110.  
  111. // If there is a recipe within a recipe, remove the shortcode 
  112. if ( has_shortcode( $html, 'recipe' ) ) { 
  113. remove_shortcode( 'recipe' ); 
  114.  
  115. // Sanitize html 
  116. $html = wp_kses_post( $html ); 
  117.  
  118. // Return the HTML block 
  119. return $html;