Skip to content

Instantly share code, notes, and snippets.

@Auke1810
Created December 24, 2022 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Auke1810/06a6778bb8f03c600c03d4ab503713d9 to your computer and use it in GitHub Desktop.
Save Auke1810/06a6778bb8f03c600c03d4ab503713d9 to your computer and use it in GitHub Desktop.
Woocommerce product feed manager snippet so the Shortcoder plugin is supported (https://wordpress.org/plugins/shortcoder/)
function wppfm_shortcoder_support( $attributes, $feed_id, $product_id ) {
if ( has_custom_field( $attributes['description'] ) ) {
$description = $attributes['description'];
$attr = [ 'name' => get_shortcode_name_from_description( $description ) ];
//parse shortcodes do_shortcode
$attributes['description'] = process_product_shortcode( $attr, $description, $product_id );
}
// IMPORTANT! Always return the $attributes
return $attributes;
}
add_filter( 'wppfm_feed_item_value', 'wppfm_shortcoder_support', 10, 3 );
function has_custom_field( $description ) {
if ( ! is_plugin_active( 'shortcoder/shortcoder.php' ) ) {
return false;
}
$shortcode_pattern = '@\[([^<>&/\[\]\x00-\x20=]++)@';
preg_match_all($shortcode_pattern, $description, $has_custom_field );
return !empty( $has_custom_field[0] );
}
function get_shortcode_name_from_description( $description ) {
if ( has_custom_field( $description ) ) {
$offset = stripos( $description, '[sc name="' ) + 10;
$length = stripos( $description, '"sc]', $offset ) - 7;
return substr( $description, $offset, $length );
} else {
return null;
}
}
function process_product_shortcode( $atts, $description, $product_id ) {
if ( ! has_custom_field( $description ) ) {
return $description;
}
$shortcoder = new Shortcoder();
$atts = (array) $atts;
$shortcodes = $shortcoder::get_shortcodes(); // Gets the available shortcodes defined in the Shortcoder plugin and put them in an array
if( empty( $shortcodes ) ){
return '<!-- No shortcodes are defined -->';
}
$shortcode = $shortcoder::find_shortcode( $atts, $shortcodes ); // Gets one of the available short codes, selected by the $atts variable
$shortcode = apply_filters( 'sc_mod_shortcode', $shortcode, $atts, $description );
if( !is_array( $shortcode ) ){
return $shortcode;
}
$sc_content = $shortcode[ 'content' ];
$sc_content = replace_custom_fields( $sc_content, $product_id ); // Fills in the custom fields in the shortcode content
return preg_replace( '(\[sc.*?\/sc\])', $sc_content, $description); // Replace the shortcode in the description and return the result
}
function replace_custom_fields( $content, $product_id ){
$product = wc_get_product( $product_id );
preg_match_all('/\$\$[^\s^$]+\$\$/', $content, $matches );
$cf_tags = $matches[0];
if( empty( $cf_tags ) ){
return $content;
}
foreach( $cf_tags as $tag ){
if( strpos( $tag, 'custom_field:' ) === false ){
continue;
}
preg_match( '/:[^\s\$]+/', $tag, $match );
if( empty( $match ) ){
continue;
}
$match = substr( $match[0], 1 );
$value = $product->get_meta($match);
$content = str_replace( $tag, $value, $content );
}
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment