recipe.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Embed recipe 'cards' in post, with basic styling and print functionality |
| 5 | * |
| 6 | * To Do |
| 7 | * - defaults settings |
| 8 | * - basic styles/themecolor styles |
| 9 | * - validation/sanitization |
| 10 | * - print styles |
| 11 | */ |
| 12 | class Jetpack_Recipes { |
| 13 | |
| 14 | private $scripts_and_style_included = false; |
| 15 | |
| 16 | function __construct() { |
| 17 | add_action( 'init', array( $this, 'action_init' ) ); |
| 18 | |
| 19 | add_filter( 'wp_kses_allowed_html', array( $this, 'add_recipes_kses_rules' ), 10, 2 ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Add Schema-specific attributes to our allowed tags in wp_kses, |
| 24 | * so we can have better Schema.org compliance. |
| 25 | * |
| 26 | * @param array $allowedtags Array of allowed HTML tags in recipes. |
| 27 | * @param array $context Context to judge allowed tags by. |
| 28 | */ |
| 29 | function add_recipes_kses_rules( $allowedtags, $context ) { |
| 30 | if ( in_array( $context, array( '', 'post', 'data' ) ) ) : |
| 31 | // Create an array of all the tags we'd like to add the itemprop attribute to. |
| 32 | $tags = array( 'li', 'ol', 'ul', 'img', 'p', 'h3', 'time' ); |
| 33 | foreach ( $tags as $tag ) { |
| 34 | $allowedtags = $this->add_kses_rule( |
| 35 | $allowedtags, |
| 36 | $tag, |
| 37 | array( |
| 38 | 'class' => array(), |
| 39 | 'itemprop' => array(), |
| 40 | 'datetime' => array(), |
| 41 | ) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | // Allow itemscope and itemtype for divs. |
| 46 | $allowedtags = $this->add_kses_rule( |
| 47 | $allowedtags, |
| 48 | 'div', |
| 49 | array( |
| 50 | 'class' => array(), |
| 51 | 'itemscope' => array(), |
| 52 | 'itemtype' => array(), |
| 53 | ) |
| 54 | ); |
| 55 | endif; |
| 56 | |
| 57 | return $allowedtags; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Function to add a new property rule to our kses array. |
| 62 | * Used by add_recipe_kses_rules() above. |
| 63 | * |
| 64 | * @param array $all_tags Array of allowed HTML tags in recipes. |
| 65 | * @param string $tag New HTML tag to add to the array of allowed HTML. |
| 66 | * @param array $rules Array of allowed attributes for that HTML tag. |
| 67 | */ |
| 68 | private function add_kses_rule( $all_tags, $tag, $rules ) { |
| 69 | |
| 70 | // If the tag doesn't already exist, add it. |
| 71 | if ( ! isset( $all_tags[ $tag ] ) ) { |
| 72 | $all_tags[ $tag ] = array(); |
| 73 | } |
| 74 | |
| 75 | // Merge the new tags with existing tags. |
| 76 | $all_tags[ $tag ] = array_merge( $all_tags[ $tag ], $rules ); |
| 77 | |
| 78 | return $all_tags; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Register our shortcode and enqueue necessary files. |
| 83 | */ |
| 84 | function action_init() { |
| 85 | // Enqueue styles if [recipe] exists. |
| 86 | add_action( 'wp_head', array( $this, 'add_scripts' ), 1 ); |
| 87 | |
| 88 | // Render [recipe], along with other shortcodes that can be nested within. |
| 89 | add_shortcode( 'recipe', array( $this, 'recipe_shortcode' ) ); |
| 90 | add_shortcode( 'recipe-notes', array( $this, 'recipe_notes_shortcode' ) ); |
| 91 | add_shortcode( 'recipe-ingredients', array( $this, 'recipe_ingredients_shortcode' ) ); |
| 92 | add_shortcode( 'recipe-directions', array( $this, 'recipe_directions_shortcode' ) ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Enqueue scripts and styles |
| 97 | */ |
| 98 | function add_scripts() { |
| 99 | if ( empty( $GLOBALS['posts'] ) || ! is_array( $GLOBALS['posts'] ) ) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | foreach ( $GLOBALS['posts'] as $p ) { |
| 104 | if ( has_shortcode( $p->post_content, 'recipe' ) ) { |
| 105 | $this->scripts_and_style_included = true; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if ( ! $this->scripts_and_style_included ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | wp_enqueue_style( 'jetpack-recipes-style', plugins_url( '/css/recipes.css', __FILE__ ), array(), '20130919' ); |
| 115 | wp_style_add_data( 'jetpack-recipes-style', 'rtl', 'replace' ); |
| 116 | |
| 117 | // add $themecolors-defined styles. |
| 118 | wp_add_inline_style( 'jetpack-recipes-style', self::themecolor_styles() ); |
| 119 | |
| 120 | wp_enqueue_script( |
| 121 | 'jetpack-recipes-printthis', |
| 122 | Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/recipes-printthis.min.js', 'modules/shortcodes/js/recipes-printthis.js' ), |
| 123 | array( 'jquery' ), |
| 124 | '20170202' |
| 125 | ); |
| 126 | |
| 127 | wp_enqueue_script( |
| 128 | 'jetpack-recipes-js', |
| 129 | Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/recipes.min.js', 'modules/shortcodes/js/recipes.js' ), |
| 130 | array( 'jquery', 'jetpack-recipes-printthis' ), |
| 131 | '20131230' |
| 132 | ); |
| 133 | |
| 134 | $title_var = wp_title( '|', false, 'right' ); |
| 135 | $rtl = is_rtl() ? '-rtl' : ''; |
| 136 | $print_css_var = plugins_url( "/css/recipes-print{$rtl}.css", __FILE__ ); |
| 137 | |
| 138 | wp_localize_script( |
| 139 | 'jetpack-recipes-js', |
| 140 | 'jetpack_recipes_vars', |
| 141 | array( |
| 142 | 'pageTitle' => $title_var, |
| 143 | 'loadCSS' => $print_css_var, |
| 144 | ) |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Our [recipe] shortcode. |
| 150 | * Prints recipe data styled to look good on *any* theme. |
| 151 | * |
| 152 | * @param array $atts Array of shortcode attributes. |
| 153 | * @param string $content Post content. |
| 154 | * |
| 155 | * @return string HTML for recipe shortcode. |
| 156 | */ |
| 157 | static function recipe_shortcode( $atts, $content = '' ) { |
| 158 | $atts = shortcode_atts( |
| 159 | array( |
| 160 | 'title' => '', // string. |
| 161 | 'servings' => '', // intval. |
| 162 | 'time' => '', // string. |
| 163 | 'difficulty' => '', // string. |
| 164 | 'print' => '', // string. |
| 165 | 'source' => '', // string. |
| 166 | 'sourceurl' => '', // string. |
| 167 | 'image' => '', // string. |
| 168 | 'description' => '', // string. |
| 169 | ), $atts, 'recipe' |
| 170 | ); |
| 171 | |
| 172 | return self::recipe_shortcode_html( $atts, $content ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * The recipe output |
| 177 | * |
| 178 | * @param array $atts Array of shortcode attributes. |
| 179 | * @param string $content Post content. |
| 180 | * |
| 181 | * @return string HTML output |
| 182 | */ |
| 183 | static function recipe_shortcode_html( $atts, $content = '' ) { |
| 184 | |
| 185 | $html = '<div class="hrecipe jetpack-recipe" itemscope itemtype="https://schema.org/Recipe">'; |
| 186 | |
| 187 | // Print the recipe title if exists. |
| 188 | if ( '' !== $atts['title'] ) { |
| 189 | $html .= '<h3 class="jetpack-recipe-title" itemprop="name">' . esc_html( $atts['title'] ) . '</h3>'; |
| 190 | } |
| 191 | |
| 192 | // Print the recipe meta if exists. |
| 193 | if ( '' !== $atts['servings'] || '' != $atts['time'] || '' != $atts['difficulty'] || '' != $atts['print'] ) { |
| 194 | $html .= '<ul class="jetpack-recipe-meta">'; |
| 195 | |
| 196 | if ( '' !== $atts['servings'] ) { |
| 197 | $html .= sprintf( |
| 198 | '<li class="jetpack-recipe-servings" itemprop="recipeYield"><strong>%1$s: </strong>%2$s</li>', |
| 199 | esc_html_x( 'Servings', 'recipe', 'jetpack' ), |
| 200 | esc_html( $atts['servings'] ) |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | if ( '' !== $atts['time'] ) { |
| 205 | // Get a time that's supported by Schema.org. |
| 206 | $duration = WPCOM_JSON_API_Date::format_duration( $atts['time'] ); |
| 207 | // If no duration can be calculated, let's output what the user provided. |
| 208 | if ( empty( $duration ) ) { |
| 209 | $duration = $atts['time']; |
| 210 | } |
| 211 | |
| 212 | $html .= sprintf( |
| 213 | '<li class="jetpack-recipe-time"> |
| 214 | <time itemprop="totalTime" datetime="%3$s"><strong>%1$s: </strong>%2$s</time> |
| 215 | </li>', |
| 216 | esc_html_x( 'Time', 'recipe', 'jetpack' ), |
| 217 | esc_html( $atts['time'] ), |
| 218 | esc_attr( $duration ) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | if ( '' !== $atts['difficulty'] ) { |
| 223 | $html .= sprintf( |
| 224 | '<li class="jetpack-recipe-difficulty"><strong>%1$s: </strong>%2$s</li>', |
| 225 | esc_html_x( 'Difficulty', 'recipe', 'jetpack' ), |
| 226 | esc_html( $atts['difficulty'] ) |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | if ( '' !== $atts['source'] ) { |
| 231 | $html .= sprintf( |
| 232 | '<li class="jetpack-recipe-source"><strong>%1$s: </strong>', |
| 233 | esc_html_x( 'Source', 'recipe', 'jetpack' ) |
| 234 | ); |
| 235 | |
| 236 | if ( '' !== $atts['sourceurl'] ) : |
| 237 | // Show the link if we have one. |
| 238 | $html .= sprintf( |
| 239 | '<a href="%2$s">%1$s</a>', |
| 240 | esc_html( $atts['source'] ), |
| 241 | esc_url( $atts['sourceurl'] ) |
| 242 | ); |
| 243 | else : |
| 244 | // Skip the link. |
| 245 | $html .= sprintf( |
| 246 | '%1$s', |
| 247 | esc_html( $atts['source'] ) |
| 248 | ); |
| 249 | endif; |
| 250 | |
| 251 | $html .= '</li>'; |
| 252 | } |
| 253 | |
| 254 | if ( 'false' !== $atts['print'] ) { |
| 255 | $html .= sprintf( |
| 256 | '<li class="jetpack-recipe-print"><a href="#">%1$s</a></li>', |
| 257 | esc_html_x( 'Print', 'recipe', 'jetpack' ) |
| 258 | ); |
| 259 | } |
| 260 | |
| 261 | $html .= '</ul>'; |
| 262 | } // End if(). |
| 263 | |
| 264 | // Output the image, if we have one. |
| 265 | if ( '' !== $atts['image'] ) { |
| 266 | $html .= sprintf( |
| 267 | '<img class="jetpack-recipe-image" itemprop="image" src="%1$s" />', |
| 268 | esc_url( $atts['image'] ) |
| 269 | ); |
| 270 | } |
| 271 | |
| 272 | // Output the description, if we have one. |
| 273 | if ( '' !== $atts['description'] ) { |
| 274 | $html .= sprintf( |
| 275 | '<p class="jetpack-recipe-description" itemprop="description">%1$s</p>', |
| 276 | esc_html( $atts['description'] ) |
| 277 | ); |
| 278 | } |
| 279 | |
| 280 | // Print content between codes. |
| 281 | $html .= '<div class="jetpack-recipe-content">' . do_shortcode( $content ) . '</div>'; |
| 282 | |
| 283 | // Close it up. |
| 284 | $html .= '</div>'; |
| 285 | |
| 286 | // If there is a recipe within a recipe, remove the shortcode. |
| 287 | if ( has_shortcode( $html, 'recipe' ) ) { |
| 288 | remove_shortcode( 'recipe' ); |
| 289 | } |
| 290 | |
| 291 | // Sanitize html. |
| 292 | $html = wp_kses_post( $html ); |
| 293 | |
| 294 | // Return the HTML block. |
| 295 | return $html; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Our [recipe-notes] shortcode. |
| 300 | * Outputs ingredients, styled in a div. |
| 301 | * |
| 302 | * @param array $atts Array of shortcode attributes. |
| 303 | * @param string $content Post content. |
| 304 | * |
| 305 | * @return string HTML for recipe notes shortcode. |
| 306 | */ |
| 307 | static function recipe_notes_shortcode( $atts, $content = '' ) { |
| 308 | $atts = shortcode_atts( array( |
| 309 | 'title' => '', // string. |
| 310 | ), $atts, 'recipe-notes' ); |
| 311 | |
| 312 | $html = ''; |
| 313 | |
| 314 | // Print a title if one exists. |
| 315 | if ( '' !== $atts['title'] ) { |
| 316 | $html .= '<h4 class="jetpack-recipe-notes-title">' . esc_html( $atts['title'] ) . '</h4>'; |
| 317 | } |
| 318 | |
| 319 | $html .= '<div class="jetpack-recipe-notes">'; |
| 320 | |
| 321 | // Format content using list functionality, if desired. |
| 322 | $html .= self::output_list_content( $content, 'notes' ); |
| 323 | |
| 324 | $html .= '</div>'; |
| 325 | |
| 326 | // Sanitize html. |
| 327 | $html = wp_kses_post( $html ); |
| 328 | |
| 329 | // Return the HTML block. |
| 330 | return $html; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Our [recipe-ingredients] shortcode. |
| 335 | * Outputs notes, styled in a div. |
| 336 | * |
| 337 | * @param array $atts Array of shortcode attributes. |
| 338 | * @param string $content Post content. |
| 339 | * |
| 340 | * @return string HTML for recipe ingredients shortcode. |
| 341 | */ |
| 342 | static function recipe_ingredients_shortcode( $atts, $content = '' ) { |
| 343 | $atts = shortcode_atts( array( |
| 344 | 'title' => esc_html_x( 'Ingredients', 'recipe', 'jetpack' ), // string. |
| 345 | ), $atts, 'recipe-ingredients' ); |
| 346 | |
| 347 | $html = '<div class="jetpack-recipe-ingredients">'; |
| 348 | |
| 349 | // Print a title unless the user has opted to exclude it. |
| 350 | if ( 'false' !== $atts['title'] ) { |
| 351 | $html .= '<h4 class="jetpack-recipe-ingredients-title">' . esc_html( $atts['title'] ) . '</h4>'; |
| 352 | } |
| 353 | |
| 354 | // Format content using list functionality. |
| 355 | $html .= self::output_list_content( $content, 'ingredients' ); |
| 356 | |
| 357 | $html .= '</div>'; |
| 358 | |
| 359 | // Sanitize html. |
| 360 | $html = wp_kses_post( $html ); |
| 361 | |
| 362 | // Return the HTML block. |
| 363 | return $html; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Reusable function to check for shortened formatting. |
| 368 | * Basically, users can create lists with the following shorthand: |
| 369 | * - item one |
| 370 | * - item two |
| 371 | * - item three |
| 372 | * And we'll magically convert it to a list. This has the added benefit |
| 373 | * of including itemprops for the recipe schema. |
| 374 | * |
| 375 | * @param string $content HTML content. |
| 376 | * @param string $type Type of list. |
| 377 | * |
| 378 | * @return string content formatted as a list item |
| 379 | */ |
| 380 | static function output_list_content( $content, $type ) { |
| 381 | $html = ''; |
| 382 | |
| 383 | switch ( $type ) { |
| 384 | case 'directions' : |
| 385 | $list_item_replacement = '<li class="jetpack-recipe-directions">${1}</li>'; |
| 386 | $itemprop = ' itemprop="recipeInstructions"'; |
| 387 | $listtype = 'ol'; |
| 388 | break; |
| 389 | case 'ingredients' : |
| 390 | $list_item_replacement = '<li class="jetpack-recipe-ingredient" itemprop="recipeIngredient">${1}</li>'; |
| 391 | $itemprop = ''; |
| 392 | $listtype = 'ul'; |
| 393 | break; |
| 394 | default: |
| 395 | $list_item_replacement = '<li class="jetpack-recipe-notes">${1}</li>'; |
| 396 | $itemprop = ''; |
| 397 | $listtype = 'ul'; |
| 398 | } |
| 399 | |
| 400 | // Check to see if the user is trying to use shortened formatting. |
| 401 | if ( |
| 402 | strpos( $content, '–' ) !== false || |
| 403 | strpos( $content, '—' ) !== false || |
| 404 | strpos( $content, '-' ) !== false || |
| 405 | strpos( $content, '*' ) !== false || |
| 406 | strpos( $content, '#' ) !== false || |
| 407 | strpos( $content, '–' ) !== false || // ndash. |
| 408 | strpos( $content, '—' ) !== false || // mdash. |
| 409 | preg_match( '/\d+\.\s/', $content ) |
| 410 | ) { |
| 411 | // Remove breaks and extra whitespace. |
| 412 | $content = str_replace( "<br />\n", "\n", $content ); |
| 413 | $content = trim( $content ); |
| 414 | |
| 415 | $ul_pattern = '/(?:^|\n|\<p\>)+(?:[\-–—]+|\–|\—|\*)+\h+(.*)/mi'; |
| 416 | $ol_pattern = '/(?:^|\n|\<p\>)+(?:\d+\.|#+)+\h+(.*)/mi'; |
| 417 | |
| 418 | preg_match_all( $ul_pattern, $content, $ul_matches ); |
| 419 | preg_match_all( $ol_pattern, $content, $ol_matches ); |
| 420 | |
| 421 | if ( 0 !== count( $ul_matches[0] ) || 0 !== count( $ol_matches[0] ) ) { |
| 422 | |
| 423 | if ( 0 !== count( $ol_matches[0] ) ) { |
| 424 | $listtype = 'ol'; |
| 425 | $list_item_pattern = $ol_pattern; |
| 426 | } else { |
| 427 | $listtype = 'ul'; |
| 428 | $list_item_pattern = $ul_pattern; |
| 429 | } |
| 430 | $html .= '<' . $listtype . $itemprop . '>'; |
| 431 | $html .= preg_replace( $list_item_pattern, $list_item_replacement, $content ); |
| 432 | $html .= '</' . $listtype . '>'; |
| 433 | |
| 434 | // Strip out any empty <p> tags and stray </p> tags, because those are just silly. |
| 435 | $empty_p_pattern = '/(<p>)*\s*<\/p>/mi'; |
| 436 | $html = preg_replace( $empty_p_pattern, '', $html ); |
| 437 | } else { |
| 438 | $html .= do_shortcode( $content ); |
| 439 | } |
| 440 | } else { |
| 441 | $html .= do_shortcode( $content ); |
| 442 | } |
| 443 | |
| 444 | // Return our formatted content. |
| 445 | return $html; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Our [recipe-directions] shortcode. |
| 450 | * Outputs directions, styled in a div. |
| 451 | * |
| 452 | * @param array $atts Array of shortcode attributes. |
| 453 | * @param string $content Post content. |
| 454 | * |
| 455 | * @return string HTML for recipe directions shortcode. |
| 456 | */ |
| 457 | static function recipe_directions_shortcode( $atts, $content = '' ) { |
| 458 | $atts = shortcode_atts( array( |
| 459 | 'title' => esc_html_x( 'Directions', 'recipe', 'jetpack' ), // string. |
| 460 | ), $atts, 'recipe-directions' ); |
| 461 | |
| 462 | $html = '<div class="jetpack-recipe-directions">'; |
| 463 | |
| 464 | // Print a title unless the user has specified to exclude it. |
| 465 | if ( 'false' !== $atts['title'] ) { |
| 466 | $html .= '<h4 class="jetpack-recipe-directions-title">' . esc_html( $atts['title'] ) . '</h4>'; |
| 467 | } |
| 468 | |
| 469 | // Format content using list functionality. |
| 470 | $html .= self::output_list_content( $content, 'directions' ); |
| 471 | |
| 472 | $html .= '</div>'; |
| 473 | |
| 474 | // Sanitize html. |
| 475 | $html = wp_kses_post( $html ); |
| 476 | |
| 477 | // Return the HTML block. |
| 478 | return $html; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Use $themecolors array to style the Recipes shortcode |
| 483 | * |
| 484 | * @print style block |
| 485 | * @return string $style |
| 486 | */ |
| 487 | function themecolor_styles() { |
| 488 | global $themecolors; |
| 489 | $style = ''; |
| 490 | |
| 491 | if ( isset( $themecolors ) ) { |
| 492 | $style .= '.jetpack-recipe { border-color: #' . esc_attr( $themecolors['border'] ) . '; }'; |
| 493 | $style .= '.jetpack-recipe-title { border-bottom-color: #' . esc_attr( $themecolors['link'] ) . '; }'; |
| 494 | } |
| 495 | |
| 496 | return $style; |
| 497 | } |
| 498 | |
| 499 | } |
| 500 | |
| 501 | new Jetpack_Recipes(); |
| 502 |