Completed
Push — master ( 431b9c...260ddf )
by
unknown
12:45
created

Mass_Interface3::set_current_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 8
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-' . sanitize_title( get_class() ) ) ) . '">';
106
			echo esc_html( $this->post_type_object->labels->add_new ) . '</a>';
107
		}
108
		?>
109
		<hr class="wp-header-end">
110
		<?php echo '<input type="hidden" id="hook" value="' . esc_attr( $this->hook ) . '">'; ?>
111
		<form id="posts-filter" method="get">
112
		<?php $wp_list_table->views(); ?>
113
		<?php $wp_list_table->search_box( $this->post_type_object->labels->search_items, 'post' ); ?>
114
		<input type="hidden" name="page" value="<?php
115
		echo esc_attr( str_replace(
116
			"{$wp_list_table->screen->post_type}_page_",
117
			'',
118
			$wp_list_table->screen->id
119
		) ); ?>">
120
		<input type="hidden" name="post_type" value="<?php echo esc_attr( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT ); ?>">
121
		</form>
122
		<?php $wp_list_table->display(); ?>
123
		<table style="display:none;">
124
		 <tbody id="posts-add">
125
		  <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">
126
		   <td colspan="<?php echo esc_attr( $wp_list_table->get_column_count() ); ?>" class="colspanchange">
127
			<fieldset class="inline-edit-col">
128
			 <legend class="inline-edit-legend"><?php echo esc_html( $this->post_type_object->labels->add_new ) ?></legend>
129
			 <div class="inline-edit-col">
130
		   <label>
131
			<span class="title"><?php esc_html_e( 'Title' ); ?></span>
132
			<span class="input-text-wrap"><input type="text" name="post_title" class="ptitle" value="" /></span>
133
		   </label>
134
			 </div>
135
			</fieldset>
136
			<p class="submit inline-edit-save">
137
			 <button type="button" class="button cancel alignleft"><?php esc_html_e( 'Cancel' ); ?></button>
138
			 <button type="button" class="button button-primary save alignright"><?php echo esc_html( $this->post_type_object->labels->add_new ); ?></button>
139
			 <span class="spinner"></span>
140
			 <span class="error" style="display:none"></span>
141
			 <br class="clear" />
142
			</p>
143
		   </td>
144
		  </tr>
145
		 </tbody>
146
		</table>
147
		</div>
148
		<?php
149
	}
150
	/**
151
	 * Notices hook (not used) & hidden_columns on page.
152
	 *
153
	 * @method mass_interface_screen_option
154
	 * @return void
155
	 */
156
	public function mass_interface_screen_option() {
157
		add_action( 'admin_notices', array( $this, 'ajax_admin_notice' ) );
158
		add_filter( 'default_hidden_columns', array( $this, 'hidden_columns' ), 10, 2 );
159
		$this->wp_list_table( $this->hook );
160
	}
161
	/**
162
	 * Instance WPS_Mass_List_Table.
163
	 *
164
	 * @method wp_list_table
165
	 * @param  string $screen Current screen.
166
	 * @return WPS_Mass_List_Table Table class.
167
	 */
168
	public function wp_list_table( $screen ) {
169
		if ( is_null( $this->wp_list_table ) ) {
170
			$this->post_type_object = get_post_type_object( WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT );
171
			include_once( WPS_PDCT_MASS_INCLUDE_PATH . 'class-wps-mass-list-table.php' );
172
			$this->wp_list_table = new WPS_Mass_List_Table(
173
				array(
174
					'screen' => $screen,
175
					'exclude_attribute_codes' => $this->exclude_attribute_codes,
176
				)
177
			);
178
			$this->wp_list_table->screen->set_screen_reader_content(
179
				array(
180
					'heading_views'      => $this->post_type_object->labels->filter_items_list,
181
					'heading_pagination' => $this->post_type_object->labels->items_list_navigation,
182
					'heading_list'       => $this->post_type_object->labels->items_list,
183
				)
184
			);
185
			$class = sanitize_title( get_class() );
186
			$this->wp_list_table->screen->add_option(
187
				'per_page', array(
188
					'default' => 20,
189
					'option' => "{$class}_per_page",
190
				)
191
			);
192
		}
193
		return $this->wp_list_table;
194
	}
195
	/**
196
	 * Filter hidden columns, only without user preferences.
197
	 *
198
	 * @method hidden_columns
199
	 * @param  array     $hidden Given by hook, see "default_hidden_columns".
200
	 * @param  WP_Screen $screen Current screen.
201
	 * @return array Return of filter.
202
	 */
203
	public function hidden_columns( $hidden, $screen ) {
204
		$wp_list_table = $this->wp_list_table( $this->hook );
205
		if ( $screen === $wp_list_table->screen ) {
206
			$hidden = array_diff( array_flip( $wp_list_table->get_columns() ), $this->default_show_columns );
207
			$hidden[] = 'thumbnail';
208
		}
209
		return $hidden;
210
	}
211
	/**
212
	 * Hook for save per_page option.
213
	 *
214
	 * @method set_screen_option
215
	 * @param  string $string Given by WordPress.
216
	 * @param  string $option Actual option to save.
217
	 * @param  mixed  $value  Actual value to save.
218
	 */
219
	public function set_screen_option( $string, $option, $value ) {
220
		$class = sanitize_title( get_class() );
221
		if ( "{$class}_per_page" === $option ) {
222
			$value = (int) $value;
223
			if ( $value < 1 || $value > 999 ) {
224
				$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...
225
			}
226
			return $value;
227
		}
228
		return $string;
229
	}
230
	/**
231
	 * Notice template, duplicated by JS for show notices.
232
	 *
233
	 * @method ajax_admin_notice
234
	 * @return void Direct display.
235
	 */
236
	public function ajax_admin_notice() {
237
		printf( '<div class="%1$s"><p></p></div>', esc_attr( 'hidden is-dismissible notice' ) );
238
	}
239
	/**
240
	 * Enqueue scripts.
241
	 *
242
	 * @method scripts
243
	 * @return void
244
	 */
245
	public function scripts() {
246
		wp_enqueue_script(
247
			'jquery_chosen_js',
248
			WPS_PDCT_MASS_CHOSEN_JS . 'chosen.jquery.min.js',
249
			array( 'jquery' ),
250
			true
251
		);
252
		wp_enqueue_script(
253
			'mass_interface3-ajax',
254
			WPS_PDCT_MASS_JS . 'wps-mass-interface3.js',
255
			array( 'jquery', 'jquery-form' ),
256
			true
257
		);
258
		wp_enqueue_media();
259
	}
260
	/**
261
	 * Enqueue styles.
262
	 *
263
	 * @method styles
264
	 * @return void
265
	 */
266
	public function styles() {
267
		wp_register_style( 'jquery_chosen_css', WPS_PDCT_MASS_CHOSEN_CSS . 'chosen.min.css' );
268
		wp_register_style( 'mass_interface3_css', WPS_PDCT_MASS_CSS . 'wps-mass-interface3.css' );
269
		wp_enqueue_style( 'jquery_chosen_css' );
270
		wp_enqueue_style( 'mass_interface3_css' );
271
		wp_deregister_style( 'wpshop_main_css' );
272
	}
273
	/**
274
	 * Change default url for set_url_scheme(). See pagination & get_views. Impossible to re-use set_url_sheme inner.
275
	 *
276
	 * @method set_current_url
277
	 * @param  string        $url    Given url.
278
	 * @param  string | null $scheme Scheme to give $url. Currently 'http', 'https', 'login', 'login_post', 'admin', 'relative', 'rest', 'rpc', or null.
279
	 */
280
	public function set_current_url( $url, $scheme ) {
0 ignored issues
show
Unused Code introduced by
The parameter $url 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...
Unused Code introduced by
The parameter $scheme 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...
281
		/*
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...
282
		Remove_filter( 'set_url_scheme', array( $this, 'set_current_url' ), 10 );
283
		$url = set_url_sheme( esc_url( $_POST['current_url'] ) );
284
		add_filter( 'set_url_scheme', array( $this, 'set_current_url' ), 10, 2 );
285
		*/
286
		return $_POST['current_url'];
287
	}
288
	/**
289
	 * Ajax callback for new element.
290
	 *
291
	 * @method ajax_new
292
	 * @return void JSON with all elements to update.
293
	 */
294
	public function ajax_new() {
295
		check_ajax_referer( 'add_post-' . sanitize_title( get_class() ) );
296
		add_filter( 'default_hidden_columns', array( $this, 'hidden_columns' ), 10, 2 );
297
		add_filter( 'set_url_scheme', array( $this, 'set_current_url' ), 10, 2 );
298
		$wp_list_table = $this->wp_list_table( sanitize_title( $_POST['hook'] ) );
299
		$wpshop_product_attribute = array();
300
		foreach ( $wp_list_table->request_items_columns() as $key_var => $var ) {
301
			$wpshop_product_attribute[ $var['data'] ][ $key_var ] = null;
302
		}
303
		$new_product_id = wp_insert_post(
304
			array(
305
				'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT,
306
				'post_status' => 'publish',
307
				'post_title' => sanitize_text_field( $_POST['title'] ),
308
			)
309
		);
310
		if ( ! empty( $new_product_id ) ) {
311
			update_post_meta( $new_product_id, '_' . WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT . '_attribute_set_id', $wp_list_table->current_view );
312
			$product_class = new wpshop_products();
313
			$product_class->save_product_custom_informations(
314
				$new_product_id, array(
315
					'post_ID' => $new_product_id,
316
					'product_id' => $new_product_id,
317
					'wpshop_product_attribute' => $wpshop_product_attribute,
318
					'user_ID' => get_current_user_id(),
319
					'action' => 'editpost',
320
				)
321
			);
322
		} else {
323
			wp_die( 1 );
324
		}
325
		$data = $wp_list_table->request( $new_product_id );
326
		$per_page = $wp_list_table->screen->get_option( 'per_page', 'option' );
327
		$wp_list_table->column_headers();
328
		$wp_list_table->items = true;
329
		ob_start();
330
		$wp_list_table->views();
331
		$subsubsub = ob_get_clean();
332
		ob_start();
333
		$wp_list_table->display_tablenav( 'top' );
334
		$tablenav_top = ob_get_clean();
335
		ob_start();
336
		$wp_list_table->display_tablenav( 'bottom' );
337
		$tablenav_bottom = ob_get_clean();
338
		ob_start();
339
		$wp_list_table->single_row( $data[0] );
340
		wp_send_json_success( array(
341
			'row' => ob_get_clean(),
342
			'per_page' => $per_page,
343
			'tablenav_top' => $tablenav_top,
344
			'tablenav_bottom' => $tablenav_bottom,
345
			'subsubsub' => $subsubsub,
346
		) );
347
	}
348
	/**
349
	 * Ajax callback for save selected elements.
350
	 *
351
	 * @method ajax_save
352
	 * @return void JSON with number of saved elements (not used).
353
	 */
354
	public function ajax_save() {
355
		check_ajax_referer( 'bulk-save-mass-edit-interface-3' );
356
		$i = 0;
357
		$product_class = new wpshop_products();
358
		if ( ! empty( $_REQUEST['cb'] ) ) {
359
			foreach ( $_REQUEST['cb'] as $id ) {
360
				$id = intval( $id );
361
				if ( isset( $_REQUEST[ 'row_' . $id ]['thumbnail'] ) ) {
362
					intval( $_REQUEST[ 'row_' . $id ]['thumbnail'] );
363
					update_post_meta( $id, '_thumbnail_id', intval( $_REQUEST[ 'row_' . $id ]['thumbnail'] ) );
364
					unset( $_REQUEST[ 'row_' . $id ]['thumbnail'] );
365
				}
366
				if ( ! empty( $_REQUEST[ 'row_' . $id ] ) ) {
367
					$product_class->save_product_custom_informations(
368
						$id,
369
						array_merge(
370
							$_REQUEST[ 'row_' . $id ],
371
							array(
372
								'post_ID' => $id,
373
								'product_id' => $id,
374
								'user_ID' => get_current_user_id(),
375
								'action' => 'editpost',
376
							)
377
						)
378
					);
379
					$i++;
380
				}
381
			}
382
		}
383
		wp_send_json_success(
384
			array(
385
				'notice' => "{$i} rows has been updated",
386
			)
387
		);
388
	}
389
}
390