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

WPS_EAV_Revisions   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B save_post() 0 11 5
A wp_save_post_revision_post_has_changed() 0 3 1
A wp_restore_post_revision() 0 11 1
A wp_post_revision_fields() 0 47 2
A wp_post_revision_field() 0 21 2
1
<?php
2
class WPS_EAV_Revisions {
3
	public static $created_revisions = array();
4
	public function __construct() {
5
		add_action( 'save_post', array( $this, 'save_post' ), 100, 2 );
6
		add_action( 'wp_restore_post_revision', array( $this, 'wp_restore_post_revision' ), 10, 2 );
7
		add_filter( '_wp_post_revision_fields', array( $this, 'wp_post_revision_fields' ), 10, 2 );
8
	}
9
	public function save_post( $post_id, $post ) {
10
		if ( false === wp_is_post_revision( $post_id ) ) {
11
			if ( ( ! isset( self::$created_revisions[ $post_id ] ) || true !== self::$created_revisions[ $post_id ] ) && post_type_supports( $post->post_type, 'revisions' ) ) {
12
				self::$created_revisions[ wp_is_post_revision( $post_id ) ] = true;
13
				add_filter( 'wp_save_post_revision_post_has_changed', array( $this, 'wp_save_post_revision_post_has_changed' ), 10, 3 );
14
				wp_save_post_revision( $post_id );
15
			}
16
		} else {
17
			self::$created_revisions[ wp_is_post_revision( $post_id ) ] = true;
18
		}
19
	}
20
	public function wp_save_post_revision_post_has_changed( $post_has_changed, $last_revision, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_has_changed 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 $last_revision 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 $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...
21
		return true;
22
	}
23
	public function wp_restore_post_revision( $post_id, $revision_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id 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 $revision_id 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...
24
		/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% 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...
25
		$post     = get_post( $post_id );
26
		$revision = get_post( $revision_id );
27
		$meta = get_metadata( 'post', $revision->ID, 'foo', true );
28
		if ( false === $meta ) {
29
			delete_post_meta( $post_id, 'foo' );
30
		} else {
31
			update_post_meta( $post_id, 'foo', $meta );
32
		}*/
33
	}
34
	public function wp_post_revision_fields( $fields, $post ) {
35
		global $wpdb;
36
		$wpsdb_attribute = WPSHOP_DBT_ATTRIBUTE;
37
		$wpsdb_attribute_set = WPSHOP_DBT_ATTRIBUTE_DETAILS;
0 ignored issues
show
Unused Code introduced by
$wpsdb_attribute_set 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...
38
		$wpsdb_unit = WPSHOP_DBT_ATTRIBUTE_UNIT;
0 ignored issues
show
Unused Code introduced by
$wpsdb_unit 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...
39
		$wpsdb_values_decimal = WPSHOP_DBT_ATTRIBUTE_VALUES_DECIMAL;
40
		$wpsdb_values_datetime = WPSHOP_DBT_ATTRIBUTE_VALUES_DATETIME;
41
		$wpsdb_values_integer = WPSHOP_DBT_ATTRIBUTE_VALUES_INTEGER;
42
		$wpsdb_values_varchar = WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR;
43
		$wpsdb_values_text = WPSHOP_DBT_ATTRIBUTE_VALUES_TEXT;
44
		$wpsdb_values_options = WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS;
45
		$datas = $wpdb->get_results( $wpdb->prepare(
46
			"SELECT attr.id,
47
			attr.code,
48
			attr.frontend_label,
49
			GROUP_CONCAT(
50
				IFNULL( val_dec.value,
51
					IFNULL( val_dat.value,
52
						IFNULL( val_tex.value,
53
							IFNULL( val_var.value,
54
								IFNULL( val_opt.value,
55
									val_int.value
56
								)
57
							)
58
						)
59
					)
60
				)
61
				SEPARATOR ', '
62
			) as data
63
			FROM wp_posts p
64
			LEFT JOIN {$wpsdb_attribute} attr ON attr.status = 'valid'
65
			LEFT JOIN {$wpsdb_values_decimal} val_dec ON val_dec.attribute_id = attr.id AND val_dec.entity_id = p.ID
66
			LEFT JOIN {$wpsdb_values_datetime} val_dat ON val_dat.attribute_id = attr.id AND val_dat.entity_id = p.ID
67
			LEFT JOIN {$wpsdb_values_integer} val_int ON val_int.attribute_id = attr.id AND val_int.entity_id = p.ID
68
			LEFT JOIN {$wpsdb_values_text} val_tex ON val_tex.attribute_id = attr.id AND val_tex.entity_id = p.ID
69
			LEFT JOIN {$wpsdb_values_varchar} val_var ON val_var.attribute_id = attr.id AND val_var.entity_id = p.ID
70
			LEFT JOIN {$wpsdb_values_options} val_opt ON val_opt.attribute_id = attr.id AND val_opt.id = val_int.value
71
			WHERE p.ID = %d
72
			GROUP BY attr.code",
73
			$post['ID']
74
		), ARRAY_A );
75
		foreach ( $datas as $data ) {
76
			$fields[ $data['id'] ] = $data['frontend_label'];
77
			add_filter( "_wp_post_revision_field_{$data['id']}", array( $this, 'wp_post_revision_field' ), 10, 4 );
78
		}
79
		return $fields;
80
	}
81
	public function wp_post_revision_field( $value, $field, $revision, $fromto ) {
0 ignored issues
show
Unused Code introduced by
The parameter $fromto 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...
82
		global $wpdb;
83
		$wpsdb_histo = WPSHOP_DBT_ATTRIBUTE_VALUES_HISTO;
84
		$wpsdb_values_options = WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS;
85
		$result = $wpdb->get_var( $wpdb->prepare(
86
			"SELECT IFNULL( val_opt.label,
87
				histo.value
88
			)
89
			FROM {$wpsdb_histo} histo
90
			LEFT JOIN {$wpsdb_values_options} val_opt ON val_opt.attribute_id = histo.attribute_id AND val_opt.id = histo.value
91
			WHERE histo.creation_date >= %s
92
			AND histo.attribute_id = %d
93
			AND histo.entity_id = %d
94
			ORDER BY histo.creation_date ASC LIMIT 1",
95
			$revision->post_date,
96
			$field,
97
			$revision->post_parent
98
		) );
99
		$result = ( '0' === $result ) ? null : $result;
100
		return $result;
101
	}
102
}
103
new WPS_EAV_Revisions();
104
/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% 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...
105
OLD Version
106
add_meta_box('wpshop_histo_attrs', __( 'Historic attributes', 'wpshop' ), function( $post ) {
107
	global $wpdb;
108
	$limit = 40;
109
	$count_rows = $wpdb->prepare( 'SELECT COUNT(value_id) FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_HISTO . ' WHERE entity_id = %d', $post->ID );
110
	$max_page = ceil( $wpdb->get_var( $count_rows ) / $limit );
111
	$current_page = absint( isset( $_GET['paged_historic'] ) ? $_GET['paged_historic'] : 1 );
112
	$query = $wpdb->prepare('SELECT *, histo.value as brut_value FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_HISTO . ' AS histo
113
	LEFT JOIN ' . WPSHOP_DBT_ATTRIBUTE . ' AS attr ON histo.attribute_id = attr.id
114
	LEFT JOIN wp_wpshop__attribute_value_options AS opt ON histo.attribute_id = opt.attribute_id AND histo.value = opt.id
115
	WHERE histo.entity_id = %d ORDER BY histo.creation_date DESC LIMIT %d OFFSET %d', $post->ID, $limit, ( ( $current_page - 1 ) * $limit ) );
116
	$histo = $wpdb->get_results( $query );
117
	$histo_array = array();
118
	foreach ( $histo as $row ) {
119
		$histo_array[ $row->creation_date_value ][] = $row;
120
	}
121
	foreach ( $histo_array as $date => $values ) {
122
		?>
123
		<fieldset style="border:1px solid #eee; margin-bottom: 20px">
124
			<legend style="margin-left: 10px; font-weight: bold"><?php printf( __( '%s ago' ), human_time_diff( strtotime( $date ), current_time( 'timestamp' ) ) ); ?> :</legend>
125
			<?php foreach ( $values as $value ) { ?>
126
				<div style="margin-left: 10px; margin-bottom: 8px"><?php echo $value->frontend_label; ?>: <br><input type="text" value="<?php echo isset( $value->value ) ? $value->value : $value->brut_value; ?>" disabled></div>
127
			<?php } ?>
128
		</fieldset>
129
		<?php
130
	}
131
	echo paginate_links( array(
132
		'base' => '%_%',
133
		'format' => '?paged_historic=%#%',
134
		'current' => $current_page,
135
		'total' => $max_page,
136
	) );
137
}, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, 'side', 'default');*/
138