Completed
Push — master ( a68bd0...85faaf )
by Mike
15:57
created

WC_Meta_Box_Order_Actions::save()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 58
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 29
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 58
rs 7.1977

How to fix   Long Method   

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
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 20 and the first side effect is on line 14.

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
 * Order Actions
4
 *
5
 * Functions for displaying the order actions meta box.
6
 *
7
 * @author      WooThemes
8
 * @category    Admin
9
 * @package     WooCommerce/Admin/Meta Boxes
10
 * @version     2.1.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit; // Exit if accessed directly
15
}
16
17
/**
18
 * WC_Meta_Box_Order_Actions Class.
19
 */
20
class WC_Meta_Box_Order_Actions {
21
22
	/**
23
	 * Output the metabox.
24
	 *
25
	 * @param WP_Post $post
26
	 */
27
	public static function output( $post ) {
28
		global $theorder;
29
30
		// This is used by some callbacks attached to hooks such as woocommerce_order_actions which rely on the global to determine if actions should be displayed for certain orders.
31
		if ( ! is_object( $theorder ) ) {
32
			$theorder = wc_get_order( $post->ID );
33
		}
34
35
		$order_type_object = get_post_type_object( $post->post_type );
36
		?>
37
		<ul class="order_actions submitbox">
38
39
			<?php do_action( 'woocommerce_order_actions_start', $post->ID ); ?>
40
41
			<li class="wide" id="actions">
42
				<select name="wc_order_action">
43
					<option value=""><?php _e( 'Actions', 'woocommerce' ); ?></option>
44
					<optgroup label="<?php esc_attr_e( 'Resend order emails', 'woocommerce' ); ?>">
45
						<?php
46
						$mailer           = WC()->mailer();
47
						$available_emails = apply_filters( 'woocommerce_resend_order_emails_available', array( 'new_order', 'cancelled_order', 'customer_processing_order', 'customer_completed_order', 'customer_invoice', 'customer_refunded_order' ) );
48
						$mails            = $mailer->get_emails();
49
50
						if ( ! empty( $mails ) ) {
51
							foreach ( $mails as $mail ) {
52
								if ( in_array( $mail->id, $available_emails ) && 'no' !== $mail->enabled ) {
53
									echo '<option value="send_email_'. esc_attr( $mail->id ) .'">' . esc_html( $mail->title ) . '</option>';
54
								}
55
							}
56
						}
57
						?>
58
					</optgroup>
59
60
					<option value="regenerate_download_permissions"><?php _e( 'Regenerate download permissions', 'woocommerce' ); ?></option>
61
62
					<?php foreach( apply_filters( 'woocommerce_order_actions', array() ) as $action => $title ) { ?>
63
						<option value="<?php echo $action; ?>"><?php echo $title; ?></option>
64
					<?php } ?>
65
				</select>
66
67
				<button class="button wc-reload" title="<?php esc_attr_e( 'Apply', 'woocommerce' ); ?>"><span><?php _e( 'Apply', 'woocommerce' ); ?></span></button>
68
			</li>
69
70
			<li class="wide">
71
				<div id="delete-action"><?php
72
73
					if ( current_user_can( 'delete_post', $post->ID ) ) {
74
75
						if ( ! EMPTY_TRASH_DAYS ) {
76
							$delete_text = __( 'Delete Permanently', 'woocommerce' );
77
						} else {
78
							$delete_text = __( 'Move to Trash', 'woocommerce' );
79
						}
80
						?><a class="submitdelete deletion" href="<?php echo esc_url( get_delete_post_link( $post->ID ) ); ?>"><?php echo $delete_text; ?></a><?php
81
					}
82
				?></div>
83
84
				<input type="submit" class="button save_order button-primary tips" name="save" value="<?php printf( __( 'Save %s', 'woocommerce' ), $order_type_object->labels->singular_name ); ?>" data-tip="<?php printf( __( 'Save/update the %s', 'woocommerce' ), $order_type_object->labels->singular_name ); ?>" />
85
			</li>
86
87
			<?php do_action( 'woocommerce_order_actions_end', $post->ID ); ?>
88
89
		</ul>
90
		<?php
91
	}
92
93
	/**
94
	 * Save meta box data.
95
	 *
96
	 * @param int $post_id
97
	 * @param WP_Post $post
98
	 */
99
	public static function save( $post_id, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post 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...
100
		global $wpdb;
101
102
		// Order data saved, now get it so we can manipulate status
103
		$order = wc_get_order( $post_id );
104
105
		// Handle button actions
106
		if ( ! empty( $_POST['wc_order_action'] ) ) {
107
108
			$action = wc_clean( $_POST['wc_order_action'] );
109
110
			if ( strstr( $action, 'send_email_' ) ) {
111
112
				do_action( 'woocommerce_before_resend_order_emails', $order );
113
114
				// Ensure gateways are loaded in case they need to insert data into the emails
115
				WC()->payment_gateways();
116
				WC()->shipping();
117
118
				// Load mailer
119
				$mailer = WC()->mailer();
120
121
				$email_to_send = str_replace( 'send_email_', '', $action );
122
123
				$mails = $mailer->get_emails();
124
125
				if ( ! empty( $mails ) ) {
126
					foreach ( $mails as $mail ) {
127
						if ( $mail->id == $email_to_send ) {
128
							$mail->trigger( $order->id );
129
							$order->add_order_note( sprintf( __( '%s email notification manually sent.', 'woocommerce' ), $mail->title ), false, true );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
130
						}
131
					}
132
				}
133
134
				do_action( 'woocommerce_after_resend_order_email', $order, $email_to_send );
135
136
				// Change the post saved message
137
				add_filter( 'redirect_post_location', array( __CLASS__, 'set_email_sent_message' ) );
138
139
			} elseif ( 'regenerate_download_permissions' === $action ) {
140
141
				delete_post_meta( $post_id, '_download_permissions_granted' );
142
				$wpdb->delete( 
143
					$wpdb->prefix . 'woocommerce_downloadable_product_permissions',
144
					array( 'order_id' => $post_id ),
145
					array( '%d' )
146
				);
147
				wc_downloadable_product_permissions( $post_id );
148
149
			} else {
150
151
				if ( ! did_action( 'woocommerce_order_action_' . sanitize_title( $action ) ) ) {
152
					do_action( 'woocommerce_order_action_' . sanitize_title( $action ), $order );
153
				}
154
			}
155
		}
156
	}
157
158
	/**
159
	 * Set the correct message ID.
160
	 *
161
	 * @param string $location
162
	 *
163
	 * @since  2.3.0
164
	 *
165
	 * @static
166
	 *
167
	 * @return string
168
	 */
169
	public static function set_email_sent_message( $location ) {
170
		return add_query_arg( 'message', 11, $location );
171
	}
172
173
}
174