コード例 #1
0
ファイル: rss.php プロジェクト: RadGH/WordPress-Sports-Theme
function lm_add_feature_image_to_rss($content)
{
    global $post;
    $img_id = false;
    // From post meta, custom field
    $key = apply_filters("rss_image_meta_key", "blog-image");
    if ($key && ($i = get_post_meta(get_the_ID(), $key, true))) {
        $img_id = image_get_attachment_id($i);
    }
    // Fall back to featured image
    if (!$img_id) {
        $img_id = get_post_thumbnail_id();
    }
    // Fall back to first image from post content
    if (!$img_id && ($i = auto_archive_image())) {
        if ($i) {
            $img_id = image_get_attachment_id($i);
        }
    }
    // If we have an image URL, attach it
    if ($img_id) {
        $full_size = smart_media_size((int) $img_id, has_image_size('rssfeed-landscape') ? 'rssfeed-landscape' : 'medium');
        $type = 'image/jpg';
        if (substr($full_size, -3) == 'png') {
            $type = 'image/png';
        }
        echo "\t";
        echo sprintf('<media:content url="%s" medium="image" width="560" height="280" type="%s" />', esc_attr($full_size), esc_attr($type));
        echo "\n";
    }
}
コード例 #2
0
function ld_social_menu($name = null, $class = null)
{
    $networks = get_field('social_networks', 'options');
    if (empty($networks) || count($networks) == 1 && empty($networks[0]['url'])) {
        if (current_user_can('manage_options')) {
            echo '<p><strong>ld_social_menu(): No social networks have been defined under Theme Options &gt; Social Media. This menu won\'t be displayed to your visitors.</strong></p>';
        }
        return;
    }
    // Get all icon layouts, if there aren't any, give an error.
    $layouts = get_field('social_layouts', 'options');
    if (empty($layouts) || count($layouts) == 1 && empty($layouts[0]['icons'])) {
        if (current_user_can('manage_options')) {
            echo '<p><strong>ld_social_menu(): No social media icon layouts have been defined under Theme Options &gt; Social Media. This menu won\'t be displayed to your visitors.</strong></p>';
        }
        return;
    }
    // Which icon layout will we use? Start with the first one, then look for a named one.
    $active_layout = $layouts[0];
    if ($name !== null) {
        foreach ($layouts as $k => $v) {
            if (strtolower($v['name']) == strtolower($name)) {
                $active_layout = $v;
            }
        }
    }
    // Display the necessary CSS & Markup
    $classes = array('nav-menu', 'nav-social');
    if ($class !== null) {
        $classes[] = esc_attr($class);
    }
    $layout_class = 'layout-' . sanitize_title_with_dashes($active_layout['name']);
    $classes[] = $layout_class;
    $icon_image = smart_media_size($active_layout['icons'], 'full');
    $width = (int) $active_layout['width'];
    $height = (int) $active_layout['height'];
    $margin = $active_layout['margin'];
    $styles = $active_layout['styles'];
    $services = $active_layout['services'];
    $offset_left = 0;
    if ($margin) {
        // Get the left margin from a CSS string allowing formats:
        //// TOP, RIGHT, BOTTOM, LEFT
        //// TOP, RIGHT/LEFT, BOTTOM
        //// TOP/BOTTOM, RIGHT/LEFT
        //// TOP/BOTTOM/RIGHT/LEFT
        if (preg_match('/^[\\s]*(-?[0-9]+)(px)? (-?[0-9]+)(px)? (-?[0-9]+)(px)? (-?[0-9]+)(px)?[\\s]*/', $margin, $matches)) {
            $offset_left = $matches[7];
        } else {
            if (preg_match('/^[\\s]*(-?[0-9]+)(px)? (-?[0-9]+)(px)?/', $margin, $matches)) {
                $offset_left = $matches[3];
            } else {
                if (preg_match('/^[\\s]*(-?[0-9]+)(px)?/', $margin, $matches)) {
                    $offset_left = $matches[1];
                }
            }
        }
        $offset_left = -1 * floor($offset_left / 2);
    }
    ?>
	<style type="text/css">
		<?php 
    if ($offset_left > 1) {
        ?>
		.<?php 
        echo $layout_class;
        ?>
 .nav-list {
			margin-left: <?php 
        echo $offset_left;
        ?>
px;
		}
		<?php 
    }
    ?>

		.<?php 
    echo $layout_class;
    ?>
 a {
			width: <?php 
    echo $width;
    ?>
px;
			height: <?php 
    echo $height;
    ?>
px;
		<?php 
    if ($margin) {
        echo 'margin: ' . $margin . ';';
    }
    ?>

			background: url(<?php 
    echo esc_attr($icon_image);
    ?>
) 1000px 1000px no-repeat;
		}
		<?php 
    foreach ($styles as $style_index => $style) {
        foreach ($services as $service_index => $service) {
            // Check if this service is being used by Branding
            $is_used = false;
            foreach ($networks as $soc) {
                if ($soc['name'] == $service['name']) {
                    $is_used = true;
                    break;
                }
            }
            if (!$is_used) {
                continue;
            }
            // Display the CSS for this service
            $sel = '.' . $layout_class . ' li.social-' . strtolower(sanitize_title_with_dashes($service['name'])) . ' a';
            if ($style != 'normal') {
                $sel .= ':' . $style;
            }
            $x_offset = -1 * ($service_index * $width);
            $y_offset = -1 * ($style_index * $height);
            printf("%s { background-position: %spx %spx; }\n", $sel, (int) $x_offset, (int) $y_offset);
        }
    }
    ?>
	</style>

	<nav class="<?php 
    echo esc_attr(implode(' ', $classes));
    ?>
">
		<ul class="nav-list">
			<?php 
    foreach ($networks as $i => $soc) {
        $service = $soc['name'];
        $action = $soc['action'];
        $url = $soc['url'];
        $classes = array('menu-item', 'menu-item-social');
        $classes[] = 'nth-' . ($i + 1);
        if ($i === 0) {
            $classes[] = 'menu-first';
        }
        if ($i == count($networks) - 1) {
            $classes[] = 'menu-last';
        }
        $classes[] = 'depth-1';
        $classes[] = 'social-' . strtolower(sanitize_title_with_dashes($service));
        printf('<li class="%s"><a href="%s" target="_blank" rel="external" %s><span>%s</span></a></li>', esc_attr(implode(' ', $classes)), esc_attr($url), $action ? 'title="' . esc_attr($action) . '"' : '', esc_html($service));
    }
    ?>
		</ul>
	</nav>
	<?php 
}
コード例 #3
0
function generate_sharing_links($sites = true, $args_or_id = null)
{
    // Defaults for $sites
    if ($sites === true || $sites === 'all') {
        $sites = array('facebook', 'googleplus', 'twitter', 'pinterest');
    }
    if (!$sites) {
        return false;
    }
    // $sites must be an array of lower case strings
    if (!is_array($sites)) {
        $sites = (array) $sites;
    }
    foreach ($sites as $k => $v) {
        $sites[$k] = strtolower($v);
    }
    if (!$sites) {
        return false;
    }
    // Defaults for args
    if ($args_or_id === null) {
        $args_or_id = array('ID' => get_the_ID());
    } else {
        if (!is_array($args_or_id) && is_numeric($args_or_id)) {
            $args_or_id = array('ID' => absint($args_or_id));
        }
    }
    // Fill our arguments with default values
    $default = array('ID' => null, 'title' => null, 'url' => null, 'image' => null, 'type' => null, 'use_html' => true, 'before_text' => '<span class="share-text">', 'after_text' => '</span>', 'element' => 'span', 'twitter_user' => false);
    $args = wp_parse_args($args_or_id, $default);
    $args = apply_filters('sharing_link_args', $args, $sites, $args_or_id);
    // Build the item that will be shared
    $share_item = array('title' => '', 'url' => '', 'image' => '', 'type' => '');
    // Use content from a wordpress post if an ID is specified
    if ($args['ID'] !== null) {
        $id = absint($args['ID']);
        $share_item['title'] = get_the_title($id);
        $share_item['url'] = get_permalink($id);
        $share_item['image'] = get_the_post_thumbnail($id, 'full');
        $share_item['type'] = get_post_type($id);
    }
    // Use specified title/url/image if specified.
    if ($args['title'] !== null) {
        $share_item['title'] = $args['title'];
    }
    if ($args['url'] !== null) {
        $share_item['url'] = $args['url'];
    }
    if ($args['image'] !== null) {
        $share_item['image'] = smart_media_size($args['image'], 'full');
    }
    if ($args['type'] !== null) {
        $share_item['type'] = $args['type'];
    }
    // Allow filtering our sharing object, especially useful if you use a custom featured image in your theme.
    if (has_filter('sharing_links_format')) {
        $share_item = apply_filters('sharing_links_format', $share_item, $args['ID'], $args);
    }
    // If not provided elsewhere, use the current page url and title
    if (!$args['url']) {
        $share_item['url'] = get_permalink();
    }
    if (!$args['title']) {
        $share_item['title'] = get_the_title();
    }
    // Build a list of links. Each item should have the "text", "url" and "attr" properties. The text is what is displayed in the link.
    $share_links = array();
    // -------- Begin social site links
    if (in_array('facebook', $sites)) {
        // Note: Facebook will check for open graph tags at $args['url'] and use that instead of the provided options
        $url_args = array();
        if ($share_item['url']) {
            $url_args['u'] = $share_item['url'];
        }
        if ($share_item['title']) {
            $url_args['t'] = $share_item['title'];
        }
        $url = add_query_arg($url_args, 'https://www.facebook.com/sharer/sharer.php');
        $attr = 'onclick="javascript:window.open(this.href, \'_blank\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600\');return false;"';
        $share_links['facebook'] = apply_filters('sharing_link', array('text' => $share_item['type'] ? 'Share ' . strtolower($share_item['type']) . ' on Facebook' : 'Share on Facebook', 'url' => $url, 'attr' => $attr), 'facebook');
    }
    if (in_array('twitter', $sites)) {
        $url_args = array();
        if ($share_item['url']) {
            $url_args['url'] = $share_item['url'];
        }
        if ($share_item['title']) {
            $url_args['text'] = $share_item['title'];
        }
        if ($args['twitter_user']) {
            $url_args['via'] = $args['twitter_user'];
        }
        $url = add_query_arg($url_args, 'https://twitter.com/share');
        $attr = 'onclick="javascript:window.open(this.href, \'_blank\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600\');return false;"';
        $share_links['twitter'] = apply_filters('sharing_link', array('text' => $share_item['type'] ? 'Tweet this ' . strtolower($share_item['type']) : 'Share on Twitter', 'url' => $url, 'attr' => $attr), 'twitter');
    }
    if (in_array('googleplus', $sites) || in_array('google', $sites) || in_array('google+', $sites)) {
        $url_args = array();
        if ($share_item['url']) {
            $url_args['url'] = $share_item['url'];
        }
        $url = add_query_arg($url_args, 'https://plus.google.com/share');
        $attr = 'onclick="javascript:window.open(this.href, \'_blank\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=350,width=480\');return false;"';
        $share_links['googleplus'] = apply_filters('sharing_link', array('text' => $share_item['type'] ? 'Share ' . strtolower($share_item['type']) . ' on Google+' : 'Share on Google+', 'url' => $url, 'attr' => $attr), 'googleplus');
    }
    if (in_array('pinterest', $sites)) {
        if (!$share_item['image']) {
            $share_item['image'] = apply_filters('share_default_image', false, 'pinterest');
        }
        if ($share_item['image']) {
            // We can only share on pinterest if we have an image.
            $url_args = array();
            if ($share_item['url']) {
                $url_args['url'] = $share_item['url'];
            }
            if ($share_item['title']) {
                $url_args['description'] = $share_item['title'];
            }
            if ($share_item['image']) {
                $url_args['media'] = $share_item['image'];
            }
            $url = add_query_arg($url_args, 'http://pinterest.com/pin/create/button/');
            $attr = 'onclick="javascript:window.open(this.href, \'_blank\', \'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600\');return false;"';
            $share_links['pinterest'] = apply_filters('sharing_link', array('text' => $share_item['type'] ? 'Pin this ' . strtolower($share_item['type']) : 'Share on Pinterest', 'url' => $url, 'attr' => $attr), 'pinterest');
        }
    }
    // -------- End of social site links
    $share_links = apply_filters('sharing_links_ready', $share_links, $args);
    if (!$share_links) {
        return false;
    }
    // Begin building the results
    if ($args['use_html']) {
        // Format our links as HTML items, and return in a numeric array
        $result = array();
        foreach ($share_links as $k => $share) {
            $result[] = sprintf('<%s class="share-item share-%s"><a href="%s" %s target="_blank" title="%s">%s%s%s</a></%s>', esc_attr($args['element']), esc_attr($k), esc_attr($share['url']), $share['attr'] ? 'attr="' . esc_attr($share['attr']) . '"' : '', esc_attr($share_item['title']), $args['before_text'], esc_html($share['text']), $args['after_text'], esc_attr($args['element']));
        }
        return $result;
    } else {
        // Return our sharing link structure without formatting it.
        return $share_links;
    }
}
コード例 #4
0
function auto_archive_image($post_id = false, $size = 'thumbnail')
{
    if (!$post_id) {
        $post_id = get_the_ID();
    }
    if (!$post_id) {
        return false;
    }
    // Use featured image if provided
    $featured = get_the_post_thumbnail($post_id, $size);
    if ($featured) {
        return $featured[0];
    }
    // Look for an image in post meta. Meta key can be customized via filter or directly.
    $meta_key = apply_filters('ld-archive-thumbnail-meta-key', 'featured-image', $post_id, $size);
    if ($meta_key) {
        $meta_thumbnail = get_post_meta($post_id, $meta_key, true);
        if ($meta_thumbnail) {
            $img = false;
            if (is_numeric($meta_thumbnail)) {
                // Attachment ID
                $img = smart_media_size((int) $meta_thumbnail, $size);
            } else {
                if (is_string($meta_thumbnail)) {
                    // Image URL
                    $img = smart_media_size((string) $meta_thumbnail, $size);
                }
            }
            if ($img) {
                return $img;
            }
        }
    }
    // If we have already detected one through content before, use the cached version.
    $meta_thumbnail = get_post_meta($post_id, 'lm_thumbnail', true);
    if ($meta_thumbnail) {
        $img = smart_media_size((string) $meta_thumbnail, $size);
        if ($img) {
            return $img;
        }
    }
    // In case we have looked before but didn't find anything
    if ($meta_thumbnail === 0) {
        return false;
    }
    // We're going to scrape the post content and look for an image. We'll store it in lm_thumbnail if we find it so we don't have to scrape in the future.
    global $post;
    if ($post->ID == $post_id) {
        $content = $post->post_content;
        // Looks for an image, roughly checking if it is on this website
        // Regex looks like: <img*src=*(__frequentflyeracademy.com__)*
        if (!preg_match('/<img.*?src=[\'\\"](.*?' . $_SERVER['HTTP_HOST'] . '.*?)[\'\\"].*?>/i', $content, $matches)) {
            return false;
        }
        $attachment_id = image_get_attachment_id($matches[1]);
        if ($attachment_id) {
            // Attachment ID was found, return the thumbnail
            $attachment = wp_get_attachment_image_src($attachment_id, $size);
            if ($attachment) {
                $attachment_src = sprintf('<img src="%s" alt="%s - Featured Image" width="%s" height="%s" />', $attachment[0], esc_attr(get_the_title()), $attachment[1], $attachment[2]);
                update_post_meta($post_id, 'lm_thumbnail', $attachment_src);
                return $attachment_src;
            }
        }
        // No attachment found, cannot use a thumbnail. Return the full size image.
        $default_src = sprintf('<img src="%s" alt="%s - Featured Image" />', $matches[1], esc_attr(get_the_title()));
        update_post_meta($post_id, 'lm_thumbnail', $default_src);
        return $default_src;
    }
    update_post_meta($post_id, 'lm_thumbnail', 0);
    return false;
}
コード例 #5
0
function ld_media_value($column_name, $id)
{
    if ($column_name != 'media_url') {
        return;
    }
    $meta = wp_get_attachment_metadata($id);
    $url = smart_media_size(wp_get_attachment_url($id), 'full');
    if (!$url) {
        $url = wp_get_attachment_url($id);
    }
    if ($url) {
        ?>
		<p><input type="text" onfocus="var $me=this;setTimeout(function(){$me.select();},60);" readonly="readonly" value="<?php 
        echo esc_attr($url);
        ?>
" class="code" style="width: 100%; box-sizing: border-box; direction: rtl;"></p>
		<?php 
    }
    if ($meta) {
        echo '<p class="description">';
        if ($meta['width'] && $meta['height']) {
            echo sprintf('Original Size: %s&times;%s', $meta['width'], $meta['height']);
        }
        if ($meta['sizes']) {
            $size_array = array();
            foreach ($meta['sizes'] as $size => $size_meta) {
                $sized_url = smart_media_size($id, $size);
                if ($sized_url == $url) {
                    continue;
                }
                $size_array[] = sprintf('<a href="%s" target="_blank" title="Image resolution: %sx%s">%s</a>', esc_attr($sized_url), $size_meta['width'], $size_meta['height'], esc_html($size));
            }
            if ($size_array && $meta['width'] && $meta['height']) {
                echo "<br/>";
            }
            if ($size_array) {
                echo "Sizes: " . implode(', ', $size_array);
            }
        }
        echo '</p>';
    }
}