/modules/shortcodes/recipe.php

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