wpshop_categories   F
last analyzed

Complexity

Total Complexity 85

Size/Duplication

Total Lines 436
Duplicated Lines 2.75 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
dl 12
loc 436
rs 1.5789
c 0
b 0
f 0
wmc 85
lcom 0
cbo 5

12 Methods

Rating   Name   Duplication   Size   Complexity  
C product_list_cats() 0 23 7
B create_product_categories() 0 30 3
A category_tree() 0 20 4
B category_tree_output() 0 23 6
A category_manage_columns() 0 12 1
A category_manage_columns_content() 4 14 3
F category_mini_output() 4 58 15
B category_fields_saver() 0 25 6
A get_product_of_category() 0 14 4
C category_edit_fields() 4 59 18
C wpshop_category_func() 0 48 14
B get_the_category_thumbnail() 0 22 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like wpshop_categories often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use wpshop_categories, and based on these observations, apply Extract Interface, too.

1
<?php if ( !defined( 'ABSPATH' ) ) exit;
2
/**
3
* Products management method file
4
*
5
*	This file contains the different methods for products management
6
* @author Eoxia <[email protected]>
7
* @version 1.1
8
* @package wpshop
9
* @subpackage librairies
10
*/
11
12
/*	Check if file is include. No direct access possible with file url	*/
13
if ( !defined( 'WPSHOP_VERSION' ) ) {
14
	die( __('Access is not allowed by this way', 'wpshop') );
15
}
16
17
/**
18
*	This file contains the different methods for products management
19
* @author Eoxia <[email protected]>
20
* @version 1.1
21
* @package wpshop
22
* @subpackage librairies
23
*/
24
class wpshop_categories
25
{
26
	/**
27
	* Retourne une liste de cat�gorie
28
	* @param boolean $formated : formatage du r�sultat oui/non
29
	* @param string $product_search : recherche demand�e
30
	* @return mixed
31
	**/
32
	public static function product_list_cats($formated=false, $product_search=null) {
33
		$where  = array('hide_empty' => false);
34
		if(!empty($product_search))
35
			$where = array_merge($where, array('name__like'=>$product_search));
36
37
		$data = get_terms(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES, $where);
38
		$cats=array();
39
		foreach($data as $d){
40
			$cats[$d->term_id] = $d->name;
41
		}
42
43
		// Si le formatage est demand�
44
		$cats_string='';
45
		if($formated) {
46
			if(!empty($cats)):
47
				foreach($cats as $key=>$value) {
48
					$cats_string.= '
49
					<li><input type="checkbox" class="wpshop_shortcode_element wpshop_shortcode_element_categories" value="'.$key.'" id="'.WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES.'-'.$key.'" name="cats[]" /><label for="'.WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES.'-'.$key.'" > '.$value.'</label></li>';
50
				}
51
			endif;
52
		}
53
		return $formated?$cats_string:$cats;
54
	}
55
56
	/**
57
	*	Call wordpress function that declare a new term type in order to define the product as wordpress term (taxonomy)
58
	*/
59
	public static function create_product_categories(){
60
		$options = get_option('wpshop_catalog_categories_option', null);
61
		$slug = array(
62
				'slug' => '',
63
				'with_front' => true,
64
				'hierarchical' => true,
65
		);
66
		( empty($options['wpshop_catalog_categories_slug']) || $options['wpshop_catalog_categories_slug'] == '/' ) ? $slug = false : $slug['slug'] = $options['wpshop_catalog_categories_slug'];
67
		register_taxonomy(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES, array(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT), array(
68
			'labels' => array(
69
				'name' => __('WPShop categories', 'wpshop'),
70
				'singular_name' => __('WPShop category', 'wpshop'),
71
				'add_new_item' => __('Add new WPShop category', 'wpshop'),
72
				'add_new' => _x( 'Add new', 'admin menu: add new wpshop category', 'wpshop'),
73
				'add_new_item' => __('Add new WPShop category', 'wpshop'),
74
				'edit_item' => __('Edit WPShop category', 'wpshop'),
75
				'new_item' => __('New WPShop category', 'wpshop'),
76
				'view_item' => __('View WPShop category', 'wpshop' ),
77
				'search_items' => __('Search WPShop categories', 'wpshop'),
78
				'not_found' =>  __('No WPShop categories found', 'wpshop'),
79
				'not_found_in_trash' => __('No WPShop categories found in trash', 'wpshop'),
80
				'parent_item_colon' => '',
81
				'menu_name' => __('WPShop Categories', 'wpshop')
82
			),
83
			'rewrite' => $slug,
84
			'hierarchical' => true,
85
			'public' => true,
86
			'show_in_nav_menus' => true
87
		));
88
	}
89
90
	/**
91
	*	Build a complete tree with the categories. Contains the complete tree for a given category and a children list for easy checking
92
	*
93
	*	@param integer $category_id The category identifier we want to get the tree element for
94
	*
95
	*	@return array $categories_list An array ordered by category with its children
96
	*/
97
	public static function category_tree($category_id = 0){
98
		$categories_list = array();
99
100
		$categories = get_terms(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES, 'hide_empty=0&parent=' . $category_id);
101
		if(count($categories) > 0){
102
			foreach($categories as $category){
103
				/*	If necessary un-comment this line in order to get the complete tree for the category	*/
104
				// $categories_list[$category->term_id]['children_tree'] = self::category_tree($category->term_id);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
				$categories_list[$category->term_id]['children_category'] = get_term_children($category->term_id, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
106
107
				/*	Get the product list for the category	*/
108
				$products = get_posts(array('post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES => $category->slug));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $products is correct as get_posts(array('post_ty...ES => $category->slug)) (which targets get_posts()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
109
				foreach($products as $product){
0 ignored issues
show
Bug introduced by
The expression $products of type null is not traversable.
Loading history...
110
					$categories_list[$category->term_id]['children_product'][] = $product->ID;
111
				}
112
			}
113
		}
114
115
		return $categories_list;
116
	}
117
	/**
118
	*	Get the sub categories of a given category
119
	*
120
	*	@param integer $parent_category The main category we want to have the sub categories for
0 ignored issues
show
Bug introduced by
There is no parameter named $parent_category. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
121
	*	@param array $instance The current instance of the widget, allows to get the different selected parameters
122
	*
123
	* @return mixed $widget_content The widget content build from option
124
	*/
125
	public static function category_tree_output($category_id = 0, $instance) {
126
		global $category_has_sub_category;
127
128
		$widget_content = '';
129
		$category_tree = wpshop_categories::category_tree($category_id);
130
		if((!isset($instance['wpshop_widget_categories']) && !isset($instance['show_all_cat'])) || ($instance['show_all_cat'] == 'yes')){
131
			$categories = get_terms(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES, 'hide_empty=0&parent=' . $category_id);
132
			if(count($categories) > 0){
133
				foreach($categories as $category){
134
					ob_start();
135
					require(wpshop_display::get_template_file('categories-widget.tpl.php'));
136
					$widget_content .= ob_get_contents();
137
					ob_end_clean();
138
				}
139
				$category_has_sub_category = true;
140
			}
141
			else{
142
				$category_has_sub_category = false;
143
			}
144
		}
145
146
		return $widget_content;
147
	}
148
149
150
	/**
151
	*	Add additionnal fields to the category edition form
152
	*/
153
	public static function category_edit_fields(){
154
		$category_id = (int) $_REQUEST["tag_ID"];
155
		$category_meta_information = get_option(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '_' . $category_id);
156
		$tpl_component = array();
157
		wp_enqueue_media();
158
		$category_thumbnail_preview = '<img src="' .WPSHOP_DEFAULT_CATEGORY_PICTURE. '" alt="No picture" class="category_thumbnail_preview" />';
159
		/*	Check if there is already a picture for the selected category	*/
160
161 View Code Duplication
		if ( !empty($category_meta_information['wpshop_category_picture']) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
			$image_post = wp_get_attachment_image( $category_meta_information['wpshop_category_picture'], 'thumbnail', false, array('class' => 'category_thumbnail_preview') );
163
			$category_thumbnail_preview = ( !empty($image_post) ) ? $image_post : '<img src="' .WPSHOP_DEFAULT_CATEGORY_PICTURE. '" alt="No picture" class="category_thumbnail_preview" />';
164
		}
165
166
167
		$tpl_component['CATEGORY_DELETE_PICTURE_BUTTON'] = '';
168
		if( !empty($category_meta_information) && !empty($category_meta_information['wpshop_category_picture']) ) {
169
			$tpl_component['CATEGORY_DELETE_PICTURE_BUTTON'] = '<a href="#" role="button" id="wps-delete-category-picture" data-nonce="' . wp_create_nonce( 'wps_delete_picture_category' ) . '" class="wps-bton-second-mini-rounded">' .__( 'Delete the category picture', 'wpshop' ). '</a> ';
170
		}
171
		$tpl_component['CATEGORY_PICTURE_ID'] = ( ( !empty($category_meta_information['wpshop_category_picture']) ) ? $category_meta_information['wpshop_category_picture'] : '' );
172
173
		$tpl_component['CATEGORY_THUMBNAIL_PREVIEW'] = $category_thumbnail_preview;
174
		if(isset($category_id)){
175
			$tpl_component['CATEGORY_TAG_ID'] = $category_id;
176
			$tpl_component['CATEGORY_FILTERABLE_ATTRIBUTES'] = '';
177
			$wpshop_category_products = wpshop_categories::get_product_of_category( $category_id );
178
			$filterable_attributes_list = array();
179
			foreach ( $wpshop_category_products as $wpshop_category_product ) {
180
				$elementId = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
181
				if ( !empty($elementId) ) {
182
					$product_attributes = wpshop_attributes::get_attribute_list_for_item($elementId, $wpshop_category_product);
183
					if ( !empty($product_attributes) ) {
184
						foreach ( $product_attributes as $key => $product_attribute ) {
185
							if ( !empty($product_attribute) && !empty($product_attribute->is_filterable) && strtolower(__($product_attribute->is_filterable, 'wpshop')) == strtolower(__('Yes', 'wpshop')) ) {
186
								if  ( !array_key_exists($product_attribute->attribute_id, $filterable_attributes_list) ) {
187
									$filterable_attributes_list[$product_attribute->attribute_id] = $product_attribute;
188
									$sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_ID'] =  $product_attribute->attribute_id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$sub_tpl_component was never initialized. Although not strictly required by PHP, it is generally a good practice to add $sub_tpl_component = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
189
									$sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_NAME'] =  __($product_attribute->frontend_label, 'wpshop');
0 ignored issues
show
Bug introduced by
The variable $sub_tpl_component does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
190
									if ( !empty($category_meta_information) && !empty($category_meta_information['wpshop_category_filterable_attributes']) && array_key_exists($product_attribute->attribute_id, $category_meta_information['wpshop_category_filterable_attributes']) ) {
191
										$sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_CHECKED'] = 'checked="checked"';
192
									}
193
									else {
194
										$sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_CHECKED'] = '';
195
									}
196
197
									$tpl_component['CATEGORY_FILTERABLE_ATTRIBUTES'] .= wpshop_display::display_template_element('wpshop_category_filterable_attribute_element', $sub_tpl_component, array(), 'admin');
198
									unset($sub_tpl_component);
199
								}
200
							}
201
						}
202
					}
203
				}
204
			}
205
		 }
206
		 else {
207
		 	$tpl_component['CATEGORY_TAG_ID'] = 1;
208
		 }
209
		 $output = wpshop_display::display_template_element('wpshop_category_edit_interface_admin', $tpl_component, array(), 'admin');
210
		 echo $output;
211
	}
212
213
	/**
214
	*	Save the different extra fields added for the plugin
215
	*
216
	*	@param integer $category_id The category identifier we want to save extra fields for
217
	*	@param mixed $tt_id
218
	*
219
	*	@return void
220
	*/
221
	public static function category_fields_saver($category_id, $tt_id){
0 ignored issues
show
Unused Code introduced by
The parameter $tt_id 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...
222
		global $wpdb;
223
		$category_meta = array();
0 ignored issues
show
Unused Code introduced by
$category_meta is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
224
		$category_option = get_option( WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '_' . $category_id);
225
226
		$wps_category_picture_id = !empty($_POST['wps_category_picture_id']) ? (int) $_POST['wps_category_picture_id'] : null;
227
		$filterable_attribute_for_category = ( !empty($_POST['filterable_attribute_for_category']) && is_array($_POST['filterable_attribute_for_category']) ) ? (array) $_POST['filterable_attribute_for_category'] : null;
228
229
		if ( isset( $wps_category_picture_id ) ) {
230
			$attach_id = intval( $wps_category_picture_id );
231
			$category_option['wpshop_category_picture'] = $attach_id;
232
		}
233
234
		if ( isset( $filterable_attribute_for_category ) ) {
235
			$category_option['wpshop_category_filterable_attributes'] = $filterable_attribute_for_category;
236
		}
237
		else {
238
			$category_option['wpshop_category_filterable_attributes'] = array();
239
		}
240
		update_option(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '_' . $category_id, $category_option);
241
242
		/** Update filter values **/
243
		$wpshop_filter_search = new wps_filter_search();
244
		$wpshop_filter_search->stock_values_for_attribute( array($category_id) );
245
	}
246
247
	/**
248
	*	Add extra column to categories listing interface
249
	*
250
	*	@param array $columns Actual columns to add extra columns to
251
	*
252
	*	@return array $columns The new array with additionnal colu
253
	*/
254
	public static function category_manage_columns($columns){
255
	    unset( $columns["cb"] );
256
257
	    $custom_array = array(
258
				'cb' => '<input type="checkbox" />',
259
				'wpshop_category_thumbnail' => __('Thumbnail', 'wpshop')
260
	    );
261
262
	    $columns = array_merge( $custom_array, $columns );
263
264
	    return $columns;
265
	}
266
267
	/**
268
	*	Define the content of extra columns to add to categories listing interface
269
	*/
270
	public static function category_manage_columns_content($string, $column_name, $category_id){
271
		$category_meta_information = get_option(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '_' . $category_id);
272
		$category_thumbnail_preview = '<img src="' .WPSHOP_DEFAULT_CATEGORY_PICTURE. '" alt="No picture" class="category_thumbnail_preview" />';
273
		/*	Check if there is already a picture for the selected category	*/
274 View Code Duplication
		if ( !empty($category_meta_information['wpshop_category_picture']) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
275
			$image_post = wp_get_attachment_image( $category_meta_information['wpshop_category_picture'], 'thumbnail', false, array('class' => 'category_thumbnail_preview') );
276
			$category_thumbnail_preview = ( !empty($image_post) ) ? $image_post : '<img src="' .WPSHOP_DEFAULT_CATEGORY_PICTURE. '" alt="No picture" class="category_thumbnail_preview" />';
277
		}
278
		$category = get_term_by('id', $category_id, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
279
		$name = $category->name;
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
280
281
		$image = $category_thumbnail_preview;
282
    	return $image;
283
	}
284
285
286
	/**
287
	*	Display a category in a list
288
	*
289
	*	@param object $category The category definition
290
	*	@param string $output_type The output type defined from plugin option
291
	*
292
	*	@return mixed $content Output the category list
293
	*/
294
	public static function category_mini_output($category, $output_type = 'list'){
295
		$content = '';
296
		/*	Get the different informations for output	*/
297
		$category_meta_information = ( !empty($category) && !empty($category->term_id) ) ? get_option(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '_' . $category->term_id) : '';
298
		$categoryThumbnail = '<img src="' .WPSHOP_DEFAULT_CATEGORY_PICTURE. '" alt="No picture" class="wps-category-thumbnail" />';
299
		/*	Check if there is already a picture for the selected category	*/
300 View Code Duplication
		if ( !empty($category_meta_information['wpshop_category_picture']) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
301
			$image_post = wp_get_attachment_image( $category_meta_information['wpshop_category_picture'], 'wps-categorie-display', false, array('class' => 'wps-category-thumbnail') );
302
			$categoryThumbnail = ( !empty($image_post) ) ? $image_post : '<img src="' .WPSHOP_DEFAULT_CATEGORY_PICTURE. '" alt="No picture" class="wps-category-thumbnail" />';
303
		}
304
305
306
		$category_title = ( !empty($category) && !empty($category->name) ) ? $category->name : '';
307
		$category_more_informations = ( !empty($category) && !empty($category->description) ) ? wp_trim_words( $category->description, 30, ' [...]' ) : '';
308
		$category_link = ( !empty($category) && !empty($category->term_id) ) ?  get_term_link((int)$category->term_id , WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES) : '';
309
310
		// $item_width = null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
311
		// /*	Make some treatment in case we are in grid mode	*/
312
		// if($output_type == 'grid'){
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
313
		// 		Determine the width of a component in a line grid
314
		// 	$element_width = (100 / WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
39% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
315
		// 	$item_width = (round($element_width) - 1) . '%';
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
316
		// }
317
318
		/*
319
		 * Template parameters
320
		 */
321
		//$template_part = 'category_mini_' . $output_type;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
322
		$template_part = 'category_mini';
323
		$tpl_component = array();
324
		$tpl_component['CATEGORY_LINK'] = $category_link;
325
		$tpl_component['CATEGORY_THUMBNAIL'] = $categoryThumbnail;
326
		$tpl_component['CATEGORY_TITLE'] = $category_title;
327
		$tpl_component['CATEGORY_DESCRIPTION'] = $category_more_informations;
328
		//$tpl_component['ITEM_WIDTH'] = $item_width;
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
329
		$tpl_component['CATEGORY_ID'] = ( !empty($category) && !empty($category->term_id) ) ? $category->term_id : '';
330
		$tpl_component['CATEGORY_DISPLAY_TYPE'] = $output_type;
331
332
		/*
333
		 * Build template
334
		 */
335
		$tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
336
		if ( $tpl_way_to_take[0] && !empty($tpl_way_to_take[1]) ) {
337
			/*	Include the old way template part	*/
338
339
			ob_start();
340
			require(wpshop_display::get_template_file($tpl_way_to_take[1]));
341
			$content = ob_get_contents();
342
			ob_end_clean();
343
		}
344
		else {
345
			//echo $template_part.'-'.$tpl_component.'<br>';
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
346
			$content = wpshop_display::display_template_element($template_part, $tpl_component);
347
		}
348
		unset($tpl_component);
349
350
		return $content;
351
	}
352
353
	/**
354
	* Traduit le shortcode et affiche une cat�gorie
355
	* @param array $atts : tableau de param�tre du shortcode
356
	* @return mixed
357
	**/
358
	public static function wpshop_category_func($atts) {
359
		global $wpdb;
360
		$string = '';
361
		if ( !empty($atts['cid']) ) {
362
			$atts['type'] = (!empty($atts['type']) && in_array($atts['type'],array('grid','list'))) ? $atts['type'] : 'grid';
363
364
			$cat_list = explode(',', $atts['cid']);
365
366
			if ( (count($cat_list) > 1) || ( !empty($atts['display']) && ($atts['display'] == 'only_cat') ) ) {
367
				if( count($cat_list) == 1) {
368
					$args = array('taxonomy' => WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES, 'parent' => $cat_list[0]);
369
					$categories = get_terms( $args );
370
					foreach($categories as $category) {
371
						$cat_list[] = $category->term_id;
372
					}
373
				}
374
				$string .= '
375
					<div class="wpshop_categories_' . $atts['type'] . '" >';
376
					foreach( $cat_list as $cat_id ){
377
						$sub_category_def = get_term($cat_id, WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
378
						$string .= wpshop_categories::category_mini_output($sub_category_def, $atts['type']);
379
					}
380
				$string .= '
381
					</div>';
382
			}
383
			else {
384
				$sub_category_def = get_term($atts['cid'], WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES);
385
386
				if ( empty($atts['display']) || ($atts['display'] != 'only_products') ){
387
					$string .= wpshop_categories::category_mini_output($sub_category_def, $atts['type']);
388
					$string .= '
389
					<div class="category_product_' . $atts['type'] . '" >
390
						<h2 class="category_content_part_title" >'.__('Category\'s product list', 'wpshop').'</h2>';
391
				}
392
393
				$string .= wpshop_products::wpshop_products_func($atts);
394
395
				if ( empty($atts['display']) || ($atts['display'] != 'only_products') ){
396
					$string .= '</div>';
397
				}
398
			}
399
		}
400
		else {
401
			$string .= __('No categories found for display', 'wpshop');
402
		}
403
404
		return do_shortcode($string);
405
	}
406
407
	public static function get_product_of_category( $category_id ) {
408
		$product_id_list = array();
409
		if ( !empty($category_id) ) {
410
			global $wpdb;
411
			$query = $wpdb->prepare("SELECT T.* FROM " . $wpdb->term_relationships . " AS T INNER JOIN " . $wpdb->posts . " AS P ON ((P.ID = T.object_id) AND (P.post_status = %s)) WHERE T.term_taxonomy_id = %d ", 'publish', $category_id);
412
			$relationships = $wpdb->get_results($query);
413
			if ( !empty($relationships) ) {
414
				foreach ( $relationships as $relationship ) {
415
					$product_id_list[] = $relationship->object_id;
416
				}
417
			}
418
		}
419
		return $product_id_list;
420
	}
421
422
	/**
423
	 * Get the category thumbnail by the category id. Get the option
424
	 * wpshop_product_category_$id, check if the option is an array and if the key
425
	 * wpshop_category_picture(id post value) it's not empty, if it is the case set the
426
	 * previously value in $id_picture. Use wp_get_attachment_image_src with the
427
	 * $id_picture for get the informations of attachment like: url, with, height and
428
	 * resized image (true for resized image, false if it is the original).
429
	 *
430
	 * @see get_option
431
	 * @see wp_get_attachment_image_src
432
	 * @param unknown_type $id
433
	 * @param unknown_type $size
434
	 * @param unknown_type $attr
0 ignored issues
show
Bug introduced by
There is no parameter named $attr. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
435
	 * @return (string or array)
0 ignored issues
show
Documentation introduced by
The doc-type (string could not be parsed: Expected ")" at position 2, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
436
	 */
437
	public static function get_the_category_thumbnail($id, $size = 'thumbnail', $icon = false) {
438
		/** Get the attachment/post ID */
439
		$array_option_category 	= get_option('wpshop_product_category_' . $id);
440
441
		/** If not attachment/post ID in the category, return "No thumbnail in the category" */
442
		if(is_array($array_option_category) && empty($array_option_category['wpshop_category_picture']))
443
			return __('No thumbnail in the category', 'wpshop');
444
445
		/** Set attachment/post ID in $id_picture */
446
		$id_picture = $array_option_category['wpshop_category_picture'];
447
448
		/**
449
		 * Set the post thumbnail in $post_thumbnail
450
		 * @get_the_post_thumbnail - WordPress function
451
		 */
452
		$post_thumbnail = wp_get_attachment_image_src($id_picture, $size, $icon);
453
454
		if(!$post_thumbnail)
455
			return __('No thumbnail in this post', 'wpshop');
456
457
		return $post_thumbnail;
458
	}
459
}
460