Completed
Push — master ( 5987fc...959a28 )
by
unknown
13:59
created

wps_message_ctr::get_xml_messages()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 6
nop 1
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
1
<?php if ( ! defined( 'ABSPATH' ) ) { exit;
2
}
3
class wps_message_ctr {
4
5
	/** Define the main directory containing the template for the current plugin
6
	 *
7
	 * @var string
8
	 */
9
	private $template_dir;
10
	/**
11
	 * Define the directory name for the module in order to check into frontend
12
	 *
13
	 * @var string
14
	 */
15
	private $plugin_dirname = WPS_MESSAGE_DIR;
0 ignored issues
show
Unused Code introduced by
The property $plugin_dirname is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
	public static $mails_display = 5;
17
	private static $xml_messages = null;
18
	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...
19
20
		/** Js */
21
		add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) );
22
		// End if().
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...
23
		$this->template_dir = WPS_MESSAGE_PATH . WPS_MESSAGE_DIR . '/templates/';
24
		// WP General actions
25
		add_action( 'admin_init', array( $this, 'wps_messages_init_actions' ) );
26
		add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
27
		add_action( 'manage_' . WPSHOP_NEWTYPE_IDENTIFIER_MESSAGE . '_posts_custom_column',  array( $this, 'messages_custom_columns' ) );
28
		add_filter( 'manage_edit-' . WPSHOP_NEWTYPE_IDENTIFIER_MESSAGE . '_columns', array( $this, 'messages_edit_columns' ) );
29
		// Shortcodes
30
		add_shortcode( 'wps_message_histo', array( $this, 'display_message_histo_per_customer' ) );
31
		add_shortcode( 'order_customer_personnal_informations', array( $this, 'order_personnal_informations' ) );
32
		/** Ajax */
33
		add_action( 'wp_ajax_get_content_message', array( $this, 'get_content_message' ) );
34
	}
35
36
	/**
37
	 * For add js
38
	 */
39 View Code Duplication
	public function enqueue_scripts() {
0 ignored issues
show
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...
40
41
		/** Css */
42
		add_thickbox();
43
		wp_register_style( 'wpeo-message-css', WPS_MESSAGE_URL . WPS_MESSAGE_DIR . '/assets/css/frontend.css', '', WPS_MESSAGE_VERSION );
44
		wp_enqueue_style( 'wpeo-message-css' );
45
		/** My js */
46
		wp_enqueue_script( 'wps-message-js', WPS_MESSAGE_URL . WPS_MESSAGE_DIR . '/assets/js/frontend.js', array( 'jquery', 'thickbox' ), WPS_MESSAGE_VERSION );
47
	}
48
49
	/**
50
	 * WPS Messages Admin init actions
51
	 */
52
	function wps_messages_init_actions() {
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...
53
54
		$this->create_message_type();
55
	}
56
57
	/**
58
	 * Create the custom post type Message to manage them
59
	 */
60
	function create_message_type() {
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...
61
62
		register_post_type( WPSHOP_NEWTYPE_IDENTIFIER_MESSAGE, array(
63
			'labels' => array(
64
				'name' => __( 'Message', 'wpshop' ),
65
				'singular_name' => __( 'message', 'wpshop' ),
66
				'add_new' => __( 'Add message', 'wpshop' ),
67
				'add_new_item' => __( 'Add New message', 'wpshop' ),
68
				'edit' => __( 'Edit', 'wpshop' ),
69
				'edit_item' => __( 'Edit message', 'wpshop' ),
70
				'new_item' => __( 'New message', 'wpshop' ),
71
				'view' => __( 'View message', 'wpshop' ),
72
				'view_item' => __( 'View message', 'wpshop' ),
73
				'search_items' => __( 'Search messages', 'wpshop' ),
74
				'not_found' => __( 'No message found', 'wpshop' ),
75
				'not_found_in_trash' => __( 'No message found in trash', 'wpshop' ),
76
				'parent-item-colon' => '',
77
			),
78
			'description' => __( 'This is where store messages are stored.', 'wpshop' ),
79
			'public' => true,
80
			'show_ui' => true,
81
			'capability_type' => 'post',
82
			'publicly_queryable' => false,
83
			'exclude_from_search' => true,
84
			'show_in_menu' => false,
85
			'hierarchical' => false,
86
			'show_in_nav_menus' => false,
87
			'rewrite' => false,
88
			'query_var' => true,
89
			'supports' => array( 'title', 'editor' ),
90
			'has_archive' => false,
91
		) );
92
	}
93
94
	/**
95
	 *	Create the different box for the product management page looking for the attribute set to create the different boxes
96
	 */
97
	function add_meta_boxes() {
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...
98
99
		// Add message sending historic meta box
100
		add_meta_box( 'wpshop_message_histo',
101
			__( 'Message historic', 'wpshop' ),
102
			array( $this, 'message_histo_box' ),
103
		WPSHOP_NEWTYPE_IDENTIFIER_MESSAGE, 'normal', 'high' );
104
	}
105
106
	/**
107
	 * META-BOX CONTENT - Display messages sending historic
108
	 *
109
	 * @param unknown_type $post
110
	 * @param unknown_type $params
111
	 */
112
	function message_histo_box( $post, $params ) {
0 ignored issues
show
Unused Code introduced by
The parameter $params 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...
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...
113
114
		$output  = '<div id="message_histo_container">';
115
		$output .= $this->get_historic_message_by_type( $post->ID );
116
		$output .= '</div>';
117
		echo $output;
118
	}
119
120
	/**
121
	 * Display Message historic by type
122
	 *
123
	 * @param  integer $message_type_id : Message type ID
124
	 * @return string
125
	 */
126
	function get_historic_message_by_type( $message_type_id ) {
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...
127
128
		global $wpdb;
129
		$output = '';
130
		if ( ! empty( $message_type_id ) ) {
131
			// Recover all sended messages
132
			$wps_message_mdl = new wps_message_mdl();
133
			$messages = $wps_message_mdl->get_messages_histo( $message_type_id );
134
			ob_start();
135
			require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, $this->template_dir, 'backend', 'message_historic' ) );
136
			$output .= ob_get_contents();
137
			ob_end_clean();
138
		}
139
		return $output;
140
	}
141
142
	public static function get_xml_messages( $code = null ) {
143
		if ( is_null( self::$xml_messages ) ) {
144
			$xml_default_emails = file_get_contents( WP_PLUGIN_DIR . '/' . WPSHOP_PLUGIN_DIR . '/assets/datas/default_emails.xml' );
145
			$default_emails = new SimpleXMLElement( $xml_default_emails );
146
			self::$xml_messages = array();
147
			foreach ( $default_emails->xpath( '//emails/email' ) as $email ) {
148
				self::$xml_messages[ (string) $email->attributes()->code ] = array(
149
					'shop_type' => (string) $email->attributes()->shop_type,
150
					'object' => (string) $email->subject,
151
					'message' => (string) $email->content,
152
				);
153
			}
154
		}
155
		if ( is_null( $code ) ) {
156
			return self::$xml_messages;
157
		} elseif ( isset( self::$xml_messages[ $code ] ) ) {
158
			return self::$xml_messages[ $code ];
159
		} else {
160
			return false;
161
		}
162
	}
163
	/**
164
	 * Create all WPShop default messages
165
	 */
166
	public static function create_default_message() {
167
		// Read default emails for options creation
168
		foreach ( self::get_xml_messages() as $code => $email ) {
169
			if ( ( WPSHOP_DEFINED_SHOP_TYPE == $email['shop_type'] ) || ( 'sale' == WPSHOP_DEFINED_SHOP_TYPE ) ) {
170
				self::createMessage( $code, $email['object'], $email['message'] );
171
			}
172
		}
173
	}
174
175
	/**
176
	 * Create a message and save its ID in option database table
177
	 *
178
	 * @param string $code : Message code
179
	 * @param string $object : Message object code (Message Title)
180
	 * @param string $message : Message content
181
	 * @return integer message ID
182
	 */
183
	public static function createMessage( $code, $object = '', $message = '' ) {
184
185
		$id = 0;
0 ignored issues
show
Unused Code introduced by
$id 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...
186
		$xml_message = self::get_xml_messages( $code );
187
		$object = empty( $object ) ? $xml_message['object'] : $object;
188
		$message = empty( $message ) ? $xml_message['message'] : $message;
189
		$message_option = get_option( $code, null );
190
		if ( empty( $message_option ) && ! empty( $object ) && ! empty( $message ) ) {
191
			$new_message = array(
192
				'post_title' => __( $object , 'wpshop' ),
193
				'post_content' => self::customize_message( __( $message, 'wpshop' ) ),
194
				'post_status' => 'publish',
195
				'post_author' => 1,
196
				'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_MESSAGE,
197
			);
198
			$id = wp_insert_post( $new_message );
199
			update_option( $code, $id );
200
		} else {
201
			$id = $message_option;
202
		}
203
204
		return $id;
205
	}
206
207
	/**
208
	 * Give the content by column
209
	 *
210
	 * @return array
211
	 */
212
	function messages_custom_columns( $column ) {
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...
213
214
		global $post;
215
		$metadata = get_post_custom();
216
		switch ( $column ) {
217
			case 'extract':
218
				echo wp_trim_words( $post->post_content, 55 );
219
		break;
220
			case 'last_dispatch_date':
221
				if ( ! empty( $metadata['wpshop_message_last_dispatch_date'][0] ) ) {
222
					echo mysql2date( 'd F Y, H:i:s',$metadata['wpshop_message_last_dispatch_date'][0], true );
223
				} else { echo '-';
224
				}
225
		break;
226
		}
227
	}
228
229
	/**
230
	 * Set the custom colums
231
	 *
232
	 * @return array
233
	 */
234
	function messages_edit_columns( $columns ) {
0 ignored issues
show
Unused Code introduced by
The parameter $columns 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...
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...
235
236
		$columns = array(
237
			'cb' => '<input type="checkbox" />',
238
			'title' => __( 'Name', 'wpshop' ),
239
			'extract' => __( 'Extract from the message','wpshop' ),
240
			'date' => __( 'Creation date','wpshop' ),
241
			'last_dispatch_date' => __( 'Last dispatch date','wpshop' ),
242
		);
243
		return $columns;
244
	}
245
246
	/**
247
	 * Manage the display of Messages options configuration panel
248
	 *
249
	 * @param integer $current
250
	 * @return string
251
	 */
252
	function getMessageListOption( $current = 0 ) {
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...
253
254
		$posts = query_posts( array(
255
			'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_MESSAGE,
256
			'posts_per_page' => '-1',
257
		) );
258
		$options = '';
259
		if ( ! empty( $posts ) ) {
260
			$options = '<option value="0">' . __( 'Select values from list', 'wpshop' ) . '</option>';
261
			foreach ( $posts as $p ) {
262
				$selected = $p->ID == $current ? ' selected="selected"': '';
263
				$options .= '<option value="' . $p->ID . '"' . $selected . '>' . $p->post_title . '</option>';
264
			}
265
		}
266
		wp_reset_query();
267
		return $options;
268
	}
269
270
	/**
271
	 * Display all messages which be sended to customer
272
	 *
273
	 * @param array   $args [message_id] : Message type id :
274
	 * @param integer $customer_id : ID to identifiate the customer
275
	 * @return string
276
	 */
277
	function display_message_histo_per_customer( $args, $customer_id = '' ) {
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...
278
279
		$customer_id = ( ! empty( $customer_id ) ) ? $customer_id : get_current_user_id();
280
		$message_id = ( ! empty( $args ) && ! empty( $args['message_id'] ) ) ? $args['message_id'] : '';
281
		$message_elements = '';
282
		$wps_message_mdl = new wps_message_mdl();
283
		$messages_data = $wps_message_mdl->get_messages_histo( $message_id, $customer_id );
284
285
		// ob_start();
286
		// require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, $this->template_dir, "frontend", "messages") );
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% 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...
287
		// $output .= ob_get_contents();
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...
288
		// ob_end_clean();
289
		/**	Order emails	*/
290
		$messages_histo = array();
291
		foreach ( $messages_data as $meta_id => $messages ) :
292
			$i = 0;
293
			foreach ( $messages as $message ) :
294
				$messages_histo[ $message['mess_dispatch_date'][0] ][ $i ]['title'] = $message['mess_title'];
295
				$messages_histo[ $message['mess_dispatch_date'][0] ][ $i ]['message'] = $message['mess_message'];
296
				$messages_histo[ $message['mess_dispatch_date'][0] ][ $i ]['dates'] = $message['mess_dispatch_date'];
297
				if ( ! empty( $message['mess_object_id'] ) ) {
298
					$messages_histo[ $message['mess_dispatch_date'][0] ][ $i ]['object'] = $message['mess_object_id'];
299
				}
300
				$i++;
301
		endforeach;
302
endforeach;
303
		ksort( $messages_histo );
304
		$messages_histo = array_reverse( $messages_histo );
305
		ob_start();
306
		require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, $this->template_dir, 'frontend', 'customer', 'messages' ) );
307
		$output = ob_get_contents();
308
		ob_end_clean();
309
		return $output;
310
	}
311
312
	/**
313
	 * Add the message content and create a HTML structure message
314
	 *
315
	 * @param string $message : message content
316
	 * @return string
317
	 */
318
	public static function customize_message( $message ) {
319
320
		if ( ! empty( $message ) ) {
321
			ob_start();
322
			require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, WPS_MESSAGE_PATH . WPS_MESSAGE_DIR . '/templates/', 'backend', 'message_html_structure' ) );
323
			$message = ob_get_contents();
324
			ob_end_clean();
325
		}
326
		return $message;
327
	}
328
329
	/** Store a new message
330
	 *
331
	 * @return boolean
332
	 */
333
	function add_message( $recipient_id = 0, $email, $title, $message, $model_id, $object, $date = null ) {
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...
334
335
		$date = empty( $date ) ? current_time( 'mysql', 0 ) : $date;
336
		$object_empty = array(
337
			'object_type' => '',
338
			'object_id' => 0,
339
		);
340
		$object = array_merge( $object_empty, $object );
341
		$historic = get_post_meta( $recipient_id, '_wpshop_messages_histo_' . $model_id . '_' . substr( $date, 0, 7 ), true );
342
		$data_to_insert = array(
343
			'mess_user_id' => $recipient_id,
344
			'mess_user_email' => $email,
345
			'mess_object_type' => $object['object_type'],
346
			'mess_object_id' => $object['object_id'],
347
			'mess_title' => $title,
348
			'mess_message' => $message,
349
			'mess_dispatch_date' => array( $date ),
350
		);
351
		$historic[] = $data_to_insert;
352
		update_post_meta( $recipient_id, '_wpshop_messages_histo_' . $model_id . '_' . substr( $date, 0, 7 ), $historic );
353
	}
354
355
	/**
356
	 * Create a custom Message, replace all "shortcodes" informations by dynamic informations
357
	 *
358
	 * @param string  $string : Message text
359
	 * @param array   $data : data to replace
360
	 * @param string  $model_name : Message model name
361
	 * @param boolean $duplicate_message : Duplicate a light message for historic storage
362
	 * @return string
363
	 */
364
	function customMessage( $string, $data, $model_name = '', $duplicate_message = false ) {
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...
365
366
		$avant = array();
367
		$apres = array();
368
		$logo_option = get_option( 'wpshop_logo' );
369
370
		$data['your_shop_logo'] = ( ! empty( $logo_option ) ) ? '<img src="' . $logo_option . '" alt="' . get_bloginfo( 'name' ) . '" />' : '';
371
372
		foreach ( $data as $key => $value ) {
373
			$avant[] = '[' . $key . ']';
374
			switch ( $key ) {
375
				case 'order_content' :
376
					$apres[] = ( $duplicate_message ) ? '[order_content]' : $this->order_content_template_for_mail( $data['order_id'] );
377
					break;
378
				case 'order_addresses' :
379
					$apres[] = ( $duplicate_message ) ? '[order_addresses]' : $this->order_addresses_template_for_mail( $data['order_id'] );
380
					break;
381
382
				case 'order_billing_address' :
383
					$apres[] = ( $duplicate_message ) ? '[order_billing_address]' : $this->order_addresses_template_for_mail( $data['order_id'], 'billing' );
384
					break;
385
386
				case 'order_shipping_address' :
387
					$apres[] = ( $duplicate_message ) ? '[order_shipping_address]' : $this->order_addresses_template_for_mail( $data['order_id'], 'shipping' );
388
					break;
389
390
				case 'order_customer_comments' :
391
					$apres[] = ( $duplicate_message ) ? '[order_customer_comments]' : $this->order_customer_comment_template_for_mail( $data['order_id'] );
392
					break;
393
				case 'order_personnal_informations' :
394
					$apres[] = ( $duplicate_message ) ? '[order_personnal_informations]' : $this->order_personnal_informations();
395
					break;
396
				default :
397
					$apres[] = $value;
398
					break;
399
			}
400
		}
401
		$string = str_replace( $avant, $apres, $string );
402
403
		$string = apply_filters( 'wps_more_customized_message', $string, $data, $duplicate_message );
404
405
		if ( ($model_name != 'WPSHOP_NEW_ORDER_ADMIN_MESSAGE') ) {
406
			$string = preg_replace( '/\[(.*)\]/Usi', '', $string );
407
		}
408
409
		return $string;
410
	}
411
412
	/**
413
	 * Prepared the mail which would be send
414
	 *
415
	 * @param string $email : Receiver e-mail
416
	 * @param string $model_name : Message mmodel name
417
	 * @param array  $data : dynamic data to replace in e-mail
418
	 * @param string $object : message object
419
	 * @param file   $attached_file : File to attached to e-mail
420
	 */
421
	function wpshop_prepared_email( $email, $model_name, $data = array(), $object = array(), $attached_file = '' ) {
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...
422
		global $wpdb;
423
		$data = apply_filters( 'wps_extra_data_to_send_in_email', $data );
424
		$model_id = get_option( $model_name, 0 );
425
		$query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->posts . ' WHERE ID = %s', $model_id );
426
		$post_message = $wpdb->get_row( $query );
427
		$duplicate_message = '';
428
		if ( ! empty( $post_message ) ) {
429
			$title = $this->customMessage( $post_message->post_title, $data, $model_name );
430
			$message = $this->customMessage( $post_message->post_content, $data, $model_name );
431
			// End if().
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...
432
			if ( array_key_exists( 'order_content', $data ) || array_key_exists( 'order_addresses', $data ) || array_key_exists( 'order_customer_comments', $data ) ) {
433
				$duplicate_message = $this->customMessage( $post_message->post_content, $data, $model_name, true );
434
			}
435
			if ( ! empty( $email ) ) {
436
				$this->wpshop_email( $email, $title, $message, true, $model_id, $object, $attached_file, $duplicate_message );
0 ignored issues
show
Bug introduced by
It seems like $object defined by parameter $object on line 421 can also be of type string; however, wps_message_ctr::wpshop_email() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
437
			}
438
		}
439
	}
440
441
	/**
442
	 * Send an e-mail and store it in WPShop Sended e-mails historic
443
	 *
444
	 * @param string  $email : Receiver e-mail
445
	 * @param string  $title : Message title
446
	 * @param string  $message : Message content to send
447
	 * @param boolean $save : save message in historic
448
	 * @param integer $model_id : Message model ID
449
	 * @param array   $object : Message object
450
	 * @param file    $attachments : File to attached to e-mail
451
	 * @param string  $duplicate_message : lighter message to store
452
	 */
453
	function wpshop_email( $email, $title, $message, $save = true, $model_id, $object = array(), $attachments = '', $duplicate_message = '' ) {
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...
454
		global $wpdb;
455
		// Sauvegarde
456
		if ( $save ) {
457
			$user = $wpdb->get_row( 'SELECT ID FROM ' . $wpdb->users . ' WHERE user_email="' . $email . '";' );
458
			$user_id = $user ? $user->ID : get_current_user_id();
459
			$query = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_author = %d AND post_type = %s ', $user_id, WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS );
460
			$user_post_id = $wpdb->get_var( $query );
461
462
			if ( ! empty( $duplicate_message ) ) {
463
				$this->add_message( $user_post_id, $email, $title, $duplicate_message, $model_id, $object );
464
			} else {
465
				$this->add_message( $user_post_id, $email, $title, $message, $model_id, $object );
466
			}
467
		}
468
469
		$emails = get_option( 'wpshop_emails', array() );
0 ignored issues
show
Documentation introduced by
array() is of type array, 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...
470
		$noreply_email = $emails['noreply_email'];
471
		// Split the email to get the name
472
		$vers_nom = substr( $email, 0, strpos( $email,'@' ) );
0 ignored issues
show
Unused Code introduced by
$vers_nom 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...
473
474
		// Headers du mail
475
		$headers = "MIME-Version: 1.0\r\n";
476
		$headers .= "Content-type: text/html; charset=UTF-8\r\n";
477
		$headers .= 'From: ' . get_bloginfo( 'name' ) . ' <' . $noreply_email . '>' . "\r\n";
478
479
		// Mail en HTML
480
		@wp_mail( $email, $title, $message, $headers, $attachments );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
481
482
		if ( ! empty( $attachments ) ) {
483
			unlink( $attachments );
484
		}
485
	}
486
487
	/**
488
	 * Order content Template
489
	 *
490
	 * @param integer $order_id : Order ID
491
	 * @return string
492
	 */
493
	function order_content_template_for_mail( $order_id ) {
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...
494
		$message = '';
495
		if ( ! empty( $order_id ) ) {
496
			$currency_code = wpshop_tools::wpshop_get_currency( false );
497
			$orders_infos = get_post_meta( $order_id, '_order_postmeta', true );
498
			ob_start();
499
			require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, $this->template_dir, 'backend/mails', 'order_content_mail_template' ) );
500
			$message .= ob_get_contents();
501
			ob_end_clean();
502
		}
503
		return $message;
504
	}
505
506
	/**
507
	 * Order Adresses Template for e-mail
508
	 *
509
	 * @param integer $order_id : Order ID
510
	 * @param integer $address_type : Address type ID
511
	 * @return string
512
	 */
513
	function order_addresses_template_for_mail( $order_id, $address_type = '' ) {
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...
514
		global $wpdb;
515
		$shipping_option = get_option( 'wpshop_shipping_address_choice' );
516
		$display_shipping = ( ! empty( $shipping_option ) && ! empty( $shipping_option['activate'] ) ) ? true : false;
517
		$message = '';
518
		if ( ! empty( $order_id ) ) {
519
			$order_addresses = get_post_meta( $order_id, '_order_info', true );
520
			if ( ! empty( $order_addresses ) ) {
521
				foreach ( $order_addresses as $key => $order_address ) {
522
					if ( ! empty( $order_address ) && ( empty( $address_type ) || $address_type == $key ) ) {
523
524
						if ( $key != 'shipping' || ($key == 'shipping' && $display_shipping) ) {
525
							$address_type_title = ( ! empty( $key ) && $key == 'billing' ) ? __( 'Billing address', 'wpshop' ) : __( 'Shipping address', 'wpshop' );
526
							$civility = '';
527
							if ( ! empty( $order_address['address']['civility'] ) ) {
528
								$query = $wpdb->prepare( 'SELECT label FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id = %d', $order_address['address']['civility'] );
529
								$civility = $wpdb->get_var( $query );
530
							}
531
532
							// Address informations
533
							$customer_last_name = ( ! empty( $order_address['address']['address_last_name'] ) ) ? $order_address['address']['address_last_name'] : '';
534
							$customer_firtsname = ( ! empty( $order_address['address']['address_first_name'] ) ) ? $order_address['address']['address_first_name'] : '';
535
							$customer_company = ( ! empty( $order_address['address']['company'] ) ) ? $order_address['address']['company'] : '';
536
							$customer_address = ( ! empty( $order_address['address']['address'] ) ) ? $order_address['address']['address'] : '';
537
							$customer_zip_code = ( ! empty( $order_address['address']['postcode'] ) ) ? $order_address['address']['postcode'] : '';
538
							$customer_city = ( ! empty( $order_address['address']['city'] ) ) ? $order_address['address']['city'] : '';
539
							$customer_state = ( ! empty( $order_address['address']['state'] ) ) ? $order_address['address']['state'] : '';
540
							$customer_phone = ( ! empty( $order_address['address']['phone'] ) ) ? ' Tel. : ' . $order_address['address']['phone'] : '';
541
							$country = '';
542
							foreach ( unserialize( WPSHOP_COUNTRY_LIST ) as $key => $value ) {
543
								if ( ! empty( $order_address['address']['country'] ) && $key == $order_address['address']['country'] ) {
544
									$country = $value;
545
								}
546
							}
547
							$customer_country = $country;
548
549
							ob_start();
550
							require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, $this->template_dir, 'backend/mails', 'order_addresses_template_for_mail' ) );
551
							$message .= ob_get_contents();
552
							ob_end_clean();
553
						}
554
					}
555
				}
556
			}// End if().
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...
557
		}// End if().
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...
558
		return $message;
559
	}
560
561
	/**
562
	 * Order Customer's comment template for e-mail
563
	 *
564
	 * @param integer $order_id : Order ID
565
	 * @return string
566
	 */
567
	function order_customer_comment_template_for_mail( $order_id ) {
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...
568
		global $wpdb;
569
		$message = '';
570
		if ( ! empty( $order_id ) ) {
571
			$query = $wpdb->prepare( 'SELECT post_excerpt FROM ' . $wpdb->posts . ' WHERE ID = %d', $order_id );
572
			$comment = $wpdb->get_var( $query );
573
			$order_infos = get_post_meta( $order_id, '_order_postmeta', true );
574
			if ( ! empty( $order_infos['order_key'] ) ) {
575
				$comment_title = __( 'Comments about the order', 'wpshop' );
576
			} else {
577
				$comment_title = __( 'Comments about the quotation', 'wpshop' );
578
			}
579
			ob_start();
580
			require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, $this->template_dir, 'backend/mails', 'order_email_customer_comments' ) );
581
			$message .= ob_get_contents();
582
			ob_end_clean();
583
		}
584
		return $message;
585
	}
586
587
	/**
588
	 * Order customer's Personnal informations template for e-mail
589
	 *
590
	 * @return string
591
	 */
592
	function order_personnal_informations() {
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...
593
		global $wpdb;
594
		$user_id = get_current_user_id();
595
		$message = '';
596
		$customer_entity = wpshop_entities::get_entity_identifier_from_code( WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS );
597
		if ( ! empty( $customer_entity ) ) {
598
599
			$query = $wpdb->prepare( 'SELECT * FROM ' . WPSHOP_DBT_ATTRIBUTE_SET . ' WHERE entity_id = %d AND status = %s', $customer_entity, 'valid' );
600
			$attributes_sets = $wpdb->get_results( $query );
601
602
			if ( ! empty( $attributes_sets ) ) {
603
				ob_start();
604
				require( wpshop_tools::get_template_part( WPS_MESSAGE_DIR, $this->template_dir, 'backend/mails', 'order_personnal_informations_template_for_mail' ) );
605
				$message .= ob_get_contents();
606
				ob_end_clean();
607
			}
608
		}
609
		return $message;
610
	}
611
612
	/**
613
	 * Support historic messages
614
	 */
615
	function wpshop_messages_historic_correction() {
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...
616
		global $wpdb;
617
		$query = $wpdb->prepare( 'SELECT * FROM ' . $wpdb->postmeta . ' WHERE meta_key LIKE %s', '_wpshop_messages_histo_%' );
618
		$messages_histo = $wpdb->get_results( $query );
619
620
		foreach ( $messages_histo as $message ) {
621
			$query_user = $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_author = %d AND post_type = %s',  $message->post_id, WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS );
622
			$user_post_id = $wpdb->get_var( $query_user );
623
			$wpdb->update( $wpdb->postmeta, array(
624
				'post_id' => $user_post_id,
625
				), array(
626
				'meta_id' => $message->meta_id,
627
			) );
628
		}
629
	}
630
631
	/** Ajax */
632
	/**
633
	 * Récupères le contenu du message
634
	 */
635
	public function get_content_message() {
636
		$_wpnonce = ! empty( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( $_REQUEST['_wpnonce'] ) : '';
637
638
		if ( ! wp_verify_nonce( $_wpnonce, 'get_content_message' ) ) {
639
			wp_die();
640
		}
641
642
		global $wpdb;
643
		$meta_id = (int) $_GET['meta_id'];
644
645
		$result = $wpdb->get_results( $wpdb->prepare( 'SELECT meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_id=%d', array( ($meta_id) ) ) );
646
		$result = unserialize( $result[0]->meta_value );
647
		$result = $result[0]['mess_message'];
648
649
		wp_die( $result );
650
	}
651
652
}
653