Completed
Push — master ( ab284c...4fdc7c )
by Justin
07:07
created

productfeed.php ➔ wpsc_generate_product_feed()   F

Complexity

Conditions 34
Paths > 20000

Size

Total Lines 216
Code Lines 125

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 34
eloc 125
c 1
b 0
f 0
nc 52572
nop 0
dl 0
loc 216
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Generates an RSS feed for WP eCommerce Products
4
 *
5
 * @package wp-e-commerce
6
 */
7
8
function wpsc_feed_publisher() {
9
10
	// If the user wants a product feed, then hook-in the product feed function
11
	if ( isset( $_GET['rss']) && ( $_GET["rss"] == "true" ) &&
12
	     ( $_GET["action"] == "product_list" ) ) {
13
    		add_action( 'wp', 'wpsc_generate_product_feed' );
14
  	}
15
}
16
17
add_action( 'init', 'wpsc_feed_publisher' );
18
19
function wpsc_generate_product_feed() {
20
21
    global $wpdb, $wp_query, $post;
22
23
    set_time_limit(0);
24
25
    $xmlformat = '';
26
27
    if ( isset( $_GET['xmlformat'] ) ) {
28
    	$xmlformat = $_GET['xmlformat'];
29
    }
30
31
	// Don't build up a huge posts cache for the whole store - http://code.google.com/p/wp-e-commerce/issues/detail?id=885
32
	wp_suspend_cache_addition(true);
33
34
    $chunk_size = apply_filters ( 'wpsc_productfeed_chunk_size', 50 );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
35
36
    // Don't cache feed under WP Super-Cache
37
    define( 'DONOTCACHEPAGE', true );
38
39
	$selected_category = '';
40
	$selected_product = '';
41
42
	$args = array(
43
			'post_type'     => 'wpsc-product',
44
			'numberposts'   => $chunk_size,
45
			'offset'        => 0,
46
			'cache_results' => false,
47
		);
48
49
	$args = apply_filters( 'wpsc_productfeed_query_args', $args );
50
51
	$self = home_url( "/index.php?rss=true&amp;action=product_list$selected_category$selected_product" );
52
53
	header("Content-Type: application/xml; charset=UTF-8");
54
	header('Content-Disposition: inline; filename="E-Commerce_Product_List.rss"');
55
56
	echo "<?xml version='1.0' encoding='UTF-8' ?>\n\r";
57
	echo "<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'";
58
59
	$google_checkout_note = false;
60
61
	if ( $xmlformat == 'google' ) {
62
		echo ' xmlns:g="http://base.google.com/ns/1.0"';
63
		// Is Google Checkout available as a payment gateway
64
        	$selected_gateways = get_option('custom_gateway_options');
65
		if (in_array('google',$selected_gateways)) {
66
			$google_checkout_note = true;
67
		}
68
	} else {
69
		echo ' xmlns:product="http://www.buy.com/rss/module/productV2/"';
70
	}
71
72
	echo ">\n\r";
73
	echo "  <channel>\n\r";
74
	echo "    <title><![CDATA[" . sprintf( _x( '%s Products', 'XML Feed Title', 'wp-e-commerce' ), get_option( 'blogname' ) ) . "]]></title>\n\r";
75
	echo "    <link>" . admin_url( 'admin.php?page=' . WPSC_DIR_NAME . '/display-log.php' ) . "</link>\n\r";
76
	echo "    <description>" . _x( 'This is the WP eCommerce Product List RSS feed', 'XML Feed Description', 'wp-e-commerce' ) . "</description>\n\r";
77
	echo "    <generator>" . _x( 'WP eCommerce Plugin', 'XML Feed Generator', 'wp-e-commerce' ) . "</generator>\n\r";
78
	echo "    <atom:link href='$self' rel='self' type='application/rss+xml' />\n\r";
79
80
	$products = get_posts( $args );
81
82
	while ( count( $products ) ) {
83
84
		foreach ( $products as $post ) {
85
86
			setup_postdata( $post );
87
88
			$purchase_link = get_permalink($post->ID);
89
90
			echo "    <item>\n\r";
91
			if ($google_checkout_note) {
92
				echo "      <g:payment_notes>" . _x( 'Google Wallet', 'Google Checkout Payment Notes in XML Feed', 'wp-e-commerce' ) . "</g:payment_notes>\n\r";
93
			}
94
			echo "      <title><![CDATA[".get_the_title()."]]></title>\n\r";
95
			echo "      <link>$purchase_link</link>\n\r";
96
			echo "      <description><![CDATA[".apply_filters ('the_content', get_the_content())."]]></description>\n\r";
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
97
98
			if ( 'google' === $xmlformat ) {
99
100
				$sku  = get_post_meta( $post->ID, '_wpsc_sku', true );
101
				$g_id = ! empty( $sku ) ? $sku : $post->ID;
102
				$g_id = apply_filters( 'wpsc_google_product_feed_product_id', $g_id, $post );
103
104
				if ( strlen( $g_id ) > 50 ) {
105
					$g_id = substr( $g_id, 0, 50 );
106
				}
107
108
				echo "      <g:id>$g_id</g:id>\n\r";
109
			} else {
110
				echo "      <guid>$purchase_link</guid>\n\r";
111
			}
112
113
			$image_link = wpsc_the_product_thumbnail() ;
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
114
115
			if ( $image_link !== false ) {
116
117
				if ( $xmlformat == 'google' ) {
118
					echo "      <g:image_link><![CDATA[$image_link]]></g:image_link>\n\r";
119
				} else {
120
					echo "      <enclosure url='" . esc_url( $image_link ) . "' />\n\r";
121
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
122
123
			}
124
125
			$price = wpsc_calculate_price($post->ID);
126
			$currargs = array(
127
				'display_currency_symbol' => false,
128
				'display_decimal_point'   => true,
129
				'display_currency_code'   => false,
130
				'display_as_html'         => false
131
			);
132
			$price = wpsc_currency_display($price, $currargs);
133
134
			$children = get_children(array('post_parent'=> $post->ID,
135
						                   'post_type'=>'wpsc-product'));
136
137
			foreach ($children as $child) {
138
				$child_price = wpsc_calculate_price($child->ID);
139
140
				if (($price == 0) && ($child_price > 0)) {
141
					$price = $child_price;
142
				} else if ( ($child_price > 0) && ($child_price < $price) ) {
143
					$price = $child_price;
144
				}
145
			}
146
147
			if ( $xmlformat == 'google' ) {
148
149
				echo "      <g:price>".$price."</g:price>\n\r";
150
151
				$google_elements = array();
152
153
				$product_meta = get_post_custom ( $post->ID );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
154
155
                if ( is_array ( $product_meta ) ) {
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
156
				    foreach ( $product_meta as $meta_key => $meta_value ) {
157
					    if ( stripos( $meta_key, 'g:' ) === 0 )
158
						    $google_elements[ $meta_key ] = $meta_value;
159
				    }
160
                }
161
162
				$google_elements = apply_filters( 'wpsc_google_elements', array( 'product_id' => $post->ID, 'elements' => $google_elements ) );
163
				$google_elements = $google_elements['elements'];
164
165
	            $done_condition = FALSE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
166
	            $done_availability = FALSE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
167
	            $done_weight = FALSE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected false, but found FALSE.
Loading history...
168
169
	            if ( count ( $google_elements ) ) {
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
170
171
					foreach ( $google_elements as $element_name => $element_values ) {
172
173
						foreach ( $element_values as $element_value ) {
174
175
							echo "      <".$element_name.">";
176
							echo "<![CDATA[".$element_value."]]>";
177
							echo "</".$element_name.">\n\r";
178
179
						}
180
181
						if ($element_name == 'g:shipping_weight')
182
							$done_weight = TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
183
184
						if ($element_name == 'g:condition')
185
							$done_condition = TRUE;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected true, but found TRUE.
Loading history...
186
187
	                    if ($element_name == 'g:availability')
188
	                        $done_availability = true;
189
					}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
190
191
				}
192
193
	            if (!$done_condition)
194
					echo "      <g:condition>new</g:condition>\n\r";
195
196
	            if (!$done_availability) {
197
198
	                if(wpsc_product_has_stock()) :
199
	                    $product_availability = "in stock";
200
	                else :
201
	                    $product_availability = "out of stock";
202
	                endif ;
203
204
	                echo " <g:availability>$product_availability</g:availability>";
205
206
	            }
207
208
				if ( ! $done_weight ) {
209
					$wpsc_product_meta = get_product_meta( $post->ID, 'product_metadata',true );
210
					$weight = apply_filters ( 'wpsc_google_shipping_weight', $wpsc_product_meta['weight'], $post->ID );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
211
					if ( $weight && is_numeric ( $weight ) && $weight > 0 ) {
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
212
						echo "<g:shipping_weight>$weight pounds</g:shipping_weight>";
213
					}
214
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
215
216
			} else {
217
218
				echo "      <product:price>".$price."</product:price>\n\r";
219
220
			}
221
222
			echo "    </item>\n\r";
223
224
		}
225
226
		$args['offset'] += $chunk_size;
227
		$products = get_posts ( $args );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
228
229
	}
230
231
	echo "  </channel>\n\r";
232
	echo "</rss>";
233
	exit();
234
}
235
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
236