-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwcbog2018.php
189 lines (153 loc) · 5.47 KB
/
wcbog2018.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Plugin Name: WordCamp Bogotá 2018
* Plugin URI: https://github.com/smilingrobots/wordcamp-bogota-2018
* Description: Plugin de prueba para el Taller de introducción al desarrollo de plugins para WordPress.
* Version: 1.0
*/
function wcbog2018_generate_button() {
if ( ! is_user_logged_in() ) {
return '';
}
$user_id = get_current_user_id();
$likes = get_post_meta( get_the_ID(), '_wcbog2018_liked_by' );
$button = '';
if ( in_array( $user_id, $likes ) ) {
$button .= '<span class="dashicons dashicons-heart" style="color: red;"></span>';
} else {
$button .= '<span class="dashicons dashicons-heart"></span>';
$button .= '<a href="' . add_query_arg( 'wcbog2018-like', '1' ) . '">';
$button .= __( 'Like', 'wcbog2018' );
$button .= '</a>';
}
return $button;
}
function wcbog2018_generate_like_count() {
$likes = get_post_meta( get_the_ID(), '_wcbog2018_liked_by' );
$no_likes = count( $likes );
return sprintf(
_n(
'Este post tiene <b>%d</b> like',
'Este post tiene <b>%d</b> likes',
$no_likes,
'wcbog2018'
),
$no_likes
);
}
function wcbog2018_add_like_box_to_content( $content ) {
if ( ! is_singular() ) {
return $content;
}
$like_box = '';
$like_box .= '<p>';
$like_box .= wcbog2018_generate_button();
$like_box .= ' / ';
$like_box .= wcbog2018_generate_like_count();
$like_box .= '</p>';
return $like_box . $content;
}
add_filter( 'the_content', 'wcbog2018_add_like_box_to_content' );
function wcbog2018_maybe_save_like() {
if ( ! is_user_logged_in() ) {
return;
}
if ( empty( $_GET['wcbog2018-like'] ) ) {
return;
}
$post_id = get_the_ID();
$user_id = get_current_user_id();
$likes = get_post_meta( $post_id, '_wcbog2018_liked_by' );
if ( ! in_array( $user_id, $likes ) ) {
add_post_meta( $post_id, '_wcbog2018_liked_by', $user_id );
}
}
add_action( 'wp', 'wcbog2018_maybe_save_like' );
add_filter( 'wp_privacy_personal_data_exporters', 'wcbog2018_register_personal_data_exporters' );
add_filter( 'wp_privacy_personal_data_erasers', 'wcbog2018_register_personal_data_erasers' );
function wcbog2018_register_personal_data_exporters( $exporters ) {
$exporters['wordcamp-bogota-2018'] = array(
'exporter_friendly_name' => __( 'WordCamp Bogotá 2018', 'wcbog2018' ),
'callback' => 'wcbog2018_export_personal_data',
);
return $exporters;
}
function wcbog2018_register_personal_data_erasers( $erasers ) {
$erasers['wordcamp-bogota-2018'] = array(
'eraser_friendly_name' => __( 'WordCamp Bogotá 2018', 'wcbog2018' ),
'callback' => 'wcbog2018_erase_personal_data',
);
return $erasers;
}
function wcbog2018_export_personal_data( $email_address, $page = 1 ) {
$user = get_user_by( 'email', $email_address );
$posts_per_page = 50;
$posts = array();
if ( ! is_null( $user ) ) {
$posts = wcbog2018_get_posts_liked_by( $user, $posts_per_page, $page );
}
$exported_items = array();
foreach ( $posts as $post ) {
$exported_items[] = array(
'group_id' => "wcbog2018-liked-entries",
'group_label' => __( 'Publicaciones Favoritas', 'wcbog2018' ),
'item_id' => $post->ID,
'data' => array(
array(
'name' => __( 'Título', 'wcbog2018' ),
'value' => $post->post_title,
),
array(
'name' => __( 'URL', 'wcbog2018' ),
'value' => get_permalink( $post ),
),
),
);
}
return array(
'data' => $exported_items,
'done' => count( $posts ) < $posts_per_page,
);
}
function wcbog2018_get_posts_liked_by( $user, $posts_per_page, $page ) {
if ( is_null( $user ) ) {
return array();
}
return get_posts( array(
'post_type' => array(
'post',
'page',
),
'meta_key' => '_wcbog2018_liked_by',
'meta_value' => $user->ID,
'posts_per_page' => $posts_per_page,
'offset' => $posts_per_page * ( $page - 1 ),
) );
}
function wcbog2018_erase_personal_data( $email_address, $page = 1 ) {
$user = get_user_by( 'email', $email_address );
$posts_per_page = 50;
$posts = array();
if ( ! is_null( $user ) ) {
$posts = wcbog2018_get_posts_liked_by( $user, $posts_per_page, $page );
}
$items_removed = false;
$items_retained = false;
$messages = array();
foreach ( $posts as $post ) {
if ( delete_post_meta( $post->ID, '_wcbog2018_liked_by', $user->ID ) ) {
$items_removed = true;
continue;
}
$items_retained = true;
$message = __( 'Ocurrió un error intentando eliminar información personal asociada a la publicación {permalink}.', 'wcbog2018' );
$message = str_replace( '{permalink}', '<a href="' . get_permalink( $post ) . '">' . esc_html( $post->post_title ) . '</a>', $message );
$messages[] = $message;
}
return array(
'items_removed' => $items_removed,
'items_retained' => $items_retained,
'messages' => $messages,
'done' => count( $posts ) < $posts_per_page,
);
}