Completed
Push — master ( 37d93b...e0e7fb )
by
unknown
18:13
created

wps_dashboard_ctr::add_dashboard_metaboxes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
eloc 8
nc 1
nop 0
1
<?php if ( !defined( 'ABSPATH' ) ) exit;
2
/**
3
 * File for installer control class definition
4
 *
5
 * @author Development team <[email protected]>
6
 * @version 1.0
7
 *
8
 */
9
10
/**
11
 * Class for installer control
12
 *
13
 * @author Development team <[email protected]>
14
 * @version 1.0
15
 *
16
 */
17
class wps_dashboard_ctr {
18
19
	/**
20
	 * Instanciate the module controller
21
	 */
22
	function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
23
// 		add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts'), '', '', true );
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
24
	}
25
26
	function add_scripts() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
		add_action( 'admin_print_scripts', array($this, 'admin_print_script') );
28
	}
29
30
	function admin_print_script() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31
		echo "<div id=\"fb-root\"></div>
32
			<script type=\"text/javascript\">(function(d, s, id) {
33
			  var js, fjs = d.getElementsByTagName(s)[0];
34
			  if (d.getElementById(id)) return;
35
			  js = d.createElement(s); js.id = id;
36
			  js.src = \"//connect.facebook.net/fr_FR/sdk.js#xfbml=1&version=v2.0\";
37
			  fjs.parentNode.insertBefore(js, fjs);
38
			}(document, 'script', 'facebook-jssdk'));</script>'";
39
	}
40
41
	/**
42
	 * DISPLAY - Display wpshop dashboard
43
	 */
44
	function display_dashboard() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
		global $order_status, $wpdb;
46
47
		$this->add_dashboard_metaboxes();
48
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, "backend", "dashboard" ) );
49
	}
50
51
	function wpshop_dashboard_orders() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
52
		$output = '';
0 ignored issues
show
Unused Code introduced by
$output 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...
53
		$orders = get_posts( array( 'posts_per_page' => 10, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ORDER, 'post_status' => 'publish', 'orderby' => 'post_date', 'order' => 'DESC') );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $orders is correct as get_posts(array('posts_p...e', 'order' => 'DESC')) (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...
Unused Code introduced by
$orders 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...
54
		// Display orders
55
		ob_start();
56
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, "backend", "wps_orders_on_dashboard" ) );
57
		$output = ob_get_contents();
58
		ob_end_clean();
59
60
61
		return $output;
62
	}
63
64 View Code Duplication
	function wpshop_rss_feed() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
65
		$output = '';
66
		include_once( ABSPATH . WPINC . '/feed.php' );
67
68
		$rss = fetch_feed( 'http://www.wpshop.fr/feed/' );
69
		if( ! is_wp_error( $rss ) ){
70
			$maxitems = $rss->get_item_quantity( 4 );
71
			$rss_items = $rss->get_items( 0, $maxitems );
72
		}
73
		else {
74
			$output .= '<p>' . __('WPShop News cannot be loaded', 'wpshop') . '</p>';
75
		}
76
77
		if ( $maxitems == 0 ) {
0 ignored issues
show
Bug introduced by
The variable $maxitems 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...
78
			$output .= '<p>' . __('No WPShop new has been found', 'wpshop') . '</p>';
79
		}
80
		else {
81
			$output .= '<ul class="recent-orders">';
82
			foreach ( $rss_items as $item ) {
0 ignored issues
show
Bug introduced by
The variable $rss_items 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...
83
				$output .= '<li><a href="' .$item->get_permalink() . '" title="' .$item->get_title(). '" target="_blank">' .$item->get_title(). '</a><br/>';
84
				$output .= $item->get_content();
85
				$output .= '</li>';
86
			}
87
			$output .= '</ul>';
88
		}
89
		echo $output;
90
	}
91
92
	function wpshop_rss_tutorial_videos() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
93
		$ini_get_checking = ini_get( 'allow_url_fopen' );
94
95
		if ( $ini_get_checking != 0 ) {
96
			$content = @file_get_contents( 'http://www.wpshop.fr/rss_video.xml' );
97
			$videos_rss = ( $content !== false ) ? new SimpleXmlElement( $content ) : null;
98
			if ( !empty($videos_rss) && !empty($videos_rss->channel) ) {
99
				$videos_items = array();
100
				foreach( $videos_rss->channel->item as $i => $item ) {
101
					$videos_items[] = $item;
102
				}
103
				$rand_element = array_rand( $videos_items );
0 ignored issues
show
Unused Code introduced by
$rand_element 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...
104
105
				ob_start();
106
				require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, "backend", "dashboard", "videos" ) );
107
				$output = ob_get_contents();
108
				ob_end_clean();
109
			}
110
			else {
111
				$output = __('No tutorial videos can be loaded', 'wpshop' );
112
			}
113
		}
114
		else {
115
			$output = __( 'Your servor doesn\'t allow to open external files', 'wpshop');
116
		}
117
118
		echo $output;
119
	}
120
121 View Code Duplication
	function wpshop_dashboard_get_changelog() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
122
		$readme_file = fopen( WPSHOP_DIR.'/readme.txt', 'r' );
123
		if ( $readme_file ) {
124
			$txt = file_get_contents( WPSHOP_DIR.'/readme.txt' );
125
			$pre_change_log = explode( '== Changelog ==', $txt );
126
			$versions = explode( '= Version', $pre_change_log[1] );
127
128
			echo $versions[1];
129
		}
130
	}
131
132
	/**
133
	 * Add custom metaboxes to WPShop dashboard
134
	 */
135
	function add_dashboard_metaboxes() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
136
		add_meta_box( 'wps-right-now', '<i class="dashicons dashicons-info"></i>' . esc_html( 'Right Now', 'wpshop' ), array( $this, 'wps_dashboard_right_now' ), 'wpshop_dashboard', 'left_column' );
137
		add_meta_box( 'wps-dashboard-quick-links', '<i class="dashicons dashicons-performance"></i>' . esc_html( 'Quick Links', 'wpshop' ), array( $this, 'wps_dashboard_quick_links' ), 'wpshop_dashboard', 'left_column' );
138
		// add_meta_box( 'wps-dashboard-customer-stats', '<i class="dashicons dashicons-chart-pie"></i>' . esc_html( 'Customers stats', 'wpshop' ), array( $this, 'wps_dashboard_customer_stats' ), 'wpshop_dashboard', 'left_column' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
139
		add_meta_box( 'wps-dashboard-export', '<i class="dashicons dashicons-download"></i>' . esc_html( 'CSV export', 'wpshop' ), array( $this, 'wps_dashboard_export' ), 'wpshop_dashboard', 'left_column' );
140
		add_meta_box( 'wps-dashboard-orders', '<i class="dashicons dashicons-cart"></i>' . esc_html( 'Recent Orders', 'wpshop' ), array( $this, 'wps_dashboard_orders' ), 'wpshop_dashboard', 'left_column' );
141
142
		add_meta_box( 'wps-dashboard-statistics', '<i class="dashicons dashicons-chart-area"></i>' . esc_html( 'Statistics', 'wpshop' ), array( $this, 'wps_dashboard_statistics' ), 'wpshop_dashboard', 'right_column' );
143
144
		add_meta_box( 'wps-dashboard-infos', '<i class="dashicons dashicons-heart"></i>' . esc_html( 'WPShop : WordPress e-commerce', 'wpshop' ), array( $this, 'wps_dashboard_infos' ), 'wpshop_dashboard', 'right_column' );
145
		add_meta_box( 'wps-dashboard-feed', '<i class="dashicons dashicons-format-status"></i>' . esc_html( 'WPShop News', 'wpshop' ), array( $this, 'wps_dashboard_feed' ), 'wpshop_dashboard', 'right_column' );
146
	}
147
148
	/**
149
	 * Display metabox with main shop summary
150
	 */
151
	function wps_dashboard_right_now() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
		global $wpdb;
153
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, 'backend', 'metabox', 'right_now' ) );
154
	}
155
156
	/**
157
	 * Display metabox with quick links
158
	 */
159
	function wps_dashboard_quick_links() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
160
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, 'backend', 'metabox', 'quicklinks' ) );
161
	}
162
163
	/**
164
	 * Display metabox with quick links
165
	 */
166
	function wps_dashboard_customer_stats() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
167
		global $wpdb;
168
	}
169
170
	/**
171
	 * Display metabox with shop main statistics
172
	 */
173
	function wps_dashboard_statistics() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
174
		global $wpdb, $current_month_offset;
175
176
		$current_month_offset = (int) current_time( 'm' );
177
		$current_month_offset = isset( $_GET['month'] ) ? (int) $_GET['month'] : $current_month_offset;
178
179
		$current_month_start = date( 'Y-m-d 00:00:00', strtotime( 'first day of this month', time() ) );
180
		$current_month_end = date( 'Y-m-d 23:59:59', strtotime( 'last day of this month', time() ) );
181
182
		$last_month_start = date( 'Y-m-d 00:00:00', strtotime( 'first day of last month', time() ) );
183
		$last_month_end = date( 'Y-m-d 23:59:59', strtotime( 'last day of last month', time() ) );
184
		$one_month_ago = date( 'Y-m-d 23:59:59', strtotime( '-1 month', time() ) );
185
186
		$dates = array(
0 ignored issues
show
Unused Code introduced by
$dates 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...
187
			__( 'Current month', 'wpshop' ) => array(
188
				'after'			=> $current_month_start,
189
				'before'		=> $current_month_end,
190
				'inclusive'	=> true,
191
			),
192
			sprintf( __( 'One month ago (%s)', 'wpshop' ), mysql2date( get_option( 'date_format' ), $one_month_ago, true ) ) => array(
193
				'after'			=> $last_month_start,
194
				'before'		=> $one_month_ago,
195
				'inclusive'	=> true,
196
			),
197
			__( 'Last month', 'wpshop' ) => array(
198
				'after'			=> $last_month_start,
199
				'before'		=> $last_month_end,
200
				'inclusive'	=> true,
201
			),
202
		);
203
204
		$orders_default_args = array(
0 ignored issues
show
Unused Code introduced by
$orders_default_args 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...
205
			'posts_per_page'	=> -1,
206
			'orderby'					=> 'post_date',
207
			'order'						=> 'DESC',
208
			'post_type'				=> WPSHOP_NEWTYPE_IDENTIFIER_ORDER,
209
			'post_status'			=> 'publish',
210
			'meta_query'			=> array(
211
				'relation'	=> 'OR',
212
				array(
213
					'key'			=> '_order_postmeta',
214
					'value'		=> 's:12:"order_status";s:9:"completed";',
215
					'compare'	=> 'LIKE',
216
				),
217
				array(
218
					'key'			=> '_order_postmeta',
219
					'value'		=> 's:12:"order_status";s:7:"shipped";',
220
					'compare'	=> 'LIKE',
221
				),
222
			),
223
		);
224
225
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, 'backend', 'metabox', 'statistics' ) );
226
	}
227
228
	/**
229
	 * Display metabox with shop main statistics
230
	 */
231
	function wps_dashboard_infos() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
232
		global $wpdb;
233
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, 'backend', 'metabox', 'infos' ) );
234
	}
235
236
	/**
237
	 * Display metabox with shop main statistics
238
	 */
239
	function wps_dashboard_feed() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
240
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, 'backend', 'metabox', 'feed' ) );
241
	}
242
243
	/**
244
	 * Display metabox with recent orders list
245
	 */
246
	function wps_dashboard_orders() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
247
		require_once( wpshop_tools::get_template_part( WPS_DASHBOARD_DIR, WPSDASHBOARD_TPL_DIR, 'backend', 'metabox', 'orders' ) );
248
	}
249
250
	/**
251
	 * Display metabox for export
252
	 */
253
	function wps_dashboard_export() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
254
		if ( class_exists( 'wps_export_ctr' ) ) {
255
			$wps_export = new wps_export_ctr();
256
			$wps_export->wps_export_tpl();
257
		}
258
	}
259
260
}
261