forked from danielbachhuber/collections
/
collections.php
279 lines (223 loc) · 7.29 KB
/
collections.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
Plugin Name: Collections
Version: 0.1-alpha
Description: Create and use collections of WordPress posts.
Author: Daniel Bachhuber
Author URI: http://danielbachhuber.com
Plugin URI: https://github.com/danielbachhuber/collections
Text Domain: collections
Domain Path: /languages
*/
class Collections {
private static $instance;
private $did_register_assets = false;
private $customizer;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Collections;
self::$instance->load();
}
return self::$instance;
}
/**
* Load plugin components
*/
private function load() {
$this->require_files();
$this->setup_actions();
$this->customizer = Collection_Customizer::get_instance();
}
/**
* Require plugin files
*/
private function require_files() {
require_once dirname( __FILE__ ). '/template-tags.php';
require_once dirname( __FILE__ ) . '/php/class-collection.php';
require_once dirname( __FILE__ ) . '/php/class-post-collection.php';
require_once dirname( __FILE__ ) . '/php/class-post-collection-widget.php';
require_once dirname( __FILE__ ) . '/php/class-collection-customizer.php';
}
/**
* Set up plugin actions
*/
private function setup_actions() {
add_action( 'init', array( $this, 'action_init_register_post_type' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'action_enqueue_scripts_register' ) );
add_action( 'wp_ajax_collections_add_post_search', array( $this, 'handle_ajax_add_post_search' ) );
add_action( 'widgets_init', array( $this, 'action_widgets_init' ) );
}
/**
* Register the post type used to store collections behind the scenes
*/
public function action_init_register_post_type() {
register_post_type( 'collection', array(
'hierarchical' => false,
'public' => false,
'show_in_nav_menus' => false,
'show_ui' => false,
'supports' => array(),
'has_archive' => false,
'query_var' => false,
'rewrite' => false,
'labels' => array(
'name' => __( 'Collections', 'collections' ),
'singular_name' => __( 'Collection', 'collections' ),
'all_items' => __( 'Collections', 'collections' ),
'new_item' => __( 'New Collection', 'collections' ),
'add_new' => __( 'Add New', 'collections' ),
'add_new_item' => __( 'Add New Collection', 'collections' ),
'edit_item' => __( 'Edit Collection', 'collections' ),
'view_item' => __( 'View Collection', 'collections' ),
'search_items' => __( 'Search Collections', 'collections' ),
'not_found' => __( 'No Collections found', 'collections' ),
'not_found_in_trash' => __( 'No Collections found in trash', 'collections' ),
'parent_item_colon' => __( 'Parent Collection', 'collections' ),
'menu_name' => __( 'Collections', 'collections' ),
),
) );
}
/**
* Register our scripts and styles
*/
public function action_enqueue_scripts_register() {
wp_register_script( 'collections', $this->get_url( 'js/collections.js' ), array( 'jquery' ) );
wp_register_style( 'collections', $this->get_url( 'css/collections.css' ) );
$this->did_register_assets = true;
}
/**
* Handle a request to search for posts to add
*/
public function handle_ajax_add_post_search() {
// @todo allow collection by collection permissions
if ( ! current_user_can( 'edit_theme_options' )
|| ! wp_verify_nonce( $_GET['nonce'], 'collections-add-post-search' ) ) {
$this->send_json_error( __( "You probably shouldn't do this.", 'collections' ) );
}
$query_args = array(
'post_type' => apply_filters( 'collections_supported_post_types', array( 'post' ) ),
'post_status' => 'publish',
);
if ( ! empty( $_GET['s'] ) ) {
$query_args['s'] = sanitize_text_field( $_GET['s'] );
}
$query = new WP_Query( $query_args );
$posts = array();
foreach( $query->posts as $post ) {
$posts[] = $this->get_post_for_json( $post );
}
$this->send_json_success( '', array( 'posts' => $posts ) );
}
/**
* Register the widgets
*/
public function action_widgets_init() {
register_widget( 'Post_Collection_Widget' );
}
/**
* Enqueue all of the necessary assets for the plugin
*/
public function enqueue_assets() {
global $wp_customize;
if ( ! $this->did_register_assets ) {
$this->action_enqueue_scripts_register();
}
wp_enqueue_script( 'collections' );
wp_enqueue_style( 'collections' );
wp_enqueue_style( 'collection-control', Collections()->get_url( 'css/collection-control.css' ), array( 'collections' ) );
wp_enqueue_script( 'collection-control', Collections()->get_url( 'js/collection-control.js' ), array( 'jquery', 'jquery-ui-sortable', 'collections' ) );
if ( is_admin() && ! has_action( 'admin_footer', array( $this, 'render_add_post_modal' ) ) ) {
add_action( 'admin_footer', array( $this, 'render_templates' ) );
}
if ( ! empty( $wp_customize ) && ! has_action( 'customize_controls_print_footer_scripts', array( $this, 'render_add_post_modal' )) ) {
add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_templates' ) );
}
}
/**
* Render the HTML associated with the add post modal
*/
public function render_templates() {
echo $this->get_view( 'add-post-modal' );
echo $this->get_view( 'single-collection-item' );
}
/**
* Get a post as its JSON representation
*
* @param mixed $post
*/
public function get_post_for_json( $post ) {
if ( is_numeric( $post ) ) {
$post = get_post( $post );
}
return array(
'ID' => $post->ID,
'post_title' => $post->post_title,
'permalink' => get_permalink( $post->ID ),
'edit_link' => get_edit_post_link( $post->ID, 'json' ),
'user_can_edit' => current_user_can( 'edit_post', $post->ID ),
);
}
/**
* Send a JSON success message
*
* @param string $message
* @param mixed $data
*/
private function send_json_success( $message = '', $data = array() ) {
header( 'Content-Type: application/json' );
echo json_encode( array( 'status' => 'success', 'message' => $message, 'data' => $data ) );
exit;
}
/**
* Send a JSON error message
*
* @param string $message
*/
private function send_json_error( $message = '' ) {
header( 'Content-Type: application/json' );
echo json_encode( array( 'status' => 'error', 'message' => $message ) );
exit;
}
/**
* Get the URL for a plugin asset
*
* @param string $path
* @return string
*/
public function get_url( $path = '' ) {
return plugins_url( $path, __FILE__ );
}
/**
* Whether or not we're currently in the Customizer
*
* @return bool
*/
public function is_customizer_preview() {
global $wp_customize;
return ( isset( $wp_customize ) && $wp_customize->is_preview() ) ;
}
/**
* Get the rendering of a given view
*
* @param string $view
* @param array $vars
* @return string
*/
public function get_view( $view, $vars = array() ) {
$file_path = dirname( __FILE__ ) . '/views/' . $view . '.tpl.php';
if ( ! file_exists( $file_path ) ) {
return '';
}
ob_start();
extract( $vars );
include $file_path;
return ob_get_clean();
}
}
/**
* Load the plugin
*/
function Collections() {
return Collections::get_instance();
}
add_action( 'plugins_loaded', 'Collections' );