-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathclass-copy-post.php
163 lines (118 loc) · 4.84 KB
/
class-copy-post.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
<?php
Writing_Helper()->add_helper( 'copy_post', new Writer_Helper_Copy_Post() );
class Writer_Helper_Copy_Post {
function init() {
add_action( 'wp_ajax_helper_search_posts', array( $this, 'add_ajax_search_posts_endpoint' ) );
add_action( 'wp_ajax_helper_get_post', array( $this, 'add_ajax_get_post_endpoint' ) );
add_action( 'wp_ajax_helper_stick_post', array( $this, 'add_ajax_stick_post_endpoint' ) );
add_action( 'wp_ajax_helper_record_stat', array( $this, 'add_ajax_record_stat_endpoint' ) );
// Add "Copy a Post" to the Posts menu.
add_action( 'admin_menu', array( $this, 'add_submenu_page' ) );
}
/**
* Add submenu links for each supported post type
*/
function add_submenu_page() {
$post_types = get_post_types();
foreach( $post_types as $post_type ) {
if( post_type_supports( $post_type, 'writing-helper' ) ) {
$post_type_obj = get_post_type_object( $post_type );
$submenu_page = 'edit.php';
if ( 'post' != $post_type ) {
$submenu_page .= '?post_type=' . $post_type;
}
if ( $post_type == 'post' ) {
$submenu_page_label = __( 'Copy a Post', 'writing-helper' );
} else if ( $post_type == 'page' ) {
$submenu_page_label = __( 'Copy a Page', 'writing-helper' );
} else {
$submenu_page_label = sprintf(
_x( 'Copy a %s', 'Copy a {post_type}', 'writing-helper' ),
$post_type_obj->labels->singular_name
);
}
$submenu_page_link = add_query_arg( 'cap#cap', '', str_replace( 'edit.php', '/post-new.php', $submenu_page ) );
add_submenu_page( $submenu_page, $submenu_page_label, $submenu_page_label, $post_type_obj->cap->edit_posts, $submenu_page_link );
}
}
}
function add_ajax_search_posts_endpoint() {
check_ajax_referer( 'writing_helper_nonce_' . get_current_blog_id(), 'nonce' );
if ( ! is_user_member_of_blog() ) {
exit;
}
$_REQUEST = stripslashes_deep( $_REQUEST );
$search_terms = trim( $_REQUEST['search'] );
$post_type = ! empty( $_REQUEST['post_type'] ) ?
sanitize_key( $_REQUEST['post_type'] ) : 'post';
Writing_Helper::json_return( self::get_candidate_posts( $post_type, $search_terms ) );
}
public static function get_candidate_posts( $post_type = 'post', $search_terms = '', $sticky = false ) {
$sticky_posts = get_option( 'copy_a_post_sticky_posts' );
$post_parameters = array(
'post_type' => $post_type,
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => 20,
'order_by' => 'date'
);
if ( $sticky && ! empty( $sticky_posts ) ) {
// Including only sticky posts as required
$post_parameters['post__in'] = (array) $sticky_posts;
$post_parameters['posts_per_page'] = 3;
} elseif ( empty( $search_terms ) && ! empty( $sticky_posts ) ) {
// Excluding sticky posts from results because they will be shown separately
$post_parameters['post__not_in'] = (array) $sticky_posts;
} else {
$post_parameters['s'] = $search_terms;
do_action( 'wh_copypost_searched_posts' );
}
return get_posts( $post_parameters );
}
function add_ajax_get_post_endpoint() {
global $wpdb, $current_blog;
check_ajax_referer( 'writing_helper_nonce_' . get_current_blog_id(), 'nonce' );
$_REQUEST = stripslashes_deep( $_REQUEST );
$post_id = (int) $_REQUEST['post_id'];
if ( ! current_user_can( 'read_post', $post_id ) ) {
exit;
}
if ( empty( $post_id ) )
die( '-1' );
$post = get_post( $post_id );
if ( 'post' == $post->post_type ) {
$post->post_tags = implode( ', ', (array) $wpdb->get_col( $wpdb->prepare( "SELECT slug FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON tt.term_id = t.term_id INNER JOIN {$wpdb->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ( 'post_tag' ) AND tr.object_id = %d", $post_id ) ) );
$post->post_categories = get_the_category( $post_id );
}
do_action( 'wh_copypost_copied_post', $post );
Writing_Helper::json_return( $post );
}
function add_ajax_stick_post_endpoint() {
check_ajax_referer( 'writing_helper_nonce_' . get_current_blog_id(), 'nonce' );
if ( ! is_user_member_of_blog() ) {
exit;
}
$_REQUEST = stripslashes_deep( $_REQUEST );
$post_id = (int) $_REQUEST['post_id'];
if ( empty( $post_id ) )
die( '-1' );
// Get sticky posts for the blog.
$sticky_posts = (array) get_option( 'copy_a_post_sticky_posts' );
$existing = array_search( $post_id, $sticky_posts );
if ( false !== $existing ) {
unset( $sticky_posts[$existing] );
} else if ( count( $sticky_posts ) > 2 ) {
array_pop( $sticky_posts );
}
array_unshift( $sticky_posts, $post_id );
update_option( 'copy_a_post_sticky_posts', $sticky_posts );
die( '1' );
}
function add_ajax_record_stat_endpoint() {
$_REQUEST = stripslashes_deep( $_REQUEST );
$stat = $_REQUEST['stat'];
if ( empty( $stat ) )
die( '-1' );
do_action( 'wh_copypost_ajax_stat', $stat );
die( '1' );
}
}