Completed
Push — master ( f95488...37fb4d )
by Mike
07:55
created

WC_Embed::the_excerpt()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 3
eloc 9
nc 3
nop 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 4.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Embed Class which handles any WooCommerce Products that are embedded on this site or another site
9
 *
10
 * @class       WC_Embed
11
 * @version     2.4.11
12
 * @package     WooCommerce/Classes/Embed
13
 * @category    Class
14
 * @author      WooThemes
15
 */
16
class WC_Embed {
17
18
	/**
19
	 * Init embed class.
20
	 *
21
     * @since 2.4.11
22
	 */
23
	public static function init() {
24
25
        // filter all of the content that's going to be embedded
26
        add_filter( 'the_title', array( __CLASS__, 'the_title' ), 10 );
27
        add_filter( 'the_excerpt_embed', array( __CLASS__, 'the_excerpt' ), 10 );
28
29
        // make sure no comments display. Doesn't make sense for products
30
        remove_action( 'embed_content_meta', 'print_embed_comments_button' );
31
32
        // in the comments place let's display the product rating
33
        add_action( 'embed_content_meta', array( __CLASS__, 'get_ratings' ), 5 );
34
35
		// Add some basic styles
36
		add_action( 'embed_head', array( __CLASS__, 'print_embed_styles' ) );
37
	}
38
39
    /**
40
     * Create the title for embedded products - we want to add the price to it
41
     *
42
     * @return string
43
     * @since 2.4.11
44
     */
45
    public static function the_title( $title ) {
46
        // make sure we're only affecting embedded products
47
        if ( self::is_embedded_product() ) {
48
49
            // get product
50
            $_product = wc_get_product( get_the_ID() );
51
52
            // add the price
53
            $title = $title . '<span class="wc-embed-price">' . $_product->get_price_html() . '</span>';
54
        }
55
        return $title;
56
    }
57
58
    /**
59
     * Check if this is an embedded product - to make sure we don't mess up regular posts
60
     *
61
     * @return bool
62
     * @since 2.4.11
63
     */
64
    public static function is_embedded_product() {
65
        if ( function_exists( 'is_embed' ) && is_embed() && is_product() ) {
66
            return true;
67
        }
68
        return false;
69
    }
70
71
    /**
72
     * Create the excerpt for embedded products - we want to add the buy button to it
73
     *
74
     * @return string
75
     * @since 2.4.11
76
     */
77
    public static function the_excerpt( $excerpt ) {
78
		global $post;
79
80
        //  make sure we're only affecting embedded products
81
        if ( self::is_embedded_product() ) {
82
			if ( ! empty( $post->post_excerpt ) ) {
83
				ob_start();
84
	            woocommerce_template_single_excerpt();
85
	            $excerpt = ob_get_clean();
86
			}
87
88
			// add the button
89
			$excerpt.= self::product_buttons();
90
        }
91
        return $excerpt;
92
    }
93
94
    /**
95
     * Create the button to go to the product page for embedded products.
96
     *
97
     * @return string
98
     * @since 2.4.11
99
     */
100
    public static function product_buttons() {
101
		$_product = wc_get_product( get_the_ID() );
102
		$buttons  = array();
103
		$button   = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>';
104
105
		if ( $_product->is_type( 'simple' ) && $_product->is_purchasable() && $_product->is_in_stock() ) {
106
			$buttons[] = sprintf( $button, add_query_arg( 'add-to-cart', get_the_ID(), wc_get_cart_url() ), __( 'Buy Now', 'woocommerce' ) );
107
		}
108
109
        $buttons[] = sprintf( $button, get_the_permalink(), __( 'Read More', 'woocommerce' ) );
110
111
		return '<p>' . implode( ' ', $buttons ) . '</p>';
112
    }
113
114
    /**
115
     * Prints the markup for the rating stars
116
     *
117
     * @return string
118
     * @since 2.4.11
119
     */
120
    public static function get_ratings( $comments ) {
0 ignored issues
show
Unused Code introduced by
The parameter $comments is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
121
        //  make sure we're only affecting embedded products
122
        if ( self::is_embedded_product() && ( $_product = wc_get_product( get_the_ID() ) ) && $_product->get_average_rating() > 0 ) {
123
            ?>
124
            <div class="wc-embed-rating">
125
                <?php printf( __( 'Rated %s out of 5', 'woocommerce' ), $_product->get_average_rating() ); ?>
126
            </div>
127
            <?php
128
        }
129
    }
130
131
	/**
132
	 * Basic styling
133
	 */
134
	public static function print_embed_styles() {
135
		if ( ! self::is_embedded_product() ) {
136
			return;
137
		}
138
		?>
139
		<style type="text/css">
140
			a.wc-embed-button {
141
				border: 1px solid #ddd;
142
				border-radius: 4px;
143
				padding: .5em;
144
				display:inline-block;
145
				box-shadow: 0px 1px 0 0px rgba(0,0,0,0.05);
146
			}
147
			a.wc-embed-button:hover, a.wc-embed-button:focus {
148
				border: 1px solid #ccc;
149
				box-shadow: 0px 1px 0 0px rgba(0,0,0,0.1);
150
				text-decoration: none;
151
				color: #999;
152
			}
153
			.wp-embed-excerpt p {
154
				margin: 0 0 1em;
155
			}
156
			.wc-embed-price {
157
				float:right;
158
			}
159
			.wc-embed-rating {
160
				display: inline-block;
161
			}
162
		</style>
163
		<?php
164
	}
165
}
166
167
WC_Embed::init();
168