index.php
1.68 KB
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
<?php
/**
* Post picker control
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Stackable_Post_Picker' ) ) {
/**
* Premium icon settings page.
*/
class Stackable_Post_Picker {
/**
* Initialize
*/
function __construct() {
// Add our REST endpoint for getting all posts.
add_action( 'rest_api_init', array( $this, 'register_rest_route' ) );
}
/**
* Register our get posts endpoint.
*/
public function register_rest_route() {
register_rest_route( 'stackable/v2', '/editor_mode_get_all_posts', array(
'methods' => 'GET',
'callback' => array( $this, 'editor_mode_get_all_posts' ),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
'args' => array(
'search' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
) );
}
/**
* Get all posts for our post picker
*/
public function editor_mode_get_all_posts( $request ) {
// Get all posts.
$args = array(
'posts_per_page' => 50,
'post_type' => 'any',
);
$search = $request->get_param( 'search' );
if ( $search ) {
$args['s'] = $search;
}
$posts = get_posts( $args );
$posts_data = array();
foreach( $posts as $post ) {
$posts_data[] = (object) array(
'postId' => $post->ID,
'edit_url' => get_edit_post_link( $post->ID, 'url' ),
'url' => get_permalink( $post->ID ),
'title' => $post->post_title,
'postType' => get_post_type_object( $post->post_type )->labels->singular_name,
);
}
$response = new WP_REST_Response( $posts_data, 200 );
return $response;
}
}
new Stackable_Post_Picker();
}