Completed
Push — master ( 0e906a...e278b9 )
by
unknown
14:05
created

Mass_Interface3::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Main class for display WPShop Mass interface.
4
 *
5
 * @package wps-mass-interface3
6
 */
7
8
if ( ! defined( 'ABSPATH' ) ) {
9
	exit;
10
}
11
/**
12
 * Mass Interface 3 is kind of controller.
13
 * Construct menu page & use WPS Mass List Table.
14
 */
15
class Mass_Interface3 {
16
	/**
17
	 * Menu page identifier.
18
	 *
19
	 * @var string
20
	 */
21
	public $hook;
22
	/**
23
	 * Post type object.
24
	 *
25
	 * @var stdClass
26
	 */
27
	private $post_type_object;
28
	/**
29
	 * Instance of WPS Mass List Table
30
	 *
31
	 * @var WPS_Mass_List_Table
32
	 */
33
	private $wp_list_table;
34
	/**
35
	 * Default configuration of displayed columns.
36
	 *
37
	 * @var array
38
	 */
39
	public $default_show_columns = array(
40
		'cb',
41
		'title',
42
		'product_price',
43
		'price_ht',
44
		'product_stock',
45
		'product_reference',
46
		'tx_tva',
47
		'manage_stock',
48
		'product_weight',
49
	);
50
	/**
51
	 * Attributes code to exclude.
52
	 *
53
	 * @var array
54
	 */
55
	public $exclude_attribute_codes = array(
56
		'product_attribute_set_id',
57
		'price_behaviour',
58
	);
59
	/**
60
	 * Constructor for menu & screen configuration & ajax actions.
61
	 *
62
	 * @method __construct
63
	 */
64
	public function __construct() {
65
		add_action( 'admin_menu', array( $this, 'mass_init' ), 350 );
66
		add_action( 'wp_ajax_wps_mass_3_new', array( $this, 'ajax_new' ) );
67
		add_action( 'wp_ajax_wps_mass_3_save', array( $this, 'ajax_save' ) );
68
		add_filter( 'set-screen-option', array( $this, 'set_screen_option' ), 10, 3 );
69
	}
70
	/**
71
	 * Page Initialisation
72
	 *
73
	 * @method mass_init
74
	 * @return void
75
	 */
76
	public function mass_init() {
77
		$page = ( isset( $_GET['page'] ) && strpos( $_GET['page'] , 'mass_edit_interface3_att_set_' ) !== false ) ? $_GET['page'] : 'mass_edit_interface3_att_set_1';
78
		$this->hook = add_submenu_page(
79
			'edit.php?post_type=' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
80
			__( 'Mass product edit', 'wpshop' ),
81
			__( 'Mass product edit', 'wpshop' ),
82
			'manage_options',
83
			$page,
84
			array( $this, 'mass_interface' )
85
		);
86
		add_action( "load-{$this->hook}", array( $this, 'mass_interface_screen_option' ) );
87
		add_action( "admin_print_scripts-{$this->hook}", array( $this, 'scripts' ) );
88
		add_action( "admin_print_styles-{$this->hook}", array( $this, 'styles' ) );
89
	}
90
	/**
91
	 * Page content.
92
	 *
93
	 * @method mass_interface
94
	 * @return void Direct display.
95
	 */
96
	public function mass_interface() {
97
		$wp_list_table = $this->wp_list_table( $this->hook );
98
		$wp_list_table->prepare_items(); ?>
99
		<div class="wrap">
100
		<h1 class="wp-heading-inline"><?php
101
		echo esc_html( $this->post_type_object->labels->name );
102
		?></h1>
103
		<?php
104
		if ( current_user_can( $this->post_type_object->cap->create_posts ) ) {
105
			echo ' <a href="#addPost" class="page-title-action" data-nonce="' . esc_attr( wp_create_nonce( 'add_post-' . $this->hook ) ) . '">';
106
			echo esc_html( $this->post_type_object->labels->add_new ) . '</a>';
107
		}
108
		?>
109
		<hr class="wp-header-end">
110
		<form id="posts-filter" method="get">
111
		<?php $wp_list_table->views(); ?>
112
		<?php $wp_list_table->search_box( $this->post_type_object->labels->search_items, 'post' ); ?>
113
		<input type="hidden" name="page" value="<?php
114
		echo esc_attr( str_replace(
115
			"{$wp_list_table->screen->post_type}_page_",
116
			'',
117
			$wp_list_table->screen->id
118
		) ); ?>">
119
		<input type="hidden" name="post_type" value="<?php echo esc_attr( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT ); ?>">
120
		</form>
121
		<?php $wp_list_table->display(); ?>
122
		<table style="display:none;">
123
		 <tbody id="posts-add">
124
		  <tr id="inline-edit" class="inline-edit-row inline-edit-row-post <?php echo esc_attr( "inline-edit-{$this->post_type_object->name} quick-edit-row quick-edit-row-post inline-edit-{$this->post_type_object->name}" ); ?>" style="display: none">
125
		   <td colspan="<?php echo esc_attr( $wp_list_table->get_column_count() ); ?>" class="colspanchange">
126
			<fieldset class="inline-edit-col">
127
			 <legend class="inline-edit-legend"><?php echo esc_html( $this->post_type_object->labels->add_new ) ?></legend>
128
			 <div class="inline-edit-col">
129
		   <label>
130
			<span class="title"><?php esc_html_e( 'Title' ); ?></span>
131
			<span class="input-text-wrap"><input type="text" name="post_title" class="ptitle" value="" /></span>
132
		   </label>
133
			 </div>
134
			</fieldset>
135
			<p class="submit inline-edit-save">
136
			 <button type="button" class="button cancel alignleft"><?php esc_html_e( 'Cancel' ); ?></button>
137
			 <button type="button" class="button button-primary save alignright"><?php echo esc_html( $this->post_type_object->labels->add_new ); ?></button>
138
			 <span class="spinner"></span>
139
			 <span class="error" style="display:none"></span>
140
			 <br class="clear" />
141
			</p>
142
		   </td>
143
		  </tr>
144
		 </tbody>
145
		</table>
146
		</div>
147
		<?php
148
	}
149
	/**
150
	 * Notices hook (not used) & hidden_columns on page.
151
	 *
152
	 * @method mass_interface_screen_option
153
	 * @return void
154
	 */
155
	public function mass_interface_screen_option() {
156
		add_action( 'admin_notices', array( $this, 'ajax_admin_notice' ) );
157
		add_filter( 'default_hidden_columns', array( $this, 'hidden_columns' ), 10, 2 );
158
		$this->wp_list_table( $this->hook );
159
	}
160
	/**
161
	 * Instance WPS_Mass_List_Table.
162
	 *
163
	 * @method wp_list_table
164
	 * @param  string $screen Current screen.
165
	 * @return WPS_Mass_List_Table Table class.
166
	 */
167
	public function wp_list_table( $screen ) {
168
		if ( is_null( $this->wp_list_table ) ) {
169
			$this->post_type_object = get_post_type_object( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT );
170
			include_once( WPS_PDCT_MASS_INCLUDE_PATH . 'class-wps-mass-list-table.php' );
171
			$this->wp_list_table = new WPS_Mass_List_Table(
172
				array(
173
					'screen' => $screen,
174
					'exclude_attribute_codes' => $this->exclude_attribute_codes,
175
				)
176
			);
177
			$this->wp_list_table->screen->set_screen_reader_content(
178
				array(
179
					'heading_views'      => $this->post_type_object->labels->filter_items_list,
180
					'heading_pagination' => $this->post_type_object->labels->items_list_navigation,
181
					'heading_list'       => $this->post_type_object->labels->items_list,
182
				)
183
			);
184
			$class = get_class();
185
			$this->wp_list_table->screen->add_option(
186
				'per_page', array(
187
					'default' => 20,
188
					'option' => "{$class}_per_page",
189
				)
190
			);
191
		}
192
		return $this->wp_list_table;
193
	}
194
	/**
195
	 * Filter hidden columns, only without user preferences.
196
	 *
197
	 * @method hidden_columns
198
	 * @param  array     $hidden Given by hook, see "default_hidden_columns".
199
	 * @param  WP_Screen $screen Current screen.
200
	 * @return array Return of filter.
201
	 */
202
	public function hidden_columns( $hidden, $screen ) {
203
		$wp_list_table = $this->wp_list_table( $this->hook );
204
		if ( $screen === $wp_list_table->screen ) {
205
			$hidden = array_diff( array_flip( $wp_list_table->get_columns() ), $this->default_show_columns );
206
			$hidden[] = 'thumbnail';
207
		}
208
		return $hidden;
209
	}
210
	/**
211
	 * Hook for save per_page option.
212
	 *
213
	 * @method set_screen_option
214
	 * @param  string $string Given by WordPress.
215
	 * @param  string $option Actual option to save.
216
	 * @param  mixed  $value  Actual value to save.
217
	 */
218
	public function set_screen_option( $string, $option, $value ) {
219
		$class = get_class();
220
		if ( "{$class}_per_page" === $option ) {
221
			$value = (int) $value;
222
			if ( $value < 1 || $value > 999 ) {
223
				$string = false;
0 ignored issues
show
Unused Code introduced by
$string 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
			}
225
			return $value;
226
		}
227
		return $string;
228
	}
229
	/**
230
	 * Notice template, duplicated by JS for show notices.
231
	 *
232
	 * @method ajax_admin_notice
233
	 * @return void Direct display.
234
	 */
235
	public function ajax_admin_notice() {
236
		printf( '<div class="%1$s"><p></p></div>', esc_attr( 'hidden is-dismissible notice' ) );
237
	}
238
	/**
239
	 * Enqueue scripts.
240
	 *
241
	 * @method scripts
242
	 * @return void
243
	 */
244
	public function scripts() {
245
		wp_enqueue_script(
246
			'jquery_chosen_js',
247
			WPS_PDCT_MASS_CHOSEN_JS . 'chosen.jquery.min.js',
248
			array( 'jquery' ),
249
			true
250
		);
251
		wp_enqueue_script(
252
			'mass_interface3-ajax',
253
			WPS_PDCT_MASS_JS . 'wps-mass-interface3.js',
254
			array( 'jquery', 'jquery-form' ),
255
			true
256
		);
257
		wp_enqueue_media();
258
	}
259
	/**
260
	 * Enqueue styles.
261
	 *
262
	 * @method styles
263
	 * @return void
264
	 */
265
	public function styles() {
266
		wp_register_style( 'jquery_chosen_css', WPS_PDCT_MASS_CHOSEN_CSS . 'chosen.min.css' );
267
		wp_register_style( 'mass_interface3_css', WPS_PDCT_MASS_CSS . 'wps-mass-interface3.css' );
268
		wp_enqueue_style( 'jquery_chosen_css' );
269
		wp_enqueue_style( 'mass_interface3_css' );
270
		wp_deregister_style( 'wpshop_main_css' );
271
	}
272
	/**
273
	 * Ajax callback for new element.
274
	 *
275
	 * @method ajax_new
276
	 * @return void JSON with all elements to update.
277
	 */
278
	public function ajax_new() {
279
		add_filter( 'default_hidden_columns', array( $this, 'hidden_columns' ), 10, 2 );
280
		$wp_list_table = $this->wp_list_table( $_POST['screen'] );
281
		$wpshop_product_attribute = array();
282
		foreach ( $wp_list_table->request_items_columns() as $key_var => $var ) {
283
			$wpshop_product_attribute[ $var['data'] ][ $key_var ] = null;
284
		}
285
		$new_product_id = wp_insert_post(
286
			array(
287
				'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
288
				'post_status' => 'publish',
289
				'post_title' => $_POST['title'],
290
			)
291
		);
292
		if ( ! empty( $new_product_id ) ) {
293
			update_post_meta( $new_product_id, '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute_set_id', $wp_list_table->current_view );
294
			$product_class = new wpshop_products();
295
			$product_class->save_product_custom_informations(
296
				$new_product_id, array(
297
					'post_ID' => $new_product_id,
298
					'product_id' => $new_product_id,
299
					'wpshop_product_attribute' => $wpshop_product_attribute,
300
					'user_ID' => get_current_user_id(),
301
					'action' => 'editpost',
302
				)
303
			);
304
		} else {
305
			wp_die( 1 );
306
		}
307
		$class = get_class();
0 ignored issues
show
Unused Code introduced by
$class 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...
308
		$data = $wp_list_table->request( $new_product_id );
309
		$per_page = $wp_list_table->screen->get_option( 'per_page', 'option' );
310
		$wp_list_table->column_headers();
311
		$wp_list_table->items = true;
312
		ob_start();
313
		$wp_list_table->views();
314
		$subsubsub = ob_get_clean();
315
		ob_start();
316
		$wp_list_table->display_tablenav( 'top' );
317
		$tablenav_top = ob_get_clean();
318
		ob_start();
319
		$wp_list_table->display_tablenav( 'bottom' );
320
		$tablenav_bottom = ob_get_clean();
321
		ob_start();
322
		$wp_list_table->single_row( $data[0] );
323
		wp_send_json_success( array(
324
			'row' => ob_get_clean(),
325
			'per_page' => $per_page,
326
			'tablenav_top' => $tablenav_top,
327
			'tablenav_bottom' => $tablenav_bottom,
328
			'subsubsub' => $subsubsub,
329
		) );
330
	}
331
	/**
332
	 * Ajax callback for save selected elements.
333
	 *
334
	 * @method ajax_save
335
	 * @return void JSON with number of saved elements (not used).
336
	 */
337
	public function ajax_save() {
338
		$i = 0;
339
		$product_class = new wpshop_products();
340
		if ( ! empty( $_REQUEST['cb'] ) ) {
341
			foreach ( $_REQUEST['cb'] as $id ) {
342
				$id = intval( $id );
343
				if ( isset( $_REQUEST[ 'row_' . $id ]['thumbnail'] ) ) {
344
					intval( $_REQUEST[ 'row_' . $id ]['thumbnail'] );
345
					update_post_meta( $id, '_thumbnail_id', intval( $_REQUEST[ 'row_' . $id ]['thumbnail'] ) );
346
					unset( $_REQUEST[ 'row_' . $id ]['thumbnail'] );
347
				}
348
				if ( ! empty( $_REQUEST[ 'row_' . $id ] ) ) {
349
					$product_class->save_product_custom_informations(
350
						$id,
351
						array_merge(
352
							$_REQUEST[ 'row_' . $id ],
353
							array(
354
								'post_ID' => $id,
355
								'product_id' => $id,
356
								'user_ID' => get_current_user_id(),
357
								'action' => 'editpost',
358
							)
359
						)
360
					);
361
					$i++;
362
				}
363
			}
364
		}
365
		wp_send_json_success(
366
			array(
367
				'notice' => "{$i} rows has been updated",
368
			)
369
		);
370
	}
371
}
372