OS : Linux
PHP Version : 7.4.33
Software : add_kses_rule(
$allowedtags,
$tag,
array(
'class' => array(),
'itemprop' => array(),
'datetime' => array(),
)
);
}
// Allow itemscope and itemtype for divs.
$allowedtags = $this->add_kses_rule(
$allowedtags,
'div',
array(
'class' => array(),
'itemscope' => array(),
'itemtype' => array(),
)
);
endif;
return $allowedtags;
}
/**
* Function to add a new property rule to our kses array.
* Used by add_recipe_kses_rules() above.
*
* @param array $all_tags Array of allowed HTML tags in recipes.
* @param string $tag New HTML tag to add to the array of allowed HTML.
* @param array $rules Array of allowed attributes for that HTML tag.
*/
private function add_kses_rule( $all_tags, $tag, $rules ) {
// If the tag doesn't already exist, add it.
if ( ! isset( $all_tags[ $tag ] ) ) {
$all_tags[ $tag ] = array();
}
// Merge the new tags with existing tags.
$all_tags[ $tag ] = array_merge( $all_tags[ $tag ], $rules );
return $all_tags;
}
/**
* Register our shortcode and enqueue necessary files.
*/
public function action_init() {
// Enqueue styles if [recipe] exists.
add_action( 'wp_head', array( $this, 'add_scripts' ), 1 );
// Render [recipe], along with other shortcodes that can be nested within.
add_shortcode( 'recipe', array( $this, 'recipe_shortcode' ) );
add_shortcode( 'recipe-notes', array( $this, 'recipe_notes_shortcode' ) );
add_shortcode( 'recipe-ingredients', array( $this, 'recipe_ingredients_shortcode' ) );
add_shortcode( 'recipe-directions', array( $this, 'recipe_directions_shortcode' ) );
}
/**
* Enqueue scripts and styles
*/
public function add_scripts() {
if ( empty( $GLOBALS['posts'] ) || ! is_array( $GLOBALS['posts'] ) ) {
return;
}
foreach ( $GLOBALS['posts'] as $p ) {
if ( has_shortcode( $p->post_content, 'recipe' ) ) {
$this->scripts_and_style_included = true;
break;
}
}
if ( ! $this->scripts_and_style_included ) {
return;
}
wp_enqueue_style( 'jetpack-recipes-style', plugins_url( '/css/recipes.css', __FILE__ ), array(), '20130919' );
wp_style_add_data( 'jetpack-recipes-style', 'rtl', 'replace' );
// add $themecolors-defined styles.
wp_add_inline_style( 'jetpack-recipes-style', self::themecolor_styles() );
wp_enqueue_script(
'jetpack-recipes-printthis',
Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/recipes-printthis.min.js', 'modules/shortcodes/js/recipes-printthis.js' ),
array( 'jquery' ),
'20170202',
false
);
wp_enqueue_script(
'jetpack-recipes-js',
Assets::get_file_url_for_environment( '_inc/build/shortcodes/js/recipes.min.js', 'modules/shortcodes/js/recipes.js' ),
array( 'jquery', 'jetpack-recipes-printthis' ),
'20131230',
false
);
$title_var = wp_title( '|', false, 'right' );
$rtl = is_rtl() ? '-rtl' : '';
$print_css_var = plugins_url( "/css/recipes-print{$rtl}.css", __FILE__ );
wp_localize_script(
'jetpack-recipes-js',
'jetpack_recipes_vars',
array(
'pageTitle' => $title_var,
'loadCSS' => $print_css_var,
)
);
}
/**
* Our [recipe] shortcode.
* Prints recipe data styled to look good on *any* theme.
*
* @param array $atts Array of shortcode attributes.
* @param string $content Post content.
*
* @return string HTML for recipe shortcode.
*/
public static function recipe_shortcode( $atts, $content = '' ) {
$atts = shortcode_atts(
array(
'title' => '', // string.
'servings' => '', // intval.
'time' => '', // string.
'difficulty' => '', // string.
'print' => '', // string.
'source' => '', // string.
'sourceurl' => '', // string.
'image' => '', // string.
'description' => '', // string.
),
$atts,
'recipe'
);
return self::recipe_shortcode_html( $atts, $content );
}
/**
* The recipe output
*
* @param array $atts Array of shortcode attributes.
* @param string $content Post content.
*
* @return string HTML output
*/
private static function recipe_shortcode_html( $atts, $content = '' ) {
$html = ' %1$s )+(?:[\-–—]+|\–|\—|\*)+\h+(.*)/mi';
$ol_pattern = '/(?:^|\n|\ )+(?:\d+\.|#+)+\h+(.*)/mi';
preg_match_all( $ul_pattern, $content, $ul_matches );
preg_match_all( $ol_pattern, $content, $ol_matches );
if ( 0 !== count( $ul_matches[0] ) || 0 !== count( $ol_matches[0] ) ) {
if ( 0 !== count( $ol_matches[0] ) ) {
$listtype = 'ol';
$list_item_pattern = $ol_pattern;
} else {
$listtype = 'ul';
$list_item_pattern = $ul_pattern;
}
$html .= '<' . $listtype . $itemprop . '>';
$html .= preg_replace( $list_item_pattern, $list_item_replacement, $content );
$html .= '' . $listtype . '>';
// Strip out any empty tags and stray )*\s*<\/p>/mi';
$html = preg_replace( $empty_p_pattern, '', $html );
} else {
$html .= do_shortcode( $content );
}
} else {
$html .= do_shortcode( $content );
}
// Return our formatted content.
return $html;
}
/**
* Our [recipe-directions] shortcode.
* Outputs directions, styled in a div.
*
* @param array $atts Array of shortcode attributes.
* @param string $content Post content.
*
* @return string HTML for recipe directions shortcode.
*/
public static function recipe_directions_shortcode( $atts, $content = '' ) {
$atts = shortcode_atts(
array(
'title' => esc_html_x( 'Directions', 'recipe', 'jetpack' ), // string.
),
$atts,
'recipe-directions'
);
$html = '' . esc_html( $atts['title'] ) . '
';
}
// Print the recipe meta if exists.
if (
'' !== $atts['servings']
|| '' !== $atts['time']
|| '' !== $atts['difficulty']
|| '' !== $atts['print']
) {
$html .= '';
}
// Output the image, if we have one.
if ( '' !== $atts['image'] ) {
$html .= sprintf(
'',
esc_url( $atts['image'] )
);
}
// Output the description, if we have one.
if ( '' !== $atts['description'] ) {
$html .= sprintf(
'
' . esc_html( $atts['title'] ) . '
';
}
$html .= '' . esc_html( $atts['title'] ) . '
';
}
// Format content using list functionality.
$html .= self::output_list_content( $content, 'ingredients' );
$html .= '
\n", "\n", $content );
$content = trim( $content );
$ul_pattern = '/(?:^|\n|\' . esc_html( $atts['title'] ) . '
';
}
// Format content using list functionality.
$html .= self::output_list_content( $content, 'directions' );
$html .= '