-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdewp-planet-feed.php
315 lines (273 loc) · 7.99 KB
/
dewp-planet-feed.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
defined( 'ABSPATH' ) or die( 'You know better.' );
/**
* Plugin Name: DEWP Planet Feed
* Description: Generates a custom feed “dewp-planet” for posts. Adds a checkbox to the Publish meta box in order to explicitly add a post to that custom feed.
* Version: 1.0.0
* Author: dewp#planet team
* Author URI: https://dewp.slack.com/messages/planet/
* Plugin URI: https://github.com/deworg/dewp-planet-feed
* GitHub Plugin URI: https://github.com/deworg/dewp-planet-feed
* License: GNU General Public License v3
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Release Asset: true
*
* Based upon DS_wpGrafie_WP_Planet_Feed class by Dominik Schilling (@ocean90).
* https://github.com/ocean90/wpgrafie-theme/blob/master/classes/class-ds-wpgrafie-wp-planet-feed.php
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* Set marker for activation.
* @since 0.1
*/
register_activation_hook(
__FILE__,
array( 'DEWP_Planet_Feed', 'activation' )
);
/**
* Flush rewrite rules.
* @since 0.1
*/
register_deactivation_hook(
__FILE__,
array( 'DEWP_Planet_Feed', 'deactivation' )
);
class DEWP_Planet_Feed {
/**
* Allowed post types.
* @since 0.1
* @var array Default: post
*/
public static $post_types;
/**
* Required capability.
* @since 0.1
* @var string Default: publish_posts
*/
public static $capability;
/**
* Plugin activation state.
* @var bool|string FALSE|activating|activated
*/
public static $maybe_activation;
/**
* Populate default values and initiate.
*/
public function __construct() {
self::$maybe_activation = get_option( 'wp_planet_feed__activated', false );
add_action( 'init', array( __CLASS__, 'init' ) );
// Register the meta.
add_action( 'init', array( __CLASS__, 'register_show_in_feed_meta' ) );
}
/**
* Set marker for activation.
* @since 0.1
* @return void
*/
public static function activation() {
update_option( 'wp_planet_feed__activated', 'activating' );
}
/**
* Flush rewrite rules and delete option on deactivation.
* @since 0.1
* @return void
*/
public static function deactivation() {
flush_rewrite_rules();
delete_option( 'wp_planet_feed__activated', 'deactivated' );
}
/**
* Initialize plugin.
* @since 0.1
* @return void
*/
public static function init() {
/**
* Filterable post types.
* @since 0.1
*/
self::$post_types = apply_filters(
'wp_planet_feed__post_types',
array( 'post' )
);
/**
* Filterable capability to enable checkbox.
* @since 0.1
*/
self::$capability = apply_filters(
'wp_planet_feed__capability',
'publish_posts'
);
// Add custom feed.
add_feed( 'dewp-planet', array( __CLASS__, 'feed_template' ) );
if ( 'activating' === self::$maybe_activation ) {
// Not recommended, but it’s only once during activation.
flush_rewrite_rules();
update_option( 'wp_planet_feed__activated', 'activated' );
}
// Publish post actions.
add_action( 'post_submitbox_misc_actions', array( __CLASS__, 'add_checkbox' ), 9 );
add_action( 'save_post', array( __CLASS__, 'save_checkbox' ) );
// Enqueue admin scripts and styles.
add_action(
'admin_enqueue_scripts',
array( __CLASS__, 'admin_enqueue_scripts' )
);
// Enqueue Gutenberg script.
add_action( 'enqueue_block_editor_assets', array( __CLASS__, 'enqueue_block_editor_assets' ) );
// Get feed content.
add_action( 'pre_get_posts', array( __CLASS__, 'feed_content' ) );
}
/**
* Register the meta data and make it visible via REST API.
* @since 0.5.0
*/
public static function register_show_in_feed_meta() {
foreach ( self::$post_types as $post_type ) {
register_post_meta(
$post_type,
'wpf_show_in_dewp_planet_feed',
[
'type' => 'boolean',
'description' => 'Ob der Beitrag im DEWP-Planet erscheinen soll oder nicht.',
'single' => true,
'show_in_rest' => true,
]
);
}
}
/**
* Load feed template.
* @since 0.1
* @return void
*/
public static function feed_template() {
load_template( ABSPATH . WPINC . '/feed-rss2.php' );
}
/**
* Add checkbox to Publish Post meta box.
* @since 0.1
* @return void
*/
public static function add_checkbox() {
global $post;
// Bail if post type is not allowed.
if ( ! in_array( $post->post_type, self::$post_types ) ) {
return false;
}
// Check user capability. Not bailing, though, on purpose.
$maybe_enabled = current_user_can( self::$capability );
// This actually defines whether post will be listed in our feed.
$value = get_post_meta( $post->ID, '_wpf_show_in_dewp_planet_feed', true );
ob_start();
include trailingslashit( dirname( __FILE__ ) ) . 'inc/pub-section.php';
$pub_section = ob_get_clean();
echo $pub_section;
}
/**
* Register and enqueue admin scripts and styles.
* @since 0.3
* @param string $hook Current admin page
* @return return Current admin page
*/
public static function admin_enqueue_scripts( $hook ) {
if ( 'post.php' !== $hook && 'post-new.php' !== $hook )
return;
$file_data = get_file_data( __FILE__, array( 'v' => 'Version' ) );
$assets_url = trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/';
// CSS
wp_register_style(
'dewp-planet-post', $assets_url . 'css/post.css',
array( 'wp-admin' ),
$file_data['v']
);
wp_enqueue_style( 'dewp-planet-post' );
return $hook;
}
/**
* Enqueue assets for the Gutenberg editor.
* @since 0.5.0
*/
public static function enqueue_block_editor_assets() {
if ( ! in_array( get_post_type(), self::$post_types ) ) {
return;
}
$file_data = get_file_data( __FILE__, array( 'v' => 'Version' ) );
$assets_url = trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/';
wp_enqueue_script( 'dewp-planet-functions', $assets_url . 'js/functions.js', array( 'wp-blocks', 'wp-element', 'wp-edit-post' ), $file_data['v'] );
}
/**
* Save option value to post meta.
* @since 0.1
* @param integer $post_id ID of current post
* @return integer ID of current post
*/
public static function save_checkbox( $post_id ) {
if ( empty( $post_id ) || empty( $_POST['post_ID'] ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( absint( $_POST['post_ID'] ) !== $post_id ) {
return $post_id;
}
if ( ! in_array( $_POST['post_type'], self::$post_types ) ) {
return $post_id;
}
if ( ! current_user_can( self::$capability ) ) {
return $post_id;
}
if ( empty( $_POST['dewp-planet__add-to-feed'] ) ) {
delete_post_meta( $post_id, '_wpf_show_in_dewp_planet_feed' );
} else {
add_post_meta( $post_id, '_wpf_show_in_dewp_planet_feed', 1, true );
}
return $post_id;
}
/**
* Set feed content.
* @since 0.1
* @param object $query WP_Query object
* @return object Altered WP_Query object
*/
public static function feed_content( $query ) {
// Bail if $posts_query is not an object or of incorrect class.
if ( ! is_object( $query ) || ( 'WP_Query' !== get_class( $query ) ) ) {
return;
}
// Bail if filters are suppressed on this query.
if ( $query->get( 'suppress_filters' ) ) {
return;
}
// Bail if this is not the Shire.
if ( ! $query->is_feed( 'dewp-planet' ) ) {
return;
}
$query->set( 'post_type', self::$post_types );
$meta_query = array(
'relation' => 'OR',
array(
'key' => 'wpf_show_in_dewp_planet_feed',
'value' => 1,
),
array(
'key' => '_wpf_show_in_dewp_planet_feed',
'value' => 1,
),
);
$query->set( 'meta_query', $meta_query );
return $query;
}
}
// Hallo!
new DEWP_Planet_Feed();