$value )
$message = preg_replace( sprintf( '/\[%s\]?/', $key ), $value, $message );
return $message;
}
function mega_is_tablet()
{
if ( !empty( $_SERVER['HTTP_USER_AGENT'] ) && false !== strpos( $_SERVER['HTTP_USER_AGENT'], 'iPad' ))
return true;
return false;
}
function mega_is_mobile()
{
if ( mega_is_tablet() )
return false;
return wp_is_mobile();
}
function mega_thumb( $args = array(), $r = false )
{
$thumb = mega_get_thumb( $args );
extract( $thumb );
$class = 'thumb ' . implode( ' ', $thumb['class'] );
if ( empty( $thumb['custom'] ))
{
$title = get_the_title();
$alt = $title;
}
if ( empty( $thumb['src'] ))
return '';
$thumb = mega_auto_tag( 'img', array( 'attr' => array(
'class' => $class,
'src' => isset( $thumb['phgo'] ) ? esc_attr( $src ) : esc_url( $src ),
'title' => isset( $title ) ? $title : '',
'alt' => empty( $alt ) ? ' ' : $alt,
'style' => 'width: ' . $width . ';height: ' . $height . ';',
'data-max-height' => $maxheight
)));
if ( function_exists( 'lazyload_images_add_placeholders' ))
$thumb = lazyload_images_add_placeholders( $thumb );
else if ( has_filter( 'bj_lazy_load_html' ))
$thumb = apply_filters( 'bj_lazy_load_html', $thumb );
if ( !$r )
echo $thumb;
else
return $thumb;
}
function mega_get_thumb( $args = array() )
{
$defaults = array(
'width' => '100%',
'height' => '',//100%
'maxheight' => false,
'custom' => false,//''
'class' => array(),
'placeholder' => 'holder.js/%1$sx%2$s?bg=' . ( isset( $args['ph_color'] ) ? $args['ph_color'] : 'f9f9f9' ),
'sources' => array( 'custom' => 1, 'featured' => 1, 'first' => 1, 'ph' => 1 )
);
$args['sources'] = wp_parse_args( $args['sources'], array( 'custom' => 0, 'featured' => 0, 'first' => 0, 'ph' => 0 ));
$args = wp_parse_args( $args, $defaults );
extract( $args );
$src = '';
if ( !empty( $custom ) && $custom )//$sources['custom']
{
$src = $custom;
}
elseif ( has_post_thumbnail() && $sources['featured'] )
{
$src = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_id() ), 'full', false );
$src = $src[0];
}
elseif ( $custom = get_post_meta( get_the_id(), 'thumbnail', true ))
{
$src = $custom;
}
elseif ( substr_count( get_the_content(), 'loadHTML( get_the_content() );
libxml_use_internal_errors($internalErrors);
$images = simplexml_import_dom( $doc )->xpath( '//img' );
$img = $images[0];
$src = $img['src'];
}
elseif ( $sources['ph'] )
{
$phwidth = $width;
$phheight = $height;
if ( preg_match( '/px/', $width ))
$phwidth = preg_replace( '/px/', '', $width );
if ( preg_match( '/%/', $width ))
$phwidth = preg_replace( '/%/', 'p', $width );
if ( preg_match( '/px/', $height ))
$phheight = preg_replace( '/px/', '', $height );
if ( preg_match( '/%/', $height ))
$phheight = preg_replace( '/%/', 'p', $height );
if ( $maxheight )
{
$phheight = $maxheight;
if ( preg_match( '/px/', $phheight ))
$phheight = preg_replace( '/px/', '', $phheight );
}
$args['phgo'] = 1;
$src = sprintf( $placeholder, $phwidth, ( $phheight == 'auto' ? 250 : $phheight ));
}
return wp_parse_args( array( 'src' => $src ), $args );
}
function mega_register_block( $block_class )
{
global $mega_registered_blocks, $mega_universal_blocks;
if ( isset( $mega_registered_blocks[$block_class] ))
return false;
//check if block_class is a class?
$block = new $block_class();
//$block->init_filters();
if ( $block->mega['args']['universal'] )
{
$mega_universal_blocks[] = $block_class;
register_widget( $block_class );
}
//do $block->mega['id_base']
$mega_registered_blocks[$block_class] = $block;
}
function mega_unregister_block( $block_class )
{
global $mega_registered_blocks;
if ( isset( $mega_registered_blocks[$block_class] ))
unset( $mega_registered_blocks[$block_class] );
$block_class = strtolower($block_class);
foreach($aq_registered_blocks as $block) {
if($block->id_base == $block_class)
unset( $mega_registered_blocks[$block_class] );
}
}
function mega_remote_data( $key )
{
$transient = get_template() . '_remote_data';
if ( get_transient( $transient ) === false )
set_transient(
$transient,
json_decode( wp_remote_retrieve_body( wp_remote_get( 'http://www.megathemes.com/api.json' )), true ),
3600
);
$data = get_transient( $transient );
if ( isset( $data[$key] ) && !empty( $data[$key] ))
return $data[$key];
return false;
}
function mega_auto_tag( $tag, array $args )
{
$args = wp_parse_args( $args, array(
'text' => '',
'attr' => array(),
'bypass' => true
));
extract( $args );
$attr_mapping = array(
'h' => array(//global
'id' => true,
'class' => true,
'style' => true
),
'a' => array(
'id' => true,
'class' => true,
'style' => true,
'href' => true,
'rel' => true,
'target' => '_blank',
'title' => true
),
'img' => array(
'id' => true,
'class' => true,
'style' => true,
'width' => true,
'height' => true,
'src' => true,
'data-src' => true,
'data-max-height' => true,
'title' => true,
'alt' => true
)
);
if ( $tag !== 'img' && empty( $text ))
return;
switch( $tag )
{
case 'h1' : case 'h2' : case 'h3' : case 'h4' : case 'h5' : case 'h6' : case 'div' :
$attr = array_intersect_key( (array) $attr, $attr_mapping['h'] );
return "<$tag" . mega_auto_attr( $attr ) . ">$text$tag>";
case 'img' :
$attr = array_intersect_key( (array) $attr, $attr_mapping['img'] );
if ( isset( $attr['src'] ))
$attr['src'] = esc_attr( $attr['src'] );
if ( empty( $attr['alt'] ))
$attr['alt'] = ' ';
return "<$tag" . mega_auto_attr( $attr ) . " />";
case 'a' :
$attr = array_intersect_key( (array) $attr, $attr_mapping[$tag] );
//$class = isset( $class ) ? ' class="' . $class . '" ' : '';
if ( empty( $attr['href'] ) && $bypass )
return $text;
$attr['href'] = esc_url( apply_filters( 'mega_external_links', $attr['href'] ));
return '$text";
//return empty( $attr['href'] ) ? $text : '' . $text . '';
}
}
function mega_auto_attr( array $attrs )
{
$r = '';
foreach( $attrs as $attr => $value )
if ( !empty( $value ))
$r .= ' ' . $attr . '="' . esc_attr( $value ) . '" ';
return $r;//implode by space
}
function mega_option( $key = '', $echo = false )
{
global $mega_registered_blocks;
$settings_slug = wp_get_theme()->get( 'Name' ) . '_Settings';
if ( is_child_theme() && !isset( $mega_registered_blocks[$settings_slug]->mega['settings'][$key] ))
$settings_slug = wp_get_theme( wp_get_theme()->get( 'Template' ))->get( 'Name' ) . '_Settings';
if ( isset( $mega_registered_blocks[$settings_slug]->mega['settings'][$key] ))
$output = $mega_registered_blocks[$settings_slug]->mega['settings'][$key];
else
$output = false;
if ( $echo == true )
echo $output;
else
return $output;
}
function mega_get_db()
{
return get_option( MEGA_DB_ID );
}
function mega_update_db( $mega_db )
{
return update_option( MEGA_DB_ID, $mega_db );
}
function mega_delete_db()
{
return delete_option( MEGA_DB_ID );
}
function mega_set_package( $package = '0', $override = false, $files = false )
{
global $mega_package, $mega_package_files;
$package_data = get_option( MEGA_DB_ID . '_package' );
if ( !$package_data || !is_array( $package_data ))
{
$package_data = array( 'db' => $package, 'files' => $package );
update_option( MEGA_DB_ID . '_package', $package_data );
}
$mega_package = $package_data['db'];
$mega_package_files = $package_data['files'];
if ( !$files && !$override && $package < $mega_package )
return false;
if ( !$files && $package === $mega_package )
return false;
$package_data['db'] = $package;
if ( $package > $package_data['files'] )
$package_data['files'] = $package;
update_option( MEGA_DB_ID . '_package', $package_data );
}
function mega_widget( $id_base, $settings = array() )
{
if ( !Mega_Walker::getBlockObject( $id_base ) && !Mega_Walker::getWidgetObject( $id_base ))
return;
if ( $obj = Mega_Walker::getWidgetObject( $id_base ))
{
if ( $object = Mega_Walker::getBlockObject( $id_base ))
$settings = wp_parse_args( $settings, $object->mega['settings'] );
the_widget( $id_base, $settings, array(
'before_widget' => sprintf( '