Completed
Push — master ( 5d016e...a7f281 )
by
unknown
17:36
created

mass_interface3   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 4.92 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 6
loc 122
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A mass_init() 0 5 1
B mass_interface() 0 45 2
A mass_interface_screen_option() 0 13 1
A hidden_columns() 0 6 2
A ajax_scripts() 0 7 1
A ajax_new() 6 23 3

How to fix   Duplicated Code   

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:

1
<?php
2
if ( ! class_exists( 'WP_List_Table' ) ) {
3
	require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
4
}
5
if ( ! class_exists( 'WP_Posts_List_Table' ) ) {
6
	require_once( ABSPATH . 'wp-admin/includes/class-wp-posts-list-table.php' );
7
}
8
new mass_interface3();
9
class mass_interface3 {
10
	private $post_type_object;
11
	private $wp_list_table;
12
	public $show_columns = array(
13
		'cb',
14
		'title',
15
		'product_price',
16
		'price_ht',
17
		'product_stock',
18
		'product_reference',
19
		'tx_tva',
20
		'manage_stock',
21
		'product_weight',
22
	);
23
	public $exclude_attribute_codes = array(
24
		'product_attribute_set_id',
25
		'price_behaviour',
26
	);
27
	public function __construct() {
28
		add_action( 'init', array( $this, 'mass_init' ) );
29
		add_action( 'wp_ajax_wps_mass_3_new', array( $this, 'ajax_new' ) );
30
	}
31
	public function mass_init() {
32
		$hook = add_submenu_page( 'edit.php?post_type=' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, __( 'Mass product edit 3', 'wpshop' ), __( 'Mass product edit 3', 'wpshop' ), 'manage_options', 'mass_edit_interface3', array( $this, 'mass_interface' ) );
33
		add_action( "load-{$hook}", array( $this, 'mass_interface_screen_option' ) );
34
		add_action( "admin_print_scripts-{$hook}", array( $this, 'ajax_scripts' ) );
35
	}
36
	public function mass_interface() {
37
		$this->wp_list_table->prepare_items( $this->exclude_attribute_codes );
38
		add_filter( 'default_hidden_columns', array( $this, 'hidden_columns' ), 10, 2 );
39
		$this->wp_list_table->screen->render_screen_meta();
40
		?>
41
		<div class="wrap">
42
		<h1 class="wp-heading-inline"><?php
43
		echo esc_html( $this->post_type_object->labels->name );
44
		?></h1>
45
46
		<?php
47
		if ( current_user_can( $this->post_type_object->cap->create_posts ) ) {
48
			echo ' <a href="' . esc_url( admin_url( $post_new_file ) ) . '" class="page-title-action" onclick="addPost(event, this)">' . esc_html( $this->post_type_object->labels->add_new ) . '</a>';
0 ignored issues
show
Bug introduced by
The variable $post_new_file does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
		}
50
		?>
51
		<form id="posts-filter" method="get">
52
		<?php $this->wp_list_table->display(); ?>
53
		<table style="display:none;">
54
			<tbody id="posts-add">
55
				<tr id="inline-edit" class="inline-edit-row inline-edit-row-<?php echo "post inline-edit-{$this->post_type_object->name} quick-edit-row quick-edit-row-post inline-edit-{$this->post_type_object->name}"; ?>" style="display: none">
56
					<td colspan="<?php echo $this->wp_list_table->get_column_count(); ?>" class="colspanchange">
57
						<fieldset class="inline-edit-col">
58
							<legend class="inline-edit-legend"><?php echo esc_html( $this->post_type_object->labels->add_new ) ?></legend>
59
							<div class="inline-edit-col">
60
								<label>
61
									<span class="title"><?php _e( 'Title' ); ?></span>
62
									<span class="input-text-wrap"><input type="text" name="post_title" class="ptitle" value="" /></span>
63
								</label>
64
							</div>
65
						</fieldset>
66
						<p class="submit inline-edit-save">
67
							<button type="button" class="button cancel alignleft"><?php _e( 'Cancel' ); ?></button>
68
							<button type="button" class="button button-primary save alignright"><?php echo esc_html( $this->post_type_object->labels->add_new ); ?></button>
69
							<span class="spinner"></span>
70
							<span class="error" style="display:none"></span>
71
							<br class="clear" />
72
						</p>
73
					</td>
74
				</tr>
75
			</tbody>
76
		</table>
77
		</form>
78
		</div>
79
		<?php
80
	}
81
	public function mass_interface_screen_option() {
82
		$this->post_type_object = get_post_type_object( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT );
83
		$this->wp_list_table = new WPS_Mass_List_Table( array(
84
			'screen' => $this->post_type_object->name,
85
		) );
86
		$this->wp_list_table->show_columns = $this->show_columns;
87
		$this->wp_list_table->screen->set_screen_reader_content( array(
88
			'heading_views'      => $this->post_type_object->labels->filter_items_list,
89
			'heading_pagination' => $this->post_type_object->labels->items_list_navigation,
90
			'heading_list'       => $this->post_type_object->labels->items_list,
91
		) );
92
		$this->wp_list_table->screen->add_option( 'per_page', array( 'default' => 20, 'option' => "edit_{$this->post_type_object->name}_per_page" ) );
93
	}
94
	public function hidden_columns( $hidden, $screen ) {
95
		if( $screen == $this->wp_list_table->screen ) {
96
			$hidden = array_diff( array_flip( $this->wp_list_table->get_columns() ), $this->show_columns );
97
		}
98
		return $hidden;
99
	}
100
	public function ajax_scripts() {
101
		wp_enqueue_script(
102
			'mass_interface3-ajax', plugin_dir_url( __FILE__ ).'interface3.js',
103
			array( 'jquery' ),
104
			true
105
		);
106
	}
107
	public function ajax_new() {
108
		$new_product_id = wp_insert_post( array(
109
				'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
110
				'post_status' => 'publish',
111
				'post_title' => $_POST['title'],
112
		) );
113 View Code Duplication
		if ( !empty( $new_product_id ) ) {
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...
114
			$product_attribute_set_id = ( !empty( $_POST['attributes_set'] ) ) ? intval( $_POST['attributes_set'] ) : 1;
115
			update_post_meta( $new_product_id, '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute_set_id', $product_attribute_set_id );
116
		} else {
117
			wp_die( 1 );
118
		}
119
		$this->post_type_object = get_post_type_object( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT );
120
		$this->wp_list_table = new WPS_Mass_List_Table( array(
121
			'screen' => $this->post_type_object->name,
122
		) );
123
		$this->wp_list_table->show_columns = $this->show_columns;
124
		$data = $this->wp_list_table->request( $this->exclude_attribute_codes, $new_product_id );
125
		add_filter( 'default_hidden_columns', array( $this, 'hidden_columns' ), 10, 2 );
126
		ob_start();
127
		$this->wp_list_table->single_row( $data[0] );
128
		wp_send_json_success( array( 'row' => ob_get_clean() ) );
129
	}
130
}
131
class WPS_Mass_List_Table extends WP_List_Table {
132
	public static $wpsdb_values_options = array();
133
	public $columns_items = array();
134
	public $show_columns = array();
135
	public $screen;
136
	public function __construct( $args ) {
137
		parent::__construct( array(
138
			'plural'	=> 'posts',
139
			'ajax'		=> true,
140
			'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
141
		) );
142
	}
143
	public function get_columns() {
144
		$columns = array(
145
			'cb'       => '<input type="checkbox" />',
146
			'title'    => __( 'Title' ),
147
		);
148
		foreach ( $this->columns_items as $column => $data_column ) {
149
			$columns[ $column ] = $data_column['name'];
150
		}
151
		return $columns;
152
	}
153
	protected function get_sortable_columns() {
154
		$sortable_columns = array(
155
			'title'    => array( 'title', false ),
156
		);
157
		foreach ( $this->columns_items as $column => $data_column ) {
158
			$sortable_columns[ $column ] = array( $data_column['code'], false );
159
		}
160
		return $sortable_columns;
161
	}
162
	public function column_default( $item, $column_name ) {
163
		if ( isset( $this->columns_items[ $column_name ] ) && is_callable( array( $this, "column_data_{$this->columns_items[ $column_name ]['type']}" ) ) ) {
164
			return call_user_func( array( $this, "column_data_{$this->columns_items[ $column_name ]['type']}" ),
165
				$this->columns_items[ $column_name ]['id'],
166
				$this->columns_items[ $column_name ]['code'],
167
				$item
168
			);
169
		}
170
		return print_r( $item[ $column_name ], true );
171
	}
172
	public function column_cb( $item ) {
173
		return sprintf(
174
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
175
			$this->_args['singular'],
176
			$item['ID']
177
		);
178
	}
179
	public function column_title( $item ) {
180
		return sprintf(
181
			'<a class="row-title" href="%s" aria-label="%s">%s</a>',
182
			get_edit_post_link( $item['ID'] ),
183
			esc_attr( sprintf( __( '&#8220;%s&#8221; (Edit)' ), $item['title'] ) ),
184
			$item['title']
185
		);
186
	}
187
	public function column_data_text( $attribute_id, $attribute_code, $item ) {
188
		$unit = '';
189 View Code Duplication
		if ( is_array( $item[ $attribute_code ] ) ) {
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...
190
			$unit = ' ' . $item[ $attribute_code ]['unit'];
191
			$value = $item[ $attribute_code ]['value'];
192
		} else {
193
			$value = $item[ $attribute_code ];
194
		}
195
		return sprintf(
196
			'<input type="text" name="%1$s[%2$s]" value="%3$s">%4$s',
197
			$attribute_code,
198
			$item['ID'],
199
			$value,
200
			$unit
201
		);
202
	}
203
	public function column_data_select( $attribute_id, $attribute_code, $item ) {
204
		$unit = '';
205 View Code Duplication
		if ( is_array( $item[ $attribute_code ] ) ) {
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...
206
			$unit = ' ' . $item[ $attribute_code ]['unit'];
207
			$value = $item[ $attribute_code ]['value'];
208
		} else {
209
			$value = $item[ $attribute_code ];
210
		}
211
		$select_items = array();
212
		foreach ( $this->get_select_items_option( $attribute_id ) as $item ) {
213
			$select_items[] = "<option value=\"{$item['id']}\"" . selected( $value, $item['id'], false ) . ">{$item['label']}</option>";
214
		}
215
		$select_items = implode( '', $select_items );
216
		return "<select name='{$attribute_code}[{$item['ID']}]'>{$select_items}</select>{$unit}";
217
	}
218
	public function request( $exclude_attribute_codes, $id_post = null ) {
219
		global $wpdb;
220
		$per_page = $this->get_items_per_page( 'edit_' . $this->screen->post_type . '_per_page' );
221
		$exclude_states = get_post_stati( array(
222
			'show_in_admin_all_list' => false,
223
		) );
224
		$exclude_states = implode( "','", $exclude_states );
225
		$orderby = isset( $_REQUEST['orderby'] ) ? esc_sql( $_REQUEST['orderby'] ) : 'p.post_date';
226
		$order = isset( $_REQUEST['order'] )  ? esc_sql( $_REQUEST['order'] ) : 'DESC';
227
		$exclude_attribute_codes = implode( "','", $exclude_attribute_codes );
228
		$entity_id = wpshop_entities::get_entity_identifier_from_code( $this->screen->post_type );
229
		$extra = '';
230
		if( !is_null( $id_post ) ) {
231
			$id_post = intval( $id_post );
232
			$extra = "AND p.ID = {$id_post}";
233
		}
234
		$wpsdb_attribute = WPSHOP_DBT_ATTRIBUTE;
235
		$wpsdb_unit = WPSHOP_DBT_ATTRIBUTE_UNIT;
236
		$wpsdb_values_decimal = WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL;
237
		$wpsdb_values_datetime = WPSHOP_DBT_ATTRIBUTE_VALUES_DATETIME;
238
		$wpsdb_values_integer = WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER;
239
		$wpsdb_values_varchar = WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR;
240
		$wpsdb_values_text = WPSHOP_DBT_ATTRIBUTE_VALUES_TEXT;
241
		$datas = $wpdb->get_results(
242
			$wpdb->prepare(
243
				"SELECT SQL_CALC_FOUND_ROWS
244
				p.ID,
245
				p.post_title as title,
246
				GROUP_CONCAT(
247
					CONCAT(
248
						{$wpsdb_attribute}.id, ':',
249
						{$wpsdb_attribute}.code, ':',
250
						{$wpsdb_attribute}.frontend_label, ':',
251
						CONCAT(
252
							IFNULL( {$wpsdb_values_decimal}.value, '' ),
253
							IFNULL( {$wpsdb_values_datetime}.value, '' ),
254
							IFNULL( {$wpsdb_values_integer}.value, '' ),
255
							IFNULL( {$wpsdb_values_text}.value, '' ),
256
							IFNULL( {$wpsdb_values_varchar}.value, '' )
257
						), ':',
258
						{$wpsdb_attribute}.is_requiring_unit, ':',
259
						IFNULL( {$wpsdb_unit}.unit, '' ), ':',
260
						{$wpsdb_attribute}.frontend_input
261
					) SEPARATOR ';'
262
				) as data
263
				FROM {$wpdb->posts} p
264
				LEFT JOIN {$wpsdb_attribute} ON {$wpsdb_attribute}.status = 'valid' AND {$wpsdb_attribute}.entity_id = %d AND {$wpsdb_attribute}.code NOT IN ( '{$exclude_attribute_codes}' )
265
				LEFT JOIN {$wpsdb_values_decimal} ON {$wpsdb_values_decimal}.attribute_id = {$wpsdb_attribute}.id AND {$wpsdb_values_decimal}.entity_id = p.ID
266
				LEFT JOIN {$wpsdb_values_datetime} ON {$wpsdb_values_datetime}.attribute_id = {$wpsdb_attribute}.id AND {$wpsdb_values_datetime}.entity_id = p.ID
267
				LEFT JOIN {$wpsdb_values_integer} ON {$wpsdb_values_integer}.attribute_id = {$wpsdb_attribute}.id AND {$wpsdb_values_integer}.entity_id = p.ID
268
				LEFT JOIN {$wpsdb_values_text} ON {$wpsdb_values_text}.attribute_id = {$wpsdb_attribute}.id AND {$wpsdb_values_text}.entity_id = p.ID
269
				LEFT JOIN {$wpsdb_values_varchar} ON {$wpsdb_values_varchar}.attribute_id = {$wpsdb_attribute}.id AND {$wpsdb_values_varchar}.entity_id = p.ID
270
				LEFT JOIN {$wpsdb_unit} ON (
271
					{$wpsdb_unit}.id = {$wpsdb_values_decimal}.unit_id
272
					OR {$wpsdb_unit}.id = {$wpsdb_values_datetime}.unit_id
273
					OR {$wpsdb_unit}.id = {$wpsdb_values_integer}.unit_id
274
					OR {$wpsdb_unit}.id = {$wpsdb_values_text}.unit_id
275
					OR {$wpsdb_unit}.id = {$wpsdb_values_varchar}.unit_id
276
				)
277
				WHERE p.post_status NOT IN ( '{$exclude_states}' )
278
				AND p.post_type = %s
279
				{$extra}
280
				GROUP BY p.ID
281
				ORDER BY {$orderby} {$order}
282
				LIMIT %d, %d",
283
				$entity_id,
284
				$this->screen->post_type,
285
				( $this->get_pagenum() -1 ) * $per_page,
286
				$per_page
287
			),
288
			ARRAY_A
289
		);
290
		return array_map( array( $this, 'data_reorganize' ), $datas );
291
	}
292
	public function prepare_items( $exclude_attribute_codes, $id_post = null ) {
293
		global $wpdb;
294
		$per_page = $this->get_items_per_page( 'edit_' . $this->screen->post_type . '_per_page' );
295
		$this->items = $this->request( $exclude_attribute_codes );
296
		$total_items = (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' );
297
		$columns = $this->get_columns();
298
		$sortable = $this->get_sortable_columns();
299
		$this->_column_headers = array( $columns, array_diff( array_flip( $this->get_columns() ), $this->show_columns ), $sortable );
300
		$this->set_pagination_args( array(
301
			'total_items'	=> $total_items,
302
			'per_page'		=> $per_page,
303
			'total_pages'	=> ceil( $total_items / $per_page ),
304
			'orderby'		=> $orderby,
0 ignored issues
show
Bug introduced by
The variable $orderby does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
305
			'order'			=> $order,
0 ignored issues
show
Bug introduced by
The variable $order does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
306
		) );
307
	}
308
	public function data_reorganize( $item ) {
309
		$values = explode( ';', $item['data'] );
310
		foreach ( $values as $value ) {
311
			$value = explode( ':', $value );
312
			if ( ! isset( $this->columns_items[ $value[1] ] ) ) {
313
				$this->columns_items[ $value[1] ] = array(
314
					'id' => $value[0],
315
					'code' => $value[1],
316
					'name' => $value[2],
317
					'type' => $value[6],
318
				);
319
			}
320
			if ( 'yes' === $value[4] ) {
321
				$item[ $value[1] ] = array(
322
					'value' => $value[3],
323
					'unit' => $value[5],
324
				);
325
			} else {
326
				$item[ $value[1] ] = $value[3];
327
			}
328
		}
329
		unset( $item['data'] );
330
		return $item;
331
	}
332
	public function get_select_items_option( $attribute_id ) {
333
		if ( ! isset( self::$wpsdb_values_options[ $attribute_id ] ) ) {
334
			global $wpdb;
335
			$wpsdb_values_options = WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS;
336
			self::$wpsdb_values_options[ $attribute_id ] = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpsdb_values_options} WHERE attribute_id = %d ORDER BY position", $attribute_id ), ARRAY_A );
337
		}
338
		return self::$wpsdb_values_options[ $attribute_id ];
339
	}
340
}
341
?>
342