Created
January 27, 2023 20:51
-
-
Save epierpont/40758e2ed3663dc5b11629976644875b to your computer and use it in GitHub Desktop.
block examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The blocks class file. | |
* | |
* @package WordPress | |
* @subpackage Swift_News_Theme | |
*/ | |
namespace Swift; | |
class Blocks { | |
// store sidebar modules | |
protected static $sidebar_content = ''; | |
/** | |
* Constructor. | |
*/ | |
public function __construct() { | |
add_action( 'acf/init', array( $this, 'create_acf_blocks' ) ); | |
add_filter( 'block_categories_all', array( $this, 'create_block_categories'), 10, 2 ); | |
// If admin is loaded. | |
if ( is_admin() ) { | |
add_filter( 'allowed_block_types_all', array( $this, 'swift_allowed_block_types' ) ); | |
add_action( 'admin_menu', array( $this, 'edit_admin_menus' ) ); | |
add_filter( 'acf/load_field/name=swift_module_type', array( $this, 'load_acf_fields' ) ); | |
add_filter( 'acf/load_field/name=swift_article_gallery_source', array( $this, 'load_acf_custom_fields' ) ); | |
add_action( 'acf/save_post', array( $this, 'update_acf_fields', ), 20 ); | |
} | |
else { | |
add_filter( 'render_block', array( $this, 'edit_core_blocks' ), 10, 3); | |
add_filter( 'the_content', array( $this, 'inject_tooltip' ), 8 ); | |
} | |
} | |
/** | |
* Add long form block category. | |
* | |
* @param array $categories all existing block categories. | |
* @param object $post WP post object. | |
*/ | |
public function create_block_categories( $categories, $post ) { | |
if ( !empty($post->post_type) && $post->post_type !== 'post' ) { | |
return $categories; | |
} | |
return array_merge( | |
$categories, | |
array( | |
array( | |
'slug' => 'long-form', | |
'title' => __( 'Long Form', 'long-form' ) | |
), | |
) | |
); | |
} | |
/** | |
* Pull module categories and populate ACF module type select. | |
* | |
* @param array $field ACF fields. | |
*/ | |
function swift_allowed_block_types( $allowed_blocks ) { | |
return array( | |
// common | |
'core/audio', | |
'core/file', | |
'core/image', | |
'core/heading', | |
'core/list', | |
'core/paragraph', | |
// formatting | |
'core/html', | |
'core/pullquote', | |
'core/table', | |
'core/freeform', | |
// layout elements | |
'acf/accordion', | |
'acf/community-chat', | |
'acf/infobox', | |
'acf/infogram', | |
'acf/iframe', | |
'acf/live-blog', | |
'acf/map', | |
'acf/module', | |
'acf/jwplayer', | |
'acf/snippet', | |
'acf/swift-gallery', | |
'core/media-text', | |
'core/separator', | |
'core/text-columns', | |
//long form | |
'acf/wide-image', | |
'acf/scroll-chart', | |
// gravity forms | |
'gravityforms/form', | |
// misc | |
'core/block', // reusable | |
//'core/reusable', | |
//'core/template' | |
// embeds | |
'core/embed', | |
'core-embed/twitter', | |
'core-embed/youtube', | |
'core-embed/facebook', | |
'core-embed/instagram', | |
'core-embed/wordpress', | |
'core-embed/soundcloud', | |
'core-embed/spotify', | |
'core-embed/flickr', | |
'core-embed/vimeo', | |
'core-embed/imgur', | |
'core-embed/issuu', | |
'core-embed/photobucket', | |
'core-embed/polldaddy', | |
'core-embed/reddit', | |
'core-embed/screencast', | |
'core-embed/scribd', | |
'core-embed/slideshare', | |
'core-embed/smugmug', | |
'core-embed/ted', | |
'core-embed/tumblr' | |
); | |
} | |
/** | |
* Edit output of core/image block. | |
* | |
* @param array $attributes ["id"] of attachment. | |
* @param string $content native image content from WP. | |
*/ | |
public function edit_core_audio_callback( $attributes, $content ) { | |
// Get id and download link. | |
$media_id = $attributes['id']; | |
$media_download_link = wp_get_attachment_url( $media_id ); | |
// Remove origina download link and use one we can actually track. | |
$content = str_replace( '<audio', '<audio id="audio-player-' . $media_id . '" controlsList="nodownload"', $content ); | |
$content = $content . '<p id="audio-download-' . $media_id . '" class="audio-download"><a href="' . $media_download_link . '" download><svg class="svg-icon"><use class="fa-download" xlink:href="#fa-download"></use></svg> Download</a></p>'; | |
// Send custom events when played or downloaded. | |
$audio_tracking = ' | |
<script> | |
$(function() { | |
$(\'#audio-player-' . $media_id . '\').on(\'playing\', function() { | |
ga( \'send\', \'event\', \'audio\', \'play\' ); | |
ga( \'newTracker.send\', \'event\', \'audio\', \'play\' ); | |
}); | |
$( \'#audio-download-' . $media_id . '\' ).click(function() { | |
ga( \'send\', \'event\', \'audio\', \'download\' ); | |
ga( \'newTracker.send\', \'event\', \'audio\', \'download\' ); | |
}); | |
}); | |
</script>'; | |
return $content . $audio_tracking; | |
} | |
/** | |
* Edit output of Gutenberg core blocks. | |
* | |
* @param string $block_content content about to be appended. | |
* @param array $block full block, including name and attributes. | |
*/ | |
public function edit_core_blocks( $block_content, $block ) { | |
// Edit output of core/audio block. | |
if ( $block['blockName'] == 'core/audio' ) { | |
$media_id = $block['attrs']['id']; | |
if ( !empty( $media_id ) ) { | |
$content = $block_content; | |
$media_download_link = wp_get_attachment_url( $media_id ); | |
// Remove origina download link and use one we can actually track. | |
$content = str_replace( '<audio', '<audio id="audio-player-' . $media_id . '" controlsList="nodownload"', $content ); | |
$content = $content . '<p id="audio-download-' . $media_id . '" class="audio-download"><a href="' . $media_download_link . '" download><svg class="svg-icon"><use class="fa-download" xlink:href="#fa-download"></use></svg> Download</a></p>'; | |
// Send custom events when played or downloaded. | |
$audio_tracking = ' | |
<script> | |
$(function() { | |
$(\'#audio-player-' . $media_id . '\').on(\'playing\', function() { | |
ga( \'send\', \'event\', \'audio\', \'play\' ); | |
ga( \'newTracker.send\', \'event\', \'audio\', \'play\' ); | |
}); | |
$( \'#audio-download-' . $media_id . '\' ).click(function() { | |
ga( \'send\', \'event\', \'audio\', \'download\' ); | |
ga( \'newTracker.send\', \'event\', \'audio\', \'download\' ); | |
}); | |
}); | |
</script>'; | |
return $content . $audio_tracking; | |
} | |
} | |
// Edit output of core/image block. | |
if ( $block['blockName'] == 'core/image' ) { | |
$image_id = !empty( $block['attrs']['id'] ) ? $block['attrs']['id'] : ''; | |
if ( !empty( $image_id ) ) { | |
$content = $block_content; | |
// get src and title. | |
$image_src = wp_get_attachment_image_src( $image_id, 'large' ); | |
$image_src_large = $image_src[0]; | |
$image_src = wp_get_attachment_image_src( $image_id, 'full' ); | |
$image_src_full = $image_src[0]; | |
$title = get_the_title( $image_id ); | |
// check to see if we have an acceptable title, hide if image name. | |
$title = \Swift\Utils::get_image_extension( $title ) ? '' : $title; | |
// add buy photos link we conditionals are met in method. | |
$buy_content = \Swift\Single::get_mycapture_content( $image_src_large, $image_src_full ); | |
// figure out if we have a caption. | |
$caption_exists = strpos( $content, '<figcaption>' ) !== false ? true : false; | |
// add in strong tags to caption. | |
if ( !empty( $caption_exists ) ) { | |
$original_content = array('<figcaption>', '</figcaption>'); | |
$modified_content = array('<figcaption><strong>', '</strong></figcaption>'); | |
$content = str_replace( $original_content, $modified_content, $content ); | |
} | |
// add title after caption. | |
if ( !empty( $title ) && !empty( $caption_exists ) ) { | |
$content = str_replace( '</figcaption>', '<br /><em>' . $title . '</em>' . $buy_content . '</figcaption>', $content ); | |
} | |
// add title if we don't have caption. | |
if ( !empty( $title ) && empty( $caption_exists ) ) { | |
$content = str_replace( '</figure>', '<figcaption>' . $title . $buy_content . '</figcaption></figure>', $content ); | |
} | |
// add Buy Photo even if title or caption are empty and buy photo is enabled | |
if ( !empty( $buy_content ) && ( empty( $title ) && !empty( $caption_exists ) ) || ( empty( $title ) && empty( $caption_exists ) ) ) { | |
$content = str_replace( '</figcaption>', $buy_content . '</figcaption>', $content ); | |
} | |
// add spacing if we don't have caption or title. | |
if ( empty( $title ) && empty( $caption_exists && empty( $buy_content ) ) ) { | |
$content = $content . '<div class="mb-4"></div>'; | |
} | |
// figure out if we have a non-aligned image to work with, replace with new caption wrapper markup if so. | |
$nonaligned_exists = strpos( $content, '<figure class="wp-block-image size-' ) !== false ? true : false; | |
if ( !empty( $nonaligned_exists ) ) { | |
$content = str_replace(' is-resized','',$content); | |
$content = str_replace(' is-style-default','',$content); | |
// match figure tags with a class of wp-block-image and size-* | |
$original_wrapper = ['#(<figure class="wp-block-image\ssize-[a-z]+">)#','#</figure>#']; | |
$modified_wrapper = ['$1<div class="caption-container">','</div></figure>']; | |
$content = preg_replace( $original_wrapper, $modified_wrapper, $content ); | |
} | |
$content = '<div class="p402_hide">' . $content . '</div>'; | |
return $content; | |
} | |
} | |
return $block_content; | |
} | |
/** | |
* Pull module categories and populate ACF module type select. | |
* | |
* @param array $field ACF fields. | |
*/ | |
public function load_acf_fields( $field ) { | |
$field['choices'] = array(); | |
$term_args = array ( | |
'hide_empty' => 0 | |
); | |
$terms = get_terms( 'swift_module_category', $term_args ); | |
$choices = NULL; | |
foreach ( $terms as $term ) { | |
$choices .= $term->name; | |
$choices .= "\n"; | |
} | |
$choices = substr( $choices, 0, -1 ); | |
$choices = explode( "\n", $choices ); | |
$choices = array_map( 'trim', $choices ); | |
if ( is_array( $choices ) ) { | |
foreach( $choices as $choice ) { | |
$field['choices'][ $choice ] = $choice; | |
} | |
} | |
return $field; | |
} | |
/** | |
* Save ACF fields. | |
* | |
* @param int $post_id WP post ID. | |
*/ | |
public function update_acf_fields( $post_id ) { | |
// Save module type select as module category tax. | |
if ( get_post_type( $post_id ) == 'swift_module' ) { | |
$module_type = get_field( 'swift_module_type' ); | |
$module_type = strtolower( $module_type ); | |
$module_type = str_replace( ' ', '-', $module_type ); | |
$module_type_array = array( $module_type ); | |
wp_set_object_terms( $post_id, $module_type_array, 'swift_module_category' ); | |
} | |
} | |
/** | |
* Pull module swift-gallery and populate ACF text field wih image src path. | |
* | |
* @param array $field ACF swift_article_gallery_source field. | |
*/ | |
public function load_acf_custom_fields( $field ) { | |
$images = ( get_field( 'swift_article_gallery_block', false, true ) ); | |
if ( $images ) { | |
foreach ( $images as $image ) { | |
$id = $image['id']; | |
$all_images[ $id ] = $image['sizes']['large']; | |
error_log('acf - load_acf_custom_fields - foreach all_images: ' . print_r($all_images, true)); | |
} | |
$field[ 'value' ] = __( json_encode( $all_images, JSON_UNESCAPED_SLASHES ), 'txtdomain' ); | |
error_log('acf - load_acf_custom_fields - field after foreach: ' . print_r($field, true)); | |
$field = str_replace( array('{', '}'), array('<!-- wp:html {', '} /wp:html -->'), $field ); | |
error_log('acf - load_acf_custom_fields - field after str_replace: ' . print_r($field, true)); | |
} | |
else $field[ 'value' ] = ''; | |
return $field; | |
} | |
/** | |
* Create the ACF blocks for Gutenberg. | |
*/ | |
public function create_acf_blocks() { | |
if ( function_exists('acf_register_block_type') ) { | |
acf_register_block_type( array( | |
'name' => 'module', | |
'title' => __( 'Module' ), | |
'description' => __( 'A module block.' ), | |
'render_callback' => array( $this, 'render_module_callback' ), | |
'category' => 'layout', | |
'icon' => 'schedule', | |
'keywords' => array( 'module', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'infobox', | |
'title' => __( 'Infobox' ), | |
'description' => __( 'An infobox block.' ), | |
'render_callback' => array( $this, 'render_infobox_callback' ), | |
'category' => 'layout', | |
'icon' => 'info', | |
'keywords' => array( 'infobox', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'infogram', | |
'title' => __( 'Infogram' ), | |
'description' => __( 'An infogram block.' ), | |
'render_callback' => array( $this, 'render_infogram_callback' ), | |
'category' => 'layout', | |
'icon' => 'chart-area', | |
'keywords' => array( 'infogram', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'wide-image', | |
'title' => __( 'Wide Image' ), | |
'description' => __( 'A 100% width image block.' ), | |
'render_callback' => array( $this, 'render_wide_image_callback' ), | |
'category' => 'long-form', | |
'icon' => 'format-image', | |
'keywords' => array( 'image', 'wide', 'block' ), | |
'post_types' => array('post'), | |
)); | |
acf_register_block_type( array( | |
'name' => 'scroll-chart', | |
'title' => __( 'Scroll Chart' ), | |
'description' => __( 'Chart scroll with text' ), | |
'render_callback' => array( $this, 'render_scroll_chart_callback' ), | |
'category' => 'long-form', | |
'icon' => 'chart-bar', | |
'keywords' => array( 'chart', 'scroll', 'block' ), | |
'post_types' => array('post'), | |
)); | |
acf_register_block_type( array( | |
'name' => 'map', | |
'title' => __( 'Map' ), | |
'description' => __( 'A map block.' ), | |
'render_callback' => array( $this, 'render_map_callback' ), | |
'category' => 'layout', | |
'icon' => 'location-alt', | |
'keywords' => array( 'map', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'jwplayer', | |
'title' => __( 'Swift Video' ), | |
'description' => __( 'A Swift Video player block.' ), | |
'render_callback' => array( $this, 'render_swift_video_callback' ), | |
'category' => 'layout', | |
'icon' => 'video-alt3', | |
'keywords' => array( 'video', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'snippet', | |
'title' => __( 'Snippet' ), | |
'description' => __( 'A snippet block.' ), | |
'render_callback' => array( $this, 'render_snippet_callback' ), | |
'category' => 'layout', | |
'icon' => 'media-code', | |
'keywords' => array( 'snippet', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'swift-gallery', | |
'title' => __( 'Swift Gallery' ), | |
'description' => __( 'Swift Gallery block.' ), | |
'render_callback' => array( $this, 'render_gallery_callback' ), | |
'category' => 'layout', | |
'mode' => 'edit', | |
'icon' => 'format-gallery', | |
'keywords' => array( 'gallery', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'iframe', | |
'title' => __( 'Iframe' ), | |
'description' => __( 'An iframe block.' ), | |
'render_callback' => array( $this, 'render_iframe_callback' ), | |
'category' => 'embed', | |
'icon' => 'desktop', | |
'keywords' => array( 'iframe', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'accordion', | |
'title' => __( 'Accordion' ), | |
'description' => __( 'An accordion block.' ), | |
'render_callback' => array( $this, 'render_accordion_callback' ), | |
'category' => 'layout', | |
'icon' => 'sort', | |
'keywords' => array( 'accordion', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'community-chat', | |
'title' => __( 'Community Chat' ), | |
'description' => __( 'A community chat block.' ), | |
'render_callback' => array( $this, 'render_community_chat_callback' ), | |
'category' => 'layout', | |
'icon' => 'format-status', | |
'keywords' => array( 'community', 'chat', 'block' ), | |
)); | |
acf_register_block_type( array( | |
'name' => 'live-blog', | |
'title' => __( 'Live Blog' ), | |
'description' => __( 'A live blog block.' ), | |
'render_callback' => array( $this, 'render_live_blog_callback' ), | |
'category' => 'layout', | |
'icon' => 'groups', | |
'keywords' => array( 'live', 'blog', 'block' ), | |
)); | |
} | |
} | |
/** | |
* Render module callback for module block. | |
*/ | |
public function render_module_callback( $block ) { | |
global $pagenow; | |
$module_type = get_field( 'swift_module_type_select' ); | |
// return if --Select-- triggered | |
if ( empty( $module_type ) ) return; | |
$module = get_field( 'swift_module_type_' . $module_type['value'] ); | |
$module_id = !empty( $module->ID ) ? $module->ID : ''; | |
$module_cat_id = get_field( 'swift_module_type_category' ); | |
// If we are on the front-end, render module. | |
if ( !is_admin() || ( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$args = array( 'id' => $module_id, 'type' => $module_type ); | |
$content = !empty( $module_type ) ? $this->render_module( $args ) : false; | |
} | |
// If we are in the admin, show module data. | |
else { | |
if ( !empty( $module_type ) ) { | |
$module_type_display_array = ['html','ad','ad_card','e_edition','instagram','live_stream','promo','swift_app','video_player','video_stream']; | |
if ( in_array( $module_type['value'] , $module_type_display_array) ) { | |
$module_title = !empty( $module_id ) ? ' | ' . get_the_title( $module_id ) : ''; | |
$content = ' | |
<div class="swift-block ' . $module_type['value'] . '"> | |
<h2>' . $module_type['label'] . $module_title . '</h2> | |
</div>'; | |
} | |
else { | |
$promo_name = !empty( get_field( 'swift_module_type_promo' ) ) ? ' | ' . get_field( 'swift_module_type_promo' ) : ''; | |
$post_count = !empty( get_field( 'swift_module_type_num' ) ) ? ' | ' . get_field( 'swift_module_type_num' ) . ' Posts' : ''; | |
$cat_name = !empty( get_field( 'swift_module_type_category' ) ) ? ' | ' . get_cat_name( get_field( 'swift_module_type_category' ) ) : ''; | |
$tag_name = ''; | |
if ( !empty( get_field( 'swift_module_type_tag' ) ) ) { | |
$tag_object = get_tag( get_field( 'swift_module_type_tag' ) ); | |
$tag_name = ' | ' . $tag_object->name; | |
} | |
$content = ' | |
<div class="swift-block ' . $module_type['value'] . '"> | |
<h2>' . $module_type['label'] . $promo_name . $post_count . $cat_name . $tag_name . '</h2> | |
</div>'; | |
} | |
} | |
else { | |
$content = '<div class="swift-block"><h2>Please select Module Type...</h2></div>'; | |
} | |
} | |
echo $content; | |
} | |
/** | |
* Render module block. | |
* | |
* @param int $module_id module ID. | |
*/ | |
public function render_module( $args ) { | |
$module_id = $args['id']; | |
$sidebar = !empty( $args['sidebar'] ) ? $args['sidebar'] : false; | |
// If we are not dealing with the sidebar. | |
if ( empty( $sidebar ) ) { | |
$module_type = $args['type']['value']; | |
} | |
// If we have a sidebar. | |
else { | |
$module_type = $args['type']; | |
} | |
if ( 'ad' == $module_type ) { | |
//$ad_slot = get_post_meta( $module_id, 'swift_module_ad_slot', true ); | |
$ad = get_post_meta( $module_id, 'swift_module_html_code', true ); | |
$ad_slug = get_post_field( 'post_name', $module_id ); | |
$ad_class = $ad_slug == 'ad-top' || $ad_slug == 'ad-bottom' || $ad_slug == 'ad-parallax' || $ad_slug == 'ad-parallax2' || $ad_slug == 'ad-parallax3' || !empty( $sidebar ) ? 'col-sm-12' : 'col-md-6 col-lg-4'; | |
$content = ' | |
<div class="card-item ' . $ad_class . ' text-center"> | |
<div class="module-ad ' . $ad_slug . '"> | |
<div class="swift-ad-wrapper-outer ' . $ad_slug . '-wrapper' . '"> | |
<div class="swift-ad-wrapper-inner"> | |
<div id="' . $ad_slug . '" class="dfp"> | |
<script> | |
AdBridg.cmd.push(function() { | |
AdBridg.display(\'' . $ad_slug . '\'); | |
}) | |
</script> | |
</div> | |
<a href="/your-ad-here/" class="your-ad-here" target="_blank" title="Your Ad Here">YOUR AD HERE »</a> | |
</div> | |
</div> | |
</div> | |
</div>'; | |
} | |
else { | |
$args = array( 'id' => $module_id, 'type' => $module_type, 'sidebar' => $sidebar ); | |
$card_class = new \Swift\Cards( $args ); | |
$blocks_content_filter_current_priority = has_filter( 'the_content', '_restore_wpautop_hook' ); | |
if ( $blocks_content_filter_current_priority ) { | |
remove_filter( 'the_content', '_restore_wpautop_hook', $blocks_content_filter_current_priority ); | |
} | |
$content = $card_class->render_card(); | |
if ( $blocks_content_filter_current_priority ) { | |
add_filter( 'the_content', '_restore_wpautop_hook', $blocks_content_filter_current_priority ); | |
} | |
} | |
return $content; | |
} | |
/** | |
* Get sidebar modules. | |
*/ | |
public static function get_sidebar_modules() { | |
// generate sidebar modules if not previously generated and stored | |
if ( empty( self::$sidebar_content ) ) { | |
$content = $sidebar_content = ''; | |
$utils_class = new \Swift\Utils(); | |
$page_ids = $utils_class->get_page_parent_ids(); | |
if ( 'swift_wrapper' == get_post_type() ) { | |
$sidebar_content = get_field( 'swift_page_sidebar_modules', get_the_ID() ); | |
} | |
else { | |
// Loop through parent pages, look for sidebar modules, stop if content is found. | |
foreach( $page_ids as $page_id ) { | |
if ( !empty( get_field( 'swift_page_sidebar_modules', $page_id ) ) ) { | |
$sidebar_content = get_field( 'swift_page_sidebar_modules', $page_id ); | |
break; | |
} | |
} | |
} | |
// Fallback to news front page if nothing is available. | |
if ( empty( $sidebar_content ) ) { | |
$sidebar_fallback = get_field( 'swift_page_sidebar_modules', get_option( 'page_on_front' ) ); | |
$sidebar_content = !empty( $sidebar_fallback ) ? $sidebar_fallback : ''; | |
} | |
self::$sidebar_content = $sidebar_content; | |
} | |
return self::$sidebar_content; | |
} | |
/** | |
* Render modules for sidebar. | |
*/ | |
public function render_sidebar_modules() { | |
$content = $sidebar_content = ''; | |
$sidebar_content = self::get_sidebar_modules(); | |
$size = 'col-sm-12'; | |
$sidebar = 1; | |
if ( empty( !$sidebar_content ) ) { | |
foreach ( $sidebar_content as $module ) { | |
$module_type = $module['swift_sidebar_type_select']; | |
// if this is an ad and obits_redirect parameter isset, then return | |
if ( $module_type == 'ad' && defined( 'SWIFT_FB_OBITS_REDIRECT' ) ) { | |
$content .= ''; | |
} | |
else { | |
$module_object = !empty( $module['swift_sidebar_type_' . $module_type] ) ? $module['swift_sidebar_type_' . $module_type] : ''; | |
$module_id = !empty( $module_object->ID ) ? $module_object->ID : NULL; | |
// We only need these when dealing with sidebar content. | |
$sidebar_array = array( | |
'category' => $module['swift_sidebar_type_category'], | |
'tag' => $module['swift_sidebar_type_tag'], | |
); | |
$args = array( 'id' => $module_id, 'type' => $module_type, 'sidebar' => $sidebar_array ); | |
$content .= $this->render_module( $args ); | |
} | |
} | |
return $content; | |
} | |
} | |
/** | |
* Render module callback for module block. | |
*/ | |
public function render_infobox_callback( $block ) { | |
global $pagenow; | |
$heading_content = get_field( 'swift_infobox_heading' ); | |
$heading_content = '<svg class="svg-icon"><use class="fa-info-circle" xlink:href="#fa-info-circle"></use></svg> ' . $heading_content; | |
$editor_content = get_field( 'swift_infobox_editor' ); | |
$align_class = $block['align'] ? 'align' . $block['align'] : ''; | |
if ( is_admin() && !( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$content = ' | |
<div class="swift-infobox ' . $align_class . '"> | |
<h4 class="heading">' . $heading_content . '</h4> | |
<div class="content"> | |
' . $editor_content . ' | |
</div> | |
</div>'; | |
} | |
else { | |
$content = ' | |
<div class="swift-infobox p402_hide ' . $align_class . '"> | |
<div class="module module-promo card shadow-sm"> | |
<div class="card-header">' . $heading_content . '</div> | |
<div class="card-body"> | |
' . $editor_content . ' | |
</div><!-- END .card-body --> | |
</div><!-- END .module --> | |
</div><!-- END .swift-infobox -->'; | |
} | |
echo $content; | |
} | |
public function render_infogram_callback( $block ) { | |
global $swift_utils, $pagenow; | |
$share_url = get_field( 'swift_infogram_share_link' ); | |
if ( is_admin() && !( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$content = ' | |
<div class="swift-block infogram" style="width: 100%;"> | |
<h2>Infogram</h2> | |
</div>'; | |
} | |
else { | |
$oembed_url = 'https://infogram.com/oembed?url=' . $share_url; | |
$oembed_feed = $swift_utils->get_remote_data( $oembed_url, array( 'timeout' => 60 ) ); | |
$oembed_array = json_decode( $oembed_feed, true ); | |
$content = '<p>' . $oembed_array['html'] . '</p>'; | |
} | |
echo $content; | |
} | |
/** | |
* Render module callback for long form wide image. | |
*/ | |
public function render_wide_image_callback( $block ) { | |
global $pagenow; | |
$image_id = get_field( 'swift_wide_image' ); | |
$parallax_mode = get_field( 'swift_wide_image_parallax' ); | |
$image_caption = wp_get_attachment_caption( $image_id ); | |
$wide_image_src_large = wp_get_attachment_image_src( $image_id, 'large' ); | |
$wide_image = wp_get_attachment_image( $image_id, 'full', '', array( "class" => "img-$image_id" ) ); | |
$wide_image_src_full = wp_get_attachment_image_src( $image_id, 'full' ); | |
if ( is_admin() && !( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$content = ' | |
<figure class="wp-block-image wide-image"> | |
' . $wide_image . ' | |
</figure>'; | |
} | |
else { | |
$content = ' | |
</div><!-- END .container --> | |
<figure class="wp-block-image wide-image"> | |
<h4>' . $image_caption . '</h4> | |
' . $wide_image . ' | |
</figure>'; | |
if ( !empty( $parallax_mode ) ) { | |
$content .= ' | |
<script type=\'text/javascript\'> | |
var image = document.getElementsByClassName("img-' . $image_id . '"); | |
new simpleParallax(image, { | |
delay: .75, | |
}); | |
</script>'; | |
} | |
$content .= '<div class="container">'; | |
$content .= '<div class="buy-link-wide">' . \Swift\Single::get_mycapture_content( $wide_image_src_large[0], $wide_image_src_full[0] ) . '</div>'; | |
} | |
echo $content; | |
} | |
/** | |
* Render module callback for scroll charts. | |
*/ | |
public function render_scroll_chart_callback( $block ) { | |
global $pagenow; | |
$scroll_image_id = get_field( 'swift_chart_image' ); | |
$scroll_image_src = wp_get_attachment_image_src( $scroll_image_id ); | |
$scroll_copy = get_field( 'swift_chart_copy' ); | |
if ( is_admin() && !( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$content = ' | |
<figure class="wp-block-image scroll-image"> | |
<img src="' . $scroll_image_src[0] . '" style="width: 100%; height: auto;" /> | |
</figure> | |
' . $scroll_copy . ''; | |
} | |
else { | |
$content = ' | |
<section> | |
<figure class="wp-block-image scroll-image scroll-image-' . $scroll_image_id . '"> | |
<img src="' . $scroll_image_src[0] . '" /> | |
</figure> | |
<div class="scroll-copy"> | |
' . $scroll_copy . ' | |
</div> | |
</section> | |
<script> | |
jQuery( document ).ready( function($) { | |
var scrollImg = $(\'.scroll-image-' . $scroll_image_id . '\'), | |
didScroll = false; | |
$(window).scroll(function() { | |
didScroll = true; | |
}); | |
setInterval(function() { | |
if ( didScroll ) { | |
didScroll = false; | |
var distance = scrollImg.offset().top, | |
$window = $(window); | |
if ( $window.scrollTop() >= distance ) { | |
scrollImg.css(\'opacity\', \'0.15\'); | |
} | |
if ( $window.scrollTop() < distance ) { | |
scrollImg.css(\'opacity\', \'1\'); | |
} | |
} | |
}, 250); | |
}); | |
</script>'; | |
} | |
echo $content; | |
} | |
/** | |
* Render module callback for map block. | |
*/ | |
public function render_map_callback( $block ) { | |
global $pagenow; | |
$lat = !empty( get_field( 'swift_map_latitude' ) ) ? get_field( 'swift_map_latitude' ) : '39.1637984'; | |
$lng = !empty( get_field( 'swift_map_longitude' ) ) ? get_field( 'swift_map_longitude' ) : '-119.7674034'; | |
$height = !empty( get_field( 'swift_map_height' ) ) ? get_field( 'swift_map_height' ) : '400'; | |
$zoom = !empty( get_field( 'swift_map_zoom' ) ) ? get_field( 'swift_map_zoom' ) : '13'; | |
$link = !empty( get_field( 'swift_map_link' ) ) ? '<button type="submit" class="btn btn-primary map-button"><a href="' . get_field( 'swift_map_link' ) . '"><i class="fas fa-map-marker-alt"></i> More Info</a></button>' : ''; | |
$map_disable_ui = false; | |
$uniq_id = uniqid(); | |
$align_class = $block['align'] ? 'align' . $block['align'] : ''; | |
// If we are dealing with long form, add custom parameters. | |
$long_form_status = !empty( get_field( 'swift_map_copy' ) ) && 'single-swift_long_form.php' == get_page_template_slug() ? true : false; | |
$copy = $copy_markup = $scroll_container_start = $scroll_container_end = $scroll_script = ''; | |
if ( !empty( $long_form_status ) ) { | |
$copy = get_field( 'swift_map_copy' ); | |
$copy_markup = !empty( $copy ) ? '<div class="scroll-copy">' . $copy . '</div>' : ''; | |
$height = 400; | |
$map_disable_ui = true; | |
$scroll_container_start = '<div class="scroll-image scroll-image-' . $uniq_id . '">'; | |
$scroll_container_end = '</div>'; | |
$scroll_script = ' | |
<script> | |
jQuery( document ).ready( function($) { | |
var scrollMap = $(\'.scroll-image-' . $uniq_id . '\'), | |
didScroll = false; | |
$(window).scroll(function() { | |
didScroll = true; | |
}); | |
setInterval(function() { | |
if ( didScroll ) { | |
didScroll = false; | |
var distance = scrollMap.offset().top, | |
$window = $(window); | |
if ( $window.scrollTop() >= distance ) { | |
scrollMap.css(\'opacity\', \'0.15\'); | |
} | |
if ( $window.scrollTop() < distance ) { | |
scrollMap.css(\'opacity\', \'1\'); | |
} | |
} | |
}, 250); | |
}); | |
</script>'; | |
} | |
$layer = !empty( get_field( 'swift_map_layer' ) ) ? get_field( 'swift_map_layer' ) : ''; | |
if ( !empty( $layer ) ) { | |
if ( $layer == 'traffic' ) { | |
$layer = 'var trafficLayer = new google.maps.TrafficLayer(); trafficLayer.setMap(map);'; | |
} | |
if ( $layer == 'bicycle' ) { | |
$layer = 'var bikeLayer = new google.maps.BicyclingLayer(); bikeLayer.setMap(map);'; | |
} | |
if ( $layer == 'transit' ) { | |
$layer = 'var transitLayer = new google.maps.TransitLayer(); transitLayer.setMap(map);'; | |
} | |
} | |
if ( is_admin() && !( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$content = ' | |
<div class="swift-block map" style="width: 100%;"> | |
<h2>Google Map</h2> | |
</div>'; | |
} else { | |
$content = ' | |
<div class="swift-map-container ' . $align_class . '"> | |
' . $scroll_container_start . ' | |
<div id="swift-map-' . $uniq_id . '" class="swift-map p402_hide" style="height: ' . $height . 'px;"></div> | |
' . $scroll_container_end . ' | |
' . $link .' | |
'. $copy_markup .' | |
</div> | |
' . $scroll_script . ' | |
<script> | |
function initMap() { | |
var map = new google.maps.Map(document.getElementById(\'swift-map-' . $uniq_id . '\'), { | |
zoom: ' . $zoom . ', | |
disableDefaultUI: \'' . $map_disable_ui . '\', | |
center: {lat: ' . $lat . ', lng: ' . $lng . '} | |
}); | |
var marker = new google.maps.Marker({ | |
position: {lat: ' . $lat . ', lng: ' . $lng . '}, | |
map: map, | |
}); | |
' . $layer . ' | |
} | |
</script> | |
<script async defer | |
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBnHd35E_1623OOspAmsCUIKl8uHu8q2x8&callback=initMap"> | |
</script>'; | |
} | |
echo $content; | |
} | |
/** | |
* Render module callback for map block. | |
*/ | |
public function render_swift_video_callback( $block ) { | |
$media_id = get_field( 'swift_video_media_id' ); | |
$uniq_id = '-' . uniqid(); | |
$ourhost = rawurlencode($_SERVER['HTTP_HOST']); | |
$ourref = !empty($_SERVER['HTTP_REFERER']) ? rawurlencode($_SERVER['HTTP_REFERER']) : ''; | |
$embed_url = "https://player.swiftcom.com?v=$media_id&refurl=$ourref&refhost=$ourhost"; | |
// Video player markup. | |
$content = ' | |
<div class="swift-video-player" id="vid-it-' . $uniq_id . '"> | |
<div style="position: relative; padding-bottom: 56.25%; height: 0px; width: 100%; box-sizing: content-box;"> | |
<iframe allowfullscreen allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" scrolling="no" src="' . $embed_url . '" style="border: 0; width: 100%; height: 100%; position: absolute; display: block;"></iframe> | |
</div> | |
</div>'; | |
echo $content; | |
} | |
/** | |
* Render module callback for snippet block. | |
*/ | |
public function render_snippet_callback() { | |
global $pagenow; | |
$snippet = get_field( 'swift_article_snippet_block' ); | |
$content = get_post_field('post_content', $snippet->ID); | |
//$width = get_field( 'swift_article_snippet_block_width' ); | |
if ( !is_admin() || ( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
echo wpautop( $content ); | |
} | |
else { | |
if ( empty( $snippet ) ) { | |
echo '<div class="swift-block"><h2>Please select Snippet...</h2></div>'; | |
} | |
else { | |
echo ' | |
<div class="swift-block"> | |
<h2>Snippet | ' . $snippet->post_title . '</h2> | |
</div>'; | |
} | |
} | |
} | |
/** | |
* Render module callback for gallery block. | |
*/ | |
public function render_gallery_callback( $block ) { | |
global $post, $pagenow; | |
$gallery_type = get_field( 'swift_article_gallery_type', false, false ); | |
$gallery_type = !empty( $gallery_type ) ? $gallery_type : 'carousel'; | |
// If we are on the front-end, render module. | |
if ( !is_admin() || ( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
\Swift\Single::render_gallery( $gallery_type, $block ); | |
} | |
// admin view | |
else { | |
echo '<div class="swift-block"> | |
<h2>Swift Gallery | ' . $gallery_type . '</h2> | |
</div>'; | |
} | |
} | |
/** | |
* Render module callback for accordion block. | |
*/ | |
public function render_accordion_callback( $block ) { | |
global $pagenow; | |
$title = get_field( 'swift_article_accordion_title' ); | |
$content = get_field( 'swift_article_accordion_content' ); | |
// If we are on the front-end, render module. | |
if ( !is_admin() || ( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
\Swift\Single::render_accordion( $title, $content, $block['id'] ); | |
} | |
// Admin view. | |
else { | |
echo '<div class="swift-block accordion"> | |
<h2>Accordion | ' . $title . '</h2> | |
</div>'; | |
} | |
} | |
/** | |
* Render module callback for community chat. | |
*/ | |
public function render_community_chat_callback( $block ) { | |
global $pagenow; | |
// If we are on the front-end, render module. | |
if ( !is_admin() || ( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$unique_id = get_post_field( 'post_name', get_the_ID() ); | |
$content = ' | |
<div id="livechat-wrapper" style="height: 550px;" class="viafoura mb-3"> | |
<vf-livechat vf-container-id="' . $unique_id . '"></vf-livechat> | |
</div>'; | |
echo $content; | |
} | |
// Admin view. | |
else { | |
echo ' | |
<div class="swift-block community-chat"> | |
<h2>Community Chat</h2> | |
</div>'; | |
} | |
} | |
/** | |
* Render module callback for live blog. | |
*/ | |
public function render_live_blog_callback( $block ) { | |
global $swift_site_settings, $pagenow; | |
// If we are on the front-end, render module. | |
if ( !is_admin() || ( $pagenow == 'admin.php' && $_REQUEST['page'] == 'swift-export' ) ) { | |
$unique_id = get_post_field( 'post_name', get_the_ID() ); | |
$ad_unit = '/5195/' . $swift_site_settings['ad_code'] . '/' . (new \Swift\Ads)->get_ad_unit(); | |
$content = ' | |
<vf-livestory promo-interval="2" first-promo-position="2" vf-container-id="' . $swift_site_settings['pub_name'] . '-liveblog-' . $unique_id . '" class="viafoura"></vf-livestory>'; | |
$content .= ' | |
<style>.vf-promo__label{left:15px !important;}</style> | |
<script> | |
window.vfQ = window.vfQ || []; | |
var vf_adctr = 0; | |
window.vfQ.push(function() { | |
// request ad on each vf requestLiveblogAd event | |
window.vf.$subscribe(\'vf-ads\', \'requestLiveblogAd\', function(vf_div_id) { | |
if (++vf_adctr > 10) { return; } | |
// inject adbridg div into each vf promo container | |
var vf_slot_div_id = "ad-comment-" + vf_adctr; | |
var vf_ad_container = document.getElementById(vf_div_id); | |
vf_ad_container.innerHTML = \'<div id="\' + vf_slot_div_id + \'" class="dfp"></div>\'; | |
AdBridg.cmd.push(function() { | |
var vf_gpt_slot = AdBridg.defineSlot(\'' . $ad_unit . '\', [320,50], vf_slot_div_id) | |
.setTargeting(\'slot\', vf_slot_div_id); | |
var vf_size_mapping = AdBridg.sizeMapping() | |
.addSize([0,0], [320,50]) | |
.addSize([1024,0], [728,90]) | |
.build(); | |
AdBridg.useSizeMapping(vf_gpt_slot, vf_size_mapping); | |
AdBridg.display(vf_slot_div_id); | |
AdBridg.serve(); | |
}); | |
// set ad vf-promo div/container to display:block | |
var el = document.getElementById(vf_div_id); | |
var r1 = el.closest(\'.vf-promo\'); | |
if (typeof r1 !== undefined) r1.style.display = "block"; | |
}); | |
}); | |
</script> | |
'; | |
echo $content; | |
} | |
// Admin view. | |
else { | |
echo ' | |
<div class="swift-block live_blog"> | |
<h2>Live Blog</h2> | |
</div>'; | |
} | |
} | |
/** | |
* Render module callback for iframe block. | |
*/ | |
public function render_iframe_callback( $block ) { | |
$src = get_field( 'swift_iframe_src' ); | |
$width = !empty( get_field( 'swift_iframe_width' ) ) ? get_field( 'swift_iframe_width' ) : '400'; | |
$width = str_replace( 'px', '', $width ); | |
$height = !empty( get_field( 'swift_iframe_height' ) ) ? get_field( 'swift_iframe_height' ) : '300'; | |
$height = str_replace( 'px', '', $height ); | |
$content = ' | |
<div class="swift-iframe"> | |
<iframe src="' . $src . '" width="' . $width . 'px" height="' . $height . 'px"></iframe> | |
</div>'; | |
echo $content; | |
} | |
/** | |
* Render legacy gallery | |
*/ | |
public function render_gallery_legacy( $post_id ) { | |
echo '<p>Legacy Gallery...</p>'; | |
} | |
/** | |
* Inject tooltip into the_content. | |
* | |
* @param string $content post content | |
*/ | |
public function inject_tooltip( $content ) { | |
if ( is_singular( 'post' ) && is_main_query() ) { | |
// Find if tooltip is present. | |
if ( strpos( $content, '<span class="swift-tooltip">' ) !== false ) { | |
preg_match_all('/<span class="swift-tooltip">(.*)<\/span>/', $content, $tooltip_data, PREG_SET_ORDER ); | |
// Loop through each result and create proper tooltip. | |
foreach ( $tooltip_data as $data ) { | |
$original_content = '<span class="swift-tooltip">' . $data[1] . '</span>'; | |
$modified_content = '<span class="swift-tooltip" data-toggle="tooltip" data-placement="top" title="' . $data[1] . '"><svg class="svg-icon"><use class="fa-info-circle" xlink:href="#fa-info-circle"></use></svg></span>'; | |
$content = str_replace( $original_content, $modified_content, $content ); | |
} | |
} | |
} | |
return $content; | |
} | |
/** | |
* Edit admin menu and submenu options. | |
*/ | |
public function edit_admin_menus() { | |
// Remove widget category and slug from cpt edit screen. | |
remove_meta_box( 'swift_module_categorydiv', 'swift_module', 'side' ); | |
remove_meta_box( 'slugdiv', 'swift_module', 'advanced' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment