コード例 #1
0
ファイル: msp-functions.php プロジェクト: blogfor/king
function msp_get_template_tag_value($tag_name, $post = null, $args = null)
{
    $post = get_post($post);
    $value = '{{' . $tag_name . '}}';
    switch ($tag_name) {
        case 'title':
            $value = $post->post_title;
            break;
        case 'content':
            $value = $post->post_content;
            break;
        case 'excerpt':
            $value = $post->post_excerpt;
            if (empty($value)) {
                $excerpt_length = isset($args['excerpt_length']) ? (int) $args['excerpt_length'] : 80;
                $value = msp_get_the_trim_excerpt($value, $excerpt_length);
            }
            break;
        case 'permalink':
            $value = $post->guid;
            break;
        case 'author':
            $value = get_the_author_meta('display_name', (int) $post->post_author);
            break;
        case 'post_id':
            $value = $post->ID;
            break;
        case 'categories':
            $taxonomy_objects = get_object_taxonomies($post, 'objects');
            $value = '';
            foreach ($taxonomy_objects as $tax_name => $tax_info) {
                if (1 == $tax_info->hierarchical) {
                    $term_list = wp_get_post_terms($post->ID, $tax_name, array("fields" => "names"));
                    $value .= implode(' / ', $term_list);
                }
            }
            $value = rtrim($value, ' / ');
            break;
        case 'tags':
            $taxonomy_objects = get_object_taxonomies($post, 'objects');
            $value = '';
            foreach ($taxonomy_objects as $tax_name => $tax_info) {
                if (1 !== $tax_info->hierarchical) {
                    $term_list = wp_get_post_terms($post->ID, $tax_name, array("fields" => "names"));
                    $value .= implode(' / ', $term_list) . ' / ';
                }
            }
            $value = rtrim($value, ' / ');
            break;
        case 'image':
            $value = msp_get_auto_post_thumbnail_src($post, 'featured');
            if (!empty($value)) {
                $value = sprintf('<img src="%s" alt="%s" />', $value, $post->post_title);
            }
            break;
        case 'image-url':
        case 'slide-image-url':
            $value = msp_get_auto_post_thumbnail_src($post, 'auto');
            break;
        case 'year':
            $value = strtotime($post->post_date);
            $value = date_i18n('Y', $value);
            break;
        case 'daynum':
            $value = strtotime($post->post_date);
            $value = date_i18n('j', $value);
            break;
        case 'day':
            $value = strtotime($post->post_date);
            $value = date_i18n('l', $value);
            break;
        case 'monthnum':
            $value = strtotime($post->post_date);
            $value = date_i18n('m', $value);
            break;
        case 'month':
            $value = strtotime($post->post_date);
            $value = date_i18n('F', $value);
            break;
        case 'time':
            $value = strtotime($post->post_date);
            $value = date_i18n('g:i A', $value);
            break;
        case 'date-published':
            $value = $post->post_date;
            break;
        case 'date-modified':
            $value = $post->post_modified;
            break;
        case 'commentnum':
            $value = $post->comment_count;
            break;
        case 'wc_price':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wc_format_decimal($product->get_price(), 2);
            break;
        case 'wc_regular_price':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wc_format_decimal($product->get_regular_price(), 2);
            break;
        case 'wc_sale_price':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = $product->get_sale_price() ? wc_format_decimal($product->get_sale_price(), 2) : '';
            break;
        case 'wc_stock_status':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = $product->is_in_stock() ? __('In Stock', 'master-slider') : __('Out of Stock', 'master-slider');
            break;
        case 'wc_stock_quantity':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = (int) $product->get_stock_quantity();
            break;
        case 'wc_weight':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = $product->get_weight() ? wc_format_decimal($product->get_weight(), 2) : '';
            break;
        case 'wc_product_cats':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wp_get_post_terms($product->id, 'product_cat', array('fields' => 'names'));
            break;
        case 'wc_product_tags':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wp_get_post_terms($product->id, 'product_tag', array('fields' => 'names'));
            break;
        case 'wc_total_sales':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = metadata_exists('post', $product->id, 'total_sales') ? (int) get_post_meta($product->id, 'total_sales', true) : 0;
            break;
        case 'wc_average_rating':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wc_format_decimal($product->get_average_rating(), 2);
            break;
        case 'wc_rating_count':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = (int) $product->get_rating_count();
            break;
        default:
            $value = get_post_meta($post->ID, $tag_name, true);
            break;
    }
    return apply_filters('masterslider_get_template_tag_value', $value, $tag_name, $post, $args);
}
コード例 #2
0
ファイル: msp-functions.php プロジェクト: rinodung/live-theme
function msp_get_template_tag_value($tag_name, $post = null, $args = null)
{
    $post = get_post($post);
    $value = '{{' . $tag_name . '}}';
    switch ($tag_name) {
        case 'title':
            $value = $post->post_title;
            break;
        case 'linked_title':
            $value = sprintf('<a href="%s" >%s</a>', $post->guid, $post->post_title);
            break;
        case 'content':
            $value = $post->post_content;
            break;
        case 'excerpt':
            $excerpt_length = isset($args['excerpt_length']) ? (int) $args['excerpt_length'] : 80;
            $value = msp_get_the_excerpt_by_id($post->ID, $excerpt_length);
            break;
        case 'permalink':
            $value = $post->guid;
            break;
        case 'author':
            $value = get_the_author_meta('display_name', (int) $post->post_author);
            break;
        case 'post_id':
            $value = $post->ID;
            break;
        case 'categories':
            $taxonomy_objects = get_object_taxonomies($post, 'objects');
            $value = '';
            foreach ($taxonomy_objects as $tax_name => $tax_info) {
                if (1 == $tax_info->hierarchical) {
                    $term_list = wp_get_post_terms($post->ID, $tax_name, array("fields" => "names"));
                    $value .= implode(' / ', $term_list);
                }
            }
            $value = rtrim($value, ' / ');
            break;
        case 'tags':
            $taxonomy_objects = get_object_taxonomies($post, 'objects');
            $value = '';
            foreach ($taxonomy_objects as $tax_name => $tax_info) {
                if (1 !== $tax_info->hierarchical) {
                    $term_list = wp_get_post_terms($post->ID, $tax_name, array("fields" => "names"));
                    $value .= implode(' / ', $term_list) . ' / ';
                }
            }
            $value = rtrim($value, ' / ');
            break;
        case 'image':
            $value = msp_get_auto_post_thumbnail_src($post, 'featured');
            if (!empty($value)) {
                $value = sprintf('<img src="%s" alt="%s" />', $value, $post->post_title);
            }
            break;
        case 'image-url':
        case 'slide-image-url':
            $value = msp_get_auto_post_thumbnail_src($post, 'auto');
            break;
        case 'thumbnail':
            $value = msp_get_auto_post_thumbnail_src($post, 'featured', 150, 150);
            if (!empty($value)) {
                $value = sprintf('<img src="%s" alt="%s" />', $value, $post->post_title);
            }
            break;
        case 'thumbnailurl':
            $value = msp_get_auto_post_thumbnail_src($post, 'auto', 150, 150);
            break;
        case 'year':
            $value = strtotime($post->post_date);
            $value = date_i18n('Y', $value);
            break;
        case 'daynum':
            $value = strtotime($post->post_date);
            $value = date_i18n('j', $value);
            break;
        case 'day':
            $value = strtotime($post->post_date);
            $value = date_i18n('l', $value);
            break;
        case 'monthnum':
            $value = strtotime($post->post_date);
            $value = date_i18n('m', $value);
            break;
        case 'month':
            $value = strtotime($post->post_date);
            $value = date_i18n('F', $value);
            break;
        case 'time':
            $value = strtotime($post->post_date);
            $value = date_i18n('g:i A', $value);
            break;
        case 'date-published':
            $value = $post->post_date;
            break;
        case 'date-modified':
            $value = $post->post_modified;
            break;
        case 'commentnum':
            $value = $post->comment_count;
            break;
        case 'wc_price':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wc_format_decimal($product->get_price(), 2);
            break;
        case 'wc_regular_price':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wc_format_decimal($product->get_regular_price(), 2);
            break;
        case 'wc_sale_price':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = $product->get_sale_price() ? wc_format_decimal($product->get_sale_price(), 2) : '';
            break;
        case 'wc_stock_status':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = $product->is_in_stock() ? __('In Stock', MSWP_TEXT_DOMAIN) : __('Out of Stock', MSWP_TEXT_DOMAIN);
            break;
        case 'wc_stock_quantity':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = (int) $product->get_stock_quantity();
            break;
        case 'wc_weight':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = $product->get_weight() ? wc_format_decimal($product->get_weight(), 2) : '';
            break;
        case 'wc_product_cats':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wp_get_post_terms($product->id, 'product_cat', array('fields' => 'names'));
            break;
        case 'wc_product_tags':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wp_get_post_terms($product->id, 'product_tag', array('fields' => 'names'));
            break;
        case 'wc_total_sales':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = metadata_exists('post', $product->id, 'total_sales') ? (int) get_post_meta($product->id, 'total_sales', true) : 0;
            break;
        case 'wc_average_rating':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = wc_format_decimal($product->get_average_rating(), 2);
            break;
        case 'wc_rating_count':
            if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                break;
            }
            $product = get_product($post);
            $value = (int) $product->get_rating_count();
            break;
        default:
            // if the tag is {{content-150}} with dynamic length
            if (strpos($tag_name, 'content-') !== false) {
                preg_match("/([\\d]{1,})/", $tag_name, $matches);
                if (isset($matches[0]) && is_numeric($matches[0])) {
                    $excerpt_length = (int) $matches[0];
                    $value = msp_get_the_trim_excerpt($post->ID, $excerpt_length);
                    break;
                }
                // if the tag is {{wc_price-2}} with dynamic points length
            } elseif (strpos($tag_name, 'wc_price-') !== false) {
                if (!msp_is_plugin_active('woocommerce/woocommerce.php')) {
                    break;
                }
                preg_match("/([\\d]{1,})/", $tag_name, $matches);
                if (isset($matches[0]) && is_numeric($matches[0])) {
                    $points_length = (int) $matches[0];
                    $product = get_product($post);
                    $value = wc_format_decimal($product->get_price(), $points_length);
                    break;
                }
                // if the tag is {{thumbnail-150x150}} with dynamic dimensions
            } elseif (strpos($tag_name, 'image-') !== false) {
                preg_match_all("/([\\d]{1,})/", $tag_name, $matches);
                if (isset($matches[0])) {
                    $image_width = isset($matches[0][0]) && is_numeric($matches[0][0]) ? (int) $matches[0][0] : null;
                    $image_height = isset($matches[0][1]) && is_numeric($matches[0][1]) ? (int) $matches[0][1] : null;
                    $value = msp_get_auto_post_thumbnail_src($post, 'featured', $image_width, $image_height, true);
                    if (!empty($value)) {
                        $value = sprintf('<img class="ms-custom-image" src="%s" alt="%s" />', $value, $post->post_title);
                    }
                    break;
                }
                // if the tag is {{thumbnail-150x150}} with dynamic dimensions
            } elseif (strpos($tag_name, 'thumbnail-') !== false) {
                preg_match_all("/([\\d]{1,})/", $tag_name, $matches);
                if (isset($matches[0])) {
                    $thumb_width = isset($matches[0][0]) && is_numeric($matches[0][0]) ? (int) $matches[0][0] : null;
                    $thumb_height = isset($matches[0][1]) && is_numeric($matches[0][1]) ? (int) $matches[0][1] : null;
                    $value = msp_get_auto_post_thumbnail_src($post, 'featured', $thumb_width, $thumb_height, true);
                    if (!empty($value)) {
                        $value = sprintf('<img class="ms-tab-thumb" src="%s" alt="%s" />', $value, $post->post_title);
                    }
                    break;
                }
                // if the tag is {{thumbnail-url-150x150}} with dynamic dimensions
            } elseif (strpos($tag_name, 'thumbnailurl-') !== false) {
                preg_match_all("/([\\d]{1,})/", $tag_name, $matches);
                if (isset($matches[0])) {
                    $thumb_width = isset($matches[0][0]) && is_numeric($matches[0][0]) ? (int) $matches[0][0] : null;
                    $thumb_height = isset($matches[0][1]) && is_numeric($matches[0][1]) ? (int) $matches[0][1] : null;
                    $value = msp_get_auto_post_thumbnail_src($post, 'featured', $thumb_width, $thumb_height, true);
                    break;
                }
            }
            $value = get_post_meta($post->ID, $tag_name, true);
            break;
    }
    return apply_filters('masterslider_get_template_tag_value', $value, $tag_name, $post, $args);
}