-
Notifications
You must be signed in to change notification settings - Fork 0
/
likes.php
149 lines (129 loc) · 4.31 KB
/
likes.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
<?php
namespace JIC\Likes;
add_action( 'init', __NAMESPACE__ . '\register_post_type', 10 );
add_action( 'save_post', __NAMESPACE__ . '\save_post', 10, 2 );
add_filter( 'wp_insert_post_data', __NAMESPACE__ . '\set_default_post_data', 10 );
add_filter( 'webmention_links', __NAMESPACE__ . '\filter_webmention_links', 10, 2 );
/**
* Register the post type used to track likes.
*/
function register_post_type() {
\register_post_type(
'like',
array(
'labels' => array(
'name' => 'Likes',
'singular_name' => 'Like',
),
'public' => true,
'menu_position' => 6,
'menu_icon' => 'dashicons-star-filled',
'supports' => array(
'comments',
'webmentions',
),
'register_meta_box_cb' => __NAMESPACE__ . '\register_meta_boxes',
'has_archive' => true,
'rewrite' => array(
'slug' => 'liked',
),
)
);
}
/**
* Register the meta boxes used to store data for likes.
*
* @param WP_Post $post The current like being edited.
*/
function register_meta_boxes( $post ) {
add_meta_box( 'like-data-primary', 'Like data', __NAMESPACE__ . '\display_meta_box', $post->post_type, 'normal', 'high' );
}
/**
* Display the meta box used to capture like data.
*
* @param WP_Post $post The current like being edited.
*/
function display_meta_box( $post ) {
$url = get_post_meta( $post->ID, 'mf2_like-of', true );
$url = is_array( $url ) ? array_pop( $url ) : $url;
$title = get_post_meta( $post->ID, 'like_title', true );
$notes = get_post_meta( $post->ID, 'like_notes', true );
wp_nonce_field( 'save-like-data', 'like_data_nonce' );
?>
<h3>URL</h3>
<input class="widefat" type="text" id="like-url" name="like_url" value="<?php echo esc_url( $url ); ?>" />
<h3>Title</h3>
<input class="widefat" type="text" id="like-title" name="like_title" value="
<?php
if ( '' !== $title ) {
echo esc_attr( $title ); }
?>
" />
<h3>Notes</h3>
<textarea class="widefat" rows="10" id="like-notes" name="like_notes"><?php echo esc_textarea( $notes ); ?></textarea>
<?php
}
/**
* Save meta data attached to a like.
*
* @param int $post_id The ID of the current like.
* @param WP_Post $post The post object representing the current like.
*/
function save_post( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( 'auto-draft' === $post->post_status ) {
return;
}
if ( ! isset( $_POST['like_data_nonce'] ) || ! wp_verify_nonce( $_POST['like_data_nonce'], 'save-like-data' ) ) {
return;
}
if ( isset( $_POST['like_url'] ) && '' !== $_POST['like_url'] ) {
update_post_meta( $post_id, 'mf2_like-of', esc_url_raw( $_POST['like_url'] ) );
} elseif ( isset( $_POST['like_url'] ) && '' === $_POST['like_url'] ) {
delete_post_meta( $post_id, 'mf2_like-of' );
}
if ( isset( $_POST['like_title'] ) && '' !== $_POST['like_title'] ) {
update_post_meta( $post_id, 'like_title', sanitize_text_field( $_POST['like_title'] ) );
} elseif ( isset( $_POST['like_title'] ) && '' === $_POST['like_title'] ) {
delete_post_meta( $post_id, 'like_title' );
}
if ( isset( $_POST['like_notes'] ) && '' !== $_POST['like_notes'] ) {
update_post_meta( $post_id, 'like_notes', wp_kses_post( $_POST['like_notes'] ) );
} elseif ( isset( $_POST['like_notes'] ) && '' === $_POST['like_notes'] ) {
delete_post_meta( $post_id, 'like_notes' );
}
}
/**
* Modify the defaults stored with a new like.
*
* @param array $post Current post data to store for the like.
* @return array $post Modified post data to store.
*/
function set_default_post_data( $post ) {
if ( 'like' === $post['post_type'] && '' === $post['post_name'] && 'Auto Draft' === $post['post_title'] ) {
$post['post_title'] = 'Like';
$post['post_name'] = gmdate( 'YmdHis' );
}
return $post;
}
/**
* Add the URL associated with this like to the list of URLs to be
* pinged with a webmention.
*
* @param array $urls List of URLs to ping.
* @param int $post_id The current post ID.
* @return array $urls Modified list of URLs to ping.
*/
function filter_webmention_links( $urls, $post_id ) {
$post = get_post( $post_id );
if ( 'like' === $post->post_type ) {
$url = get_post_meta( $post_id, 'mf2_like-of', true );
$url = is_array( $url ) ? array_pop( $url ) : $url;
if ( '' !== $url ) {
$urls[] = $url;
}
}
return $urls;
}